1 / 57

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman. CPCS 202 Chapter 2 – Input/Output. 12-10-1429. CHAPTER 2 – Input / Output. #. Hello World Get User ID Convert Miles to Kilometers Finding the Value of the Coins.

darice
Download Presentation

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

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. Problem Solving and Program Design in C (5th Edition)by Jeri R. Hanly and Elliot B. Koffman CPCS 202 Chapter 2 – Input/Output 12-10-1429

  2. CHAPTER 2 – Input / Output # • Hello World • Get User ID • Convert Miles to Kilometers • Finding the Value of the Coins • The Software Development Method • Variables • Constants • Output Operations and Functions • Input Operations and Functions • Using Comments • Design / Algorithm Types • Writing Program Structure • Changing the Display • Types of Errors •  column shows the topics index.  column shows the programs index.

  3. The Software Development Method 1 • Introduction • You need to be a problem solver • A good problem solver  a good programmer • Programmers use the Software Development Method • This is what you will learn in this course. • Prototype 1. 2. 3. 4. 5. 6. Problem Analysis Design / Algorithm Implementation Testing Maintenance

  4. The Software Development Method 1 • Example 1. 2. 3. 4. 5. 6. Problem Analysis Design / Algorithm Implementation Testing Maintenance 1. Problem: Your summer surveying job requires you to study some maps that give distances in kilometers and some that use miles. You and your coworkers prefer to deal in metric measurements. Write a program that performs the necessary conversion.

  5. The Software Development Method 1 • Example 1. 2. 3. 4. 5. 6. Problem Analysis Design / Algorithm Implementation Testing Maintenance 2. Analysis (تحليل): 1. Problem Input: miles 2. Problem Output: kilometers 3. Relevant Formula: 1 mile = 1.609 kilometers

  6. The Software Development Method 1 • Example 1. 2. 3. 4. 5. 6. Problem Analysis Design / Algorithm Implementation Testing Maintenance 3. Design / Algorithm: 1. Get the distance in miles from the user. 2. Convert the distance to kilometers. (1 kilometer = 1.609 miles) 3. Display the distance in kilometers on the screen.

  7. The Software Development Method 1 • Example 1. 2. 3. 4. 5. 6. Problem Analysis Design / Algorithm Implementation Testing Maintenance 4. Implementation: (تنفيذ) Write the code.

  8. The Software Development Method 1 • Example 1. 2. 3. 4. 5. 6. Problem Analysis Design / Algorithm Implementation Testing Maintenance 5. Testing: 1. Verify that the program works properly. 2. Try few test cases. ( if the input is …, the output has to be … )

  9. The Software Development Method 1 • Example 1. 2. 3. 4. 5. 6. Problem Analysis Design / Algorithm Implementation Testing Maintenance 6. Maintenance (صيانة): 1. Remove undetected errors. 2. Keep it up-to-date.

  10. Variables (متغير) 2 Introduction • Store values in the memory under given names; these values can be changed • The types of the possible values called Data Types • Choose good names for the variables: • No need to write any comment • Easy to track • Case sensitive • Don’t use a Reserved Word • Three important stages in variables: • Declaring a variable • Initializing a variable • Assigning a new value • The three data types showing are not all of the data types in C

  11. Variables – Declaring 2 • Introduction • Each variable needs to be declared before using it • Declaring a variable reserves space in the memory • Declaring variables has to be at the beginning of functions (before writing any statement) • Syntax • Example • int id; • double dollar; • char gender; • double tall, width, weight; typevariable_list;

  12. Variables – Initializing 2 • Introduction • The first value for a variable called initialing a variable • You can not use a variable without initializing • You can initial a variable at any place inside the function • Syntax • Example • id = 0750428; • dollar = 3.75; • gender = ‘M’; variable = expression ; • Expression could be a number or an equation

  13. Variables – Declaring & Initializing 2 • Introduction • You can save the space and the time by initializing a variable in the same time with declaring it • Again, this has to be at the beginning of functions before writing any statement • Syntax • Example • int id = 0750428; • double dollar = 3.75; • char gender = ‘M’; typevariable = expression ; • Expression could be a number or an equation

  14. Variables – Assigning 2 • Introduction • Changing the value of a variable called assigning a new value to the variable • Syntax • Example • id = 0750428; • dollar = 3.75; • gender = ‘M’; variable = expression ; • Expression could be a number or an equation

  15. VariablesDeclaring, Assigning, and Initializing 2 Conclusion 1 • int id, age; • double dollar; • char gender = ‘d’; • id = 0750428; • dollar = 3.75; • age = 21; • dollar = 3.77; Declaring Declaring & Assigning Initializing Assigning

  16. VariablesTracking Variables in the Memory 2 Conclusion 2 • You can track the values of each variable in a table • The following program consists of 3 variables. The table tracks the values of each variable after executing each statement: • X = 10; • Z = 15.5; • Y = X + Z; • Y = Y + 1; • Z = Y – 1.5 + 5; • Z = Z / 2 + 5; • X = Z % 3; • The memory stores the last value of each variable

  17. Constants (ثابت) 3 • Introduction • Store a value under a name • The value of the constant can’t be changed = You can’t assign a new value • Constants require one stage only, which is defining; it has to be at the begging of the program before writing ant function • Work with numbers only • Syntax • Examples • #define id 0750428 • #define dollar 3.75 #define variablevalue

  18. Output Operations and Functions 4 • Introduction • The output operations and functions will help you to display anything on the screen. • You can display some text only, a value of a variable only, or both. • In C language, you need to include the file stdioin order to use the function printf. • Syntax printf (format string); printf (format string, print list);

  19. Output Operations and Functions 4 • Examples • Display Hello… on the screen in C?use the function printfprintf(“Hello…”); • Display Hello… in 1st line and good in 2nd line?use the operation \nprintf(“Hello…\ngood”); • Display the value of the variable age; (if age is integer)?use the operation %dprintf(“%d”, age);

  20. Output Operations and Functions 4 • Examples • Display the value of the variable X; (if X is double\float)? use the operation %fprintf(“%f”, X); • Display the value of the variable Y; (if Y is character)? use the operation %cprintf(“%c”, Y); • Display My age is then the value of the variable AGE?printf(“My age is %d”, AGE); • Display the variables age and GPA in one statement?printf(“I am %d years old, GPA: %f”, age, GPA);

  21. Output Operations and Functions 4 • Conclusion • The output operations and functions will help you to display text and/or the values of a group of variables on the screen. For examples: • printf(“Enter the object mass in grams?”); • printf(“%c”, first_init); • printf(“I am %d years old.”, AGE);

  22. Input Operations and Functions 5 • Introduction • The input operations and functions will help you to get values for the variables from users. • You need to ask the user to input a value using the output function, then you can use the input function to get the value from the user. • In C language, you need to include the file stdioin order to use the function scanf. • Syntax scanf(format string, input list);

  23. Input Operations and Functions 5 • Examples • Get the value for the variable X; (if X is integer)?use the operation %dscanf(“%d”, &X); • Get the value for the variable X; (if X is double\float)? use the operation %lf (Long Float) and not%fscanf(“%lf”, &X); • Get the value for the variable X; (if X is character)?use the operation %dscanf(“%c”, &X);

  24. Input Operations and Functions 5 • Examples • Get 3 characters from the user?scanf(“%c%c%c”, &first, &second, &third); • Conclusion • The input operations and functions will help you to get a value for a declared variable from the user. For examples: • scanf(“%c%d”, &first_initial, &age);

  25. Using Comments 6 • With your comments, it will easy to remember the job of each statement in your program. • Comment Types in C language: • Single-line: Start with the symbol // and end up with the end of the line • Single-line or Multi-lines: Start with /* and end up with */ • Example: • /* Name: Daniyal • ID: 707997 */ • double miles, kms; • /* EXECUTABLE STATMENTS */ • printf(“Enter the distance in miles: “); // ask the user • scanf(“%lf”, &miles); // get the miles

  26. Design/Algorithm Types 7 • Algorithm without variables:1. Get the distance in miles.2. Convert the distance to kilometers. (1 kilometer = 1.609 miles)3. Display the distance in kilometers. • Algorithm with variables:1. Get the value X (X: the distance in miles)2. Y = X / 1.609 (Y: the distance in kilo)3. Display the value Y • Algorithm with good variables’ names:1. Get the value TotalMiles2. TotalKilo = TotalMiles / 1.6093. Display the value TotalKilo • Algorithms could write any way, but they have to be understandable

  27. Writing Program Structure 8 • Any group of statements needs to be inside a function(Note: you will learn later more about functions and write more than one function) • The main function will be executed first • /* include the header files here for any external function, such as input/output functions */ • /* define any Constant (not Variable) here */ • int main (void) • { • /* Declare the variables here */ • /* Start writing the statements */ • return (0); • } Be Ready to Write a Program in C • you have to declare the variables at the beginning of any function

  28. Writing Program Structure 8 • Example

  29. Hello World Implementation Testing Maintenance Problem Analysis Design Outline 1 • Problem • We need a program that displays on the screen the text Hello World!! • Analysis • Input • Output • the text “Hello World!!” • Formula • Design • Display “Hello World!!” on the screen

  30. Hello World Implementation Testing Maintenance Problem Analysis Design Outline 1 • Outline • Implementation 1. 2. 3. 4. 5. 6. 7. 8. 9. #include <stdio.h> intmain(void) { // 1. Display “Hello World!!” on the screen return(0); } #include <stdio.h> intmain(void) { // 1. Display “Hello World!!” on the screen printf(“Hello World!!\n”); return(0); } 1. 2. 3. 4. 5. 6. 7. 8. 9.

  31. Hello World Implementation Testing Maintenance Problem Analysis Design Outline 1 • Outline • Implementation 1. 2. 3. 4. 5. 6. 7. 8. 9. #include <stdio.h> intmain(void) { // 1. Display “Hello World!!” on the screen return(0); } #include <stdio.h> intmain(void) { // 1. Display “Hello World!!” on the screen printf(“Hello World!!\n”); return(0); } 1. 2. 3. 4. 5. 6. 7. 8. 9.

  32. Get User ID Implementation Testing Maintenance Problem Analysis Design Outline 2 • Problem • Write a program that gets the ID value from the user • Analysis • Input • ID • Output • Formula • Design • Get the ID from the user  user_ID Number of variables: 1 This indicates that you need to declare 1 variable in the program Put the value you get from the user in a variable called user_ID

  33. Get User ID Implementation Testing Maintenance Problem Analysis Design Outline 2 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. #include <stdio.h> intmain(void) { intuser_ID; // 1. Get the ID from the user return(0); }

  34. Get User ID Implementation Testing Maintenance Problem Analysis Design Outline 2 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. #include <stdio.h> int main(void) { intuser_ID; // 1. Get the ID from the user printf(“Please enter your user ID: “); scanf(“%d”, user_ID); return(0); } • DON’T try to get a value from a user (scanf) without asking (printf)

  35. Convert Miles to Kilometers Implementation Testing Maintenance Problem Analysis Design Outline 3 • Problem • Your summer surveying job requires you to study some maps that give distances in kilometers and some that use miles. You and your coworkers prefer to deal in metric measurements. Write a program that performs the necessary conversion. • Each miles equal to 1.609 kilometer. • Write the Analysis & Design for this problem?

  36. Convert Miles to Kilometers Implementation Testing Maintenance Problem Analysis Design Outline 3 • Analysis • Input • KMS_PER_MILE = 1.609 • Miles • Output • Kilometers • Formula • Kilometers = Miles x 1.609 • Design • Get the number of miles from the user  miles • Convert miles to kilometers:kms= miles x KMS_PER_MILE • Display the number of kilometers  kms • This input has a constant value • They are 2 variables in the Design, so 2 variables need to be declared

  37. Convert Miles to Kilometers Implementation Testing Maintenance Problem Analysis Design Outline 3 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. /* Converts distances from miles to kilometers. */ #include <stdio.h> /* printf, scanf definitions */ #define KMS_PER_MILE 1.609 /* conversion constant */ int main(void) { double miles, /* distance in miles */ kms; /* equivalent distance in kilometers */ /* 1. Get the number of miles from the user */ /* 2. Convert miles to kilometers */ /* 3. Display the number of kilometers */ return (0); }

  38. Convert Miles to Kilometers Implementation Testing Maintenance Problem Analysis Design Outline 3 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. /* Converts distances from miles to kilometers. */ #include <stdio.h> /* printf, scanf definitions */ #define KMS_PER_MILE 1.609 /* conversion constant */ int main(void) { double miles, /* distance in miles */ kms; /* equivalent distance in kilometers */ /* 1. Get the number of miles from the user */ scanf("%lf", &miles); printf("The distance in miles is %.2f.\n", miles); /* 2. Convert miles to kilometers */ kms = KMS_PER_MILE * miles; /* 3. Display the number of kilometers */ printf("That equals %.2f kilometers.\n", kms); return (0); } • This program has a mistake that may confuse any user, what is it?

  39. Convert Miles to Kilometers Implementation Testing Maintenance Problem Analysis Design Outline 3 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. /* Converts distances from miles to kilometers. */ #include <stdio.h> /* printf, scanf definitions */ #define KMS_PER_MILE 1.609 /* conversion constant */ int main(void) { double miles, /* distance in miles */ kms; /* equivalent distance in kilometers */ /* 1. Get the number of miles from the user */ scanf("%lf", &miles); printf("The distance in miles is %.2f.\n", miles); /* 2. Convert miles to kilometers */ kms = KMS_PER_MILE * miles; /* 3. Display the number of kilometers */ printf("That equals %.2f kilometers.\n", kms); return (0); } The programmer tries to get a value from the user without display any message on the screen to notify the user

  40. Convert Miles to Kilometers Implementation Testing Maintenance Problem Analysis Design Outline 3 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. /* Converts distances from miles to kilometers. */ #include <stdio.h> /* printf, scanf definitions */ #define KMS_PER_MILE 1.609 /* conversion constant */ int main(void) { double miles, /* distance in miles */ kms; /* equivalent distance in kilometers */ /* 1. Get the number of miles from the user */ printf("Please enter the value of distance in miles : "); scanf("%lf", &miles); printf("The distance in miles is %.2f.\n", miles); /* 2. Convert miles to kilometers */ kms = KMS_PER_MILE * miles; /* 3. Display the number of kilometers */ printf("That equals %.2f kilometers.\n", kms); return (0); }

  41. Convert Miles to Kilometers Implementation Testing Maintenance Problem Analysis Design Outline 3 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. /* Converts distances from miles to kilometers. */ #include <stdio.h> /* printf, scanf definitions */ #define KMS_PER_MILE 1.609 /* conversion constant */ int main(void) { double miles, /* distance in miles */ kms; /* equivalent distance in kilometers */ /* 1. Get the number of miles from the user */ printf("Please enter the value of distance in miles : "); scanf("%lf", &miles); printf("The distance in miles is %.2f.\n", miles); /* 2. Convert miles to kilometers */ kms = KMS_PER_MILE * miles; /* 3. Display the number of kilometers */ printf("That equals %.2f kilometers.\n", kms); return (0); }

  42. Finding the Value of the Coins Implementation Testing Maintenance Problem Analysis Design Outline 4 • Problem • We need a program that calculates the number of coins in a save box, and it displays the number of the dollars and the changes in cents. • USA Currency Coins: • 1 quarter = 25 cents • 1 dime = 10 cents • 1 nickel = 5 cents • 1 penny = 1 cent • For example: • 10 quarters + 8 dimes + 1 nickels + 10 pennies= (10 x 25) + (8 x 10) + (1 x 5) + (10 x 1)= 345 cents = 3 dollars and 45 cents • Quiz: Write the Analysis & Design for this problem?

  43. Finding the Value of the Coins Implementation Testing Maintenance Problem Analysis Design Outline 4 • Analysis • Input • The count of quarters • The count of dimes • The count of nickels • The count of pennies • Output • The value in dollars • The changes in cents • Formula • 1 quarter = 25 cents 1 dime = 10 cents • 1 nickel = 5 cents 1 penny = 1 cent

  44. Finding the Value of the Coins Implementation Testing Maintenance Problem Analysis Design Outline 4 • Design (1st version) • Get the count of each kind of coin • Find the value in dollars and change • Display the value in dollars and the change • Design (2nd version) • Get the count of each kind of coin • Compute the total value in cents • Find the value in dollars and change • Display the value in dollars and the change

  45. Finding the Value of the Coins Implementation Testing Maintenance Problem Analysis Design Outline 4 • Design (3rd version) • Get the count of the quarters  quarters • Get the count of the dimes  dimes • Get the count of the nickels  nickels • Get the count of the pennies  pennies • Compute the total value in cents:total_cents= quarters x 25 + dimes x 10 + nickels x 5 + pennies x 1 • Find the value in dollars and change:dollars = total_cents / 100change = total_cents % 100 • Display the value in dollars  dollars • Display the change  change • How many variables need to be declared? and what are their types?

  46. Finding the Value of the Coins Implementation Testing Maintenance Problem Analysis Design Outline 4 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. /* * Determines the value of a collecting of coins. */ #include <stdio.h> int main(void) { int pennies, nickels; /* input - count of each coin type */ int dimes, quarters; /* input - count of each coin type */ int change; /* output - change amount */ int dollars; /* output - dollar amount */ inttotal_cents; /* total cents */ /* 1. Get the count of the quarters */ /* 2. Get the count of the dimes */ /* 3. Get the count of the nickels */ /* 4. Get the count of the pennies */ /* 5. Compute the total value in cents. */ /* 6. Find the value in dollars and change. */ /* 7. Display the value in dollars. */ /* 8. Display the change. */ return(0); }

  47. Finding the Value of the Coins Implementation Testing Maintenance Problem Analysis Design Outline 4 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. /* * Determines the value of a collecting of coins. */ #include <stdio.h> int main(void) { int pennies, nickels; /* input- count of each coin type */ int dimes, quarters; /* input- count of each coin type */ int change; /* output- change amount */ int dollars; /* output- dollar amount */ inttotal_cents; /* total cents */ /* 1. Get the count of the quarters */ printf("Number of quarters> "); scanf("%d", &quarters); /* 2. Get the count of the dimes */ printf("Number of dimes> "); scanf("%d", &dimes); • 1 dollar = 100 cents • 1 quarter = 25 cents • 1 dime = 10 cents • 1 nickel = 5 cents • 1 penny = 1 cent

  48. Finding the Value of the Coins Implementation Testing Maintenance Problem Analysis Design Outline 4 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. /* 3. Get the count of the nickels */ printf("Number of nickels> "); scanf("%d", &nickels); /* 4. Get the count of the pennies */ printf("Number of pennies> "); scanf("%d", &pennies); /* 5. Compute the total value in cents. */ total_cents = 25 * quarters + 10 * dimes + 5 * nickels + pennies; /* 6. Find the value in dollars and change. */ dollars = total_cents / 100; change = total_cents % 100; /* 7. Display the value in dollars. 8. Display the change. */ printf("\nYour coins are worth %d dollars and %d cents.\n", dollars, change); return(0); } • 1 dollar = 100 cents • 1 quarter = 25 cents • 1 dime = 10 cents • 1 nickel = 5 cents • 1 penny = 1 cent

  49. Changing the Display - Integer 9 • You can organize the display for integer variables. • Formats: • Example: • printf(“ X Y\n”); • printf(“---------\n”); • printf(“%4d%4d\n”, 1, 2); • printf(“%4d%4d\n”, 13, 6); • printf(“%4d%4d\n”, 37, 513); • Run: X Y --------- 1 2 13 6 37 513

  50. Changing the Display - Double 9 • You can organize the display for double variables. • Formats: • Example: • double X, Y, Z; • X = 10.23; • Y = 102.235; • Z = 20.2; • printf(“>%6.2f\n”, X); • printf(“>%6.2f\n”, Y); • printf(“>%6.2f\n”, Z); • Run: • 10.23 • 102.24 • 20.20 • Hint: think about the displayed output first, then write the format

More Related