Vous êtes sur la page 1sur 9

Assignment1

Topics
Output
cout

Description
Write a program that will display the following
three-line message five times. After printing the
three-line message, print an empty line before
printing the three-line message again.

Today is a good day.


I am learning C++ programming.
It is a lot of fun.

Requirements
Print the above message five times each time using a
different method. The different methods to be used
are listed below. For an example of the use of these
five methods, see the sample code in the Sample
Code section below.

1. Use multiple cout and use endl for newline.


2. Use multiple cout and use \n for newline.
3. Use a single cout and use endl for newline.
4. Use a single cout and use \n for newline.
5. Use a variable for storing the whole message
and then display the content of the variable.

Submit
A copy of the output.
A copy of the source code.

How to copy a program output in a MS Word


document
When you run a program in Code Block on the PC,
its output appears in a black window called the
Console window. To copy contents from the
Console window into a word document, follow the
following procedure.

1. Click on the Console Window Menu Icon (It


is located on the top left corner of the console
window and it is a small white square with a
dark greenish square inside it).
(A drop-down menu will appear)
2. From the drop-down menu, select Edit |
Mark.
(A cursor in the console window will start
blinking)
3. Using the mouse and the blinking cursor,
highlight the area in the console window that
you want to copy.
4. After highlighting, click the Console
Window Menu Icon
(a drop-down menu will appear)
5. From the drop-down menu, select Edit |
Copy.
(The contents of the highlighted area will be
copied in the computer’s memory)
6. Open the word document in which you want
to copy these contents.
7. Click in the word document where you want
to copy these contents
8. Click ctrl-v (Ctrl and V keys together) or
select Paste (Edit | Paste) from the MS Word
menu options
(The contents will appear in the word document)
Sample Code
The sample program below displays the following
three-line message with an empty line at the end. It
displays this message five times - each time using a
different method.

Hi1
Hi2
Hi3

#include< iostream>
#include< string>
using namespace std;
int main ( )
{
//method 1
cout << "Hi1" << endl;
cout << "Hi2" << endl;
cout << "Hi3" << endl << endl;

//method 2
cout << "Hi1\n";
cout << "Hi2\n";
cout << "Hi3\n\n";

//method 3
cout << "Hi1" << endl
<< "Hi2" << endl
<< "Hi3" << endl << endl;

//method 4
cout << "Hi1\nHi2\n"
<< "Hi3\n\n";

//method 5
string out;
out = "Hi1\n";
out = out + "Hi2\n";
out = out + "Hi3\n\n";
cout << out;

return 0;
}
Assignment 2

Topics

input
output

Description

Write a program that will input some of user's personal information and then display it back to
the user.

First, the program will ask the user to enter name. Then it will ask the user to enter address. Then
it will ask the user to enter phone number. Then it will ask the user to enter email. At the end, it
will display the user’s name, address, phone number and email. See the Testing section below.

Testing

Test Run
(User input is shown in bold).

Enter Name:
John Doe
Enter Address:
321 Golf Club Road, Pleasant Hill, CA 94523
Enter Phone Number:
925-685-1230
Enter Email:
jdoe@dvc.edu

User Information:
Name: John Doe
Address: 321 Golf Club Road, Pleasant Hill, CA 94523
Phone: 925-685-1230
Email: jdoe@dvc.edu

Submit

The final output titled "User Information",


Source code

Sample Code
/*
The sample code below inputs user's name and address and then displays it back to the user
under the title Personal Information.
*/

string name, addr;

//Input name
cout << "Enter Name:" << endl;
getline (cin, name);

//Input address
cout << "Enter Address:" << endl;
getline (cin, addr);

//Display output;
cout << "Personal Information" << endl;
cout << "Name: " << name << endl;
cout << "Address: " << addr << endl;
Assignment 3

Topics
Inputting numeric data
Outputing numeric data
Assignment statement
Expressions
Evaluating an expression

Description
Write a program that computes the sum and the average of any three decimal numbers provided
by the user.

The program asks the user to input three decimal numbers one at a time. It will compute the sum
and the average of the three numbers. Then it will display the three numbers and their sum in one
line and the three numbers and their average in a second line. See the Test Run in the Testing
section.

Testing
Use the test data in Test Run below:

Test Run
(User input is shown in bold).

Enter the first number:


2.5
Enter the second number:
3.3
Enter the third number:
4.4

The sum of 2.5, 3.3 and 4.4 is 10.2


The average of 2.5, 3.3 and 4.4 is 3.4

Submit
The final two-line output
The source code

Sample Code

//declare double variables for holding decimal values


double n1, n2, n3, sum, avg;

//Input numbers
cout << "Enter the first number" << endl;
cin >> n1;
cout << "Enter the second number" << endl;
cin >> n2;
cout << "Enter the third number" << endl;
cin >> n3;

//compute the sum of the numbers and save it in the variable sum
sum=n1+n2+n3;

//compute the average and save it in the variable avg

//display the first line of the output


cout << "The sum of " << n1 << ", " << n2 << " and " << n3 << " is " << sum << endl;

//display the second line of the output.

Vous aimerez peut-être aussi