Programming and Data Structures: Group I: Sections 1,2,5,6
Programming and Data Structures: Group I: Sections 1,2,5,6
Programming and Data Structures: Group I: Sections 1,2,5,6
Structures
Group I: sections 1,2,5,6
Group I: sections 1,2,5,6
Prof. Rimli Sengupta, CSE
Course Outline (Tentative)
C programming
C programming
Input, output, arithmetic and logical ops
Input, output, arithmetic and logical ops
Control constructs
Control constructs
Functions
Functions
Recursion
Recursion
Dynamic memory allocation
Dynamic memory allocation
File I/o
File I/o
Course Outline (contd.)
Data structures
Data structures
Arrays (1 D, 2 D)
Arrays (1 D, 2 D)
Linked lists
Linked lists
Stack, queue
Stack, queue
Trees, binary search trees
Trees, binary search trees
The C Programming
The C Programming
Language
Language
Gottfried
Gottfried
Programming with C
Programming with C
Lectures:
Lectures:
will be made available
will be made available
Grading Plan
Theory: 60%
Theory: 60%
Endsem 50%
Endsem 50%
Midsem 30%
Midsem 30%
TA 20%
TA 20%
2 class tests
2 class tests
attendance
attendance
Lab: 40%
Lab: 40%
Questions..questions..
How many have programmed before?
How many have programmed before?
What is programming?
What is programming?
What do you need?
What do you need?
A computer
A computer
Ability to talk to it
Ability to talk to it
High level language (e.g. C)
High level language (e.g. C)
Knowledge of where/how the
Knowledge of where/how the
communication may fail
communication may fail
Programs vs. Programming
Programs:
Programs:
precise recipe for a task
precise recipe for a task
Programming:
Programming:
no
no
recipe
recipe
for good programming
for good programming
but there are guidelines for the result
but there are guidelines for the result
correct
correct
efficient
efficient
upgradeable
upgradeable
style (beautiful code)
style (beautiful code)
science
art
Practice is key
A computer (Level 0 Version)
Central
Processing
Unit (CPU)
Input
Input
Peripherals
Peripherals
Output
Peripherals
Main
Memory
Storage
Peripherals
Memory: Address and Values
CPU: A first cut
PC
IR
MAR
MDR
ALU
R1
R2
R3
R4
FLAGS
Instruction Set
Program
Start
Read M
Write M
Load Data, M
Copy M1, M2
Add M1, M2, M3
Sub M1, M2, M3
Compare M1, M2, M3
Jump L
J_Zero M, L
Halt
0: Start
1: Read 10
2: Read 11
3: Add 10, 11, 12
4: Write 12
5: Halt
High-Level Programs
0: Start
1: Read 20
2: Read 21
3: Compare 20, 21, 22
4: J_Zero 22, 7
5: Write 20
6: Jump 8
7: Write 21
8: Halt
Variables x, y;
Begin
Read (x);
Read (y);
If (x >y) then Write (x)
else Write (y);
End.
Examples of Software
Read an integer and
Read an integer and
determine if it is a prime
determine if it is a prime
number.
number.
A Palindrome recognizer
A Palindrome recognizer
Read in airline route
Read in airline route
information as a matrix and
information as a matrix and
determine the shortest time
determine the shortest time
journey between two airports
journey between two airports
Telephone pole placement
Telephone pole placement
problem
problem
Patriot Missile Control
Patriot Missile Control
A Word
A Word
-
-
processor
processor
A C language Compiler
A C language Compiler
Windows 2000 operating
Windows 2000 operating
system
system
Finger
Finger
-
-
print recognition
print recognition
Chess Player
Chess Player
Speech Recognition
Speech Recognition
Language Recognition
Language Recognition
Discovering New Laws in
Discovering New Laws in
Mathematics
Mathematics
Automatic drug discovery
Automatic drug discovery
First C program - 1
#include <
#include <
stdio
stdio
.h>
.h>
main ()
main ()
{
{
printf
printf
("Hello, World!
("Hello, World!
\
\
n") ;
n") ;
}
}
First C program - 2
#include <
#include <
stdio
stdio
.h>
.h>
main ()
main ()
{
{
printf
printf
("Hello, World! ") ;
("Hello, World! ") ;
printf
printf
("Hello
("Hello
\
\
n World!
n World!
\
\
n") ;
n") ;
}
}
First C program - 3
#include <
#include <
stdio
stdio
.h>
.h>
main ()
main ()
{
{
printf
printf
("Hello, World!
("Hello, World!
\
\
n") ;
n") ;
printf
printf
("Hello
("Hello
\
\
n World!
n World!
\
\
n") ;
n") ;
printf
printf
("Hell
("Hell
\
\
no
no
\
\
t World!
t World!
\
\
n") ;
n") ;
}
}
First Program: Reading
#include <
#include <
stdio
stdio
.h>
.h>
main ()
main ()
{
{
int
int
num;
num;
scanf
scanf
("%d", &num) ;
("%d", &num) ;
printf
printf
("%d
("%d
\
\
n", num) ;
n", num) ;
}
}
Second C program - 1
#include <
#include <
stdio
stdio
.h>
.h>
main ()
main ()
{
{
int
int
x;
x;
int
int
y;
y;
x=1;
x=1;
y=3;
y=3;
printf
printf
("x = %d, y= %d
("x = %d, y= %d
\
\
n", x, y);
n", x, y);
}
}
Variables in Memory
Memory location allocated
to a variable X
Instruction executed
X = 10
T
i
m
e
10 X = 20
X = X +1
X = X*5
Variables in Memory
Memory location allocated
to a variable X Instruction executed
X = 10
T
i
m
e
20
X = 20
X = X +1
X = X*5
Variables in Memory
Memory location allocated
to a variable X Instruction executed
X = 10
T
i
m
e
21 X = 20
X = X +1
X = X*5
Variables in Memory
Memory location allocated
to a variable X Instruction executed
X = 10
T
i
m
e
105
X = 20
X = X +1
X = X*5
Variables (contd.)
20
?
X = 20
X
Y=15
X = Y+3
Y
Y=x/6
Variables (contd.)
20
15
X = 20
X
Y=15
X = Y+3
Y
Y=x/6
Variables (contd.)
18
15
X = 20
X
Y=15
X = Y+3
Y
Y=x/6
Variables (contd.)
18
3
X = 20
X
Y=15
X = Y+3
Y
Y=X/6
Second C program - 2
#include <
#include <
stdio
stdio
.h>
.h>
main()
main()
{
{
int
int
x, y, sum;
x, y, sum;
scanf
scanf
(%d%d,&x,&y);
(%d%d,&x,&y);
sum = x + y;
sum = x + y;
printf
printf
( %d plus %d is %d
( %d plus %d is %d
\
\
n, x, y, sum );
n, x, y, sum );
}
}
Second C program - 3
#include <
#include <
stdio
stdio
.h>
.h>
main()
main()
{
{
float x, y;
float x, y;
scanf
scanf
(%f%f,&x,&y);
(%f%f,&x,&y);
printf
printf
( %f plus %f is %f
( %f plus %f is %f
\
\
n, x, y, x+y);
n, x, y, x+y);
}
}
Third C program
#include <
#include <
stdio
stdio
.h>
.h>
main()
main()
{
{
float cent,
float cent,
fahr
fahr
;
;
scanf
scanf
(%f,¢);
(%f,¢);
fahr
fahr
=
=
printf
printf
( %f C equals %f F
( %f C equals %f F
\
\
n, cent,
n, cent,
fahr
fahr
);
);
}
}
Centigrade to Fahrenheit
#include <
#include <
stdio
stdio
.h>
.h>
main()
main()
{
{
float cent,
float cent,
fahr
fahr
;
;
scanf
scanf
(%f,¢);
(%f,¢);
fahr
fahr
= cent*(9.0/5.0) + 32;
= cent*(9.0/5.0) + 32;
printf
printf
( %f C equals %f F
( %f C equals %f F
\
\
n, cent,
n, cent,
fahr
fahr
);
);
}
}
This does not work!
#include <
#include <
stdio
stdio
.h>
.h>
main()
main()
{
{
float cent,
float cent,
fahr
fahr
;
;
scanf
scanf
(%f,¢);
(%f,¢);
fahr
fahr
= cent*(9/5) + 32;
= cent*(9/5) + 32;
printf
printf
( %f C equals %f F
( %f C equals %f F
\
\
n, cent,
n, cent,
fahr
fahr
);
);
}
}
More on expressions
#include <
#include <
stdio
stdio
.h>
.h>
main()
main()
{
{
float cent,
float cent,
fahr
fahr
;
;
scanf
scanf
(%f,¢);
(%f,¢);
fahr
fahr
= cent*9/5 + 32;
= cent*9/5 + 32;
printf
printf
( %f C equals %f F
( %f C equals %f F
\
\
n, cent,
n, cent,
fahr
fahr
);
);
}
}
Largest of two numbers
#include <
#include <
stdio
stdio
.h>
.h>
main()
main()
{
{
int
int
x, y;
x, y;
scanf
scanf
(%d%d,&x,&y);
(%d%d,&x,&y);
if (x>y)
if (x>y)
printf
printf
(Largest is %d
(Largest is %d
\
\
n,x);
n,x);
else
else
printf
printf
(Largest is %d
(Largest is %d
\
\
n,y);
n,y);
}
}
What does this do?
#include <
#include <
stdio
stdio
.h>
.h>
main()
main()
{
{
int
int
x, y;
x, y;
scanf
scanf
(%d%d,&x,&y);
(%d%d,&x,&y);
if (x>y)
if (x>y)
printf
printf
(Largest is %d
(Largest is %d
\
\
n,x);
n,x);
printf
printf
(Largest is %d
(Largest is %d
\
\
n,y);
n,y);
}
}