Arrays in Functions 2. Arrays in Functions You can use both the array index vari
ID: 3762286 • 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 coutExplanation / 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;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.