C++ Vectors: Finding Values in vectors

  • Context: C/C++ 
  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    C++ Vectors
Click For Summary
SUMMARY

The discussion focuses on counting the occurrences of a specific value in a C++ vector using the STL (Standard Template Library). The user initially miscalculates the number of matches by incorrectly initializing the variable numMatches with the first element of the vector instead of zero. The correct approach involves initializing numMatches to zero and incrementing it within a loop that checks each element against the matchValue. This method ensures accurate counting of elements equal to matchValue.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with STL vectors in C++
  • Basic knowledge of loops and conditional statements in programming
  • Experience with input/output operations in C++
NEXT STEPS
  • Study C++ vector operations and methods
  • Learn about C++ control structures, specifically loops and conditionals
  • Explore debugging techniques in C++ to identify logical errors
  • Practice writing functions that manipulate vectors and return values
USEFUL FOR

C++ beginners, software developers looking to improve their understanding of vectors, and educators teaching programming concepts.

needOfHelpCMath
Messages
70
Reaction score
0
I would like some help or guide to if i am going on the right track on my program ***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. ***
Code:
#include <iostream>
#include <vector>
using namespace std;

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

   userValues.at(0) = 2;
   userValues.at(1) = 2;
   userValues.at(2) = 1;
   userValues.at(3) = 2;

   matchValue = 2;

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

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

   return 0;
}

**MY TEST**

Testing matchValue = 2,
userValues = {2, 2, 1, 2}
Expected value: 3
Your value: 9
 
Technology news on Phys.org
You want something like:

Code:
numMatches= 0;
for (i = 0; i < NUM_VALS; i++) 
{
	if (userValues.at(i) == matchValue)
	{
		numMatches++;
	}
}

You see, in your code, you first assign the value of [m]userValues.at(0)[/m] to [m]numMatches[/m], which is 2, and then in your second for loop, you add each of the values in the array to [m]numMatches[/m], so you wind up with:

[m]numMatches = 2 + 2 + 2 + 1 + 2 = 9[/m]

As you can see in the code I wrote, we initialize [m]numMatches[/m] to zero before the loop, and then we check each element of the array to see if it is equal to [m]matchValue[/m], and if it is, then we increment [m]numMatches[/m], so that we get a count of the number of elements that match.

Does this make sense?
 
MarkFL said:
You want something like:

Code:
numMatches= 0;
for (i = 0; i < NUM_VALS; i++) 
{
	if (userValues.at(i) == matchValue)
	{
		numMatches++;
	}
}

You see, in your code, you first assign the value of [m]userValues.at(0)[/m] to [m]numMatches[/m], which is 2, and then in your second for loop, you add each of the values in the array to [m]numMatches[/m], so you wind up with:

[m]numMatches = 2 + 2 + 2 + 1 + 2 = 9[/m]

As you can see in the code I wrote, we initialize [m]numMatches[/m] to zero before the loop, and then we check each element of the array to see if it is equal to [m]matchValue[/m], and if it is, then we increment [m]numMatches[/m], so that we get a count of the number of elements that match.

Does this make sense?

Yes! it does! I am so horrible at understanding what the programs wants...any tips for me so that i can understand what the programs wants. Thank you it made a lot of sense!
 
Understanding the expected output, and how to get it generally comes with practice. Coding can be extremely rewarding, and it can be extremely frustrating at the same time. :)
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
8K
Replies
1
Views
5K
Replies
2
Views
5K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 10 ·
Replies
10
Views
3K