c lector1

34
Programming and Data Structures Group I: sections 1,2,5,6 Group I: sections 1,2,5,6 Prof. Rimli Sengupta, CSE

Transcript of c lector1

Programming and Data Structures

Group I: sections 1,2,5,6Group I: sections 1,2,5,6

Prof. Rimli Sengupta, CSE

Course Outline (Tentative)C programmingC programming

Input, output, arithmetic and logical opsInput, output, arithmetic and logical opsControl constructsControl constructsFunctionsFunctionsRecursionRecursionDynamic memory allocationDynamic memory allocationFile I/oFile I/o

Course Outline (contd.)

Data structuresData structuresArrays (1 D, 2 D)Arrays (1 D, 2 D)Linked listsLinked listsStack, queueStack, queueTrees, binary search treesTrees, binary search trees

Algorithms and efficiency analysisAlgorithms and efficiency analysis

Courseware

Textbook: any reputed C language referenceTextbook: any reputed C language referenceKernighan and RitchieKernighan and Ritchie ““The C Programming The C Programming LanguageLanguage””Gottfried “Gottfried “Programming with CProgramming with C””

Lectures: Lectures: will be made availablewill be made available

Grading Plan

Theory: 60%Theory: 60%Endsem 50%Endsem 50%Midsem 30%Midsem 30%TA 20% TA 20%

2 class tests2 class testsattendanceattendance

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 computerA computerAbility to talk to itAbility 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 failcommunication may fail

Programs vs. ProgrammingPrograms: Programs: precise recipe for a taskprecise recipe for a task

Programming: Programming: no no reciperecipe for good programmingfor good programming… but there are guidelines for the result… but there are guidelines for the result

correctcorrectefficientefficientupgradeableupgradeablestyle (“beautiful” code)style (“beautiful” code)

science

art

Practice is key

A computer (Level 0 Version)

Central Processing

Unit (CPU)

InputInputPeripheralsPeripherals

OutputPeripherals

MainMemory

StoragePeripherals

Memory: Address and Values

CPU: A first cut

PCIR

MAR

MDRALU

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: Start1: Read 102: Read 113: Add 10, 11, 124: Write 125: Halt

High-Level Programs0: Start1: Read 202: Read 213: Compare 20, 21, 224: J_Zero 22, 75: Write 206: Jump 87: Write 218: Halt

Variables x, y;BeginRead (x);Read (y);If (x >y) then Write (x)

else Write (y);End.

Examples of SoftwareRead an integer and Read an integer and determine if it is a prime determine if it is a prime number.number.A Palindrome recognizerA Palindrome recognizerRead 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 airportsjourney between two airportsTelephone pole placement Telephone pole placement problemproblemPatriot Missile ControlPatriot Missile Control

A WordA Word--processorprocessorA C language Compiler A C language Compiler Windows 2000 operating Windows 2000 operating systemsystemFingerFinger--print recognitionprint recognitionChess PlayerChess PlayerSpeech RecognitionSpeech RecognitionLanguage RecognitionLanguage RecognitionDiscovering New Laws in Discovering New Laws in MathematicsMathematicsAutomatic drug discoveryAutomatic drug discovery

First C program - 1

#include <#include <stdiostdio.h>.h>main ()main (){{

printfprintf ("Hello, World! ("Hello, World! \\n") ; n") ; }}

First C program - 2

#include <#include <stdiostdio.h>.h>main ()main (){{

printfprintf ("Hello, World! ") ;("Hello, World! ") ;printfprintf ("Hello ("Hello \\n World! n World! \\n") ; n") ;

}}

First C program - 3

#include <#include <stdiostdio.h>.h>main ()main (){{

printfprintf ("Hello, World! ("Hello, World! \\n") ;n") ;printfprintf ("Hello ("Hello \\n World! n World! \\n") ;n") ;printfprintf ("Hell("Hell\\no no \\t World! t World! \\n") ;n") ;

}}

First Program: Reading

#include <#include <stdiostdio.h>.h>main ()main (){{

intint num;num;scanfscanf ("%d", &num) ;("%d", &num) ;printfprintf ("%d("%d\\n", num) ;n", num) ;

}}

Second C program - 1#include <#include <stdiostdio.h>.h>main ()main (){{

intint x;x;intint y;y;x=1;x=1;y=3;y=3;printfprintf("x = %d, y= %d("x = %d, y= %d\\n", x, y);n", x, y);

}}

Variables in MemoryMemory location allocated

to a variable XInstruction executed

X = 10Ti

me

10X = 20

X = X +1

X = X*5

Variables in MemoryMemory location allocated

to a variable XInstruction executed

X = 10Ti

me

20X = 20

X = X +1

X = X*5

Variables in MemoryMemory location allocated

to a variable XInstruction executed

X = 10Ti

me

21X = 20

X = X +1

X = X*5

Variables in MemoryMemory location allocated

to a variable XInstruction executed

X = 10Ti

me

105X = 20

X = X +1

X = X*5

Variables (contd.)

20

?

X = 20

XY=15

X = Y+3 Y

Y=x/6

Variables (contd.)

20

15

X = 20

XY=15

X = Y+3 Y

Y=x/6

Variables (contd.)

18

15

X = 20

XY=15

X = Y+3 Y

Y=x/6

Variables (contd.)

18

3

X = 20

XY=15

X = Y+3 Y

Y=X/6

Second C program - 2

#include <#include <stdiostdio.h>.h>main()main(){{

intint x, y, sum;x, y, sum;scanfscanf(“%d%d”,&x,&y);(“%d%d”,&x,&y);sum = x + y;sum = x + y;printfprintf( “%d plus %d is %d( “%d plus %d is %d\\n”, x, y, sum );n”, x, y, sum );

}}

Second C program - 3

#include <#include <stdiostdio.h>.h>main()main(){{

float x, y;float x, y;scanfscanf(“%f%f”,&x,&y);(“%f%f”,&x,&y);printfprintf( “%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 <stdiostdio.h>.h>main()main(){{

float cent, float cent, fahrfahr;;scanfscanf(“%f”,&cent);(“%f”,&cent);fahrfahr ==printfprintf( “%f C equals %f F( “%f C equals %f F\\n”, cent, n”, cent, fahrfahr););

}}

Centigrade to Fahrenheit

#include <#include <stdiostdio.h>.h>main()main(){{

float cent, float cent, fahrfahr;;scanfscanf(“%f”,&cent);(“%f”,&cent);fahrfahr = cent*(9.0/5.0) + 32;= cent*(9.0/5.0) + 32;printfprintf( “%f C equals %f F( “%f C equals %f F\\n”, cent, n”, cent, fahrfahr););

}}

This does not work!

#include <#include <stdiostdio.h>.h>main()main(){{

float cent, float cent, fahrfahr;;scanfscanf(“%f”,&cent);(“%f”,&cent);fahrfahr = cent*(9/5) + 32;= cent*(9/5) + 32;printfprintf( “%f C equals %f F( “%f C equals %f F\\n”, cent, n”, cent, fahrfahr););

}}

More on expressions

#include <#include <stdiostdio.h>.h>main()main(){{

float cent, float cent, fahrfahr;;scanfscanf(“%f”,&cent);(“%f”,&cent);fahrfahr = cent*9/5 + 32;= cent*9/5 + 32;printfprintf( “%f C equals %f F( “%f C equals %f F\\n”, cent, n”, cent, fahrfahr););

}}

Largest of two numbers

#include <#include <stdiostdio.h>.h>main()main(){{

intint x, y;x, y;scanfscanf(“%d%d”,&x,&y);(“%d%d”,&x,&y);if (x>y)if (x>y) printfprintf(“Largest is %d(“Largest is %d\\n”,x);n”,x);elseelse printfprintf(“Largest is %d(“Largest is %d\\n”,y);n”,y);

}}

What does this do?

#include <#include <stdiostdio.h>.h>main()main(){{

intint x, y;x, y;scanfscanf(“%d%d”,&x,&y);(“%d%d”,&x,&y);if (x>y)if (x>y) printfprintf(“Largest is %d(“Largest is %d\\n”,x);n”,x);printfprintf(“Largest is %d(“Largest is %d\\n”,y);n”,y);

}}