Introduction to Programming Mehul Jain
Mumbai darshan again on th 16 july
Overview • • • •
Computer Systems: The Basics Algorithms Flowcharts Programs
Computer Systems • Dumb machines that only process instructions (programs) • Follow Garbage In Garbage Out (GIGO) principle • Can’t differentiate into correct/incorrect logic • Problem solving tool
Under the hood • Input: e.g.., Keyboard and Mouse • Processor: Central Processing Unit • Output: Monitor or a Printer
Algorithms • An Algorithm is a solution to a problem that is independent of any programming language. • Steps are essentially sequential in nature • All Inputs and Outputs have to be specified explicitly • Have a start state and an end state (explicit start and end points) • For each input, a result should be generated and that too in finite time
Use of algorithms • • • •
Facilitates easy development of programs Iterative refinement Easy to convert it to a program Review is easier
Q Write an Algorithm to solve quadratic equation (ax2 + bx +c =0) for Real Roots •
0. START
•
Read a, b and c //(non zero constants) (Input)
•
Disc=b2-4ac //Calc. Discriminant (Process)
•
If Disc>=0 Then Goto step 5 Else Goto step 7 //For Real Roots (Decision)
•
X1= (-b+√Disc)/2a, X2= (-b-√Disc)/2a //Calculating Roots (Process)
•
Write X1 and X2
•
STOP
FLOWCHARTS: A PICTURE IS WORTH A THOUSAND WORDS • A flowchart is a diagrammatic representation of an algorithm • Normally the “flow” of control is from top to bottom
Q Draw a flowchart for the algorithm of the prev. example (quad. eqn)
START Read a, b & c
Disc=b2-4ac No
Disc>=0 Yes
X1= (-b+√Disc)/2a, X2= (-b-√Disc)/2a
Write X1 and X2 STOP
PROGRAM • A program is an algorithm expressed in a programming language • Each language has a certain syntax and has a variety of programming constructs available for easier problem solving • “Perfectly Optimal Code” is a Holy Grail that has not many takers (abstraction v/s user friendliness)
CONSTANTS & VARIABLES
2007 NMIMS
• A constant is a constant is a constant • 23, 44, “NMIMS”, 65, “Hello”, a. • A Variable is a Container for Constants • Variables can change values during the execution of the program • e.g. a, num1,x
Arrays n
A(1)
A(n)
• An array is a set of identical variables that contain similar data • Arrays have a fixed size • Index numbers are used to access the Array variables