Professional Documents
Culture Documents
CPCS 202
Chapter 2 – Input/Output
11-10-1429
© FCIT@KAU
١
١٧/١٠/١٤٢٩
© 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
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
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
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’
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’;
٦
١٧/١٠/١٤٢٩
© 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’;
© 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’;
٧
١٧/١٠/١٤٢٩
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
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
٨
١٧/١٠/١٤٢٩
© 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
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…”);
19
© FCIT@KAU
C. Examples
4. Display the value of the variable X; (if X is double\float)?
use the operation %f
printf(“%f”, X);
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
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);
23
© FCIT@KAU
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
25
© FCIT@KAU
Design/Algorithm Types 7
١٣
١٧/١٠/١٤٢٩
© FCIT@KAU
Writing Program Structure 8
© FCIT@KAU
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
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
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
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.
١٧
١٧/١٠/١٤٢٩
© FCIT@KAU
Problem Analysis Design Outline Implementation Testing Maintenance
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
© FCIT@KAU
36
١٨
١٧/١٠/١٤٢٩
© FCIT@KAU
Problem Analysis Design Outline Implementation Testing Maintenance
This program has a mistake that may confuse any user, what is it? 37
© FCIT@KAU
38
١٩
١٧/١٠/١٤٢٩
© FCIT@KAU
Problem Analysis Design Outline Implementation Testing Maintenance
© FCIT@KAU
٢٠
١٧/١٠/١٤٢٩
© FCIT@KAU
Problem Analysis Design Outline Implementation Testing Maintenance
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
© FCIT@KAU
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
43
© FCIT@KAU
٢٢
١٧/١٠/١٤٢٩
© FCIT@KAU
Problem Analysis Design Outline Implementation Testing Maintenance
© FCIT@KAU
٢٣
١٧/١٠/١٤٢٩
© FCIT@KAU
Problem Analysis Design Outline Implementation Testing Maintenance
47
© FCIT@KAU
٢٤
١٧/١٠/١٤٢٩
© FCIT@KAU
Changing the Display - Double 9
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
٢٦
١٧/١٠/١٤٢٩
© FCIT@KAU
Questions …
© FCIT@KAU
Questions …
-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)
© FCIT@KAU
٢٨