[go: up one dir, main page]

0% found this document useful (0 votes)
43 views4 pages

Lab 3

The document summarizes a lab exercise on basic elements of a computer program. It includes 11 questions about identifiers, variables, constants, data types, arithmetic operators, and converting mathematical formulas to C++ expressions. The questions cover naming rules, valid/invalid identifiers, differences between identifiers and keywords, reserved words in C++, differences between variables and constants, appropriate data types for given values, creating variables with names and data types, and representing formulas in code.

Uploaded by

pandamalaya10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views4 pages

Lab 3

The document summarizes a lab exercise on basic elements of a computer program. It includes 11 questions about identifiers, variables, constants, data types, arithmetic operators, and converting mathematical formulas to C++ expressions. The questions cover naming rules, valid/invalid identifiers, differences between identifiers and keywords, reserved words in C++, differences between variables and constants, appropriate data types for given values, creating variables with names and data types, and representing formulas in code.

Uploaded by

pandamalaya10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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:

example student name Not valid


a. r2d2 ……………….
valid
b. H2O valid
……………….
c. secondCousinOnceRemoved valid
……………….
d. 2ndBirthday ……………….
not valid
e. the_Legend_City_of_Malaysia ……………….
valid
f. _TIME_ valid
……………….
g. _12345 valid
……………….
h. x(3) not valid
……………….
i. cost_in_$ not valid
……………….
Question 3
State the difference between identifier and keyword.

Answer

identifier are names of thing that appear in a program


meanwhile keyword are word symbol and cannot be refined
within any program

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

variable - the value can be change

constant - the value cannot be change

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:

example 884 integer


a. 23 integer
……………….
b. ‘c’ char
……………….
c. 8.52 float
……………….
d. 9537 integer
……………….
e. 20125.12345 ……………….
double
f. True boolean
……………….

Question 8
Create a variable name for each of the following and give the appropriate data type:

Variable name: Data type:

example Discount rate rate float


a. Speed of an automobile ………………
speed ………………
float
b. Shipping rate per ringgit shipping_rate ………………
……………… float
c. Highest score in exam ……………… float
highest_score ………………
d. Initial ‘m’ for male or ‘f’ for female gender
……………… ………………
char
e. Amount of students in a class ………………
amount_student integer
………………
f. Pass or fail result
……………… boolean
………………

Question 9
How do the following two statements differs:

1)char ch = ‘A’;
2)char ch = 65;

Answer -Statment 1 is assign to ch the value 'A'


-Statement 2 is assign to ascii code the value 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)
……………….

You might also like