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

  • Context: MHB 
  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    Elements
Click For Summary

Discussion Overview

The discussion revolves around modifying elements in a vector based on a specified minimum value (minVal). Participants are addressing a programming challenge where elements of an array should be doubled only if they are less than minVal. The focus is on correcting the logic within a loop to achieve the desired output.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about how to selectively double elements in an array based on a condition.
  • Another participant suggests using an if statement within the loop to check if each element is less than minVal before doubling it.
  • A subsequent post reiterates the need for an if statement but incorrectly implements the condition based on the loop index rather than the array element.
  • Another participant corrects the previous misunderstanding by emphasizing the need to compare the array element against minVal in the if statement.

Areas of Agreement / Disagreement

Participants generally agree on the need to use a conditional statement to selectively double elements, but there is disagreement on the correct implementation of that logic, with multiple incorrect approaches presented.

Contextual Notes

Some participants' solutions rely on incorrect comparisons, such as using the loop index instead of the array element for the condition. There are also unresolved issues regarding the expected output versus the actual output from the provided code snippets.

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;
	}
}
 

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
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
8K