Lab 3
Lab 3
BASIC ELEMENTS OF A
COMPUTER PROGRAM
Learning Outcomes
After completing this lab, you will be able to:
o describe an identifier concept
o distinguish between variable and constant
o select an appropriate name, data type and initial value for a memory location
o use arithmetic operators
A. PRE-LAB ACTIVITIES.
Question 1
List TWO (2) rules for naming an identifier.
Answer
-Cannot begin with digit
-Must have no space
-Cannot have have reserve word
-It cannot contains any symblos
Question 2
Determine which of the following are valid identifiers?
Identifier Answer:
Answer
Question 4
Which of the following is a reserved word in C++?
Identifier Answer:
example number no
a. integer no
……………….
b. long ……………….
no
c. float yes
……………….
d. single no
……………….
e. double yes
……………….
f. char yes
……………….
Question 5
What is the difference between variable and constant?
Answer
Question 6
Write a constant declaration for each of the following:
Answer:
example A constant that holds the maximum value of const int max = 30;
integer 30.
a. A constant that holds the value of character const char character = 'G'
‘G’. ……………….
b. A constant that holds the value of pi.
const float PI = 3.142
……………….
c. A constant that contains 1200 peoples living const int people = 1200
in Shah Alam. ……………….
Question 7
Give the most appropriate data type for each of the following values:
Answer:
Question 8
Create a variable name for each of the following and give the appropriate data type:
Question 9
How do the following two statements differs:
1)char ch = ‘A’;
2)char ch = 65;
Question 10
For the following short scenario, list the variables that you need to solve the problem. In
your list, include the variable name, its data type and the declaration statement that you
would use.
Your friend asked you to write a program that will calculate the area of rectangle and the
total price of a tile. The program will receive a length and width, in feet, of a rectangle, and
the price of a square foot of tile.
#include <iostream>
variable : length
using namespace std;
data type: int,float,double
int main()
declare : float length
{
int length,width,price,area;
Answer width
cout << "Enter length = ";
int,float,double
cin>>length;
float width
cout << "Enter width = ";
price
cin >> width;
int,float,double
cout << "Enter price of square foot of tile = ";
float price
cin>>price;
area=length*width;
area
cout<<"the area is = "<<area<<endl;
int,float,double
cout<<"the total price is = "<<price*area;
float area
}
Question 11
Convert each of the following mathematical formulas to C++ expression:
Answer:
a. 3x 3*x
……………….
b. 6x + 2y (6*x)+(2*y)
……………….
c. x+y
(x+y)/6
6 ……………….
2
d. b – 4ac
2
((pow(b,2))-4*a*c)/2
……………….
e. x(y + z) x*(y+z)
……………….