You are on page 1of 18

Lecture Notes CSC128

Week 4

58
Lecture Notes CSC128

Notes
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------

59
Lecture Notes CSC128

Notes
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------

60
Lecture Notes CSC128

Notes
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------

61
Lecture Notes CSC128

Notes
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------

62
Lecture Notes CSC128

Notes
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------

63
Lecture Notes CSC128

Notes
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------

64
Lecture Notes CSC128

Notes
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------

65
Lecture Notes CSC128

Notes
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------

66
Lecture Notes CSC128

Notes
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------

67
Lecture Notes CSC128

Lab Exercise 4

1. Declare the following declaration statements :


a. a constant name NEWLINE with the value of ‘\n’.
b. a variable name company moto consists of string “Connecting People”.

2. The following declarations is given :


int num1 = 2020, num2 = 20, num4 = 2 ;
double num3 = 20.25 , result = 0.00 ;
a. cout << num1 / num2 + num2 % num4 << endl;
b. result = num3 * num2 – num1 % num2 ;
cout << result ;

68
Lecture Notes CSC128

3. Write C++ assignment statements for the following algebraic equations.


a. A=π r 2 h + 2 π r

50
b. + ( x+ a )n
2y

4. TRACE the following program segments :


a).
int main()
{
int a =100, b = 2, c = 50 , sum =0;
int x, y ;
cout << “Please enter value of x and y “ << endl;
cin>> x >> y ;
sum = sqrt(x) * y + a / c % b ;
cout <<” The sum is “ << sum << endl;

69
Lecture Notes CSC128

} //endMain
Assume you enter x = 16, y = 68.

a b c x y sum

b).
int main()
{ int a = 5000, b = 500, c = 50 , sum=0;
int r1 = a / b ;
int r2 = a % b ;
int r3 = b / c + pow(r1,2) ;
int r4 = b % c ;
sum = r1 + r2 + r3 + r4 ;
cout <<” The sum is “ << sum << endl;
} //endMain

a b c r1 r2

r3 r4 sum

c).
int main()
{ int n1 = 40, n2 = 8 ; float x = 5.5, y = 10.5;
double xNum = 0.75 ; yNum = 2.25, result= 0.00;
int R1 = n1 / x ;
float R2 = y / n2 ;
double R3 = x * xNum + yNum ;
cout <<” The result = “ << result << endl;
} //endMain

R1 R2 R3 result

70
Lecture Notes CSC128

5. What will be the output for the following program segments :

a)
#include<string> Output :
int main()
{ char name[30], addr1[40], addr2[40],pCode[5] ;
cout << “Please enter your name : “ <<endl ;
cin.getline(name,30) ;
cout << “ Address : “ << endl ;
cin.getline(addr1,40) ;
cin.getline(addr2,40) ;
cout << “Postcode : “ << endl;
cin>>pCode ;
// output
cout << “\n\n Name : “ << name << endl;
cout << “Address : “ << addr1 << endl;
cout << addr2 << “\t” << pCode << endl;
}

b)
#include<string> Output :
int main()
{ char kolejA[20] , kolejB[20] ;
// using strcpy to assign string value
strcpy(kolejA,”Redang”) ;
strcpy(kolejA,”Kapas”) ;
cout << kolejA << “ and ” << kolejB <<endl;
}

c)
#include <string>
using namespace std;
int main()
{int number;
string name;
cout << "Please enter a number." << endl;

71
Lecture Notes CSC128

cin >> number;


cout << "Enter your entire name (first and last)." <<
endl;
getline(cin, name);

cout << "Your full name is " << name


<< ", and the number you entered is " << number
<< endl;
return 0; }

c)
#include <string>
using namespace std;
int main()
{ string team = ” Liverpool FC ” ;
string EPL = ”English Premier League” ;
cout << " My fav team in the " << EPL
<< " is " << team << endl;
return 0; }

d)
#include <string>
using namespace std;
int main()
{ string firstname, lastname, fullname;
cout << "First name: ";
getline (cin, firstname);
cout << "Last name: ";
getline (cin, lastname);
fullname = lastname + " , " + firstname;
cout << "Fullname : " << fullname << endl;
return 0;}

72
Lecture Notes CSC128

6. Write a program that calculates the energy needed to heat water from an initial
temperature to final temperature. Your program should prompt the user to enter
amount of water in kilograms, the initial and final temperatures of the water. The
formula to compute the energy is
Q = M x ( final temp – initial temp) x 4184
Where M is the weight of water in kilograms, temperatures are in degrees Celcius
and energy Q is measured in joules. Here is a sample run :

Enter the amount of water in kilograms : 55.5


Enter the initial temperature : 3.5
Enter the final temperature : 10.5
The energy needed is 1625484.0

Coding :
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------

73
Lecture Notes CSC128

7. Given an airplane’s acceleration a and take-off speed v , you can compute the
minimum runway length needed for an airplane to take off using the following
formula:
Length = v2 / 2a

Write a program that prompts the user to enter v in meters/second (m/s) and the
acceleration a in a meters/second squared (m/s2), and displays the mimimum
runway length. Here is a sample run :

Enter speed and acceleration : 60 3.5


The minimum runway length for this airplane is 514.286

Coding :
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------

74
Lecture Notes CSC128

8. Body Mass Index (BMI) is a measure of heatlh on weight. It can be calculated by


taking your weight in kilograms and dividing by the square of your height in meters.
Write a program that prompts the user to enter a weight in pounds and height in
inches and displays the BMI. Note that one pound is 0.45359 kilograms and one
inch is 0.0254 meters. Here is a sample run :

Enter weight in pounds : 95.5


Enter height in inches : 50
BMI is 26.8573

Coding :
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------

75

You might also like