1 / 33

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. Slides By Dr. Daniyal Alghazzawi. CP 202 Chapter 2. CHAPTER 2 - Outline. Software Development Method Variables vs. Constants Memory Syntax (Grammar) Data Types Input / Output Functions

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 Slides By Dr. DaniyalAlghazzawi CP 202 Chapter 2

  2. CHAPTER 2 - Outline • Software Development Method • Variables vs. Constants • Memory • Syntax (Grammar) • Data Types • Input / Output Functions • Changing the Display Output • Using Comments • Writing a Program in C

  3. Be A Problem Solver • 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.

  4. The Software Development Method 1. 2. 3. 4. 5. 6. Problem Analysis Design / Algorithm Implementation Testing Maintenance

  5. The Software Development MethodCase Study: Converting Miles to Kilo 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.

  6. The Software Development MethodCase Study: Converting Miles to Kilo 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

  7. The Software Development MethodCase Study: Converting Miles to Kilo 1. 2. 3. 4. 5. 6. Problem Analysis Design / Algorithm Implementation Testing Maintenance 3. Design / Algorithm: 1. Get the distance in miles. 2. Convert the distance to kilometers. (1 kilometer = 1.609 miles) 3. Display the distance in kilometers.

  8. The Software Development MethodCase Study: Converting Miles to Kilo 1. 2. 3. 4. 5. 6. Problem Analysis Design / Algorithm Implementation Testing Maintenance 4. Implementation: (تنفيذ)

  9. The Software Development MethodCase Study: Converting Miles to Kilo 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.

  10. The Software Development MethodCase Study: Converting Miles to Kilo 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.

  11. Variables (متغير) • Use variables to store the values in the algorithm. • Variables vs. Constants (ثابت). • 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

  12. Variables • Choose good names for the variables: • No need to write any comment • Easy to track • Case sensitive • Don’t use a Reserved Word • The algorithm with good variables’ names:1. Get the value TotalMiles2. TotalKilo = TotalMiles / 1.6093. Display the value TotalKilo

  13. Variables Syntax (القاعدة) in C • True or False: • X = 10; • Y = 20 • Z = 15.5; • Y = X + Z; • Y = Y + 1; • Z = Y – 1.5 + 5; • 23 = Z; • Z = Z / 2 + 5; variable = expression ;

  14. Variables Syntax (القاعدة) in C • True or False: • X = 10; • Y = 20 False; Why? • Z = 15.5; • Y = X + Z; • Y = Y + 1; • Z = Y – 1.5 + 5; • 23 = Z; False; Why? • Z = Z / 2 + 5; variable = expression ;

  15. Variables and Memory (الذاكرة) • The memory stores the last value of each variable in the program. • The following program consists of 3 variables. Show the value of each variable in the memory 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; • What are the values of (X, Y, Z) in the memory after executing the program?

  16. Data Types (نوعية البيانات) • Note: there are other data types.

  17. Data Types (Syntax in C) • Example of each data type with an assigned value. • int id; • double dollar; • char gender; • id = 0750428; • dollar = 3.75; • gender = ‘M’; intvariable_list ; double variable_list ; char variable_list ;

  18. Output Operations and Functions • The output operations and functions will help you to display variables and values on the screen. • Display “Hello…” on the screen in C? • use the function printfprintf(“Hello…”); • Display “Hello” in 1st line and “good” in 2nd line? • use the Newline Escape Sequence \nprintf(“Hello\ngood”);

  19. Output Operations and Functions • Display the value of the variable X? • If the variable is integer, use the Placeholder %dprintf(“%d”, X); • If the variable is double, use the Placeholder %fprintf(“%f”, X); • If the variable is character, use the Placeholder %cprintf(“%c”, X); • Display “My age is “ then the variable AGE? • printf(“My age is %d”, AGE); • Write an example using the two variables: age,GPA? • printf(“I am %d years old, my GPA: %f\n”, age, GPA); • Note: no need to memorize some terms, such as “Placeholder”

  20. Output Functions Syntax • Examples: • printf(“Enter the object mass in grams?”); • printf(“I am %d years old, and my gpa is %f\n”, age, gpa); printf (format string); printf (format string, print list); • You can ask a user to enter a value, so what is the function to get this value?

  21. Input Operations and Functions • The input operations and functions will help you to get values for the variables from a user (مستخدم). • Get the value for the variable X? • If the variable is integer, use the Placeholder %dscanf(“%d”, &X); • If the variable is double, use the Placeholder %lfscanf(“%lf”, &X); • If the variable is character, use the Placeholder %cscanf(“%c”, &X); • Get 3 characters from the user for the variable name? • scanf(“%c%c%c”, &name);

  22. Input Function Syntax • Examples: • scanf(“%c%d”, &first_initial, &age); scanf (format string, input list);

  23. Input/Output Function • Ask the user to enter the distance in miles and display the distance in kilometers? • double miles, kms; • printf(“Enter the distance in miles: “); • scanf(“%lf”, &miles); • kms = miles * 1.609; • printf(“That equal %f kilometers.\n”, kms); • What will you see on the screen after run these steps?Enter the distance in miles: 102.9That equal 165.5661 kilometers. • How to change the display from 165.5661 to 165.57 ?

  24. Changing the Display - Integer • Example: • int X; • X = 102; • printf (“X1 = %d\n”, X); • printf (“X2 = %4d\n”, X); • printf (“X3 = %5d\n”, X); • Run: X1 = 102 X2 = 102 X3 = 102

  25. Changing the Display - Double • Example: • double X, Y, Z; • X = 10.23 • Y = 102.235; • Z = 20.2 • printf (“>%6.2f\n”, X); • printf (“>%.2f\n”, Y); • printf (“>%6.2f\n”, Z); • Run: > 10.23 >102.24 > 20.20 • Hint: write the displayed output first, and then write the format

  26. Using Comments • With your comments, it will easy to remember the job of each statement in your program. • Each comment starts with /* and end up with */ • For example: • /* DECLARATIONS */ • double miles, kms; • /* EXECUTABLE STATMENTS */ • printf(“Enter the distance in miles: “);/* ask the user */ • scanf(“%lf”, &miles); /* get the miles */ • kms = miles * 1.609; /* miles to kms */ • printf(“That equal %f kilometers.\n”, kms); • Note: you need to learn writing Mathematical Formulas in C

  27. Be Ready to Write a Program in C • 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 */ • /* Write the executed statements */ • return (0); • }

  28. Be Ready to Write a Program in C

  29. Program 1:Miles-to-Kilometers Conversion • What will be displayed on the screen after run this program?

  30. Program 2:Finding the Value of the Coins • 1 Dollar = 100 cents , 1 Quarter = 25 cents , 1 dime = 10 cents • 1 Nickels = 5 cents , x pennies = x cents • What will be displayed on the screen after run this program? (2 slides)

  31. Program 2:Finding the Value of the Coins (cont’d) • What is the problem that this program tries to solve?

  32. Common Programming Errors • Learn in the lab: • Debugging • Syntax Errors • Run-Time Errors • Undetected Errors

  33. CHAPTER 2 - Conclusion • Software Development Method • Variables vs. Constants • Memory • Syntax (Grammar) • Data Types • Input / Output Functions • Changing the Display Output • Using Comments • Writing a Program in C

More Related