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 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
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...
Back
Top