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

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    Elements Vector
Click For Summary
The discussion revolves around modifying an array of integers, specifically decrementing each element in the array `lowerScores` by 1, while ensuring that any element that is already 0 or negative is set to 0. The initial code provided incorrectly sets elements to 0 based on a comparison with `SCORES_SIZE`, rather than checking if they are positive. The correct approach involves iterating through the array and using a conditional statement to decrement positive values and assign 0 to non-positive values. The proposed solution effectively addresses the issue, ensuring the expected output of `{4, 0, 1, 0}` when starting with `{5, 0, 2, -3}`.
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;
	}
}
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

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
3K
Replies
1
Views
4K
  • · Replies 13 ·
Replies
13
Views
2K
Replies
12
Views
2K