Upload
lawlietll
View
214
Download
0
Embed Size (px)
Citation preview
7/22/2019 Ece246 Chapter 2 (2)
1/25
ECE 246(INTRODUCTION TO C
PROGRAMMING)CHAPTER 2:
INPUT AND OUTPUT
7/22/2019 Ece246 Chapter 2 (2)
2/25
Learning Outcomes Able to apply the programming development
procedure
Able to understand the usage of printf withdifferent data types of variables
Able to understand the usage of scanf with
different data types of variables
7/22/2019 Ece246 Chapter 2 (2)
3/25
Lesson Outline
Programming development procedure
printf
scanf
7/22/2019 Ece246 Chapter 2 (2)
4/25
Programming development procedure1. Analyze the problem
2. Develop a solution
3. Code the solution
4. Test and correct the program
7/22/2019 Ece246 Chapter 2 (2)
5/25
Procedure 1: Analyze the problem Basic analysis: determine and understand the
input items and the desired outputs items to be
produced.If necessary, do:
Extended analysis: obtain additional info
about the problem so that you can thoroughlyunderstand what is being asked and how to
achieve the result
7/22/2019 Ece246 Chapter 2 (2)
6/25
Procedure 2: Develop a solution Design & development: algorithm for
transforming the input items into the desired
outputs Top-down approach: start with the most
general solution and refine it into clearly
defined tasks
7/22/2019 Ece246 Chapter 2 (2)
7/25
Procedure 3: Code the solution Code the solution algorithm in C
7/22/2019 Ece246 Chapter 2 (2)
8/25
Procedure 4:
Test & Debug the Program Select test data
If error(s) found, make correction
7/22/2019 Ece246 Chapter 2 (2)
9/25
ExampleThe electrical resistance, rof a metal wire, in
ohms is given by the formula: r = (ml)/awhere mis the resistivity of the metal; lis thelength of the wire (feet); and ais the cross-sectional area of the wire (circular mills).Using this info, write a C program to calculate
the resistance of a wire that is 125 feet long,cross sectional area of 500 circular mills, andis copper. Resistivity of copper, mis 10.4.
7/22/2019 Ece246 Chapter 2 (2)
10/25
Step 1 Key problem statement: to calculate the
resistance of the wire
Output: resistance Inputs: m, l and a
7/22/2019 Ece246 Chapter 2 (2)
11/25
Step 2
Given formula: r = (ml)/a
Pseudocodes:
Assign values to m, l and a
Calculate the resistance using the formula
Display the result
Do manual calculation to verify
7/22/2019 Ece246 Chapter 2 (2)
12/25
Step 3
#include
Int main()
{
float resistivity, area, length, resistance;
resistivity = 10.4;
area = 500;
length = 125;
resistance = (resistivity * length)/ area;
printf(The resistance of the wire (in ohms) is %f \n, resistance);
return 0;
}
7/22/2019 Ece246 Chapter 2 (2)
13/25
Step 4
Verify the correctness of the program
Try with different set of data and check with
manual calculation
7/22/2019 Ece246 Chapter 2 (2)
14/25
Exercise
Write a C program to calculate the total
resistance of a series circuit. In such a circuit,
the total resistance is the sum of all 56 ohm,33 ohm and 15 ohm resistors.
7/22/2019 Ece246 Chapter 2 (2)
15/25
printf
C function for output
Used for printing/displaying text
For string:printf ("..some string...");
For integer:
printf ("Integer = %d", integer name); For float
printf ("Integer = %f", float name);
7/22/2019 Ece246 Chapter 2 (2)
16/25
scanf
C function for input
Gets input from the keyboard
Examples of scanf usage:
int i;
char ch;
float x;
scanf ("%d %c %f", &i, &ch, &x);
7/22/2019 Ece246 Chapter 2 (2)
17/25
1 /* Fig. 2.5: fig02_05.c2 Addition program */3 #include4
5 /* function main begins program execution */6 intmain( void)7 {8 intinteger1; /* first number to be input by user */9 intinteger2; /* second number to be input by user */10 intsum; /* variable in which sum will be stored */11
12 printf( Enter first integer\n ); /* prompt */13 scanf( %d , &integer1 ); /* read an integer */14
15 printf( Enter second integer\n ); /* prompt */16 scanf( %d , &integer2 ); /* read an integer */17
18 sum = integer1 + integer2; /* assign total to sum */19
20 printf( Sum is %d\n , sum ); /* print sum */21
22 return0; /* indicate that program ended successfully */2324 } /* end function main */Enter first integer45Enter second integer72Sum is 117
Definitions of variables
scanfobtains a value from the
user and assigns it to integer1
scanfobtains a value from theuser and assigns it to integer2
Assigns a value to sum
7/22/2019 Ece246 Chapter 2 (2)
18/25
More Symbols
& - is called ampersand (address operator in C)
followed by a variable name. When combined a
variable name, it tells scanf the location in memory
at which the variable is located. The value for the
variable is then stored in the location
% - is called conversion specifier, tells scanf or
printf what types of data it is going to read. Printfand scanf treat this symbol as a special character that
begins conversion specifier
7/22/2019 Ece246 Chapter 2 (2)
19/25
Memory Concepts
Variable names correspond to locations in the
computer's memory
Every variable has a name, a type, a size and avalue
Whenever a new value is placed into a variable
(through scanf, for example), it replaces (and
destroys) the previous value
Reading variables from memory does not change
them
7/22/2019 Ece246 Chapter 2 (2)
20/25
Memory location showing thename and value of a variable.
7/22/2019 Ece246 Chapter 2 (2)
21/25
Memory locations after bothvariables are input.
7/22/2019 Ece246 Chapter 2 (2)
22/25
Memory locations after acalculation.
7/22/2019 Ece246 Chapter 2 (2)
23/25
Standard algebraicequality operator orrelational operator
C equality orrelationaloperator
Example ofC condition
Meaning of C condition
Equality operators
== x == y xis equal to y
!= x != y xis not equal to y
Relational operators
> x > y xis greater than y
< x < y xis less than y
>= x >= y xis greater than or equal to y
7/22/2019 Ece246 Chapter 2 (2)
24/25
Exercises:
Fill in the blanks
True/False
Correct the error
7/22/2019 Ece246 Chapter 2 (2)
25/25
References
Burgess, M. (1999), C Programming Tutorial
(K&R version 4)
Deitel, P.J. & Deitel, H.M. (2007), C How To
Program, Pearson International Edition
http://www.space.unibe.ch/comp_doc/c_manu
al/C/CONCEPT/data_types.html
SEE YOU NEXT LESSON
http://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/data_types.htmlhttp://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/data_types.htmlhttp://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/data_types.htmlhttp://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/data_types.htmlhttp://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/data_types.htmlhttp://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/data_types.htmlhttp://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/data_types.htmlhttp://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/data_types.htmlhttp://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/data_types.htmlhttp://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/data_types.htmlhttp://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/data_types.htmlhttp://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/data_types.htmlhttp://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/data_types.htmlhttp://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/data_types.html