Programming in Ansi C_E. Balagurusamy

January 18, 2017 | Author: amitukumar | Category: N/A
Share Embed Donate


Short Description

Download Programming in Ansi C_E. Balagurusamy...

Description

Scilab Textbook Companion for Programming In Ansi C by E. Balagurusamy1 Created by Devender Thakur computer science Computer Engineering Shoolini University, Solan College Teacher Mr. Amit Nayyar Cross-Checked by

October 7, 2013

1 Funded

by a grant from the National Mission on Education through ICT, http://spoken-tutorial.org/NMEICT-Intro. This Textbook Companion and Scilab codes written in it can be downloaded from the ”Textbook Companion Project” section at the website http://scilab.in

Book Description Title: Programming In Ansi C Author: E. Balagurusamy Publisher: Tata McGraw - Hill Education, New Delhi Edition: 4 Year: 2008 ISBN: 978-0-07-064822-7

1

Scilab numbering policy used in this document and the relation to the above book. Exa Example (Solved example) Eqn Equation (Particular equation of the above book) AP Appendix to Example(Scilab Code that is an Appednix to a particular Example of the above book) For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 means a scilab code whose theory is explained in Section 2.3 of the book.

2

Contents List of Scilab Codes

4

1 Overview of C

7

2 Constants Variables and Data Types

11

3 Operators and Expressions

16

4 Managing Input and Output Operations

22

5 Decision Making and Branching

31

6 Decision Making and Looping

40

7 Arrays

51

8 Character Arrays and Strings

65

9 User Defined Functions

76

10 Structures and Unions

87

12 File Management in C

95

13 Dynamic Memory Allocation and linked Lists

3

104

List of Scilab Codes Exa Exa Exa Exa Exa Exa Exa Exa

1.3 1.4 1.5 1.6 1.7 2.1 2.1cs 2.2

Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa

2.2cs 2.3 2.4 3.1 3.1cs 3.2 3.2cs 3.3 3.4 3.5 3.6 4.1 4.1cs 4.2

Exa Exa Exa Exa

4.2cs 4.3 4.4 4.5

Printing a mesage . . . . . . . . . . . . . . . . . . . . Adding two numbers . . . . . . . . . . . . . . . . . . . Interest Calculation . . . . . . . . . . . . . . . . . . . Use of subroutines . . . . . . . . . . . . . . . . . . . . Use of math functions . . . . . . . . . . . . . . . . . . Representation of integer constants on a 16 bit computer Case study 1 avg of numbers . . . . . . . . . . . . . . typical declarations assignments and values stored in various types of variables . . . . . . . . . . . . . . . . Case study temprature in Farenheit and Celsius . . . . use of scanf function . . . . . . . . . . . . . . . . . . . Interest calculation using scanf . . . . . . . . . . . . . Use of integer arithmetic . . . . . . . . . . . . . . . . case study 1 salesmans salary . . . . . . . . . . . . . . Sequence of squares of numbers . . . . . . . . . . . . . case study 2 solution of the quadratic equation . . . . Different kind of operators . . . . . . . . . . . . . . . . Use of variables in expressions . . . . . . . . . . . . . Round off errors . . . . . . . . . . . . . . . . . . . . . Cast to evaluate the equation . . . . . . . . . . . . . . Use of getchar function . . . . . . . . . . . . . . . . . Case study 1 Inventory report . . . . . . . . . . . . . . Whether character is alphabet or digit or special character . . . . . . . . . . . . . . . . . . . . . . . . . . . . Case study 2 Reliability graph . . . . . . . . . . . . . Print character in reverse case . . . . . . . . . . . . . Input formatting options . . . . . . . . . . . . . . . . Reading of real numbers . . . . . . . . . . . . . . . . . 4

7 7 8 9 9 11 12 12 13 14 14 16 16 17 18 18 19 20 21 22 23 24 25 25 26 27

Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa

4.6 4.8 4.9 4.10 4.11 5.1 5.1cs 5.2 5.2cs 5.3 5.4 5.5 5.6 5.7 6.1 6.1cs 6.2 6.2cs 6.3 6.3cs 6.4 6.4cs 6.5 6.6 6.7 1.cs 2.cs 3.cs 4.cs 7.1 7.2 7.3 7.4 7.5 1.cs 2.cs 8.1 8.2

Reading of strings . . . . . . . . . . . . . . . . . Testing for correctness . . . . . . . . . . . . . . . Output of integer numbers . . . . . . . . . . . . Printing a real number . . . . . . . . . . . . . . . Printing of character and strings . . . . . . . . . Ratio . . . . . . . . . . . . . . . . . . . . . . . . Case study 1 range of numbers . . . . . . . . . . counts the number of boys . . . . . . . . . . . . . Case study 2 Pay Bill Calculations . . . . . . . . Evaluate the power series . . . . . . . . . . . . . Largest of the three numbers . . . . . . . . . . . Reads the customer number and power consumed Loan applications and to sanction loans . . . . . square root for five numbers . . . . . . . . . . . . evaluate the equation . . . . . . . . . . . . . . . Case study 1 Table of Binomial Coefficients . . . Multiplication table . . . . . . . . . . . . . . . . Case study 2 Histogram . . . . . . . . . . . . . . Uses a for loop . . . . . . . . . . . . . . . . . . . Case study 3 Minimum Cost . . . . . . . . . . . Read the marks and print total marks . . . . . . Case study 4 Plotting of two Functions . . . . . Use of the break statement . . . . . . . . . . . . Evaluate the series . . . . . . . . . . . . . . . . . Use of continue statement . . . . . . . . . . . . . Case study 1 Median of list of numbers . . . . . Case study 2 Calculation of standard deviation . Case study 3 Evaluating a Test . . . . . . . . . . Case study 4 Production and sales analysis . . . Sum of squares of 10 numbers . . . . . . . . . . . Count the number of students . . . . . . . . . . . Compute and print . . . . . . . . . . . . . . . . . Multiplication table . . . . . . . . . . . . . . . . Popularity of various cars . . . . . . . . . . . . . Case study 1 Counting words in a text . . . . . . Case study 2 Processing of a customer list . . . . Read a series of words . . . . . . . . . . . . . . . Read a line of text . . . . . . . . . . . . . . . . . 5

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

27 28 29 29 30 31 32 33 33 35 36 37 37 38 40 40 42 42 43 44 45 46 48 48 49 51 52 53 54 58 59 60 62 62 65 66 68 68

Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa

8.3 8.4 8.5 8.6 8.7 8.8 8.9 1.cs 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.16 1.cs 10.1 10.2 10.3 10.4 10.5 12.1 12.2 12.3 12.4 12.5 12.6 1.cs 2.cs 13.3 13.4 13.5

Copy one string into another . . . . . . . . . . . . . . Display the string under various format specifications . Program using for loop . . . . . . . . . . . . . . . . . Print the alphabet set a to z and A to Z . . . . . . . . Concatinate the three parts into one string . . . . . . Compare whether strings are equal . . . . . . . . . . . Sort a list of names in alphabetical order . . . . . . . Case study 1 Calculation of Area under a Curve . . . Multiple functions . . . . . . . . . . . . . . . . . . . . Include arguments in function calls . . . . . . . . . . . Return result . . . . . . . . . . . . . . . . . . . . . . . Computes x raised to the power y . . . . . . . . . . . Calculate standard deviation of an array values . . . . Sort an array . . . . . . . . . . . . . . . . . . . . . . . Autometic variables . . . . . . . . . . . . . . . . . . . Global variables . . . . . . . . . . . . . . . . . . . . . Factorial of a number using recursion . . . . . . . . . . Case study 1 Book Shop Inventory . . . . . . . . . . . Define a structure type . . . . . . . . . . . . . . . . . Comparison of structure variables . . . . . . . . . . . Calculate the subject wise and student wise totals . . Array member to represent the three subjects . . . . . structure as a parameter to a function . . . . . . . . . Read data from keyboard and write it to a file . . . . Read and write odd and even numbers . . . . . . . . . Read and write data to and from the file INVENTORY Error handling in file operations . . . . . . . . . . . . use of function ftell or mtell and fseek or mseek . . . . Append additional items to the file INVENTORY . . Case study 1 Insertion in a sorted list . . . . . . . . . Case study 2 Building a Sorted List . . . . . . . . . . Create a linear linked list . . . . . . . . . . . . . . . . Insert the item before the specified key node . . . . . . Delete a specified node in the list . . . . . . . . . . . .

6

69 69 70 71 72 73 74 76 77 78 79 80 81 82 83 84 85 87 89 90 91 92 93 95 96 98 99 100 102 104 107 109 111 113

Chapter 1 Overview of C

Scilab code Exa 1.3 Printing a mesage 1 2 3 4 5

// Example 1 . 3 // SAMPLE PROGRAM 1 : PRINTING A MESSAGE

// P r i n t i n g B e g i n s printf ( ” I s e e , I remember ” ) // P r i n t i n g u s i n g p r i n t f ( ) function 6 // P r i n t i n g e n d s 7 8 9

//We can a l s o p r i n t a m e s s a g e u s i n g d i s p ( ) f u n c t i o n in s c i l a b 10 disp ( ” I s e e , I remember ” ) // P r i n t i n g u s i n g d i s p ( ) function

Scilab code Exa 1.4 Adding two numbers

7

1 2 3 4 5 6

// Example 1 . 4 // SAMPLE PROGRAM 2 : ADDING TWO NUMBERS

number =100; amount =30.75+75.35 ; // A d d i t i o n o f two numbers disp ( number ) ; // D i s p l a y v a l u e o f number variable 7 // D i s p l a y v a l u e o f amount i n f l o a t i n g p o i n t w i t h f i v e p l a c e s i n a l l and two p l a c e s t o t h e r i g h t o f decimalpoint 8 printf ( ”%5 . 2 f ” , amount ) ;

Scilab code Exa 1.5 Interest Calculation 1 // Example 1 . 5 2 // SAMPLE PROGRAM 3 : INTEREST CALCULATION 3 4 PRINCIPAL =5000.00; 5 PERIOD =10; 6 // A s s i g n m e n t S t a t e m e n t s 7 amount = PRINCIPAL ; 8 inrate =0.11; 9 year =0; 10 // Computation u s i n g w h i l e l o o p 11 while ( year
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF