### homework help

I did normal the program but my teacher says i need to make a array in this program without messing with "int main" part expect for changing few variables in there, most of the work has to be done in "void input". we strated arrays a few days ago but its not clicking for me. The function for this program is to give the average grade from 3 grades and also shows the letter grade but with arrays i need for it to ask me for 10 grades "const int N=10;"

My working normal program(no array)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include using namespace std; double math(int test1, int test2, int test3){ return double(test1+test2+test3)/3.0; } void output (double avg1, char lg){ cout<<"Your average is: "<< avg1; cout<<" You made a: "<< lg; } void input(int &t1, int &t2, int &t3){ cout<<"Enter test1"<>t1; cout<<"Enter test2"<>t2; cout<<"Enter test3"<>t3; } char lettergrade (double average) { if (average>=93){ return 'A'; } else if ((85<= average)&&(average <=93)){ return 'B'; } else if ((76<= average)&&(average <=85)){ return 'C'; } else if ((70<= average)&&(average <=76)){ return 'D'; } else if ((0<= average)&&(average <=70)){ return 'F'; } } int main() { int grade1; int grade2; int grade3; double average; char let; cout<<"Enter grade 1:"<>grade1; cout<<"Enter grade 2:"<>grade2; cout<<"Enter grade 3:"<>grade3; average = math(grade1, grade2, grade3); let = lettergrade(average); output (average, let); return 0; }``````

works alright, best array i could do:

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````// Input (): get 10 grades from user into array //Calculate (): find and return average of grades //lettergrade():find and return lettergrade of average //output(): display average and letter grade //int main (){ // int grades[N] // double averag; // char letter; #include using namespace std; int main() { // Declare Variables const int TOTAL_NUMBERS = 10; int numbers[TOTAL_NUMBERS]; double average; double sum = 0.0; int num; //Read all numbers for (int i = 0; i < TOTAL_NUMBERS; i++) { cout << "Enter a score: "; cin >> num; numbers[i] = num; sum += numbers[i]; } //Find the average average = sum/10; // Display average cout <<"\nThe average score is " << average <<"\n\n"; return 0; }``````

I think you are struggling in how to pass array into functions.

By default arrays will be passed by reference(that is the values in the array can be changed by that function)
when passing arrays in a function, two things has to be there.One array name and second its size.)

For example in your case, if you want input work to be done by a function,then it will like this

FUNCTION DEFINITION

 ``12345678910111213`` ``````//Input function definition void input(int grade[],int size){ for(int i=0;i>grade[i]; }} //Output function definition void output(const int grade[],int size){ for(int i=0;i

 ``123456789101112131415`` ``````const int TOTAL_NUMBERS=10; int main() { //input function call input(grade,TOTAL_NUMBERS); //In the above, we passed the array as reference //output function call output(grade,TOTAL_NUMBERS); //in the above call, the array has been sent as constant.Because we //are only gonna output the value and dont want array value to be //changed return 0; }``````

Last edited on
Topic archived. No new replies allowed.