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
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 . . . . . . . . . . . .
// 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
Thank you for interesting in our services. We are a non-profit group that run this website to share documents. We need your help to maintenance this website.