108k views
3 votes
Complete the PrintVals function to print each array element on a separate line.

void PrintVals(int arrayVals[], int numElements) {
int i;
for(i = 0; i < numElements; ++i) {
cout << arrayVals[i] << endl;
}
}

1 Answer

3 votes

Final answer:

The student's question pertains to completing a C++ function to print each element of an array on its own line, using a for loop and standard output (cout).

Step-by-step explanation:

The student is asking for assistance in completing a function in programming that prints each element of an array on a separate line. Here is the completed PrintVals function:

void PrintVals(int arrayVals[], int numElements) {
int i;
for(i = 0; i < numElements; ++i) {
cout << arrayVals[i] << endl;
}
}

The function uses a for loop to iterate through the elements of the array. Within the loop, each element is accessed using arrayVals[i] and printed out with a newline character, endl, ensuring that each element appears on its own line. It is essential that this loop continues to iterate while i is less than the number of elements in the array, numElements, to avoid accessing elements outside the bounds of the array.

User Nomesh DeSilva
by
7.6k points

Related questions

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.