LAB 2.1 WORKING WITH THE cout
STATEMENT
Exercise 1: Copy and paste
the program name.cpp into Visual Studio IDE. Fill in the code
so that the program will do the following:
Print
your first and last name in one line on the screen.
Print
your address on the next line (recall the function of the endl manipulator).
Print
your city, state and zip on the next line.
Print
your telephone number on the next line.
Remember
that to output a literal, such as "Hello", you
must use quotes.
Compile
and run the program.
An Example of Running Result:
Deano Beano
123 Markadella Lane
Fruitland, Md. 55503
489-555-5555
The
code for name.cpp is as follows:
// This
program will write the name, address and telephone
// number of the
programmer.
#include <iostream>
using
namespace std;
int main()
{
// Fill in an cout statement below to write your first and last name
// Fill in an cout statement below to write your address (on new line)
// Fill in an cout statement below to write you city, state and zip (on
new line)
// Fill in an cout statement below to write your telephone number (on new
line)
return
0;
}
Exercise 2: Change the
program so that the following is printed. Try to get the spacing just like the following
example (recall the function of \t). Compile and
run the program.
*************************************
Programmer: Deano Beano
123 Markadella Lane
Fruitland,
Md. 55503
Telephone:
489-555-5555
*************************************
(Print out the program and the running
result for exercise 2, and hand them in with the rest of the lab.)
LAB 2.2 WORKING WITH CONSTANTS, VARIABLES
AND ARITHMETIC
OPERATORS
Exercise 1: Copy and paste
the file circlearea.cpp into Visual Studio IDE. The code of circlearea.cpp
is as follows:
// This
program will output the circumference and area
// of the circle with a
given radius.
#include <iostream>
using
namespace std;
const
double PI = 3.14; //
Define a constant PI=3.14
const
double RADIUS = 5.4; //
Define a constant RADIUS=5.4
int main()
{
// Fill in a variable type in the blank space below (replace the
underlined blank with a proper keyword)
__________
area; //
define the variable area (of circle)
float
circumference; // define
the variable circumference (of circle)
circumference
= 2 * PI * RADIUS; //
computes circumference
// Fill in the code in the blank space below to calculate the area
of the circle
// replace the underlined blank with a proper expression
area
= ________; //
computes area
// Fill in the code for the cout statement below that will output (with description) the
circumference
// Fill in the code for the cout statement below that will output (with description) the area of the
circle
return
0;
}
Exercise 2: Fill in the
blanks and the cout statements so that the output will produce the following:
The circumference of the circle is
33.912
The area of the circle is 91.5624
(Print out the program and the running
result for exercise 2, and hand them in with the rest of the lab.)
Lab 2.3 and Lab 2.4 will follow.
LAB 2.3 RECTANGLE AREA AND
PERIMETER
Exercise 1: Using Lab 2.2 as
an example, develop a program that will determine the area and perimeter of a
rectangle. The length and width can be given as constants. (LENGTH=8
WIDTH=3)
Exercise 2: Compile and run
your program. Continue to work on it until you get the following output.
The area of the rectangle is
24
The perimeter of the rectangle
is 22
(Print out the program and the running
result, and hand them in with the rest of the lab.)
LAB 2.4 WORKING WITH CHARACTERS
AND STRINGS
Exercise
1: Copy
and paste the file stringchar.cpp into
Visual Studio IDE. This program illustrates the use of characters and strings.
The char data type allows only one character to be
stored in its memory location. The string data type (actually a class and not a true data type built into
the language) allows a sequence of characters to be stored in one memory
location. The code follows:
// This
program demonstrates the use of characters and strings
#include <iostream>
#include <string>
using
namespace std;
// Definition of
constants
const
string FAVORITESODA = "Dr. Dolittle"; // use double quotes for
strings
const char
BESTRATING = 'A'; //
use single quotes for characters
int main()
{
char
rating; //
2nd highest product rating
string
favoriteSnack; //
most preferred snack
int numberOfPeople; //
the number of people in the survey
int topChoiceTotal; //
the number of people who prefer the top choice
// Fill in the code to do the following:
// Assign the value of "crackers" to favoriteSnack
// Assign a grade of 'B' to rating
// Assign the number 250 to the numberOfPeople
// Assign the number 148 to the topChoiceTotal
// Fill in the blanks of the following with the appropriate
variables or named constants
// to print the appropriate values:
cout << "The preferred soda is "
<< _____________ << endl;
cout << "The preferred snack is "
<< ____________ << endl;
cout << "Out of " << _______ << " people "
<< _________ << " chose these items!" << endl;
cout << "Each of these products were given
a rating of " << ___________;
cout << " from our expert tasters"
<< endl;
cout << "The other products were rated no
higher than a " << rating << endl;
return
0;
}
Exercise 2: Fill in the
indicated code, then compile and run the program. Continue to work on the program
until you have no syntax, run-time, or logic errors.
The
output should look similar to the following:
The preferred soda is Dr. Dolittle
The preferred snack is crackers
Out of 250 people 148 chose these items!
Each of these products were given a rating
of A from our expert tasters
The other products were rated no higher
than a B
(Print out the program and the running
result, and hand them in with Lab 2.1, 2.2 and 2.3.)