



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Basic exercise from Computing in Engineering and Science course. Keywords in this course are: Writing Simple C++ Programs, Keyboard Input and Screen Output in C++, Output Command, Preparing the File to Submit for This Exercise, Programming Tasks, Simple Input and Output with Type Double, Output
Typology: Exercises
1 / 6
This page cannot be seen from the preview
Don't miss anything!




This exercise gives you practice in writing simple C++ programs. It should familiarize you with the basic structure that you need to use for all your C++ programs as well as the basic approaches for getting output to the screen and getting input from the keyboard.
From the first exercise, you should be familiar with the basic structure of a simple C++ program shown below.
#include
int main() { // program statements go here
return EXIT_SUCCESS; }
You will use this basic structure for all your C++ programs. The purpose of each statement in this basis structure is described below.
The#include
successfully. The value of EXIT_SUCCESS is zero and you can simply write return 0; at the end of the program.
In addition to this basic program structure, you should include comment statements at the start of the program to describe its purpose and other information such as the programmer’s name and the date the program was written. See exercise one for examples of such comments.
In this exercise, you will use the basic structure to write a number of simple C++ programs. You can simply replace the line, // program statements go here, by the commands that you need.
Use the cout command with the output operator (sometimes called the insertion operator, <<) for screen output and the cin command with the input operator (sometimes called the extraction operator, >>) for keyboard input. Here some basic rules.
A single cout command can have one or more output (<<) operators to output one or more items to the screen.
The command cout << “
The command cout << x;, where x is a variable, will write the value of the variable to the screen. E.g., the statements int x = 2; cout << x; will write 2 to the screen.
User input is almost always preceded by a cout command instructing the user on what to input. E.g., cout << “Enter a current in milliamps between 0.1 and 10: “; cin << current;
You can enter several variables with a single cin command. Separate your entries by a space (or the enter key) and press the enter key after you enter the last data item. E.g., cout << “Enter x, y, and z: “; cin >> x >> y >> z; if you entered 3.2 5. 16 and then pressed the enter key you would set x = 3.2, y = 5.1, and z = 16.
Thecout command does not provide any spacing between output values. You must provide this yourself. You can provide space by outputting a blank string, a series of blanks surrounded by quotation marks. You can also use special characters \t or \n to insert a tab or to start a new line, respectively. The four letters endl (short for endline) inserted in an output stream also move the output to a new line. For example, if x = 13.2 and y = 0.123, the output results from various cout commands are shown in the table below.
Output command Screen Output cout << x << y; 13.20. cout << x << “ “ << y; 12.2 0. cout << x << “\t“ << y; 12.2 0. cout “x = “ << x << “ and y = “ << y; x = 13.2 and y = 0. cout “x = “ << x << “\ny = “ << y; x = 13. cout “x = “ << x << endl << y = “ << y; y = 0.
This exercise has five tasks, which are slight variations of the same program. This paragraph will describe how you can create a single file, called the submission file, to which you will copy all your listings and all your results.
You should define a new project for this exercise. You can use the same program name (and project name) for all the tasks listed here. Follow the procedure that you learned in the first
Task one: Simple input and output with type double – copy and compile the following code.
#include
int main() { double x, y, z; cout << "Enter a value for x: "; cin >> x; cout << "Enter a value for y: "; cin >> y; z = x / y; cout << "For x = " << x << " and y = " << y << ", x / y = " << z << "\n\n"; return EXIT_SUCCESS; }
Execute the code for the following input pairs: (1) x = 1, y = 2, (2) x = 1, y = 0, (3) x = 1, y = 3; (4) x = 10 -6, y = 10 6 , (5) x = 6, y = 3, (6) x = 6, y = 4. Copy the code and all output from the screen to the submission file. Note that a number like 5.6x10 -6^ can be entered as 5.6E-6. The number 10 -6, which is really shorthand for 1x10 -6^ should be entered as 1E-6. Similarly, the input for 10 6 would be 1E6. (You could also enter 10 -6and 10 6 as, respectively, 0.000001 and 1000000.)
Task two: Simple input and output with type int – modify the code so that the first statement is reads int x, y, z; compile the code and repeat the execution with the input values shown above. Copy the code and all output to the submission file. Make sure that you understand why the results from this task are different from those in task one.
Task three: Alternative input approach – modify the code in task two so that the user sees a single input prompt asking the user values for x and y. The user should then enter two values. Run this program for a single pair of input values. Copy the code and the output to the submission file
Task four: Output spacing – remove the statement in the code in task three that prints the results to the screen. (Do not change or remove the input prompt.) Use the following statement in place of the deleted results-output statement: cout << x <<y << z; run this program for x = 8 and y = 4; copy the code and the screen to the submission file. Fix the new output statement so that there is spacing between the numbers on the output. After you make the correction, run the program for the same input values. Once you have the result you want, copy the modified code and the screen output to the submission file.
Task five: Still more output spacing – modify the output statement in the code from task four so that the screen looks like the following when you are done (place your name and today’s date in the second output line):
Results for programming exercise 2:
Larry Caretto February 9, 2006
Input value of x = 10 Input value of y = 3 Output value of z = x / y = 3.
Press any key to continue
In particular, the output has the following features: (1) there is a blank line after the title line, the name line, and the line with the value of z; (2) the final equal signs in the last three lines are in the same column, (3) the last line starts two spaces to the left of the title line and the name line, and (4) the end of the date line is aligned with the end of the line for the output value. Copy the code for this task and the output screen to the submission file. HINT: It is easier to use spaces than the “\t” tab spacing.
Submit a copy of your submission file with the elements specified above. Make sure that your name is at the top of your submission file. Edit the submission file to show where one task ends and the other begins. Here is a sample of what your submission file should look like.
Sample submission file for exercise two Larry Caretto Comp 106 Submitted February 9, 2006
Task one code
#include
int main() { double x, y, z; cout << "Enter a value for x: "; cin >> x; cout << "Enter a value for y: "; cin >> y; z = x / y; cout << "For x = " << x << " and y = " << y << ", x / y = " << z << "\n\n"; return EXIT_SUCCESS; }
Task one output, first input data set
Enter a value for x: 3 Enter a value for y: 9 For x = 3 and y = 9, x / y = 0.
Task one output, second input data set
Enter a value for x: 5 Enter a value for y: 3 For x = 5 and y = 3, x / y = 1.
Remaining task one output not shown
Task two code
Task two code not shown
Task two output, first input data set