Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // udari.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include "pch.h"
- #include <iostream> //standard input output file. (input and output ganna programs waladi me library eka thiyenna oone)
- #include<iomanip> //this is also a part of the input output library but for massives
- #include<fstream> //also like iostream but to read and write text files
- #include<string>
- #include<sstream>
- #include<Windows.h>
- using namespace std;
- const int n = 20;
- int main()
- {
- string fileName;
- string stringElement;
- ifstream inputFile;
- stringstream streamstr;
- int count = 0;
- do
- {
- cout << "Enter the file name: " << endl;
- getline(cin, fileName);//console eke type karapu file name eka "filename" kiyala variable ekakata dagannawa
- inputFile.open(fileName);//ios::in -> allow(разрещать) to read the file
- if (inputFile)
- break; //exit from the loop
- cout << " file: " << fileName << " Unable to Access! " << endl;
- } while (!inputFile.is_open());
- //ws - манипулятор, он пропускает пробельные символы
- //Пропускаем пробельные символы, если они есть
- inputFile >> ws; //Проверяем файл на пустоту
- if (inputFile.eof())
- {
- inputFile.close();
- cout << "The file is empty!" << endl;
- system("pause");
- return 0;
- }
- while (!inputFile.eof())
- {
- int x;
- inputFile >> stringElement >> ws; //помещаем элемент stringElement в поток stringstream
- streamstr.str(stringElement); //Извлекаем элемент из потока stringstream в переменную x для проверки на коректность
- streamstr.seekg(0) >> x;
- //Если был установлен failbit то извлекаеммый символ некоректный
- //Если был установлен goodbit и НЕ БЫЛ установлен eofbit то ЗНАЧИТ ЭТО ЧАСТНЫЙ СЛУЧАЙ (когда элемент в потока имеет вид(пример): 3g)
- if (streamstr.fail() || (streamstr.good() && !streamstr.eof()))
- {
- inputFile.close();
- cout << "Invalid Element! '" << stringElement << "'[position in File : " << count + 1 << "]" << endl;
- system("pause");
- return 0;
- }
- count++;
- }
- //Выделение памяти
- int*array;
- try
- {
- array = new int[count];
- }
- catch (...)
- {
- inputFile.close();
- cout << "Memory allocation error!" << endl;
- system("pause");
- return 0;
- }
- inputFile.clear();
- inputFile.seekg(0);
- for (int i = 0; i < count; i++)
- inputFile >> array[i];
- inputFile.close(); //Close the file
- //Display the numbers read:
- cout << "The numbers are:" << endl;
- for (int i = 0; i < count; i++)
- {
- cout << array[i] << " "<<endl;
- }
- cout << endl;
- cout << "------------------------------------------------------------------------------------------------------------------------" <<endl;
- cout << "-------------------------------------------------------Result-----------------------------------------------------------" << endl;
- //Task
- int sum = 0, avg = 0;
- for (int i = 0; i < n; i++)
- {
- sum = sum + array[i];
- }
- avg = (int)sum / n;
- int min = array[0];
- int index = 0;
- for (int i = 0; i < n; i++)
- {
- if (array[i] < min)
- {
- min = array[i];
- index = 1;
- }
- }
- for (int i = index+1; i < n; i++)
- {
- array[i] = avg;
- }
- for (int i = 0; i < n; i++)
- {
- cout << (int)array[i] << ' ' << endl;
- }
- delete[]array; //freeing the memory
- system("pause");
- return 0;
- }
- /*
- int avg = 0, sum = 0;
- int min = array[0];
- int k = 0;
- for (int i = 0; i < sizeof(array); ++i)
- {
- sum += array[i];
- if (min < array[i])
- {
- min = array[i];
- k = i;
- }
- array[k + 1] = sum / sizeof(array);
- cout << array[i] << ' ';
- cout << endl;
- }
- */
- /*for (int i = 0; i < count; i++)
- {
- sum = sum + array[i];
- }
- avg = sum / count;
- int Lmin = array[1];
- for (int j = 1; j < count; j++)
- {
- if (array[j] < Lmin)
- {
- Lmin = array[j];
- }
- if (Lmin == array[j])
- {
- for (int k = Lmin; k < count; k++)
- {
- array[k] = avg;
- }
- }
- for (int m = 0; m < count; m++)
- cout << array[m] << ' ';
- cout << endl;*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement