×
Don't wait for the right time for studying... It won't come.

# Note for Programming in C - C by Bhubanjit Sahu

• Programming in C - C
• Note
• Biju Patnaik University of Technology BPUT - BPUT
• 17 Topics
• 42 Views
0 User(s)

#### Text from page-1

Programming Techniques Programming Techniques The actions of a computer are directed by individual computer instructions that operate on input data and convert it into meaningful output information. INPUT Process OUTPUT Example: 5, 10 Multiply 50 Algorithm An algorithm is a set of ordered statements written in English language to solve a particular problem. An algorithm is a well-defined procedure that allows a computer to solve a problem. To write a logical step-by-step method to solve the problem is called algorithm, in other words, an algorithm is a procedure for solving problems. Characteristics of an Algorithm An algorithm is a well–defined sequence of computational steps that takes some value, or set of values, as input and produces some value, or set of values as output, for solving a particular problem. Every algorithm must satisfy the following criteria: 1) Input/Output: It must have I/O statement. There are some input data which are externally supplied to the algorithm, and there will be at least one output as a result. 2) Definiteness: There should not be any ambiguity. Each instruction/step of the algorithm must be clear and unambiguous. 3) Finiteness: The statements should be precise enough to be executed within a finite time. The total algorithm should be completed within finite time. 4) Effectiveness: The steps of an algorithm must be sufficiently clear idea that it can be carried out by a person mechanically using a pen and paper. Steps to develop an Algorithm In order to develop an algorithm, the problem has to be properly analyzed. The programmer has to understand the problem and come out with a simple logic and that must be adaptable on a computer. 1) Understand the problem. 2) Identify the output of the problem. 3) Identify the inputs required by the problem to achieve he desired output. 4) Design a logic or process that will produce the required output from the given inputs 5) Test the algorithm for a given set of input data. 6) Repeat steps 1 to 5 till the algorithm produces the desired results. Algorithmic Language (PSEUDOCODE) Although several tools are used to define algorithms, one of the most common is pseudocode. Pseudocodes are user’s prepared unambiguous sentence that describes the procedure to solve a particular problem.  Pseudocodes are modern alternatives of traditional flow chart.  Pseudocode is an English–like representation of the algorithm logic. It is part English and part structured code. The English part provides a relaxed syntax that describes what must be done without showing unnecessary details such as error messages. The code part consists of an extended version of the basic algorithmic constructs– sequence, selection and iteration. 1 K. L. Narayana

#### Text from page-2

Programming Techniques Components of the Algorithm 1) Algorithm Header: Each algorithm begins with a header that names it, lists its parameters, and describes any preconditions and postconditions. Example: ALGORITHM FACTORIAL(DATA) 2) Purpose, Conditions and Return: The purpose is a short statement about what the algorithm does. The precondition lists any precursor requirements for the parameters. For example, the algorithm that calls the FACTORIAL must pass the DATA as a preconditions. The post condition identifies any action taken and the status of any output parameters. If a value is returned it is identified by a return condition. 3) Statement Numbers: Statements are numbered using an abbreviated decimal notation. 4) Variables: To ensure that the menaing is understood, we use intelligent data names– that is names that describe the meaning of the data. Do not use generic names (eg. count) in programs. Rather add an intelligent qualifier to the generic names for example, studcount instead of count. 5) Statement Constructs: Any algorithm could be written using only three programming constructs: sequence, selection and loops. a. Sequence: Sequence is a one or more statements that do not alter the execution path within an algorithm. b. Selection: A selection statement evaluates a condition and executes zero or more alternatives. The two–way selection is the if statement, and multi–way statement is the switch–case statement. c. Loop: Loop statement itrates a block of code. Ex: while, for and do–while statements Data Handling Data is simply a value or set of values. Data is information inside a computer. The main function of a computer program is to process data.  Accepts data as its input  Carries out a set of processes on data  Stores data for future use  Presents the results of this processing as its output Data which is predetermined before a program is used. Information may exist in many forms. Values remain unchanged during the execution of a program is known as constants. Have a fixed range in computer applications Types: integer constants real/floating point constants character constants string constants Integer Constants  Consists of digits 0-9, may have a leading + or –  Do not contain a decimal point, comma or space  Must be within minimum and maximum limits specified by machine operating system and language  Range of integers in 16 bit compiler Signed integer: –32768 to 32767 and Unsigned integers: 0 to 65535 Valid integer constants: 0, 32767, –32456, +9789, 0654 Invalid integer constants: 2•5 Must not contain a decimal point 40,000 Comma invalid – 786 space is not allowed between sign and the digit 2 K. L. Narayana

#### Text from page-3

Programming Techniques Real and floating point Constants  Consists of digits 0-9  May have a leading or trailing decimal point  Must contain a decimal point  Range for microprocessor based computers +/- (10 ** –38 to 10 ** +38) approximately  No other characters, especially comma or space are allowed Valid floating constants: 34.567, 0.783, •5, –15.3, 10•, 0•007 Invalid floating constants: –45,250.2 Comma not allowed 25 468 Space not allowed Character constants  Enclosed within single quotes( ‘ ‘)  Any character on the keyboard is valid character constant. Valid character constants: 'A ' , '9 ', '+ ', '\$ ', ' ' String constants  String constants can also be enclosed within double quotes Valid string constants: "James ", "Bush2 ", "Jocky ", "Lakshmi Narayana", " " Variables and it’s Names In computer programming, a variable is a storage location and an associated symbolic name (an identifier) which contains some known or unknown quantity or information, a value. Every variable has a name, called the variable name, and a data type. A variable's data type indicates what sort of value the variable represents, such as whether it is an integer, a floating-point number, or a character. Rules for Naming a variables: a) Limitations imposed by language on maximum number of characters used to form variable names b) Alphabetic, numeric characters allowed in variable names c) Space and other special characters not allowed in variable name d) Special characters are not allowed except ‘_’ (underscore) e) Specific rules applied by different languages Flow Chart A Flow Chart is a pictorial representation of programming logic. In simple words we can say that the flow chart is like a road map which provides a detailed description of the flow of processing of data.  Pictorial description of program logic  Separation of logic development and program syntax activity  Clarifies step by step logic or each program  Symbolic representation of each input, output and processing steps  Part of program documentation  Helps understanding, debugging and maintenance of programs As flowchart is a pictorial representation of program logic, boxes of different shapes are used to denote different operations. The actual operations are stated within the boxes. The boxes are connected by flow lines indicating direction of flow. 3 K. L. Narayana

#### Text from page-4

Programming Techniques Flow chart symbols suggested by ANSI are: Terminal Symbol used for start and stop Input / Output Symbol Processing Symbol Decision Symbol Flow Symbols Connector Symbol Flowcharts contains the following Facilities To indicate actual operations to be performed statements and operations are used to form instructions which are written in the respective symbols. Different operators and statements used in flowcharts are: Operators  Assignment  Arithmetic  Relational  Logical Assignemnt Operators Assignment operators assign a certain value to a memory variable. Variable-name Argument Example: A 25 Arithmetic operators Arithmetic operators – used for performing computations + Addition, - Subtraction, * Multiplication, / Division Relational operators Relational operators used for comparing values and results in TRUE/FALSE values. = Equal to < Less than > Greater than ≥ Greater than or equal to ≤ Less than or equal to <> Not Equal to Statements for Basic Logic Constructs  Input  Output  Sequence  Selection  Iteration/Loops 4 K. L. Narayana