--Your friends at LectureNotes

Note for Programming in C - C by Bhubanjit Sahu

  • Programming in C - C
  • Note
  • Biju Patnaik University of Technology BPUT - BPUT
  • 17 Topics
  • Uploaded 6 months ago
0 User(s)
Order Printed Copy

Share it with your friends

Leave your Comments

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

Text from page-5

Programming Techniques Input This statement is used to get the data for processing from any input source like keyboard or file. The verb used to get data from keyboard is ACCEPT or INPUT Syntax: ACCEPT Variable name or INPUT Variable name When this statement is encountered in the program user is prompted to enter value for corresponding variables which get stored in the variables mentioned in the statement. Representation in flowchart Input Marks Output These statements are used to display or print information on VDU(Monior) or printer. The verbs used for this are: Syntax: DISPLAY/PRINT Variables Values When these statements are encountered in the program, constant values or values of variables are displayed or printed on the screen/printer. Representation in Flowchart Display ERROR Print Roll, Name Display Avg_marks Program Structure A program is a sequence of instructions, written to perform a specified task with a computer. It depends on various factors: 1) Programming Instructions 2)Problem on Hand 3) Type of processing Programming Instructuions Instructions to be used in the program are depends on the type of language selected. 1) Structure of instructions: The instructions are simple or difficult to understand. For example, to represent the operation of incrementing a count. In COBOL, the statement is : ADD 1 TO COUNT In C Programming, the statement is: COUNT = COUNT + 1; 2) General layout of the program. Some languages are free-format languages and independent rules can be used freely, blank lines can be left at logical separations to enhance readability. Whereas in other languages, fixed format is to be followed and such flexibilities are not allowed i.e. ‘PASCAL’ is a free format language and ‘GWBASIC’ is a fixed format language. 3) Size of variables: Some languages allow the variable names to be given using 2 characters, some others allow 6 and COBOL allows 30 characters. The more the number of characters allowed, more meaningful names can be given. 5 K. L. Narayana

Lecture Notes