Question #37215

Write a program that reads integer numbers from a file (number of them is unknown). Then, your program should print
Maximum number.
Total of even numbers.
Average of all numbers.
How many of them greater than the average.

Expert's answer

Answer on Question#37215 - Programming, C++

Write a program that reads integer numbers from a file (number of them is unknown). Then, your program should print

Maximum number.

Total of even numbers.

Average of all numbers.

How many of them greater than the average.

Solution.

#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char * argv[])
{
    char ch[]="45 65 45 78 98 9 8 78 9 8 s"; //Here you can connect a file
    //like this
    // ifstream in;
    // in.open("D:\\filename.txt")
    // char ch;
    // in.get(ch);
    char chArray[5];
    int max=0;
    double median=0;
    int evennumbers=0,moreMedn=0;
    int count=0;
    int i=0,j=-1;
    int inum;
    while(1)
    {
        j++;i=0;
        while(ch[j]!=' ')
        {
            chArray[i]=ch[j];
            if(ch[j]==115){break;}
            j++; i++;
        }
        if(ch[j]==115){break;}
        inum = atoi(chArray);
        memset(chArray,0,5);
        if(inum>max){max=inum;}
        if (inum%2 == 0) {evennumbers++; }
        median += inum;
        count++;
    }
    median = median / count;
    j = -1;
    while (1)
    {
        j++; i = 0;
        while (ch[j] != ' ')
        {
            chArray[i] = ch[j];
            if (ch[j] == 115) {break;}
            j++; i++;
        }
        if (ch[j] == 115) {break;}
        inum = atoi(chArray);
        memset(chArray, 0, 5);
        if (inum > median) {moreMedn++; }
    }
    cout << max << endl;
    cout << evennumbers << endl;
    cout << median << endl;
    cout << moreMedn << endl;
    system("pause");
    return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS