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

In summary, the conversation is about writing a loop to decrement numbers in an array and assigning 0 if the element is already 0 or negative. The code provided initially only sets the element to 0 if it is less than SCORES_SIZE, but the desired outcome is to decrement the element if it is positive and set it to 0 otherwise. The suggested code uses an if statement to check if the element is positive, and if so, decrements it, otherwise sets it to 0.
  • #1
needOfHelpCMath
72
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
  • #2
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;
	}
}
 

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

What is a decrement vector?

A decrement vector is a data structure that stores a sequence of numbers in memory. It is similar to an array, but the elements are automatically decremented by a specified value. This means that each element in the vector is reduced by a fixed amount when the vector is created or modified.

How do you decrement elements in a vector?

To decrement elements in a vector, you need to first create the vector and specify the decrement value. Then, you can use a loop or built-in functions to iterate through the vector and decrement each element by the specified value.

What are the benefits of using a decrement vector?

Decrement vectors can be useful for certain applications, such as simulations or data analysis, where a fixed decrement value needs to be applied to a sequence of numbers. They can also save memory space compared to other data structures, as the values are modified in-place rather than creating new copies.

Can you increment and decrement elements in a vector simultaneously?

Yes, it is possible to increment and decrement elements in a vector at the same time. This can be achieved by using different loops or built-in functions to target specific elements for incrementing or decrementing.

Are decrement vectors supported in all programming languages?

No, not all programming languages have built-in support for decrement vectors. However, many languages have libraries or packages that can be used to implement decrement vectors. It is important to check the documentation of the specific language you are using to see if it supports decrement vectors.

Similar threads

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