computer programming all chapters
DESCRIPTION
TRANSCRIPT
LOGO
Prepaid By: Eng. Ibrahim Elewah
Higher Technological Institute 10th of Ramadan City6th of October Branch
Electrical and Computer Engineering Department
Main Reference
HTI Student Book and “C For Dummies”
by Dan Gookin 2nd Edition
Lecture Notes in
1
Course Contents
Introduction
C Programming Higher Technological Institute2
1
Program Development2
The Essential of C Programs3
Manipulating Data with Operators 4
Reading from and Writing to Standard I/O5
Decision6
Iteration7
Arrays8
C Functions9
Course Contents
Introduction
Higher Technological Institute3
1
Computer Program
Programming Language
Machine Language
Assembly Language
High Level Language
The C Programming Language
Interpreter, Compiler and Assembler
C Programming
Computer Program
A Computer Program is a sequence of instructions written to perform a specified task with a computer. The program has an executable form that the computer can use directly to execute the instructions.
The Executable program in its human-readable form is called thesource code. Computer source code is often written by computerprogrammers. It is written in a programming language.
Source Code may be converted into an executable file ( anexecutable program or a binary) by a compiler and later executed by a central processing unit. Alternatively, computer programs may be executed with the aid of an interpreter, or may be embedded directly into hardware.
Higher Technological Institute4C Programming
Programming Language
A Programming language is an artificial language designed to communicate instructions to a computer.
Most programming language are purely textual; they use sequences of text including words, numbers and punctuation.
The programming languages are divided into three groups:
1. Machine Language
2. Assembly Language
3. High Level Language
Higher Technological Institute5C Programming
Machine Language
Essentially, computers really understand only one language, which consists of zeroes and ones, also known as machine language.
Buto Difficult and take a Long Time to Read and Write
o Difficult to Detect and Correct the Errors
o It is also called Binary CodeC Programming
Assembly Language
Higher Technological Institute7
o It uses some Mnemonic English codes instead of Binary Codes.
o Assembly language programs are easier to Readand Write than machine language programs.
o It Needs an Assembler to translate Mnemonic codes into Machine Language.
ExampleADD 5,9
SUB 7,3
C Programming
Assembly Language
Higher Technological Institute8
o It uses some Mnemonic English codes instead of Binary Codes.
o Assembly language programs are easier to Readand Write than machine language programs.
o It Needs an Assembler to translate Mnemonic codes into Machine Language.
ExampleADD 5,9
SUB 7,3
But in Large ProgramsWriting a program in assemblylanguage can prove extremelyTime-consuming, and complicated.
C Programming
High Level Language
Higher Technological Institute9
Because writing machine- or assembly-language
programs was so Difficult and Confusing, peopledeveloped additional languages that look more likehuman languages
With names such as JAVA, BASIC, and C.
o High Level Language needs Compiler or Interpreter.
C Programming
High Level Language
Saving your programs into library and invoke them in next programs
Higher Technological Institute10
Readability
Maintainability
Portability
Reusability
Programs are easy to Read
Programs are easy to Maintain
Programs are easy to port across Different computer Platforms
C Programming
C Programming Language
1. High Level Language.
2. C allows you to get control of computer Hardware.
3. Many other high level language developed based on C.
4. The American National Standard Institute ANSIdefine the standard for the C programming language.
5. Your HTI Book focuses on the functions defined in ANSI
See Page 3
Higher Technological Institute11C Programming
Interpreter, Compiler and Assembler
Higher Technological Institute12
Interpreter
Compiler
Assembler
High Level
Language
Lower Level Language
Assembly Language
Binary Language
High Level
Language Lower Level Language
Computer
Instruction Binary 0’s and 1’s
Read more Pages 3 and 4
C Programming
Your First C Program
/* This is my First C Program */
#include<stdio.h>
int main ()
{
printf(" Hello World! \n ") ;
return 0 ;
}Higher Technological Institute13C Programming
Comments
/* This is my First C Program */
o The compiler ignore everything between two slashes and asterisks.
o Just to help you document your program.
o C compilers allows to write comments like
Higher Technological Institute14
/*This comment does not increase the size of the executable file
(Binary Code), nor does it affect the performance speed
*/
C Programming
Your First C Program
/* This is my First C Program */
#include<stdio.h>
int main ()
{
printf(" Hello World! \n ") ;
return 0 ;
}Higher Technological Institute15C Programming
Header Files
#include<stdio.h>o This Header Files required by include directive
o Header Files extensions .h means header
o The stdio.h header file numerous prototypes and macros to perform input or output I/O for C Program
Higher Technological Institute16
stdio.h Header File
C Programming
Your First C Program
/* This is my First C Program */
#include<stdio.h>
int main ()
{
printf(" Hello World! \n ") ;
return 0 ;
}Higher Technological Institute17C Programming
Main Function
int main ()
{
return 0 ;
}Higher Technological Institute18
o Very special function
o Each C Program MUST have a main() function
o The main() function syntax as follows
C Programming
Your First C Program
/* This is my First C Program */
#include<stdio.h>
int main ()
{
printf(" Hello World! \n ") ;
return 0 ;
}Higher Technological Institute19C Programming
Printing On Screen
printf(" Hello World! \n ") ;
Higher Technological Institute20
o To print a text or numbers printf is used.
o Syntax printf(“ ”);
o The new line character \n
o To print Hello World! write the following statement
C Programming
Your First C Program
/* This is my First C Program */
#include<stdio.h>
int main ()
{
printf(" Hello World! \n ") ;
return 0 ;
}Higher Technological Institute21C Programming
Higher Technological Institute22
References
C Programming
References
Logic Design A Higher Technological Institute23
Course Contents
Introduction
C Programming Higher Technological Institute24
1
Program Development2
The Essential of C Programs3
Manipulating Data with Operators 4
Reading from and Writing to Standard I/O5
Decision6
Iteration7
Arrays8
C Functions9
Course Contents
Higher Technological Institute25
Program Development2
Phases of Computer Program Development
Problem Definition and Analysis
Algorithms and Flowcharts
Program Coding
Program Execute and Testing
Exercise
C Programming
Problem Definition and Analysis
o The problem which you write a program for should becompletely understood by gathering informationabout it.
o This information is the program input.
o How information is processed to give the requiredoutput.
oExample
Higher Technological Institute26
𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎
C Programming
Problem Definition and Analysis
o The General Quadratic Equation
Higher Technological Institute27
𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎
C Programming
Problem Definition and Analysis
o The General Quadratic Equation
𝒙 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂
Higher Technological Institute28
𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎
C Programming
Problem Definition and Analysis
o The General Quadratic Equation
𝒙 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂
𝒙𝟏 =−𝒃 + 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂𝒂𝒏𝒅 𝒙𝟐 =
−𝒃 − 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂
Higher Technological Institute29
𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎
C Programming
Problem Definition and Analysis
o The General Quadratic Equation
𝒙 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂
𝒙𝟏 =−𝒃 + 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂𝒂𝒏𝒅 𝒙𝟐 =
−𝒃 − 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂
Higher Technological Institute30
𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎
Discriminator
C Programming
Problem Definition and Analysis
o The General Quadratic Equation
𝒙 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂
𝒙𝟏 =−𝒃 + 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂𝒂𝒏𝒅 𝒙𝟐 =
−𝒃 − 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂𝑫 = 𝒃𝟐 − 𝟒𝒂𝒄𝒊𝒇 𝑫 < 𝟎 𝒙𝟏 𝒂𝒏𝒅 𝒙𝟐 𝒂𝒓𝒆 𝑰𝒎𝒂𝒈𝒊𝒏𝒂𝒓𝒚𝒊𝒇 𝑫 > 𝟎 𝒙𝟏 𝒂𝒏𝒅 𝒙𝟐 𝒄𝒂𝒏 𝒃𝒆 𝑪𝒂𝒍𝒄𝒖𝒍𝒂𝒕𝒆𝒅
Higher Technological Institute31
𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎
Discriminator
C Programming
Problem Definition and Analysis
o How information is processed to give the requiredoutput ?
𝒂 , 𝒃 𝒂𝒏𝒅 𝒄 𝒂𝒓𝒆 𝑮𝒊𝒗𝒆𝒏 = 𝑰𝒏𝒑𝒖𝒕𝒔
𝑫 = 𝒃𝟐 − 𝟒𝒂𝒄 Question D > 0 ?
YES 𝒙𝟏 =−𝒃+ 𝒃𝟐−𝟒𝒂𝒄
𝟐𝒂𝒂𝒏𝒅 𝒙𝟐 =
−𝒃− 𝒃𝟐−𝟒𝒂𝒄
𝟐𝒂
NO 𝒙𝟏 𝒂𝒏𝒅 𝒙𝟐 𝒂𝒓𝒆 𝑰𝒎𝒂𝒈𝒊𝒏𝒂𝒓𝒚
Higher Technological Institute32
𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎
C Programming
Algorithms and Flowcharts
Higher Technological Institute33C Programming
Flowchart
Higher Technological Institute34
Preparation
Start, Stop ( Begin , End)
Input , Output
Processing Program Instruction
Decision
Connector
Comment( Instruction that change the program)
C Programming
Flowcharts
Higher Technological Institute35
𝑫 = 𝒃𝟐 − 𝟒𝒂𝒄
𝒙𝟏,𝟐 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂
C Programming
Flowcharts
Higher Technological Institute36
𝑫 = 𝒃𝟐 − 𝟒𝒂𝒄
𝒙𝟏,𝟐 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂
C Programming
Flowcharts
Higher Technological Institute37
𝑫 = 𝒃𝟐 − 𝟒𝒂𝒄
𝒙𝟏,𝟐 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂
C Programming
Flowcharts
Higher Technological Institute38
𝑫 = 𝒃𝟐 − 𝟒𝒂𝒄
𝒙𝟏,𝟐 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂
YES NO
C Programming
Flowcharts
Higher Technological Institute39
𝑫 = 𝒃𝟐 − 𝟒𝒂𝒄
𝒙𝟏,𝟐 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂
YES NO
C Programming
Flowcharts
Higher Technological Institute40
𝑫 = 𝒃𝟐 − 𝟒𝒂𝒄
𝒙𝟏,𝟐 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂
YES NO
C Programming
Flowcharts Examples
Higher Technological Institute41
Example 1
Draw a flow chart to
read two numbers and
compare between both
if they are equal or not.
C Programming
Flowcharts Examples
Higher Technological Institute42
Example 1
Draw a flow chart to
read two numbers and
compare between both
if they are equal or not.
C Programming
Flowcharts Examples
Higher Technological Institute43
Example 2
Draw a flow chart to generate
a table of squares and cubes
of integers from 1 to 9.
C Programming
Flowcharts Examples
Higher Technological Institute44
Example 2
Draw a flow chart to generate
a table of squares and cubes
of integers from 1 to 9.
C Programming
Flowcharts Examples
Higher Technological Institute45
Example 3
Draw a flow chart to
determine the minimum
value of three input
numbers.
C Programming
Flowcharts Examples
Higher Technological Institute46
Example 3
Draw a flow chart to
determine the minimum
value of three input
numbers.
C Programming
Flowcharts Examples
Higher Technological Institute47
Example 4
Draw a flow chart to generates the famous sequence
of numbers called the Fibonacci series.
Here are the first few terms of the series:
0 1 1 2 3 5 8 13 21 34 55
C Programming
Flowcharts Examples
Higher Technological Institute48
Example 4
Draw a flow chart to generates the famous sequence
of numbers called the Fibonacci series.
Here are the first few terms of the series:
0 1 1 2 3 5 8 13 21 34 55
Each term is found by adding the two previous
ones:
1+1 is 2, 1+2 is 3, 2+3 is 5, 3+5 is 8, and so on.
C Programming
Flowcharts Examples
Higher Technological Institute49
Example 4
Draw a flow chart to generates the famous sequence
of numbers called the Fibonacci series.
Here are the first few terms of the series:
0 1 1 2 3 5 8 13 21 34 55
Each term is found by adding the two previous
ones:
1+1 is 2, 1+2 is 3, 2+3 is 5, 3+5 is 8, and so on.
𝒇𝟎 = 𝟎 , 𝒇𝟏 = 𝟏 𝒂𝒏𝒅 𝒇𝒏 = 𝒇𝒏−𝟏 + 𝒇𝒏−𝟐
Terminate the program after calculation the first 10
numbers of the Fibonacci series and print them.
C Programming
Flowcharts Examples
Higher Technological Institute50
Example 4
𝒇𝟎 = 𝟎𝒇𝟏 = 𝟏𝒇𝒏 = 𝒇𝒏−𝟏 + 𝒇𝒏−𝟐
0
1
1
2
3
5
8
13
21
34
55
C Programming
Flowcharts Examples
Higher Technological Institute51
Example 5
It is desired to compute income Tax for a given
income as per the following table:
Income Amount of tax
Up to Rs 35000 Nil
From Rs 35001
To Rs 60000
20% of income
in Excess of Rs 35000
From Rs 60001
To Rs 120000
Rs 5000 + 30% of income in Excess of Rs 60000
More than Rs 120000Rs 23000+ 40% of income in
Excess of Rs 120000
C Programming
Flowcharts Examples
Higher Technological Institute52C Programming
Flowcharts Examples
Higher Technological Institute53
Example 6
It is desired to sum 20 values read from a data
statement. This can be done by means of a loop. It
uses the following algorithm for summation.
Sj+1 = Sj + A
Where: j = 1,2,3,..20Sj+1 : The new sum
Sj : The old sum
A : The variable whose values are to be added
C Programming
Flowcharts Examples
Higher Technological Institute54C Programming
Flowcharts Examples
Higher Technological Institute55
Example 7
Draw a flowchart of Program
that Calculate and Display
The Factorial of n
C Programming
Flowcharts Examples
Higher Technological Institute56
Example 7
Draw a flowchart of Program
that Calculate and Display
The Factorial of n𝒏! = 𝒏 × 𝒏 − 𝟏 × 𝒏 − 𝟐 … × 𝟑 × 𝟐 × 𝟏
C Programming
Flowcharts Examples
Higher Technological Institute57
Example 7
Draw a flowchart of Program
that Calculate and Display
The Factorial of n
𝒏! = 𝒏 × 𝒏 − 𝟏 × 𝒏 − 𝟐 … × 𝟑 × 𝟐 × 𝟏
C Programming
Flowcharts Examples
C Programming Higher Technological Institute58
Example 8
Draw a flowchart to compute all possible products of
X and Y
X and Y varying from 0 through 9
(100 products)
Home Work ..!
0x0=0 1x0=0 … 9x0=0
0x1=0 1x1=1 … 9x1=9
0x2=0 1x2=2 … 9x2=18
0x3=0 1x3=3 … 9x3=27
. .
. .
0x9=0 1x9=9 … 9x9=81
Flowcharts Examples
C Programming Higher Technological Institute59
Example 8
0x0=0 1x0=0 … 9x0=0
0x1=0 1x1=1 … 9x1=9
0x2=0 1x2=2 … 9x2=18
0x3=0 1x3=3 … 9x3=27
. .
. .
0x9=0 1x9=9 … 9x9=81
Algorithms
C Programming Higher Technological Institute60
Program Coding
o In this phase, the algorithm ( or Flowchart ) is transferred into program using codes of one of programming language.
o Machine Language ( Binary Code ).
o Assembly Language.
o High Level Language.
C Programming Higher Technological Institute61
Program Executing and Testing
C Programming Higher Technological Institute62
Editing
Compiling
Linking
Executing
Program Executing and Testing
Means to use any editingprogram to convert the program intoelectronic form and save it with asuitable and valid name with theextension (.c)
The program becomes an electronic source code
C Programming Higher Technological Institute63
Editing
Program Executing and Testing
Means to use a compilerprogram matches with theprogramming language the sourceprogram written with, to detect thelinguistic errors and convert thesource code program into a binarycodes.
The program becomes an objectprogram, saved with the source codename but with the extension (.0).
C Programming Higher Technological Institute64
Compiling
Program Executing and Testing
The program when it is writtencontains some functions, as printf( )function which is saved in the library ofthe compiler and may be someexternal functions written to do certainjobs by the program after linking withlibraries functions is converted to anexecutable program, with the samename entered by the user but with theextension .exe (dot exe).
C Programming Higher Technological Institute65
Linking
Program Executing and Testing
The program in this step i.e.,after linking is ready to be run ordebugged on the computer.
C Programming Higher Technological Institute66
Executing
Program Executing and Testing
C Programming Higher Technological Institute67
Editing
Compiling
Linking
Executing
Steps of Obtaining an Executable Program
C Programming Higher Technological Institute68
Course Contents
Introduction
C Programming Higher Technological Institute69
1
Program Development2
The Essential of C Programs3
Manipulating Data with Operators 4
Reading from and Writing to Standard I/O5
Decision6
Iteration7
Arrays8
C Functions9
Course Contents
C Programming Higher Technological Institute70
The Essential of C Programs3
Constants and variables
Expressions
Arithmetic operators
Statements
Statement blocks
Data Types and Names in C
Naming a Variable
Expressions
o An expression is a combination of constants,variables, and operators that are used todenote computations
1. Taking the value contained in the drawer(variable) A
2. Multiply this value by 2
3. Subtract 1 from result obtained from 2
4. The value contained in drawer A is omitted, thenputting the result obtained from 3 into drawer A.
C Programming Higher Technological Institute71
𝑨 = 𝟐 ∗ 𝑨 − 𝟏
Arithmetic Operations
C Programming Higher Technological Institute72
Addition
DivisionMultiplication
Subtraction
Reminder
The Reminder operator % is used to obtainthe reminder of the first operand divided bythe second operand
Arithmetic Operations
C Programming Higher Technological Institute73
Addition
DivisionMultiplication
Subtraction
Reminder
𝟔 % 𝟒 = 𝟐 𝟏𝟎𝟎𝟏%𝟐 = 𝟏 𝟒𝟖%𝟓 =
Example
Arithmetic Operations
C Programming Higher Technological Institute74
Addition
DivisionMultiplication
Subtraction
Reminder
𝟔 % 𝟒 = 𝟐 𝟏𝟎𝟎𝟏%𝟐 = 𝟏 𝟒𝟖%𝟓 = 𝟑
Example
Constants and Variables
o As its name implies, a constant is a value that neverchanges.
o A variable, on the other hand, can be used to presentdifferent values.
o For instance, consider the following:
𝒊 = 𝟏 ;o Where the symbol i is a constant because it always has
the same value (1) and the symbol i is assigned theconstant 1.
o In other words, i contains the value of 1 after thestatement is executed.
C Programming Higher Technological Institute75
Data Types and Names
oThe C language reserves some keywordswords that have special meanings to thelanguage.
oThose reserved words should not be usedas variables, constants, or function namesin your program.
oAll C keywords must be written inlowercase letters, for instance INT will notbe treated as a keyword, it must be writtenas int.C Programming Higher Technological Institute76
The computer list of C keywords
auto break case char
const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while
C Programming Higher Technological Institute77
Naming a Variable
o Characters A through Z and a through z
o Digit characters 0 through 9, which can be used in any position except the first of a variable name.
o The underscore character _
Examplesstop_sign loop3 and_pause
C Programming Higher Technological Institute78
Valid Variable Name Can Use
Naming a Variable
o A variable name can’t contain any C arithmetic signs.
o A variable name can’t contain any dots.
o A variable name can’t contain any apostrophes.
o A variable name can’t contain any other special symbols such as *, @, #, and so on.
Examples4flags sum-result method*4
return what_size? ahmed.ali
C Programming Higher Technological Institute79
Invalid Variable Name Can NOT be Used
Data Types
C Programming Higher Technological Institute80
C Data Type
char
a, B, $, #
int
5, 17, 128
float
2.5 , 0.3
double
23433.3455
Declaration Statement
C Programming Higher Technological Institute81
Typechar
int
float
double
Namec1
N1
F1
d1
Value‘&’
100
32/10
5e3
char c1 = ‘&’ ;
int n1 = 100 ;
Declaration Statement
C Programming Higher Technological Institute82
Typechar
int
float
double
Namec1
N1
F1
d1
Value‘&’
100
32/10
5e3
char c1 ;
int n1 ;
c1 = ‘&’ ;
n1 = 100 ;
Declaration Statement
C Programming Higher Technological Institute83
Typechar
int
float
double
Namec1
n1
f1
d1
Value‘&’
100
32/10
5e3
float f1= 32/100 ;
double d1=5e3 ;
Declaration Statement
C Programming Higher Technological Institute84
Typechar
int
float
double
Namec1
n1
f1
d1
Value‘&’
100
32/10
5e3
float f1 ;
double d1 ;
f1 = 32/100 ;
d1 = 5e3 ;
Some special characters in C
\b BackspaceMoves the cursor to the left one character
\f Form feedGoes to the top of a new page
\n New lineCarriage return and line feeds
\r ReturnReturns to the beginning of the current line
\t TabAdvances to the next tab stop
C Programming Higher Technological Institute85
Examples
/* Example1 : Printing out characters */
# include <stdio.h>
/* the header file for the printf () function */
main ( )
{ /* the main function body till line 13 */
char c1; /* declaration of the character variable c1 */
char c2; /* declaration of the character variable c2 */
c1 = ‘A’; /* assigning c1 with the character constant A */
c2 = ‘a’; /* assigning c2 with the character constant a */
return 0;
}
C Programming Higher Technological Institute86
Examples /* Example1 : Printing out characters */
# include <stdio.h>
/* the header file for the printf () function */
main ( )
{ /* the main function body till line 13 */
char c1; /* declaration of the character variable c1 */
char c2; /* declaration of the character variable c2 */
c1 = ‘A’; /* assigning c1 with the character constant A */
c2 = ‘a’; /* assigning c2 with the character constant a */
printf ( “ The character c1 is : %c \n ”, c1);
printf ( “ The character c1 is : %c ” , c1);
printf ( “ while the character c2 is : %c \n”, c2);
return 0;
}
C Programming Higher Technological Institute87
Examples /* Example1 : Printing out characters */
# include <stdio.h>
/* the header file for the printf () function */
main ( )
{ /* the main function body till line 13 */
char c1; /* declaration of the character variable c1 */
char c2; /* declaration of the character variable c2 */
c1 = ‘A’; /* assigning c1 with the character constant A */
c2 = ‘a’; /* assigning c2 with the character constant a */
printf ( “ The character c1 is : %c \n ”, c1);
printf ( “ The character c1 is : %c ” , c1);
printf ( “ while the character c2 is : %c \n”, c2);
return 0;
}
C Programming Higher Technological Institute88
printf ( “ The character c1 is : %c \n ”, c1);
Examples /* Example1 : Printing out characters */
# include <stdio.h>
/* the header file for the printf () function */
main ( )
{ /* the main function body till line 13 */
char c1; /* declaration of the character variable c1 */
char c2; /* declaration of the character variable c2 */
c1 = ‘A’; /* assigning c1 with the character constant A */
c2 = ‘a’; /* assigning c2 with the character constant a */
printf ( “ The character c1 is : %c \n ”, c1);
printf ( “ The character c1 is : %c ” , c1);
printf ( “ while the character c2 is : %c \n”, c2);
return 0;
}
C Programming Higher Technological Institute89
printf ( “ The character c1 is : %c \n ”, c1);
Examples /* Example1 : Printing out characters */
# include <stdio.h>
/* the header file for the printf () function */
main ( )
{ /* the main function body till line 13 */
char c1; /* declaration of the character variable c1 */
char c2; /* declaration of the character variable c2 */
c1 = ‘A’; /* assigning c1 with the character constant A */
c2 = ‘a’; /* assigning c2 with the character constant a */
printf ( “ The character c1 is : %c \n ”, c1);
printf ( “ The character c1 is : %c ” , c1);
printf ( “ while the character c2 is : %c \n”, c2);
return 0;
}
C Programming Higher Technological Institute90
printf ( “ The character c1 is : %c \n ”, c1);
Examples /* Example1 : Printing out characters */
# include <stdio.h>
/* the header file for the printf () function */
main ( )
{ /* the main function body till line 13 */
char c1; /* declaration of the character variable c1 */
char c2; /* declaration of the character variable c2 */
c1 = ‘A’; /* assigning c1 with the character constant A */
c2 = ‘a’; /* assigning c2 with the character constant a */
printf ( “ The character c1 is : %c \n ”, c1);
printf ( “ The character c1 is : %c ” , c1);
printf ( “ while the character c2 is : %c \n”, c2);
return 0;
}
C Programming Higher Technological Institute91
printf ( “ The character c1 is : %c \n ”, c1);
New Line
Specifier
for
Character Data Type
Examples /* Example1 : Printing out characters */
# include <stdio.h>
/* the header file for the printf () function */
main ( )
{ /* the main function body till line 13 */
char c1; /* declaration of the character variable c1 */
char c2; /* declaration of the character variable c2 */
c1 = ‘A’; /* assigning c1 with the character constant A */
c2 = ‘a’; /* assigning c2 with the character constant a */
printf ( “ The character c1 is : %c \n ”, c1);
printf ( “ The character c1 is : %c ” , c1);
printf ( “ while the character c2 is : %c \n”, c2);
return 0;
}
C Programming Higher Technological Institute92
You Have To Know about Data Types
C Programming Higher Technological Institute93
Type
char
int
float
double
Name
c1
n1
f1
d1
Value
‘&’
100
32/10
5e3
Specifier
%c
%d
%f
%e,%E
Examples /* The arithmetic operations on integers */
# include<stdio.h>
/* the header file for the printf () function */
main ( )
{ /* the main function body till line 15 */
int m = 3;
int n=2;
printf ( "The summation of %d and %d is : %d.\n", m, n, m+n);
printf ( "The difference between %d and %d is : %d.\n", m, n, m-n);
printf ( "The multiplication of %d by %d is : %d.\n", m, n, m*n);
printf ( "The division of %d by %d is : %d.\n", m, n, m/n);
printf ( "The remainder of division of %d by %d is : %d.\n", m, n, m%n);
return 0 ;
}
C Programming Higher Technological Institute94
Operators Precedence
Operator Sign Name
( ) Brackets
/ Division
* Multiplication
+ Addition
- Subtraction
C Programming Higher Technological Institute95
Logic Operators
C Programming Higher Technological Institute96
Operator Sign Name
| | OR
&& AND
!= NOT
Examples /* Example3 : Integer vs. floating point divisions */
# include <stdio.h> /* the header file for the printf ()
function */
main ( )
{
int n1,n2,n3;
float m1,m2,m3;
n1 = 32/10; m1= 32/10;
n2 = 32.0/10; m2= 32.0/10;
n3 = 32/10.0; m3 = 32/10.0;
return 0;
}
C Programming Higher Technological Institute97
Examples /* Example3 : Integer vs. floating point divisions */
# include <stdio.h> /* the header file for the printf () function */
main ( )
{
int n1,n2,n3; float m1,m2,m3;
n1 = 32/10; m1= 32/10;
n2 = 32.0/10; m2= 32.0/10;
n3 = 32/10.0; m3 = 32/10.0;
printf ( “ The integer division of 32/10 is : %d \n”, n1);
printf ( “The floating point division of 32/10 is : %f \n”, m1);
printf ( “The integer division of 32.0/10 is : %d \n”, n2);
printf ( “The floating point division of 32.0/10 is : %f \n”, m2);
printf ( “The integer division of 32/10.0 is : %d \n”, n3);
printf ( “The floating point division of 32/10.0 is : %f \n”, m3);
return 0;
}C Programming Higher Technological Institute98
Double Data Type
o Here are two examples:
[mantissa] e [exponent]
[mantissa] E [exponent]
Example
5000 5e3.
-300 -3e2
0.0025 2.5e-3.
C Programming Higher Technological Institute99
Specifier %e or %E
With printf ( )
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;
X = E * D – B / C + A ;
Y = ( E * D ) – ( B / C ) + A ;
Z = A * ( D – B ) / ( C + E ) ;
printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);
return 0 ;
}
C Programming Higher Technological Institute100
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;
X = E * D – B / C + A ;
Y = ( E * D ) – ( B / C ) + A ;
Z = A * ( D – B ) / ( C + E ) ;
printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);
return 0 ;
}
C Programming Higher Technological Institute101
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;
X = E * D – B / C + A ;
Y = ( E * D ) – ( B / C ) + A ;
Z = A * ( D – B ) / ( C + E ) ;
printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);
return 0 ;
}
C Programming Higher Technological Institute102
X = E * D – B / C + A ;
Precedence Example
X = E * D – B / C + A ;
C Programming Higher Technological Institute103
X = E * D – B / C + A ;
Precedence Example
C Programming Higher Technological Institute104
X = E * D – B / C + A ;
Precedence Example
C Programming Higher Technological Institute105
X = E * D – B / C + A ;
A = 20
B = 6
C = 3
D = 10
E = 2
( )
*
/
+ -
Precedence Example
C Programming Higher Technological Institute106
X = E * D – B / C + A ;
2 * 10 – 6 / 3 + 20 A = 20
B = 6
C = 3
D = 10
E = 2
( )
*
/
+ -
Precedence Example
C Programming Higher Technological Institute107
X = E * D – B / C + A ;
2 * 10 – 6 / 3 + 20
20 – 2 + 20
A = 20
B = 6
C = 3
D = 10
E = 2
( )
*
/
+ -
Precedence Example
C Programming Higher Technological Institute108
X = E * D – B / C + A ;
2 * 10 – 6 / 3 + 20
20 – 2 + 20
38
A = 20
B = 6
C = 3
D = 10
E = 2
( )
*
/
+ -
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;
X = E * D – B / C + A ;
Y = ( E * D ) – ( B / C ) + A ;
Z = A * ( D – B ) / ( C + E ) ;
printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);
return 0 ;
}
C Programming Higher Technological Institute109
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;
X = E * D – B / C + A ;
Y = ( E * D ) – ( B / C ) + A ;
Z = A * ( D – B ) / ( C + E ) ;
printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);
return 0 ;
}
C Programming Higher Technological Institute110
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;
X = E * D – B / C + A ;
Y = ( E * D ) – ( B / C ) + A ;
Z = A * ( D – B ) / ( C + E ) ;
printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);
return 0 ;
}
C Programming Higher Technological Institute111
Y = ( E * D ) – ( B / C ) + A ;
Precedence Example
.
.
.
C Programming Higher Technological Institute112
Y = ( E * D ) – ( B / C ) + A ;
Precedence Example
.
.
.
38
C Programming Higher Technological Institute113
Y = ( E * D ) – ( B / C ) + A ;
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;
X = E * D – B / C + A ;
Y = ( E * D ) – ( B / C ) + A ;
Z = A * ( D – B ) / ( C + E ) ;
printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);
return 0 ;
}
C Programming Higher Technological Institute114
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;
X = E * D – B / C + A ;
Y = ( E * D ) – ( B / C ) + A ;
Z = A * ( D – B ) / ( C + E ) ;
printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);
return 0 ;
}
C Programming Higher Technological Institute115
Z = A * ( D – B ) / ( C + E ) ;
Precedence Example
C Programming Higher Technological Institute116
Z = A * ( D – B ) / ( C + E ) ;
A = 20
B = 6
C = 3
D = 10
E = 2
( )
*
/
+ -
Precedence Example
C Programming Higher Technological Institute117
Z = A * ( D – B ) / ( C + E ) ;
A = 20
B = 6
C = 3
D = 10
E = 2
( )
*
/
+ -
Precedence Example
C Programming Higher Technological Institute118
Z = A * ( D – B ) / ( C + E ) ;
20 * 4 / 5A = 20
B = 6
C = 3
D = 10
E = 2
( )
*
/
+ -
Precedence Example
C Programming Higher Technological Institute119
Z = A * ( D – B ) / ( C + E ) ;
20 * 4 / 5
16
A = 20
B = 6
C = 3
D = 10
E = 2
( )
*
/
+ -
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;
X = E * D – B / C + A ;
Y = ( E * D ) – ( B / C ) + A ;
Z = A * ( D – B ) / ( C + E ) ;
printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);
return 0 ;
}
C Programming Higher Technological Institute120
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;
X = E * D – B / C + A ;
Y = ( E * D ) – ( B / C ) + A ;
Z = A * ( D – B ) / ( C + E ) ;
printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);
return 0 ;
}
C Programming Higher Technological Institute121
Home Work ..!!Exercises 3 Page 44
C Programming Higher Technological Institute122
Course Contents
Introduction
C Programming Higher Technological Institute123
1
Program Development2
The Essential of C Programs3
Manipulating Data with Operators 4
Reading from and Writing to Standard I/O5
Decision6
Iteration7
Arrays8
C Functions9
Course Contents
C Programming Higher Technological Institute124
Manipulating Data with Operators 4
Course Contents
C Programming Higher Technological Institute125
Errors Types
Arithmetic Assignment Operators
Unary Minus Operator
The Cast Operator
Manipulating Data with Operators 4
Error Types
C Programming Higher Technological Institute126
Syntax
Execution
Logic
Error Types
They are errors that youhave made in the form
Or syntax of the language
o Spelling a keyword incorrectly
o Errors that are detectedduring running of a programare shown in a dialog box thatappears and the error ishighlighted in the program.
C Programming Higher Technological Institute127
Syntax
Example
Error Types
If the program has no syntax errors. The computer can execute it.
During execution, execution errors may be detected.
o If a number is divided by zerocauses an execution error.
Whenever an execution erroris detected, the computer displays adialog box with an error messageand stops executing the program.
C Programming Higher Technological Institute128
Execution
Example
Error Types
If the output of the programdoes not agree with what isexpected, this is logic error.
The computer cannot detectsuch an error because it does notknow the logic of the programshould be.
So, it is your responsibility todetect logic errors in the program.
C Programming Higher Technological Institute129
Logic
Error Types
C Programming Higher Technological Institute130
Syntax
Execution
Logic
Arithmetic Assignment Operators
o Here the statement causes the value of the right-hand-operand to be assigned to the memory location of the left-hand-operand.
o Statement writes the value of the right-hand-operand (5) into the memory location of the integer variable a (which is the left-hand-operand in this case).
C Programming Higher Technological Institute131
left-hand-operand = right-hand-operand
a=5 ;
b=a=5 ;
Assignment and Arithmetic Operators
Operator Description
+ = Addition Assignment Operator
- = Subtraction Assignment Operator
* = Multiplication Assignment Operator
/ = Division Assignment Operator
% = Remainder Assignment Operator
C Programming Higher Technological Institute132
Equivalence of Statements
Statement Equivalence
x+ = y; x = x + y;
x- = y; x = x - y;
x* = y; x = x * y;
x/ = y; x = x / y;
x% = y; x = x % y;
C Programming Higher Technological Institute133
z = z * x + y ; z *= x + y ; ?
Equivalence of Statements
Statement Equivalence
x+ = y; x = x + y;
x- = y; x = x - y;
x* = y; x = x * y;
x/ = y; x = x / y;
x% = y; x = x % y;
C Programming Higher Technological Institute134
z = z * x + y ; z *= x + y ; ≠
Example# include <stdio.h> main ( ){ int x, y, z; x =1; y =3; z= 10;printf (“Given x = %d, y = %d, and z = %d,\n”, x, y, z);x= x + y;printf ( “ x= x +y assigns %d to x;\n”, x);x = 1; x+= y ; printf ( “ x+= y assigns %d to x;\n”, x);x = 1; z = z* x + y;printf ( “z = z*x+y assigns %d to z;\n”, z); z = 10; z = z* (x + y);printf ( “z = z*( x+ y) assigns %d to z;\n”, z);z = 10; z *= x + y;printf ( “z *= x+ y assigns %d to z;\n”, z);return 0; }
C Programming Higher Technological Institute135
Unary Minus Operator
o Given an integer, you can get its negation by changing the sign of the integer by using, -, the minus operator, which is called the unary minus operator.
o Differ between the unary minus operator and the subtraction operator.
o The first – symbol is used as the subtraction operator while the second –symbol is the unary minus operator.
C Programming Higher Technological Institute136
x = 1.234 ; -x equals -1.234
z = x- -y; OR z = x- (-y);
Incrementing or Decrementing by ONE
C Programming Higher Technological Institute137
X++ ++X
X-- --X
IncrementP
ost-
Decrement
Pre
-
Example# include <stdio.h> main ( ){int x=5; printf (“ X++ %d ,\n”, x++);printf (“++X %d ,\n”, ++x);printf (“ X-- %d ,\n”, x--);printf (“ --X %d ,\n”, --x);return 0; }
C Programming Higher Technological Institute138
Relational Operators
Operator Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
C Programming Higher Technological Institute139
oAll relational expressions produce a result of either 0 or 1.
oGiven x = 3 and y = 5, for instance, the relational expression x < y gives a result of 1
C Programming Higher Technological Institute140
x * y < z + 3 (x * y) < ( z + 3 )
Relational Operators
Example# include <stdio.h> main ( ){ int x, y;
double z;
x = 7; y = 25; z = 24.46;
printf (“ Given x = %d, y = %d and z = %2f, \n”, x, y, z);printf ( “ x > = y produces: %d \n”, x > =y);printf ( “x = = y produces: %d \n”, x = =y);printf ( “x < y produces: %d \n”, x < y); printf ( “x > y produces: %d \n”, x > y);printf ( “x != y - 18 produces: %d \n”, x ! =y -18); printf ( “x + y ! = z produces: %d \n”, x +y ! = z); return 0; }
C Programming Higher Technological Institute141
This is algebraically True and is supposed to return 1.
o However, the expression returns 0 which means that the equal to relationship does not hold.
o This is because the truncation of the integer division – that is , 1 /2 – produces 0 not 0.5.
C Programming Higher Technological Institute142
1 / 2 + 1 / 2 = = 1 1 Or 0
Relational Operators
The Cast Operator
o You can convert one data type to a different oneby prefixing and cast operator to the operand.
o The general form of the cast operator is
o Here data-type specifies the data type you wantto convert to x is a variable ( or, expression)that contains the value of the current data type.
o You have to include the parentheses (and) tomake up a cast operator.
C Programming Higher Technological Institute143
(data-type) x
Example# include <stdio.h> main ( ){int x, y;
x = 7;
y = 5;
printf ( “ Given x = %d, y = %d, \n”, x, y);
printf ( “ x / y produces: %d \n”, x /y);
printf ( “(float) x / y produces: %f \n”, (float) x / y);
return 0 ;
}
C Programming Higher Technological Institute144
Course Contents
Introduction
C Programming Higher Technological Institute145
1
Program Development2
The Essential of C Programs3
Manipulating Data with Operators 4
Reading from and Writing to Standard I/O5
Decision6
Iteration7
Arrays8
C Functions9
Course Contents
C Programming Higher Technological Institute146
Reading from and Writing to Standard I/O5
scanf ( ) functiono The scanf ( ) function is a very important function in C.
o It is used to read data from the standard input device; the keyboard.
o The syntax for the scanf ( ) function is :
o Example : to input two variables integer x and float y
C Programming Higher Technological Institute147
scanf(“ % % ”, & & );
scanf(“ %d %f ”, &x &y );
scanf ( ) functiono The scanf ( ) function is a very important function in C.
o It is used to read data from the standard input device; the keyboard.
o One thing you need to be aware of; is that the scanf ( ) function doesn’t actually start reading the input untilthe Enter key is pressed.
o Data entered from the keyboard is placed in an inputbuffer.
o When the Enter key is pressed, the scanf () function looks for its input in the buffer.
C Programming Higher Technological Institute148
printf( ) functiono The printf ( ) function is a very important function in C.
o It is used to print out messages on the screen.
o The syntax for the printf ( ) function is
o Example : to print two variables integer x and float y
C Programming Higher Technological Institute149
printf(“ % % ”, );
printf(“ %d %f ”, x y );
printf( ) functiono The printf ( ) function is a very important function in C.
o It is used to print out messages on the screen.
o The number of expressions is determined by thenumber of the format specifiers inside the firstargument.
o The format specifiers and the expressions arematched in order from left to right, and you shoulduse exactly the same number of expressions as thenumber of format specifiers within the format string.
C Programming Higher Technological Institute150
Adding the Minimum Field Width
o A integer is added between the percent sign (%) and the letter in a format specifier to specify the minimum field width and ensures that the output reaches the minimum width.
o For example in %10f
10 is a minimum field width specifier thatensures that the output is at least 10 characterspacers wide.
C Programming Higher Technological Institute151
printf(“ %10f ”, x );
Example
C Programming Higher Technological Institute152
# include <stdio.h>
main ( )
{
int x=12; int y=12345;
printf (“%d \n”, x);
printf (“%d \n”, y);
printf (“%5d \n”, x);
printf (“%05d \n”, x);
printf (“%2d \n”, y);
return 0;
}
Example
C Programming Higher Technological Institute153
# include <stdio.h>
main ( )
{
int x=12; int y=12345;
printf (“%d \n”, x);
printf (“%d \n”, y);
printf (“%5d \n”, x);
printf (“%05d \n”, x);
printf (“%2d \n”, y);
return 0;
}
12
12345
12
00012
12345
Aligning Output
o By default, all output is placed on the Right edge ofthe field, as long as the field width is longer than thewidth of the output.
o You can change this and force output to be left-justified.
o To do so, you need to prefix the minimum fieldspecifier with the minus sign (-).
o Specifies the minimum field width as 12, and justifiesthe output from the left edge of the field.
C Programming Higher Technological Institute154
%-12d
Example
C Programming Higher Technological Institute155
# include <stdio.h>
main ( )
{
int x,y,z,m,n; x=1; y=12; z=123;
m=1234; n=12345;
printf (“%8d %-8d \n”, x, x );
printf (“%8d %-8d \n”, y, y );
printf (“%8d %-8d \n”, z, z );
printf (“%8d %-8d \n”, m, m );
printf (“%8d %-8d \n”, n, n );
return 0;
}
Example
C Programming Higher Technological Institute156
# include <stdio.h>
main ( )
{
int x,y,z,m,n; x=1; y=12; z=123;
m=1234; n=12345;
printf (“%8d %-8d \n”, x, x );
printf (“%8d %-8d \n”, y, y );
printf (“%8d %-8d \n”, z, z );
printf (“%8d %-8d \n”, m, m );
printf (“%8d %-8d \n”, n, n );
return 0;
}1 1
12 12
123 123
1234 1234
12345 12345
The Precision Specifier
o You can put a period (.) and an integer right after theminimum field width specifier.
o The combination of the period and the integer makeup a precision specifier.
o The precision specifier is another important specifieryou can use to determine the number of decimalplaces for floating-point numbers, or to specify themaximum field width for integers or strings.
o The minimum field width length is specified as 10character long, and the number of decimal places isset to 3.
C Programming Higher Technological Institute157
%10.3f
The Precision Specifier
o You can put a period (.) and an integer right after theminimum field width specifier.
o The combination of the period and the integer makeup a precision specifier.
o The precision specifier is another important specifieryou can use to determine the number of decimalplaces for floating-point numbers, or to specify themaximum field width for integers or strings.
o Remember, the default number of decimal places is 6.
C Programming Higher Technological Institute158
Example
C Programming Higher Technological Institute159
# include <stdio.h>
main ( )
{
int x=123; float y=123.456789;
printf (“Default integer format \n %d \n”, x );
printf (“With precision specifier \n %2.8d \n”, x );
printf (“Default float format \n %f \n”, y );
printf (“With precision specifier \n %10.2f \n”, y );
printf (“With precision specifier \n %-10.2f \n”, y );
return 0;
}
Example
C Programming Higher Technological Institute160
# include <stdio.h>
main ( )
{
int x=123; float y=123.456789;
printf (“Default integer format \n %d \n”, x );
printf (“With precision specifier \n %2.8d \n”, x );
printf (“Default float format \n %f \n”, y );
printf (“With precision specifier \n %10.2f \n”, y );
printf (“With precision specifier \n %-10.2f \n”, y );
return 0;
}
Course Contents
Introduction
C Programming Higher Technological Institute161
1
Program Development2
The Essential of C Programs3
Manipulating Data with Operators 4
Reading from and Writing to Standard I/O5
Decision6
Iteration7
Arrays8
C Functions9
Course Contents
C Programming Higher Technological Institute162
Decision6
Course Contents
C Programming Higher Technological Institute163
The if statement
Flowchart of if statement
The if else statement and flowchart
Nested if statements and their flowcharts
The switch statement
The break and continue statement
The goto statements
Decision6
The if statemento The if statement is the most popular conditional
branching statement.
o It can be used to evaluate the conditions as well as tomake the decision whether the block of codecontrolled by the statement is going to be executed.
o Here if condition is logical TRUE, the statements insidethe braces are executed.
o If condition is logical FALSE, then the statements areskipped.
C Programming Higher Technological Institute164
The if statement Flowchart
C Programming Higher Technological Institute165
…..if ( Test Expression) {statement1;statement2;…}….
IF Statement Syntax
The if statement Flowchart
C Programming Higher Technological Institute166
…if ( Test Expression) statement;…
IF Statement Syntax
Example
if ( x>5) printf( “ X > 5 ” );…
Example
C Programming Higher Technological Institute167
# include <stdio.h>
main ( )
{
int i;
printf (“Integers that can be divided by 3 \n ” );
printf (“Enter a positive number : \n ”, x );
scanf(“%d”, &i );
if ( i %3 == 0 )
printf (“The number %d is divisible by 3 \n ”, i );
return 0;
}
Output
C Programming Higher Technological Institute168
Integers that can be divided by 3Enter a positive number : 12The number 12 is divisible by 3
Integers that can be divided by 3Enter a positive number : 7
The if else statement
C Programming Higher Technological Institute169
…if ( Test Expression) {statement1;statement2;…}else {statement_Astatement_B…}…
if else Statement Syntax
The if else statement
C Programming Higher Technological Institute170
…if ( Test Expression) statement1;elsestatementA;…
if else Statement Syntax
if ( X > 5) printf( “ X > 5 ” );elseprintf( “ X < 5 ” );
Example
if else Example
C Programming Higher Technological Institute171
Draw a flow chart to
read two numbers and
compare between both
if they are equal or not.
C Programming Higher Technological Institute172
# include <stdio.h>
main ( )
{
int x,y;
printf (“Please Enter X and Y \n ” );
scanf(“%d %d”, &x , &y );
if ( x == y )
printf (“ X = Y \n ” );
else
printf (“ X != Y \n ” );
return 0; }
if else Example
Nested if Flowchart
C Programming Higher Technological Institute173
if ( Test Expression1) { if ( Test Expression2)
{ statement1;statement2;
} else { statementA;
statementB;}
}else {statement X ;}
Nest IF Statement Syntax
Nested if Flowchart
C Programming Higher Technological Institute174
Draw a flow chart to
determine the minimum
value of three input
numbers.
C Programming Higher Technological Institute175
# include <stdio.h>
main ( )
{
int A,B,C;
printf (“Please Enter A,B and C \n ” );
scanf(“%d %d %d”, &A , &B , &C );
if ( A<B )
{ if ( A<C )
printf (“ A The is the Smallest \n ” );
else
printf (“ C The is the Smallest \n ” );
} else if ( B<C )
printf (“ B The is the Smallest \n ” );
else
printf (“ C The is the Smallest \n ” );
return 0; }
Nested if Example
The switch statement
C Programming Higher Technological Institute176
o The nested if statements will become very complex ifthere are many decisions that need to be made.
o The switch statement, can be used to make unlimiteddecisions or choices based on the value of a conditionalexpression and specified cases.
o The conditional expression is evaluated first, if thereturn value of the conditional expression is equal tothe constant expression expression 1, the statementstatement 1 is executed.
o the value of the conditional expression is not equal toany values of the constant expressions labeled by thecase keyword, the statement (statement-default)following by the default keyword is executed.
The Switch Statement Syntax
C Programming Higher Technological Institute177
switch ( Conditional Expression ) {
case expression1;statment1;case expression2;statment2;…default;statement-default;
}
The Switch Statement Syntax
C Programming Higher Technological Institute178
switch ( Conditional Expression ) {case expression1;statment1;case expression2;statment2;…default;statement-default; }
o You have to use the case keyword to label each case.
o The default keyword is recommended to be used forthe default case.
o Note that no constant expressions are identical in theswitch statement.
The break statemento An important feature of the switch statement is that
the computer continues to execute the statementsfollowing the selected case until the end of the switchstatement.
o You can add a break statement at the end of thestatement list following every case label, if you wantto exit the switch construct after the statementswithin a selected case are executed
C Programming Higher Technological Institute179
The Break Statement Syntax
C Programming Higher Technological Institute180
switch ( Conditional Expression ) {
case expression1;statment1;break ;case expression2;statment2;break;…default;statement-default;
}
Example
Total Score Letter Grade
85 <= T <= 100 A
80 <= T < 85 B+
75 <= T < 80 B
70 <= T < 75 C+
65 <= T < 70 C
55 <= T < 65 D+
50 <= T < 55 D
T < 50 F
C Programming Higher Technological Institute181
Write Program that compute the grade for a student, the
grade is based on Total Score T
( from 0 to 100) The Grading Scale Is Illustrated In The
Following Table:
# include <stdio.h>
main ( )
{ int score;
printf (" Enter Scorre: \n");
scanf ("%d", &score);
switch (score / 10 ) {
case 10:
printf ("your grade is A \n");
break;
case 9:
printf ("your grade is A \n");
break;
case 8:
printf ("your grade is B \n");
break;
case 7:
printf ("your grade is C \n");
break;
C Programming Higher Technological Institute182
case 6:
printf ("your grade is D \n");
break;
case 5:
printf ("your grade is D \n");
break;
case 4:
printf ("your grade is F \n");
break;
case 3:
printf ("your grade is F \n");
break;
case 2:
printf ("your grade is F \n");
break;
case 1:
printf ("your grade is F \n");
break;
case 0:
printf ("your grade is F \n");
break;
default:
printf ( "illegal input \n");
break;
}
printf ( "GOODBYE\n" );
return 0;
}
Example
The Continue statemento There are times when you want to stay in a loop
but skip over some statements within the loop.o To do this, the continue statement causes
execution to jump to the top of the loopimmediately.
o The statement can be used within a while,do…while or for statement to terminate thecurrent iteration of the loop and begin thenext.
C Programming Higher Technological Institute183
The goto statemento Do not use the goto statement unless it’s absolutely
necessary because its usage may make the C programunreliable and hard to debug.
o The Syntax of goto Statement:
C Programming Higher Technological Institute184
…lablename :
statment1;statment2;..
goto lablename ;…
C Programming Higher Technological Institute185
# include <stdio.h>
main ( )
{
int x;
guess_lbl:
printf (“Guess integer number in the range 1 to 10 \n”);
scanf (“%d”, &x );
if ( x == 7)
goto rght_lbl;
printf (“NO, wrong guess, try again \n”);
goto guess_lbl;
rght_lbl:
printf (“WELL DONE your guess = %d is right\n” , x);
return 0;
}
goto Example
C Programming Higher Technological Institute186
# include <stdio.h>
main ( )
{
int x;
guess_lbl:
printf (“Guess integer number in the range 1 to 10 \n”);
scanf (“%d”, &x );
if ( x == 7) goto rght_lbl;
printf (“NO, wrong guess, try again \n”);
goto guess_lbl;
rght_lbl:
printf (“WELL DONE your guess = %d is right\n” , x);
return 0;
}
goto Example
Course Contents
Introduction
C Programming Higher Technological Institute187
1
Program Development2
The Essential of C Programs3
Manipulating Data with Operators 4
Reading from and Writing to Standard I/O5
Decision6
Iteration7
Arrays8
C Functions9
Course Contents
C Programming Higher Technological Institute188
Iteration 7
Course Contents
C Programming Higher Technological Institute189
The for statement
The Null Statement
The while statement
The infinite while Loop
The do-while Statement
The Nested Loop
Examples
Iteration7
Iterationo Iteration (looping) is used in programming
to perform the same set of statementsover and over until certain specifiedconditions are met.
C Programming Higher Technological Institute190
Repeat the same statement(s) until certain condition
Iteration
C Programming Higher Technological Institute191
Example
Draw a flow chart to generate
a table of squares and cubes
of integers from 1 to 9.
Iteration
C Programming Higher Technological Institute192
Example
Draw a flow chart to generate
a table of squares and cubes
of integers from 1 to 9.
From
1 to 9
Step 1
The for statement
C Programming Higher Technological Institute193
for ( initialization ; Test Expression ; Step) statement;…
for Statement Syntax One Statement
for ( initialization ; Test Expression ; Step) {statement1;statement2;…}
for Statement Syntax Multiple Statements
The for statement
C Programming Higher Technological Institute194
for ( i=1 ; i <= 10 ; i++ )
printf (“ Hello \n ”);
printf (“ Goodbye \n ”);
Will Print Hello 10 Times Then PrintGoodbye
for ( i=1 ; i <= 10 ; i++ )
{
printf (“ Hello \n ”);
printf (“ Goodbye \n ”);
}
Will Print Hello Goodbye10 Times
Example
C Programming Higher Technological Institute195
# include <stdio.h>
main ( )
{
int i=1; int j,k;
for ( i=1 ; i <= 9 ; i++ )
{
j=i*i; k=j*i;
printf (“ i = %d ”, i ) ;
printf (“ j = %d ”, j ) ;
printf (“ k = %d \n”, k ) ;
}
return 0;
}
Null statement
C Programming Higher Technological Institute196
for ( i=1 ; i <= 10 ; i++ ) ;
printf (“ Hello \n ”);
printf (“ Goodbye \n ”);
The while statement
C Programming Higher Technological Institute197
Initializationwhile ( Test Expression ){…Increment Or Decrement ;}
while Statement Syntax
The while statement
C Programming Higher Technological Institute198
int w = 1;
while ( w <= 10 )
{
printf (“ Hello \n ”);
w = w++;
}
printf (“ Goodbye \n ”);
Will Print Hello 10 Times Then PrintGoodbye
While Loop Example
C Programming Higher Technological Institute199
# include <stdio.h>
main ( )
{
int i=1; int j,k;
while (i<=10)
{
j=i*i; k=j*i;
printf (“ i = %d ”, i ) ;
printf (“ j = %d ”, j ) ;
printf (“ k = %d \n”, k ) ;
i=i+1;
}
return 0; }
The Infinite while loop
C Programming Higher Technological Institute200
while(1)
{
Statement1;
Statement2;
}
Always returns 1, the statements inside thestatement block will be executed over andover- that is, the while loop will continueforever.
You can set certain conditions inside the while loopto break the infinite loop as soon as theconditions are met.
The do-while statement
C Programming Higher Technological Institute201
Initializationdo{…Increment Or Decrement ;
} while ( Test Expression ) ;
do-while Statement Syntax
The do-while statement
C Programming Higher Technological Institute202
Initializationdo{…Increment Or Decrement ;
} while ( Test Expression ) ;
do-while Statement Syntax
Only after do-while loop
Don’t forget to write ;
The do-while statement
C Programming Higher Technological Institute203
int w = 1;
do
{
printf (“ Hello \n ”);
w = w++;
} while ( w <= 10 ) ;printf (“ Goodbye \n ”);
Will Print Hello 10 Times Then PrintGoodbye
do-while Loop Example
C Programming Higher Technological Institute204
# include <stdio.h>
main ( )
{
int i=1; int j,k;
do
{
j=i*i; k=j*i;
printf (“ i = %d ”, i ) ;
printf (“ j = %d ”, j ) ;
printf (“ k = %d \n”, k ) ;
i=i+1;
} while (i<=10);
return 0; }
Examples
C Programming Higher Technological Institute205
Example 1
Write a program to calculate the
summation and Average of 20 values
read from a data statement.
Examples
C Programming Higher Technological Institute206
# include <stdio.h>
main ( )
{
int i,x,sum; float av;
sum=0;
for ( i=1 ; i<=20 ; i++ )
{
printf(“Please enter new no \n”);
scanf(“%d”,&x);
sum=sum+x;
}
av=sum/20;
printf(“Sum=%d \n Av=%f\n”, sum , av);
return 0; }
Examples
C Programming Higher Technological Institute207
Example 2
Write a program to calculate the
summation and Average of N numbers
read from a data statement.
Examples
C Programming Higher Technological Institute208
# include <stdio.h>
main ( )
{
int i,n,x,sum; float av;
sum=0;
printf(“How many Numbers ? \n”);
scanf(“%d”,&n);
for ( i=1 ; i<=n ; i++ )
{
printf(“Please enter new no \n”);
scanf(“%d”,&x);
sum=sum+x;
}
av=sum/n;
printf(“Sum=%d \n Av=%f\n”, sum , av);
return 0; }
Flowcharts Examples
C Programming Higher Technological Institute209
Example 3 Nested Loop
Write a program and Draw its flowchart to compute all
possible products of X and Y
X and Y varying from 1 through 9
(100 products)
1x0=0 2x0=0 … 9x0=0
1x1=0 2x1=1 … 9x1=9
1x2=0 2x2=2 … 9x2=18
1x3=0 2x3=3 … 9x3=27
. .
. .
1x9=0 2x9=9 … 9x9=81
Flowcharts Examples
C Programming Higher Technological Institute210
Example 3 Nested Loop
# include <stdio.h>
int main ( )
{
int x,y;
for ( x=1 ; x <= 9 ; x++)
for ( y=1 ; y <= 9 ; y++)
printf(“%d x %d = %d\n”,x,y,x*y);
return 0;
}
Flowcharts Examples
C Programming Higher Technological Institute211
Example 4
Draw a flowchart and write a Program that
Calculate and Display The Factorial of n
𝒏! = 𝒏 × 𝒏 − 𝟏 × 𝒏 − 𝟐 … × 𝟑 × 𝟐 × 𝟏
Flowcharts Examples
C Programming Higher Technological Institute212
# include <stdio.h>
int main ( )
{
int n,fact; fact=1;
printf(“Enter your N \n”);
scanf(“%d”,&n);
for( int i=1;i<=n;i++)
fact=fact*i;
printf(“ N! =%d \n”,fact);
return 0;
}
Home Work..!!
Rewrite The previous examples using:
a) while Loop
b) do-while Loop
C Programming Higher Technological Institute213
Course Contents
Introduction
Higher Technological Institute214
1
Program Development2
The Essential of C Programs3
Manipulating Data with Operators 4
Reading from and Writing to Standard I/O5
Decision6
Iteration7
Arrays8
C Functions9
C Programming
Course Contents
Higher Technological Institute215
Arrays8
C Programming
Examples
Higher Technological Institute216
Example 2
Write a program to calculate the
summation and Average of N numbers
read from a data statement.
C Programming
Examples
Higher Technological Institute217
# include <stdio.h>
main ( )
{
int i,n,x,sum; float av;
sum=0;
printf(“How many Numbers ? \n”);
scanf(“%d”,&n);
for ( i=1 ; i<=n ; i++ )
{
printf(“Please enter new no \n”);
scanf(“%d”,&x);
sum=sum+x;
}
av=sum/n;
printf(“Sum=%d \n Av=%f\n”, sum , av);
return 0; }C Programming
Arrays
Higher Technological Institute218
An array is a collection of variables that are of the samedata type.
Each item in an array is called an element.
All elements in an array are referenced by the name of the array and are stored in a set of consecutive memory slots.
The array takes two forms :
One dimensional array (1-D) ; where the data is arranged horizontally or vertically.
Multi-dimensional array; where the data is arranged in two
dimensional (2-D), three dimensional (3-D),…etc.
C Programming
Arrays
Higher Technological Institute219
The following is the syntax form to declare an array:
The data-type is the type specifier that indicates what data type the declared array will be.
Array-Name is the name of the declared array. When choosing a name for an array, you should follow the same rules of naming a variable.
Array-Size defines how many elements the array can contain. Note that the brackets are required in declaring an array.
data-type Array-Name [Array-Size];
C Programming
Arrays Example
Higher Technological Institute220
Where int specifies the data type of the array whose name is arrat_int.
The size of the array is 8, which means that the array can store 8 elements
int array_int [8];
C Programming
Indexing Arrays
Higher Technological Institute221
You can access each of the elements in the array separately.
The following declaration declares an array called day of integer numbers:
You can access the elements in the array of day one after another: day[0], day[1], …, day[6].
The important thing to remember is that all arrays in C are indexed starting at 0.
The first element in the array of day is day[0].
int day [7];
C Programming
Initializing Arrays
Higher Technological Institute222
You can initialize each element in an array by two way.
For instance if we have the array :
Initialize the first element in the array: like this:
Here the value 24 is assigned to the first element of the array number, number[0].
int number [5];
number[0] = 24;
C Programming
Initializing Arrays
Higher Technological Institute223
The second way to initialize an array is to initializeall elements in the array together.
For instance, the following statement initializes allelements of the array, number:
Here the integers inside the braces are assigned tothe corresponding elements of the array number.
That is, 100 is given to the first element(number[0]), 4 to the second element (number[1]),and so on.
int number [5]= {100, 4, 24, 34,5, 16};
C Programming
Initializing Arrays
Higher Technological Institute224
The second way to initialize an array is to initializeall elements in the array together.
For instance, the following statement initializes allelements of the array, number:
Here the integers inside the braces are assigned tothe corresponding elements of the array number.
That is, 100 is given to the first element(number[0]), 4 to the second element (number[1]),and so on.
int number [5]= {100, 4, 24};
C Programming
Initializing Arrays
Higher Technological Institute225
# include <stdio.h>
main ( )
{
int i; int A[10];
for ( i=0 ; i<10 ; i++ )
{
A[i]=i+1;
printf(“A[%d]=%d \n”,i,A[i]);
}
return 0; }
C Programming
Initializing Arrays
Higher Technological Institute226
# include <stdio.h>
main ( )
{
int i; int A[10];
for ( i=0 ; i<10 ; i++ )
{
A[i]=i+1;
printf(“A[%d]=%d \n”,i,A[i]);
}
return 0; }
C Programming
Example
Higher Technological Institute227
Example
Write a program to calculate the
Summation and Average of 10 numbers read
from a data statement.( Using Array )
C Programming
Example
Higher Technological Institute228
# include <stdio.h>
main ( )
{
int i; int A[10]; int sum=0; float av;
for ( i=0 ; i<10 ; i++ )
{
printf(“Enter the value of Element [%d] \n”,i);
scanf(“%d”,A[i]);
sum=sum+A[i]
}
av=sum/10;
printf(“Sum=%d \n Av=%f\n”, sum , av);return 0; }
C Programming
Two Dimensional Arrays
Higher Technological Institute229
int A[3][4] ;
Rows
Columns
C Programming
Two Dimensional Arrays
Higher Technological Institute230
int A[3][4] = { { 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 8 , 7 , 6 }} ;
Assigning Static Value
A[3][4] = { { 1 , 2 , 3 , 4 }, { 5 , 6 , 7 , 8 }, { 9 , 8 , 7 , 6 }} ;
OR
C Programming
Two Dimensional Arrays
Higher Technological Institute231
Example 1
Write a program to calculate and display
the Summation of two Matrices A and B.
A = 𝟏 𝟐 𝟑𝟒 𝟓 𝟔𝟕 𝟖 𝟗
, B = 𝟗 𝟖 𝟕𝟔 𝟓 𝟒𝟑 𝟐 𝟏
C Programming
Example
Higher Technological Institute232
# include <stdio.h> main ( ){
int A[3][3] = { { 1 , 2 , 3 } ,
{ 4 , 5 , 6 } ,
{ 7 , 8, 9 } } ;
int B[3][3] = { { 9 , 8 , 7 } ,
{ 6 , 5 , 4 } ,
{ 3 , 2, 1 } } ;
return 0; }
C Programming
Example
Higher Technological Institute233
# include <stdio.h> main ( ){
int A[3][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8, 9 } } ;
int B[3][3] = { { 9 , 8 , 7 } , { 6 , 5 , 4 } , { 3 , 2, 1 } } ;
return 0; }
C Programming
Example
Higher Technological Institute234
# include <stdio.h> main ( ){
int A[3][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8, 9 } } ;
int B[3][3] = { { 9 , 8 , 7 } , { 6 , 5 , 4 } , { 3 , 2, 1 } } ;
int S[3][3] ; int i,j;
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }
return 0; }
C Programming
Example
Higher Technological Institute235
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }
return 0; }
S3,3 = A3,3 + B3,3
= 𝟏 𝟐 𝟑𝟒 𝟓 𝟔𝟕 𝟖 𝟗
+𝟗 𝟖 𝟕𝟔 𝟓 𝟒𝟑 𝟐 𝟏
=𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎
at i = 0 j = 0
C Programming
Example
Higher Technological Institute236
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }
return 0; }
S3,3 = A3,3 + B3,3
= 𝟏 𝟐 𝟑𝟒 𝟓 𝟔𝟕 𝟖 𝟗
+𝟗 𝟖 𝟕𝟔 𝟓 𝟒𝟑 𝟐 𝟏
=𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎
at i = 0 j = 1
C Programming
Example
Higher Technological Institute237
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }
return 0; }
S3,3 = A3,3 + B3,3
= 𝟏 𝟐 𝟑𝟒 𝟓 𝟔𝟕 𝟖 𝟗
+𝟗 𝟖 𝟕𝟔 𝟓 𝟒𝟑 𝟐 𝟏
=𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎
at i = 1 j = 1
C Programming
Example
Higher Technological Institute238
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }
return 0; }
S3,3 = A3,3 + B3,3
= 𝟏 𝟐 𝟑𝟒 𝟓 𝟔𝟕 𝟖 𝟗
+𝟗 𝟖 𝟕𝟔 𝟓 𝟒𝟑 𝟐 𝟏
=𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎
at i = 2 j = 0
C Programming
Example
Higher Technological Institute239
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }
return 0; }
S3,3 = A3,3 + B3,3
= 𝟏 𝟐 𝟑𝟒 𝟓 𝟔𝟕 𝟖 𝟗
+𝟗 𝟖 𝟕𝟔 𝟓 𝟒𝟑 𝟐 𝟏
=𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎
at i = 2 j = 2
C Programming
Example
Higher Technological Institute240
# include <stdio.h> main ( ){
int A[3][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8, 9 } } ;
int B[3][3] = { { 9 , 8 , 7 } , { 6 , 5 , 4 } , { 3 , 2, 1 } } ;
int S[3][3] ; int i,j;
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }
return 0; }
C Programming
Two Dimensional Arrays
Higher Technological Institute241
Example 2
Write a program to calculate and display
the Summation of any two 3x3 Matrices.
C Programming
Example 2
Higher Technological Institute242
# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;
.
.
.
.
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }
return 0; }C Programming
Example 2
Higher Technological Institute243
# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;
for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics A*/
for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics A */
{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;
scanf(“%d”,A[i][j]); }
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }
return 0; }C Programming
Example 2
Higher Technological Institute244
# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;
for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics A*/
for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics A */
{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;
scanf(“%d”,A[i][j]); }
for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics B*/
for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics B */
{ printf(“Please Enter B[%d][%d] = \n ”, i , j ) ;
scanf(“%d”, B[i][j]); }
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }
return 0; }C Programming
Two Dimensional Arrays
Higher Technological Institute245
Example 3
Write a program to calculate and display
the Summation of any two 3x3 Matrices.
Then from Summation Matrix Find :
a) The Largest and Smallest element.
b) The average
c) Matrix Transpose
C Programming
Example 3
Higher Technological Institute246
# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;
for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics A*/
for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics A */
{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;
scanf(“%d”,A[i][j]); }
for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics B*/
for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics B */
{ printf(“Please Enter B[%d][%d] = \n ”, i , j ) ;
scanf(“%d”, B[i][j]); }
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
return 0; }
C Programming
Example 3
Higher Technological Institute247
# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;
for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics A*/
for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics A */
{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;
scanf(“%d”,A[i][j]); }
for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics B*/
for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics B */
{ printf(“Please Enter B[%d][%d] = \n ”, i , j ) ;
scanf(“%d”, B[i][j]); }
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
int L=S[0][0], M=S[0][0];
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
if (L<S[i][j])
L=S[i][j];
return 0; }
C Programming
Example 3 (a)
Higher Technological Institute248
# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;
scanf(“%d”,A[i][j]); }
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter B[%d][%d] = \n ”, i , j ) ;
scanf(“%d”, B[i][j]); }
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
int L=S[0][0], M=S[0][0];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (L<S[i][j])
L=S[i][j];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (M>S[i][j])
M=S[i][j];
printf(“Largest Element = %d \n ”, L ) ;
printf(“Smallest Element = %d \n ”, M ) ;
return 0; }
C Programming
Example 3 (a) (b)
Higher Technological Institute249
# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;
scanf(“%d”,A[i][j]); }
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter B[%d][%d] = \n ”, i , j ) ;
scanf(“%d”, B[i][j]); }
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
int L=S[0][0], M=S[0][0];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (L<S[i][j])
L=S[i][j];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (M>S[i][j])
M=S[i][j];
printf(“Largest Element = %d \n ”, L ) ;
printf(“Smallest Element = %d \n ”, M ) ;
int sum = 0 ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
sum=sum + S[i][j];
float average =sum/9 ;
printf(“Average = %d \n ”, average ) ;
return 0; }
C Programming
Example 3 (a) (b) (c)
Higher Technological Institute250
# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;
scanf(“%d”,A[i][j]); }
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter B[%d][%d] = \n ”, i , j ) ;
scanf(“%d”, B[i][j]); }
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
int L=S[0][0], M=S[0][0];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (L<S[i][j])
L=S[i][j];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (M>S[i][j])
M=S[i][j];
printf(“Largest Element = %d \n ”, L ) ;
printf(“Smallest Element = %d \n ”, M ) ;
int sum = 0 ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
sum=sum + S[i][j];
float average =sum/9 ;
printf(“Average = %d \n ”, average ) ;
int ST[3][3];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
ST[i][j]=S[j][i];
return 0; }
C Programming
Example 3 (a) (b) (c)
Higher Technological Institute251
# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;
scanf(“%d”,A[i][j]); }
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter B[%d][%d] = \n ”, i , j ) ;
scanf(“%d”, B[i][j]); }
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
int L=S[0][0], M=S[0][0];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (L<S[i][j])
L=S[i][j];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (M>S[i][j])
M=S[i][j];
printf(“Largest Element = %d \n ”, L ) ;
printf(“Smallest Element = %d \n ”, M ) ;
int sum = 0 ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
sum=sum + S[i][j];
float average =sum/9 ;
printf(“Average = %d \n ”, average ) ;
int ST[3][3];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
ST[i][j]=S[j][i];
return 0; }
C Programming
Do Not Forget to
add the display
statements for ST
Example 3 (a) (b) (c)
Higher Technological Institute252
# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;
scanf(“%d”,A[i][j]); }
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter B[%d][%d] = \n ”, i , j ) ;
scanf(“%d”, B[i][j]); }
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
int L=S[0][0], M=S[0][0];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (L<S[i][j])
L=S[i][j];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (M>S[i][j])
M=S[i][j];
printf(“Largest Element = %d \n ”, L ) ;
printf(“Smallest Element = %d \n ”, M ) ;
int sum = 0 ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
sum=sum + S[i][j];
float average =sum/9 ;
printf(“Average = %d \n ”, average ) ;
int ST[3][3];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf (“\n”); ST[i][j]=S[j][i];
printf (“%d”, ST[i][j]); }
return 0; }
C Programming
Matrix Multiplication
Higher Technological Institute253
Example 4
Write a program to calculate and display
the Multiplication of any two 3x3 Matrices.
C Programming
Two Dimensional Arrays
Higher Technological Institute254
Remember
C Programming
=
New Matrix
Dimensions
Example 4
Higher Technological Institute255
# include <stdio.h> main ( ){ int A[4][3]={ 2 , 3 , 4 ,
1 , 2 , 1 ,2 , 1 , 0 ,1 , 0 , 2 };
int B[3][4]={ 10 , 0 , 20 , 10 ,0 , 20 , 0 , 20,
20 , 10 , 10 , 0 };int M[4][4];int ra = 4 , ca = 3 , rb = 3, cb = 4 ;int i , j , k ;for (i=0;i<ra;i++)for (j=0;j<cb;j++){M[i][j]=0;for(k=0;k<ca;k++)M[i][j]= M[i][j]+ A[i][k]*B[k][j];}
printf("\n The M is \n");
for(i=0;i<ra;i++)
{
printf("\n");
for (j=0;j<cb;j++)
printf("%4d",M[i][j]);
}
return 0 ; }
C Programming
C Programming Higher Technological Institute256
Home Work ..!!
Write a program to calculate thedeterment of 3x3 Matrix.
Course Contents
Introduction
Higher Technological Institute257
1
Program Development2
The Essential of C Programs3
Manipulating Data with Operators 4
Reading from and Writing to Standard I/O5
Decision6
Iteration7
Arrays8
C Functions9
C Programming
Course Contents
Higher Technological Institute258
C Functions9
C Programming
Functions
Higher Technological Institute259
Why Function
o To make large programs Manageable,
Programmers modularize them into
subprograms.
o These subprograms are called
Functions
C Programming
Functions
Higher Technological Institute260C Programming
C Standard FunctionsOR
Built in Function
C Standard Functions
Higher Technological Institute261
The C language has a standard library which is acollection of predefined functions and other programelements which are accessed through header files.
One of the most important header file <math.h>contains useful mathematical functions.
C Programming
Note
Every mathematical function return doubledata type
Some Mathematical in <math.h>
Higher Technological Institute262C Programming
Function Descriptioncos(x) Cosine of x in Radian
exp(x) Exponential of x (base e)
log(x) Natural Logarithm of x (base e)
log10(x) Common Logarithm of x (base e)
pow(x,p) x to the power p = xP
sin(x) Sine of x in Radian
sqrt(x) Square root of x = 𝒙
tan(x) Tangent of x in Radian
Example 1
Higher Technological Institute263C Programming
Write a program to calculate the following:
a) cos (x)b) e x
c) x 3/2
d) 𝒙Test your program with
x = 8, 16, 32
Example 1
Higher Technological Institute264C Programming
# include <stdio.h>
# include <math.h>
main ( )
{
int x=8; double a,b,c,d;
do
{
a = cos(x*180/(22/7));
b=exp(x);
c=pow(x, 3/2);
d=sqrt(x);
printf(“At x = %d \n A = %f \n B= %f \n C= %f \n D=%f \n ”,x,a,b,c,d);x=x*2;
} while ( x<=32 );
return 0;
}
Example 1
Higher Technological Institute265C Programming
# include <stdio.h>
# include <math.h>
main ( )
{
int x=8; double a,b,c,d;
do
{
a = cos(x*180/(22/7));
b=exp(x);
c=pow(x, 3/2);
d=sqrt(x);
printf(“At x = %d \n A = %f \n B= %f \n C= %f \n D=%f \n ”,x,a,b,c,d);x=x*2;
} while ( x<=32 );
return 0;
}
Example 1
Higher Technological Institute266C Programming
# include <stdio.h>
# include <math.h>
main ( )
{
int x=8; double b ;
b=exp(x);
return 0;
}
x
b
8
Function 1
Function 2
…
…
exponential Function
…
…
math
main
Example 1
Higher Technological Institute267C Programming
# include <stdio.h>
# include <math.h>
main ( )
{
int x=8; double b ;
b=exp(x);
return 0;
}
x
b
8
Function 1
Function 2
…
…
exponential Function
…
…
math
main
8
Example 1
Higher Technological Institute268C Programming
# include <stdio.h>
# include <math.h>
main ( )
{
int x=8; double b ;
b=exp(x);
return 0;
}
x
b
8
Function 1
Function 2
…
…
exponential Function
…
…
math
main
8
8886110.520508
Some Header Files in C Library
Higher Technological Institute269C Programming
Header File
Description
<ctype.h> Defines function to test characters
<float.h> Defines constant to float
<limits.h>Defines the integer limits on your local computer
<math.h> Defines mathematical function
<stdio.h> Defines functions for standard input and output
<stdlib.h> Defines utility functions
<string.h> Defines functions for processing string
<time.h> Defines time and date functions
User Defined Function
Higher Technological Institute270C Programming
You Have to Know
o Function Type
o Function Name
o Arguments to the function
Typevoid
returnable
Name
Func_1
n1
Arguments
with
without
Build Your Function
Higher Technological Institute271
Func_Type Func_name( type1 Arg1, type2 Arg2 )
C Programming
{
statment1 ;
statment2 ;
.
.
.
return variable;
}
Function’s
Body
Where I can put my Function ??
Higher Technological Institute272
# include <stdio.h>
func_type Func_1( Argument1, Argument2 )
{
statment1 ;
statment2 ;
…
…
return ;
}
main ( )
{
…
Func1 Call ;
…
return 0;
}
C Programming
Main Function
Function’s Body
Where I can put my Function ??
Higher Technological Institute273
# include <stdio.h>
func_type Func_1( typeArgument1, typeArgument2 )main ( )
{
…
Func1 Call ;
…
return 0;
}
func_type Func_1( Argument1, Argument2 )
{
statment1 ;
statment2 ;
…
…
return ;
}
C Programming
Function’s Body
Main Function
;Function DeclarationOR
Where I can put my Function ??
Higher Technological Institute274
# include <stdio.h>
func_type Func_1( typeArgument1, typeArgument2 )main ( )
{
…
Func1 Call ;
…
return 0;
}
func_type Func_1( Argument1, Argument2 )
{
statment1 ;
statment2 ;
…
…
return ;
}
C Programming
Function’s Body
Main Function
;Function Declaration
Please Do not
forget ; after
Declaration Statement
Simple Example
Higher Technological Institute275
# include <stdio.h>
int sum ( int x, int y)
{
z = x + y;
return z ;
}
main ( )
{
int a, b, c;
a = 3;
b = 5;
c = sum( a , b );
printf("C=%d \n", c);
return 0;
}
C Programming
Simple Example
Higher Technological Institute276
# include <stdio.h>
int sum ( int x, int y)
{
z = x + y;
return z ;
}
main ( )
{
int a, b, c;
a = 3;
b = 5;
c = sum( a , b );
printf("C=%d \n", c);
return 0;
}
C Programming
Simple Example
Higher Technological Institute277
# include <stdio.h>
int sum ( int x, int y)
{
z = x + y;
return z ;
}
main ( )
{
int a, b, c;
a = 3;
b = 5;
c = sum( a , b );
printf("C=%d \n", c);
return 0;
}
C Programming
Simple Example
Higher Technological Institute278
# include <stdio.h>
int sum ( int x, int y)
{
z = x + y;
return z ;
}
main ( )
{
int a, b, c;
a = 3;
b = 5;
c = sum( a , b );
printf("C=%d \n", c);
return 0;
}
C Programming
• Why we use x, y,z in Function’s Body
While use a,b,c in main Function …?
• Effect of use the same variable name.
Simple Example by Method 2
Higher Technological Institute279
# include <stdio.h>
int sum ( int, int ) ;
main ( )
{
int a, b, c;
a = 3;
b = 5;
c = sum( a , b );
printf("C=%d \n", c);
return 0;
}
int sum ( int x, int y)
{
z = x + y;
return z ; }
C Programming
Simple Example by Method 2
Higher Technological Institute280
# include <stdio.h>
int sum ( int, int ) ;
main ( )
{
int a, b, c;
a = 3;
b = 5;
c = sum( a , b );
printf("C=%d \n", c);
return 0;
}
int sum ( int x, int y)
{
z = x + y;
return z ; }
C Programming
Again Do not
forget ; after
Declaration Statement
Why Functions
Higher Technological Institute281
ExampleWrite a program to calculate the
factorial of any integer number. Then use
your program to calculate the value of Y.
𝒀 =𝒏! − 𝒌!
𝒏 − 𝒌 !
C Programming
Example
Higher Technological Institute282
# include <stdio.h>
main ( )
{
int n ;
printf(“Please Enter The Value of n \n");
scanf(“%d”,&n);int factorial =1;
for ( int i=n ; i>=1 ; i-- )
factorial = factorial * i ;
printf(“Factorial =%d \n", factorial);
return 0;
}
C Programming
Example
Higher Technological Institute283
# include <stdio.h>
int fact ( int ) ;
main ( )
{
int n, k, y;
printf(“Please Enter The Value of n and k \n");
scanf(“%d %d”,&n,&k);y = ( fact(n) * fact(k) ) / fact( n – k ) ;
printf(“Y=%d \n", y);
return 0;
}
int fact ( int fc )
{
int factorial =1;
for ( int i=fc ; i>=1 ; i-- )
factorial = factorial * i ;
return factorial ;
}
C Programming
LOGO
Eng. Ibrahim Elewah
Higher Technological Institute 10th of Ramadan City6th of October Branch
Electrical and Computer Engineering Department
284
HTI Student Book
Main Reference “C For Dummies”
by Dan Gookin 2nd Edition