Question

Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements....

Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements. Ex: If userValues is {2, 2, 1, 2} and matchValue is 2 , then numMatches should be 3.

Your code will be tested with the following values:
* matchValue: 2, userValues: {2, 2, 1, 2} (as in the example program above)
* matchValue: 0, userValues: {0, 0, 0, 0}
* matchValue: 50, userValues: {10, 20, 30, 40}
(Notes)

#include <iostream>
#include <vector>
using namespace std;

int main() {
const int NUM_VALS = 4;
int matchValue;
unsigned int i;
int numMatches = -99; // Assign numMatches with 0 before your for loop
vector<int> userValues(NUM_VALS);

cin >> matchValue;

for (i = 0; i < userValues.size(); ++i) {
cin >> userValues.at(i);
}

/* Your solution goes here */

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

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <vector>
using namespace std;

int main() {
   const int NUM_VALS = 4;
   int matchValue;
   unsigned int i;
   int numMatches = -99; // Assign numMatches with 0 before your for loop
   vector<int> userValues(NUM_VALS);
   
   cin >> matchValue;
   
   for (i = 0; i < userValues.size(); ++i) {
      cin >> userValues.at(i);
   }
   
   numMatches = 0;
   for (i = 0; i < userValues.size(); ++i) {
      if(userValues.at(i)==matchValue){
         numMatches++;
      }
   }
   
   cout << "matchValue: " << matchValue << ", numMatches: " << numMatches << endl;
   
   return 0;
}
Add a comment
Know the answer?
Add Answer to:
Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS...

    1. Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements. Ex: If userValues is {2, 1, 2, 2} and matchValue is 2 , then numMatches should be 3. Your code will be tested with the following values: matchValue: 2, userValues: {2, 1, 2, 2} (as in the example program above) matchValue: 0, userValues: {0, 0, 0, 0} matchValue: 10, userValues: {20, 50, 70, 100} What i am given: import java.util.Scanner; public class FindMatchValue...

  • C++ Write a loop that subtracts 1 from each element in lowerScores. If the element was...

    C++ Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}. #include <iostream> using namespace std; int main() { const int SCORES_SIZE = 4; int lowerScores[SCORES_SIZE]; int i; for (i = 0; i < SCORES_SIZE; ++i) { cin >> lowerScores[i]; } /* Your solution goes here */ for (i = 0; i < SCORES_SIZE;...

  • in C++ Problem: Subtract 4 to any element's value that is greater than maxVal. Ex: If...

    in C++ Problem: Subtract 4 to any element's value that is greater than maxVal. Ex: If maxVal = 10, then dataPoints = {2, 12, 9, 20} becomes {2, 8, 9, 16}. ------------------------ #include #include using namespace std; int main() { int maxVal; int NUM_POINTS; unsigned int i; cin >> maxVal; cin >> NUM_POINTS; vector dataPoints(NUM_POINTS); for (i = 0; i < dataPoints.size(); ++i) { cin >> dataPoints.at(i); } /* Your solution goes here */ for (i = 0; i <...

  • C++ Write a function SwapArrayEnds() that swaps the first and last elements of the function's array...

    C++ Write a function SwapArrayEnds() that swaps the first and last elements of the function's array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}. The array's size may differ from 4. #include <iostream> using namespace std; /* Your solution goes here */ int main() {    const int SORT_ARR_SIZE = 4;    int sortArray[SORT_ARR_SIZE];    int i = 0;    sortArray[0] = 10;    sortArray[1] = 20;    sortArray[2] = 30;    sortArray[3] = 40;...

  • CHALLENGE ACTIVITY 8.4.2: Find char in C string Assign a pointer to any instance of searchChar...

    CHALLENGE ACTIVITY 8.4.2: Find char in C string Assign a pointer to any instance of searchChar in personName to searchResult. #include <iostream> #include <cstring> using namespace std; int main() { char personName[100]; char searchChar; char* searchResult = nullptr; cin.getline(personName, 100); cin >> searchChar; /* Your solution goes here */ if (searchResult != nullptr) { cout << "Character found." << endl; } else { cout << "Character not found." << endl; } return 0; }

  • Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int...

    Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

  • /* Array expander: Write a function that accepts an int array as argument. The function should...

    /* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT