Average Function in Array

4. Write a third function that will determine the average value in the array. You will create this function from scratch, including the function definition and prototype. Make sure the average is sent back to main and printed from there. Before writing the code for this function, write the comments.

5. In main, call FillFunction, call PrintArray, and call the function that finds the average. Print the average from main.


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
  #include <iostream>
#include <cmath>
 using namespace std;
 int FillFunction(double []);
 void PrintArray(const double [], int);


 const int MAXSIZE =100;

 int main()
 {
 double array1[100];
 int count;


 count = FillFunction(array1);
 cout<<"There is "<<count<<" numbers in this array"<<endl;

 PrintArray( array1, count); //function call to function that will print the array

 cout<<"The average of all these numbers is "


 return 0;
 }

 //Write the function here to read in numbers into the array. The array is called myarray.
 //add comments describing what this function does, what is passed to it, what it sends back, etc.
 int FillFunction(double myarray[]){
 using namespace std;
 cout << "Enter no more than 100 positive numbers \n";
 int next, index = 0;
 cin >> next; 

 while (( next >= 0 ) && ( index < MAXSIZE )){
 myarray[index]= next;
 index++;
 cin >> next;
 }

 return index;
 }

 //Write the new function here along with comments
 //add comments describing what this function does, what is passed to it, what it sends back, etc.
 void PrintArray(const double a1[], int size)
 {
 for ( int i =0; i < size; i++)
 cout << a1[i] << " ";
 cout<< endl;
 }


not sure what to do, any help would be appreciated
Here is how i will write a function that will compute the average of all the elements of an array:
1
2
3
4
5
6
7
8
9
10
long double findAverage ( double array[], int size )
{
    long double return_value = 0.0; // i forgot to initialize it to 0.0 earlier

    for ( int i = 0; i < size; i++ ) {
        return_value += array[ i ];
    }

    return ( return_value / size );
}

Last edited on
Thank you very much for the reply. As soon as I have access to a computer I will try it out. Kind of seems like I have the hard part done, but I just can't seem to figure out this last part. I will post my code, for what it's worth, when I get it working for anyone else who might be struggling with something like this.
Last edited on
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
#include <iostream>
#include <cmath>
 using namespace std;
 int FillFunction(double []);
 void PrintArray(const double [], int);
 double findAverage (double array[], int size, double returnvalue);


 const int MAXSIZE =100;

 int main()
 {
 double array1[100];
 int count;


 count = FillFunction(array1);
 cout<<"There is "<<count<<" numbers in this array"<<endl;

 PrintArray(array1, count); //function call to function that will print the array

 cout<<"The average of all these numbers is " << endl;
findAverage (double array[], int size, double returnvalue);



 return 0;
 }

 //Write the function here to read in numbers into the array. The array is called myarray.
 //add comments describing what this function does, what is passed to it, what it sends back, etc.
 int FillFunction(double myarray[]){
 using namespace std;
 cout << "Enter no more than 100 positive numbers \n";
 int next, index = 0;
 cin >> next; 

 while (( next >= 0 ) && ( index < MAXSIZE )){
 myarray[index]= next;
 index++;
 cin >> next;
 }

 return index;
 }

 //Write the new function here along with comments
 //add comments describing what this function does, what is passed to it, what it sends back, etc.
 void PrintArray(const double a1[], int size)
 {
 for ( int i =0; i < size; i++)
 cout << a1[i] << " ";
 cout<< endl;
 }

 double findAverage (double array[], int size, double returnvalue)
{ 

    for(int i = 0; i < size; i++ ) {
        returnvalue += array[ i ];
    }

    return (returnvalue / size );
}


not sure if im clueless or if there's a stupid mistake im missing haha, everything is going fine until the program gets to the point where its suppose to calc the avg but nothing prints out. :/

(Write a third function that will determine the average value in the array)
Last edited on
You should call the function inside cout so that the return value of the function will be printed, and also your way of calling the function is wrong :

you should call it like this :
 
cout<< "The average of all these numbers is " << findAverage ( array1, count ) << endl;


move double returnvalue parameter in findAverage function inside the function since it is not really needed as a parameter

NOTE
And learn to indent your code, it makes you and anyone who will read your code to clearly understand the structure :

Example :

your fillFunction function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int FillFunction ( double myarray[] )
{
    int next, index = 0;

    cout << "Enter no more than 100 positive numbers \n";
    cin >> next; 
    
    while ( ( next >= 0 ) && ( index < MAXSIZE ) ) {
        myarray [ index ] = next;
	index++;
	cin >> next;
    }

    return index;
}

Last edited on
I gave more or less an identical answer in a previous thread:
http://www.cplusplus.com/forum/beginner/114796/#msg626663

Please don't post multiple threads for the same question.
Last edited on
Topic archived. No new replies allowed.