module1 new c programming for begginers.pptx

amruthavarshini849703 37 views 93 slides Jun 30, 2024
Slide 1
Slide 1 of 93
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93

About This Presentation

nil


Slide Content

Algorithm An algorithm is a procedure or formula for solving a problem. An algorithm is define as a complete , unambiguous , finite number of logical steps for solving a specified problem. Algorithms are used for calculation, data processing, and many other fields. Each algorithm is a list of well-defined instructions for completing a task.Starting from an initial state(START), the instructions describe a computation that proceeds through a well-defined series of successive states, eventually terminating in a final ending state (STOP).

Guidelines for construct an algorithm It should begin with start statement Given problem should be broken down into simple and meaningful steps Steps should be numbered sequentially Steps should be descriptive and written in simple English Last step should be end statement

Eg : Algorithm to average of n numbers Step 1 : start Step 2 : Read value of n Step 3 Read n numbers Step 4 : add these n numbers Step 5 : divide the sum by n to get average Step 6 : print the average Step 7 : Stop

Properties: 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.

flowchart A diagrammatic representation of sequential steps of solution of problem consisting standard block symbols being link together by directional arrows are called flowchart.

Basic Flowchart symbols

Meaning of flowchart symbols Symbol name Description Terminal Indicates the beginning or end of an algorithm Input/Output The point at which values of some data item have to read or some results written are indicated by input/output box Process Indicates computation or data manipulation Flow lines Connects the flowchart symbols and indicates the logic flow Decision Is used to indicate point at which decision has to be made and branch one/two alternatives possible Connector Indicates an entry to, or exit from another part of the flowchart or a connection point

Guidelines for constructing a flowchart While drawing flowchart Initially concentrate on the logic of the problem and draw the main path of the flowchart Flowchart can have only 1 start point and 1 stop point Use descriptive term that aptly represent logic of the problem. Do not use ambiguous terms Remember that another user/programmer should easily understand flowchart

Example of flowchart Flowchart for sum of first 50 natural numbers

Importance/ benefits of flowcharts Communication : Flowcharts are better way of communicating the logic of a system to all concerned. Effective analysis : With the help of flowchart, problem can be analyzed in more effective way. Proper documentation : Program flowcharts serve as a good program documentation, which is needed for various purposes. Efficient Coding : The flowcharts act as a guide or blueprint during the systems analysis and program development phase.

Proper Debugging : The flowchart helps in debugging process. Efficient Program Maintenance : The maintenance of operating program becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that part

LIMITATIONS OF USING FLOWCHARTS Complex logic : Sometimes, the program logic is quite complicated. In that case, flowchart becomes complex and clumsy. Alterations and Modifications : If alterations are required the flowchart may require re-drawing completely. Reproduction : As the flowchart symbols cannot be typed, reproduction of flowchart becomes a problem. The essentials of what is done can easily be lost in the technical details of how it is done.

Distinguish between algorithm and flow chart Algorithm Flowchart Algorithm is a procedure for solving a problem Flowchart is a graphical diagram of the solution to the problem The algorithm is constructed out of boxes and it is written as various steps It is constructed with the shapes of each box indicating the kind of operation to be performed. The actual operation are written in different steps The actual operation to be performed is written inside the symbol The step numbers shows which step is to be done next or ‘ go to ‘ statements is used to specified the next step to be performed The arrow coming out of symbol indicates which operation to perform next

Structured programming concept Structured programming is a concept in which a large program can be divided in to small modules and can be solved each module one by one. Computer programming in which the statements are organized in a specific manner to minimize error or misinterpretation. Principles of structured programming are Structuring of control flow Decompose a program into portions or modules Program design using Top down or bottom up approach

Structuring of control flow is to keep the complexity of a program under control so that design and understanding of a program logic become easier. Three main structures in structured programming are Sequence Selection Repetition structuring of control flow

Sequence logic It refers to the line-by-line execution. The program enters the sequence, does each step, and exits the sequence

Selection logic In it there are various conditions on the basis of which one module is selected out of several given modules.

Repetition logic In it loops are implemented. Loop is used to implement those statements which are to be repeated again and again until some condition is satisfied.

Modular programming Modular programming is a programming strategy where a program is divided into segment called modules. A module is a unit or entity that is responsible for a single task or routine. module consisting of series of program instructions or statements in some programming language Program using this approach consisting of a main module called control module and number of functional modules A module may also decomposed into subordinate modules representing a sub task.

A module that control subordinate modules is also known as superior module Superior module does not include codes of subordinate modules . superior module calls subordinate modules for execution Subordinate modules also referred as called module and superior module as calling module.

Main/control module Functional module 1 Functional module n Subordinate modules Superior module

Advantages of modular programming Modularization has several benefits, especially on large and complex programs: Divide work for multiple programmers. Reuse code -modules can be re-used in several projects changing the implementation details of a modules does not require to modify the clients using them as far as the interface does not change; faster re-compilation , as only the modules that have been modified are actually re-compiled; self-documenting, as the interface specifies all that we need to know to use the module; easier debugging , as modules dependencies are clearly specified and every module can be tested separately;

The program can be solved in two ways they are: Top down programming Bottom up Programming Top down and bottom up approach

Top-down programming Top-down programming is a programming style, in which complex pieces are divided into smaller pieces. Each pieces are again divided into sub systems until we get the most simple piece of program . Then each pieces are solved independently Top down approach program design starts with specification of function to be performed by a program and then breaks it down into progressively subsidiary functions In topdown approach calling module is always designed before the called module D B A C E F G H Each box in the figure is a module . Topmost module represented by A.A Decomposed to B,C,D. B and C further subdivided to sub components.

It initiates human tendancy to solve a problem by outlining broad concepts first and then subsequently going into detail Development of module can take place in parallel. Separating the low level work from the higher level objects leads to a modular design. Very less time consuming (Each programmers are involved only a part of the big project) Very optimized way of processing . (Each programmers have to apply their own knowledge and experience to their parts (modules), so our project will become an optimized one ) Advantages of top-down programming

Disadvantages of top-down programming Top-down programming complicates testing . Noting executable exists until the very late in the development, so in order to test what has been done so far, one must write stubs. (Stubs are software programs which act as a module and give the output as given by an actual product/software.) top-down programming tends to generate modules that are very specific to the application that is being written, thus not very reusable. The implementation cost is likely to be higher.

Bottom-up approach Is reverse of topdown approach In a bottom-up approach the individual base elements of the system are first specified in great detail These elements are then linked together to form larger subsystems In this approach the beginnings are small but eventually grow in complexity and completeness A F E B C D H G

Advantages of Bottom up programming The advantages of using a bottom-up approach to software testing is the fact that there is no need for stubs. bottom-up design yields programs which are smaller and more agile . A shorter program doesn't have to be divided into so many components, and fewer components means programs which are easier to read or modify. Fewer components also means fewer connections between components, and thus less chance for errors there. errors in critical modules are found earlier major functions and processing are tested earlier. Bottom-up design promotes code re-use.

Disadvantages of bottom up programming A disadvantage of bottom-up design is that it is not focused on specific requirements. a program designed from the bottom up may not exactly fit the given problem. High quality bottom-up solutions prove very hard to construct Components may be coded without having a clear idea of how they link to other

Difference between topdown and bottom up programming Top Down Programming Bottom up programming Begins the design with main module progress downward to lowest modules Begins the design with lowest level module progress upward to top level or main module Code is not reusable Code is reusable Testing become complicate Testing become easy compared to top down

Programming A program is a sequence of instructions written to perform a specified task. programming is the process of designing, writing, testing, debugging / troubleshooting, and maintaining the source code of computer programs. source code is a text written in a programming language.

Translators Translation is the process of converting high level language into low level language. Translators are the programs used to do this translation Translator Source code Object code

Two types of translators are Compilers Interpreters Computer programs written in any language other than machine language must be either interpreted or compiled.

Compilers A compiler is a computer program (or set of programs) that transforms source code written in high level programming language into machine language. It converts entire program in one go and report all the errors of the program along with line numbers Once object code available compiler is not needed in memory

Interpreter In computer science, an interpreter is a program which translates one statement of a high level language program into machine code and Executes it . if there is any error in any line it reports it at the same time and program execution cannot resume until the error is rectified. Interpreter must always present in memory every time program executed as every time program is run it is first interpreted then executed BASIC and LISP are especially designed to be executed by an interpreter.

Difference Between Compilers and Interpreters Compiler Interpreter 1.It reads the entire program once and then translates it before execution. 1.It read one statement at a time and translate it into machine code, executes it and then go to the next statement of the program. 2.Object code is stored after compilation. 2. Object code is not stored. 3.compilation is very fast 3. Whole execution is slow. 4.Errors are displayed after compilation 4. Errors are displayed as and when they are found and further translation is possible after correcting it.

Introduction to C language

C Character Set A character denotes any alphabet ,digit or symbols to represent information.   Numerals: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9  Alphabets: a, b, ….z, A, B, ……...Z  Arithmetic Operations + - * / %(Mod) Special Characters ) ( { } etc                           

C Identifiers They are user defined names. They do not have any predefined meanings Identifiers" or "symbols" are the names you supply for variables, functions, and labels in our program. Identifier names must differ in spelling and case from any keywords. You create an identifier by specifying it in the declaration of a variable, type, or function. Eg : sum,total

Rules for the identifiers You cannot use keywords Must not contain white space First character must be letter(or underscore) Must consist of only letters, digits or underscore

C Keywords Key words are NOT user defined names They have predefined meanings These keywords can be used only for their intended purpose; they cannot be used as programmer-defined identifiers. keywords are all lowercase. C makes use of only 32 keywords or reserved words.

The standard keywords are auto extern sizeof break f l o a t s t a t i c case f o r s t r u c t char got0 switch const i f typedef continue i n t union d e f a u l t long unsigned do r e g i s t e r void double return v o l a t i l e else short while enum signed

Data Types C supports several different types of data, each of which may be represented differently within the computer’s memory. Data types allow programmer to select type of data appropriate to the needs of the application as well as the machine.

Int type Figure for Storing the integer 7 using a binary code. An integer is a number with no fractional part Integers are stored as binary numbers.

Floating Point Types Floating point number is a number containing a decimal point and or an exponent Floating point number represents a real number with 6 digits precision . Floating point numbers are denoted by the keyword float . Some floating-point numbers are 2.75, 3.16E7, 7.00, and 2e–8. When the accuracy of the floating point number is insufficient, we can use the double to define the number. The double is same as float but with longer precision( 14 digits precision )

Float and double storage Floating-point representation involves breaking up a number into a fractional part (mantissa) and an exponent part and storing the parts separately IEEE format for floating point storage use sign bit, mantissa and exponent. Sign bit denotes sign of a number .0 for positive value 1 for negative value mantissa is represented in binary Exponent is an integer stored in unsigned binary format after adding a positive integer bias. This ensures that the stored exponent is always positive. Value of bias is 127 for floats and 1023 for double IEEE format for floating point storage

A single-precision binary floating-point number is stored in 32 bits Storing the number 0.15625 in 32 bit representation is shown below In IEEE format ,value= sign  = the sign bit. (0 makes the number positive ; 1 makes it negative.) exp  = the 8-bit number stored in the exponent field.  exp  = biased exponent = the true exponent + 127. fraction  (mantissa)= the 23 bits of the fraction field.

In the example, the  sign  is 0, so the number is positive;   exp  is 124, so the true exponent is –3; and  fraction  is .01. So, the represented number is:

value =(-1) sign * 1.fraction * (2 exp-1023 ) Exponent =true exponent +1023 IEEE format for Double storage double precision  is essentially the same except that the fields are wider. The fraction part is much larger, while the exponent is only slightly larger.

Character Type A single character can be defined as a character(char) type of data. Characters are usually stored in 1 byte of internal storage.

Void Type Void type has no values Used to specify type of function Type of function is said to be void when it does not return any values to the calling function

The qualifiers define the amount of storage allocated to the variable .  4 data type qualifiers are available in c short long signed Unsigned size qualifiers- Short and long Sign qualifiers- Signed and unsigned Points to be noted All of these modifiers can be applied to base type int Signed and unsigned can be apply to base type char Long can be apply for double When basetype omitted from declaration int is assumed Data type qualifiers(data type modifiers)

A short int may require less memory than an ordinary int or it may require the same amount of memory as an ordinary int , but it will never exceed an ordinary int in word length. a long int may require the same amount of memory as an ordinary int or it may require more memory, but it will never be less than an ordinary int. A signed integer use one bit for storing sign and rest 15 bits for number.   Sign bit is 0 for positive number and 1 for negative Unsigned numbers are always positive and consume all the bits for the magnitude of the number

Size and range of data types on a 16 bit machine

CONSTANTS constant refer to fixed values that do not change during execution of a program.

Integer and floating-point constants represent numbers. They are often referred to collectively as numeric-type constants . Commas and blank spaces cannot be included within the constant. The constant can be preceded by a minus (-) sign if desired. The value of a constant cannot exceed specified minimum and maximum bounds. rules apply to all numeric-type constants.

1. Integer Constants: An integer constant is an integer-valued number. it consists of a sequence of digits . C allows 3 types of integer constants decimal (base 10 ) octal (base 8) hexadecimal (base 16).

     ( i ) Decimal Integer Constant: A decimal integer constant can consist of any combination of digits taken from the set 0 through 9 preceded by optional – or + sign. If the constant contains two or more digits, the first digit must be something other than .    Several valid decimal integer constants are shown below. 743 5280 32760 9999         

(ii) Octal Integer Constant: An octal integer constant can consist of any combination of digits taken from the set 0 through 7. first digit must be 0 , in order to identify the constant as an octal number. Eg .: 01 ,0743 ,0551          

(iii) Hexadecimal Integer: A hexadecimal integer constant must begin with either o x or X . It can then be followed by any combination of digits taken from the sets through 9 and a through f (either upper- or lowercase). the letters a through f (or A through F ) represent the decimal quantities 10 through 15 , respectively E.g : x2,, X7FFF, xbcd

Floating point Constant(Real constants) A floating-point constant is a base 10 number that contains either a decimal point or an exponent (or both). floating point constants can be expressed in two forms Decimal notation Exponential notation

A floating point constant in decimal notation Having whole number followed by decimal point and fractional part with optional + or – sign. Examples +867.9, -26.9876

In exponential form , the real constant is represented as two parts. General form of floating point constant in exponential form is mantissa e exponent The part before the ‘ e ’ is the ‘ mantissa’, and the one following ‘e’ is the ‘ exponent’. Mantissa is either a real number expressed in decimal notation or an integer. Exponent is an integer number with optional plus or minus sign. 1.6667E+8, 0.006e-3

Single Character constant A character constant is a single character, enclosed in single quotation marks. Eg :  ’B’,  ’?’,    ’3’  Character constants have integer values known as ASCII values(example ‘A’ have the value 65)

Escape Sequences Certain nonprinting characters as well as the backslash(\) and apostrophe(‘),can be expressed in terms of escape sequences. An escape sequence always begins with a backward slash and is followed by one or more special characters.

The commonly used escape sequences are listed below.

\a   is used to give an audible alert or beep sound. \b   is used to delete one character to the left of cursor , equivalent to backspace. \f  is used for form feed , or to feed a blank page when printing documents. \t   is used for horizontal tab , or to move the cursor to next tab stop position. \n   is used to go to the start of next line \v   is used to Go to the start of the line at the next vertical tab position. \r   is used for  carriage  return , or Go back to the start of the current line. \\   is used to print a backslash. \’   Is used to print a single quote. \”   is used to print a double quote.

null character \0 The escape sequence \0. This represents the null character (ASCII OOO), which is used to indicate the end of a string . Note that the null character constant \0 is not equivalent to the character constant ‘ 0’ .

String Constants A string constant is a sequence of characters enclosed in double quotation marks. Eg : "green" "Washington, D.C. 20005H “ “270-32-3456“

C Variables A variable is an identifier that is used to represent a single data item; i.e a numerical quantity or a character constant. A variable may take different values at different times during the execution. However datatype associated with the variable cannot change. It is a good practice to choice a meaningful variable names. For example average , total

Rules for Constructing Variable Names They must begin with a letter or underscore. the maximum allowable length of a variable name is 31 characters. However length should not be normally more than 8 characters. Uppercase and lowercase are significant. That is, the variable  Amount    is not the same as  amount  or  AMOUNT. It should not be a keyword. White space is not allowed.

ARRAYS An array is an identifier that refers to a collection of data items that all have the same name . The data items must all be of the same type (e.g., all integers, all characters, etc.). The individual data items are represented by their corresponding array-elements

The individual array elements are distinguished from one another by the value that is assigned to a subscript or index . The subscript associated with each element in an array is shown in square braces. Value of subscript for first element is 0,second element is 1 and so on. For example A is 5 element array. First element referred by A[0],second by A[1],last element by A[4]

For an n-element array, the subscripts w ill range from 0 to n-1 Note that an n-character string will require an (n+1 ) element array, because of the null character (\0) that is automatically placed at the end of the string.

Suppose that the string "California" is to be stored in a one-dimensional character array called l e t t e r . " C a l i f o r n i a " contains 10 characters, letter will be an 11-element array.

DECLARATIONS A declaration associates a group of variables with a specific data type. All variables must be declared before they can appear in executable statements. A declaration consists of a data type, followed by one or more variable names, ending with a semicolon. datatype v1, v2, v3…. vn ; V1,v2,…are variable names Each array variable must be followed by a pair of square brackets, containing a positive integer which specifies the size (i.e., the number of elements) of the array.

example of type declarations i n t a, b ; f l o a t ro ot ; char f l a g , t e x t [ 8 0 ] ; a and b are declared to be integer variables root is a floating-point variable f l a g is a char-type variable and t e x t is an 80-element, char-type array. We can use type qualifiers in variable declaration like short int a, b

Intial values of variables can be assigned to variables within type declaration. Then declaration must consist of a data type, followed by a variable name, an equal sign (=) and a constant of the appropriate type. For examples i n t c = 12; char star = " * ”;

Expressions An expression represents a single data item, such as a number or a character. The expression may consist of a single entity, such as a constant, a variable, an array element or a reference to a function. It may also consist of some combination of such entities, interconnected by one or more operators. Expressions can also represent logical conditions that are either true or false. in C the conditions true and false are represented by the integer values 1 and 0 ,respectively Expressions in C are basically operators acting on operands. In C every expression evaluates to a value i.e., every expression results in some value of a certain type that can be assigned to a variable.

Examples of expression a + b represents sum of values of a and b x = y Value represented by y to be assigned to x c = a + b represents sum of values of a and b is assigned to variable c x <= y Will have value true if x is less than or equal to the value of y otherwise expression will have value 0 Expression meaning

A statement causes the computer to carry out some action. There are three different classes of statements in C. expression statements, compound statements control statements STATEMENTS

An expression statement consists of an expression followed by a semicolon; The execution of an expression statement causes the expression to be evaluated. Example of expression statement c= a+b ; Expression statement

compound statement A compound statement consists of several individual statements enclosed within a pair of braces { }. The individual statements may themselves be expression statements, compound statements or control statements. Unlike an expression statement, a compound statement does not end with a semicolon. Example of compound statement { p i = 3.141593; area = p i * radius * radius; } This compound statement consists of 2 expression statements

Control statements Control statements are used to create special program features, such as logical tests, loops and branches

L-Value and R-Value There are 2 values with a variable Its location value(L value) that is the address in memory at which its data value is stored. Its data value stored at some location in memory referred to as r value. Rvalue of A=10 Rvalue of C=25 Lvalue of A=1052 Lvalue of C=1054

Lvalue is an expression to which you can assign a value. L value and R value refer to on the left and right side of the assignment operator. The  Lvalue  concept refers to the requirement that the operand on the left side of the assignment operator is modifiable, usually a variable.   l-values can be made nonmodifiable by using the  const keyword; Rvalue  concept pulls or fetches the value of the expression or operand on the right side of the assignment operator. l-values are r-values but not all r-values are l-values.

 consider a=b; b,on the right hand of assignment operator is the quantity to be found at the address associated with b i.e an rvalue a ,on the left hand side is assigned an value stored in the address associated with b so a is an lvalue Assignment operation deposits b’s rvalue at a’s lvalue

SYMBOLIC CONSTANTS A symbolic constant is a name that substitutes for a sequence of characters. a symbolic constant allows a name to appear in place of a numeric constant, a character constant or a string. When a program is compiled, each occurrence of a symbolic constant is replaced by its corresponding character sequence. Symbolic constants are usually defined at the beginning of a program.

General format of symbolic constant is #define name text Name symbolic name typically written in uppercase letters Text represents sequence of characters that is associated with the symbolic name. Text does not end with a semicolon example #define PI 3.141593

the symbolic names are written in uppercase , to distinguish them from ordinary C identifiers. It is much easier to change the value of a single symbolic constant than to change every occurrence of some numerical constant that may appear in several places within the program.