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 <= PERIOD ) 12 printf ( ”%2d %8 . 2 f \n ” , year , amount ) ; 13 value = amount + inrate * amount ; 14 year = year +1; 15 amount = value ; 16 end // End o f w h i l e l o o p 8 Scilab code Exa 1.6 Use of subroutines 1 2 3 4 5 6 7 8 9 10 11 12 // Example 1 . 6 // SAMPLE PROGRAM 4 : USE OF SUBROUTINES // Program u s i n g f u n c t i o n function []= mul (a , b ) // mul ( ) f u n c t i o n s t a r t s ( i . e . definition starts ) y=a*b; printf ( ” M u l t i p l i c a t i o n o f %d and %d i s %d” ,a ,b , y ) ; endfunction // mul ( ) f u n c t i o n e n d s a =5; b =10; // C a l l i n g mul ( ) f u n c t i o n mul (a , b ) Scilab code Exa 1.7 Use of math functions 1 2 3 4 5 6 7 8 9 // Example 1 . 7 // SAMPLE PROGRAM 5 : USE OF MATH FUNCTIONS // Program u s i n g c o s i n e f u n c t i o n angle =0; MAX =180; printf ( ” Angle Cos ( a n g l e ) \n ” ) ; while ( angle <= MAX ) x =( %pi / MAX ) * angle ; y = cos ( x ) ; // Use o f c o s i n e f u n c t i o n 9 10 11 12 printf ( ”%15d %13 . 4 f \n ” , angle , y ) ; angle = angle +10; end 10 Chapter 2 Constants Variables and Data Types Scilab code Exa 2.1 Representation of integer constants on a 16 bit computer 1 2 // Example 2 . 1 // R e p r e s e n t a t i o n o f i n t e g e r c o n s t a n t s on a 16− b i t computer . 3 4 5 disp ( ” I n t e g e r v a l u e s ” ) ; // I n t e g e r v a l u e s l a r g e r t h a n 3 2 7 6 7 a r e n o t s t o r e d p r o p e r l y on 16− b i t machine 6 printf ( ”%d %d %d \n ” , int16 (32767) , int16 (32767+1) , int16 (32767+10) ) ; 7 8 9 disp ( ” Long i n t e g e r v a l u e s ” ) ; //To s t o r e l o n g i n t e g e r s p r o p e r l y , u s e i n t 3 2 i n t e g e r type 10 printf ( ” %ld %ld %ld \n ” , int32 (32767) , int32 (32767+1) , int32 (32767+10) ) ; 11 // The same r e s u l t a s from a b o v e s t a t e m e n t can be a c h i e v e d d i r e c t l y from b e l o w commented s t a t e m e n t 12 // p r i n t f ( ” %ld %ld %ld \n ” , 3 2 7 6 7 , 3 2 7 6 7 + 1 , 3 2 7 6 7 + 1 0 ) ; 11 Scilab code Exa 2.1cs Case study 1 avg of numbers 1 // Case Study :− C h a p t e r 2 Page No. −47 2 // 1 . C a l c u l a t i o n o f A v e r a g e o f numbers 3 4 N =10; sum1 =0; count =0; // I n i t i a l i z a t i o n 5 6 7 8 9 10 11 12 13 of variables printf ( ” E n t e r t e n numbers ” ) ; while ( count < N ) number = scanf ( ” %f ” ) ; // R e a d i n g number ( u s i n g scanf () function ) sum1 = sum1 + number ; count = count +1; end average = sum1 / N ; // Avarage i s c a l c u l a t e d printf ( ” N = %d Sum1 = %f ” ,N , sum1 ) ; printf ( ” A v e r a g e = %f ” , average ) ; Scilab code Exa 2.2 typical declarations assignments and values stored in various types of variables 1 2 // Example 2 . 2 // Program shows t y p i c a l d e c l a r a t i o n s , a s s i g n m e n t s and v a l u e s s t o r e d i n v a r i o u s t y p e s o f v a r i a b l e s . 3 4 // D e c l a r a t i o n s and A s s i g n m e n t s 5 m = int16 (54321) ; 12 6 n = int32 (1234567890) ; 7 k = uint16 (54321) ; 8 // A s s i g n m e n t s 9 x =1.234567890000; 10 11 12 13 14 15 16 17 18 19 // B y d e f a u l t t y p e i s d o u b l e in s c i l a b y =9.87654321; // −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− p =1.0; q =1.0; // P r i n t i n g printf ( ” m=%d\n ” ,m ) printf ( ” n=%ld \n ” ,n ) printf ( ” x=%. 1 2 f \n ” ,x ) printf ( ” x=%f\n ” ,x ) printf ( ” y=%. 1 2 f \n ” ,y ) printf ( ” y=%f\n ” ,y ) printf ( ” k=%u p=%f q=%. 1 2 f \n ” ,k ,p , q ) Scilab code Exa 2.2cs Case study temprature in Farenheit and Celsius 1 2 3 4 5 6 7 8 9 10 11 Case Study :− C h a p t e r 2 Page no . −48 2 . S o l u t i o n o f t e m p r a t u r e i n F a r e n h e i t and Celsius F_LOW =0; F_MAX =250; STEP =25; fahrenheit = F_LOW ; // I n i t i a l i z a t i o n printf ( ” F a h r e n h e i t C e l s i u s \n\n ” ) ; while ( fahrenheit < = F_MAX ) celsius =( fahrenheit -32.0) /1.8; // c o n v e r s i o n from F a r e n h e i t t o C e l s i u s printf ( ”%6 . 2 f %7 . 2 f \n ” , fahrenheit , celsius ) ; fahrenheit = fahrenheit + STEP ; // // 13 12 end Scilab code Exa 2.3 use of scanf function 1 2 3 4 5 6 7 8 9 // Example 2 . 3 // The program i l l u s t r a t e t h e u s e o f s c a n f ( ) function disp ( ” E n t e r an i n t e r g e r number : ” ) ; number = scanf ( ”%d” ) ; // Read from k e y b o a r d if ( number <100) then disp ( ” Your number i s s m a l l e r t h a n 100 ” ) ; else disp ( ” Your number c o n t a i n more t h a n two d i g i t s ” ) ; end Scilab code Exa 2.4 Interest calculation using scanf 1 2 3 4 5 6 7 8 // Example 2 . 4 // Sample program 3 ( exm1 . 5 ) d i s c u s s e d i n c h a p t e r 1 can be c o n v e r e d i n t o a more f l e x i b l e i n t r a c t i v e program u s i n g s c a n f ( ) f u n c t i o n disp ( ” E n t e r i n s i n g l e l i n e s e p a r t e d by s p a c e ” ) ; disp ( ” I n p u t amount , i n t e r e s t r a t e , and p e r i o d ” ) ; [ amount , inrate , period ]= scanf ( ” %f %f %d” ) ; // u s e of scanf () year =1; // Computation u s i n g w h i l e l o o p while ( year <= period ) 14 9 10 11 12 13 end value = amount + inrate * amount ; printf ( ”%2d Rs %8 . 2 f \n ” , year , value ) year = year +1; amount = value ; 15 Chapter 3 Operators and Expressions Scilab code Exa 3.1 Use of integer arithmetic // Example 3 . 1 // The program shows t h e u s e o f i n t e g e r a r i t h m e t i c t o c o n v e r t a g i v e n number− 3 // o f d a y s i n t o months and d a y s 1 2 4 5 6 7 8 days = input ( ’ E n t e r d a y s : ’ ) ; months = int16 ( days /30) ; // Compute f o r months days = int16 ( pmodulo ( days ,30) ) ; // compute f o r d a y s disp ( days , ” Days =” , months , ” Months =” ) ; Scilab code Exa 3.1cs case study 1 salesmans salary 1 2 3 4 5 // // Case Study :− C h a p t e r 3 , Page No : 7 6 1 . Salesman ’ s S a l a r y BASE_SALARY =1500.00; // Minimum b a s e s a l a r y 16 6 7 8 9 10 11 12 13 14 15 16 BONUS_RATE =200.00; // Bonus f o r e v e r y c o m p u t e r sold COMMISSION =0.02; // Commission on t o t a l monthly sales printf ( ” I n p u t number s o l d and p r i c e \n [ E n t e r i n s i n g l e l i n e s e p a r a t e d by s p a c e ] ” ) ; [ quantity , price ] = scanf ( ”%d %f ” ) ; // I n p u t q u a n t i t y and p r i c e // Computation f o r bonus , c o m m i s s i o n and g r o s s s a l a r y of a salesman bonus = BONUS_RATE * quantity ; commission = COMMISSION * quantity * price ; gross_salary = BASE_SALARY + bonus + commission ; printf ( ” Bonus = %6 . 2 f \n ” , bonus ) ; printf ( ” Commission = %6 . 2 f \n ” , commission ) ; printf ( ” G r o s s salary = %6 . 2 f \n ” , gross_salary ) ; Scilab code Exa 3.2 Sequence of squares of numbers 1 // Example 3 . 2 2 // Program t o p r i n t a s e q u e n c e o f s q u a r e s o f numbers . 3 4 N =100; A =2; 5 a=A; 6 while (a < N ) 7 disp ( a ) ; // P r i n t s s q u a r e o f number 8 a = a ^2; // compute s q u a r e o f number 9 end 17 Scilab code Exa 3.2cs case study 2 solution of the quadratic equation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // // Case Study :− C h a p t e r 3 , Page No : 7 7 2. Solution of the Quadratic equation printf ( ” I n p u t v a l u e s o f a , b , and c \n ” ) ; a = input ( ” a =” ) ; b = input ( ” b =” ) ; c = input ( ” c =” ) ; discriminant = b ^2 -4* a * c ; if ( discriminant <0) printf ( ” \n\nROOTS ARE IMAGINARY\n ” ) ; else // Computes r o o t 1 and r o o t 2 root1 = ( - b + sqrt ( discriminant ) ) /(2.0* a ) ; root2 = ( - b - sqrt ( discriminant ) ) /(2.0* a ) ; printf ( ” \n\ nRoot1 = %5 . 2 f \n\ nRoot2 = %5 . 2 f \n ” , root1 , root2 ) ; 17 end Scilab code Exa 3.3 Different kind of operators 1 2 // Example 3 . 3 // The program e m p l o y s d i f f r e n t k i n d o f o p e r a t o r s . The r e s u l t s o f t h e i r e v a l u a t i o n a r e a l s o shown f o r comparison 3 18 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // I n c r e m e n t ( i . e . ++)/ Decrement (−−) o p e r a t o r s a r e unavailable in Scilab a = int16 (15) ; b = int16 (10) ; a = a +1; // R e p l a c e m e n t f o r ++a c =a - b ; printf ( ” a = %d b = %d c = %d\n ” ,a ,b , c ) ; b = b +1; // R e p l a c e m e n t f o r b++ d=b+a; printf ( ” a = %d b = %d d = %d\n ” ,a ,b , d ) ; printf ( ” a /b = %d\n ” ,a / b ) ; // Division operator printf ( ” pmodulo ( a , b ) = %d\n ” , pmodulo (a , b ) ) ; // Modulus o p e r a t i o r printf ( ” a ∗b = %d\n ” ,a * b ) ; // Multiplication // I n s c i l a b t h e r i s no c o n d i t i o n a l o p e r a t o r ( ? : ) , h e n c e ’ i f ’ can be u s e d i n p l a c e o f ? : if (c > d ) then disp (1) ; end if (c < d ) then disp (0) ; end Scilab code Exa 3.4 Use of variables in expressions 1 2 // Example 3 . 4 // The program i l l u s t r a t e s t h e u s e o f v a r i a b l e s i n e x p r e s s i o n s and t h e i r e v a l u a t i o n . 3 4 a =9; b =12; c =3; 5 // E x p r e s s i o n s and t h e i r evaluations 19 6 x =a - b /3+ c *2 -1; 7 y =a - b /(3+ c *(2 -1) ) ; 8 z =a -( b /(3+ c ) *2) -1; 9 10 printf ( ” x=%f\n ” ,x ) 11 printf ( ” y=%f\n ” ,y ) 12 printf ( ” z=%f\n ” ,z ) 13 // d i s p can be u s e d . . 14 // d i s p ( x , ” x =”) 15 // d i s p ( y , ” y =”) 16 // d i s p ( z , ” z =”) Scilab code Exa 3.5 Round off errors 1 2 // Example 3 . 5 // Output o f program shows round− o f f e r r o r s t h a t can o c c u r i n c o m p u t a t i o n o f f l o a t i n g p o i n t numbers 3 4 //Sum o f n t e r m s o f 1/ n 5 count =1; 6 sum1 =0; 7 n = input ( ” E n t e r v a l u e o f n : ” ) ; 8 term =1.0/ n ; 9 while ( count <= n ) 10 sum1 = sum1 + term ; 11 count = count +1; 12 end 13 printf ( ”Sum= %f ” , sum1 ) ; 20 Scilab code Exa 3.6 Cast to evaluate the equation 1 // Example 3 . 6 2 // Program u s i n g a c a s t t o e v a l u a t e t h e e q u a t i o n . 3 4 sum1 =0; 5 for n = int8 (1:10) 6 sum1 = sum1 +1/ double ( n ) ; // c o n v e r s i o n from ’ i n t ’ 7 8 end to ’ double ’ or ’ f l o a t ’ printf ( ”%2d %6 . 4 f \n ” ,n , sum1 ) ; 21 Chapter 4 Managing Input and Output Operations Scilab code Exa 4.1 Use of getchar function // Exaymple 4 . 1 // The program shows t h e u s e o f g e t c h a r f u n c t i o n i n an i n t r a c t i v e e n v i r o n m e n t . 3 // I n S c i l a b i n p l a c e o f g e t c h a r f u n c t i o n s c a n f f u n c t i o n can be u s e d t o g e t 4 // c h a r a c t e r a s t h e r e i s no g e t c h a r f u n c t i o n i n Scilab . 1 2 5 6 7 8 9 10 11 12 13 disp ( ” Would you l i k e t o know my name ? ” ) ; disp ( ” Type Y f o r YES and N f o r NO: ” ) ; answer = scanf ( ”%c” ) ; // R e a d i n g character if ( answer == ’Y ’ ) |( answer == ’ y ’ ) then // T e s t f o r answer disp ( ”My name i s BUSY BEE” ) ; else disp ( ”You a r e good f o r n o t h i n g ” ) end 22 Scilab code Exa 4.1cs Case study 1 Inventory report 1 // Case Study :− C h a p t e r 4 , Page No : 1 0 6 2 // 1 . I nv e nt o ry Report 3 4 ITEMS =4; 5 i =1; 6 printf ( ” [ E n t e r i n s i n g l e l i n e s e p e r a t e d be s p a c e s ] \ n 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ”); while ( i <= 4) printf ( ” E n t e r code , q u a n t i t y , and r a t e : ” ) ; [ code ( i ) , quantity ( i ) , rate ( i ) ]= scanf ( ”%s %d %f ” ) ; i = i +1; end printf ( ”INVENTORY REPORT\n ” ) ; printf ( ” −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n ” ); printf ( ” Code Quantity Rate V a l u e \n ” ) ; printf ( ” −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n ” ); total_value =0; i =1; while (i <= ITEMS ) value = quantity ( i ) * rate ( i ) ; printf ( ” %6s %10d %10 . 2 f %e\n ” , code ( i ) , quantity ( i ) , rate ( i ) , value ) ; total_value = total_value + value ; i = i +1; 23 23 end 24 printf ( ” 25 26 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\ n”); printf ( ” T o t a l V a l u e = %e\n ” , total_value ) ; printf ( ” −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\ n”); Scilab code Exa 4.2 Whether character is alphabet or digit or special character // Example 4 . 2 // The program r e q u e s t s t h e u s e r t o e n t e r a c h a r a c t e r and d i s p l a y a m e s s a g e on 3 // t h e s c r e e n t e l l i n g t h e u s e r w h e t h e r t h e c h a r a c t e r i s an a l p h a b e t o r d i g i t , 4 // o r any o t h e r s p e c i a l c h a r a c t e r . 1 2 5 6 7 8 9 10 11 12 13 14 disp ( ” P r e s s any key ” ) ; character = scanf ( ”%c” ) ; // Reading c h a r a c t e r if ( isletter ( character ) ) then // T e s t for letter disp ( ” The c h a r a c t e r i s a l e t t e r ” ) ; elseif ( isdigit ( character ) ) then // T e s t for digit disp ( ” The c h a r a c t e r i s a d i g i t ” ) ; else disp ( ” The c h a r a c t e r i s n o t a l p h a n u m e r i c ” ); end 24 Scilab code Exa 4.2cs Case study 2 Reliability graph 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // // Case s t u d y : c h a p t e r 4 2 . R e l i a b i l i t y Graph LAMBDA =0.001; for i =1:27 printf ( ”−−” ) ; end printf ( ” \n ” ) ; for t =0:150:3000 r = exp ( - LAMBDA * t ) ; R = int32 (50* r +0.5) ; printf ( ” | ” ) ; for i =1: R printf ( ” ∗ ” ) ; end printf ( ”#\n ” ) ; end for i =1:2 printf ( ” | \ n ” ) ; end Scilab code Exa 4.3 Print character in reverse case 1 2 // Example 4 . 3 // A program t h a t r e a d s a c h a r a c t e r from t h e k e y b o a r d and t h e n p r i n t i n r e v e r s e 25 3 4 5 6 7 8 9 10 // c a s e , t h a t i s , i f i n p u t i s i n u p p e r c a s e , t h e o u t p u t w i l l be l o w e r c a s e and v i c e −v e r s a disp ( ” E n t e r an a l p h a b e t ” ) ; alphabet = scanf ( ”%c” ) ; // R e a d i n g character if (( ascii ( alphabet ) ) >=97) then disp ( convstr ( alphabet , ” u” ) ) ; // R e v e r s e and display else disp ( convstr ( alphabet , ” l ” ) ) ; // R e v e r s e and display end Scilab code Exa 4.4 Input formatting options // Example 4 . 4 // The program i l l u s t r a t e s t h e v a r i o u s o p t i o n s f o r reading 3 // i n t e g e r s a r e e x p e r i m e n t e d i n t h i s program 1 2 4 5 6 printf ( ” E n t e r t h r e e i n t e g e r numbers \n ” ) ; 7 [n ,a ,b , c ]= mscanf ( ”%d %∗d %d” ) ; 8 disp (c ,b , a ) ; 9 printf ( ” E n t e r two 4− d i g i t numbers \n ” ) ; 10 [n ,x , y ]= mscanf ( ”%2d %4d” ) ; 11 printf ( ’%d %d\n ’ ,x , y ) ; 12 13 printf ( ” E n t e r two i n t e g e r s \n ” ) ; 14 [n ,a , x ]= mscanf ( ”%d %d” ) ; 15 printf ( ’%d %d\n ’ ,a , x ) ; 16 17 printf ( ” E n t e r a n i n e d i g i t number \n ” ) ; 26 18 [n ,p ,q , r ]= mscanf ( ”%3d %4d %3d” ) ; 19 printf ( ’%d %d %d\n ’ ,p ,q , r ) ; 20 21 printf ( ” E n t e r two t h r e e d i g i t numbers \n ” ) ; 22 [n ,x , y ]= mscanf ( ”%d %d” ) ; 23 printf ( ’%d %d \n ’ ,x , y ) ; Scilab code Exa 4.5 Reading of real numbers 1 2 // Example 4 . 5 // R e a d i n g o f r e a l numbers ( i n b o t h d e c i m a l p o i n t and exponential notation 3 4 printf ( ” V a l u e s o f x and y : ” ) ; 5 [x , y ]= scanf ( ” %f %e” ) ; // r e a d i n g x [ d e c i m a l p o i n t ] 6 7 8 9 10 and y [ e x p o n e n t i a l ] from s t a n d a r d i n p u t printf ( ” \n ” ) ; printf ( ” x = %f\ ny = %f\n ” ,x , y ) ; printf ( ” V a l u e s o f p and q : ” ) ; [p , q ]= scanf ( ” %lg %lg ” ) ; printf ( ” \ np = %. 1 2 f \ nq = %. 1 2 e \n ” ,p , q ) ; Scilab code Exa 4.6 Reading of strings 1 2 3 4 // // Example 4 . 6 R e a d i n g o f s t r i n g s u s i n g %wc and %ws . // s c a n f ( ) can o n l y r e a d one c h a r a c t e r a t a t i m e and r e t u r n i t t o name1 , t h e r e f o r e 27 // m s c a n f ( ) i s u s e d t h a t can r e a d more t h a n one character 6 printf ( ” E n t e r s e r i a l number and name one : ” ) ; 7 [n , no , name1 ]= mscanf ( ”%d %15c ” ) ; 8 printf ( ”%d %15s \n ” ,no , name1 ) ; 5 9 10 // Read and d i s p l a y a s t r i n g 11 printf ( ” E n t e r s e r i a l number and name two : ” ) ; 12 [ no , name2 ]= scanf ( ”%d %s” ) ; 13 printf ( ”%d %15s \n ” ,no , name2 ) ; 14 15 // Read and d i s p l a y a s t r i n g o f w i d t h 15 16 printf ( ” E n t e r s e r i a l number and name t h r e e : ” ) ; 17 [ no , name3 ]= scanf ( ”%d %15s ” ) ; 18 printf ( ”%d %15s ” ,no , name3 ) ; Scilab code Exa 4.8 Testing for correctness 1 2 3 4 5 6 7 8 9 10 11 // Example 4 . 8 // The program i l l u s t r a t e s t h e t e s t i n g f o r c o r r e c t n e s s o f r e a d i n g d a t a by s c a n f f u n c t i o n // I n S c i l a b m s c a n f f u n c t i o n i s s i m i l a r t o s c a n f function of c printf ( ” E n t e r v a l u e s o f a , b and c ” ) ; [n ,a ,b , c ]= mscanf ( ”%d %f %c” ) ; if ( n ==3) then // T e s t c o n d i t i o n printf ( ” a = %d b = %f c = %c” ,a ,b , c ) ; else printf ( ” E r r o r i n i n p u t . ” ) ; end 28 Scilab code Exa 4.9 Output of integer numbers 1 2 // Example 4 . 9 // The program i l l u s t r a t e s t h e o u t p u t o f i n t e g e r numbers u n d e r v a r i o u s f o r m a t s 3 4 m = int16 (12345) ; 5 n = int32 (987654) ; 6 printf ( ’%d\n ’ ,m ) ; 7 printf ( ’ %10d\n ’ ,m ) ; 8 printf ( ’ %010d \n ’ ,m ) ; 9 printf ( ’%−10d\n ’ ,m ) ; 10 printf ( ’ %10d\n ’ ,n ) ; 11 printf ( ’ %10d\n ’ ,-n ) ; // i n t e g e r v a r i a b l e m // i n t e g e r v a r i a b l e n Scilab code Exa 4.10 Printing a real number 1 2 // Example 4 . 1 0 // The program i l l u s t r a t e s a l l t h e o p t i o n s o f p r i n t i n g a r e a l number . 3 4 y =98.7654; // r e a l number 5 // V a r i o u s o p t i o n s o f p r i n t i n g a r e a l number 6 printf ( ’%7 . 4 f \n ’ ,y ) ; 7 printf ( ’ %f\n ’ ,y ) ; 8 printf ( ’%7 . 2 f \n ’ ,y ) ; 9 printf ( ’%−7.2 f \n ’ ,y ) ; 10 printf ( ’ %07 . 2 f \n ’ ,y ) ; 29 11 12 13 14 15 16 printf ( ’%7 . 2 f \n ’ ,y ) ; printf ( ’ \n ’ ) ; printf ( ’ %10 . 2 e \n ’ ,y ) ; printf ( ’ %12 . 4 e \n ’ ,-y ) ; printf ( ’%−10.2 e \n ’ ,y ) ; printf ( ’ %e\n ’ ,y ) ; Scilab code Exa 4.11 Printing of character and strings 1 2 // Example 4 . 1 1 // The program i l l u s t r a t e s t h e p r i n t i n g o f c h a r a c t e r and s t r i n g s . 3 4 x = ’A ’ ; // x v a r i a b l e h a s c h a r a c t e r 5 name = ’ ANIL KUMAR GUPTA ’ ; // name v a r i a b l e h a s s t r i n g 6 disp ( ”OUTPUT OF CHARACTERS” ) ; 7 printf ( ’ %c\n%3c\n%5c\n ’ ,x ,x , x ) ; 8 printf ( ’ %3c\n%c\n ’ ,x , x ) ; 9 printf ( ’ \n ’ ) ; 10 disp ( ”OUTPUT OF STRINGS” ) ; // d i s p can a l s o be u s e d 11 12 13 14 15 16 for display printf ( ’ %s\n ’ , name ) ; printf ( ’ %20s \n ’ , name ) ; printf ( ’ %20 . 1 0 s \n ’ , name ) ; printf ( ’%. 5 s \n ’ , name ) ; printf ( ’%−20.10 s \n ’ , name ) ; printf ( ’ %5s\n ’ , name ) ; 30 Chapter 5 Decision Making and Branching Scilab code Exa 5.1 Ratio // Example 5 . 1 // The program r e a d s f o u r v a l u e s a , b , c and d from t h e t e r m i n a l and 3 // e v a l u a t e s t h e r a t i o o f ( a+b ) t o ( c+d ) and p r i n t s t h e r e s u l t , i f c−d 4 // i s n o t e q u a l t o z e r o 1 2 5 6 7 8 9 10 11 12 13 14 15 disp ( ” E n t e r f o u r i n t e g e r v a l u e s ” ) ; a = int8 ( input ( ” a=” ) ) ; b = int8 ( input ( ” b=” ) ) ; c = int8 ( input ( ” c=” ) ) ; d = int8 ( input ( ” d=” ) ) ; if (c - d ~= 0) then // E x e c u t e s t a t e m e n t b l o c k ratio = double ( a + b ) / double (c - d ) ; printf ( ” R a t i o=%f\n ” , ratio ) ; end 31 Scilab code Exa 5.1cs Case study 1 range of numbers 1 // Case Study :− C h a p t e r 5 , Page No : 1 3 9 2 // 1 . Range o f numbers 3 4 sum1 =0; 5 count =0; 6 f =0; value =1; 7 printf ( ” E n t e r numbers [ p r e s s e n t e r a f t e r e a c h number ] : \ n i n p u t a NEGATIVE number t o end \n ” ) ; 8 while ( value ) 9 [ value ]= scanf ( ” %f ” ) ; // Read d a t a 10 if ( value <0) then 11 break ; 12 end 13 count = count +1; 14 // C a l c u l a t i n g h e i g h t and l o w e s t v a l u e 15 if ( count ==1) then 16 high = value ; 17 low = value ; 18 elseif ( value > high ) then 19 high = value ; 20 elseif ( value < low ) then 21 low = value ; 22 end 23 // C a l c u l a t e sum 24 sum1 = sum1 + value ; 25 end 26 average = sum1 / count ; // A v e r a g e c o s t 27 range1 = high - low ; // Range o f v a l u e s 28 // P r i n t t h e r e s u l t s 29 printf ( ” T o t a l v a l u e s : %d\n ” , count ) ; 32 printf ( ” H i g h e s t −v a l u e s : %f\ nLowest−v a l u e : %f\n ” , high , low ) ; 31 printf ( ” Range : %f\n Avarage : %f\n ” , range1 , average ) ; 30 Scilab code Exa 5.2 counts the number of boys // Example 5 . 2 // The program c o u n t s t h e number o f b o y s whose w e i g h t i s l e s s t h a n 50Kg 3 // and h e i g h t i s g r e a t e r t h a n 170cm . 1 2 4 5 count = int (0) ; 6 disp ( ” E n t e r w e i g h t and h e i g h t f o r 10 b o y s ” ) ; 7 for i =1:10 8 [ weight , height ]= scanf ( ” %f %f ” ) ; 9 if ( weight <50& height >170) then // T e s t f o r w e i g h t and h e i g h t count = count +1; 10 11 end 12 end 13 disp ( ”Number o f b o y s w i t h w e i g h t <50 kg and h e i g h t >170 cm =” ) ; 14 disp ( count ) Scilab code Exa 5.2cs Case study 2 Pay Bill Calculations 1 // Case Study :− C h a p t e r 5 33 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 // 2 . Pay− B i l l C a l c u l a t i o n s CA1 =1000; CA2 =750; CA3 =500; CA4 =250; EA1 =500; EA2 =200; EA3 =100; EA4 =0; level =1; while ( level ) printf ( ” E n t e r 0 [ z e r o ] f o r l e v e l t o end ” ) ; // Read d a t a level = input ( ” E n t e r l e v e l : ” ) ; if ( level ==0) break ; end printf ( ” E n t e r j o b number , and b a s i c pay \n ” ) ; // Read d a t a [ jobnumber , basic ]= scanf ( ”%d %f ” ) ; // D e c i d e l e v e l number and c a l c u l a t e p e r k s select level case 1 then perks = CA1 + EA1 ; case 2 then perks = CA2 + EA2 ; case 3 then perks = CA3 + EA3 ; case 4 then perks = CA4 + EA4 ; else printf ( ” E r r o r i n l e v e l c o d e ” ) ; return ; end house_rent =0.25* basic ; // C a l c u l a t e g r o s s s a l a r y gross = basic + house_rent + perks ; // C a l c u l a t e i n c o m e t a x if ( gross <=2000) then incometax =0; elseif ( gross <=4000) 34 40 incometax =0.03* gross ; 41 elseif ( gross <=5000) 42 incometax =0.05* gross ; 43 else 44 incometax =0.08* gross ; 45 end 46 // Compute t h e n e t s a l a r y 47 net = gross - incometax ; 48 // P r i n t t h e r e s u l t s 49 printf ( ”%d %d %. 2 f \n ” , level , jobnumber , net ) ; 50 end 51 printf ( ”END OF THE PROGRAM” ) ; Scilab code Exa 5.3 Evaluate the power series // Example 5 . 3 // A program t o e v a l u a t e t h e power s e r i e s . I t u s e s i f . . . e l s e to t e s t accuracy . 3 // e ˆ x=1+x+x ˆ 2 / 2 ! + x ˆ 3 / 3 ! + +x ˆ n/ n ! ,0 < x<1 1 2 4 5 ACCURACY =0.0001; 6 x = input ( ” E n t e r v a l u e o f x : ” ) ; 7 n =1; term =1; sum1 =1; count = int8 (1) ; 8 while (n <=100) 9 term = term * x / n ; 10 sum1 = sum1 + term ; 11 count = count +1; 12 if ( term < ACCURACY ) then // T e s t f o r 13 n =999; 14 else 15 n = n +1; 16 end 17 end 18 // P r i n t r e s u l t s 19 printf ( ”Term=%d Sum=%f ” , count , sum1 ) ; 35 accuracy Scilab code Exa 5.4 Largest of the three numbers // Example 5 . 4 // The program s e l e c t s and p r i n t s t h e l a r g e s t o f t h e t h r e e numbers 3 // u s i n g n e s t e d i f . . . e l s e s t a t e m e n t 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 disp ( ” E n t e r t h r e e v a l u e s ” ) ; A = input ( ”A=” ) ; B = input ( ”B=” ) ; C = input ( ”C=” ) ; disp ( ” L a r g e s t v a l u e i s : ” ) ; if (A > B ) , // T e s t f o r l a r g e s t b e t w e e n A &B if (A > C ) , // T e s t f o r l a r g e s t b e t w e e n A &C disp ( A ) ; else disp ( C ) ; end else if (C > B ) , // T e s t f o r l a r g e s t b e t w e e n C& B disp ( C ) ; else disp ( B ) ; end end 36 Scilab code Exa 5.5 Reads the customer number and power consumed // Example 5 . 5 // The program r e a d s t h e c u s t o m e r number and power consumed and p r i n t s 3 // t h e amount t o be p a i d by t h e c u s t o m e r 1 2 4 5 disp ( ” E n t e r CUSTOMER NO. and UNITS consumed ” ) ; 6 // Read d a t a 7 custnum = input ( ”CUSTOMER NO . : ” ) ; 8 units = input ( ”UNITS : ” ) ; 9 // Use o f e l s e . . . i f l a d d e r 10 // C a l c u l a t e s c h a r g e s 11 if ( units <=200) , 12 charges =0.5* units ; 13 elseif ( units <=400) , 14 charges =100+0.65*( units -200) ; 15 elseif ( uints <=600) , 16 charges =230+0.8*( units -400) ; 17 else 18 charges =390+( units -600) ; 19 end 20 // P r i n t r e s u l t s 21 printf ( ” Customer No : %d C h a r g e s=%. 2 f ” , custnum , charges ) ; Scilab code Exa 5.6 Loan applications and to sanction loans 37 1 2 // Example 5 . 6 // A program t o p r o c e s s l o a n a p p l i c a t i o n s and t o sanction loans . 3 4 MAXLOAN =50000; 5 disp ( ” E n t e r t h e v a l u e s o f p r e v i o u s two l o a n s ” ) ; 6 loan1 = int32 ( input ( ” E n t e r f i r s t l o a n : ” ) ) ; 7 loan2 = int32 ( input ( ” E n t e r s e c o n d l o a n : ” ) ) ; 8 loan3 = int32 ( input ( ” E n t e r t h e v a l u e s o f new l o a n : ” ) ) ; 9 sum23 = loan2 + loan3 ; 10 // C a l c u l a t e t h e s a n c t i o n l o a n 11 if ( loan1 >0) , 12 sancloan =0; 13 elseif ( sum23 > MAXLOAN ) , 14 sancloan = MAXLOAN - loan2 ; 15 else 16 sancloan = loan3 ; 17 end 18 // P r i n t t h e r e s u l t s 19 printf ( ” P r e v i o u s l o a n s p e n d i n g : %d %d\n ” , loan1 , loan2 ) ; 20 printf ( ” Loan r e q u e s t e d =%d\n ” , loan3 ) ; 21 printf ( ” Loan s a n c t i o n e d =%d\n ” , sancloan ) ; Scilab code Exa 5.7 square root for five numbers 1 2 3 4 5 6 // Example 5 . 7 // The program e v a l u a t e s t h e s q u a r e r o o t f o r f i v e numbers . count =1; printf ( ” E n t e r FIVE r e a l v a l u e s \n ” ) ; while ( count <=5) 38 7 8 9 10 11 x = scanf ( ” %f ” ) ; if (x <0) then printf ( ” V a l u e − %d i s n e g a t i v e \n ” , count ) ; else y = sqrt ( x ) ; // C a l c u l a t e s q u a r e root printf ( ” %f\ t%f \n ” ,x , y ) ; // P r i n t r e s u l t end count = count +1; 12 13 14 15 end 16 printf ( ”End o f c o m p u t a t i o n ” ) ; 39 Chapter 6 Decision Making and Looping Scilab code Exa 6.1 evaluate the equation 1 2 3 4 5 6 7 8 9 10 11 12 13 // Example 6 . 1 //A program t o e v a l u a t e t h e e q u a t i o n y=x ˆ n when n i s a non−n e g a t i v e i n t e g e r . x = input ( ” E n t e r t h e v a l u e o f x : ” ) ; n = input ( ” E n t e r t h e v a l u e o f n : ” ) ; y =1.0; count =1; // i n t i a l i z a t i o n // Loop b e g i n s while ( count <= n ) // T e s t i n g y=y*x; count = count +1; // I n c r e a m e n t i n g end // End o f l o o p printf ( ” x = %f ; n = %d ; x t o power n = %f\n ” ,x ,n , y ) ; Scilab code Exa 6.1cs Case study 1 Table of Binomial Coefficients 40 1 // Case Study :− C h a p t e r 6 , Page No :176 2 // 1 . Table o f Binomial 3 4 MAX =10; 5 printf ( ”mx” ) ; 6 for m =0:10 7 printf ( ”%4d” ,m ) ; 8 end 9 printf ( ” \n 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 Coefficients −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n ”); m =0; // p r i n t t h e t a b l e o f b i n o m i a l c o e f f i c i e n t s f o r m=10 // Computation u s i n g w h i l e l o o p while (m <= MAX ) printf ( ”%2d” ,m ) ; x =0; binom =1; while (x <= m ) if ( m ==0| x ==0) printf ( ”%4d” , binom ) ; // P r i n t t h e r e s u l t i . e . binom else binom = binom *( m - x +1) / x ; // compute t h e binomial c o e f f i c i e n t printf ( ”%4d” , binom ) ; // P r i n t t h e r e s u l t i . e . binom end x = x +1; end printf ( ” \n ” ) ; m = m +1; end printf ( ” −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n ”); 41 Scilab code Exa 6.2 Multiplication table 1 2 // Example 6 . 2 //A program t o p r i n t m u l t i p l i c a t i o n t a b l e from 1∗1 to 12∗10. 3 4 5 disp ( ” MULTIPLICATION TABLE ”); 6 disp ( ”−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−” ) ; 7 row =1; 8 while ( row <=12) // Outer l o o p b e g i n s 9 column =1; 10 while ( column <=10) // I n n e r l o o p b e g i n s 11 y = row * column ; 12 printf ( ”%4d” ,y ) ; 13 column = column +1; 14 end 15 row = row +1; 16 printf ( ” \n ” ) ; 17 end Scilab code Exa 6.2cs Case study 2 Histogram 1 // 2 // 3 4 N =5; 5 for n =1: N Case Study :− C h a p t e r 6 2 . Histogram 42 6 7 8 printf ( ” E n t e r e m p l o y e e s i n Group−%d : ” ,n ) ; value ( n ) = scanf ( ”%d” ) ; // Read d a t a i n t h e a r r a y named v a l u e printf ( ”%d\n ” , value ( n ) ) ; // P r i n t number which i s a t p o s i t i o n n 9 end 10 printf ( ” \n ” ) ; 11 printf ( ” | \ n”); 12 // Computation u s i n g f o r l o o p and draw a h i s t o g r a m 13 for n =1: N 14 for i =1:3 15 if ( i ==2) then 16 printf ( ” Group−%1d | ” ,n ) ; 17 else 18 printf ( ” | ”); 19 end 20 for j =1: value ( n ) 21 printf ( ” ∗ ” ) ; 22 end 23 if ( i ==2) 24 printf ( ” (%d) \n ” , value ( n ) ) ; 25 else 26 printf ( ” \n ” ) ; 27 end 28 end 29 printf ( ” | \ n”); 30 end Scilab code Exa 6.3 Uses a for loop 1 2 // Example 6 . 3 // The program u s e s a f o r l o o p t o p r i n t t h e ” Power o f 2” t a b l e f o r t h e 43 3 4 5 6 7 8 9 10 11 12 // power 0 t o 2 0 , b o t h p o s i t i v e and n e g a t i v e . disp ( ”−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−” ) ; printf ( ” 2 t o power n n 2 t o power −n\n ” ) ; disp ( ”−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−” ) ; for n =0:20 // Loop b e g i n s p =2^ n ; q =2^ - n ; printf ( ”%7d %9d %10 . 1 2 f \n ” ,p ,n , q ) ; end // Loop e n d s Scilab code Exa 6.3cs Case study 3 Minimum Cost 1 // Case Study :− C h a p t e r 6 2 // 3 . Minimum C o s t 3 4 for p =0:0.1:10 5 cost =48 -8* p + p ^2; 6 if ( p ==0) , 7 cost1 = cost ; 8 continue ; // Use o f c o n t i n u e s t a t e m e n t 9 end 10 11 if ( cost >= cost1 ) , 12 break ; // Use o f b r e a k s t a t e m e n t 13 end 14 cost1 = cost ; 15 p1 = p ; 16 end 17 p =( p + p1 ) /2.0; 18 cost =40 -8* p + p ^2; // Computes t h e c o s t 19 // p r i n t t h e r e s u l t 44 20 printf ( ”MINIMUM COST=%. 2 f AT p=%. 1 f \n ” , cost , p ) ; Scilab code Exa 6.4 Read the marks and print total marks // Example 6 . 4 //A c l a s s o f n s t u d e n t s t a k e an a n n u a l e x a m i n a t i o n in m subjects . 3 // A program t o r e a d t h e marks o b t a i n e d by e a c h student in various subjects 4 // and t o compare and p r i n t t h e t o t a l marks o b t a i n e d by e a c h o f them . 1 2 5 6 FIRST =360; SECOND =240; 7 disp ( ” E n t e r number o f s t u d e n t s and s u b j e c t s ” ) ; 8 [n , m ]= scanf ( ”%d %d” ) ; 9 for i =1: n 10 roll_number = input ( ” E n t e r r o l l n u m b e r : ” ) ; 11 total =0; 12 printf ( ” E n t e r marks o f %d s u b j e c t s f o r ROLL NO 13 14 15 16 17 18 19 20 21 22 23 24 25 %d” ,m , roll_number ) ; printf ( ” [ E n t e r e a c h i n n e w l i n e ] ” ) ; for j =1: m marks = scanf ( ”%d” ) ; total = total + marks ; // Compute t h e t o t a l end // p r i n t t h e t o t a l marks printf ( ”TOTAL MARKS =%d” , total ) ; // T e s t f o r d i v i s i o n and d i s p l a y i t if ( total >= FIRST ) , disp ( ” F i r s t D i v i s i o n ” ) ; elseif ( total >= SECOND ) disp ( ” S e c o n d D i v i s i o n ” ) ; else 45 26 disp ( ” ∗∗∗F A I L ∗∗∗ ” ) 27 end 28 end Scilab code Exa 6.4cs Case study 4 Plotting of two Functions 1 2 // // Case Study :− C h a p t e r 6 4 . P l o t t i n g o f two F u n c t i o n s i . e . y1=exp (−ax ) and y2=exp (−ax ˆ 2 / 2 ) 3 4 a =0.4; 5 printf ( ” 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 y−−−−−−−> \n ” ) ; printf ( ” 0 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\ n”); for x =0:0.25:4 // E v a l u a t i o n o f f u n c t i o n s y1 = int32 (50* exp ( - a * x ) +0.5) ; y2 = int32 (50* exp ( - a *( x ^2) /2) +0.5) ; // p l o t t i n g when y1=y2 if ( y1 == y2 ) , if ( x ==2.5) , printf ( ” x | ” ) ; else printf ( ” | ” ) ; end for i =1:( y1 -1) printf ( ” ” ) ; end printf ( ”#\n ” ) ; continue ; end 46 24 25 // P l o t t i n g when y1>y2 26 if ( y1 > y2 ) , 27 if ( x ==2.5) , 28 printf ( ” x | ” ) ; 29 else 30 printf ( ” | ” ) ; 31 end 32 for i =1: y2 -1 33 printf ( ” ” ) ; 34 end 35 printf ( ” ∗ ” ) ; 36 for i =1:( y1 - y2 -1) 37 printf ( ”−” ) ; 38 end 39 printf ( ” 0\ n ” ) ; 40 continue ; 41 end 42 43 // P l o t t i n g when y2>y1 44 if ( y2 > y1 ) , 45 if ( x ==2.5) 46 printf ( ” x | ” ) ; 47 else 48 printf ( ” | ” ) ; 49 end 50 for i =1:( y1 -1) 51 printf ( ” ” ) ; 52 end 53 printf ( ” 0 ” ) ; 54 for i =1:( y2 - y1 -1) 55 printf ( ”−” ) ; 56 end 57 printf ( ” ∗\ n ” ) ; 58 end 59 end 60 printf ( ” | \ n ” ) ; 47 Scilab code Exa 6.5 Use of the break statement 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // Example 6 . 5 // Program i l l u s t r a t e u s e o f t h e b r e a k s t a t e m e n t disp ( ” T h i s program c o m p u t e s t h e a v a r a g e o f s e t o f numbers ” ) ; disp ( ” E n t e r v a l u e s and e n t e r a NEGATIVE v a l u e a t t h e end ” ) ; sum1 =0; for m =1:1000 x = scanf ( ” %f ” ) ; // Read d a t a if (x <0) then break ; //EXIT FROM LOOP end sum1 = sum1 + x ; // Computes sum end average = sum1 /( m -1) ; // Computes A v e r a g e // P r i n t t h e r e s u l t s printf ( ”Number o f v a l u e s =%d\n ” ,m -1) ; printf ( ” sum1=%f\n ” , sum1 ) ; printf ( ” Avarage =%f\n ” , average ) ; Scilab code Exa 6.6 Evaluate the series 1 2 3 // Example 6 . 6 // Program t o e v a l u a t e t h e s e r i e s i . e . // 1/1−x = 1+x+xˆ2+x ˆ 3 + . . . . . + x ˆn 48 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 x = input ( ” I n p u t v a l u e o f x : ” ) ; LOOP =100; ACCURACY =0.0001; sum1 =0; term =1; flag =0; // Computation u s i n g f o r l o o p for n =1: LOOP sum1 = sum1 + term ; if ( term <= ACCURACY ) then flag =1; break ; end term = term * x ; // Read v a l u e o f x // I n i t i a l i z a t i o n // T e s t f o r a c c u r a c y end // P r i n t t h e r e s u l t s if ( flag ==1) then printf ( ” EXIT FROM LOOP\n ” ) ; printf ( ” Sum =%f ; No . o f t e r m s =%d” , sum1 , n ) ; else disp ( ”FINAL VALUE OF N I S NOT SUFFICIENT TO ACHIEVE DESIRED ACCURCY” ) ; 24 end Scilab code Exa 6.7 Use of continue statement 1 2 3 4 5 6 7 // Example 6 . 7 // The program i l l u s t r a t e t h e u s e o f c o n t i n u e statement disp ( ” E n t e r 9 9 9 9 t o STOP” ) ; count =0; negative =0; while ( count <=100) 49 8 number = input ( ” E n t e r a number : ” ) ; 9 if ( number ==9999) then 10 break ; //EXIT FROM THE LOOP 11 end 12 if ( number <0) , 13 disp ( ”Number i s n e g a t i v e ” ) ; 14 negative = negative +1; 15 continue ; // SKIP REST OF LOOP 16 end 17 sqrot = sqrt ( number ) ; //COMPUTE SQUARE ROOT 18 printf ( ”Number = %f\n ” , number ) ; 19 printf ( ” S q u a r e r o o t = %f ” , sqrot ) ; 20 count = count +1; 21 end 22 //PRINT RESULTS 23 printf ( ”Number o f i t e m s done = %d\n ” , count ) ; 24 printf ( ” N e g a t i v e i t e m s = %d\n ” , negative ) ; 25 disp ( ”END OF DATA” ) ; 50 Chapter 7 Arrays Scilab code Exa 1.cs Case study 1 Median of list of numbers 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 // // Case Study : C h a p t e r 7 , Page No : 2 1 0 1 . Median o f l i s t o f numbers N =10; disp ( ” E n t e r t h e number o f i t e m s ” ) ; n = scanf ( ”%d” ) ; // R e a d i n g i t e m s i n t o a r r a y a printf ( ” I n p u t %d v a l u e s [ One a t a t i m e ] \ n ” ,n ) ; for i =1: n a ( i ) = scanf ( ” %f ” ) ; end // S o r t i n g b e g i n s for i =1: n -1 for j =1: n - i if ( a ( j ) <= a ( j +1) ) t=a(j); a ( j ) = a ( j +1) ; a ( j +1) = t ; else continue ; end 51 22 23 24 25 26 27 28 29 30 31 32 33 34 end end // s o r t i n g e n d s // C a l c u l a t i o n o f median if ( n /2==0) then median1 =( a ( n /2) + a ( n /2+1) ) /2.0; else median1 = a ( n /2+1) ; end // P r i n t i n g for i =1: n printf ( ” %f ” ,a ( i ) ) ; end printf ( ” \ nMedian i s %f\n ” , median1 ) ; Scilab code Exa 2.cs Case study 2 Calculation of standard deviation 1 // Case Study : Chapter −7 2 // 2. Calculation of standard deviation 3 4 MAXSIZE =100; 5 sum1 =0; n =0; sumsqr =0; 6 disp ( ” I n p u t v a l u e s : i n p u t −1 t o end ” ) ; 7 for i =1: MAXSIZE 8 value ( i ) = scanf ( ” %f ” ) ; // E n t e r i n g v a l u e s i n t h e a r r a y named v a l u e if ( value ( i ) == -1) break ; end sum1 = sum1 + value ( i ) ; n = n +1; 9 10 11 12 13 14 end 15 mean1 = sum1 / n ; 16 for i =1: n // Computes mean 52 17 deviation = value ( i ) - mean1 ; 18 sumsqr = sumsqr + deviation ^2; 19 end 20 variance1 = sumsqr / n ; 21 stddeviation = sqrt ( variance1 ) ; 22 23 24 25 // Computes standard deviation // P r i n t i n g i t e m s , Mean and S t a n d a r d d e v i a t i o n printf ( ”Number o f i t e m s : %d\n ” ,n ) ; printf ( ”Mean : %f\n ” , mean1 ) ; printf ( ” S t a n d a r d d e v i a t i o n : %f\n ” , stddeviation ) ; Scilab code Exa 3.cs Case study 3 Evaluating a Test 1 // Case Study : Chapter −7 2 // 3 . Evaluating a Test 3 4 STUDENTS =3; 5 ITEMS =5; 6 // R e a d i n g o f c o r r e c t a n s w e r s 7 printf ( ” I n p u t key t o t h e i t e m s \n ” ) ; 8 for i =1: ITEMS 9 key ( i ) = read ( %io (1) ,1 ,1 , ’ ( a ) ’ ) ; // Read d a t a u s i n g 10 read function // key ( i )=s c a n f ( ” %c ” ) ; I t can be u s e d t o r e a d data 11 end 12 // E v a l u a t i o n b e g i n s 13 14 for student =1: STUDENTS 15 // R e a d i n g s t u d e n t s r e s p o n s e s and c o u n t i n g 16 17 c o r r e c t ones count =0; printf ( ” \ n I n p u t r e s p o n s e s o f s t u d e n t −%d” , student 53 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 ); for i =1: ITEMS response ( i ) = scanf ( ”%c” ) ; end correct = zeros (1 , ITEMS ) // Commented c o d e can be u s e d t o r e p l a c e a b o v e l i n e i . e . c o r r e c t =z e r o s ( 1 , ITEMS ) // f o r i =1:ITEMS // c o r r e c t ( i ) =0; // end for i =1: ITEMS if ( response ( i ) == key ( i ) ) then count = count +1; correct ( i ) =1; end end // P r i n t i n g o f r e s u l t s printf ( ” S t u d e n t −%d\n ” , student ) ; printf ( ” S c o r e i s %d o u t o f %d\n ” , count , ITEMS ) ; printf ( ” R e s p o n s e t o t h e i t e m s b e l o w a r e wrong \n ” ); n =0; for i =1: ITEMS if ( correct ( i ) ==0) printf ( ” %d” ,i ) ; n = n +1; end end if ( n ==0) then printf ( ” NIL\n ” ) ; end end 54 Scilab code Exa 4.cs Case study 4 Production and sales analysis 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 // // Case Study : Chapter −7 4 . P r o d u c t i o n and s a l e s a n a l y s i s // I n p u t Data disp ( ” E n t e r p r o d u c t s m a n u f a c t u r e d w e e k w i s e ” ) ; disp ( ”M11 , M12,−−,M21 , M22,−− e t c ” ) ; for i =1:2 for j =1:5 M (i , j ) = scanf ( ”%d” ) ; end end disp ( ” E n t e r p r o d u c t s s o l d w e e k w i s e ” ) ; disp ( ” S11 , S12 ,−−, S21 , S22 ,−− e t c ” ) ; for i =1:2 for j =1:5 S (i , j ) = scanf ( ”%d” ) ; end end disp ( ” E n t e r c o s t o f e a c h p r o d u c t ” ) ; for j =1:5 C ( j ) = scanf ( ”%d” ) ; end // V a l u e s m a t r i c e s o f p r o d u c t i o n and s a l e s for i =1:2 for j =1:5 Mvalue (i , j ) = M (i , j ) * C ( j ) ; Svalue (i , j ) = S (i , j ) * C ( j ) ; end end // T o t a l v a l u e o f w e e k l y p r o d u c t i o n and s a l e s for i =1:2 Mweek ( i ) =0; Sweek ( i ) =0; for j =1:5 Mweek ( i ) = Mweek ( i ) + Mvalue (i , j ) ; Sweek ( i ) = Sweek ( i ) + Svalue (i , j ) ; 55 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 end end // Monthly v a l u e o f p r o d u c t w i s e p r o d u c t i o n and s a l e s for j =1:5 Mproduct ( j ) =0; Sproduct ( j ) =0; for i =1:2 Mproduct ( j ) = Mproduct ( j ) + Mvalue (i , j ) ; Sproduct ( j ) = Sproduct ( j ) + Svalue (i , j ) ; end end // Grand t o t a l o f p r o d u c t i o n and s a l e s v a l u e s Mtotal =0; Stotal =0; for i =1:2 Mtotal = Mtotal + Mweek ( i ) ; Stotal = Stotal + Sweek ( i ) ; end // ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ // S e l e c t i o n and p r i n t i n g o f i n f o r m a t i o n r e q u i r e d // ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ disp ( ” F o l l o w i n g i s t h e l i s t o f t h i n g s you r e q u e s t f o r ”); 59 disp ( ” E n t e r a p p r o p r i a t e number and p r e s s r e t u r n key ” ); 60 61 62 disp ( ” 1 . V a l u e m a t r i c e s o f p r o d u c t i o n and s a l e s ” ) ; disp ( ” 2 . T o t a l v a l u e o f w e e k l y p r o d u c t i o n and s a l e s ” ) ; 63 disp ( ” 3 . P r o d u c t i o n w i s e monthly v a l u e o f p r o d u c t i o n and s a l e s ” ) ; 64 disp ( ” 4 . Grand t o t a l v a l u e o f p r o d u c t i o n and s a l e s ” ) ; 65 disp ( ” 5 . E x i t ” ) 66 67 68 69 70 number =0; while (1) // B e g i n i n g o f w h i l e l o o p number = input ( ”ENTER YOUR CHOICE : ” ) ; 56 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 if ( number ==5) then disp ( ”Good Bye ” ) ; break ; end select number // V a l u e M a t i c e s case 1 then disp ( ”VALUE MATRIX OF PRODUCTION” ) ; for i =1:2 printf ( ”Week (%d) \ t ” ,i ) ; for j =1:5 printf ( ”%7d” , Mvalue (i , j ) ) ; end printf ( ” \n ” ) ; end disp ( ”VALUE MATRIX OF SALES” ) ; for i =1:2 printf ( ”Week (%d) \ t ” ,i ) ; for j =1:5 printf ( ”%7d” , Svalue (i , j ) ) ; end printf ( ” \n ” ) ; end // Weekly A n a l y s i s case 2 then disp ( ”TOTAL WEEKLY PRODUCTION AND SALES” ) ; disp ( ” PRODUCTION SALES” ) ; disp ( ” −−−−−−−−−− −−−−−−” ) ; for i =1:2 printf ( ”Week (%d) \ t ” ,i ) ; printf ( ”%7d\ t%9d \n ” , Mweek ( i ) , Sweek ( i ) ) ; end // P r o d u c t w i s e A n a l y s i s case 3 then disp ( ”PRODUCTWISE TOTAL PRODUCTION AND SALES ”); disp ( ” PRODUCTION SALES” ) ; disp ( ” −−−−−−−−−− −−−−−−” ) ; 57 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 for i =1:5 printf ( ” P r o d u c t (%d) \ t ” ,i ) ; printf ( ”%7d\ t%7d \n ” , Mproduct ( i ) , Sproduct (i)); end // Grand T o t a l s case 4 then disp ( ”GRAND TOTAL OF PRODUCTION AND SALES” ) ; printf ( ” T o t a l p r o d u c t i o n = %d\n ” , Mtotal ) ; printf ( ” T o t a l s a l e s = %d\n ” , Stotal ) ; // D e f a u l t else printf ( ”Wrong c h o i c c e , s e l e c t a g a i n \n ” ) ; end // End o f s e l e c t end // End o f w h i l e disp ( ” E x i t from t h e program ” ) ; Scilab code Exa 7.1 Sum of squares of 10 numbers // Example : 7 . 1 // W r i t e a program u s i n g s i n g l e −s u b s c r i p t e d v a r i a b l e to evaluate : 3 // sum o f s q u a r e s o f 10 numbers . The v a l u e s x1 , x2 , . . . a r e r e a d from t h e t e r m i n a l . 1 2 4 5 // R e a d i n g v a l u e s i n t o a r r a y 6 disp ( ”ENTER 10 REAL NUMBERS[ Each i n n e w l i n e ] ” ) ; 7 total =0; 8 for i =1:10 9 x ( i ) = input ( ” ” ) ; 10 total = total + x ( i ) ^2; // Computation o f t o t a l 11 end 12 // P r i n t i n g o f x ( i ) v a l u e s and t o t a l 58 13 for i =1:10 14 printf ( ” x ( %2d ) =%5 . 2 f \n ” ,i , x ( i ) ) ; 15 end 16 printf ( ” T o t a l =%. 2 f ” , total ) ; Scilab code Exa 7.2 Count the number of students 1 2 3 4 5 6 7 // Example : 7 . 2 // Given b e l o w i s t h e l i s t o f marks o b t a i n e d by a c l a s s o f 50 s t u d e n t s i n an // a n n u a l e x a m i n a t i o n . 43 65 51 27 79 11 56 61 82 09 25 36 07 49 55 63 74 81 49 // 37 40 49 16 75 87 91 33 24 58 78 65 56 76 67 45 54 36 63 12 21 73 49 51 19 39 // 49 68 93 85 59 // W r i t e a program t o c o u n t t h e number o f s t u d e n t s b e l o n g i n g to each o f // f o l l o w i n g g r o u p s o f marks :0 −9 ,10 −19 ,20 −29 ,.......100. 8 9 // T h i s program c o m p u t e s f o r 10 s t u d e n t s . We c o u l d compute f o r 50 s t u d e n t s by 10 // c h a n g i n g MAXVAL=50. 11 12 MAXVAL =10; COUNTER =11; 13 disp ( ” I n p u t Data [ Marks o f 10 s t u d e n t s ] ” ) ; 14 group1 = zeros (1 ,11) ; 15 // R e a d i n g and c o u n t i n g 16 for i =1: MAXVAL 17 // R e a d i n g o f v a l u e s 18 value ( i ) = input ( ” ” ) ; 19 // C o u n t i n g f r e q u e n c y o f g r o u p s 20 a = int16 (( value ( i ) /10) ) ; 59 21 if ( a ==0) then 22 group1 ( a +1) = group1 ( a +1) +1; 23 else 24 group1 ( a +1) = group1 ( a +1) +1; 25 end 26 27 end 28 // P r i n t i n g o f f r e q u e n c y t a b l e 29 printf ( ” Group Range F r e q u e n c y \n ” ) ; 30 for i =0: COUNTER -1 31 if ( i ==0) , 32 low =0; 33 else 34 low = i *10; 35 end 36 if ( i ==10) , 37 high =100; 38 else 39 high = low +9; 40 end 41 printf ( ”%2d %8d t o %3d %5d\n ” ,i +1 , low , high , group1 ( i +1) ) ; 42 end Scilab code Exa 7.3 Compute and print 1 2 3 4 5 6 // Example : 7 . 3 // W r i t e a program u s i n g two d i m e n s i o n a l a r r a y t o compute p r i n t f o l l o w i n g // i n f o r m a t i o n from t h e t a b l e o f d a t a d i s c u s s e d : // ( a ) T o t a l v a l u e o f s a l e s by e a c h g i r l . // ( b ) T o t a l v a l u e o f e a c h i t e m s o l d // ( c ) Grand t o t a l o f a l l s a l e s o f a l l i t e m s by a l l 60 girls . 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 MAXGIRLS =4; MAXITEMS =3; frequency = zeros (1 ,5) ; disp ( ” I n p u t d a t a ” ) ; // R e a d i n g v a l u e s and c o m p u t i n g g i r l t o t a l disp ( ” E n t e r v a l u e s , one a t a t i m e ” ) ; for i =1: MAXGIRLS girl_total ( i ) =0; for j =1: MAXITEMS value (i , j ) = scanf ( ”%d” ) ; girl_total ( i ) = girl_total ( i ) + value (i , j ) ; end end // Computing i t e m t o t a l for j =1: MAXITEMS item_total ( j ) =0; for i =1: MAXGIRLS item_total ( j ) = item_total ( j ) + value (i , j ) ; end end // Computing g r a n d t o t a l grand_total =0; for i =1: MAXGIRLS grand_total = grand_total + girl_total ( i ) ; end // P r i n t i n g o f r e s u l t disp ( ”GIRLS TOTALS” ) ; for i =1: MAXGIRLS printf ( ” S a l e s g i r l (%d)=%d\n ” ,i , girl_total ( i ) ) ; end disp ( ”ITEM TOTALS” ) ; for j =1: MAXITEMS printf ( ” Item (%d)=%d\n ” ,j , item_total ( j ) ) ; end printf ( ” Grand T o t a l=%d\n ” , grand_total ) ; 61 Scilab code Exa 7.4 Multiplication table 1 2 // Example : 7 . 4 // W r i t e a program t o compute and p r i n t a m u l t i p l i c a t i o n t a b l e f o r numbers 1 t o 5 3 4 ROWS =5; COLUMNS =5; 5 disp ( ”MULTIPLICATION TABLE” ) ; 6 printf ( ” ∗ | ” ) 7 for j =1: COLUMNS 8 printf ( ”%4d” ,j ) ; 9 end ”); 10 disp ( ” 11 for i =1: ROWS 12 printf ( ”%1d | ” ,i ) ; 13 for j =1: COLUMNS 14 product (i , j ) = i * j ; // C a l c u l a t e t h e product 15 printf ( ”%4d” , product (i , j ) ) ; 16 end 17 printf ( ” \n ” ) ; 18 end // P r i n t t h e p r o d u c t Scilab code Exa 7.5 Popularity of various cars 1 2 // Example : 7 . 5 //A s u r v e y t o know t h e p o p u l a r i t y o f f o u r c a r s ( Ambassador , f a i t , D o l p h i n and 62 // M a r u t i ) was c o n d u c t e d i n f o u r c i t i e s ( Bombay , C a l c u t t a , D e l h i and Madras ) . 4 // Each p e r s o n s u r v e y e d was a s k e d t o g i v e h i s c i t y and t y p e o f c a r he was u s i n g . 5 // W r i t e a program t o p r o u c e a t a b l e s h o w i n g t h e popularity of various cars in 6 // f o u r c i t i e s . 3 7 8 9 frequency = zeros (5 ,5) ; printf ( ” For e a c h p e r s o n , e n t e r t h e c i t y c o d e [ B , C , D,M ] \ n”); 10 printf ( ” f o l l o w e d by t h e c a r c o d e [ 1 , 2 , 3 , 4 ] . \ n ” ) ; 11 printf ( ” E n t e r t h e l e t t e r X 0 ( z e r o ) t o i n d i c a t e end . \ n ”); 12 13 // T a b u l a t i o n b e g i n s 14 for i =1:99 15 [n , city , car ]= mscanf ( ”%c %d” ) ; 16 if ( city == ’X ’ ) then 17 break ; 18 end 19 select city 20 case ’B ’ then frequency (1 , car ) = frequency (1 , car ) 21 22 23 +1; case ’C ’ then frequency (2 , car ) = frequency (2 , car ) +1; case ’D ’ then frequency (3 , car ) = frequency (3 , car ) +1; case ’M’ then frequency (4 , car ) = frequency (4 , car ) +1; end 24 25 26 end 27 28 // T a b u l a t i o n c o m p l e t e d and P r i n t i n g b e g i n s 29 disp ( ” POPULATORY TABLE” ) ; 30 printf ( ” \n ” 63 ); 31 printf ( ” C i t y \n ” ) ; 32 printf ( ” Ambasseador fait Dolphin Maruti \n ” ); 33 34 for i =1:4 35 select i 36 case 1 then printf ( ” Bombay ” ) ; 37 case 2 then printf ( ” C a l c u t t a ” ) ; 38 case 3 then printf ( ” D e l h i ”); 39 case 4 then printf ( ” Madras ” ) ; 40 end 41 for j =1:4 42 printf ( ”%8d” , frequency (i , j ) ) ; 43 end 44 printf ( ” \n ” ) ; 45 end 46 printf ( ” ”); 47 // P r i n t i n g ends 64 Chapter 8 Character Arrays and Strings Scilab code Exa 1.cs Case study 1 Counting words in a text 1 Csae s t u d y : Chapter −8 , Page No // :253 2 // 1 . C o u n t i n g words i n a t e x t 3 4 characters =0; words =0; lines1 =0; 5 printf ( ” KEY IN THE TEXT. \ n ” ) ; 6 printf ( ”GIVE ONE SPACE AFTER EACH WORD. \ n ” ) ; 7 printf ( ”WHEN COMPLETED, ENTER end \n ” ) ; 8 9 l= ’ ’ ; 10 while ( l ~= ’ end ’ ) 11 l = read ( %io (1) ,1 ,1 , ’ ( a ) ’ ) ; // R e a d i n g a l i n e o f 12 13 14 15 16 17 text if ( l == ’ end ’ ) then break ; end line =[ ascii ( l ) ]; values of line l len = length ( line ) ; line for i =1: len // Array o f a s c i i // compute l e n g t h o f 65 // a s c i i v a l u e o f ’ ’ ( i . e . s p a c e ) i s 32 if ( line ( i ) ==32) then words = words +1; // Count t h e number o f words end 18 19 20 21 22 23 end lines1 = lines1 +1; // Count t h e number of l i n e s characters = characters + len ; // Count t h e number of characters 24 25 end 26 // P r i n t i n g r e s u l t s 27 printf ( ”Number o f l i n e s = %d\n ” , lines1 ) ; 28 // Number o f l i n e s a r e added t o words b e c a u s e word o f e a c h 29 // u n c o u n t e d a s occurence of 30 printf ( ”Number 31 printf ( ”Number last l i n e remains− words a r e i n c r e m e n t e d a t t h e space . o f words = %d\n ” , words + lines1 ) ; o f c h a r a c t e r s = %d\n ” , characters ) ; Scilab code Exa 2.cs Case study 2 Processing of a customer list 1 // 2 3 4 5 6 7 // Csae s t u d y : Chapter −8 , Page No :253 2 . Processing of a customer l i s t CUSTOMERS =10; printf ( ” I n p u t names and t e l e p h o n e numbers \n ” ) ; printf ( ” [ Names must have F i r s t , S e c o n d and L a s t n a m e ] \ n”); 8 for i =1: CUSTOMERS 9 // Read d a t a 66 10 11 12 [ first_name ( i ) , second_name ( i ) , surname ( i ) , telephone ( i ) ]= scanf ( ”%s %s %s %s” ) ; // C o n v e r t i n g f u l l name t o surname w i t h i n i t i a l s l1 = length ( surname ( i ) ) ; // Compute l e n g t h o f surname a t i name ( i ) = strncpy ( surname ( i ) , l1 ) ; name ( i ) = strcat ([ name ( i ) , ’ , ’ ]) ; dummy (1) = part ( first_name (i ,1) ,1) ; name ( i ) = strcat ([ name ( i ) , dummy ]) ; name ( i ) = strcat ([ name ( i ) , ’ . ’ ]) ; dummy (1) = part ( second_name (i ,1) ,1) ; name ( i ) = strcat ([ name ( i ) , dummy ]) ; 13 14 15 16 17 18 19 20 end 21 // A l p h a b e t i c a l o d e r i n g o f s u r n a m e s 22 for i =1: CUSTOMERS // Outer l o o p 23 begins for j =2: CUSTOMERS - i +1 // I n n e r l o o p begins k = strcmp ( name (j -1) , name ( j ) ) ; if (k >0) then 24 25 26 27 // Swaping names 28 l1 = length ( name (j -1) ) ; 29 l2 = length ( name ( j ) ) ; 30 dummy = strncpy ( name (j -1) , l1 ) ; 31 name (j -1) = strncpy ( name ( j ) , l2 ) ; 32 l3 = length ( dummy ) ; 33 name ( j ) = strncpy ( dummy , l3 ) ; 34 35 // Swapping t e l e p h o n e numbers 36 l3 = length ( telephone (j -1) ) ; 37 l4 = length ( telephone ( j ) ) ; 38 dummy = strncpy ( telephone (j -1) , l3 ) ; 39 telephone (j -1) = strncpy ( telephone ( j ) , l4 ) ; 40 telephone ( j ) = strncpy ( dummy , l3 ) ; 41 end 42 end // I n n e r l o o p e n d s 43 end // Outer l o o p e n d s 67 44 // P r i n t i n g a l p h a b e t i c a l l i s t 45 disp ( ”CUSTOMER LIST IN ALPHABETICAL ORDER” ) ; 46 for i =1: CUSTOMERS 47 printf ( ”%−20 s \ t %−10 s \n ” , name ( i ) , telephone ( i ) ) ; 48 end Scilab code Exa 8.1 Read a series of words 1 2 // Exampple 8 . 1 // W r i t e a program t o r e a d a s e r i e s o f words from terminal using scanf function . 3 4 // Read d a t a u s i n g s c a n f f u n c t i o n 5 disp ( ” E n t e r t e x t : ” ) 6 [ word1 , word2 , word3 , word4 ]= scanf ( ”%s %s %s %s” ) ; 7 // P r i n t i n g t h e r e s u l t s 8 printf ( ” word1 = %s\ nword2 = %s\n ” , word1 , word2 ) ; 9 printf ( ” word3 = %s\ nword4 = %s\n ” , word3 , word4 ) ; Scilab code Exa 8.2 Read a line of text // Example 8 . 2 // W r i t e a program t o r e a d a l i n e o f t e x t c o n t a i n i n g a s e r i e s of− 3 // words from t h e t e r m i n a l . 1 2 4 5 6 7 disp ( ” E n t e r t e x t . P r e s s <Return > a t end ” ) ; line = read ( %io (1) ,1 ,1 , ’ ( a ) ’ ) ; // Read a l i n e disp ( line ) ; // D i s p l a y l i n e 68 Scilab code Exa 8.3 Copy one string into another // Example 8 . 3 // W r i t e a program t o copy one s t r i n g i n t o a n o t h e r and c o u n t t h e number 3 // o f c h a r a c t e r s c o p i e d . 1 2 4 5 6 7 8 9 10 11 12 13 14 15 // Read d a t a u s i n g s c a n f f u n c t i o n disp ( ” E n t e r a s t r i n g : ” ) [ string2 ]= scanf ( ”%s” ) ; // Read s t r i n g l = length ( string2 ) ; // Compute t h e l e n g t h string1 = ’ ’ ; // s t r i n g 1 i s empty for i =1: l string1 = string1 + part ( string2 , i ) ; end // P r i n t i n g t h e r e s u l t s printf ( ” %s\n ” , string1 ) ; printf ( ” Number o f c h a r a c t e r s = %d\n ” ,l ) ; Scilab code Exa 8.4 Display the string under various format specifications // Exampple 8 . 4 // W r i t e a program t o s t o r e t h e s t r i n g ” U n i t e d Kingdom ” i n t h e a r r a y c o u n t r y − 3 // and d i s p l a y t h e s t r i n g u n d e r v a r i o u s f o r m a t specifications . 1 2 4 69 5 6 7 8 9 10 11 12 13 14 15 16 17 country = ’ U n i t e d Kingdom ’ ; printf ( ” \n ” ) ; printf ( ” ∗ 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 ∗ \ n ” ) ; printf ( ”−−−−−−−−\n ” ) ; printf ( ” %15s \n ” , country ) ; printf ( ” %5s\n ” , country ) ; printf ( ”%15 . 6 s \n ” , country ) ; printf ( ”%−15.6 s \n ” , country ) ; printf ( ”%15 . 0 s \n ” , country ) ; printf ( ”%. 3 s \n ” , country ) ; printf ( ”%s\n ” , country ) ; printf ( ”−−−−−−−−\n ” ) ; Scilab code Exa 8.5 Program using for loop 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // Example 8 . 5 // W r i t e a program u s i n g f o r l o o p t o p r i n t t h e f o l l o w i n g output : // C // CP // .... // .... // CProgrammimg // CProgrammimg // .... // .... // CPr // CP // C string1 = ’ CProgramming ’ ; printf ( ” −−−−−−−−−−−−−\n ” ) ; f= ’ ’ ; 70 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 for i =1:12 f = f + part ( string1 , i ) ; printf ( ” | %−13 s | \ n ” ,f ) ; end printf ( ”|−−−−−−−−−−−−−|\n ” ) ; for j =0:11 s= ’ ’ ; for i =1:12 - j s = s + part ( string1 , i ) ; end printf ( ” | %−13 s | \ n ” ,s ) ; end printf ( ” −−−−−−−−−−−−−” ) ; // f o r c =0:11 // d=c +1; // m p r i n t f ( ” | %−12.∗ s | \ n ” , d , s t r i n g 1 ) ; // end // d i s p(”−−−−−−−−−−−−−−−−−−−−−”) ; // f o r c =11: c −1:0 // d=c +1; // p r i n t f ( ” | %−12.∗ s | \ n ” , d , s t r i n g 1 ) ; // end // d i s p(”−−−−−−−−−−−−−−−−−−−−−−”) ; Scilab code Exa 8.6 Print the alphabet set a to z and A to Z // Example 8 . 6 // W r i t e a program which would p r i n t t h e a l p h a b e t s e t a to z A to Z in decimal − 3 // c h a r a c t e r form . 1 2 4 5 for c =65:122 71 6 7 8 9 10 11 12 if (c >90& c <97) then continue ; // T e r m i n a t e c u r r e n t iteration end c1 = ascii ( c ) ; // C o n v e r t a s c i i v a l u e t o character printf ( ” | %4d − %c \ ” , c , c 1 ) ; end p r i n t f ( ” |\ n ” ) Scilab code Exa 8.7 Concatinate the three parts into one string // Example 8 . 7 // The name o f e m p l o y e e s o f an o r g a n i z a t i o n a r e s t o r e d i n t h r e e a r r a y s namely− 3 // f i r s t n a m e , s e c o n d n a m e and l a s t n a m e . 4 // W r i t e a program t o c o n c a t i n a t e t h e t h r e e p a r t s i n t o one s t r i n g c a l l e d name . 1 2 5 6 7 // S t o r e t h e name i n t h e t h r e e a r r a y s 8 first_name =[ ’VISWANATH ’ ]; 9 second_name =[ ’PRATAP ’ ]; 10 last_name =[ ’ SINGH ’ ]; 11 12 // C o n c a t i n a t e t h r e e p a r t s i n t o one 13 name =[ first_name second_name last_name ]; 14 // P r i n t t h e r e s u l t 15 for i =1:3 16 printf ( ”%s ” , name ( i ) ) ; 17 end 18 // S t a t e m e n t b e l o w can a l s o be u s e d t o p r i n t t h e result 72 19 // d i s p ( name ) ; Scilab code Exa 8.8 Compare whether strings are equal // Example 8 . 8 // s1 , s 2 and s 3 a r e t h r e e s t r i n g v a r i a b l e s . W r i t e a program t o r e a d two s t r i n g − 3 // c o n s t a n t s i n t o s 1 and s 2 and compare w h e t h e r t h e y a r e e q u a l o r not , j o i n − 4 // them t o g e t h e r . Then copy c o n t e n t s o f s 1 t o v a r i a b l e s 3 . At t h e end program− 5 // s h o u l d p r i n t a l l t h r e e v a r i a b l e s and t h e i r lengths 1 2 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Read d a t a printf ( ” E n t e r two s t r i n g c o n s t a n t s \n ” ) ; [ s1 s2 ]= scanf ( ”%s %s” ) ; // Comparing two s t r i n g s x = strcmp ( s1 , s2 ) ; if x ~=0 then printf ( ” S t r i n g a r e n o t e q u a l \n ” ) ; // C o n c a t i n a t e two s t r i n g s s 1 and s 2 s1 = strcat ([ s1 , s2 ]) ; else printf ( ” S t r i n g a r e e q u a l \n ” ) ; end l1 = length ( s1 ) ; // Coping s 1 t o s 3 s3 = strncpy ( s1 , l1 ) ; // f i n d i n g l e n g t h o f s t r i n g s 73 26 l2 = length ( s2 ) ; 27 l3 = length ( s3 ) ; 28 // Output 29 printf ( ” s 1 = %s\ t l e n g t h = %d c h a r a c t e r s \n ” ,s1 , l1 ) ; 30 printf ( ” s 2= %s\ t l e n g t h = %d c h a r a c t e r s \n ” ,s2 , l2 ) ; 31 printf ( ” s 3= %s\ t l e n g t h = %d c h a r a c t e r s \n ” ,s3 , l3 ) ; Scilab code Exa 8.9 Sort a list of names in alphabetical order 1 2 // Example 8 . 9 // W r i t e a program t h a t would s o r t a l i s t o f names i n alphabetical order . 3 4 ITEMS =5; 5 // R e a d i n g t h e l i s t 6 printf ( ” E n t e r names o f %d i t e m s \n ” , ITEMS ) ; 7 i =1; 8 while (i <= ITEMS ) 9 string1 ( i ) = scanf ( ”%s” ) ; 10 i = i +1; 11 end 12 // S o r t i n g b e g i n s 13 for i =1: ITEMS // Outer l o o p b e g i n s 14 for j =2: ITEMS - i +1 // I n n e r l o o p b e g i n s 15 k = strcmp ( string1 (j -1) , string1 ( j ) ) 16 if (k >0) then 17 // Compute l e n g t h and Exchange o f 18 19 20 21 22 contents l1 = length ( string1 (j -1) ) ; l2 = length ( string1 ( j ) ) ; dummy = strncpy ( string1 (j -1) , l1 ) ; string1 (j -1) = strncpy ( string1 ( j ) , l2 ) ; l3 = length ( dummy ) ; 74 23 string1 ( j ) = strncpy ( dummy , l3 ) ; 24 end 25 end // I n n e r l o o p e n d s 26 end // Outer l o o p e n d s 27 // S o r t i n g c o m p l e t e d 28 disp ( ” A l p h a b e t i c a l l i s t ” ) ; 29 for i =1: ITEMS 30 printf ( ”%s\n ” , string1 ( i ) ) ; 31 end 75 Chapter 9 User Defined Functions Scilab code Exa 1.cs Case study 1 Calculation of Area under a Curve 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 // Case Study : Chapter −9[ p a g e no : 3 1 0 ] // 1 . C a l c u l a t i o n o f Area u n d e r a Curve funcprot (0) ; // g l o b a l v a r i a b l e s global start_point global end_point ; global total_area ; global numtraps ; function []= input1 () global start_point ; global end_point ; global total_area ; global numtraps ; total_area =0; start_point = input ( ” E n t e r l o w e r l i m i t : ” ) ; end_point = input ( ” E n t e r u p p e r l i m i t : ” ) ; numtraps = input ( ” E n t e r number o f t r a p e z o i d s : ” ) ; endfunction function [ total_area ]= find_area (a ,b , n ) global total_area ; base =( b -1) / n ; // b a s e i s l o c a l 76 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 variable lower = a ; // l o w e r i s l o c a l variable for lower = a :( lower + base ) :( b - base ) h1 = function_x ( lower ) ; h2 = function_x ( lower + base ) ; total_area = total_area + trap_area ( h1 , h2 , base ) ; // t o t a l a r e a i s c a l c u l a t e d end endfunction function [ area ]= trap_area ( height_1 , height_2 , base ) area =0.5*( height_1 + height_2 ) * base ; // a r e a is local variable endfunction function [ x ] = function_x ( x ) x =( x ^2) +1; endfunction // c a l l i n g f u n c t i o n s disp ( ”AREA UNDER CURVE” ) ; input1 () ; // c a l l i n g i n p u t 1 () function 39 total_area = find_area ( start_point , end_point , numtraps ) ; // c a l l i n g f i n d a r e a ( ) f u n c t i o n 40 printf ( ”TOTAL AREA = %f ” , total_area ) ; Scilab code Exa 9.1 Multiple functions // Example 9 . 1 // W r i t e a program w i t h m u l t i p l e f u n c t i o n s t h a t do n o t communicate− 3 // d a t a b e t w e e n them . 1 2 4 77 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 // F u n c t i o n 1 : p r i n t l i n e ( ) funcprot (0) ; function []= printline () // c o n t a i n s no argument for i =1:35 printf ( ”%c” , ’− ’ ) ; end printf ( ” \n ” ) ; endfunction // F u n c t i o n 2 : v a l u e ( ) function []= value () // c o n t a i n s no argument principal = input ( ” P r i n c i p a l amount ? ” ) ; inrate = input ( ” I n t e r e s t r a t e ? ” ) ; period = input ( ” P e r i o d ? ” ) ; sum1 = principal ; year =1; // Computation u s i n g w h i l e l o o p while ( year <= period ) sum1 = sum1 *(1+ inrate ) ; // c a l c u l a t e s p r i n c i p a l amount a f t e r c e r t a i n y e a r s year = year +1; end printf ( ”%8 . 2 f %5 . 2 f %5d %12 . 2 f \n ” , principal , inrate , period , sum1 ) ; endfunction // C a l l i n g functions printline () ; value () ; printline () ; Scilab code Exa 9.2 Include arguments in function calls 1 2 // Example 9 . 2 // Modify Example 9 . 1 t o i n c l u d e a r g u m e n t s i n 78 function calls . 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 funcprot (0) ; function []= printline ( ch ) // f u n c t i o n w i t h argument ch for i =1:52 printf ( ”%c” , ch ) ; end printf ( ” \n ” ) ; endfunction function []= value (p ,r , n ) // f u n c t i o n w i t h argument p , r , n sum1 = p ; year =1; while ( year <= n ) sum1 = sum1 *(1+ r ) ; year = year +1; end printf ( ” %f\ t%f \t%d\ t%f \n ” ,p ,r ,n , sum1 ) ; endfunction printf ( ” E n t e r p r i n c i p a l amount , i n t e r e s t r a t e , and p e r i o d \n [ E n t e r i n s i n g l e l i n e s e p e r a t e d by s p a c e ] ”); [ principal , inrate , period ]= scanf ( ” %f %f %d” ) ; // r e a d from s t a n d a r d i n p u t // C a l l i n g functions printline ( ’ z ’ ) ; value ( principal , inrate , period ) ; printline ( ’ c ’ ) ; Scilab code Exa 9.3 Return result 1 // Example 9 . 3 79 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Modify Example 9 . 2 ’ s f u n c t i o n v a l u e ( ) t o r e t u r n r e s u l t and e x t e n d v e r s a t i l i t y o f t h e f u n c t i o n // p r i n t l i n e by t a k i n g t h e l e n t h o f l i n e a s an argument function []= printline ( ch , len ) for i =1: len printf ( ”%c” , ch ) ; end printf ( ” \n ” ) ; endfunction function [ amount ]= value (p ,r , n ) // r e t u r n s amount sum1 = p ; year =1; while ( year <= n ) sum1 = sum1 *(1+ r ) ; year = year +1; end amount = sum1 ; endfunction printf ( ” E n t e r p r i n c i p a l amount , i n t e r e s t r a t e , and p e r i o d \n [ E n t e r i n s i n g l e l i n e s e p e r a t e d by s p a c e ] ”); [ principal , inrate , period ]= scanf ( ” %f %f %d” ) ; // C a l l i n g functions printline ( ’ ∗ ’ ,52) ; amount = value ( principal , inrate , period ) ; printf ( ” %f\ t%f \t%d\ t%f \n ” , principal , inrate , period , amount ) ; printline ( ’= ’ ,52) ; Scilab code Exa 9.4 Computes x raised to the power y 1 // Example 9 . 4 80 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // W r i t e a program w i t h f u n c t i o n power t h a t c o m p u t e s x r a i s e d t o t h e power y . funcprot (0) ; function p = power (x , y ) p =1.0; // x t o power 0 if (( y >=0) ) then while ( y ) // c o m p u t e s p o s i t i v e p o w e r s p=p*x; y =y -1; end else while ( y ) // c o mp u t e s n e g a t i v e p o w e r s p=p/x; y = y +1; end end endfunction disp ( ” E n t e r x , y : ” ) ; disp ( ” [ E n t e r i n s i n g l e l i n e s e p e r a t e d by s p a c e ] ” ) ; [x , y ]= scanf ( ”%d %d” ) ; // i n p u t u s i n g s c a n f function // c a l l i n g power ( ) f u n c t i o n and p r i n t i n g i t s o u t p u t printf ( ”%d t o t h e power %d i s %f ” ,x ,y , power (x , y ) ) ; Scilab code Exa 9.5 Calculate standard deviation of an array values // Example 9 . 5 // W r i t e a program t o c a l c u l a t e s t a n d a r d d e v i a t i o n o f an a r r a y v a l u e s . 3 // Array e l e m e n t s a r e r e a d from t e r m i n a l . Use f u n c t i o n s to c a l u l a t e − 4 // s t a n d a r d d e v i a t i o n and mean 1 2 81 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 funcprot (0) ; // p a s s i n g a r r a y named ’ v a l u e ’ t o f u n c t i o n s t d d e v a t ’a ’ function [ std ]= std_dev (a , n ) sum1 =0; x = mean1 (a , n ) ; // c a l l i n g mean1 ( ) f u n c t i o n for i =1: n sum1 = sum1 +( x - a ( i ) ) ^2; std = sqrt ( sum1 / double ( n ) ) ; // c o mp u t e s standard deviation end endfunction function [ x ]= mean1 (a , n ) sum1 =0; for i =1: n sum1 = sum1 + a ( i ) ; x = sum1 / double ( n ) ; // x c o n t a i n mean v a l u e end endfunction SIZE = int8 (5) ; // s i z e o f array printf ( ” E n t e r %d f l o a t v a l u e s ” , SIZE ) ; for i =1: SIZE value ( i ) = input ( ” ” ) ; // e n t e r i n g values in the array end printf ( ” Std . d e v i a t i o n i s %f ” , std_dev ( value , SIZE ) ) ; // c a l l i n g s t d d e v ( ) f u n c t i o n Scilab code Exa 9.6 Sort an array 82 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // Example 9 . 6 // W r i t e a program t h a t u s e s a f u n c t i o n t o s o r t an array of i n te ge rs . funcprot (0) ; function [ x ]= sort (m , x ) // P a s s i n g an a r r a y i . e . marks t o f u n c t i o n s o r t ( ) for i =1: m // i r e p e s e n t s number of passes for j =2: m - i +1 // j r e p r e s e n t s number o f c o m p e r i s i o n i n e a c h p a s s if ( x (j -1) >= x ( j ) ) then t = x (j -1) ; x (j -1) = x ( j ) ; x(j)=t; end end end endfunction marks = int16 ([40 ,90 ,73 ,81 ,35]) ; // c r e a t i n g an a r r a y named marks o f 5 i n t e g e r s disp ( ” Marks b e f o r e s o r t i n g ” ) ; disp ( marks ) ; x = sort (5 , marks ) ; // c a l l i n g s o r t ( ) function disp ( ” Marks a f t e r s o r t i n g ” ) ; disp ( x ) ; Scilab code Exa 9.7 Autometic variables // Example 9 . 7 // W r i t e a m u l t i f u n c t i o n how a u t o m e t i c v a r i a b l e s work . 3 funcprot (0) ; 1 2 83 4 function []= function1 () 5 m = int32 (10) ; // L o c a l V a r i a b l e 6 disp ( m ) ; // F i r s t Output 7 endfunction 8 function []= function2 () 9 m = int32 (100) ; // L o c a l V a r i a b l e 10 function1 () ; // C a l l i n g f u n c t i o n 1 ( ) 11 disp ( m ) ; // S e c o n d Output 12 endfunction 13 function []= funcmain () 14 m = int32 (1000) ; 15 function2 () ; // c a l l i n g f u n c t i o n 2 ( ) 16 disp ( m ) ; // T h i r d o u t p u t 17 endfunction 18 funcmain () // c a l l i n g f u n c m a i n ( ) f u n c t i o n Scilab code Exa 9.8 Global variables 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // Example 9 . 8 // W r i t e a m u l t i f u n c t i o n t o i l l u s t r a t e t h e p r o p e r t i e s of global variables . funcprot (0) ; function [ x ]= fun1 () global x ; x = x +10; // g l o b a l x endfunction function [ x ]= fun2 () x =1 // L o c a l x endfunction function [ x ]= fun3 () global x ; x = x +10; // g l o b a l x endfunction 84 15 16 17 18 19 20 21 global x ; x =10; printf ( ” x=%d\n ” ,x ) // c a l l i n g f u n 1 ( ) , f u n 2 ( ) , f u n 3 ( ) f u n c t i o n s printf ( ” x=%d\n ” , fun1 () ) ; printf ( ” x=%d\n ” , fun2 () ) ; printf ( ” x=%d\n ” , fun3 () ) ; Scilab code Exa 9.16 Factorial of a number using recursion 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // T o p i c 9 . 1 6 RECURSION // Page no . 288 // W r i t e a program t o c a l c u l a t e f a c t o r i a l o f a number using recursion function [ fact1 ]= factorial1 ( n ) fact1 = -1 if (n <0) then disp ( ” P l e a s e e n t e r p o s i t i v e v a l u e [ i . e . 0 o r g r e a t e r than 0 ] ”); return ; // Q u i t s t h e current function end if (( n ==0) |( n ==1) ) then fact1 =1; else fact1 = n * factorial1 (n -1) ; // r e c u r s i v e c a l l to f a c t o r i a l 1 () end endfunction n = input ( ” E n t e r number : ” ) ; // c a l l i n g f a c t o r i a l 1 ( ) f u n c t i o n i n s i d e p r i n t f ( ) printf ( ” F a c t o r i a l o f %d = %d” ,n , factorial1 ( n ) ) ; 85 86 Chapter 10 Structures and Unions Scilab code Exa 1.cs Case study 1 Book Shop Inventory 1 // 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // Case s t u d y : Chapter −10 , Page No :341 Book Shop I n v e n t o r y funcprot (0) ; // D e f i n i n g f u n c t i o n s function [ string1 ]= get1 () string1 = read ( %io (1) ,1 ,1 , ’ ( a ) ’ ) ; endfunction function [ i ] = look_up ( table , s1 , s2 , m ) for i =1: m x = strcmp ( s1 , table ( i ) . title ) ; y = strcmp ( s2 , table ( i ) . author ) ; if x ==0 & y ==0 then return i ; // Book f o u n d end end i = -1; // Book n o t f o u n d endfunction // C r e a t e s a r r a y o f s t r u c t u r e s 87 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 book =[ struct ( ’ a u t h o r ’ , ’ R i t c h e ’ , ’ t i t l e ’ , ’C Language ’ , ’ p r i c e ’ ,45.00 , ’ month ’ , ’ May ’ , ’ y e a r ’ ,1977 , ’ p u b l i s h e r ’ , ’ PHI ’ , ’ q u a n t i t y ’ ,10) struct ( ’ a u t h o r ’ , ’ Kochan ’ , ’ t i t l e ’ , ’ Programming i n C ’ , ’ p r i c e ’ ,75.50 , ’ month ’ , ’ J u l y ’ , ’ y e a r ’ ,1983 , ’ p u b l i s h e r ’ , ’ Hayden ’ , ’ q u a n t i t y ’ ,5) struct ( ’ a u t h o r ’ , ’ Balagurusamy ’ , ’ t i t l e ’ , ’ BASIC ’ , ’ p r i c e ’ ,30.00 , ’ month ’ , ’ J a n u a r y ’ , ’ y e a r ’ ,1984 , ’ p u b l i s h e r ’ , ’TMH ’ , ’ q u a n t i t y ’ ,0) struct ( ’ a u t h o r ’ , ’ Balagurusamy ’ , ’ t i t l e ’ , ’COBOL ’ , ’ p r i c e ’ ,60.00 , ’ month ’ , ’ December ’ , ’ y e a r ’ ,1988 , ’ p u b l i s h e r ’ , ’ M a c m i l l a n ’ , ’ q u a n t i t y ’ ,25) ]; n = size ( book ) ; no_of_records = n (1) ; response = ’ ’ , a =1; while (( response == ’Y ’ | response == ’ y ’ ) | a ==1) // Read d a t a printf ( ” E n t e r t i t l e and a u t h o r name a s p e r t h e l i s t : \ n”); printf ( ” T i t l e : \n ” ) ; title1 = get1 () ; printf ( ” Author : \ n ” ) ; author1 = get1 () ; // C a l l i n g i n d e x ( ) f u n c t i o n and // P a s s i n g s t r u c t u r e book t o f u n c t i o n l o o k u p ( ) index = look_up ( book , title1 , author1 , no_of_records ) ; // I f book f o u n d t h e n p r i n t t h e book d e t a i l o t h e r w i s e not in l i s t if index ~= -1 & index then // Book f o u n d printf ( ”% s %s” , book ( index ) . author , book ( index ) . title ) ; printf ( ”% . 2 f %s” , book ( index ) . price , book ( index ) . month ) ; printf ( ”% d %s\n ” , book ( index ) . year , book ( index ) . publisher ) ; 88 44 45 46 47 48 49 50 51 52 53 quantity = input ( ” E n t e r number o f c o p i e s : ” ) ; if quantity < book ( index ) . quantity then printf ( ” C o s t o f %d c o p i e s = %. 2 f \n ” , quantity , book ( index ) . price * quantity ) ; else printf ( ” R e q u i r e d c o p i e s n o t i n s t o c k \n ” ) ; end else printf ( ” Book n o t i n l i s t \n ” ) ; end printf ( ” \nDo you want any o t h e r book ? ( YES/NO) : ” ) ; response = get1 () ; a =2; 54 55 end 56 printf ( ” Thank you . Good Bye ” ) ; Scilab code Exa 10.1 Define a structure type // Example 1 0 . 1 // D e f i n e a s t r u c t u r e type , s t r u c t p e r s o n a l t h a t would c o n t a i n p e r s o n name ,− 3 // d a t e o f j o i n i n g and s a l a r y . W r i t e a program t o r e a d t h i s i n f o r m a t i o n from 4 // k e y b o a r d and p r i n t same on t h e s c r e e n . 1 2 5 6 7 8 9 funcprot (0) ; function [ ]= struc (n ,d ,m ,y , s ) // D e f i n i n g s t r u c t u r e members personal = struct ( ’ name ’ ,n , ’ day ’ ,d , ’ month ’ ,m , ’ y e a r ’ ,y , ’ s a l a r y ’ ,s ) ; 10 person = personal ; 11 // A c c e s s i n g s t r u c t u r e members 12 printf ( ” %s %d %s %d %. 2 f ” , person . name , person . day , 89 13 14 15 16 17 18 person . month , person . year , person . salary ) ; endfunction disp ( ” I n p u t v a l u e s [ Name day month y e a r and s a l a r y ] ” ) ; // R e a d i n g d a t a [ name , day , month , year , salary ]= scanf ( ”%s %d %s %d %f ” ) ; // C a l l i n g f u n c t i o n s t r u c ( ) struc ( name , day , month , year , salary ) ; Scilab code Exa 10.2 Comparison of structure variables 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // Example 1 0 . 2 // W r i t e a program t o i l l u s t r a t e t h e c o m p a r i s o n o f structure variables . function []= class () // D e f i n i n g s t r u c t u r e s student1 = struct ( ’ number ’ ,111 , ’ name ’ , ’ Rao ’ , ’ marks ’ ,72.50) ; student2 = struct ( ’ number ’ ,222 , ’ name ’ , ’ Raddy ’ , ’ marks ’ ,67.00) ; student3 = struct ( ’ number ’ ,[] , ’ name ’ ,[] , ’ marks ’ ,[]) ; student3 = student2 ; if ( student3 == student2 ) , // L o g i c a l o p e r a t i o n on s t r u c t u r e s disp ( ” S t u d e n t 2 and s t u d e n t 3 a r e same ” ) ; printf ( ” %d %s %f ” , student3 . number , student3 . name , student3 . marks ) ; else disp ( ” S t u d e n t 2 and s t u d e n t 3 a r e n o t same ” ) ; end 90 16 17 18 endfunction // c a l l i n g f u n c t i o n c l a s s class () Scilab code Exa 10.3 Calculate the subject wise and student wise totals // Example 1 0 . 3 // W r i t e a program t o c a l c u l a t e t h e s u b j e c t −w i s e and s t u d e n t −w i s e t o t a l s 3 // and s t o r e them a s a p a r t o f t h e s t r u c t u e . 1 2 4 5 6 7 8 // D e f i n i n g a r r a y o f s t r u c t u r e s student =[ struct ( ’ s u b 1 ’ ,45 , ’ s u b 2 ’ ,67 , ’ s u b 3 ’ ,81 , ’ t o t a l ’ ,0) struct ( ’ s u b 1 ’ ,75 , ’ s u b 2 ’ ,53 , ’ s u b 3 ’ ,69 , ’ t o t a l ’ ,0) struct ( ’ s u b 1 ’ ,57 , ’ s u b 2 ’ ,36 , ’ s u b 3 ’ ,71 , ’ t o t a l ’ ,0) ]; total = struct ( ’ s u b 1 ’ ,0 , ’ s u b 2 ’ ,0 , ’ s u b 3 ’ ,0 , ’ t o t a l ’ ,0) ; 9 10 11 12 // C a l c u l a t e t h e s t u d e n t −w i s e and s u b j e c t −w i s e t o t a l s 13 for i =1:3 14 student ( i ) . total = student ( i ) . sub1 + student ( i ) . sub2 + student ( i ) . sub3 ; total . sub1 = total . sub1 + student ( i ) . sub1 ; total . sub2 = total . sub2 + student ( i ) . sub2 ; total . sub3 = total . sub3 + student ( i ) . sub3 ; total . total = total . total + student ( i ) . total ; 15 16 17 18 19 end 20 // P r i n t i n g s t u d e n t −w i s e t o t a l s 21 printf ( ”STUDENT TOTAL\n ” ) ; 22 for i =1:3 91 23 printf ( ” s t u d e n t (%d) total ) ; %d\n ” ,i , student ( i ) . 24 end 25 // P r i n t i n g s u b j e c t −w i s e t o t a l s 26 printf ( ”SUBJECT TOTAL\n ” ) ; 27 printf ( ” %s %d\n ” ,” S u b j e c t 1 ” , total . sub1 ) ; 28 printf ( ” %s %d\n ” ,” S u b j e c t 2 ” , total . sub2 ) ; 29 printf ( ” %s %d\n ” ,” S u b j e c t 3 ” , total . sub3 ) ; 30 // P r i n t i n g g r a n d t o t a l 31 printf ( ” Grand T o t a l = %d” , total . total ) ; Scilab code Exa 10.4 Array member to represent the three subjects // Example 1 0 . 4 // R e w r i t e t h e program o f Example 1 0 . 3 t o u s i n g an a r r a y member t o r e p r e s e n t 3 // t h e t h r e e s u b j e c t s . 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // D e f i n i n g a r r a y o f s t r u c t u r e s and a r r a y w i t h i n structure student (1) =[ struct ( ’ sub ’ ,[45 67 81] , ’ t o t a l ’ ,0) ]; student (2) =[ struct ( ’ sub ’ ,[75 53 69] , ’ t o t a l ’ ,0) ]; student (3) =[ struct ( ’ sub ’ ,[57 36 71] , ’ t o t a l ’ ,0) ]; total = student ; for i =1:3 total . sub ( i ) =0; end total . total =0; // C a l c u l a t e t h e s t u d e n t −w i s e and s u b j e c t −w i s e t o t a l s for i =1:3 for j =1:3 student ( i ) . total = student ( i ) . total + student ( i ) . sub ( j ) ; 92 18 19 20 total . sub ( j ) = total . sub ( j ) + student ( i ) . sub ( j ) ; end total . total = total . total + student ( i ) . total ; Grand t o t a l // 21 end 22 // P r i n t i n g s t u d e n t −w i s e t o t a l s 23 printf ( ”STUDENT TOTAL\n ” ) ; 24 for i =1:3 25 printf ( ” s t u d e n t (%d) %d\n ” ,i , student ( i ) . total ) ; 26 end 27 // P r i n t i n g s u b j e c t −w i s e t o t a l s 28 printf ( ”SUBJECT TOTAL\n ” ) ; 29 for j =1:3 30 printf ( ” s u b j e c t −(%d) %d\n ” ,j , total . sub ( j ) ); 31 end 32 // P r i n t i n g g r a n d t o t a l 33 printf ( ” Grand T o t a l = %d” , total . total ) ; Scilab code Exa 10.5 structure as a parameter to a function // Example 1 0 . 5 // W r i t e a s i m p l e program t o i l l u s t r a t e t h e method o f s e n d i n g an e n t i r e 3 // s t r u c t u r e a s a p a r a m e t e r t o a f u n c t i o n . 1 2 4 5 6 7 8 9 10 funcprot (0) ; // D e f i n i n g f u n c t i o n s function [ item ]= update ( product ,p , q ) product . price = product . price + p ; product . quantity = product . quantity + q ; item = product ; 93 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 endfunction function [ value ] = mul ( stock ) value = stock . price * stock . quantity ; endfunction // C r e a t e s s t r u c t u r e i t e m item = struct ( ’ name ’ , ’XYZ ’ , ’ p r i c e ’ ,25.75 , ’ q u a n t i t y ’ ,12) ; // Read d a t a printf ( ” I n p u t i n c r e m e n t v a l u e s : ” ) ; printf ( ” p r i c e i n c r e m e n t and q u a n t i t y i n c r e m e n t \n ” ) ; [ p_increment , q_increment ]= scanf ( ” %f %d” ) ; // C a l l i n g u p d a t e ( ) and mul ( ) f u n c t i o n s // P a s s i n g s t r u c t u r e i t e m t o f u n c t i o n s u p d a t e ( ) and mul ( ) //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− item = update ( item , p_increment , q_increment ) ; value = mul ( item ) ; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− // P r i n t i n g R e s u l t s printf ( ” Updated v a l u e s o f i t e m s \n ” ) ; printf ( ”Name : %s\n ” , item . name ) ; printf ( ” P r i c e : %f\n ” , item . price ) ; printf ( ” Q u a n t i t y : %d\n ” , item . quantity ) ; printf ( ” V a l u e o f i t e m = %f\n ” , value ) ; 94 Chapter 12 File Management in C Scilab code Exa 12.1 Read data from keyboard and write it to a file // Example 1 2 . 1 // W r i t e a program t o r e a d d a t a from k e y b o a r d , w r i t e i t t o a f i l e c a l l e d INPUT , 3 // a g a i n r e a d t h e same d a t a from t h e INPUT f i l e and d i s p l a y i t on t h e s c r e e n . 1 2 4 5 warning ( ’ o f f ’ ) ; 6 disp ( ” Data I n p u t ” ) ; 7 8 // Open t h e f i l e INPUT 9 f1 = mopen ( ’INPUT . t x t ’ , ’w ’ ) ; 10 // Get c h a r a c t e r from k e y b o a r d 11 c = read ( %io (1) ,1 ,1 , ’ ( a ) ’ ) ; 12 mfprintf ( f1 , ’ %s ’ ,c ) ; 13 14 // c l o s e t h e f i l e i n p u t 15 mclose ( f1 ) ; 16 17 18 disp ( ” Data Output ” ) ; 19 // Reopen t h e f i l e INPUT 95 20 f1 = mopen ( ’INPUT . t x t ’ , ’ r ’ ) ; 21 txt = mgetl ( f1 ) ; 22 printf ( ”%s” , text ) ; 23 // c l o s e t h e f i l e i n p u t 24 mclose ( f1 ) ; Scilab code Exa 12.2 Read and write odd and even numbers // Example12 . 2 //A f i l e named DATA c o n t a i n s a s e r i e s o f i n t e g e r numbers . Code a program 3 // t o r e a d t h e s e numbers and t h e n w r i t e a l l ’ odd ’ numbers t o a f i l e t o be 4 // c a l l e d ODD and a l l ’ even ’ numbers t o a f i l e t o be c a l l e d EVEN . 1 2 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 warning ( ’ o f f ’ ) ; // I n p u t numbers i n t h e DATA. t x t f i l e printf ( ” C o n t e n t s o f DATA f i l e \n ” ) ; f1 = mopen ( ’DATA. t x t ’ , ’ wt ’ ) ; for i =1:30 number ( i ) = scanf ( ”%d” ) ; if ( number ( i ) == -1) break ; end mfprintf ( f1 , ’%d\n ’ , number ( i ) ) ; end mclose ( f1 ) ; f2 = mopen ( ’ODD. t x t ’ , ’ wt ’ ) ; f3 = mopen ( ’EVEN . t x t ’ , ’ wt ’ ) ; f1 = mopen ( ’DATA. t x t ’ , ’ r t ’ ) ; // Read numbers from DATA f i l e 96 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 EOF = length ( number ) ; i =1; even =0; odd =0; while (i < EOF ) [n , number ]= mfscanf ( f1 , ” %d” ) if ( pmodulo ( number ,2) ==0) mfprintf ( f3 , ’%d\n ’ , number ) ; even = even +1; else mfprintf ( f2 , ’%d\n ’ , number ) ; odd = odd +1; end i = i +1; end mclose ( f1 ) ; mclose ( f2 ) ; mclose ( f3 ) ; // W r i t e odd numbers i n t h e ODD. t x t f i l e f2 = mopen ( ’ODD. t x t ’ , ’ r t ’ ) ; printf ( ” \ n C o n t e n t s o f ODD f i l e \n ” ) ; i =1; while (i <= odd ) [n , number ]= mfscanf ( f2 , ”%d” ) printf ( ”%4d” , number ) ; i = i +1; end // W r i t e e v e n numbers i n t h e EVEN . t x t f i l e f3 = mopen ( ’EVEN . t x t ’ , ’ r t ’ ) ; printf ( ” \ n C o n t e n t s o f EVEN f i l e \n ” ) ; i =1; while (i <= even ) [n , number ]= mfscanf ( f3 , ”%d” ) printf ( ”%4d” , number ) ; i = i +1; end // c l o s e t h e f i l e s mclose ( f2 ) ; 97 61 mclose ( f3 ) ; Scilab code Exa 12.3 Read and write data to and from the file INVENTORY 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // Example12 . 3 //A program t o open a f i l e named INVENTORY and s t o r e in i t the f o l l o w i n g // d a t a : Item name Number Price Quantity // AAA−1 111 17.50 115 // BBB−2 125 36.00 75 // C−3 247 31.75 104 // Extend t h e program t o r e a d t h i s d a t a from t h e f i l e INVENTORY and d i s p l a y // i n v e n t o r y t a b l e w i t h t h e v a l u e o f e a c h i t e m . disp ( ” I n p u t f i l e name ” ) ; filename = scanf ( ”%s” ) ; // Read f i l e name t h a t i s ’ INVENTORY’ fp = mopen ( filename , ’w ’ ) ; // Open f i l e i n w r i t e mode , fp i s f i l e d e s c r i p t o r disp ( ” I n p u t i n v e n t o r y d a t a ” ) ; disp ( ” Item name Number Price Quantity ”); for i =1:3 // r e a d d a t a from t e r m i n a l [n , item ( i ) , number ( i ) , price ( i ) , quantity ( i ) ]= mscanf ( ”%s %d %f %d” ) ; // w r i t e d a t a t o t h e f i l e mfprintf ( fp , ’ %s\t%d\t% . 2 f \t%d\n ’ , item ( i ) , number ( i ) , price ( i ) , quantity ( i ) ) ; end mclose ( fp ) ; // c l o s e t h e f i l e fp = mopen ( filename , ’ r ’ ) ; // open f i l e i n r e a d mode 98 23 24 25 26 27 28 29 30 31 disp ( ” Item name Number Price Quantity Value ” ); for i =1:3 // Read d a t a from t h e f i l e ’INVENTORY’ [n , item , number , price , quantity ]= mfscanf ( fp , ”%s %d %f %d” ) ; value = price * quantity ; // Computes v a l u e // P r i n t i n g o f t h e d a t a printf ( ’ %s %7d %8 . 2 f %8d %11 . 2 f \n ’ , item , number , price , quantity , value ) ; end mclose ( fp ) ; Scilab code Exa 12.4 Error handling in file operations 1 2 // Example12 . 4 // W r i t e a program t o i l l u s t a t r e e r r o r h a n d l i n g i n f i l e operations . 3 4 warning ( ’ o f f ’ ) ; 5 fp1 = mopen ( ’TEST ’ , ’w ’ ) ; 6 7 8 9 10 11 12 13 14 15 16 // Open f i l e i n w r i t e mode , fp1 i s f i l e d e s c r i p t o r for i =10:10:100 // w r i t e d a t a t o t h e f i l e mfprintf ( fp1 , ’%d\n ’ ,i ) ; end mclose ( fp1 ) ; disp ( ” I n p u t f i l e name ” ) ; filename = ’ a ’ ; while ( filename ~= ’ ’ ) filename = scanf ( ”%s” ) ; // E r r o r h a n d l i n g try 99 17 18 19 20 21 22 23 fp2 = mopen ( filename , ’ r ’ ) ; if ( fp2 >0) , break ; // T e r m i n a t e s t h e l o o p i f f i l e e x i s t or opened end catch // M e s s a g e s t o be d i s p l a y e d when e r r o r occured printf ( ”Can n o t open f i l e . \ n ” ) ; printf ( ” Type f i l e name a g a i n . \ n ” ) ; end 24 25 26 27 end 28 // Code b e l o w r u n s w h i l e t h e r e i s no e r r o r 29 for i =1:20 30 number = mfscanf ( fp2 , ”%d” ) ; // Read d a t a from 31 f i l e ’TEST ’ if meof ( fp2 ) then // T e s t f o r end o f file printf ( ”Ran o u t o f d a t a ” ) ; break ; else printf ( ”%d\n ” , number ) ; // p r i n t s t h e d a t a end 32 33 34 35 36 37 end 38 mclose ( fp2 ) ; Scilab code Exa 12.5 use of function ftell or mtell and fseek or mseek 1 2 // Example12 . 5 // W r i t e a program t h a t u s e s f u n c t i o n and f s e e k ( mseek ) . 3 100 f t e l l ( mtell ) 4 5 6 7 8 9 warning ( ’ o f f ’ ) ; // Open f i l e ’RANDOM’ i n w r i t e mode , f p i s f i l e descriptor fp = mopen ( ’RANDOM ’ , ’w ’ ) ; c = read ( %io (1) ,1 ,1 , ’ ( a ) ’ ) ; mfprintf ( fp , ’ %s ’ ,c ) ; // w r i t e d a t a t o t h e f i l e printf ( ”Number o f c h a r a c t e r s e n t e r e d = %d\n ” , mtell ( fp ) ) ; mclose ( fp ) ; 10 11 12 // Open f i l e ’RANDOM’ i n r e a d mode 13 fp = mopen ( ’RANDOM ’ , ’ r ’ ) ; 14 n =0; 15 while ( meof ( fp ) ==0) 16 // n i s t h e o f f s e t from o r i g i n i n number o f b y t e s . 17 // The new p o s i t i o n i s a t t h e s i g n e d d i s t a n c e g i v e n 18 19 20 21 22 23 24 25 26 27 28 29 30 31 by n b y t e s from t h e b e g i n n i n g mseek (n , fp , ’ s e t ’ ) ; // P r i n t t h e c h r a c t e r and i t s p o s t i o n printf ( ” P o s i t i o n o f %c i s %d\n ” , ascii ( mget (1 , ’ c ’ , fp ) ) , mtell ( fp ) ) ; n = n +5; end n =0; // I n i t i a l o f f s e t while ( mtell ( fp ) >1) //New p o s i t i o n i s a t t h e s i g n e d d i s t a n c e g i v e n by n b y t e s from t h e end mseek (n , fp , ’ end ’ ) ; // P r i n t t h e c h a r a c t e r s from t h e end printf ( ”%c” ,( ascii ( mget (1 , ’ c ’ , fp ) ) ) ) ; n =n -1; end mclose ( fp ) ; 101 Scilab code Exa 12.6 Append additional items to the file INVENTORY 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 // Example 1 2 . 6 // W r i t e a program t o append a d d i t i o n a l i t e m s t o t h e f i l e INVENTORY // c r e a t e d i n Example 1 2 . 3 and p r i n t t h e t o t a l contents of the f i l e . funcprot (0) ; warning ( ’ o f f ’ ) ; function [ item ] = append ( product , fp ) printf ( ” Item name : \ n ” ) ; product . name = scanf ( ”%s” ) ; printf ( ” Item number : . \ n ” ) ; product . number = scanf ( ”%d” ) ; printf ( ” Item p r i c e \n ” ) ; product . price = scanf ( ” %f ” ) ; printf ( ” Q u a n t i t y : \ n ” ) ; product . quantity = scanf ( ”%d” ) ; // W r i t e d a t a i n t h e f i l e mfprintf ( fp , ’ %s %d %. 2 f %d\n ’ , product . name , product . number , product . price , product . quantity ) ; item = product ; endfunction // C r e a t i n g s t r u c t u r e item = struct ( ’ name ’ , ’ 0 ’ , ’ number ’ , ’ 0 ’ , ’ p r i c e ’ , ’ 0 ’ , ’ q u a n t i t y ’ , ’ 0 ’ ); // Read f i l e name t h a t i s ’INVENTORY’ disp ( ” Type f i l e name ” ) ; filename = scanf ( ”%s” ) ; // Open f i l e i n append mode , f p i s f i l e d e s c r i p t o r fp = mopen ( filename , ’ a+ ’ ) ; b =0; response = -1; // Read d a t a while ( response ==1| b ==0) item = append ( item , fp ) ; // c a l l i n g append ( ) function printf ( ” Item %s appended . \ n ” , item . name ) ; 102 31 printf ( ”Do you want t o add a n o t h e r i t e m \ ( 1 f o r YES/0 f o r NO) ? ” ) ; response = scanf ( ”%d” ) ; b =1; 32 33 34 end 35 n = mtell ( fp ) ; // p o s i t i o n o f l a s t c h a r a c t e r 36 mclose ( fp ) ; 37 38 // Opening f i l e i n t h e r e a d mode 39 fp = mopen ( filename , ’ r ’ ) ; 40 while ( mtell ( fp ) < n -2) 41 // r e a d d a t a from t e r m i n a l 42 [g , item . name , item . number , item . price , item . quantity ]= mfscanf ( fp , ”%s %d %f %d” ) ; // P r i n t Data t o s c r e e n printf ( ’ %s %7d %8 . 2 f %8d\n ’ , item . name , item . number , item . price , item . quantity ) ; 45 end 46 mclose ( fp ) ; 43 44 103 Chapter 13 Dynamic Memory Allocation and linked Lists Scilab code Exa 1.cs Case study 1 Insertion in a sorted list 1 // Case Study : C h a p t e r : 1 3 , Page No .:434 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // 1. Insertion in a sorted list funcprot (0) ; // C r e a t e t h e l i s t function [ List ]= create ( list1 ) global List ; // C r e a t e t h e c u r r e n t node list1 . number = input ( ” I n p u t a number ( Type −999 t o end ) ; ” ) ; if list1 . number == -999 then list1 . next = NULL ; list1 . add = NULL ; else list1 . add = list1 . add +1; list1 . next = NULL ; List ( i ) = list1 ; if ( i ==1) then 104 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 else List (i -1) . next = List ( i ) . add end i = i +1; create ( list1 ) ; // C r e a t e t h e n e x t node end return ; endfunction function []= print1 ( list1 ) if list1 ( i ) (1) . next ~= NULL then printf ( ”%d−−−>” , list1 ( i ) (1) . number ) ; // P r i n t c u r r e n t item i = i +1; if list1 ( i ) (1) . next == NULL then printf ( ”%d” , list1 ( i ) (1) . number ) ; end print1 ( list1 ) ; // Move t o n e x t i t e m end return ; endfunction function [ List ]= insert ( list1 ) global List ; x = input ( ” I n p u t number t o be i n s e r t e d : ” ) ; // Read t h e number // f i n d t h e l o c a t i o n s o t h a t number c o u l d be placed in sorted order while ( list1 ( i ) (1) . next ~= NULL ) if ( list1 ( i ) (1) . number >= x ) then break ; end i = i +1; end key = i ; // I n s e t i o n a t end if ( list1 ( i ) (1) . next == NULL & list1 ( i ) (1) . number < x ) then list1 ( i +1) (1) . number = x ; 105 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 list1 ( i +1) (1) . add = i +1; list1 ( i +1) (1) . next = NULL ; list1 ( i ) (1) . next = list1 ( i +1) (1) . add ; List = list1 ; return ; end i =1; while ( list1 ( i ) (1) . next ~= NULL ) i = i +1; end j = i +1; // Key node f o u n d and i n s e r t new node o r i t e m while ( list1 ( i ) (1) . add ~= key ) list1 ( i +1) (1) . number = list1 ( i ) (1) . number ; i =i -1; end list1 ( i +1) (1) . number = list1 ( i ) (1) . number list1 ( i ) (1) . number = x ; list1 ( j ) (1) . add = j ; list1 ( j ) (1) . next = NULL ; list1 (j -1) (1) . next = list1 ( j ) (1) . add ; List = list1 ; endfunction global List ; NULL =0; i =1; // C r e a t e t h e s t r u c t u r e i . e . node node = struct ( ’ number ’ ,0 , ’ add ’ ,0 , ’ n e x t ’ ,0) ; head = node ; // C a l l i n g t h e f u n c t i o n s printf ( ” I n p u t a s o r t e d ( a s c e n d i n g ) l i s t ” ) ; List = create ( head ) ; printf ( ” \ n O r i g i n a l L i s t : ” ) ; print1 ( List ) ; List = insert ( List ) ; printf ( ” \nNew L i s t : ” ) ; print1 ( List ) ; 106 Scilab code Exa 2.cs Case study 2 Building a Sorted List 1 // Case Study : C h a p t e r : 1 3 , Page No .:438 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 // 2. Building a Sorted List funcprot (0) ; // C r e a t e t h e l i s t function [ List ]= create ( list1 ) global List ; // C r e a t e t h e c u r r e n t node list1 . number = input ( ” I n p u t a number ( Type −999 t o end ) ; ” ) ; if list1 . number == -999 then list1 . next = NULL ; list1 . add = NULL ; else list1 . add = list1 . add +1; list1 . next = NULL ; List ( i ) = list1 ; if ( i ==1) then else List (i -1) . next = List ( i ) . add end i = i +1; create ( list1 ) ; // C r e a t e t h e n e x t node end return ; endfunction function []= print1 ( list1 ) if list1 ( i ) (1) . next ~= NULL then 107 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 printf ( ”%d−−−>” , list1 ( i ) (1) . number ) ; // P r i n t c u r r e n t item i = i +1; if list1 ( i ) (1) . next == NULL then printf ( ”%d” , list1 ( i ) (1) . number ) ; end print1 ( list1 ) ; // Move t o n e x t i t e m end return ; endfunction // S o r t i n g o f t h e numbers i n t h e l i s t function [ List ]= insert_sort ( list1 ) global List ; j =1; while ( list1 ( j ) (1) . next ~= NULL ) i =1; while ( list1 ( i ) (1) . next ~= NULL ) if ( list1 ( i ) (1) . number > list1 ( i +1) (1) . number ) then temp = list1 ( i ) (1) . number ; list1 ( i ) (1) . number = list1 ( i +1) (1) . number ; list1 ( i +1) (1) . number = temp ; end i = i +1; end j = j +1; end List = list1 ; endfunction global List ; NULL =0; i =1; // C r e a t e t h e s t r u c t u r e i . e . node node = struct ( ’ number ’ ,0 , ’ add ’ ,0 , ’ n e x t ’ ,0) ; head = node ; // C a l l i n g t h e f u n c t i o n s List = create ( head ) ; 108 64 65 66 67 68 printf ( ” \ n O r i g i n a l L i s t : ” ) ; print1 ( List ) ; List = insert_sort ( List ) ; // S o r t t h e l i s t printf ( ” \ n A f t e r s o r t i n g : ” ) ; print1 ( List ) ; Scilab code Exa 13.3 Create a linear linked list // Example 1 3 . 3 // W r i t e a program t o c r e a t e a l i n e a r l i n k e d l i s t interactively 3 // and p r i n t t h e l i s t and t o t a l number o f i t e m s i n the l i s t . 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 funcprot (0) ; NULL =0; i =1; // C r e a t e t h e l i s t function [ List ]= create ( list1 ) global List ; // C r e a t e t h e c u r r e n t node i n t h e l i s t list1 . number = input ( ” I n p u t a number ( Type −999 t o end ) ; ” ) if list1 . number == -999 then list1 . next = NULL ; list1 . add = NULL ; else // C r e a t e t h e n e x t node i n t h e l i s t list1 . add = list1 . add +1; list1 . next = NULL ; List ( i ) = list1 ; if ( i ==1) then else 109 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 List (i -1) . next = List ( i ) . add end i = i +1; create ( list1 ) ; // C a l l c r e a t e ( ) f u n c t i o n end return ; endfunction // F u n c t i o n t o p r i n t t h e numbers o f l i s t function []= print1 ( list1 ) if list1 ( i ) (1) . next ~= NULL then printf ( ”%d−−−>” , list1 ( i ) (1) . number ) ; // P r i n t c u r r e n t item i = i +1; if list1 ( i ) (1) . next == NULL then printf ( ”%d” , list1 ( i ) (1) . number ) ; end print1 ( list1 ) ; // Move t o n e x t i t e m end return ; endfunction // F u n c t i o n t o c o u n t t h e number o f i t e m s i n t h e l i s t function []= count ( list1 ) global c ; if list1 ( i ) (1) . next == NULL then return ; else i = i +1; c=i; count ( list1 ) ; end return ; endfunction // C r e a t e t h e s t r u c t u r e i . e . node node = struct ( ’ number ’ ,0 , ’ add ’ ,0 , ’ n e x t ’ ,0) ; head = node ; global List ; // C a l l i n g t h e f u n c t i o n s List = create ( head ) ; 110 60 print1 ( List ) ; 61 global c ; 62 c =1; 63 count ( List ) ; 64 // P r i n t t h e t o t a l number o f i t e m s 65 printf ( ” \nNumber o f i t e m s = %d” ,c ) ; Scilab code Exa 13.4 Insert the item before the specified key node // Example 1 3 . 4 // W r i t e a f u n c t i o n t o i n s e r t a g i v e n i t e m b e f o r e a s p e c i f i e d node known a s 3 // key node . 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 funcprot (0) ; // C r e a t e t h e l i s t function [ List ]= create ( list1 ) global List ; // C r e a t e t h e c u r r e n t node list1 . number = input ( ” I n p u t a number ( Type −999 t o end ) ; ” ) ; if list1 . number == -999 then list1 . next = NULL ; list1 . add = NULL ; else list1 . add = list1 . add +1; list1 . next = NULL ; List ( i ) = list1 ; if ( i ==1) then else List (i -1) . next = List ( i ) . add end 111 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 i = i +1; create ( list1 ) ; // C r e a t e s t h e n e x t node end return ; endfunction // F u n c t i o n t o i n s e r t t h e i t e m b e f o r e t h e s p e c i f i e d key node function [ List ]= insert ( list1 ) x = input ( ” V a l u e o f new i t e m ? ” ) ; printf ( ” V a l u e o f key i t e m ? ( B e f o r e which you want to i n s e r t ?) ”); key = scanf ( ”%d” ) ; while list1 ( i ) (1) . next ~= NULL i = i +1; end j = i +1; // Find t h e key node and i n s e r t t h e new node while ( list1 ( i ) (1) . number ~= key ) list1 ( i +1) (1) . number = list1 ( i ) (1) . number ; i =i -1; if ( i ==0) then printf ( ” Item n o t Found ” ) ; return ; end end list1 ( i +1) (1) . number = list1 ( i ) (1) . number list1 ( i ) (1) . number = x ; // I n s e t t h e new node b e f o r e t h e key node list1 ( j ) (1) . add = j ; list1 ( j ) (1) . next = NULL ; list1 (j -1) (1) . next = list1 ( j ) (1) . add ; List = list1 ; endfunction // F u n c t i o n t o p r i n t t h e numbers o f l i s t function []= print1 ( list1 ) if list1 ( i ) (1) . next ~= NULL then printf ( ”%d−−−>” , list1 ( i ) (1) . number ) ; // P r i n t c u r r e n t item 112 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 i = i +1; if list1 ( i ) (1) . next == NULL then printf ( ”%d” , list1 ( i ) (1) . number ) ; end print1 ( list1 ) ; // Move t o n e x t i t e m end return ; endfunction global List ; NULL =0; i =1; // C r e a t e t h e s t r u c t u r e i . e . node node = struct ( ’ number ’ ,0 , ’ add ’ ,0 , ’ n e x t ’ ,0) ; head = node ; // C a l l i n g t h e f u n c t i o n s List = create ( head ) ; printf ( ” \ n O r i g i n a l L i s t : ” ) ; print1 ( List ) ; List = insert ( List ) ; printf ( ” \nNew L i s t : ” ) ; print1 ( List ) ; Scilab code Exa 13.5 Delete a specified node in the list 1 2 3 4 5 6 7 8 9 // Example 1 3 . 5 // W r i t e a program / f u n c t i o n t o d e l e t e a s p e c i f i e d node . funcprot (0) ; // C r e a t e t h e l i s t function [ List ]= create ( list1 ) global List ; // C r e a t e t h e c u r r e n t node list1 . number = input ( ” I n p u t a number ( Type −999 t o 113 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 end ) ; ” ) // s c a n f ( ”%d” ) ; if list1 . number == -999 then list1 . next = NULL ; list1 . add = NULL ; else list1 . add = list1 . add +1; list1 . next = NULL ; List ( i ) = list1 ; if ( i ==1) then else List (i -1) . next = List ( i ) . add end i = i +1; create ( list1 ) ; // C r e a t e t h e n e x t node end return ; endfunction // F u n c t i o n t o p r i n t t h e numbers o f l i s t function []= print1 ( list1 ) if list1 ( i ) (1) . next ~= NULL then printf ( ”%d−−−>” , list1 ( i ) (1) . number ) ; // P r i n t c u r r e n t item i = i +1; if list1 ( i ) (1) . next == NULL then printf ( ”%d” , list1 ( i ) (1) . number ) ; end print1 ( list1 ) ; // Move t o n e x t i t e m end return ; endfunction // F u n c t i o n t o d e l e t e t h e s p e c i f i e d node function [ List ]= delet ( list1 ) key = input ( ” V a l u e o f i t e m number t o be d e l e t e d ? ” ) ; // Read v a l u e o f key // Find and d e l e t e t h e key node while ( list1 ( i ) (1) . number ~= key ) then if list1 ( i ) (1) . next == NULL then 114 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 printf ( ” Item n o t f o u n d i n t h e l i s t ” ) ; return ; end i = i +1; end while ( list1 ( i ) . next ~= NULL ) list1 ( i ) (1) . number = list1 ( i +1) (1) . number ; i = i +1; end list1 (i -1) (1) . next = NULL ; List = list1 ; endfunction global List ; NULL =0; i =1; // C r e a t e t h e s t r u c t u r e i . e . node node = struct ( ’ number ’ ,0 , ’ add ’ ,0 , ’ n e x t ’ ,0) ; head = node ; // C a l l i n g t h e f u n c t i o n s List = create ( head ) ; printf ( ” \ n O r i g i n a l L i s t : ” ) ; print1 ( List ) ; List = delet ( List ) ; printf ( ” \ n A f t e r d e l e t i o n L i s t i s : ” ) ; print1 ( List ) 115