#include <iostream> // input and output #include <stdlib.h> // pause screen after completion using namespace std; //average function double average (double sum, int i, double avg) { avg = sum / i; return average(avg); } // main function int main() { int grades[9], i; double avg, sum = 0; while( grades[i] != -1) { for( i = 0; i <= 10; i++) { cout<<"Enter Grade: "<< i + 1 <<endl; cin>>grades[i]; sum = sum + grades[i]; } // end for } // end while average(sum,i); avg = average(avg); cout << "Average is: " << avg << endl; cout << grades[i] << endl; system("pause"); //stop the screen } //end main
42 Replies - 16598 Views - Last Post: 28 February 2013 - 02:09 AM
#1
using an array to calculate average c++
Posted 24 February 2013 - 06:54 PM
I'm trying to do an assignment, but I don't really understand arrays or functions all the way. I've done the average before in c++ where the main function did all the calculations as variable ie sum = numbers and avg = sum/ n but for functions and arrays its a bit more confusing. I mostly get too few argument errors. In this case, the numbers are grades the user is entering, calculating the average once a grade of -1 is entered. I want a function called average to do the calculations, so once the main finishes getting the sum of the grades, it will pass the value of sum to the average fucntion, which will calculate the average and return the variable avg to the main function, where main will output both the avg variable (as defined in main) and the values of the array.
Replies To: using an array to calculate average c++
#2
Re: using an array to calculate average c++
Posted 24 February 2013 - 07:20 PM
I changed the return part to
but I just want the variable sum to be passed so that main can output it as a result.
return average(avg,sum,i);
but I just want the variable sum to be passed so that main can output it as a result.
#3
Re: using an array to calculate average c++
Posted 24 February 2013 - 07:30 PM
double average (double sum, int i, double avg) { avg = sum / i; return average(avg); }
You need to review how functions work.
http://www.cplusplus...rial/functions/
This post has been edited by jjl: 24 February 2013 - 07:30 PM
#4
Re: using an array to calculate average c++
Posted 24 February 2013 - 07:49 PM
when I do :
it doesn't give me an error. I if I only put avg in the return (), it gives an error of too few arguments. Am I doing something wrong?
return average(avg,sum,i);
it doesn't give me an error. I if I only put avg in the return (), it gives an error of too few arguments. Am I doing something wrong?
#5
Re: using an array to calculate average c++
Posted 24 February 2013 - 08:17 PM
If avg is what you're calculating, then why pass it to the function? And why do you change the order of the parameters??
I would think you'd want to pass the array and the number of items in the array to the function and have the function return the average.
I would think you'd want to pass the array and the number of items in the array to the function and have the function return the average.
#6
Re: using an array to calculate average c++
Posted 24 February 2013 - 08:24 PM
Quote
it doesn't give me an error. I if I only put avg in the return (), it gives an error of too few arguments. Am I doing something wrong?
It is too hard to explain why that is completely wrong. Read the reference material that I posted earlier.
#7
Re: using an array to calculate average c++
Posted 24 February 2013 - 09:03 PM
//average function //changes below corrects the average () function double average (double sum, int i) {double avg=0; avg = sum / i; return avg;//delete the word average and ellipses }
This post has been edited by buffalobill: 24 February 2013 - 09:05 PM
#8
Re: using an array to calculate average c++
Posted 24 February 2013 - 09:49 PM
Made some changes and now get an 47 invalid conversion from `int*' to `int' initializing argument 1 of `int average(int)'
#include <iostream> // input and output #include <stdlib.h> // pause screen after completion using namespace std; //average function int average(int grades) { int i =0; int avg = 0; int sum = 0; for( i = 0; i <= 9; i++) { sum = sum + grades; } //end for avg = sum / i; return (avg); } // main function int main() { int grades[9], i=0; double GradeAvg, input; while(grades[i] != -1) { cout<<"Enter Grade: "<< i + 1 <<endl; cin>> grades[i]; } //end while GradeAvg = average(grades); cout << "Average is: " << GradeAvg << endl; cout << grades[i] << endl; system("pause"); //stop the screen } //end main
#9
Re: using an array to calculate average c++
Posted 24 February 2013 - 09:58 PM
You need to pass grades as an array of integers, not a single integer.
Then in your loop, you need to index which grade you are adding to the sum.
int average(int grades[]);
Then in your loop, you need to index which grade you are adding to the sum.
#10
Re: using an array to calculate average c++
Posted 24 February 2013 - 10:37 PM
jjl, on 24 February 2013 - 09:58 PM, said:
You need to pass grades as an array of integers, not a single integer.
Then in your loop, you need to index which grade you are adding to the sum.
int average(int grades[]);
Then in your loop, you need to index which grade you are adding to the sum.
makes sense. How do I pass an array of integers, instead of a single one? Would it be: "int average(int grades[i]"? Does the i mean all value if I do that in the loop? I've been following the array example
// arrays example #include <iostream> using namespace std; int billy [] = {16, 2, 77, 40, 12071}; int n, result=0; int main () { for ( n=0 ; n<5 ; n++ ) { result += billy[n]; } cout << result; return 0
from http://www.cplusplus...utorial/arrays/
Where they have n, I call it i in mine.
In my for loop, should it be something like " sum = sum + grades[i]; ?
#11
Re: using an array to calculate average c++
Posted 24 February 2013 - 11:17 PM
Quote
How do I pass an array of integers
By declaring the function to accept an array of integers
i.e.
//accepts array of integers, and number of integers in array double average(int grades[], int size) { int sum = 0; for(int i=0; i<size; i++) { //calculate sum } return //calculate average } int main() { int grades[5] = {55, 23, 99, 88, 22}; //array of grades double avg = average(grades, 5);
Quote
In my for loop, should it be something like " sum = sum + grades[i];
Exactly
#12
Re: using an array to calculate average c++
Posted 25 February 2013 - 02:48 AM
Now my problem is that the size of the array cannot be greater than 10 total, and the user can input less than 10 grades in. It also fails to store the value of the entered grades in the appropriate array indexes, it just overwrites the first grade.
//average function int average(int grades[], int size =9) { int avg = 0; int sum = 0; for( int i = 0; i = size; i++) { sum = sum + grades[i]; } //end for avg = sum / size; return (avg);
int main() { int grades[9], i=0; double GradeAvg, input; while(grades[i] != -1) { cout<<"Enter Grade: "<< i + 1 <<endl; cin>> grades[i]; } //end while
#13
Re: using an array to calculate average c++
Posted 25 February 2013 - 03:19 AM
forgot to add i++; in my 2nd code block in the loop
The problem is not only is my sentinel value of -1 ignored, but it asks for grades up to grade #19
int main() { int grades[9], i=0; double avg; while(grades[i] != -1) { cout<<"Enter Grade "<< i+1 <<": " ; cin>> grades[i]; i++; } //end while
The problem is not only is my sentinel value of -1 ignored, but it asks for grades up to grade #19
#14
Re: using an array to calculate average c++
Posted 25 February 2013 - 06:33 AM
Quote
The problem is not only is my sentinel value of -1 ignored
Your sentinel value is not ignored, it is not seen. You increment your index value before you test the value so you are testing an element that hasn't been initialized. You need to test the value before you increment the index.
Quote
but it asks for grades up to grade #19
Actually there is really nothing the program from asking for grades much higher than that. Where do you test to insure your index doesn't become larger than your array?
Jim
#15
Re: using an array to calculate average c++
Posted 27 February 2013 - 04:51 AM
Changes:
Always gives me average = 0. Still does not see sentinel value of -1.
When I say [code/ while(grades[i] != -1 [/code]
I want it to mean
Because to me, "grades[i]" is the stored grade the user entered. So if this were the 5th iteration of the loop (grade #5) grades[i] = grades[4] (number might be off) but I seem to have this concept wrong somehow.
#include <iostream> // input and output #include <stdlib.h> // pause screen after completion using namespace std; //average function int average(int grades[], int size) { int avg = 0; int sum = 0; for( int i = 0; i < size; i++) { sum += grades[i]; } //end for avg = sum / size; return avg; } // main function int main() { int grades[9], i=0, Gradeavg, size; while(grades[i] != -1 && i < 10) { cout<<"Enter Grade "<< i+1 <<": " ; cin>> grades[i]; i++; } //end while Gradeavg = average(grades,size); cout << "Average is: " << Gradeavg << endl; system("pause"); //stop the screen } //end main
Always gives me average = 0. Still does not see sentinel value of -1.
When I say [code/ while(grades[i] != -1 [/code]
I want it to mean
cout<<"Enter Grade "<< i+1 <<": " ; cin>> grades[i];
Because to me, "grades[i]" is the stored grade the user entered. So if this were the 5th iteration of the loop (grade #5) grades[i] = grades[4] (number might be off) but I seem to have this concept wrong somehow.