Anda di halaman 1dari 28

١٧/١٠/١٤٢٩

Problem Solving and Program Design in C


(5th Edition)

by Jeri R. Hanly and Elliot B. Koffman

CPCS 202
Chapter 2 – Input/Output

11-10-1429

© FCIT@KAU

CHAPTER 2 – Input / Output #

1. The Software Development 1. Hello World


Method 2. Get User ID
2. Variables 3
3. Convert Miles to Kilometers
3. Constants 4. Finding the Value of the Coins
4. Output Operations and
Functions
5. Input Operations and
Functions
6 Using Comments
6.
7. Design / Algorithm Types
8. Writing Program Structure
9. Changing the Display
10. Types of Errors
 column shows the topics index.  column shows the programs index. 2

١
١٧/١٠/١٤٢٩

© FCIT@KAU
The Software Development Method 1

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

© FCIT@KAU

The Software Development Method 1

C. Example
1. 2. 3. 4. 5. 6.
Design /
Problem Analysis Implementation Testing Maintenance
Algorithm

1. Problem:
Your summer surveying job requires you to study some maps
that give distances in kilometers and some that use miles. You
andd your coworkers
k prefer
f to
t deal
d l iin metric
t i measurements.
t
Write a program that performs the necessary conversion.

٢
١٧/١٠/١٤٢٩

© FCIT@KAU
The Software Development Method 1

C. Example
1. 2. 3. 4. 5. 6.
Design /
Problem Analysis Implementation Testing Maintenance
Algorithm

2. Analysis (‫)ﺗﺤﻠﻴﻞ‬:
1. Problem Input: miles
2. Problem Output: kilometers
3. Relevant Formula: 1 mile = 1.609 kilometers

© FCIT@KAU

The Software Development Method 1

C. Example
1. 2. 3. 4. 5. 6.
Design /
Problem Analysis Implementation Testing Maintenance
Algorithm

3. Design / Algorithm:
1. Get the distance in miles from the user.
2. Convert the distance to kilometers.
( kilometer = 1.609 miles))
(1
3. Display the distance in kilometers on the screen.

٣
١٧/١٠/١٤٢٩

© FCIT@KAU
The Software Development Method 1

C. Example
1. 2. 3. 4. 5. 6.
Design /
Problem Analysis Implementation Testing Maintenance
Algorithm

4. Implementation: (‫)ﺗﻨﻔﻴﺬ‬
Write the code.

© FCIT@KAU

The Software Development Method 1

C. Example
1. 2. 3. 4. 5. 6.
Design /
Problem Analysis Implementation Testing Maintenance
Algorithm

5. Testing:
1. Verify that the program works properly.
2. Try few test cases.
( iff the input is …, the output has to be … )

٤
١٧/١٠/١٤٢٩

© FCIT@KAU
The Software Development Method 1

C. Example
1. 2. 3. 4. 5. 6.
Design /
Problem Analysis Implementation Testing Maintenance
Algorithm

6. Maintenance (‫)ﺻﻴﺎﻧﺔ‬:
1. Remove undetected errors.
2. Keep it up-to-date.

© FCIT@KAU

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: Data Types
 No need to write any comment
 Easy to track Character
Numbers only
 Case sensitive gender = ‘M’

 Don’tt use a Reserved Word


Don
 Three important stages in variables: Integer
age = 36
1. Declaring a variable
2. Initializing a variable Double \ Float

3. Assigning a new value degree = 36.4

The three data types showing are not all of the data types in C 10

٥
١٧/١٠/١٤٢٩

© FCIT@KAU
Variables – Declaring 2

A. Introduction
 Each variable needs to be declared before using it
 Declaring
D l i a variable
i bl reserves space ini the
th memory for f the
th
value
 Declaring variables has to be at the beginning of functions
(before writing any statement)
Data Types
B. Syntax In C

Numbers Character
only
l ( h )
(char)
C. Example
Integer
 int id; (int)
 double dollar;
Double
 char gender; (double)
11

© FCIT@KAU

Variables – Initializing 2

A. Introduction
 The first value for a variable called initialing a variable
 You
Y can nott use a variable
i bl without
ith t initializing
i iti li i
 You can initial a variable at any place inside the function
B. Syntax

C
C. Example
 id = 0750428;
 dollar = 3.75;
 gender = ‘M’;

Expression could be a number or an equation 12

٦
١٧/١٠/١٤٢٩

© FCIT@KAU
Variables – Declaring & Initializing 2

A. 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’;

Expression could be a number or an equation 13

© FCIT@KAU

Variables – Assigning 2

A. 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’;

Expression could be a number or an equation 14

٧
١٧/١٠/١٤٢٩

Variables

© FCIT@KAU
Declaring, Assigning, and Initializing 2

Conclusion 1

aring
 int id, age;

Decla
 double dollar;

Declaring

Assigning
 char gender = ‘d’;

&
 id = 0750428;

alizing
 dollar = 3.75;
Initia
 age = 21;
Assigning
 dollar = 3.77;

15

Variables
© FCIT@KAU

Tracking Variables in the Memory 2

Conclusion 2
 You can track the values of each variable in a table
 The
Th following
f ll i program m consists
i t off 3 variables.
i bl TheTh table
t bl tracks
t k
the values of each variable after executing each statement:
1. X = 10; # X Y Z
2. Z = 15.5; 1. 10
3. Y = X + Z; 2. 15.5
3. 25.5
4. Y = Y + 1;
4
4. 26 5
26.5
5. Z = Y – 1.5 + 5;
5. 30
6. Z = Z / 2 + 5;
6. 20
7. X = Z % 3;
7. 2

The memory stores the last value of each variable 16

٨
١٧/١٠/١٤٢٩

© FCIT@KAU
Constants (‫)ﺛﺎﺑﺖ‬ 3

A. Introduction
 Store a value under a name
 The
Th value
l off the
th constant
t t can’t
’t be
b changed
h d
= 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
B. Syntax

C. Examples
 #define id 0750428
 #define dollar 3.75
17

© FCIT@KAU

Output Operations and Functions 4

A. Introduction
 The output operations and functions will help you to
display anything on the screen.
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

٩
١٧/١٠/١٤٢٩

© FCIT@KAU
Output Operations and Functions 4

C. Examples
1. Display Hello… on the screen in C?
use the function printf
printf(“Hello…”);

2. Display Hello… in 1st line and good in 2nd line?


use the operation \n
printf(“Hello…\ngood”);

3. Display the value of the variable X; (if X is integer)?


use the operation %d
printf(“%d”, X);

19

© FCIT@KAU

Output Operations and Functions 4

C. Examples
4. Display the value of the variable X; (if X is double\float)?
use the operation %f
printf(“%f”, X);

5. Display the value of the variable X; (if X is character)?


use the operation %c
printf(“%c”, X);
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

١٠
١٧/١٠/١٤٢٩

© FCIT@KAU
Output Operations and Functions 4

D. 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

© FCIT@KAU

Input Operations and Functions 5

A. Introduction
 The input operations and functions will help you to get values
for the variables from users.
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
B. Syntax

22

١١
١٧/١٠/١٤٢٩

© FCIT@KAU
Input Operations and Functions 5

C. 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

© FCIT@KAU

Input Operations and Functions 5

C. Examples
4. Get 3 characters from the user?
scanf(“%c%c%c”
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

١٢
١٧/١٠/١٤٢٩

© FCIT@KAU
Using Comments 6

A. With your comments, it will easy to remember the job of


each statement in your program.
B.
B Comment
C t Types
T in
i C language:
l
 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 user
6. scanf(“%lf”, &miles); // get the miles

25

© FCIT@KAU

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
variables’ names:
1. Get the value TotalMiles
2. TotalKilo = TotalMiles / 1.609
3. Display the value TotalKilo

Algorithms could write any way, but they have to be understandable 26

١٣
١٧/١٠/١٤٢٩

© FCIT@KAU
Writing Program Structure 8

A. Any group of statements needs to be


Be Ready to
inside a function
(Note: you will learn later more about Write a
functions and write more than one function) Program in C
B. 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. /* Start writing the statements */
7. return (0);
8. }

you have to declare the variables at the beginning of any function 27

© FCIT@KAU

Writing Program Structure 8

C. Example

28

١٤
١٧/١٠/١٤٢٩

© FCIT@KAU
Problem Analysis Design Outline Implementation Testing Maintenance

Hello World 1

A. Problem
 We need a program that displays on the screen the text
Hello World!!
B. Analysis
 Input
 Output
 the text “Hello World!!”
 Formula
C. Design
1. Display “Hello World!!” on the screen

29

© FCIT@KAU

Problem Analysis Design Outline Implementation Testing Maintenance

Hello World 1

D. Outline
1. #include <stdio.h>
2.
3. int main(void)
4. {
5. // 1. Display “Hello World!!” on the screen
6. return(0);
7. }
E. Implementation
1. #include <stdio.h>
2.
3. int main(void)
4. {
5. // 1. Display “Hello World!!” on the screen
6
6. printf(“Hello
printf( Hello World!!\n”);
World!!\n );
7. return(0);
8. }

F. Testing

30

١٥
١٧/١٠/١٤٢٩

© FCIT@KAU
Problem Analysis Design Outline Implementation Testing Maintenance

Get User ID 2

A. Problem
 Write a program that gets the ID value from the user
B. Analysis
A l i
 Input
 ID
 Output
 Formula
C. Design
g Number of variables: 1
1. Get the ID from the user  user_ID 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
31

© FCIT@KAU

Problem Analysis Design Outline Implementation Testing Maintenance

Get User ID 2

1. #include <stdio.h>
2.
3. int main(void)
4.
4 {
5. int user_ID;
6.
7. // 1. Get the ID from the user
8.
9. return(0);
10. }

32

١٦
١٧/١٠/١٤٢٩

© FCIT@KAU
Problem Analysis Design Outline Implementation Testing Maintenance

Get User ID 2

1. #include <stdio.h>
2.
3. int main(void)
4.
4 {
5. int user_ID;
6.
7. // 1. Get the ID from the user
8. printf(“Please enter your user ID: “);
9. scanf(“%d”, user_ID);
10.
11. return(0);
12
12. }

DON’T try to get a value from a user (scanf) without asking (printf) 33

© FCIT@KAU

Problem Analysis Design Outline Implementation Testing Maintenance

Convert Miles to Kilometers 3

A. 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? 34

١٧
١٧/١٠/١٤٢٩

© FCIT@KAU
Problem Analysis Design Outline Implementation Testing Maintenance

Convert Miles to Kilometers 3

B. Analysis
 Input
 Miles
 Output
 Kilometers
 Formula
 Kilometers = Miles x 1.609
C. Design
1. Get the number of miles from the user  miles
2. Convert miles to kilometers:
kilometers = miles x 1.609
3. Display the number of kilometers  kilometers

They are 2 variables in the Design, so 2 variables need to be declared 35

© FCIT@KAU

Problem Analysis Design Outline Implementation Testing Maintenance

Convert Miles to Kilometers 3


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

36

١٨
١٧/١٠/١٤٢٩

© FCIT@KAU
Problem Analysis Design Outline Implementation Testing Maintenance

Convert Miles to Kilometers 3


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

This program has a mistake that may confuse any user, what is it? 37

© FCIT@KAU

Problem Analysis Design Outline Implementation Testing Maintenance

Convert Miles to Kilometers 3


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

38

١٩
١٧/١٠/١٤٢٩

© FCIT@KAU
Problem Analysis Design Outline Implementation Testing Maintenance

Convert Miles to Kilometers 3


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

© FCIT@KAU

Problem Analysis Design Outline Implementation Testing Maintenance

Convert Miles to Kilometers 3


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

٢٠
١٧/١٠/١٤٢٩

© FCIT@KAU
Problem Analysis Design Outline Implementation Testing Maintenance

Finding the Value of the Coins 4

A. Problem
 We need a program that calculates the number of coins in
a save box
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? 41

© FCIT@KAU

Problem Analysis Design Outline Implementation Testing Maintenance

Finding the Value of the Coins 4

B. 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

42

٢١
١٧/١٠/١٤٢٩

© FCIT@KAU
Problem Analysis Design Outline Implementation Testing Maintenance

Finding the Value of the Coins 4

C. Design (1st version)


1. Get the count of each kind of coin
2 Find the value in dollars and change
2.
3. Display the value in dollars and the change

C. Design (2nd version)


1. Get the count of each kind of coin
2. Compute the total value in cents
3 Find the value in dollars and change
3.
4. Display the value in dollars and the change

43

© FCIT@KAU

Problem Analysis Design Outline Implementation Testing Maintenance

Finding the Value of the Coins 4

C. Design (3rd version)


1. Get the count of the quarters  quarters
2 Get the count of the dimes  dimes
2.
3. Get the count of the nickels  nickels
4. Get the count of the pennies  pennies
5. Compute the total value in cents:
total_cents = quarters x 25 + dimes x 10 + nickels x 5
+ pennies x 1
6. Find
Fi d the
h value
l iin d
dollars
ll and
d change:
h
dollars = total_cents / 100
change = total_cents % 100
7. Display the value in dollars  dollars
8. Display the change  change
How many variables need to be declared? and what are their types? 44

٢٢
١٧/١٠/١٤٢٩

© FCIT@KAU
Problem Analysis Design Outline Implementation Testing Maintenance

Finding the Value of the Coins 4


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

© FCIT@KAU

Problem Analysis Design Outline Implementation Testing Maintenance

Finding the Value of the Coins 4


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

٢٣
١٧/١٠/١٤٢٩

© FCIT@KAU
Problem Analysis Design Outline Implementation Testing Maintenance

Finding the Value of the Coins 4


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

47

© FCIT@KAU

Changing the Display - Integer 9

A. You can organize the display for integer variables.


B. Formats:
Value Format Displayed C. Example:
Output
1. printf(“ X Y\n”);
234 %4d ▒234 2. printf(“---------\n”);
234 %5d ▒▒234 3. printf(“%4d%4d\n”, 1, 2);
4. printf(“%4d%4d\n”, 13, 6);
234 %6d ▒▒▒234 5. printf(“%4d%4d\n”, 37, 513);
234 %1d 234 Run:
-234 %4d -234 X Y
---------
-234 %5d ▒-234 1 2
-234 %6d ▒▒-234 13 6
37 513
-234 %2d -234
48

٢٤
١٧/١٠/١٤٢٩

© FCIT@KAU
Changing the Display - Double 9

A. You can organize the display for double variables.


B. Formats:
Value Format Displayed C. Example:
Output
1. double X, Y, Z;
3.14159 %5.2f ▒3.14 2. X = 10.23;
3.14159 %3.2f 3.14 3. Y = 102.235;
3.14159 %5.3f 3.142 4. Z = 20.2;
5. printf(“>%6.2f\n”, X);
3.14159 %5.1f ▒▒3.1
6. printf(“>%6.2f\n”, Y);
0 1234
0.1234 %4 2f
%4.2f 0 12
0.12 7. printf(“>%6.2f\n”,
i tf(“>%6 2f\ ” Z)
Z);
-0.006 %8.3f ▒▒-0.006
-0.006 %4.2f -0.01
Run:
10.23
-0.006 %8.5f -0.00600 102.24
-3.14159 %.4f -3.1416 20.20

Hint: think about the displayed output first, then write the format 49

© FCIT@KAU

Types of Errors 10

2. Run-time
• The program will Error • The result is not
not run until the correct.
error fixed. • Discovered while
the program • e.g. to find the sum
• e.g. missing a running. of X and Y, the
semicolon. equation is X/Y
• e.g. dividing a
11. Syntax number byy zero
3. Logic Error
Error

50

٢٥
١٧/١٠/١٤٢٩

© FCIT@KAU
Types of Errors: Syntax Error 10

.3.
Fix the error

.1.
A pop-up window will
appear CHOSE No to
appear.
stop the program

.2.
Check the error
message and the line
number
51

© FCIT@KAU

Types of Errors: Run-Times Error 10

Check the cause of the pop-up error message? 52

٢٦
١٧/١٠/١٤٢٩

© FCIT@KAU
Questions …

1. The main different between the variables and the


constants in a program is:
a) the value of the constant can be changed during the program
b) the value of the variable can be changed during the program
c) there is no difference between them
d) 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.
statements Then, we declare the
variables
b) first, we declare the variables. Then, we write the executed
statements
c) we can declare any variable at anyplace inside a function
-53-

© FCIT@KAU

Questions …

3. Which of the following statements have a syntax error:


a) X = 10;
b) Y = 20
c) 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;

-54-

٢٧
١٧/١٠/١٤٢٩

© FCIT@KAU
Homework …

1. 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)

HANDWRITING IN THE HOMEWORK IS NOT ACCEPTABLE -55-

© FCIT@KAU

CHAPTER 2 – Input / Output #

1. The Software Development 1. Hello World


Method 2. Get User ID
2. Variables 3
3. Convert Miles to Kilometers
3. Constants 4. Finding the Value of the Coins
4. Output Operations and
Functions
5. Input Operations and
Functions
6 Using Comments
6.
7. Design / Algorithm Types
8. Writing Program Structure
9. Changing the Display
10. Types of Errors
 column shows the topics index.  column shows the programs index. 56

٢٨

Anda mungkin juga menyukai