1 / 13

Programming

Programming. Problem Solving and Program Design. Problem Solving Process. Define and analyze the problem. Develop a solution. Write down the solution steps in detail. Test the solution and revise if necessary. Document and maintain the solution.

ivie
Download Presentation

Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Problem Solving and Program Design

  2. Problem Solving Process • Define and analyze the problem. • Develop a solution. • Write down the solution steps in detail. • Test the solution and revise if necessary. • Document and maintain the solution.

  3. Programming as a Problem Solving Process • Define and analyze the problem. • What is the input & output? • What other information is necessary? • Develop an algorithm. • What steps must be done? • Implement a program. • Compile, test, and debug the program. • Document and maintain the program.

  4. Example • Problem Statement • 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. • Problem Analysis • Input: • nickels (integer) - number of US nickels • pennies (integer) - number of US pennies • Output: • dollars (integer) - number of HK dollar coins to return • houji (integer) - number of HK 10-cent coins to return

  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. 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 <iostream> 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. Arithmetic Operators • Common • Addition + • Subtraction - • Multiplication * • Division / • Mod % • Note • No exponentiation operator

  10. Integer Division • Integer division produces an integer result • Truncates the result • Examples • 3 / 2 evaluates to 1 • 4 / 6 evaluates to 0 • 10 / 3 evaluates to 3

  11. Mod • Produces the remainder of the division • Examples 5 % 2 evaluates to 1 12 % 4 evaluates to 0 4 % 5 evaluates to 4

  12. Operators and Precedence • Which of the following is equivalent to m * x + b ? • (m * x) + b • m * (x + b) • Operator precedence tells how to evaluate expressions. • 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. Operator Precedence & Assignment Statements Syntax of assignment statements: <identifier> = <expression>; 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;

More Related