60.8k views
5 votes
In c++

Given an int variable k, an int array incompletes that has been declared and initialized, an int variable nIncompletes that contains the number of elements in the array, an int variable studentID that has been initialized, and an int variable numberOfIncompletes, write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes. You may use only k, incompletes, nIncompletes, studentID, and numberOfIncompletes.

User Ghan
by
7.2k points

1 Answer

0 votes

Answer:

Here is an example of how you could write this code:

numberOfIncompletes = 0;

for (int k = 0; k < nIncompletes; k++) {

if (incompletes[k] == studentID) {

numberOfIncompletes++;

}

}

In this code, we first initialize the numberOfIncompletes variable to 0. Then, we use a for loop to iterate through each element of the incompletes array. Inside the loop, we use an if statement to check if the current element is equal to the value of studentID. If it is, we increment the numberOfIncompletes variable by 1. After the loop completes, the numberOfIncompletes variable will contain the number of times studentID appears in the incompletes array.

User Vikash Pandey
by
6.9k points