-2

How to code Average of the numbers in case 5? I already done a convert string to int. How can I make average of the numbers from text file named "dane.txt"?

case 5: //srednia
                {
                    fstream plik;
                    plik.open("dane.txt");
                    while(!plik.eof())
                    {
                        plik>>a;
                        licznik++;
                        if((licznik)%3 == 0)
                        {
                            string str = a;
                            int m = atoi(str.c_str());
                            
                        }
                    }
                    plik.close();
                    break;
                }
  • Can you write a function that can calculate the average of an array of integers? – RoQuOTriX Jan 27 '21 at 08:01
  • I can but I dont know how to make array with numbers from textfile –  Jan 27 '21 at 08:02
  • Ok that sounds good. So your problem is to create a dynamic array of integers in C++. Do you know what the std::vector is? – RoQuOTriX Jan 27 '21 at 08:03
  • @RoQuOTriX You don;t need an array to calculate an average, you just need to sum the numbers and keep a count of how many there are. – john Jan 27 '21 at 08:04
  • I dont know. I haven't had it in school yet –  Jan 27 '21 at 08:05
  • @john you are right of course. Did not think this mathematically through... I wanted that the OP thinks about separating of his concerns – RoQuOTriX Jan 27 '21 at 08:05
  • @john How can I make this? –  Jan 27 '21 at 08:06
  • `for(int val; plik >> val; ) { ... }` would be a good start. – paddy Jan 27 '21 at 08:07
  • @KarolPawlak I made a wrong assumption about the problem itself. As John said to calculate the average you need the sum of all numbers and the total count of the numbers. So you need to calculate two new variables, the sum and the total count of numbers in the text file – RoQuOTriX Jan 27 '21 at 08:07
  • [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – molbdnilo Jan 27 '21 at 08:15

1 Answers1

1

Add two variables to keep track of the count of numbers, and the total from adding up all the numbers (apologies for the English names).

Then at the end of the loop you can divide the total by the count and that is your average.

                int total = 0;
                int count = 0;
                ...
                while(...)
                {
                        ...
                        string str = a;
                        total += atoi(str.c_str());
                        ++count;
                }
                cout << "average is " << (double)total/count << '\n';
john
  • 85,011
  • 4
  • 57
  • 81
  • The `(double)` conversion is not needed if the requirement is for an integral result for the average, with rounding being applied. – Peter Jan 27 '21 at 09:06