Calculate average of array and print elements.

Use function decomposition.

-To prompt user to enter value and assign it to correct position in array
-calculate the average of an array
-display all element.
-display average.

For some reason, i keep getting error and it doesn't compile. I use Dev-C++

Here is my code:

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
#include <iostream>
#include <iosmanip>
#include <math.h>

using namespace std;

const int SIZE = 5;
const int LINE = 4;

//function prototypes
void getData(int arr[]);
void print(const int arr[]);
double findAverage (int arr[]);
void printAverage(double average);

int main()
{
    int arr[SIZE];
    double average;
    
    getData(arr);
    average = findAverage(arr);
    print(arr);
    printAverage(average);
    
    cout <<endl;
    
    system("PAUSE");
    return 0;
}
// Prompt user to enter value and then assign value to array.
void getData(int arr[])
{
     for (int i=0;i<SIZE;i++)
     {
         cout <<"Enter a value #"<< i+1<<endl<<" :";
         cin>>arr[i];
     }
}

// Calculate the average.
double findAverage (int arr[])
{
      int sum = 0;
      double average;
      
      for (int i=0; i<SIZE;i++)
      {
          sum += arr[i];
          average = static_cast<double>(sum)/SIZE;
      }
      return average;
}

// Print each element of the array by printing 4 element per line
void print(const int arr[])
{
     int i;
     int j;
     cout <<"Array Element Are: "<<endl<<endl;
     
     for (i=0, j=0; i<SIZE;i++,j++)           // for-loop to print all element
     {                                         
         if (j==LINE)                         // Print 4 element per line
         {
             j=0;
             cout <<endl;
         }
         cout <<setw(3)<<arr [i];             // print element, space 3.
     }
}
// Print average.
void printAverage(double average)
{
     cout << " The average is : " <<average<<endl;   
}


My error is on line 69. I don't see anything wrong with it.
If i take out setw(3), it doesnt get any error but for some reason it doesnt run. Any idea or suggestion? Possible i did something wrong there?

Thank in advance.
The header is called <iomanip> not <iosmanip>
Note also you should be using <cmath> instead of <math.h>. The .h headers are depreciated.

Last edited on
Thank you.
I usually use visual studio and it tell me right away if i type something wrong, but that not the case. hehe. I will have to be more careful next time and thank for the advice.
Topic archived. No new replies allowed.