Download presentation
Presentation is loading. Please wait.
Published byKerry McCormick Modified over 9 years ago
1
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal 14300 Pulau Pinang Week 3
2
2 Overview We have learned the basics of programming. Understand C++ library, data type, basic memory allocation, etc. Use of Main program Now need to understand control structures for decision making procedures in the program
3
3 Case Study 1 How much money you need to survive in Desasiswa ? Where to start? Use of Flow Chart
4
4 Decision Making Within a Program Consider a student’s budget control living in USM If(income>= expenses) { cout << “OK” << endl; cout << “OK” << endl; } else else { cout << “Please reduce your expenses!” << endl; cout << “Please reduce your expenses!” << endl; }
5
5 Math Example int main() { double r; { double r; ….. ….. if(r>= 0) if(r>= 0) { double root = sqrt(r); double root = sqrt(r); } …… …… return 0; return 0; }
6
6 To use ‘=’ or ‘==’ Recall that ‘x=y’ means you are assigning the value y into x If(income == expenses) { cout << “OK” << endl; cout << “OK” << endl; } else else { cout << “Please reduce your expenses!” << endl; cout << “Please reduce your expenses!” << endl; }
7
7 Multiple Choices Consider a program that asks a user to specify a coin and its quantities: 1 sen, 5 sen, 10 sen, 20 sen, 50 sen The computer program is supposed to determine the total RM value of the coins
8
8 Coins Program #include #include using namespace std; int main() { int N; double value=0; int N; double value=0; string coin_type; string coin_type; cout << "enter coin type: " << endl; cout << "enter coin type: " << endl; getline(cin,coin_type); getline(cin,coin_type); cout << "Enter the number of coins: " << endl; cout << "Enter the number of coins: " << endl; cin >> N; cin >> N; if(coin_type == "1 sen") if(coin_type == "1 sen") value = N*0.01; value = N*0.01; else if (coin_type == "5 sen") else if (coin_type == "5 sen") value = N*0.05; value = N*0.05; else if (coin_type == "10 sen") else if (coin_type == "10 sen") value = N*0.10; value = N*0.10; else if (coin_type == "20 sen") else if (coin_type == "20 sen") value = N*0.20; value = N*0.20; else if (coin_type == "50 sen") else if (coin_type == "50 sen") value = N*0.5; value = N*0.5; else else cout << coin_type << " is not a valid coin" << endl; cout << coin_type << " is not a valid coin" << endl; cout << "Coins value is RM " << value << endl; cout << "Coins value is RM " << value << endl; getch(); return 0; } // end of program body
9
9 Nested Branches Deployed as a way to perform more complex decision making procedure Example USM students performance Need to select if student is from (1) Matriculation OR (2) STPM (2) STPM Define performance criteria as A=4.0, B=3.0, C=2.0, else fail
10
10 Exercises Write a program i) to input the origin of the students (STPM/Matriculation) i) to input the origin of the students (STPM/Matriculation) ii) and to determine the GPA of the students based on their grades. ii) and to determine the GPA of the students based on their grades. Write a program to i) Enter your total monthly income i) Enter your total monthly income ii) Enter your type of expenses and cost for a month ii) Enter your type of expenses and cost for a month iii) Determine whether you can survive in Desasiswa iii) Determine whether you can survive in Desasiswa
11
11 Homework 1 Refer to assignment section in website
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.