How to decrement vector elements and handle negative values in C++.

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

This discussion addresses how to decrement elements in a C++ vector while ensuring that negative values are handled correctly. The provided solution involves iterating through a vector named lowerScores and decrementing each element if it is positive; otherwise, it assigns a value of 0. The corrected code snippet demonstrates the proper logic to achieve the expected output of {4, 0, 1, 0} from the initial vector {5, 0, 2, -3}. Key functions used include vector::at() for element access and standard loop constructs.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with C++ STL vectors
  • Basic knowledge of control flow statements (if-else, loops)
  • Experience with C++ input/output operations
NEXT STEPS
  • Explore C++ STL algorithms for more efficient vector manipulation
  • Learn about error handling in C++ to manage unexpected vector states
  • Investigate advanced vector operations, such as sorting and searching
  • Study memory management in C++ to optimize vector usage
USEFUL FOR

C++ developers, software engineers, and students learning about data structures and algorithms, particularly those interested in vector manipulation and control flow in programming.

needOfHelpCMath
Messages
70
Reaction score
0
I am stuck on now to decrement the numbers and would like some help please.

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}.
Code:
#include <iostream>
#include <vector>
using namespace std;

int main() {
   const int SCORES_SIZE = 4;
   vector<int> lowerScores(SCORES_SIZE);
   int i = 0;

   lowerScores.at(0) = 5;
   lowerScores.at(1) = 0;
   lowerScores.at(2) = 2;
   lowerScores.at(3) = -3;

   /* Your solution goes here  */  for (i = 0; i < SCORES_SIZE; ++i) { 
if (lowerScores.at(i) < SCORES_SIZE) { 
lowerScores.at(i) = 0;

}
  }  
  
   for (i = 0; i < SCORES_SIZE; ++i) {
      cout << lowerScores.at(i) << " ";
   }
   cout << endl;

   return 0;
}

Testing for lowerScores = {5, 0, 2, -3}
Expected output: 4 0 1 0
Your output: 5 0 2 0
 
Last edited:
Technology news on Phys.org
In the following for-loop:

Code:
  for (i = 0; i < SCORES_SIZE; ++i) { 
if (lowerScores.at(i) < SCORES_SIZE) { 
lowerScores.at(i) = 0;

}
  }

You have code that will set the array value to zero if the value there is less than [M]SCORES_SIZE[/M]. However, what you want is code that will set the array value to zero if the value is not positive, otherwise you want to decrement it. So, I would try something like:

Code:
for (i = 0; i < SCORES_SIZE; i++)
{
	if (lowerScores.at(i) > 0)
	{
		lowerScores.at(i)--;
	}
	else
	{
		lowerScores.at(i) = 0;
	}
}
 

Similar threads

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