0% found this document useful (0 votes)
36 views

Practical 01 - Simple Program

The document provides sample code and questions to help students learn about basic C++ programming concepts like functions, variables, input/output objects, and arithmetic operations. It covers matching code snippets to English descriptions, modifying sample programs, writing simple programs to calculate values like age, sums, and geometric shapes, and self-review questions. Students are asked to write, run, and test programs involving user input, calculations, and output displays.

Uploaded by

Jasper Lim
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Practical 01 - Simple Program

The document provides sample code and questions to help students learn about basic C++ programming concepts like functions, variables, input/output objects, and arithmetic operations. It covers matching code snippets to English descriptions, modifying sample programs, writing simple programs to calculate values like age, sums, and geometric shapes, and self-review questions. Students are asked to write, run, and test programs involving user input, calculations, and output displays.

Uploaded by

Jasper Lim
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical 1 – Refer to Topics 01 and 02

Part A (Understanding Concepts)


1. Match the English statements to the statements/words/symbols in the program below.
(a) Indicates the start of the body of function main.
(b) Makes the program display something on the computer screen.
(c) Tells the preprocessor where to get additional information about the object cout.
1 #include <iostream>
2 using namespace std;
3
4 int main(void)
5 {
6 cout << "Hello! How are you?\n";
7 return 0;
8 }

2. Match the English statements to the statements/words/symbols in the program below.


(a) Tells the compiler the names given to memory cells used by the program and indicates that the values to be
stored in those memory cells are type integer.
(b) A symbol that indicates the operation of storing a value in a variable or memory cell.
1 #include <iostream>
2 using namespace std;
3
4 int main(void)
5 {
6 int current_year, birth_year;
7 current_year = 2019;
8 birth_year = 2000;
9
10 int age;
11 age = current_year - birth_year;
12
13 cout << "Your age is " << age << endl;
14 return 0;
15 }

3. For the program in question 2 above, what is the purpose of the ‘endl’?

4. Consider the program below.


(a) What is the purpose of the object cin?
(b) Modify the program to display a prompt asking the user to enter the year of birth.
1 #include <iostream>
2 using namespace std;
3
4 int main(void)
5 {
6 int current_year, birth_year;
7 current_year = 2019;
8 cin >> birth_year;
9
10 int age;
11 age = current_year – birth_year;
12
13 cout << "Your age is " << age << endl;
14 return 0;
15 }

1
5. For the program in question 4 above, can we change the order of the statements as follows and get the same
results?
(a) age = current_year - birth_year; (b) cin >> birth_year;
current_year = 2019; current_year = 2019;
cin >> birth_year; age = current_year - birth_year;

Part B (Programming Exercises)

1. Refer to steps 1 to 9 in Practical 00.docx, run the following program and observe the output.
#include <iostream>
using namespace std;

int main(void)
{
cout << "Hello!\nHow are you?\n";
return 0;
}

2. Refer to steps 10 and 11 in Practical 00.docx, run the following program and observe the output.
#include <iostream>
using namespace std;

int main(void)
{
int current_year, birth_year;
current_year = 2019;
birth_year = 2000;

int age;
age = current_year - birth_year;

cout << "Your age is " << age << endl;


return 0;
}

3. Modify the program in question 2 to ask the user to enter the current year and birth year.

4. Write a program that asks the user to enter three numbers, computes the sum of the three numbers, and displays
the result. The output format should be as follows:
Enter first number: 3
Enter second number: 12
Enter third number: 44
The sum of
3, 12, and 44
is 59.

Note: The numbers underlined are sample input.

5. Correct the errors in the following program and run it.

include iostream ;
int Main() ;
{
cout >> (The program finally runs!\n)

return
}

2
Part C (Self-Review / Revision)

1. What are the different computer programs in IDE used to develop and run a program?
2. What is the name of the function where program execution starts?
3. What is the name of the object to call when your program wants to display results?
4. What is the name of the object to call when your program wants to get input data from the program user?
5. What is the purpose of the line #include <iostream> in a program?
6. What is a variable?
7. What are two ways to store a value in a variable?

Part D (Practice Exercises)

1. Write a program that inputs a number (n) and then computes and displays the square (n * n) of the number.

2. Write a program that asks a user to enter two numbers (a and b) and then computes and displays the product
(a * b) of the two numbers.

3. Write a program that calculates the area and perimeter of a rectangle from a user-supplied length and width.
Note: area is l * w and perimeter is 2 * (l + w) where l is the length and w is the width of the rectangle.

You might also like