Session1 Introduction to C programming language Brief history

Session-1 Introduction to C programming language

Brief history of C • C is Structured Programming Language • The C programming language is a standardized programming language developed by Ken Thompson and Dennis Ritchie • It was developed in early 1970 s • It is one of the most widely used programming languages • It is praised for its efficiency, and is the most popular programming language for writing system software

Programming • A program is an algorithm expressed in a programming language • Programming is an act of creating software or some other set of instructions for a computer • Computer programming is the craft of implementing one or more interrelated algorithms using a particular programming language to produce a concrete computer program

Program development lifecycle WHAT Requirements Gathering, Problem definition HOW Analysis & Design DO IT Build TEST Testing Deploy & Maintain USE

Program Development life cycle • • • Problem definition Program Design Program Coding Program compilation and Execution Program testing and Debugging Program Documentation 9/5/2021 5

Problem definition • • What must the program do? What outputs are required and in what form? What inputs are available and in what form? The complete set of requirements and assumptions constitute the problem definition.

Program Design • • Involves devising an algorithm Divide and conquer Top down approach used Each successive subdivision is referred to as a stepwise refinement

Program Coding • Program coding is the process of expressing the algorithm developed for solving a problem as a computer program in a programming language • Use of structured programming improves understandability • Use of combination of only three control structures: sequential, selective, and repetitive

Program compilation and Execution • Two steps: compilation and linking. • The compilation step converts your source program into an intermediate form, called object code • The linking step combines this object code with other code to produce an executable program

Program testing and Debugging There are four levels of debugging • desk checking • compile-time errors • run-time, or execution-time, errors • Logical errors

Program Documentation • Refers to information about the program that specifies what is being done and how. • It includes detailed --requirements of program -- layout of all output reports and input data -- top down decomposition -- algorithm for each component --input data used for testing --output of the test run.

Program Life cycle Req. Gathering Algorithm Test Plan Coding Pseudo code Source Code + Test Script Compilation Object code Linking Libraries Debugging / Testing Executable File

Program Life cycle(Cont’d) • Requirement Gathering – Gathering all the required information to understand the problem in hand • Algorithm – A simple English, programming language independent pseudo code is generated. • Test Plan – Gives a plan to test the solution. It involves test cases, test scripts and test data

Program Life cycle(Cont’d) • Coding – Enter the source code using some standard editor, care must be taken for proper formatting, indenting and using comment entries. • Compilation – The compiler program scans the source code for SYNTAX errors, and if it does not find any errors, it converts the source code to OBJECT code. • Link – The object files are combined with other ‘library’ files to create the EXECUTABLE file.

Program Life cycle(Cont’d) • Library – It is a catalogue of previously developed objects. They contain procedures for input, output, math, etc. A user can create their own libraries • Testing – Synthesize test data such that it will evaluate the different criteria. The testing should be NEGATIVE testing. • Debugger – It is a very useful tool to test the program as the intermediate results, new values of the variables, etc are visible

Algorithm • Algorithm is a detailed sequence of actions to perform or accomplish some task • An Algorithm is a solution to a problem that is independent of any programming language. • An algorithm is: • A finite sequence of steps • Each step shall be explicit and unambiguous • For each input, it shall terminate in finite time with output • A program is an algorithm expressed in a programming language.

Structure of an algorithm • Algorithm can be written using a standard structure • Most of the time it has the following contents – – – – Begin Step Control Comments Variables Input / Output Exit • Algorithm Always Begins with – Begin or Start • Algorithm Ends with – End or Exit or Stop • Rest of the structure contents are used depending on the need

Example-1 • Example of an algorithm to find the sum of two numbers Algorithm Starting point num 1, num 2 are the variables declared 1. 2. 3. 4. 5. Begin Input num 1, num 2 Sum=num 1+num 2 Print Sum End of Algorithm “Input” is used to perform read values to num 1 & num 2 “print” is used to print the output (result)

Flowchart • Flowchart is a graphic representation of the logic or steps in a program or system. • The flowchart can be defined as a diagram of the problem solving steps in a process. • A flowchart is a schematic representation of a process.

Flowchart depictions • Each symbol depicts a process • The start point, end points, Start – End points inputs, outputs, etc are represented by a graphical Processing Logic image • Any flow chart – should have Input/Output – Start in the beginning – End to the end of the flow chart – Depending on the requirement, rest of the symbols are used Decisions Connectors Flow Line Loops 20

Flowchart – Example 1 Example of Flow chart to read marks of three subjects. Find the average scored by the student, and print “Pass” if the score is more than 50 otherwise print “Fail”. Start Enter m 1, m 2, m 3 Avg=m 1+m 2+m 3/3 Avg>50 Fail pass end 21

Activities • Activity 1. 0 Write an algorithm, flow chart & test cases to find the sum of first 5 integers (It uses Iterational algorithm technique. ) • Activity 1. 1 Write an algorithm, flow chart & test cases to search an element in a table of N values (Uses iterative and selection algorithm technique) • Activity 1. 2 Write an algorithm, flowchart & test cases to find the sum of N even numbers (Uses iterative algorithm technique) • Activity 1. 3 Write an algorithm, flowchart & test cases to find a Fibonacci series (Fibonacci series is of the form 0, 1, 1, 2, 3, 5……………Generate the series up to N terms) • Activity 1. 4 Write an algorithm and flowchart & test cases to find the maximum & minimum among three numbers entered from the keyboard. (Use selection technique. )

Solution for activity 1. 0 • /*algorithm to find sum of first 50 integers*/ • • step 1: start Step 2: Let i=1 Step 3: Let sum=0 Step 4: sum=sum+i step 5: i=i+1 step 6: if i<=50 goto step 4 step 7: output sum step 8: end

Solution for activity 1. 1 • /*algorithm to search an element in a table of N values. Assume that we have a table named TAB of N values*/ • • • Step 1: start Step 2: INPUT X/*x is search element*/ Step 3: let i=1 Step 4: Let flag=0 step 5: IF TAB[i]==X let flag=1 goto step 8 Step 6: i=i+1 Step 7: if i<=N goto step 5 Step 8: if flag=1 OUTPUT "element found at I th position" else OUTPUT "element not found" Step 9: End

Solution for activity 1. 2 • /*Alogorithm to find the sum of first N even numbers*/ • • Step 1: Start Step 2: let i=2 Step 3: let sum=0 Step 4: sum=sum+i Step 5: i=i+2 Step 6: if i<=n goto step 4 Step 7: Output sum Step 8: End

Solution for activity 1. 3 • /*Algorithm to find the fibonacci series upto N terms */ • • • • Step 1: Start Step 2: Input N Step 3: let f 1=0/*f 1 and f 2 are intial terms of fibonacci sequence*/ Step 4: let f 2=1 Step 5: let count=2/* counter variable*/ Step 5: Output f 1, f 2 Step 6: f 3=f+f 2 Step 7: count=count+1 Step 8: if count>N goto step 13 Step 9: output f 3 Step 10: f 1=f 2 Step 11: f 2=f 3 Step 12: goto step 6 Step 13: End

Solution for activity 1. 4 • /* algorithm to find maximum and minimum among three numbers entered from the keyboard*/ • • • • • • Step 1: Start Step 2: INPUT a, b, c Step 3: if a>b /*to find maximum*/ max=a else max=b endif step 4: if c>max max=c endif step 5: if a<b/* to find minimum*/ min=a else min=b endif Step 6: if c<min c=min endif Step 7: OUTPUT "maximum of three numbers", max Step 8: OUTPUT "minimum of three numbers", min Step 9: end

• Any Queries? ? ?

Quiz!!! 1. Give three reasons why C is the best choice of programming language. 2. What does the compiler do? 3. What are the steps in the program development cycle?
- Slides: 29