Obtaning the Average of An Array

I have to find the average of an array, but I can't get the program to run...I'm mostly following the sum function.

I have tried using return average_array;. That doesn't work.

The instructions are:
You will be adding a
function that returns an integer value after being passed an
array and the number of array elements. The value it should
return is the average of the values that are within the array.
Since we are dealing with integer values within this lab, we
will use integer division. The average would be the total
divided by the number of elements in the array and it will
truncate to a whole number and that is OK! The name of the
function should be: average_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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  
// Headers and Other Technical Items

#include <iostream>
#include <fstream>         // For file I/O 
#include <iomanip>         // For setw()
using namespace std;

// Function Prototypes

int  count_file_values(char input_filename[]);
void load_array_from_file(char input_filename[], int things[], int array_size);
void display_array(int things[], int array_size);
int  sum_array(int things[], int array_size);
int  average_array(int things [], int array_size); 
void pause(void);

// Variables

// Place the Demo_Farm_Acres_Input.txt file in a folder on your
// drive.  We suggest that you create a folder named: 
// Data_Files on the root level of your C: (hard disk) drive
// or flash drive as appropriate. 

                           // File Specifications for the input file
                           // A file specificatoin includes a 
                           // drive, path, filename and file extention 
char     data_filename[]  = "C:\\Data_Files\\Demo_Farm_Acres_Input.txt";

//******************************************************
// main
//******************************************************

int main(void)
  {
  int record_count = count_file_values(data_filename);
  int acres[record_count];
  load_array_from_file(data_filename, acres, record_count);
  cout << "The individual farm acerages are: \n";
  display_array(acres, record_count);
  cout << "\n\nThe total acerage is: " << sum_array(acres, record_count);
  pause();

  return 0;
  }

//******************************************************
// count_file_values
//******************************************************

int count_file_values(char input_filename[])
  {
  fstream  inData;           // device token for the input stream
  double   next_value;
  int      number_of_values = 0;
  
  inData.open(input_filename, ios::in);        //Open input file
  if (!inData)
    {
    cout << "\n\nError opening input data file: " << input_filename << "\n\n";
    pause();
    exit(EXIT_FAILURE);
    }
  // Get data values and count them

  while (inData >> next_value)
    {
    number_of_values++;
    }

  // Close the files
  inData.close();                              //Close input file

  return number_of_values;
  }

//******************************************************
// load_array_from_file
//******************************************************

void load_array_from_file(char input_filename[], int things[], int array_size)
  {
  fstream  inData;           // device token for the input stream

  inData.open(input_filename, ios::in);        //Open input file
  if (!inData)
    {
    cout << "\n\nError opening input data file: " << input_filename << "\n\n";
    pause();
    exit(EXIT_FAILURE);
    }
  else
    {
    for (int i = 0; i < array_size; i++)   // Load the array
      {
      inData >> things[i];
      }
    inData.close();
    }
  return;
  }

//******************************************************
// display_array
//******************************************************

void display_array(int things[], int array_size)
  {

  cout << "\n************************************";
  for (int i = 0; i < array_size; i++)
    {
    cout << "\nOffset: " << setw(2) << i;
    cout << " Memeber: " << setw(2) << i+1;
    cout << " value is: " << setw(3) << things[i];
    }
  cout << "\n************************************";

  return;
  }

//******************************************************
// sum_array
//******************************************************

int sum_array(int things[], int array_size)
  {
  int  total = 0;         // Accumulator

  for (int i = 0; i < array_size; i++)
    {
    total += things[i];
    }
  return total;
  }
  
//******************************************************
// average_array
//******************************************************

int average_array(int things[], int array_size)
   {
    
   int total= 0; 
   
   for (int i = 0; i < array_size; i++)  
    {
   total / array_size ;                   
    }                
   return average;                    
   }                  

//error   `average' undeclared (first use this function)  
// (Each undeclared identifier is reported only once for each function it appears in.) 

//******************************************************
// pause
//******************************************************

void pause(void)
  {
  cout << "\n\n";
  system("PAUSE");
  cout << "\n\n";
  return;
  }

//******************************************************
// End of Program
//******************************************************
Last edited on
Line 148 does not assign its result to any variable so the compiler will warn or complain. The "average" in line 150 is undeclared, and is an error as you have seen.

But overall "average_array()" does not seem to be going in the right direction. Better to use the "sum_array()" already developed and divide by the number of elements to get the average.

So change
1
2
3
4
5
6
7
8
9
10
11
int average_array(int things[], int array_size)
   {
    
   int total= 0; 
   
   for (int i = 0; i < array_size; i++)  
    {
   total / array_size ;                   
    }                
   return average;                    
   }


to something like:

1
2
3
4
int average_array(int things[], int array_size)
{
  return sum_array(things, array_size)/array_size;
}


Note that the division is integer, and will truncate. Refer http://www.cplusplus.com/articles/1UCRko23/ for rounding.

Also, the function "average_array()" is not used anywhere in your code that I can see.
Last edited on
I tried writing it as...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19


//******************************************************
// average_array
//******************************************************

int average_array(int things[], int array_size)
   {
    
   int total= 0; 
   
   for (int i = 0; i < array_size; i++)  
    {
   average_array = total / array_size ;                   
    }                
   return average_array;                  
   }                  



But then, I get these errors:

assignment of function `int average_array(int*, int)'
cannot convert `int' to `int ()(int*, int)' in assignment
invalid conversion from `int (*)(int*, int)' to `int'
The program is using the average_array function, right? (Since that's part of the assignment.)
The compiler is complaining about "average_array" which is a function. However you are assigning an integer to it in line 14. Totally wrong!
To the OP: why are you trying to use the name of your function as if it were a variable?
Topic archived. No new replies allowed.