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;
	}
}
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...
Back
Top