186k views
3 votes
Set numMatches to the number of elements in userValues (having NUM_VALS elements) that equal matchValue. Ex: If matchValue = 2 and userVals = {2, 2, 1, 2}, then numMatches = 3.

#include

int main(void) {
const int NUM_VALS = 4;
int userValues[NUM_VALS];
int i = 0;
int matchValue = 0;
int numMatches = -99; // Set numMatches to 0 before your for loop

userValues[0] = 2;
userValues[1] = 2;
userValues[2] = 1;
userValues[3] = 2;

matchValue = 2;

/* Your solution goes here */

printf("matchValue: %d, numMatches: %d\\", matchValue, numMatches);

return 0;
}

User DKK
by
6.7k points

1 Answer

3 votes

Answer:

following are the code this question:

numMatches = 0; //assign value 0 in numMatches variable

for (i = 0; i < NUM_VALS; i++) //defining a loop that count and match value inside the loop

{

if (userValues[i] == matchValue) //define if block that check value

{

++numMatches; //increment value of numMatches by 1

}

}

Step-by-step explanation:

Description of the above code as follows:

  • In the above code, we assign a value, that is 0 in the "numMatches" variable, in the next step, a for loop is declare, that will count all array value.
  • Inside the loop a conditional statement is used, that matches "matchValue" to the array "userValues".
  • In this if the given value is matched, it will first increment the value of "numMatches".
User Volodymyr Sorokin
by
7.3k points