Practical 01 Slide
Practical 01 Slide
Practical 01 Slide
FUNDAMENTALS OF
PROGRAMMING
UECS1004/UECS1104
PROGRAMMING AND PROBLEM
SOLVING
Practical 1 – Simple Program
1
Practical
■ Practical exercises are assigned for each practical session.
■ The questions of practical exercises will be uploaded to
WBLE. Please check in a timely manner.
■ Students are strongly encouraged to do the suggested
revision on lecture notes and attempt the questions in Part A
and B before each practical session.
2
Practical Plan
■ Week 3: Practical 1 – Simple Program
■ Week 4: Practical 2 – Primitive Data Types & Operations
■ Week 5: Practical 3 – Functions
■ Week 6: Practical 4 – More on Functions
■ Week 7: Practical 5 – Selection and Flow Charts
■ Week 8: Practical 6 – Multi-way Selection and Loops
■ Week 9: Practical 7 – More on Loops and Problem Solving
■ Week 10: Practical 8 – Arrays and Array Applications
■ Week 11: Practical 9 – Characters and Strings Manipulation
■ Week 12: Practical 10 – typedef and C Structure type
■ Week 13: Practical 11 – Files Handling
■ Week 14: Practical 12 – Revision Exercises (Self-Practice)
3
Practical Exercises
■ Each practical exercise contains 4 parts:
1. Part A (Understanding Concepts)
■ Tutor will go through and discuss the questions in Part A with students during each
practical session.
2. Part B (Programming Exercises)
■ Tutor will go through the questions in Part B with students after Part A. The answer
guideline will be uploaded to WBLE on every Monday morning.
3. Part C (Self-Review / Revision)
■ Students are required to do suggested revision on lecture notes for finding answers in
Part C. Students may send their answers to the lecturer or tutor for review.
4. Part D (Practice Exercises)
■ The questions in Part D are similar problems in Part B. Students may refer to the
answer guideline for Part B to code the solution. Students may send their answer to the
lecturer or tutor for review.
4
Practical 1 – Simple Program
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.
Opening curly brace ( { ) in line 5
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.
Line 6: int current_year, birth_year; and Line 10: int age;
b) A symbol that indicates the operation of storing a value in a variable or memory cell.
= (assignment operator) in line 7, 8 and 11.
3. For the program in question 2 above, what is the purpose of the ‘endl’?
The ‘endl’ is to make the cursor go to the next line after displaying the output.
6
Practical 1 – Part A (Understanding Concepts)
4. Consider the program below.
int main(void)
{
cout << "Hello!\nHow are you?\n";
return 0;
}
10
Practical 1 – Part B (Programming Exercises)
2. Run the following program and observe the output.
#include <iostream>
using namespace std;
int main(void)
{
int current_year, birth_year;
current_year = 2020;
birth_year = 2001;
int age;
age = current_year - birth_year;
11
Practical 1 – Part B (Programming Exercises)
3. Modify the program in question 2 to ask the user to enter the current year and birth year.
#include <iostream>
using namespace std;
int main(void)
{ Replace
int current_year, birth_year; each assignment statement
current_year = 2020;
birth_year = 2001; with
a pair “prompt and get”
int age; statement.
age = current_year - birth_year;
12
Practical 1 – Part B (Programming Exercises)
3. Modify the program in question 2 to ask the user to enter the current year and birth year.
#include <iostream>
using namespace std;
int main(void)
{
int current_year, birth_year;
cout << "Enter the current year: ";
cin >> current_year;
int age;
age = current_year - birth_year;
13
Practical 1 – Part B (Programming Exercises)
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:
14
Practical 1 – Part B (Programming Exercises)
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:
#include <iostream>
using namespace std;
int main(void)
{
int num1, num2, num3;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << "Enter third number: ";
cin >> num3;
cout << "The sum of\n" << num1 << ", " << num2 << ", and " << num3 << "\nis " << sum << ".\
n";
3 12 44 59
return 0; 15
Practical 1 – Part B (Programming Exercises)
5. Correct the errors in the following program and run it.
include iostream ;
int Main() ;
{
cout >> (The program finally runs!\n)
return
}
The errors are highlighted in red colour and ? denotes the missing symbols/texts.
?include ?iostream? ;
?
int Main() ;
{
cout >> (The program finally runs!\n)?
return ??
}
Please try to fix these errors, I will show you how to fix them 10 minutes later.
16
Practical 1 – Part B (Programming Exercises)
5. Corrected program:
#include <iostream>
using namespace std;
int main()
{
cout << "The program finally runs!\n";
return 0;
}
17