Professional Documents
Culture Documents
Lab L02: Introduction To C++ Programming: BEJ10102 Computer Programming Semester 1, Session 20202021
Lab L02: Introduction To C++ Programming: BEJ10102 Computer Programming Semester 1, Session 20202021
ANSWER SHEET
Lecturer’s Name:
Pn Nor’aisah Sudin
Prepared by:
HARIHARAN A/L DHARMALINGAM (CE200223)
Section:
7
Submission Date:
11/11/2020
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming
ANSWER for :
Practice 1
Practice 2
1(a)
1(b)
This is a compiler error in the line 13. The suggestion is to include semicolon ‘;’.
1(c)
Page 2
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming
1(d)
Its an invalid output, because the calculation formula of age is incorrect to the year 2039, it should be
age= age + 19.
2(a)
2(b)
Page 3
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming
The result is incorrect because it is a logic error. The steps are incorrect. The formula sum= a + b is a
process which it should be after the input and before the results. The correct process is shown below :
Practice 3
(1)
Page 4
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming
(2)
cout<<"Rohana Othman"<<endl;
//cout statement to display your program <BEV> or <BEJ> (on a new line)
cout<<"Program : BEJ"<<endl;
//cout statement to display Course Code and Section (on a new line)
cout<<"Section : 2 "<<endl;
Practice 4
(1,2,3)
Page 5
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming
(4,)
(5)
Because data type (int) does not read numbers with decimals and fraction.
(6)
Page 6
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming
(7)
This function ‘cout << setprecision(3) <<fixed<< showpoint;’ specifies the amount of decimal places
you want to display in a floating-point number. The syntax for the statement is setprecision(n). In this
setprecision(3) which made 836.400 feet and 0.255 km to 3 decimal places.
Practice 5
(1)
(2)
Line 15 and Line 16 can implement the math function
vol = pow(length,3); //calculate volume of a cube (modified code)
s_area = 6*pow(length,2); //calculate surface area of a cubecout <<setprecision(2)<<fixed<<
showpoint; (modified code)
#includecmath
Problem Solving
(1)
Requirement Specifications
1. Prompt a user to input object mass in grams and object velocity m/s.
Page 7
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming
(3)
Algorithm
Pseudocode
Begin
1. Prompt to enter object mass (g)
2. Get object mass (g)
3. Prompt to enter object velocity (m/s)
4. Convert the mass from gram to kilogram.
5. Calculate the value of Kinetic energy = ½ mv^2
6. Display the result in Joules.
End
(4)
//filename:problemsolving.cpp
//Programmer: Hari
//Date: 10/11/20
#include <iostream> //required for C++ Stream I/O
Page 8
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming
int main()
{
float mass, gram, kilogram, velocity, ke; //variables declaration of type floats
cout<<"Enter object mass in grams <g> : "; //Prompt user to enter input
cin>>gram; //input from user
cout<<"Enter object velocity : "; //Prompt user to enter input
cin>>velocity; //input from user
(5 a)
Page 9
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming
(5 b)
Mass = 20000 g
Velocity = 6 m/s
Ke = 360 Joules
(5 C)
Page 10
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming
This is a valid output because the result is accurate and correct to the calculation without experiencing
any errors in the program.
Page 11