Professional Documents
Culture Documents
PL S7 S20 CTSD
PL S7 S20 CTSD
Thinking for
Structured
Design
Introductory Class
KLEF (CTSD) BES-1
Introduction to Computers
Literally
Modern Computers
More Pervasive in Life
Computers has to do much more than to calculate
KLEF (CTSD) BES-1
Receives
Obtained after
Applies any
Data Processing
operation
instructions
Memory Unit
Input data
and keyboard JoyStick
Instructions
Printer
Monitor Speakers
Plotters Projector
Headsets
Device Used to provide information for user in the required format.
KLEF (CTSD) BES-1
KLEF (CTSD) BES-1
KLEF (CTSD) BES-1
CPU Executes Program Instructions Step-By-Step
int main()
{
//variable declaration
int a,b,sum;
a =10; //assign a=10;
b =20; //assign b=20;
sum=a + b; //assign a+b value to sum
printf(“%d \n”,sum);
return 0;
}//end of the program
BCPL B 1972
C
KLEF (CTSD) BES-1
C Became So Popular
Text Application
Editors Programs
KLEF (CTSD) BES-1
First Program in C
#include<stdio.h>
int main()
{
printf(“HelloWorld”);
return 0;
}
BCPL and B
Introduction
to C
INTERNAL EXTERNAL
ASSESSMENT ASSESSMENT
60 40
KLEF (CTSD) BES-1
KLEF (CTSD) BES-1
• Course Title : (CTSD)
• Course Code : 21SC1101
• L-T-P-S Structure : 3-0-2-6
• Credits : 5.5
• Pre-requisite : Problem Solving Ability
Logical Thinking
Programming Style
BLOCK STRUCTURES
KLEF (CTSD) BES-1
Structured Programming
❑ Systematic Organization
Structured Programming
❑Top-Down Analysis
❑Modular Programming
❑Structured Code
KLEF (CTSD) BES-1
Top-Down Analysis
main()
{
. . . .
Approach . . . .
}
main() fun1()
{
{ . . . .
. . . .
. . . . }
fun2()
. . . . {
. . . . . . . .
. . . .
} }
Advantages
Program
Solution
Larger Problems
Developing Solution
Complicated
Advantages
Old Unstructured
Languages Code
Well
Organised
Code
KLEF (CTSD) BES-1
Advantages
❑Improves Problem Solving
❑Organisation of Program
❑Easily Modifiable
Introduction to
Algorithms & Flow Charts
KLEF (CTSD) BES-1
ALGORITHM
of complete
Obtained after
Applies any
Data Processing
operation
instructions
Example: If you see back side of a noodle packet you can see step
wise procedure which says how to cook noodles. That’s nothing
but an algorithm
Input
Step1:Start ? a ,b
Process
Step2:Read a,b
Step3:Calculate EQ=(a*a)+(b*b)+(2*a*b)
EQ=(a*a)+(b*b)+(2*a*b)
Step4:Display EQ
Output
Step5:Stop ? EQ
Sequence
Selection
Iteration
Read m , a
f = m * a
print f
Stop
Step 1:Start
aor=l*b
Step 2:Read l,b
Step 3:Calculate
aor=l*b
Step 4:Display aor print aor
Step 5:Stop
stop
KLEF (CTSD) BES-1
2. Design an algorithm for the Following equations
EQ=a2+b2+2ab start
Read a,b
Step1:Start
Step2:Read a,b
Step3:Calculate EQ=(a*a)+(b*b)+(2*a*b)
EQ=(a*a)+(b*b)+(2*a*b)
Step4:Display EQ
print EQ
Step5:Stop
stop
KLEF (CTSD) BES-1
Areaofcircle=3.14*r2
SimpleInterest=(p*t*r)/100
Direct Implementation
Sequence Algorithms
KLEF (CTSD) BES-1
Selection (Decision Making)
• Example
Greatest of two numbers
Check if the number is even or
odd
Step2:Read a,b
start
Step3:if(a>b)
print a
else
print b Read a
and b
Step4:Stop
stop
KLEF (CTSD) BES-1
2. Design an algorithm to check whether the given
number is positive or negative or zero
Input: num
4 X is positive
0 X is zero stop
-17 X is negative
Design an algorithm to illustrate the following. A
company calculates discounts allowed to
customers on the following basis
Order quantity Normal discount
Step1: start 1-99 5%
Step2: read q 100-199 7%
Step3: if (q>=1 and q<=99) 200-499 9%
500 and above 10%
print “discount is 5%”
else if (q>=100 and q<=199)
print “discount is 7%”
else if(q>=200 and q<=499)
print “discount is 9%”
else
print “discount is 10%”
Step4 : stop
input
process Process/Calculation
is done in User
Module
ouput
Stop
Modular Programming
KLEF (CTSD) BES-1
Modular Programming
Start Start
Start
Read m , a Read m , a
Initialize f
res=calculate(m,a)
f = m * a
print res f = m*a
print f
Stop return f
Stop Stop
Start Start
Start
Read Read
Initialize f
print
Stop return
Stop Stop
calculate(m,a) calculate(m,a)
res f
Function Call:
functionname(arglist);
Function Definition
returntype functionname(arglist)
{
Body of Function;
}
KLEF (CTSD) BES-1
Problem Comprehension:
• Each person must travel half of the distance to meet at midpoint.
• Here it is 25 miles
• 1 mile → 1.6 kms
25 miles → ?
By cross multiplying we get ?*1mile = 25*1.6 => ? = 40kms
Step 1: start
Algorithms for method
Step 2: Declare variables distance, ans midDistance
Step 3: distance = 50 Step 1: start
Step 4: ans=midDistance(distance) Step 2: declare variables res
Step 5: print “The distance to be Step 3:
travelled to meet at midpoint is”,ans
Calculate res=(dist/2) *1.6
Step 6: stop
Step 4: return res
Assign distance = 50
distance =50
initialize
res
ans=midDistance(distance)
res=
(distance/2)*1.6
print ans res=(dist/2) * 1.6
Print res
Stop
Stop
• Let x be 12 gallons
• 1 gallon → 3.785 litres
12 gallons → ?
By cross multiplying we get ?*1gallon =
12*3.785 => ? = 45.42 litres
• if he pours 36 litres, then 45.42-36 =9.42 litres
will be left with him
X=12 X=12
initialize
res
ans=remainingMilk(x)
res=(gallons*3.785)-36
res=(gallons*3.785)-
print ans 36
print res
Stop
Stop
ans=calDistance(deg,height);
printf("Distance=%f",ans);
return 0; float calDistance(float d,float h)
} {
float res,rad;
rad= d * 3.14 /180;
res=h/tan(rad);
return res;
KLEF (CTSD) } BES-1
KEYWORDS
int for
if struct
float while
else union
char do
typedef
short enum
void
long switch return
double case
break auto
continue extern
signed default register
unsigned goto static
sizeof
const
volatile
KLEF (CTSD) BES-1
Input-Output Functions
printfoutputfunction 2usages
1.)printf(“message”); syntax-Rule
Ex printf(“HelloSection7Students”);
printf(“gdfjsfjhskjdfhskjdhfk”);
any message can be printed here
2.)printf(“typespecifier”,variablename);
Ex: printf(“% d % d”,a,b); 10 20
Operand << n
• Right-shift operator
Operand >> n
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 0 0 1 0
Now shifting the bits towards left for 1 time, will give the
following result
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 0 1 0 0
If you left shift like 2<<2, then it will give the result
as 8. Therefore left shifting 1 time, is equal to
multiplying the value by 2.
KLEF (CTSD) BES-1
Bit Wise Shift Operators
#include <stdio.h>
int main()
{
int a=6;
printf("The value of a<<2 is : %d ",
a<<2);
return 0;
} 6 X 2 =12 X 2 =24
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 1 0 0 0
Now shifting the bits towards right for 1 time, will give the
following result
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 0 1 0 0
Age
float 5.7
int
10
6.4
20
10 Data/ 5.7
Information
age 20
Datatypes
( int )
He is 20
Years old. height 5.7
His height is ( float )
5.7 Feet
Data Type
Data types are used to define a variable to hold those facts as data value.
Variable
Name given to memory block which holds value. The value can be changed at any
moment so called variable.
KLEF (CTSD) BES-1
height
age
Data
int float
Datatypes
1. 20 5.4
Variables
2. 25 5.7
int float
20002
Address of the cell
(Memory Location)
1. 20 5.4
2. 25 5.7
int float
20002
Address of the cell
(Memory Location)
int float
20010
Address of the cell
(Memory Location)
1. 20 5.4
2. 25 5.7
So, need another Datatype
%f
long signed
float unsigned
printf(“% d”,c);
}
KLEF (CTSD) BES-1
include <stdio.h>
int main() no1 no2 sum
{ 10 15 25
int no1, no2, sum; 5678
1234 910
no1=10;
no2=15;
// calculating sum
sum = no1 + no2;
printf("%d + %d = %d", no1, n02, sum);
return 0;
}
switchcase
if if-else if-else-if Nested -if
if(condition1)
if(condition) if(condition1)
if(condition) { //true {
{
{ } if( )//true }
//true
//true elseif(condition2) elseif(condition2)
}
} { //true } { if( )//true }
else
else else
{
{ //false } { if( )//false }
//false
KLEF
} (CTSD) BES-1
#include<stdio.h> no
int main()
{ 5
int no;
no=5;
if(no%2==0)
printf(“given no is even”);
else
printf(“given no is odd”);
return 0;
}
if(no==0)
printf(“given no is zero”);
else if(no>0)
printf(“given no is positive”);
else
printf(“given no is negative”);
return 0;
}
#include<stdio.h>
int main() if else
{
int age;
if(age>=18)
printf(“Eligible for Vaccination”);
else
printf(“Not Eligible for Vaccination”);
return 0; }
KLEF (CTSD) BES-1
Nested if
{
case 1: printf("I am One");
break;
case 2: printf("I am Two");
break;
case 3: printf("I am Three");
break;
default: printf(“definitely I am not 1, 2 and 3.");
}
FALSE
TRUE
FALSE
KLEF (CTSD) BES-1
Ternary/ Conditional Operator
#include <stdio.h>
int main()
{
int age;
printf("Enter your age");
scanf("%d",&age);
(age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting"));
return 0;
}
}
else
ans = 0;
printf ("%d\n", ans);
int main()
a
{
int a;
22
a=22;
------
34
-------
56
a=34;
-------
a=56; This shows that whenever
---------
return 0;
we declare a variable, in
} that at a time we can store
only one value .
KLEF (CTSD) BES-1
For example I am writing a program where I need to store 5
integer values. For this I must declare 5 individual variables
like,
memory
int a,b,c,d,e;
a b
d e
datatype arrayname[size];
int arrayname[size];
float arrayname[size];
int x[100] ;
// x is an array to store maximum 100 integers
float height[50];
// height is an array to store heights of maximum 50
persons
int fib[15];
// Fib is an array to store maximum 15 elements of
fibinocci sequence.
a[0]=22 a[1]=-34
a[3]=45
a[2]=65 a[4]=36
data
4 bytes 4 bytes 4 bytes 4 bytes 4 bytes
type
array
name
size of
the array
a[0]=3.14 a[1]=-0.465
a[3]=4.5
a[2]=8.5 a[4]=33.6
scanf(“%f%f”,&a[0],&a[1]);
printf(“% f%f”,a[0],a[1]);
return 0;
}
float *pa=&a; 0 1 2 3 4
pa=&a;
❖ Integer Array Pointer
int a[10];
int *pa;
❖ Float Array Pointer
float a[10];
float *pa;
pa=a;
KLEF (CTSD) BES-1
#include <stdio.h>
int main()
{
int a[2] ; a[0]=10;a[1]=20;
int *p = a;
printf("a[0] = %d, a[1] = %d, *p = %d",
*p, *(p+1), *p);
return 0;
}
#include <stdio.h>
int main(void)
{
int a=9,*pa;
pa = &a;
printf("%d",*pa);
printf("\t% p",a);
printf("\t%d",&a);
return 0;
}
KLEF (CTSD) BES-1
#include <stdio.h>
int main()
{
int var = 5;
int *p=&var;
printf("var value: %d", var);
printf("\naddress of var: %p", &var);
printf("\nPointer value:%d",*p);
//value of var is used by p
printf("\npointing to address:\n%p",p);
//address of var stored in p
return 0;
}
Address of first cell of an array is called base address of that array. Float array element
can be accessed using float pointer by assigning base address to float pointer and
adding cell index.
0 1 2 3 4
float *fptr = a;
3.14 -0.465 8.5 4.5 33.6
*(fptr + 1); // equivalent to a[1] a[0] a[1] a[2] a[3] a[4]
fptr[2]; // equivalent to a[2]
fptr
#include<stdio.h>
int main()
{
char ch;
scanf(“%c”,&ch); ch=‘p’;
if(((ch>=97)&&(ch<=122))||((ch>=65) && (ch<=90)))
printf(“%c is character”,ch);
else
printf(“not a character”);
return 0;
}
KLEF (CTSD) BES-1
❖Every variable in C programming has two properties:
type and storage class.
❖Type refers to the data type of a variable. And,
storage class determines the scope, visibility and
lifetime of a variable.
❖There are 4 types of storage class:
1. automatic
2. external
3. static
4. register
KLEF (CTSD) BES-1
Storage Class and its Purpose
auto
default storage class(block)
GarbageValue
extern
global variable.(Program)
0
Static
local variable which returns value even when
control transfers to function call(block)
0
register
store inside the register.(block)
GarbageValue
KLEF (CTSD) BES-1
Example of Automatic Storage Class
#include<stdio.h>
int main() This variable scope and lifetime is within
{ main method
auto int i=1;
{
auto int i=2;
{
This variable auto int i=3;
scope and lifetime printf(“\n%d”,i);
is within this }
block printf(“%d”,i);
}
printf(“%d”,i);
return 0;
}
Output: 3 2 1
Loops
Loops
set of statements;
4
}
1 2 3 4 5 6 7 8 9 10
i=1 1<=10 1
i=2 2<=10 2
1 1 1
i=3 3<=10 3
i=4 4<=10 4
i=5 5<=10 5
i=6 6<=10 6
for(i=1;i<=10;i++) i=7
i=8
7<=10
8<=10
7
8
i=9 9<=10 9
i=10 10<=10 10
1=11 11<=10
KLEF (CTSD) BES-1
1
2 5
for(i=1;i<=3;i++)
{
3
4
printf(“%d\t”,i);
}
{
statements;
3
4
increment/decrement;
} While(condition);
for(i=1;i<=5;i++) for(i=1;i<=5;i++)
{ {
if(i==3) if(i==3)
break; continue;
printf(“%d\t”,i); printf(“%d\t”,i);
} }
Output
Output
12 12 4 5
function purpose
fopen () Creating a file or opening an existing file
fclose () Closing a file
fprintf () Writing a block of data to a file
Read input
Open the file Display output in
values from
in read Mode output screen using Close file
file using
printf()
fscanf()
Output
Value of n=50
fp=fopen("first.txt","r");
for(i=0;i<3;i++)
fscanf(fp,"%d",&a[i]);
printf("%d\t",a[i]);
fclose(fp);
return 0;
}
KLEF (CTSD) BES-1
#include<stdio.h>
int main()
{
float Salary,bonus;
char gender;
FILE *fp;
fp=fopen("Salaray.txt","w");
scanf("%c%f",&gender,&Salary);
fprintf(fp,"%c %f",gender,Salary);
fclose(fp);
fp = fopen("Salary.txt","r");
fscanf(fp,"%c%f",&gender,&Salary);
if(gender=='f'|| gender =='F')
bonus = 0.1*Salary;
else
bonus = 0.05*Salary;
fclose(fp);
printf("The Bonus: %f",bonus);
return 0;
}
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" , e );
e = a+b*c/d
e = a+b*c/d
a + b && b / c % d * e || 5 <= 0 != a + b – d
k l u \0
String Declaration
char str1[]=“klu”;
char str1[5]=“klu”;
\0 will automatically inserted at the end in this type of
declaration
char str1[]={‘k’,’L’,’u’,’\0’};
char str1[5]={‘k’,’L’,’u’,’\0’};
#include <stdio.h>
float calculateSum(float age[]);
int main() {
float calculateSum(float age[]) {
float result, age[] = {23.4, 55, 22.6, 3, 40.5, 18};