Problem Solving and Program Design in C 5

Problem Solving and Program Design in C (5 th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 2 Slides By Dr. Daniyal Alghazzawi

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

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.

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

The Software Development Method Case 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.

The Software Development Method Case 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

The Software Development Method Case 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.

The Software Development Method Case Study: Converting Miles to Kilo 1. 2. 3. 4. 5. 6. Problem Analysis Design / Algorithm Implementation Testing Maintenance 4. Implementation: ( )ﺗﻨﻔﻴﺬ

The Software Development Method Case 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.

The Software Development Method Case 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.

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 2. Y = X / 1. 609 3. Display the value Y (X: the distance in miles) (Y: the distance in kilo)

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 Total. Miles 2. Total. Kilo = Total. Miles / 1. 609 3. Display the value Total. Kilo

Variables Syntax ( )ﺍﻟﻘﺎﻋﺪﺓ in C variable = expression ; True or False: 1. 2. 3. 4. 5. 6. 7. 8. X = 10; Y = 20 Z = 15. 5; Y = X + Z; Y = Y + 1; Z = Y – 1. 5 + 5; 23 = Z; Z = Z / 2 + 5;

Variables Syntax ( )ﺍﻟﻘﺎﻋﺪﺓ in C variable = expression ; True or False: 1. 2. 3. 4. 5. 6. 7. 8. X = 10; Y = 20 Z = 15. 5; Y = X + Z; Y = Y + 1; Z = Y – 1. 5 + 5; 23 = Z; Z = Z / 2 + 5; False; Why?

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? 1. 2. 3. 4. 5. 6. 7. X = 10; Z = 15. 5; Y = X + Z; Y = Y + 1; Z = Y – 1. 5 + 5; Z = Z / 2 + 5; X = Z % 3; # X 1. 10 Y 2. Z 15. 5 3. 25. 5 4. 26. 5 5. 30 6. 20 7. 2 What are the values of (X, Y, Z) in the memory after executing the program?

Data Types ( )ﻧﻮﻋﻴﺔ ﺍﻟﺒﻴﺎﻧﺎﺕ Data Types In C Numbers only Integer age = 36 Character gender = ‘M’ Double degree = 36. 4 Note: there are other data types.

Data Types (Syntax in C) int variable_list ; double variable_list ; char variable_list ; Example of each data type with an assigned value. 1. 2. 3. int id; double dollar; char gender; 4. 5. 6. 7. id = 0750428; dollar = 3. 75; gender = ‘M’;

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 printf(“Hello…”); Display “Hello” in 1 st line and “good” in 2 nd line? use the Newline Escape Sequence n printf(“Hellongood”);

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

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

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 %d scanf(“%d”, &X); If the variable is double, use the Placeholder %lf scanf(“%lf”, &X); If the variable is character, use the Placeholder %c scanf(“%c”, &X); Get 3 characters from the user for the variable name? scanf(“%c%c%c”, &name);

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

Input/Output Function Ask the user to enter the distance in miles and display the distance in kilometers? 1. 2. 3. 4. 5. 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. 9 That equal 165. 5661 kilometers. How to change the display from 165. 5661 to 165. 57 ?

Changing the Display - Integer Example: 1. 2. 3. 4. 5. int X; X = 102; printf (“X 1 = %dn”, X); printf (“X 2 = %4 dn”, X); printf (“X 3 = %5 dn”, X); Run: X 1 = 102 X 2 = 102 X 3 = 102 Value Format Displayed Output 234 %4 d ▒ 234 %5 d ▒▒ 234 %6 d ▒▒▒ 234 %1 d 234 -234 %4 d -234 %5 d ▒-234 %6 d ▒▒-234 %2 d -234

Changing the Display - Double Example: 1. 2. 3. 4. 5. 6. 7. double X, Y, Z; X = 10. 23 Y = 102. 235; Z = 20. 2 printf (“>%6. 2 fn”, X); printf (“>%. 2 fn”, Y); printf (“>%6. 2 fn”, Z); Run: > 10. 23 >102. 24 > 20. 20 Value Format Displayed Output 3. 14159 %5. 2 f ▒ 3. 14159 %3. 2 f 3. 14159 %5. 3 f 3. 142 3. 14159 %4. 2 f 3. 14159 %5. 1 f ▒▒ 3. 14159 %8. 5 f ▒ 3. 14159 0. 1234 %4. 2 f 0. 12 -0. 006 %8. 3 f ▒▒-0. 006 %. 3 f -0. 006 %4. 2 f -0. 01 -0. 006 %8. 5 f -0. 00600 -3. 14159 %. 4 f -3. 1416 Hint: write the displayed output first, and then write the format

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: 1. /* DECLARATIONS */ 2. double miles, kms; 3. /* EXECUTABLE STATMENTS */ 4. printf(“Enter the distance in miles: “); /* ask the user */ 5. scanf(“%lf”, &miles); /* get the miles */ 6. kms = miles * 1. 609; /* miles to kms */ 7. printf(“That equal %f kilometers. n”, kms); Note: you need to learn writing Mathematical Formulas in C

Be Ready to Write a Program in C 1. 2. 3. 4. 5. 6. 7. 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 */ /* Write the executed statements */ return (0); }

Be Ready to Write a Program in C

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

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)

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

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

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
- Slides: 33