Reading Numbers from a text file into an array, then compute the average of the numbers!

May 11, 2013 at 2:55am
I need to read numbers into an array from a file and then compute the average and check how many numbers are above a certain quota and below a certain quota. I need to use all functions for these computations. I have the average function done but when I check to see if it computes correctly I get 0 as my value.

I just want to check that my numbers are into the array correctly and that my average computes correctly before starting my next function. There are 12 float numbers in each row of the text file and 7 rows of numbers.

Thanks,

TECK

Here is my code so far:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void displayHeader();
float readIntoSales(ifstream &fp, float &Numsales, float sales[], float SIZE);
float computeAverage(float sales[], float SIZE, float &sum, float Numsales);
void displayResults(float totalAverage);

int main()
{
  float Numsales, salesInfo, totalAverage, sum = 0, results, SalesAverage = 0;
  const int SIZE = 12;
  float targetNumber[] = {23.0, 33.1, 21.0, 23.5, 54.0, 34.3, 35.0, 45.0, 56.3, \
45.6, 34.0, 55.0};
  float sales[SIZE];
  bool meet[SIZE];


  ifstream fp;
  fp.open("sales.dat");
  if(!fp)
    {
      cout <<"Error opening the file!" <<endl;
    }

  displayHeader();

  while(fp.eof()==false)
    {
      salesInfo = readIntoSales(fp, Numsales, sales, SIZE);
      totalAverage = computeAverage(sales, SIZE, sum, Numsales);

    }
 displayResults(totalAverage);
}

void displayHeader()
{

  cout<< "Store Statistics"<<endl;
  cout<<"                   "<<endl;
  cout<<"  Dept  "  <<  "  Average  "  <<  "  Above  "  <<  "  Below  "  <<  "  \
Performance  " <<endl;
  cout<<"----------------------------------------------------------------" <<end\
l;

}


float readIntoSales(ifstream &fp, float &Numsales, float sales[], float SIZE)
{

   for(int i = 0; i < SIZE; i++)
     {
       fp>>Numsales;
       sales[i] = Numsales;
     }
}

float computeAverage(float sales[], float SIZE, float &sum, float Numsales)
{
  float avg =0;
  for(int i = 0; i < SIZE; i++)
    {
      avg += sales[i];
    }
 return sum/SIZE;
}




void displayResults(float totalAverage)
{
  cout <<totalAverage <<endl;


}
May 11, 2013 at 3:33am
This dosn't even compile because readIntoSales dosn't return a value. The problem is you don't assign a value to sum on line 70.

try this
63
64
65
66
67
68
69
70
71
float computeAverage(float sales[], float SIZE, float &sum, float Numsales)
{

  for(int i = 0; i < SIZE; i++)
    {
      sum += sales[i];
    }
 return sum/SIZE;
}


Last edited on May 11, 2013 at 3:46am
May 11, 2013 at 11:19pm
could you explain this further

i have on line 56-60 where im reading in values from the text file, how to i pass that array thats holding those values to the average to compute it?

here is the code im talking about.

1
2
3
4
5
for(int i = 0; i < SIZE; i++)
     {
       fp>>Numsales;
       sales[i] = Numsales;
     }

Topic archived. No new replies allowed.