Question

in C++ Problem: Subtract 4 to any element's value that is greater than maxVal. Ex: If...

in C++

Problem:

Subtract 4 to any element's value that is greater than maxVal.

Ex: If maxVal = 10, then dataPoints = {2, 12, 9, 20} becomes {2, 8, 9, 16}.

------------------------

#include
#include
using namespace std;

int main() {
int maxVal;
int NUM_POINTS;
unsigned int i;
cin >> maxVal;
cin >> NUM_POINTS;
vector dataPoints(NUM_POINTS);

for (i = 0; i < dataPoints.size(); ++i) {
cin >> dataPoints.at(i);
}

/* Your solution goes here */

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

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include<iostream>
#include<vector>
using namespace std;

int main() {
int maxVal;
int NUM_POINTS;
unsigned int i;
cin >> maxVal;
cin >> NUM_POINTS;
vector<int> dataPoints(NUM_POINTS);

for (i = 0; i < dataPoints.size(); ++i) {
cin >> dataPoints.at(i);
}

/* Your solution goes here */
for (i = 0; i < dataPoints.size(); ++i) {
    if(dataPoints.at(i) > maxVal ){
        dataPoints.at(i) = dataPoints.at(i) - 4;
    }
}

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

return 0;
}

Only added a for block.

Add a comment
Know the answer?
Add Answer to:
in C++ Problem: Subtract 4 to any element's value that is greater than maxVal. Ex: If...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In the C language Double any element's value that is less than controlVal. Ex: If controlVal...

    In the C language Double any element's value that is less than controlVal. Ex: If controlVal = 10, then dataPoints = {2, 12, 9, 20} becomes {4, 12, 18, 20}. #include <stdio.h> int main(void) { const int NUM_POINTS = 4; int dataPoints[NUM_POINTS]; int controlVal; int i; scanf("%d", &controlVal); for (i = 0; i < NUM_POINTS; ++i) { scanf("%d ", &(dataPoints[i])); } /* Your solution goes here */ for (i = 0; i < NUM_POINTS; ++i) { printf("%d ", dataPoints[i]); }...

  • Double any element's value that is less than minVal. Ex: If minVal = 10, then dataPoints...

    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}. #include <stdio.h> int main(void) { const int NUM_POINTS = 4; int dataPoints[NUM_POINTS]; int minVal; int i; dataPoints[0] = 2; dataPoints[1] = 12; dataPoints[2] = 9; dataPoints[3] = 20; minVal = 10; /* Your solution goes here */ for (i = 0; i < NUM_POINTS; ++i) { printf("%d ", dataPoints[i]); } printf("\n"); return 0; }

  • Double any element's value that is less than minValue. Ex: If minValue 10, then dataPoints =...

    Double any element's value that is less than minValue. Ex: If minValue 10, then dataPoints = {2, 12, 9, 20) becomes {4, 12, 18, 20). public static void main (String ] args) { Scanner scnr = new Scanner(System.in); 4 5 final int NUM_POINTS = 4; int dataPoints = int minValue int i new int[NUM POINTS]; 7 8 minValue 10 scnr.nextInt(); 11 for (i 0; i dataPoints[i] } dataPoints.length; ++i) { scnr.nextInt); 12 13 14 15 /Your solution goes here */...

  • C++ Write a function so that the main() code below can be replaced by the simpler...

    C++ Write a function so that the main() code below can be replaced by the simpler code that calls function MphAndMinutesToMiles(). Original main(): int main() { double milesPerHour; double minutesTraveled; double hoursTraveled; double milesTraveled; cin >> milesPerHour; cin >> minutesTraveled; hoursTraveled = minutesTraveled / 60.0; milesTraveled = hoursTraveled * milesPerHour; cout << "Miles: " << milesTraveled << endl; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using...

  • C++ Write a loop that subtracts 1 from each element in lowerScores. If the element was...

    C++ 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}. #include <iostream> using namespace std; int main() { const int SCORES_SIZE = 4; int lowerScores[SCORES_SIZE]; int i; for (i = 0; i < SCORES_SIZE; ++i) { cin >> lowerScores[i]; } /* Your solution goes here */ for (i = 0; i < SCORES_SIZE;...

  • 3.3.2: If-else statements. C++ Print "userNum1 is negative." if userNum1 is less than 0. End with...

    3.3.2: If-else statements. C++ Print "userNum1 is negative." if userNum1 is less than 0. End with newline. Assign userNum2 with 1 if userNum2 is greater than 12. Otherwise, print "userNum2 is less or equal 12.". End with newline. #include <iostream> using namespace std; int main() { int userNum1; int userNum2; cin >> userNum1; cin >> userNum2; /*answer goes here*/ cout << "userNum2 is " << userNum2 << endl; return 0; }

  • Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements....

    Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements. Ex: If userValues is {2, 2, 1, 2} and matchValue is 2 , then numMatches should be 3. Your code will be tested with the following values: * matchValue: 2, userValues: {2, 2, 1, 2} (as in the example program above) * matchValue: 0, userValues: {0, 0, 0, 0} * matchValue: 50, userValues: {10, 20, 30, 40} (Notes) #include <iostream> #include <vector> using namespace...

  • C++ class 3.2.4: If-else statement: Fix errors. Re-type the code and fix any errors. The code...

    C++ class 3.2.4: If-else statement: Fix errors. Re-type the code and fix any errors. The code should convert non-positive numbers to 1. if (userNum > 0) cout << "Positive." << endl; else cout << "Not positive, converting to 1." << endl; Answer #include using namespace std; int main() { int userNum; cin >> userNum; /* Your solution goes here */ return 0; userNum = 1; cout << "Final: " << userNum << endl;

  • Re-type the code and fix any errors. The code should convert non-positive numbers to 1. if...

    Re-type the code and fix any errors. The code should convert non-positive numbers to 1. if (userNum > 0) cout << "Positive." << endl; else cout << "Not positive, converting to 1." << endl; userNum = 1; cout << "Final: " << userNum << endl; answer: #include <iostream> using namespace std; int main() { int userNum; cin >> userNum; /* Your solution goes here */ return 0; }

  • Write an expression that executes the loop while the user enters a number greater than or...

    Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with userNum initially 9 and user input of 5, 2, -1, then with userNum initially 0 and user input of -17, then with userNum initially -1. See "How to Use zyBooks". . Also note: If the submitted code has an infinite loop, the system will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT