So for the past few hours I've been getting stuck on one homework assignment and it's mainly because I missed the beginning of class one day and that was the day I should have been there early. We were learning about arrays and how to use them inside a code, but now I have to use the array to read from a file and I'm not exactly sure on how to go about even trying to do that.
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
ifstream input;
input.open("numbers.txt");
int count = 0;
double total = 0;
double i;
double avg;
while (input >> i)
{
if (i != -1)
{
count++;
total += i;
avg = total / count;
}
}
cout << "There were " << count << " numbers." << endl;
cout << "The total was " << total << endl;
cout << "The average was " << avg << endl;
//cout << "There were "<< "blandk" <<" numbers that were above the average";
//Above is just a place holder for the main part of what I have to use the arrays for.
input.close();
return 0;
}
So far this is all I could come up with. We had a similar assignment earlier in the week and he told us to copy that code and use it as the basis for this weeks assignment.
Any information or pieces of code anyone has is greatly appreciated.
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
ifstream input("numbers.txt");
constint size = 500;
int array[size];
int count = 0;
int total;
double avg = total / count;
int above = 0;
while (input >> array[count])
{
if(array[count] > -1)
{
count++;
}
while (array[count] > avg)
{
above++;
}
total += array[count];
}
input.close();
cout << "There were " << count << " numbers." << endl;
cout << "The total was " << total << endl;
cout << "The average was " << avg << endl;
cout << "There were " << above << " numbers that were above the average." << endl;
//array[0] returns first number in series, array[count-1] returns last number in a series
return 0;
}
What I would tend to do is to leave the code which reads from the file into the array as simple and uncluttered as possible. After that stage has completed, do whatever processing you like with the resulting array.
But for your immediate problem, this looks like an infinite loop to me:
1 2 3 4
while (array[count] > avg)
{
above++;
}
if the condition array[count] > avg is true, then it will remain true, and the loop will never terminate.
If you can do that, then you can accumulate the total. When you have the total, you can find the average then loop through the array a second time to get other statistics - don't be afraid to loop through the array more than once.
What I'm suggesting here is to concentrate on writing code which is clear and easy to understand, rather than trying to squeeze out the last bit of efficiency by trying to do everything all in one go.
//-------- Stage 1. get the data --------
int count = 0;
while (input >> array[count])
{
count++;
}
//-------- Stage 2. get the total --------
double total = 0;
for (int i=0; i<count; i++)
{
total += array[i];
}
// calculate the average
double average = total / count;
//-------- Stage 3. get other statistics --------
// initialise whatever variables are needed here
for (int i=0; i<count; i++)
{
// do something with array[i] and other variables here
}
// print out the results
// etc.
Actually you could accumulate the total in the first loop. At this stage I'm suggesting to do it afterwards, partly for practice with using the array, and partly to avoid breaking the code which was working correctly.
In any case you can't do anything else until you have the average. And you can't get the average until all of the data has been read, so that you know count and total. So the count of number above average etc. cannot possibly be in the initial loop.