MHB How to Modify Vector Elements to Double Values Less Than a Minimum?

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    Elements
Click For Summary
The discussion centers on a coding challenge involving the manipulation of an array of integers based on a specified minimum value (minVal). The goal is to double the value of any element in the array that is less than minVal. The initial code provided incorrectly doubles all elements, leading to unexpected output. Participants emphasize the need to implement a conditional check within the loop to ensure only elements less than minVal are doubled. The correct approach involves using an if statement to compare each element of the array against minVal, allowing for selective modification. The final suggested code snippet illustrates the proper implementation, ensuring that only qualifying elements are altered, resulting in the expected output of {4, 12, 18, 20}.
needOfHelpCMath
Messages
70
Reaction score
0
I don't understand how it able to skip one element to the other in order to multiply the numbers by 2. It be helpful if anyone can explain.Double any element's value that is less than minVal. Ex: If minVal = 10, then dataPoints = {2, 12, 9, 20} becomes {4, 12, 18, 20}.
Code:
#include <iostream>
#include <vector>
using namespace std;

int main() {
   const int NUM_POINTS = 4;
   vector<int> dataPoints(NUM_POINTS);
   int minVal = 0;
   int i = 0;

   dataPoints.at(0) = 2;
   dataPoints.at(1) = 12;
   dataPoints.at(2) = 9;
   dataPoints.at(3) = 20;

   minVal = 10;

   /* Your solution goes here  */
  for(i = 0; i < NUM_POINTS; ++i) {
   dataPoints.at(i)= dataPoints.at(i) * 2;
  }
 
   for (i = 0; i < NUM_POINTS; ++i) {
      cout << dataPoints.at(i) << " " ;
   }
   cout << endl;

   return 0;
}
✖ Testing minVal = 10 and dataPoints = {2, 12, 9, 20}
Expected output: 4 12 18 20
Your output: 4 24 18 40
Tests aborted.
 
Technology news on Phys.org
You are simply doubling all elements of the array...inside the for loop you need to use an if statement to check if the element is less than [M]minVal[/M], and only if it is, then double the element. :)
 
MarkFL said:
You are simply doubling all elements of the array...inside the for loop you need to use an if statement to check if the element is less than [M]minVal[/M], and only if it is, then double the element. :)

i was only able get the first element to be multiply.
Code:
if (i < minVal) {
      dataPoints.at(i) = dataPoints.at(i) * 2;
   }
   else {
      dataPoints.at(i) = i;
   }

   for (i = 0; i < NUM_POINTS; ++i) {
      cout << dataPoints.at(i) << " " ;
   }
   cout << endl;

   return 0;
}
Testing minVal = 10 and dataPoints = {2, 12, 9, 20}
Expected output: 4 12 18 20
Your output: 4 12 9 20
Tests aborted.
 
You need to use the array element, not the loop index for the comparison. Like so:

Code:
for(i = 0; i < NUM_POINTS; i++)
{
	if (dataPoints.at(i) < minVal)
	{
		dataPoints.at(i) *= 2;
	}
}
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 15 ·
Replies
15
Views
4K
Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
3
Views
1K