C++ Vectors: Finding Values in vectors

  • C/C++
  • Thread starter needOfHelpCMath
  • Start date
  • Tags
    C++ Vectors
In summary, the code above creates an array of integers, and it checks to see if any of the elements in the array are equal to the value of matchValue. If any of the elements are equal to matchValue, then the array is incremented by 1.
  • #1
needOfHelpCMath
72
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
  • #2
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?
 
  • #3
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!
 
  • #4
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. :)
 

FAQ: C++ Vectors: Finding Values in vectors

What is a C++ vector?

A C++ vector is a data structure that represents a dynamic array. It can store a collection of elements of the same data type and allows for efficient insertion, deletion, and accessing of elements.

How do I find a value in a C++ vector?

To find a value in a C++ vector, you can use the std::find() function from the algorithm library. It takes in the starting and ending iterators of the vector and the value to be searched for, and returns an iterator to the first occurrence of the value in the vector.

What if the value is not present in the vector?

If the value is not present in the vector, the std::find() function will return the ending iterator of the vector, which can be compared to the starting iterator to determine if the value was found or not.

Can I search for a specific type of value in a vector?

Yes, the std::find() function can search for any type of value as long as it is of the same data type as the elements in the vector. You can also provide a custom comparison function if you want to search for a specific type of value in the vector.

Is there a more efficient way to search for values in a vector?

Yes, if the vector is sorted, you can use the std::binary_search() function to search for values in O(log n) time. This function also uses the algorithm library and takes in the starting and ending iterators of the vector, as well as the value to be searched for.

Similar threads

Replies
5
Views
8K
Replies
5
Views
2K
Replies
22
Views
3K
Replies
23
Views
2K
Replies
1
Views
1K
Replies
13
Views
4K
Back
Top