UNIVERSITI KUALA LUMPUR MALAYSIA FRANCE INSTITUTE
Lab 02 - Introduction to C++ Programming (PRG3012/PRG0012) Example Write a pseudocode to find the area of a circle using the following formula:
Area = pi x radius2
•
Prompts the user to enter a value for radius and reads it. Display the result on the screen. function main() begin var area, var radius const PI ← 3.14 print “Input Radius: “ read radius area ← PI * radius * radius print “Area :” area end
C++ program #include #include using namespace std; int main(int argc, char *argv[]) { float area, radius; const float PI = 3.14; cout<<"Input radius: "; cin>>radius; area=PI*radius*radius; cout<<"Area :"<<area; cout<<endl; system("PAUSE"); return EXIT_SUCCESS; }
//var area, var radius //const PI <- 3.14 //print "Input Radius: " //read radius //area <- PI * radius * radius //print "Area :" area
UNIVERSITI KUALA LUMPUR MALAYSIA FRANCE INSTITUTE
EXERCISE 1 Follwing is the pseudocode to find the area of a square using the following formula: Area = Height x Width Prompts the user to enter a value for height and width, and reads it. Display the result on the screen.
func main() begin var height, var width, var area print “Input Height: “ read height print “Input Width: “ read width area ← height * width print “Area :” area end Convert the above pseudocode into C++ program. EXERCISE 2 Write a pseudocode/flowchart and C++ program that computes the city income tax for the city of Kuala Lumpur, given a taxpayer’s gross annual income. Whereby, the city income tax is 1.75% of the gross annual income. Display the result on the screen. EXERCISE 3 Write a pseudocode/flowchart and C++ program to solve the following problems: Problem 1 1. Prompts the user to enter a value for his or her heights in inches and reads it. 2. Converts the height from inches to centimeters and prints the output. Height(cm) = 2.54 x Height(inches) Problem 2 1. Prompts the user to enter a value for his or her weight in pounds and reads it 2. Converts the weight from pounds to kilograms and prints the outputs Weight(kg) = 0.45359 x Weight(pounds) EXERCISE 4 Write a pseudocode/flowchart and C++ program that does the followings: 1. Prompts the user to enter his/her year of birth and reads it. 2. Add 100 to the year of birth 3. Display the value
UNIVERSITI KUALA LUMPUR MALAYSIA FRANCE INSTITUTE
EXERCISE 5
function main() begin var a a ← 10 a ← a * 10 print a end
function main() begin var a, var b a ← 10 b←5 b ←a*b print b end
(a) Study the above pseducode. What is the value printed? (a) _______________________
(b) _______________________ To confirm your answer write a C++ program. EXERCISE 6
Convert the above flow chart into C++ program.
(b)
UNIVERSITI KUALA LUMPUR MALAYSIA FRANCE INSTITUTE
EXERCISE 7
Convert the above flow chart into C++ program. EXERCISE 8 Convert the following C++ program into flowchart. #include #include #include #include using namespace std;
// Access cout // Access power function // Access manipulators
const float LOAN_AMOUNT = 50000.00; // Amount of loan const float YEARLY_INTEREST = 0.0524; // Yearly interest const int NUMBER_OF_YEARS = 7; // Number of years int argc, char *argv[]) { // Local variables float monthlyInterest; // Monthly interest rate int numberOfPayments; // Total number of payments float payment; // Monthly payment // Calculate values monthlyInterest = YEARLY_INTEREST / 12; numberOfPayments = NUMBER_OF_YEARS * 12; payment =(LOAN_AMOUNT * pow(monthlyInterest + 1, numberOfPayments) * monthlyInterest)/(pow(monthlyInterest + 1, numberOfPayments) - 1); // Output results cout << fixed << setprecision(2) << "For a loan amount of " << LOAN_AMOUNT << " with an interest rate of " << YEARLY_INTEREST << " and a " << NUMBER_OF_YEARS << " year mortgage, " << endl; cout << " your monthly payments are $" << payment << "." << endl; system("PAUSE"); return EXIT_SUCCESS; }