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
SUMMARY

The discussion focuses on modifying vector elements in C++ to double values that are less than a specified minimum value (minVal). The provided code snippet incorrectly doubles all elements instead of selectively doubling those below minVal. The correct implementation requires an if statement within the loop to check each element against minVal. The final solution correctly outputs the expected results when minVal is set to 10 and dataPoints initialized to {2, 12, 9, 20}.

PREREQUISITES
  • Understanding of C++ vector manipulation
  • Knowledge of conditional statements in C++
  • Familiarity with basic C++ syntax and structure
  • Experience with compiling and running C++ programs
NEXT STEPS
  • Learn about C++ vector methods and their applications
  • Explore conditional statements and their usage in C++
  • Investigate debugging techniques for C++ programs
  • Study C++ loops and iteration constructs in depth
USEFUL FOR

C++ developers, programming students, and anyone looking to enhance their skills in vector manipulation and conditional logic in C++.

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