Numbers in a file (using arrays)

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream> 
#include <fstream>
using namespace 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.
Basic code to read from a file into an array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream> 
#include <fstream>
using namespace std;

int main()
{
    ifstream input("numbers.txt");  
    
    const int size = 500;         // set size generously large
    int array[size];              // array size must be a constant
    int count = 0;

    while (input >> array[count])
    {
        count++;                
    }
    input.close(); 
    
    cout << "There were " << count << " numbers." << endl;
    cout << "first is " << array[0]       << endl;
    cout << "last is "  << array[count-1] << endl;;
     
    return 0;
}

See tutorial: http://www.cplusplus.com/doc/tutorial/arrays/
Well I've replaced and added some things and the code stopped working. It's not printing anything now. It's an empty box.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream> 
#include <fstream>
using namespace std;

int main()
{
  ifstream input("numbers.txt");  
    
  const int 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.
Last edited on
So what would be the best way to fix the infinite loop? And how do I not make this mistake in the future?
So what would be the best way to fix the infinite loop?

Well, it wasn't immediately clear what that loop was supposed to be doing.

I think first you should practice iterating through the array. For example, to display all the numbers in the array you might do this:
1
2
3
4
for (int i=0; i<count; i++)
{
    cout << array[i] << ' ';
}

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.
Do I need to keep everything inside the while (input >> array[count]) code until I cout or can I put things outside of that loop and it still work?
Perhaps I didn't make my thought processes clear. What I had in mind was something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    //-------- 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.

Topic archived. No new replies allowed.