using namespace std;
int main() {
int runTimes[5];
// Populate array
runTimes[0] = 800;
runTimes[1] = 775;
runTimes[2] = 790;
runTimes[3] = 805;
runTimes[4] = 808;
cout << runTimes[0] << endl << runTimes[1] << endl << runTimes[2] << endl; return 0; }
800
775
790
Note: These activities may test code with different test values. This activity will perform two tests, the first with a 5-element array (int runTimes[5]), the second with a 4-element array (int runTimes[4]). See How to Use zyBooks.
Also note: If the submitted code tries to access an invalid array element, such as runTime[9] for a 5-element array, the test may generate strange results. Or the test may crash and report “Program end never reached”, in which case the system doesn’t print the test case that caused the reported message.
#include
using namespace std;
int main() {
const int NUM_VALS = 4;
int testGrades[NUM_VALS];
int i = 0;
int sumExtra = -9999; // Assign sumExtra with 0 before your for loop
testGrades[0] = 101;
testGrades[1] = 83;
testGrades[2] = 107;
testGrades[3] = 90;
sumExtra = 0;
for (i = 0; i < NUM_VALS; ++i) {
if (testGrades[i] >= 101) {
sumExtra += (testGrades[i] – 100);
}
}
cout << "sumExtra: " << sumExtra << endl; return 0; }
using namespace std;
int main() {
const int NUM_VALS = 4;
int hourlyTemp[NUM_VALS];
int i = 0;
hourlyTemp[0] = 90;
hourlyTemp[1] = 92;
hourlyTemp[2] = 94;
hourlyTemp[3] = 95;
for (i = 0; i < NUM_VALS; ++i) { if (i == NUM_VALS - 1) { cout << hourlyTemp[i]; } else { cout << hourlyTemp[i] << ", "; } } cout << endl; return 0; }
90, 92, 94, 95
Note that the last element is not followed by a comma, space, or newline.
using namespace std;
int main() {
const int NUM_VALS = 4;
int userValues[NUM_VALS];
int i = 0;
int matchValue = 0;
int numMatches = 0; // Assign numMatches with 0 before your for loop
userValues[0] = 2;
userValues[1] = 2;
userValues[2] = 1;
userValues[3] = 2;
matchValue = 2;
numMatches = 0;
for (i = 0; i < NUM_VALS; ++i) { if (userValues[i] == matchValue){ ++numMatches; } } cout << "matchValue: " << matchValue << ", numMatches: " << numMatches << endl; return 0; }
using namespace std;
int main() {
const int NUM_ELEMENTS = 8; // Number of elements
int userVals[NUM_ELEMENTS]; // User values
int i = 0; // Loop index
// Prompt user to input values
cout << "Enter " << NUM_ELEMENTS << " integer values..." << endl;
for (i = 0; i < NUM_ELEMENTS; ++i) {
cout << "Value: " << endl;
cin >> userVals[i];
}
for (i = 0; i < NUM_ELEMENTS; ++i) { userVals[i] = userVals[i] * 2; } // Print numbers cout << "New numbers: "; for (i = 0; i < NUM_ELEMENTS; ++i) { cout << userVals[i] << " "; } return 0; }
#include
using namespace std;
int main() {
const int SCORES_SIZE = 4;
vector
int i = 0;
lowerScores.at(0) = 5;
lowerScores.at(1) = 0;
lowerScores.at(2) = 2;
lowerScores.at(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.at(i) << " "; } cout << endl; return 0; }
using namespace std;
int main() {
const int SCORES_SIZE = 4;
int oldScores[SCORES_SIZE];
int newScores[SCORES_SIZE];
int i = 0;
oldScores[0] = 10;
oldScores[1] = 20;
oldScores[2] = 30;
oldScores[3] = 40;
for (i = 0; i < SCORES_SIZE; i++) { if (i == (SCORES_SIZE - 1)) { newScores[i] = oldScores[0]; } else { newScores[i] = oldScores[i + 1]; } } for (i = 0; i < SCORES_SIZE; ++i) { cout << newScores[i] << " "; } cout << endl; return 0; }
Note: These activities may test code with different test values. This activity will perform two tests, the first with a 4-element array (newScores = {10, 20, 30, 40}), the second with a 1-element array (newScores = {199}). See How to Use zyBooks.
Also note: If the submitted code tries to access an invalid array element, such as newScores[9] for a 4-element array, the test may generate strange results. Or the test may crash and report “Program end never reached”, in which case the system doesn’t print the test case that caused the reported message.
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 < dataPoints[i]; ++i) { if (dataPoints[i] < minVal) { dataPoints[i] = dataPoints[i] * 2; } } for (i = 0; i < NUM_POINTS; ++i) { cout << dataPoints[i] << " " ; } cout << endl; return 0; } ONLY ONE SOLUTION
using namespace std;
int main () {
const int NUM_ELEMENTS = 14; // Number of elements
double letterWeights[NUM_ELEMENTS]; // Weights in ounces
int postageCosts[NUM_ELEMENTS]; // Costs in cents (usps.com 2013)
double userLetterWeight = 0.0; // Letter weight
bool foundWeight = false; // Found weight specified by user
int i = 0; // Loop index
// Populate letter weight/postage cost arrays
letterWeights[i] = 1; postageCosts[i] = 46; ++i;
letterWeights[i] = 2; postageCosts[i] = 66; ++i;
letterWeights[i] = 3; postageCosts[i] = 86; ++i;
letterWeights[i] = 3.5; postageCosts[i] = 106; ++i;
letterWeights[i] = 4; postageCosts[i] = 152; ++i;
letterWeights[i] = 5; postageCosts[i] = 172; ++i;
letterWeights[i] = 6; postageCosts[i] = 192; ++i;
letterWeights[i] = 7; postageCosts[i] = 212; ++i;
letterWeights[i] = 8; postageCosts[i] = 232; ++i;
letterWeights[i] = 9; postageCosts[i] = 252; ++i;
letterWeights[i] = 10; postageCosts[i] = 272; ++i;
letterWeights[i] = 11; postageCosts[i] = 292; ++i;
letterWeights[i] = 12; postageCosts[i] = 312; ++i;
letterWeights[i] = 13; postageCosts[i] = 332; ++i;
// Prompt user to enter letter weight
cout << "Enter letter weight (in ounces): " << endl;
cin >> userLetterWeight;
// Postage costs is based on smallest letter weight greater than
// or equal to mailing letter weight
foundWeight = false;
for (i = 0; (i < NUM_ELEMENTS) && (!foundWeight); ++i) { if( userLetterWeight <= letterWeights[i] ) { foundWeight = true; cout << "Postage for USPS first class mail is "; cout << postageCosts[i] << " cents" << endl; cout << "The next higher weight is " << letterWeights[i + 1] << " with a cost of " << postageCosts[i + 1] << " cents." << endl; } } if( !foundWeight ) { cout << "Letter is too heavy for USPS first class mail." << endl; } return 0; }
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) { origList[i] + offsetAmount[i]; cout << origList[i] + offsetAmount[i] << " "; } cout << endl; return 0; }
45 57 63 70
using namespace std;
int main() {
const int SIZE_LIST = 4;
int keysList[SIZE_LIST];
int itemsList[SIZE_LIST];
int i = 0;
keysList[0] = 42;
keysList[1] = 105;
keysList[2] = 101;
keysList[3] = 100;
itemsList[0] = 10;
itemsList[1] = 20;
itemsList[2] = 30;
itemsList[3] = 40;
for (i < 0; i < SIZE_LIST; ++i) {
if (keysList[i] > 100) {
cout << itemsList[i] << " ";
}
}
cout << endl;
return 0;
}
20 30
Since keysList[1] and keysList[2] have values greater than 100, the value of itemsList[1] and itemsList[2] are printed.
#include
using namespace std;
int main() {
vector
// Populate vector
runTimes.at(0) = 800;
runTimes.at(1) = 775;
runTimes.at(2) = 790;
runTimes.at(3) = 805;
runTimes.at(4) = 808;
cout << runTimes.at(0) << endl; cout << runTimes.at(1) << endl; cout << runTimes.at(2) << endl; return 0; }
800
775
790
Note: These activities may test code with different test values. This activity will perform two tests, the first with a 5-element vector (vector
Also note: If the submitted code tries to access an invalid vector element, such as runTime.at(9) for a 5-element vector, the test may generate strange results. Or the test may crash and report “Program end never reached”, in which case the system doesn’t print the test case that caused the reported message.
#include
using namespace std;
int main() {
const int NUM_VALS = 4;
vector
int i = 0;
courseGrades.at(0) = 7;
courseGrades.at(1) = 9;
courseGrades.at(2) = 11;
courseGrades.at(3) = 10;
for (i = 0; i < NUM_VALS; ++i) {
cout << courseGrades.at(i) << " ";
}
cout << endl;
for (i = NUM_VALS - 1; i >= 0; i–) {
cout << courseGrades.at(i) << " ";
}
cout << endl;
return 0;
}
7 9 11 10
10 11 9 7
Hint: Use two for loops. Second loop starts with i = NUM_VALS – 1.
Note: These activities may test code with different test values. This activity will perform two tests, the first with a 4-element vector (vector
Also note: If the submitted code tries to access an invalid vector element, such as courseGrades.at(9) for a 4-element vector, the test may generate strange results. Or the test may crash and report “Program end never reached”, in which case the system doesn’t print the test case that caused the reported message.
using namespace std;
struct PatientData {
int heightInches;
int weightPounds;
};
int main() {
PatientData lunaLovegood;
lunaLovegood.heightInches = 63;
lunaLovegood.weightPounds = 115;
cout << "Patient data: " << lunaLovegood.heightInches << " in, " << lunaLovegood.weightPounds << " lbs" << endl; return 0; }
Patient data: 63 in, 115 lbs
using namespace std;
struct InventoryTag {
int itemID;
int quantityRemaining;
};
int main() {
InventoryTag redSweater;
cout << "Inventory ID: "
<< redSweater.itemID << ", ";
cout << "Qty: "
<
Inventory ID: 314, Qty: 500
Chapter 6 C++ Challenge Problems. (2018, Oct 20). Retrieved from https://artscolumbia.org/chapter-6-c-challenge-problems-36867-61250/