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
AI Thread 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;
	}
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top