Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit-I Program Development Concepts and Introduction to C

Similar presentations


Presentation on theme: "Unit-I Program Development Concepts and Introduction to C"— Presentation transcript:

1 Unit-I Program Development Concepts and Introduction to C
Computer Programming Unit-I Program Development Concepts and Introduction to C

2 Topics Algorithms, flowcharts Types of programming languages
Programming language tools History of C programming Data types in C Writing simple programs

3 ALGORITHMS

4 Questions : Define algorithm with example.
Explain sequence, repetition and decision statements in algorithm? Write an algorithm to find larger of two numbers.

5 Algorithm An algorithm is a finite step by step procedure to transform input into desired output.

6 Informal definition of an algorithm
used in a computer

7 Algorithm (cont.) Characteristics or Properties of an Algorithm
Finiteness: - an algorithm terminates after a finite numbers of steps. Definiteness: - each step in algorithm is unambiguous. This means that the action specified by the step cannot be interpreted (explain the meaning of) in multiple ways & can be performed without any confusion. Input:- an algorithm accepts zero or more inputs Output:- it produces at least one output. Effectiveness:- it consists of basic instructions that are realizable. This means that the instructions can be performed by using the given inputs in a finite amount of time.

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23 Algorithm (cont.) Key Features (Constructs) of Algorithms Sequence
Decision Repetition

24 Three constructs (Key Features)

25 Algorithm (cont.) 1. Sequence Each step executed in specified order
Example: Addition of two numbers

26 Algorithm (cont.) Sequence Algorithm: Addition of two numbers
Step 1: Input the first number as A Step 2: Input the second number as B Step 3: Set Sum=A+B Step 4: Print/ Display Sum Step 5: End

27 Algorithm (cont.) 2. Decision
Used when outcome depends on some conditions General form: if condition then process if condition then process1 else process2

28 Algorithm (cont.) 2. Decision
Algorithm: To check equality of two numbers Step 1: Input the first number as A Step 2: Input the second number as B Step 3: if A=B then print “Equal” else print “Not Equal” Step 4: End

29 Algorithm (cont.) 3. Repetition
Executing one/more steps number of times Implemented using while, do-while, for loops Example: Print first 10 natural numbers

30 Algorithm (cont.) 3. Repetition
Algorithm: Print first 10 natural numbers Step 1: [Initialize] Set I=1, N=10 Step 2: Repeat Step 3 & 4 while I<=N Step 3: Print “I” Step 4: Set I = I + 1 Step 5: End

31 Algorithm (cont.) Exercise Write algorithm for…
Interchanging / swapping two values To find larger of two numbers To find whether a number is even or odd To find sum of first N natural numbers

32 Algorithm (cont.) Exercise 1
Algorithm : Interchanging / swapping two values Step 1: Input 1st number A Step 2: Input 2nd number B Step 3: Set Temp = A Step 4: Set A=B Step 5: Set B=Temp Step 6: Print A, B Step 7: End

33 Algorithm (cont.) Exercise 2 Algorithm : To find larger of two numbers
Step 1: Input 1st number A Step 2: Input 2nd number B Step 3: if A > B then print A else if A < B then print B else print “Numbers are Equal” Step 4: End

34 Algorithm (cont.) Exercise 3
Algorithm : To find whether a number is even or odd Step 1: Input 1st number A Step 2: if A % 2 = 0 then print “Even” else print “Odd” Step 3: End

35 Algorithm (cont.) Exercise 4
Algorithm : To find sum of first N natural numbers Step 1: Input N Step 2: Set I = 0, Sum = 0 Step 3: Repeat Step 3 & 4 while I<=N Step 4: Set Sum = Sum + I Set I = I + 1 Step 5: Print Sum Step 6: End

36 FLOWCHARTS

37 questions With the help of example explain the use of flowchart.
Write an algorithm to calculate the sum of the first 10 natural numbers & draw the flowchart for the same. Draw & Explain different Flowchart notations

38 Flowcharts 1. Graphical representation of any program is called flowchart. 2. There are some standard graphics that are used in flowchart as following:

39

40 Flowcharts (Cont.) Symbols Used Start or End Connecter Input / Output
Arrows (Flow of Control) Decision Processing

41 Flowcharts (Cont.) Start & End Symbols Arrows Generic Processing Step
Terminal Symbols Always first and last in flowchart Arrows Flow of Control Exact sequence of execution Generic Processing Step Activity Represents Instructions One / More Instruction can be specified

42 Flowcharts (Cont.) Input / Output Symbols Condition or Decision Symbol
Get input from User Displays Result Condition or Decision Symbol Depicts Yes/No or True/False Arrows always labeled Connectors Indicate continued flow

43 Flowcharts (Cont.)

44 Flowcharts (Cont.) Significance of Flowchart
Illustrates sequence of steps to be performed Drawn in early stages For understanding of complicated logic For better documentation of complex programs

45 Flowcharts (Cont.) Example 1 Draw a flowchart to add two numbers

46 Flowcharts (Cont.) Example 3
Draw a flowchart to calculate the sum of the first 10 natural numbers

47 Flowcharts (Cont.) Example 4
Draw a flowchart to determine largest of three numbers

48 Example 4

49 Flowcharts (Cont.) Advantages Very good communication tool
Also used for program documentation Acts as guide or blueprint for programmer Used to debug programs that have errors

50 Flowcharts (Cont.) Limitations Laborious and time consuming activity
Many time complex and clumsy Small changes results in re-drawing of flowchart No well defined standard for details to be included in flowchart

51 PROGRAMMING LANGUAGES

52 Programming Languages
Designed to express computations Used to create programs that controls systems behavior Used to express algorithms To describe set of consecutive actions to be executed by a computer Way to give instructions to computer Interface between computer & human being

53 Programming Languages (Cont.)
Consists vocabulary of syntax and semantics Usually refers to HLL (Ex. C, C++ etc.) HLL : Easy to read, understand : Unique set of keywords

54 Programming Languages (Cont.)
Computer understands Machine Language Compiling or Interpreting the program Which language is best? - Type of Computer - Type of Program - Expertise of Programmer

55 Generation of Programming Languages
Level of Programming Languages

56 Generation of Programming Languages
Level of Programming Languages Machine Language (1GL) Assembly Language (2GL) High Level Language (3GL) Very High Level Language (4GL)

57 Generation of Programming Languages
Machine Language (1GL) Lowest level Native language of Computer All data and commands expressed using 1’s and 0’s Equivalent to On/Off signals Most detailed level Machine dependent

58 Generation of Programming Languages
Machine Language (1GL) Advantage Efficient and quick execution of code Limitations Program writing: slow & time consuming Difficult to learn, difficult to edit errors No portability of Code Machine dependent

59 Assembly Language (2GL) Symbolic programming language
Symbolic notations for instructions Mnemonics Codes, easy to remember Ex. ADD, DIV, MUL etc. Related to internal architecture of computer Machine dependent

60 Generation of Programming Languages
Assembly Language (2GL) Advantages Programming much easier than machine language Limitations Machine dependent No portability of code Translator or Assembler required

61 Generation of Programming Languages
High Level Language (3GL) Refinement of 2GL OR Abstraction of Assembly Language Programming easier, efficient and less error-prone Convenient to use Interpreter or Compiler required (Translator)

62 Generation of Programming Languages
High Level Language (3GL) Characteristics Easier to learn Machine Independent No need to know kind of machine Availability of Program Libraries Code easy to Debug

63 Generation of Programming Languages
Very High Level Language (4GL) Non-procedural ‘What’ instead of ‘How’ Define only what they want , without specifying how it has to be done No standard rule that defines 4GL Ex. SQL (Structured Query Language)

64 Generation of Programming Languages
Very High Level Language (4GL) Characteristics Code written in English like sentence Non-procedural Code is easier to maintain Enhance productivity of programmer

65 Programming Language Tools
A programming tool or software development tool is a program or application that software developers use to create, debug, maintain, or otherwise support other programs and applications. 

66 Source code editors A source code editor is a text editor program designed specifically for editing source code of computer programs by programmers. Source code editors are the most fundamental programming tool, as the fundamental job of programmers is to write and edit source code.

67 Source code editors (cont.)
Source code editors have features specifically designed to simplify and speed up input of source code, such as syntax highlighting, indentation, autocomplete and bracket matching functionality. These editors also provide a convenient way to run a compiler, interpreter, debugger, or other program relevant for the software development process. So, while many text editors can be used to edit source code, if they don't enhance, automate or ease the editing of code, they are not source code editors, but simply text editors that can also be used to edit source code.

68 Compiler The "compiler" is primarily used for programs that translate source code from a high-level programming language to a lower level language

69 Interpreter In computer science, an interpreter is a computer program that directlyexecutes, i.e. performs, instructions written in programming or scripting language, without previously compiling them into a machine languageprogram.

70 Assembler Assembly language is converted into executable machine code by a utility program referred to as an assembler. The conversion process is referred to as assembly, or assembling the source code.

71 Linker  In computing, a linker or link editor is a computer program that takes one or moreobject files generated by a compiler and combines them into a single executablefile, library file, or another object file.

72 Debugger A debugger or debugging tool is a computer program that is used to test anddebug other programs (the "target" program). 

73 Loader   a loader is the part of an operating system that is responsible for loading programs and libraries. It is one of the essential stages in the process of starting a program, as it places programs into memory and prepares them for execution. Loading a program involves reading the contents of the executable file containing the program instructions into memory, and then carrying out other required preparatory tasks to prepare the executable for running. Once loading is complete, the operating system starts the program by passing control to the loaded program code.

74 Questions Define algorithm. How is it useful in context of software development process? Explain sequence, repetition and decision statements? With the help of example explain the use of flowchart. Define the term programming language. Give certain examples of such language.

75 Questions (cont.) What are the different levels of programming languages? What is machine language? Explain. Write a short note on Assembly language. Code written in machine language is efficient and fast to execute. Comment. How is the third generation programming language better than its predecessors? Write a short note on 4GL.

76 INTRODUCTION TO ‘C’ LANGUAGE

77 History of ‘C’ In 1972, by Dennis Ritche at AT&T Bell Lab., USA

78 Characteristics of C Reliable, Simple and Easy to learn (or Use)
Well suited for structured programming It is stable language (Less or no modification) Facilitates low level (bitwise) programming C is core language

79 Characteristics of C (cont.)
Enable programmer to concentrate on problem and not worry about background details C is a portable language C is an extensible language. (User add own function to C library) Procedure oriented language Follows ‘Top-down’ approach

80 Getting Started with ‘C’

81 The C Character Set A character denotes any alphabet, digit or special symbol used to represent information.

82 Constants, Variables and Keywords Constants - An entity that doesn’t change Variables - An entity that may change - Values are stored in these memory cells - Memory cells (also called memory locations) are given names Keywords - Whose meaning already known to system

83 Constants, Variables and Keywords Variables Variable names are names given to locations in memory Ex. x is a variable and 3 or 5 is value

84 Types of C Constants

85 Rules for constructing Integer Constants
Constant must have at least one digit It must not have a decimal point It can be either positive or negative No commas or blanks are allowed Allowable range for integer constants is to Ex. 426 +720 -8000

86 Rules for constructing Real Constants
Constant must have at least one digit It must have a decimal point It can be either positive or negative No commas or blanks are allowed Expressed in Fractional and Exponential form Ex.: , , , Ex.: +3.2e-5 , 4.1e8 , -0.2e+3 , -3.2e-5

87 Rules for constructing Character Constants
A single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Ex. ‘S’, ‘>’ Maximum length of a character constant can be 1 character. Ex.: ‘A’, ‘i’, ‘=’, ‘5’

88 Types of C Variables Variable names are names given to locations in memory Locations can contain integer, real or character constants Types of variables depend on the types of constants that it can handle Ex. - Integer variable can hold only an integer constant - Real variable can hold only a real constant

89 Rules for Constructing Variable Names
Variable name is any combination of 1 to 31 alphabets, digits or underscores Do not create unnecessarily long variable names First character in the variable name must be an alphabet or underscore No commas or blanks are allowed within a variable name No special symbol other than an underscore (as in gross_sal) can be used in a variable name EX. roll_no, m_hra, page1, salary etc.

90 C Keywords Words whose meaning has already been explained to the C compiler or Computer Keywords cannot be used as variable names Also called ‘Reserved words’ 32 keywords available in C

91 C Keywords

92 Rules applicable to all C programs
Each instruction in a C program is written as a separate statement All statements are entered in small case letters Every C statement must end with a ; (semi-colon). Thus ; acts as a statement terminator No blank spaces are allowed within a variable, constant or keyword

93 How to write ‘C’ program?
Start the TC Select New from the File menu Type the program Save the program using F2 under a proper name (say Program1.c) Use Ctrl + F9 to compile and execute the program. Use Alt + F5 to view the output

94 First C programs /* Program to display hello message */ #include<stdio.h> void main() { printf(“hello”); }

95 1. Comment 2. Header Files 3. Main Function 4. Print Instruction

96 Example 2: Contact Details
Output

97 Example 3: How to clear ‘Consol’

98 Data Types Primary (or Fundamental) data type Derived data type User-defined data type

99 void Data Types Floating Point Type Primary Data Types float double
long double Integer Signed unsigned type int unsigned int short int unsigned short int long int unsigned long int Character char signed char unsigned char

100 Data Types

101 Declaration of Variables
It tells the compiler what the variable name is. It specifies what type of data the variable will hold. Primary Type Declaration Ex. char name; int roll; float radius; data_type v1, v2, …, vn;

102 Declaration of Variables (Position)
main() /* …Program Name..*/ { /*… Variable Declaration...*/ char c; int roll; float radius; /*… Computation…*/ … } /*… Program Ends…*/

103 Assigning Values to Variables
Ex. int p; p=100; Ex. float pi = 3.14; variable_name = constant; data_type variable_name = constant;

104 printf(“control string”, variable1, variable2,… );
Printing Variables printf(“control string”, variable1, variable2,… ); Ex.1 int p; p=100; printf(“%d”, p); Ex.2 float radius = 3.4; printf(“%f”, radius); Ex.3 char name = ‘A’; printf(“%c”, name);

105 Ex. 4: Data Types Output

106 Ex. 5 Addition of Two Numbers
Output

107 Reading Data from Keyboard
Ex.1 int roll; scanf(“%d”, &roll); Ex.2 float radius; scanf(“%f”, &radius); Ex.3 char name; scanf(“%c”, &name); scanf(“control string”, &variable1, &variable2,… );

108 Ex. 6 Reading Data from Keyboard
Output

109 Exercise Write a program to calculate: area of circle, square and rectangle Write a program to calculate average of 3 numbers Write a program to calculate volume of sphere

110 Ex.1 Area of Circle

111 Ex. 2 Average of 3 Numbers

112 Programming Tools Editor Compiler Interpreter

113 Programming Tools Compiler
Compiler translate source code from a high level language to a lower level language (e.g., assembly language or machine language) Program that translates from a low level language to a higher level one is a decompiler

114 Programming Tools Compiler

115 Programming Tools Compiler Compiler characteristics:
spends a lot of time analyzing and processing the program the resulting executable is some form of machine- specific binary code the computer hardware interprets (executes) the resulting code program execution is fast

116 Programming Tools 2. Interpreter
program that translates an instruction into a machine language and executes it before proceeding to the next instruction translates and runs the program at the same time

117 Programming Tools 2. Interpreter

118 Programming Tools 2. Interpreter Characteristics:
relatively little time is spent analyzing and processing the program the resulting code is some sort of intermediate code the resulting code is interpreted by another program program execution is relatively slow


Download ppt "Unit-I Program Development Concepts and Introduction to C"

Similar presentations


Ads by Google