Lab 2 Questions
Lab 2 Questions
Variable syntax
Input / Output
Basic input / output requires the iostream library to be included at the top of the C++ file. The library will
provide the cin function (for input) and the cout function (for output). cin will read user inputs from the
keyboard and cout will display unto the screen (of the command prompt).
Task 1
Dev C++
CodeBlocks
1) Install CodeBlocks.. There are different versions of CodeBlocks available from here:
http://www.codeblocks.org/downloads/26
P a g e 1 | 11
LAB PROGRAM DESIGN | TPD 4124
2) After installation, test the software with simple code to confirm the software is workable.
File >> New >> Empty File
P a g e 2 | 11
LAB PROGRAM DESIGN | TPD 4124
int main(){
cout<<"Hello";
}
P a g e 3 | 11
LAB PROGRAM DESIGN | TPD 4124
4) Press F9 to build and run. Or you may click Build >> Build and Run. This is to compile to check the
program. If there is an error, error message will be given. Else, it is fine to proceed.
5) If your file is not saved, it will prompt with the Save As screen.
P a g e 4 | 11
LAB PROGRAM DESIGN | TPD 4124
Be careful here. The default extension for the file name is “.c”. This is a C program file, not C++
program file. In order to be saved as a C++ program file, you have to make sure the extension is
“.cpp”. So let’s save the file as “test.cpp”. Then click Save.
Task 2
In order to properly use the C++ language to solve problems, we would definitely need to use variables.
These variables can be used to store values for mathematical calculations, record keeping, for further
editing, etc.
Identifier
To create a variable we must provide it with a name or an identifier. An identifier can only be made up of
letters (a, b… z, A, B... Z), digits (0, 1…9), or underscore character (_). An identifier also CANNOT begin with
a digit (e.g. 7grades is wrong).
Sample usable identifiers are userBMI, student_name, result, counter, and computerProgramming1.
Variable identifiers must also avoid C++ reserved keywords such as if, else, while, for, exit, etc.
P a g e 5 | 11
LAB PROGRAM DESIGN | TPD 4124
Data type
To create a variable we must also declare its data type. The basic data types are as follows:
Code snippet 1:
Task 3
Before you start using any variables, you first need to declare them. A variable can be global or local.
Global variables are declared outside of the main body or any other function bodies. A local variable is
declared inside a function body.
P a g e 6 | 11
LAB PROGRAM DESIGN | TPD 4124
A body is easily identified by the opening and closing curly braces { }. So any variables declared inside a
pair of { } is a local variable and only visible inside the { }. The variable will no longer exist when we leave
the { }.
Code snippet 2:
Explain:
1) Why is it possible to re-declare the
variable inside the { }
Global variables are declared as below. Try to guess the output before execution.
Code snippet 3:
Explain:
1) Must we always assign values into a char
variable using ‘ ‘ ?
P a g e 7 | 11
LAB PROGRAM DESIGN | TPD 4124
Task 4
Input / output commands are used in almost all C++ programs. They are supplied by the iostream
library.
cin syntax
cin>> variable; // single user input
cin>> variable1 >> variable2; // multiple user input
cout syntax
cout<< “hello world” <<endl; // multiple output
<< is called the insertion operator. This operator is used to put data into the output stream, cout.
>> is called the extraction operator. This operator is used to get data from the user and put into the
variable.
Code snippet 4:
Explain:
1) In the first cout, why must you use
““?
P a g e 8 | 11
LAB PROGRAM DESIGN | TPD 4124
Question 1
Write a program that asks the user for the number of males and the number of females registered
in a class. The program should compute and report what percentage of the student s are males
and what percentage are females. Display the output with two decimal points. If you
remembered to convert the decimal result of each calculation to percent form when you
displayed it, the two values should add up to 100.00 percent.
Question 2
Circular velocity refers to the velocity that one object must travel in order to maintain its circular
orbit around another object, usually a planet or other gravitating mass. The circular velocity of
an object is calculated by dividing the circumference of the circular path by the time period over
which the object travels.
P a g e 9 | 11
LAB PROGRAM DESIGN | TPD 4124
Question 3
A movie theater only keeps 80 percent of the revenue earned from ticket sales. The other 20
percent goes to the distributor. Write a program that calculates a theater's gross and net box
office revenue for a night. The program should ask for the name of the movie, and how many
adult and child tickets were sold. (The price of an adult ticket is $10 and a child's ticket is $6.) It
should display a report similar to the following:
Note: Can store some values that are fixed using constant variables.
Question 4
Basal Metabolic rate (BMR) is the number of calories you would burn without doing any
activities. The formula:
BMR = 66 + (6.23 x weight in pounds) + (12.7 x height in inches) – (6.8 x age)
Question 5
[Ingredients adjuster program ] A cookie recipe calls for the following ingredients:
Write a program that asks the user how many cookies he or she wants to make and then displays
the number of cups of each ingredient needed for the specified number of cookies.
Sample output 1
Sample output 2
P a g e 11 | 11