Hands On Day 2

1. Create a new project and enter the program below:

#include <iostream>
using namespace std;

int main( )
    int number_of_pods, peas_per_pod, total_peas;
    cout << "Press return after entering a number.\n";
    cout << "Enter the number of pods:\n";
    cin >> number_of_pods;
    cout << "Enter the number of peas in a pod:\n";
    cin >> peas_per_pod;

    total_peas = number_of_pods * peas_per_pod;
    //number_of_pods * peas_per_pod = total_peas;
    cout << "If you have ";
    cout << number_of_pods;
    cout << " pea pods\n";
    cout << "and ";
    cout << peas_per_pod;
    cout << " peas in each pod, then\n";
    cout << "you have ";
    cout << total_peas;
    cout << " peas in all the pods.\n";
    return 0;

2. Modify the program created above. Change the program so that it first writes the word Hello to the screen and then goes on to do the same things as before. You will only have to add one line to the program to make this happen. Recompile the changed program and run the changed program. Then change the program even more. Add one more line that will make the program write the word Good-bye to the screen at the end of the program. Be certain to add the symbol endl to the last output statement so that it reads as follows:

cout <<  "Good-bye" << endl;

Recompile and run the changed program.

3. Further modify the program that you already have modified above. Change the multiplication sign * in your C++ program to a division sign /. Recompile the changed program. Run the program. Enter a zero input for “number of peas in a pod.” Notice the run time error message due to division by zero.

4. Modify the C++ program that you above once more. Change the multiplication sign * in your C++ program to an addition sign +. Recompile and run the changed program. Notice that the program compiles and runs perfectly fine, but the output is incorrect. That is because this modification is a logic error.

Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2017-01-19 - JimSkon
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback