problem solving and program design in c (5th edition) by jeri r. hanly and elliot b. koffman
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/1.jpg)
Problem Solving and Program Design in C
(5th Edition)
by Jeri R. Hanly and Elliot B. Koffman
CPCS 202Chapter 2 – Input/Output
12-10-1429
![Page 2: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/2.jpg)
© F
CIT@
KAU
CHAPTER 2 – Input / Output1. The Software
Development Method2. Variables3. Constants4. Output Operations and
Functions5. Input Operations and
Functions6. Using Comments7. Design / Algorithm Types8. Writing Program
Structure9. Changing the Display10.Types of Errors 2
#1. Hello World2. Get User ID3. Convert Miles to
Kilometers4. Finding the Value of the
Coins
column shows the topics index. column shows the programs index.
![Page 3: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/3.jpg)
© F
CIT@
KAU
The Software Development MethodA. 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.
B. Prototype
3
1
ImplementationProblem Analysis Design /Algorithm Testing Maintenance
1. 2. 3. 4. 5. 6.
![Page 4: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/4.jpg)
© F
CIT@
KAU
C. ExampleThe Software Development Method
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.
ImplementationProblem Analysis Design /Algorithm Testing Maintenance
1. 2. 3. 4. 5. 6.
4
1
![Page 5: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/5.jpg)
© F
CIT@
KAU
The Software Development Method
2. Analysis (تحليل): 1. Problem Input: miles 2. Problem Output: kilometers 3. Relevant Formula: 1 mile = 1.609 kilometers
ImplementationProblem Analysis Design /Algorithm Testing Maintenance
1. 2. 3. 4. 5. 6.
5
1
C. Example
![Page 6: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/6.jpg)
© F
CIT@
KAU
The Software Development Method
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.
ImplementationProblem Analysis Design /Algorithm Testing Maintenance
1. 2. 3. 4. 5. 6.
6
1
C. Example
![Page 7: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/7.jpg)
© F
CIT@
KAU
The Software Development Method
4. Implementation: (تنفيذ) Write the code.
ImplementationProblem Analysis Design /Algorithm Testing Maintenance
1. 2. 3. 4. 5. 6.
7
1
C. Example
![Page 8: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/8.jpg)
© F
CIT@
KAU
C. ExampleThe Software Development Method
5. Testing: 1. Verify that the program works properly. 2. Try few test cases. ( if the input is …, the output has to be … )
ImplementationProblem Analysis Design /Algorithm Testing Maintenance
1. 2. 3. 4. 5. 6.
8
1
![Page 9: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/9.jpg)
© F
CIT@
KAU
The Software Development Method
6. Maintenance (صيانة): 1. Remove undetected errors. 2. Keep it up-to-date.
ImplementationProblem Analysis Design /Algorithm Testing Maintenance
1. 2. 3. 4. 5. 6.
9
1
C. Example
![Page 10: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/10.jpg)
© F
CIT@
KAU
Variables (متغير)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:1. Declaring a variable2. Initializing a variable3. Assigning a new value
10
2
Data TypesNumbers only
Integerage = 36
Double \ Floatdegree = 36.4
Charactergender = ‘M’
The three data types showing are not all of the data types in C
![Page 11: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/11.jpg)
© F
CIT@
KAU
Variables – DeclaringA. 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)B. Syntax
C. Example int id; double dollar; char gender; double tall, width, weight; 11
2
type variable_list ;
Data TypesNumbers
onlyInteger
(int)Doubl
e (doub
le)
Character
(char)
![Page 12: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/12.jpg)
© F
CIT@
KAU
Variables – InitializingA. 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
functionB. Syntax
C. Example id = 0750428; dollar = 3.75; gender = ‘M’; 12
2
Expression could be a number or an equation
variable = expression ;
![Page 13: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/13.jpg)
© F
CIT@
KAU
Variables – Declaring & InitializingA. 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
B. Syntax
C. Example int id = 0750428; double dollar = 3.75; char gender = ‘M’; 13
2
type variable = expression ;
Expression could be a number or an equation
![Page 14: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/14.jpg)
© F
CIT@
KAU
Variables – AssigningA. Introduction
Changing the value of a variable called assigning a new value to the variable
B. Syntax
C. Example id = 0750428; dollar = 3.75; gender = ‘M’;
14
2
variable = expression ;
Expression could be a number or an equation
![Page 15: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/15.jpg)
© F
CIT@
KAUVariables
Declaring, Assigning, and InitializingConclusion 1
int id, age;
double dollar;
char gender = ‘d’;
id = 0750428;
dollar = 3.75;
age = 21;
dollar = 3.77;
15
2
Decla
ring
Initi
alizi
ng
Assig
ning
Decla
ring
&As
signi
ng
![Page 16: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/16.jpg)
© F
CIT@
KAUVariables
Tracking Variables in the MemoryConclusion 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: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;
16The memory stores the last value of each variable
# X Y Z1. 102. 15.53. 25.54. 26.55. 306. 207. 2
2
![Page 17: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/17.jpg)
© F
CIT@
KAU
Constants (ثابت)A. 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 onlyB. Syntax
C. Examples #define id 0750428 #define dollar 3.75 17
3
#define variable value
![Page 18: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/18.jpg)
© F
CIT@
KAU
Output Operations and FunctionsA. 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 stdio in order to use the function printf.
B. Syntax
18
printf (format string);printf (format string, print list);
4
![Page 19: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/19.jpg)
© F
CIT@
KAU
Output Operations and FunctionsC. Examples
1. Display Hello… on the screen in C?use the function printfprintf(“Hello…”);
2. Display Hello… in 1st line and good in 2nd line?use the operation \nprintf(“Hello…\ngood”);
3. Display the value of the variable age; (if age is integer)?use the operation %d printf(“%d”, age);
19
4
![Page 20: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/20.jpg)
© F
CIT@
KAU
Output Operations and FunctionsC. Examples
4. Display the value of the variable X; (if X is double\float)? use the operation %fprintf(“%f”, X);
5. Display the value of the variable Y; (if Y is character)? use the operation %cprintf(“%c”, Y);
6. Display My age is then the value of the variable AGE?printf(“My age is %d”, AGE);
7. Display the variables age and GPA in one statement?printf(“I am %d years old, GPA: %f”, age, GPA);
20
4
![Page 21: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/21.jpg)
© F
CIT@
KAU
Output Operations and FunctionsD. 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);
21
4
![Page 22: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/22.jpg)
© F
CIT@
KAU
Input Operations and FunctionsA. 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 stdio in order to use the function scanf.
B. Syntaxscanf (format string, input list);
22
5
![Page 23: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/23.jpg)
© F
CIT@
KAU
Input Operations and FunctionsC. Examples
1. Get the value for the variable X; (if X is integer)?use the operation %d scanf(“%d”, &X);
2. Get the value for the variable X; (if X is double\float)? use the operation %lf (Long Float) and not %f scanf(“%lf”, &X);
3. Get the value for the variable X; (if X is character)?use the operation %d scanf(“%c”, &X);
23
5
![Page 24: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/24.jpg)
© F
CIT@
KAU
Input Operations and FunctionsC. Examples
4. Get 3 characters from the user?scanf(“%c%c%c”, &first, &second, &third);
D. 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);
24
5
![Page 25: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/25.jpg)
© F
CIT@
KAU
Using CommentsA. With your comments, it will easy to remember
the job of each statement in your program.B. 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 */
C. Example:1. /* Name: Daniyal 2. ID: 707997 */3. double miles, kms;4. /* EXECUTABLE STATMENTS */5. printf(“Enter the distance in miles: “); // ask the user6. scanf(“%lf”, &miles); // get the miles
25
6
![Page 26: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/26.jpg)
© F
CIT@
KAU
Design/Algorithm Types 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
26
7
Algorithms could write any way, but they have to be understandable
![Page 27: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/27.jpg)
© F
CIT@
KAU
Writing Program StructureA. Any group of statements needs to be
inside a function(Note: you will learn later more about functions and write more than one function)
B. The main function will be executed first1. /* 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. /* Start writing the statements */7. return (0);8. }
you have to declare the variables at the beginning of any function 27
8
Be Ready to Write a Program in
C
![Page 28: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/28.jpg)
© F
CIT@
KAU
Writing Program Structure
28
8
C. Example
![Page 29: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/29.jpg)
© F
CIT@
KAU
Hello WorldA.Problem
We need a program that displays on the screen the text Hello World!!
B.Analysis Input Output
the text “Hello World!!” Formula
C.Design1. Display “Hello World!!” on the screen
29
1
ImplementationProblem Analysis Design Outline Testing Maintenance
![Page 30: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/30.jpg)
© F
CIT@
KAU
#include <stdio.h>
int main(void){ // 1. Display “Hello World!!” on the screen printf(“Hello World!!\n”);
return(0);}
1.2.3.4.5.6.7.8.9.
Hello WorldD.Outline
E.Implementation
30
1
ImplementationProblem Analysis Design Outline Testing Maintenance
#include <stdio.h>
int main(void){ // 1. Display “Hello World!!” on the screen
return(0);}
1.2.3.4.5.6.7.8.9.
![Page 31: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/31.jpg)
© F
CIT@
KAU
#include <stdio.h>
int main(void){ // 1. Display “Hello World!!” on the screen printf(“Hello World!!\n”);
return(0);}
1.2.3.4.5.6.7.8.9.
Hello WorldD.Outline
E.Implementation
31
1
ImplementationProblem Analysis Design Outline Testing Maintenance
#include <stdio.h>
int main(void){ // 1. Display “Hello World!!” on the screen
return(0);}
1.2.3.4.5.6.7.8.9.
![Page 32: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/32.jpg)
© F
CIT@
KAU
Get User IDA.Problem
Write a program that gets the ID value from the userB.Analysis
Input ID
Output Formula
C.Design1. Get the ID from the user user_ID
32
Put the value you get from the user in a
variable called user_ID
2
ImplementationProblem Analysis Design Outline Testing Maintenance
Number of variables: 1This indicates that you need to declare 1 variable in the program
![Page 33: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/33.jpg)
© F
CIT@
KAU
Get User ID
33
2
ImplementationProblem Analysis Design Outline Testing Maintenance
#include <stdio.h>
int main(void){ int user_ID;
// 1. Get the ID from the user
return(0);}
1.2.3.4.5.6.7.8.9.10.11.
![Page 34: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/34.jpg)
© F
CIT@
KAU
Get User ID
34
2
DON’T try to get a value from a user (scanf) without asking (printf)
ImplementationProblem Analysis Design Outline Testing Maintenance
#include <stdio.h>
int main(void){ int user_ID;
// 1. Get the ID from the user printf(“Please enter your user ID: “); scanf(“%d”, user_ID);
return(0);}
1.2.3.4.5.6.7.8.9.10.11.12.
![Page 35: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/35.jpg)
© F
CIT@
KAU
Convert Miles to KilometersA.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.
35Write the Analysis & Design for this problem?
3
Miles Kilometer
ImplementationProblem Analysis Design Outline Testing Maintenance
![Page 36: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/36.jpg)
© F
CIT@
KAU
Convert Miles to KilometersB.Analysis
Input KMS_PER_MILE = 1.609 Miles
Output Kilometers
Formula Kilometers = Miles x 1.609
C.Design1. Get the number of miles from the user miles2. Convert miles to kilometers:
kms = miles x KMS_PER_MILE3. Display the number of kilometers kms
36
3
ImplementationProblem Analysis Design Outline Testing Maintenance
They are 2 variables in the Design, so 2 variables need to be declared
This input has a constant value
![Page 37: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/37.jpg)
© F
CIT@
KAU
Convert Miles to Kilometers
37
3
ImplementationProblem Analysis Design Outline Testing Maintenance
/* 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);}
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.
![Page 38: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/38.jpg)
© F
CIT@
KAU
/* 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);}
Convert Miles to Kilometers
38
3
ImplementationProblem Analysis Design Outline Testing Maintenance
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22. This program has a mistake that
may confuse any user, what is it?
![Page 39: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/39.jpg)
© F
CIT@
KAU
/* 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);}
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.
Convert Miles to Kilometers
39
3
ImplementationProblem Analysis Design Outline Testing Maintenance
The programmer tries to get a value from the user without display any message on the screen to notify the user
![Page 40: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/40.jpg)
© F
CIT@
KAU
/* 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);}
Convert Miles to Kilometers
40
3
ImplementationProblem Analysis Design Outline Testing Maintenance
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.
![Page 41: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/41.jpg)
© F
CIT@
KAU
/* 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);}
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.
Convert Miles to Kilometers
41
3
ImplementationProblem Analysis Design Outline Testing Maintenance
![Page 42: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/42.jpg)
© F
CIT@
KAU
Finding the Value of the CoinsA.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
42Quiz: Write the Analysis & Design for this problem?
4
ImplementationProblem Analysis Design Outline Testing Maintenance
![Page 43: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/43.jpg)
© F
CIT@
KAU
Finding the Value of the CoinsB.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
43
4
ImplementationProblem Analysis Design Outline Testing Maintenance
![Page 44: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/44.jpg)
© F
CIT@
KAU
Finding the Value of the CoinsC.Design (1st version)
1. Get the count of each kind of coin2. Find the value in dollars and change3. Display the value in dollars and the change
44
4
ImplementationProblem Analysis Design Outline Testing Maintenance
C.Design (2nd version)1. Get the count of each kind of coin2. Compute the total value in cents3. Find the value in dollars and change4. Display the value in dollars and the change
![Page 45: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/45.jpg)
© F
CIT@
KAU
Finding the Value of the CoinsC.Design (3rd version)
1. Get the count of the quarters quarters2. Get the count of the dimes dimes3. Get the count of the nickels nickels4. Get the count of the pennies pennies5. Compute the total value in cents:
total_cents = quarters x 25 + dimes x 10 + nickels x 5 + pennies x 1
6. Find the value in dollars and change:dollars = total_cents / 100change = total_cents % 100
7. Display the value in dollars dollars8. Display the change change
45
4
ImplementationProblem Analysis Design Outline Testing Maintenance
How many variables need to be declared? and what are their types?
![Page 46: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/46.jpg)
© F
CIT@
KAU
/* * 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 */ int total_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);}
Finding the Value of the Coins
46
4
ImplementationProblem Analysis Design Outline Testing Maintenance
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.
![Page 47: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/47.jpg)
© F
CIT@
KAU
/* * 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 */ int total_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);
Finding the Value of the Coins
47
41 dollar = 100 cents 1 quarter = 25 cents1 dime = 10 cents1 nickel = 5 cents1 penny = 1 cent
ImplementationProblem Analysis Design Outline Testing Maintenance
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.
![Page 48: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/48.jpg)
© F
CIT@
KAU
/* 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);}
Finding the Value of the Coins
48
41 dollar = 100 cents 1 quarter = 25 cents1 dime = 10 cents1 nickel = 5 cents1 penny = 1 cent
ImplementationProblem Analysis Design Outline Testing Maintenance
23.24.25.26.27.28.29.30.31.32.
33.34.35.36.37.38.39.40.
41.42.43.
![Page 49: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/49.jpg)
© F
CIT@
KAU
Changing the Display - IntegerA. You can organize the display for integer
variables.B. Formats:
49
Value
Format
Displayed Output
234 %4d ▒234234 %5d ▒▒234234 %6d ▒▒▒234234 %1d 234-234 %4d -234-234 %5d ▒-234-234 %6d ▒▒-234-234 %2d -234
9
C. Example:1. printf(“ X Y\n”);2. printf(“---------\n”);3. printf(“%4d%4d\n”, 1, 2);4. printf(“%4d%4d\n”, 13, 6);5. printf(“%4d%4d\n”, 37, 513);
Run: X Y--------- 1 2 13 6 37 513
![Page 50: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/50.jpg)
© F
CIT@
KAU
Changing the Display - DoubleA. You can organize the display for double
variables.B. Formats:
50
Value Format
Displayed Output
3.14159
%5.2f ▒3.14
3.14159
%3.2f 3.14
3.14159
%5.3f 3.142
3.14159
%5.1f ▒▒3.1
0.1234 %4.2f 0.12-0.006 %8.3f ▒▒-
0.006-0.006 %4.2f -0.01-0.006 %8.5f -0.00600-3.14159
%.4f -3.1416
Hint: think about the displayed output first, then write the format
9
C. Example:1. double X, Y, Z;2. X = 10.23;3. Y = 102.235;4. Z = 20.2;5. printf(“>%6.2f\n”, X);6. printf(“>%6.2f\n”, Y);7. printf(“>%6.2f\n”, Z);
Run: 10.23102.24 20.20
![Page 51: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/51.jpg)
© F
CIT@
KAU
Types of Errors
• The program will not run until the error fixed.• e.g. missing a semicolon.
1. Syntax Error
51
10
![Page 52: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/52.jpg)
© F
CIT@
KAU
Types of Errors: Syntax Error
52
10
.1.A pop-up window
will appear. CHOSE No to stop
the program.2.
Check the error message and the
line number
.3.Fix the error
![Page 53: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/53.jpg)
© F
CIT@
KAU
Types of Errors: Run-Times Error
53
10
Check the cause of the pop-up error message?
![Page 54: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/54.jpg)
© F
CIT@
KAU
Questions1. The main different between the variables and
the constants in a program is:a) the value of the constant can be changed during
the programb) the value of the variable can be changed during
the programc) there is no difference between themd) none of the above is a correct statement
2. The codes at any function written in C language need to follow the following order:a) first, we write the executed statements. Then, we
declare the variablesb) first, we declare the variables. Then, we write the
executed statementsc) we can declare any variable at anyplace inside a
function-54-
…
![Page 55: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/55.jpg)
© F
CIT@
KAU
Questions3. Which of the following statements have a
syntax error:a) X = 10;b) Y = 20c) Z = 15.5;d) Y = X + Z;e) Y = Y + 1;f) Z = Y – 1.5 + 5;g) 23 = Z; h) Z = Z / 2 + 5;
-55-
…
![Page 56: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/56.jpg)
© F
CIT@
KAU
Homework1. Type Program 3 and run it, and display your
name and your ID at the beginning of the output ?(Take a copy of the code and a snapshot of the output of three different test cases, and then print them in ONE page only)
2. Track the memory in Program 4 ?(You need to show the code, the output of one test case, and tracking the memory in ONE page only; you can choose any input values)
-56-
…
HANDWRITING IN THE HOMEWORK IS NOT ACCEPTABLE
![Page 57: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman](https://reader034.vdocuments.net/reader034/viewer/2022051317/56816359550346895dd416de/html5/thumbnails/57.jpg)
© F
CIT@
KAU
CHAPTER 2 – Input / Output1. The Software
Development Method2. Variables3. Constants4. Output Operations and
Functions5. Input Operations and
Functions6. Using Comments7. Design / Algorithm Types8. Writing Program
Structure9. Changing the Display10.Types of Errors 57
#1. Hello World2. Get User ID3. Convert Miles to
Kilometers4. Finding the Value of the
Coins
column shows the topics index. column shows the programs index.