C 1 Set numMatches to the number of elements in userValues h

C++

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

#include <iostream>
using namespace std;

int main() {
const int NUM_VALS = 4;
int userValues[NUM_VALS];
int i = 0;
int matchValue = 0;
int numMatches = 0; // Assign numMatches with 0 before your for loop

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

matchValue = 2;

/* Your solution goes here */

cout << \"matchValue: \" << matchValue << \", numMatches: \" << numMatches << endl;

return 0;
}

2) Write a for loop to populate array userGuesses with NUM_GUESSES integers. Read integers using cin. Ex: If NUM_GUESSES is 3 and user enters 9 5 2, then userGuesses is {9, 5, 2}.

#include <iostream>
using namespace std;

int main() {
const int NUM_GUESSES = 3;
int userGuesses[NUM_GUESSES];
int i = 0;

/* Your solution goes here */

for (i = 0; i < NUM_GUESSES; ++i) {
cout << userGuesses[i] << \" \";
}

return 0;
}

Solution

Question 1:

#include <iostream>
using namespace std;
int main() {
const int NUM_VALS = 4;
int userValues[NUM_VALS];
int i = 0;
int matchValue = 0;
int numMatches = 0; // Assign numMatches with 0 before your for loop
userValues[0] = 2;
userValues[1] = 2;
userValues[2] = 1;
userValues[3] = 2;
matchValue = 2;
/* Your solution goes here */
for(i-0; i<NUM_VALS ;i++){
if(matchValue == userValues[i]){
numMatches++;
}
}
cout << \"matchValue: \" << matchValue << \", numMatches: \" << numMatches << endl;
return 0;
}
Output:

sh-4.3$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                        

sh-4.3$ main                                                                                                                                                                                                                

matchValue: 2, numMatches: 3

Question 2:

#include <iostream>
using namespace std;
int main() {
const int NUM_GUESSES = 3;
int userGuesses[NUM_GUESSES];
int i = 0;
/* Your solution goes here */
for (i = 0; i < NUM_GUESSES; ++i) {
cin >> userGuesses[i];
}
for (i = 0; i < NUM_GUESSES; ++i) {
cout << userGuesses[i] << \" \";
}
return 0;
}

Output:

sh-4.3$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                        

sh-4.3$ main                                                                                                                                                                                                                

9 5 2                                                                                                                                                                                9 5 2                                         

C++ 1) Set numMatches to the number of elements in userValues (having NUM_VALS elements) that equal matchValue. Ex: If matchValue = 2 and userValues = {2, 2, 1,
C++ 1) Set numMatches to the number of elements in userValues (having NUM_VALS elements) that equal matchValue. Ex: If matchValue = 2 and userValues = {2, 2, 1,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site