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
AI Thread 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;
	}
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top