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

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

description

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 - PowerPoint PPT Presentation

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

Page 1: 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

CP 202Chapter 2

Slides ByDr. Daniyal Alghazzawi

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

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

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

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.

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

The Software Development Method

ImplementationProblem AnalysisDesign /Algorithm

Testing Maintenance

1. 2. 3. 4. 5. 6.

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

The Software Development MethodCase Study: Converting Miles to Kilo

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.

ImplementationProblemProblem AnalysisDesign /Algorithm

Testing Maintenance

1. 2. 3. 4. 5. 6.

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

The Software Development MethodCase Study: Converting Miles to Kilo

2. Analysis (تحليل):1. Problem Input: miles2. Problem Output: kilometers3. Relevant Formula: 1 mile = 1.609 kilometers

ImplementationProblem AnalysisAnalysisDesign /Algorithm

Testing Maintenance

1. 2. 3. 4. 5. 6.

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

The Software Development MethodCase Study: Converting Miles to Kilo

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.

ImplementationProblem Analysis Design /AlgorithmDesign /Algorithm

Testing Maintenance

1. 2. 3. 4. 5. 6.

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

The Software Development MethodCase Study: Converting Miles to Kilo

4. Implementation:(تنفيذ)

ImplementationImplementationProblem AnalysisDesign /Algorithm

Testing Maintenance

1. 2. 3. 4. 5. 6.

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

The Software Development MethodCase Study: Converting Miles to Kilo

5. Testing:1. Verify that the program works properly.2. Try few test cases.

ImplementationProblem AnalysisDesign /Algorithm TestingTesting Maintenance

1. 2. 3. 4. 5. 6.

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

The Software Development MethodCase Study: Converting Miles to Kilo

6. Maintenance (صيانة):1. Remove undetected errors.2. Keep it up-to-date.

ImplementationProblem AnalysisDesign /Algorithm

Testing MaintenanceMaintenance

1. 2. 3. 4. 5. 6.

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

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

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

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

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

Variables Syntax (القاعدة) in C

True or False:1. X = 10;2. Y = 203. Z = 15.5;4. Y = X + Z;5. Y = Y + 1;6. Z = Y – 1.5 + 5;7. 23 = Z;8. Z = Z / 2 + 5;

variable = expression ;

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

Variables Syntax (القاعدة) in C

True or False:1. X = 10;2. Y = 20 False; Why?3. Z = 15.5;4. Y = X + Z;5. Y = Y + 1;6. Z = Y – 1.5 + 5;7. 23 = Z; False; Why?8. Z = Z / 2 + 5;

variable = expression ;

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

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. X = 10;2. Z = 15.5;3. Y = X + Z;4. Y = Y + 1;5. Z = Y – 1.5 + 5;6. Z = Z / 2 + 5;7. X = Z % 3;

What are the values of (X, Y, Z) in the memory after executing the program?

# X Y Z

1. 10

2. 15.5

3. 25.5

4. 26.5

5. 30

6. 20

7. 2

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

Data Types ( البيانات (نوعية

Note: there are other data types.

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

Data Types (Syntax in C)

Example of each data type with an assigned value.1. int id;2. double dollar;3. char gender;4. 5. id = 0750428;6. dollar = 3.75;7. gender = ‘M’;

int variable_list ;double variable_list ;

char variable_list ;

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

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

printf(“Hello…”); Display “Hello” in 1st line and “good” in 2nd

line? use the Newline Escape Sequence \n

printf(“Hello\ngood”);

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

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”

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

Output Functions Syntax

Examples:1. printf(“Enter the object mass in grams?”);2. 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?

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

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);

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

Input Function Syntax

Examples:1. scanf(“%c%d”, &first_initial, &age);

scanf (format string, input list);

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

Input/Output Function Ask the user to enter the distance in miles and

display the distance in kilometers?1. double miles, kms;

2. printf(“Enter the distance in miles: “);

3. scanf(“%lf”, &miles);

4. kms = miles * 1.609;

5. 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 ?

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

Changing the Display - Integer Example:

1. int X;2. X = 102;3. printf (“X1 = %d\n”, X);4. printf (“X2 = %4d\n”, X);5. printf (“X3 = %5d\n”, X);

Run:X1 = 102

X2 = 102

X3 = 102

Value Format

DisplayedOutput

234 %4d ▒234

234 %5d ▒▒234

234 %6d ▒▒▒234

234 %1d 234

-234 %4d -234

-234 %5d ▒-234

-234 %6d ▒▒-234

-234 %2d -234

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

Changing the Display - Double Example:

1. double X, Y, Z;2. X = 10.233. Y = 102.235;4. Z = 20.25. printf (“>%6.2f\n”, X);6. printf (“>%.2f\n”, Y);7. printf (“>%6.2f\n”, Z);

Run:> 10.23

>102.24

> 20.20

Value Format

Displayed Output

3.14159 %5.2f ▒3.14

3.14159 %3.2f 3.14

3.14159 %5.3f 3.142

3.14159 %4.2f 3.14

3.14159 %5.1f ▒▒3.1

3.14159 %8.5f ▒3.14159

0.1234 %4.2f 0.12

-0.006 %8.3f ▒▒-0.006

-0.006 %.3f -0.006

-0.006 %4.2f -0.01

-0.006 %8.5f -0.00600

-3.14159

%.4f -3.1416Hint: write the displayed output first, and then write the format

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

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

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

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.1./* include the header files here for any external function, such as input/output functions */

2./* define any Constant (not Variable) here */

3.int main (void)

4.{

5. /* Declare the variables here */

6. /* Write the executed statements */

7. return (0);

8.}

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

Be Ready to Write a Program in C

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

Program 1:Miles-to-Kilometers Conversion

What will be displayed on the screen after run this program?

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

Program 2:Finding the Value of the Coins1 Dollar = 100 cents , 1 Quarter = 25 cents , 1 dime = 10 cents1 Nickels = 5 cents , x pennies = x cents

What will be displayed on the screen after run this program? (2 slides)

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

Program 2:Finding the Value of the Coins (cont’d)

What is the problem that this program tries to solve?

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

Common Programming Errors Learn in the lab:

Debugging Syntax Errors Run-Time Errors Undetected Errors

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

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