Get help now
  • Page 1
  • Words 16
  • Views 206
  • Download

    Cite

    Bernadette
    Verified writer
    Rating
    • rating star
    • rating star
    • rating star
    • rating star
    • rating star
    • 4.9/5
    Delivery result 4 hours
    Customers reviews 247
    Hire Writer
    +123 relevant experts are online

    Chapter 6 C++ Challenge Problems

    Academic anxiety?

    Get original paper in 3 hours and nail the task

    Get help now

    124 experts online

    #include
    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; }

    Write three statements to print the first three elements of array runTimes. Follow each statement with a newline. Ex: If runTime = {800, 775, 790, 805, 808}, print:
    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.

    INCOMPLETE

    #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; }

    Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total extra credit received. Full credit is 100, so anything over 100 is extra credit. Ex: If testGrades = {101, 83, 107, 90}, then sumExtra = 8, because 1 + 0 + 7 + 0 is 8.
    #include
    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; }

    Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a comma and space. Ex: If hourlyTemp = {90, 92, 94, 95}, print:
    90, 92, 94, 95
    Note that the last element is not followed by a comma, space, or newline.
    #include
    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; }

    Set numMatches to the number of elements in userValues (having NUM_VALS elements) that equal matchValue. Ex: If matchValue = 2 and userValues = {2, 2, 1, 2}, then numMatches = 3.
    #include
    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; }

    Complete the following program to double each number in the array.
    #include
    #include
    using namespace std;

    int main() {
    const int SCORES_SIZE = 4;
    vector lowerScores(SCORES_SIZE);
    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; }

    Write a loop that subtracts 1 from each element in lowerScores if the original element was greater than 0, and otherwise just assigns the element with 0. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}.
    #include
    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; }

    Write a loop that sets newScores to oldScores shifted once left, with element 0 copied to the end. Ex: If oldScores = {10, 20, 30, 40}, then newScores = {20, 30, 40, 10}.

    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.

    #include
    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

    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
    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; }

    Improve the program by also outputting “The next higher weight is ___ with a cost of ___ cents”.
    #include
    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; }

    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:
    45 57 63 70
    #include
    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; }

    For any element in keysList with a value greater than 100, print the corresponding value in itemsList, followed by a space. Ex: If keysList = {42, 105, 101, 100} and itemsList = {10, 20, 30, 40}, print:
    20 30
    Since keysList[1] and keysList[2] have values greater than 100, the value of itemsList[1] and itemsList[2] are printed.
    #include
    #include
    using namespace std;

    int main() {
    vector runTimes(5);

    // 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; }

    Write three statements to print the first three elements of vector runTimes. Follow each with a newline. Ex: If runTime = {800, 775, 790, 805, 808}, print:
    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 runTimes(5)), the second with a 4-element vector (vector runTimes(4)). See How to Use zyBooks.

    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
    #include
    using namespace std;

    int main() {
    const int NUM_VALS = 4;
    vector courseGrades(NUM_VALS);
    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; }

    Write a for loop to print all NUM_VALS elements of vector courseGrades, following each with a space (including the last). Print forwards, then backwards. End with newline. Ex: If courseGrades = {7, 9, 11, 10}, print:
    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 courseGrades(4)), the second with a 2-element vector (vector courseGrades(2)). See How to Use zyBooks.

    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.

    #include
    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; }

    Declare a struct named PatientData that contains two integer data members named heightInches and weightPounds. Sample output for the given program:
    Patient data: 63 in, 115 lbs
    #include
    using namespace std;

    struct InventoryTag {
    int itemID;
    int quantityRemaining;
    };

    int main() {
    InventoryTag redSweater;

    cout << "Inventory ID: " << redSweater.itemID << ", "; cout << "Qty: " <

    Write a statement to print the data members of InventoryTag. End with newline. Ex: if itemID is 314 and quantityRemaining is 500, print:
    Inventory ID: 314, Qty: 500

    This essay was written by a fellow student. You may use it as a guide or sample for writing your own paper, but remember to cite it correctly. Don’t submit it as your own as it will be considered plagiarism.

    Need custom essay sample written special for your assignment?

    Choose skilled expert on your subject and get original paper with free plagiarism report

    Order custom paper Without paying upfront

    Chapter 6 C++ Challenge Problems. (2018, Oct 20). Retrieved from https://happyessays.com/chapter-6-c-challenge-problems/

    Hi, my name is Amy 👋

    In case you can't find a relevant example, our professional writers are ready to help you write a unique paper. Just talk to our smart assistant Amy and she'll connect you with the best match.

    Get help with your paper
    We use cookies to give you the best experience possible. By continuing we’ll assume you’re on board with our cookie policy