Introduction To C Programming - 4
Introduction To C Programming - 4
Objectives
Introduce structured programming
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS
Problem statement
Develop a program that would count and display the number of
students that have passed and the number of students that have
failed from a list of exam results for 10 students. If more than 8
students have passed, display "Bonus to instructor!"
Top-level pseudocode
Analyse exam results and decide if instructor should receive a
bonus
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS
First refinement
Initialise variables
Input the 10 exam grades and count passes and failures
Display a summary of the exam results and decide if instructor
should receive a bonus
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS
Second refinement
Initialise passes to 0
Initialise failures to 0
Initialise student counter to 1
While student counter is less than or equal to 10
Input the next exam result
If the student passed
Add 1 to passes
else
Add 1 to failures
Add 1 to student counter
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS
C code (cont’d. . . )
// process 10 students using counter-controlled loop
while ( student <= 10 ) {
// prompt user for input and obtain value from user
printf( "Enter result for student %d (1=pass;2=fail): ", student);
scanf( "%d", &result );
if ( result == 1 ) { // if result is 1, increment passes
passes = passes + 1;
} // end if
else { // otherwise, increment failures
failures = failures + 1;
} // end else
student = student + 1; // increment student counter
}
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS
C code (cont’d. . . )
Assignment Operators I
Assignment Operators I
Preincrement
Postincrement
Operator is used after the variable (c++ or c--)
Expression executes before the variable is changed
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS
Variable in an expression:
int j;
j = 5;
printf( "%d", ++j );
prints 6
int j;
j = 5;
printf( "%d", j++ );
Print 5
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS
Formatted Input/Ouput I
Formatted Input/Ouput I
Printing integers with field with Field width specifies the minimum
space that the displayed integer should occupy
C code:
printf( "%4d\n", 1 );
printf( "%4d\n", 1234 );
printf( "%4d\n", 12345 );
Output:
1
1234
12345
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS
Formatted Input/Ouput I
Printing floating-point values with field width and precision
Field width specifies the minimum space that the displayed floating
point number should occupy
Precision specifies the number of digits to appear after the decimal
point
C code:
printf( "%.3f\n", 3.8663 );
printf( "%9f\n", 3.8663 );
printf( "%9.3f\n", 3.8663 );
Output:
3.866
3.866300
3.866
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS
Formatted Input/Ouput I
Formatted Input/Ouput I
Today
Structured Program Development III
Program design 3: nested control structures
Assignment, increment and decrement operators
Formatted input/output
Next lecture
Program Control
‘for’ repetition structure