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)
so can anyone please help me combine these simple programs
// 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 <iostream>
usingnamespace std;
int main()
{
// Declare Variables
constint 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
1 2 3 4 5 6 7 8 9 10 11 12 13
//Input function definition
void input(int grade[],int size){
for(int i=0;i<size;i++){
cout<<"Enter Letter Grade "<<i+1<<": ";
cin>>grade[i];
}}
//Output function definition
void output(constint grade[],int size){
for(int i=0;i<size;i++){
cout<<grade[i]<<endl;
}}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
constint 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;
}