diff options
Diffstat (limited to '662/CH8')
-rwxr-xr-x | 662/CH8/EX8.11/file01.sci | 40 | ||||
-rwxr-xr-x | 662/CH8/EX8.12/first_file.sci | 19 | ||||
-rwxr-xr-x | 662/CH8/EX8.13/Example8_13.sci | 21 | ||||
-rwxr-xr-x | 662/CH8/EX8.2/Example8_2.sci | 23 | ||||
-rwxr-xr-x | 662/CH8/EX8.3/Example8_3.sci | 39 | ||||
-rwxr-xr-x | 662/CH8/EX8.3/example8_3.jpeg | bin | 0 -> 24920 bytes | |||
-rwxr-xr-x | 662/CH8/EX8.4/Example8_4.jpg | bin | 0 -> 13436 bytes | |||
-rwxr-xr-x | 662/CH8/EX8.4/Example8_4.sci | 61 | ||||
-rwxr-xr-x | 662/CH8/EX8.5/Example8_5.sci | 43 | ||||
-rwxr-xr-x | 662/CH8/EX8.5/example8_5.jpeg | bin | 0 -> 23540 bytes | |||
-rwxr-xr-x | 662/CH8/EX8.7/Example8_7.jpeg | bin | 0 -> 45270 bytes | |||
-rwxr-xr-x | 662/CH8/EX8.7/Example8_7.sci | 38 | ||||
-rwxr-xr-x | 662/CH8/EX8.8/file1.sci | 13 | ||||
-rwxr-xr-x | 662/CH8/EX8.9/mainSimulation.sci | 35 |
14 files changed, 332 insertions, 0 deletions
diff --git a/662/CH8/EX8.11/file01.sci b/662/CH8/EX8.11/file01.sci new file mode 100755 index 000000000..bfcd65d4a --- /dev/null +++ b/662/CH8/EX8.11/file01.sci @@ -0,0 +1,40 @@ +//Programming Example 8.11
+//Seach for maximum
+ //First File
+funcprot(0);
+cnst=0.0001
+function[] = file1()
+ global yl
+ global yr
+ global a
+ global b
+ global xl
+ global xr
+ global cnst //external variables
+
+ //read input data (interval end points)
+ printf("\n a=");
+ a=scanf("%lg");
+ printf("\n b=");
+ b=scanf("%lg");
+
+ //interval reduction loop
+ first="yes";
+ reduce();
+ while((yl <> yr ) & ((b-a)>(3*cnst)))
+ reduce();
+ end
+ xmax = 0.5 *( xl + xr );
+ ymax=curve(xmax);
+
+ printf("\n xmax=%8.6f ymax=%8.6f", xmax, ymax);
+endfunction
+
+ //calling routine
+path=get_absolute_file_path("file01.sci");
+PATH=path+"file02.sci";
+exec(PATH);
+path1=get_absolute_file_path("file01.sci");
+PATH1=path1+"file03.sci";
+exec(PATH1);
+file1()
\ No newline at end of file diff --git a/662/CH8/EX8.12/first_file.sci b/662/CH8/EX8.12/first_file.sci new file mode 100755 index 000000000..af57ed774 --- /dev/null +++ b/662/CH8/EX8.12/first_file.sci @@ -0,0 +1,19 @@ +//Programming Example 8.12
+//Program to calculate successive fibonacci numbers
+ //First File
+
+function[]=mainFibonacci()
+ printf("How many Fibonacci numbers?");
+ n=scanf("%d");
+ for(count=1:n)
+ printf("\n i= %2d f=%1d", count, Fibonacci(count));
+ end
+endfunction
+
+ //calling routine
+p=get_absolute_file_path("first_file.sci");
+P=p+"second_file.sci";
+exec(P);
+global first; // To implement the functionality of static
+first="true";
+mainFibonacci();
\ No newline at end of file diff --git a/662/CH8/EX8.13/Example8_13.sci b/662/CH8/EX8.13/Example8_13.sci new file mode 100755 index 000000000..fab4ae0b6 --- /dev/null +++ b/662/CH8/EX8.13/Example8_13.sci @@ -0,0 +1,21 @@ +//Programming Example 8.13
+//simple compound interest problem
+
+function[]=mainCI()
+ //read input data(including prompts)
+ printf("Please enter a value for the principle:(p)");
+ p=scanf("%f");
+ printf("Please Enter a value for the interest rate(r): ")
+ r=scanf("%f");
+ printf("Please Enter a value for the number of years(n): ")
+ n=scanf("%f");
+
+ //calculate i, then f
+ i= r/100;
+ f=p*(n^(1+i));
+ //display the output
+ printf("\n The final value(F) is: %.2f\n", f);
+endfunction
+
+//calling routine
+mainCI();
\ No newline at end of file diff --git a/662/CH8/EX8.2/Example8_2.sci b/662/CH8/EX8.2/Example8_2.sci new file mode 100755 index 000000000..5741c374d --- /dev/null +++ b/662/CH8/EX8.2/Example8_2.sci @@ -0,0 +1,23 @@ +//Programming Example 8.2
+// calculate the factorial of an integer quantity
+
+funcprot(0)
+function[] = mainfact()
+ //read in the integer quantity
+ printf("\nn=")
+ n=scanf("%d")
+ //calculate and display the factorial
+ printf("\nn! = %d",Factorial(n))
+endfunction
+
+function[prod] = Factorial(n) //calculate the factorial
+ prod=1
+ if n > 1 then
+ for i = 2 : n
+ prod = prod * i
+ end
+ end
+ return(prod);
+endfunction
+// calling routine
+mainfact()
\ No newline at end of file diff --git a/662/CH8/EX8.3/Example8_3.sci b/662/CH8/EX8.3/Example8_3.sci new file mode 100755 index 000000000..998e5410a --- /dev/null +++ b/662/CH8/EX8.3/Example8_3.sci @@ -0,0 +1,39 @@ +//Programming Example 8.3
+//Read several lines of text and determine the average number of characters per line
+
+funcprot(0)
+function[]= mainReadAvg()
+ //n=number of chars in a given line
+ count=0 //number of lines
+ Sum=0 //total number of characters
+ printf("Enter the text below:")
+ //raed a line of text and update the cumulative counters
+ n=linecount();
+ while ( n>0)
+ Sum=Sum+n;
+ count=count+1;
+ n=linecount();
+ end
+ avg = Sum/count; //average number of characters per line
+ printf("\nAverage number of character per line: %5.2f", avg)
+endfunction
+
+function[count] = linecount()
+ count=0
+ //as the getchar is used to used with while loop to
+ //read character array which is here replced by read command
+ // and the count of characters along with white spaces is made
+ // with the help of length().
+
+ s=read(%io(1), 1, 1, '(a)');
+ if (s == " ") then
+ return;
+ else
+ count=length(s);
+ end
+endfunction
+
+//calling routine
+mainReadAvg()
+
+
\ No newline at end of file diff --git a/662/CH8/EX8.3/example8_3.jpeg b/662/CH8/EX8.3/example8_3.jpeg Binary files differnew file mode 100755 index 000000000..0414fc4bc --- /dev/null +++ b/662/CH8/EX8.3/example8_3.jpeg diff --git a/662/CH8/EX8.4/Example8_4.jpg b/662/CH8/EX8.4/Example8_4.jpg Binary files differnew file mode 100755 index 000000000..cc1b40256 --- /dev/null +++ b/662/CH8/EX8.4/Example8_4.jpg diff --git a/662/CH8/EX8.4/Example8_4.sci b/662/CH8/EX8.4/Example8_4.sci new file mode 100755 index 000000000..d0fe91735 --- /dev/null +++ b/662/CH8/EX8.4/Example8_4.sci @@ -0,0 +1,61 @@ +//Programming Example 8.4
+//find the maximum of a function within a specified interval
+
+funcprot(0)
+CNST=0.0001;
+
+function[]= mainmax()
+ global a;
+ global b;
+ //read input data(interval end points)
+ printf("a=");
+ a=scanf("%f");
+ printf("b=");
+ b=scanf("%f");
+
+ global x1;
+ global y1;
+ global xr;
+ global yr;
+ //interval reduction loop
+ first="t";
+ while ((( y1<> yr) & (( b- a)>3*CNST) ) | (first=="t"))
+ reduce();
+ first="f";
+ end
+ //calculate xmax and ymax and display the result
+ xmax=0.5*( x1+ xr);
+ ymax=curve(xmax);
+ //disp(xmax,ymax,"xmax ymax")
+ printf("xmax=%f , yamx=%f", xmax,ymax);
+endfunction
+
+//interval reduction routine
+function[]=reduce()
+ global a;
+ global b;
+ global x1;
+ global y1;
+ global xr;
+ global yr;
+ x1= a+0.5*( b- a-CNST);
+ xr= x1+CNST;
+ y1=curve(x1);
+ yr=curve(xr);
+ if y1>yr then
+ b=xr;
+ end
+ if y1<yr then
+ a=xr
+
+ end
+endfunction
+
+//evaluate the function y=x*cos(x)
+function[sol]= curve(x)
+ sol=x*cos(x);
+endfunction
+
+// routine call
+mainmax()
+
diff --git a/662/CH8/EX8.5/Example8_5.sci b/662/CH8/EX8.5/Example8_5.sci new file mode 100755 index 000000000..b9ea72a64 --- /dev/null +++ b/662/CH8/EX8.5/Example8_5.sci @@ -0,0 +1,43 @@ +//Programming Example 8.5
+//read several lines of text & determine the average number of characters per line
+
+function[]= mainAvg()
+ global Sum //total number of characters
+ Sum=0
+ global Lines //total number of lines
+ Lines=0
+ printf("Enter the text below")
+ //read a line of text and update the cumulative counters
+ //n=number of chars in agiven line
+ //avg=average number of chars per line
+
+ n=linecount(); //expression can not b evaluated inside while,
+ //hence first calculated outside and used within while()
+ while ( n>0)
+ Sum=Sum+n;
+ Lines=Lines+1;
+ n=linecount();
+ end
+ avg = Sum/Lines; //average number of characters per line
+ printf("\nAverage number of character per line: %5.2f", avg)
+
+endfunction
+
+//read a line of text and count the number of charcters
+function[count]= linecount()
+ count=0
+ //as the getchar is used to used with while loop to
+ //read character array which is here replced by read command
+ // and the count of characters along with white spaces is made
+ // with the help of length().
+
+ s=read(%io(1), 1, 1, '(a)');
+ if (s == " ") then
+ return;
+ else
+ count=length(s);
+ end
+endfunction
+
+//calling routine
+mainAvg()
\ No newline at end of file diff --git a/662/CH8/EX8.5/example8_5.jpeg b/662/CH8/EX8.5/example8_5.jpeg Binary files differnew file mode 100755 index 000000000..f54cac793 --- /dev/null +++ b/662/CH8/EX8.5/example8_5.jpeg diff --git a/662/CH8/EX8.7/Example8_7.jpeg b/662/CH8/EX8.7/Example8_7.jpeg Binary files differnew file mode 100755 index 000000000..6adba03bd --- /dev/null +++ b/662/CH8/EX8.7/Example8_7.jpeg diff --git a/662/CH8/EX8.7/Example8_7.sci b/662/CH8/EX8.7/Example8_7.sci new file mode 100755 index 000000000..4ce62d510 --- /dev/null +++ b/662/CH8/EX8.7/Example8_7.sci @@ -0,0 +1,38 @@ +//Programming Example 8.7
+//Program to calculate successive fibonacci numbers
+
+funcprot(0);
+
+function[]= main()
+ printf("How many fibonacci numbers?");
+ n=scanf("%d");
+ first_call="true";
+ for count=1:n
+ printf("\ni = %2d F = %1d", count, fibonacci(count));
+ first_call="false";
+ end
+endfunction
+
+function[F]=fibonacci(count)
+ //calculate a fibinacci number using the formulas
+ //F=1 for i<3, and F=F1+f2 for i>=3
+ global F1
+ global F2
+ //as static variable sre not available in scilab ,
+ //hence their functionality is made through -
+ //global and first_call variables in this program.
+ if(first_call=="true") then
+ F1=1;
+ F2=1;
+ end
+ if count < 3 then
+ F=1;
+ else
+ F=F1+F2;
+ end
+ F2=F1;
+ F1=F;
+endfunction
+
+//calling routine
+main()
\ No newline at end of file diff --git a/662/CH8/EX8.8/file1.sci b/662/CH8/EX8.8/file1.sci new file mode 100755 index 000000000..f4725c3ca --- /dev/null +++ b/662/CH8/EX8.8/file1.sci @@ -0,0 +1,13 @@ +//Programming Example 8.8
+//simple , multifile program to write "Hello there!"
+ //First file
+funcprot(0)
+function[] = mainHello()
+ output();
+endfunction
+
+ //Calling Routine
+path=get_absolute_file_path("file1.sci");
+PATH=path+"file2.sci";
+exec(PATH);
+mainHello();
\ No newline at end of file diff --git a/662/CH8/EX8.9/mainSimulation.sci b/662/CH8/EX8.9/mainSimulation.sci new file mode 100755 index 000000000..4e6ac4f7c --- /dev/null +++ b/662/CH8/EX8.9/mainSimulation.sci @@ -0,0 +1,35 @@ +//Programming Example 8.9
+//Simulation of a craps game
+ //First File
+funcprot(0);
+global SEED;
+SEED = 12345;
+global x1;
+x1=1;
+global x2;
+x2=1;
+
+function[]=mainSimulation()
+ answer='Y'
+ printf("\n\n Welcome to the game of CRAPS:\n ");
+ printf("To throw dice, press RETURN\n\n");
+ rand(SEED); //initialize the random number generator
+
+ //main loop
+ answer=convstr(answer,'l');
+ while (answer<>'N')
+ play(); //external function
+ printf("\n Do you want to play again? (Y/N): ");
+ answer=scanf("%c");
+ printf("\n");
+ answer=convstr(answer,'u');
+ end
+ printf("Bye, have a nice day");
+endfunction
+
+ //calling routine
+//getting and executing path of called file
+o=get_absolute_file_path('mainsimulation.sci');
+o=o+"play.sci";
+exec (o);
+mainSimulation();
|