Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem Solving and Program Design Programming. COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 2 Problem Solving Process l Define.

Similar presentations


Presentation on theme: "Problem Solving and Program Design Programming. COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 2 Problem Solving Process l Define."— Presentation transcript:

1 Problem Solving and Program Design Programming

2 COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 2 Problem Solving Process l Define and analyze the problem. l Develop a solution. l Write down the solution steps in detail. l Test the solution and revise if necessary. l Document and maintain the solution.

3 COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 3 Programming as a Problem Solving Process l Define and analyze the problem. n What is the input & output? n What other information is necessary? l Develop an algorithm. n What steps must be done? l Implement a program. l Compile, test, and debug the program. l Document and maintain the program.

4 COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 4 Example l Problem Statement n Given a collection of nickels (US 5-cent coins) and pennies (US 1-cent coins), find the equivalent number of Hong Kong dollars and 10-cent coins. l Problem Analysis n Input: –nickels (integer) - number of US nickels –pennies (integer) - number of US pennies n Output: –dollars (integer) - number of HK dollar coins to return –houji (integer) - number of HK 10-cent coins to return

5 COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 5 Example: Initial Algorithm 1. Read in the numbers of nickels and pennies. 2. Compute the total value in US dollars. 3. Compute the corresponding total value in HK dollars. 4. Find the number of HK dollar coins and houji coins. 5. Display the number of HK dollar coins and houji coins.

6 COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 6 Example: Refined Algorithm 1. Read in the number of nickels and pennies. 2. Compute the total value in US dollars. 2.1 total_USD = (5 * nickel + penny)/100 3. Compute the corresponding total in HK dollars. 3.1. total_HKD = total_USD * US2HK 4. Find the number of HK dollar coins and 10-cent coins. 4.1. total_HK_cent = total_HKD * 100 4.2. dollar = total_HK_cent / 100 4.3. houji = (total_HK_cent % 100) / 10 5. Display the number of HK dollar and 10-cent coins.

7 // Determines the number of HK coins to exchange for US coins #include using namespace std; int main(){ const double US2HK = 7.8;// assume exchange rate is US$1 = HK$7.8 int nickel;// number of nickels int penny;// number of pennies int dollar;// number of HK dollar coins int houji;// number of HK 10-cent coins double total_USD;// total value in US$ int total_HK_cent;// total value in HK cents double total_HKD;// total value in HK$ // Read in the number of nickels and pennies cout << "Enter the number of nickels and press return: "; cin >> nickel; cout << "Enter the number of pennies and press return: "; cin >> penny;

8 // Compute the total value in US$ total_USD = (5 * nickel + penny) / 100.0; // Compute the total value in HK$ using the assumed exchange rate total_HKD = total_USD * US2HK; // Find the value in HK dollars and change total_HK_cent = total_HKD * 100; dollar = total_HK_cent / 100; houji = (total_HK_cent % 100)/10; // Display the number of HK dollar and 10-cent coins cout << "The exchange is HK " << dollar << " dollars and " << houji << " 10-cent coins." << endl; return 0; }

9 COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 9 Arithmetic Operators l Common Addition + Subtraction - Multiplication * Division / Mod % l Note n No exponentiation operator

10 COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 10 Integer Division l Integer division produces an integer result n Truncates the result l Examples 3 / 2 evaluates to 1 4 / 6 evaluates to 0 10 / 3 evaluates to 3

11 COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 11 Mod l Produces the remainder of the division l Examples 5 % 2 evaluates to 1 12 % 4 evaluates to 0 4 % 5 evaluates to 4

12 COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 12 Operators and Precedence l Which of the following is equivalent to m * x + b ? n (m * x) + b n m * (x + b) l Operator precedence tells how to evaluate expressions. l Standard precedence order ( ) Evaluated first. If nested, then evaluate the innermost first. * / % Evaluated second. If there are several, then evaluate from left-to-right. + - Evaluated third. If there are several, then evaluate from left-to-right.

13 COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 13 Operator Precedence & Assignment Statements Syntax of assignment statements: = ; Examples: int n, m, k;// declaration n = 5; m = 6 + (4 * 6); k = (n * 2) + m; k = k / 2; n = 2 * 4 / 5 + 3 * 5 % 4; cout << n << " " << m << " " << k << endl;


Download ppt "Problem Solving and Program Design Programming. COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 2 Problem Solving Process l Define."

Similar presentations


Ads by Google