# ECE 246 (INTRODUCTION TO C PROGRAMMING

)
CHAPTER 2: INPUT AND OUTPUT

Learning Outcomes

Able to apply the programming development procedure Able to understand the usage of printf with different data types of variables Able to understand the usage of scanf with different data types of variables

Lesson Outline    Programming development procedure printf scanf .

3. 4. 2. Analyze the problem Develop a solution Code the solution Test and correct the program .Programming development procedure 1.

do:  Extended analysis: obtain additional info about the problem so that you can thoroughly understand what is being asked and how to achieve the result  . If necessary.Procedure 1: Analyze the problem Basic analysis: determine and understand the input items and the desired outputs items to be produced.

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 .

Procedure 3: Code the solution  Code the solution algorithm in C .

Procedure 4: Test & Debug the Program   Select test data If error(s) found. make correction .

write a C program to calculate the resistance of a wire that is 125 feet long.Example The electrical resistance. and a is the crosssectional area of the wire (circular mills). r of a metal wire. in ohms is given by the formula: r = (ml)/a where m is the resistivity of the metal. . m is 10. Using this info. l is the length of the wire (feet). cross sectional area of 500 circular mills. and is copper.4. Resistivity of copper.

l and a .Step 1    Key problem statement: to calculate the resistance of the wire Output: resistance Inputs: m.

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

Step 3 #include <stdio. } .h> Int main() { float resistivity. area. length = 125. resistance = (resistivity * length)/ area. return 0. area = 500. resistance. resistance). printf(“The resistance of the wire (in ohms) is %f \n”. resistivity = 10.4. length.

Step 4   Verify the correctness of the program Try with different set of data and check with manual calculation .

33 ohm and 15 ohm resistors. . the total resistance is the sum of all 56 ohm. In such a circuit.Exercise Write a C program to calculate the total resistance of a series circuit.

. For integer: printf ("Integer = %d". . integer name). float name).").printf      C function for output Used for printing/displaying text For string: printf (". For float printf ("Integer = %f"...some string.

&x). &i. . char ch.scanf    C function for input Gets input from the keyboard Examples of scanf usage: int i. scanf ("%d %c %f". float x. &ch.

2. /* print sum */ return 0. /* prompt */ scanf( "%d". sum ). &integer2 ). /* variable in which sum will be stored */ printf( "Enter first integer\n" ).c Addition program */ #include <stdio.5: fig02_05. /* read an integer */ sum = integer1 + integer2. /* indicate that program ended successfully */ Definitions of variables scanf obtains a value from the user and assigns it to integer1 scanf obtains a value from the user and assigns it to integer2 Assigns a value to sum 23 24 } /* end function main */ Enter first integer 45 Enter second integer 72 Sum is 117 .h> /* function main begins program execution */ int main( void ) { int integer1.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 /* Fig. /* first number to be input by user */ int integer2. /* prompt */ scanf( "%d". /* assign total to sum */ printf( "Sum is %d\n". /* second number to be input by user */ int sum. /* read an integer */ printf( "Enter second integer\n" ). &integer1 ).

More Symbols…   & . 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. it tells scanf the location in memory at which the variable is located.is called ampersand (address operator in C) followed by a variable name. When combined a variable name. Printf and scanf treat this symbol as a special character that begins conversion specifier .

a type. a size and a value 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 .Memory Concepts Variable names correspond to locations in the computer's memory    Every variable has a name.

Memory location showing the name and value of a variable. .

.Memory locations after both variables are input.

.Memory locations after a calculation.

More Operators… Standard algebraic equality operator or relational operator Equality operators   Relational operators   ≥ ≤ > < >= <= x > y x < y x >= y x <= y x is greater than y x is less than y x is greater than or equal to y x is less than or equal to y == != x == y x != y x is equal to y x is not equal to y C equality or relational operator Example of C condition Meaning of C condition .

Exercises:    Fill in the blanks True/False Correct the error .

References    Burgess. P.space. & Deitel. (1999).html SEE YOU NEXT LESSON!  . M. C How To Program.J. C Programming Tutorial (K&R version 4) Deitel.unibe. (2007). H. Pearson International Edition http://www.M.ch/comp_doc/c_manu al/C/CONCEPT/data_types.