Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

BEJ10102 Computer Programming

Semester 1, Session 20202021

ANSWER SHEET

Lab L02: Introduction to C++ Programming

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 statement to display your first and last name

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<<"Course Code : BEJ10102 "<<endl;


//cout statement to display Section

cout<<"Section : 2 "<<endl;

Practice 4
(1,2,3)

Page 5
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming

(4,)

Enter the distance in meter : 255


255 m is equal to 836 ft
255 m is equal to 0 km

(5)

Because data type (int) does not read numbers with decimals and fraction.

(6)

Enter the distance in meter : 255


255 m is equal to 836.400 ft

Page 6
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming

256 m is equal to 0.255 km

(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)

Suitable preprocessor directive :

 #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

2. Convert mass from gram to kilogram.


3. Calculate the Kinetic Energy = ½ mv^2
4. Display result in joules.
(2)
Analysis
Input : Object mass (g), Object velocity (m/s)
Output : Kinetic energy (joules)
Process : Kinetic energy = ½ mv^2
Constraints : 1 gram = 0.001 kg

(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

#include <iomanip> //required stream manipulators


#include <cmath>
using namespace std; // accessing C++ Standard Library
const float gtokg = 0.001;

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

mass = gram * gtokg; //convert meter to feet*/


ke = 0.5 *mass *velocity *velocity; //calculate kinetic energy
cout<<"Kinetic Energy : "<<ke<<endl; //cout statement to display the kinetic energy//
return 0;

(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

You might also like