d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Arrays In C++ > Beginning Arrays
Add Reply New Topic New Poll
Member
Posts: 1,129
Joined: May 25 2013
Gold: 0.11
Oct 25 2013 10:01am
"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."

#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 just clueless or if there's a mistake im missing
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 26 2013 03:45am
I don't really answer questions but maybe before someone rolls around, you should clarify what issue you are having? Or if it is a compile/runtime error, maybe you should post it?
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Oct 26 2013 01:02pm
Quote (Eep @ Oct 26 2013 01:45am)
I don't really answer questions but maybe before someone rolls around, you should clarify what issue you are having? Or if it is a compile/runtime error, maybe you should post it?


This. Also, you should always put your code in code brackets.
Member
Posts: 1
Joined: Oct 27 2013
Gold: 0.00
Oct 27 2013 10:44pm
> findAverage (double array[], int size, double returnvalue);

You have used this line in main(). This is not how you call a function. This is normally how functions are declared ( as it is done at the top of the program for this function). To call this function, you omit the types, and only use variable names, like this:

findAverage( array[], size, returnvalue);

This would have been a compiler error, I assume.

however, after you fix this, there is another error here. The variable returnvalue, going by its name, seems out of place, or superfluous. A return value is something that is returned by a function, not something we pass it as an argument ( like array, size). So, a better statement would look like this:

int returnvalue = findAverage( array, size);

The function findAverage() would look like this:

int findAverage( int array[], int size) {
// code that calculates average

return average;
}

the "int" before findAverage tells us that it returns an int. And in the main(), we assign the returned value to "int returnvalue".




Go Back To Programming & Development Topic List
Add Reply New Topic New Poll