Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Arrays in functions 2. Arrays in Functions You can use both the array index vari

ID: 3762287 • Letter: A

Question

Arrays in functions

2. Arrays in Functions You can use both the array index variables and the entire array itself as arguments to functions. The following program updates the elements of the array grades, one-by-one. Thus, we have used a call-by-reference-ish mechanism to update the values (although no & was used). Note that there is a major difference between the grade in the function call and the one in the function definition. In the statement get_grade (grades[i]); "grades" is the array grades [] and we are referring to its ith element, while in statement: void get_grade (int& grade) "grade" is a single integer Il An array element as an argument to a function #include using namespace std; l Obtains a grade from the user and stores it in parameter, grade. void get grade(int& grade); int main(void) int grades[5] int i cout

Explanation / Answer

#include <iostream>

using namespace std;

void get_grade(int arg[], int length) {
int n;
cout<< "input " <<length <<" grades between 0 and 100 : "<<length <<endl;
for (n=0; n<length; n++){
cin >> arg[n];
}
}

void printarray (int arg[], int length) {
int n;
cout<< "Elements of array :"<<endl;
for (n=0; n<length; n++)
cout << arg[n] << " ";
cout << " ";
}

int main()
{
cout << "Hello World" << endl;
int grades[5];
get_grade(grades,5);
printarray(grades,5);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote