Professional Documents
Culture Documents
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
68
Lecture Notes CSC128
50
b. + ( x+ a )n
2y
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
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
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 :
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 :
Coding :
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
74
Lecture Notes CSC128
Coding :
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
75