in C++ 1) Double any element\'s value that is less than minVal. Ex: If minVal =
ID: 675037 • Letter: I
Question
in C++
1) 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} Ex: Testing minVal = 4 and dataPoints = {1, 2, 3, 4, 5, -3} becomes {2 4 6 4 5 -6}
#include <iostream>
using namespace std;
int main() {
const int NUM_POINTS = 4;
int dataPoints[NUM_POINTS];
int minVal = 0;
int i = 0;
dataPoints[0] = 2;
dataPoints[1] = 12;
dataPoints[2] = 9;
dataPoints[3] = 20;
minVal = 10;
// student code here
for (i = 0; i < NUM_POINTS; ++i) {
cout << dataPoints[i] << " " ;
}
cout << endl;
return 0;
}
2) 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 = 0;
lowerScores[0] = 5;
lowerScores[1] = 0;
lowerScores[2] = 2;
lowerScores[3] = -3;
/* Your solution goes here */
for (i = 0; i < SCORES_SIZE; ++i) {
cout << lowerScores[i] << " ";
}
cout << endl;
return 0;
}
3)Add each element in origList with the corresponding value in offsetAmount. Print each sum followed by a space. Ex: If origList = {40, 50, 60, 70} and offsetAmount = {5, 7, 3, 0}, print:
#include <iostream>
using namespace std;
int main() {
const int NUM_VALS = 4;
int origList[NUM_VALS];
int offsetAmount[NUM_VALS];
int i = 0;
origList[0] = 40;
origList[1] = 50;
origList[2] = 60;
origList[3] = 70;
offsetAmount[0] = 5;
offsetAmount[1] = 7;
offsetAmount[2] = 3;
offsetAmount[3] = 0;
/* Your solution goes here */
cout << endl;
return 0;
}
Explanation / Answer
1.
#include <iostream>
using namespace std;
int main() {
const int NUM_POINTS = 4;
int dataPoints[NUM_POINTS];
int minVal = 0;
int i = 0;
dataPoints[0] = 2;
dataPoints[1] = 12;
dataPoints[2] = 9;
dataPoints[3] = 20;
minVal = 10;
for(i = 0; i < NUM_POINTS; i++)
if(dataPoints[i] < minVal)
dataPoints[i] *= 2;
for (i = 0; i < NUM_POINTS; ++i) {
cout << dataPoints[i] << " " ;
}
cout << endl;
return 0;
}
2.
#include <iostream>
using namespace std;
int main() {
const int SCORES_SIZE = 4;
int lowerScores[SCORES_SIZE];
int i = 0;
lowerScores[0] = 5;
lowerScores[1] = 0;
lowerScores[2] = 2;
lowerScores[3] = -3;
for(i = 0; i < SCORES_SIZE; i++)
if(lowerScores[i] > 0)
lowerScores[i]--;
else
lowerScores[i] = 0;
for (i = 0; i < SCORES_SIZE; ++i) {
cout << lowerScores[i] << " ";
}
cout << endl;
return 0;
}
3.
#include <iostream>
using namespace std;
int main() {
const int NUM_VALS = 4;
int origList[NUM_VALS];
int offsetAmount[NUM_VALS];
int i = 0;
origList[0] = 40;
origList[1] = 50;
origList[2] = 60;
origList[3] = 70;
offsetAmount[0] = 5;
offsetAmount[1] = 7;
offsetAmount[2] = 3;
offsetAmount[3] = 0;
for(i = 0; i < NUM_VALS; i++)
cout<<origList[i] + offsetAmount[i]<<" ";
cout << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.