diff options
Diffstat (limited to '662/CH7')
-rwxr-xr-x | 662/CH7/EX7.1/ex7_1.sci | 23 | ||||
-rwxr-xr-x | 662/CH7/EX7.10/ex7_10.sci | 25 | ||||
-rwxr-xr-x | 662/CH7/EX7.11/ex7_11.sci | 102 | ||||
-rwxr-xr-x | 662/CH7/EX7.12/ex7_12.jpeg | bin | 0 -> 13528 bytes | |||
-rwxr-xr-x | 662/CH7/EX7.12/ex7_12.sci | 18 | ||||
-rwxr-xr-x | 662/CH7/EX7.13/ex7_13.sci | 74 | ||||
-rwxr-xr-x | 662/CH7/EX7.14/ex7_14.JPG | bin | 0 -> 16080 bytes | |||
-rwxr-xr-x | 662/CH7/EX7.14/ex7_14.sci | 20 | ||||
-rwxr-xr-x | 662/CH7/EX7.16/ex7_16.jpeg | bin | 0 -> 29637 bytes | |||
-rwxr-xr-x | 662/CH7/EX7.16/ex7_16.sci | 28 | ||||
-rwxr-xr-x | 662/CH7/EX7.4/ex7_4.sci | 9 | ||||
-rwxr-xr-x | 662/CH7/EX7.5/ex7_5.sce | 12 | ||||
-rwxr-xr-x | 662/CH7/EX7.7/ex7_7.sci | 9 | ||||
-rwxr-xr-x | 662/CH7/EX7.8/ex7_8.sci | 23 | ||||
-rwxr-xr-x | 662/CH7/EX7.9/ex7_9.sci | 27 |
15 files changed, 370 insertions, 0 deletions
diff --git a/662/CH7/EX7.1/ex7_1.sci b/662/CH7/EX7.1/ex7_1.sci new file mode 100755 index 000000000..aaa73570c --- /dev/null +++ b/662/CH7/EX7.1/ex7_1.sci @@ -0,0 +1,23 @@ + //Example 7.1
+//lowercase to uppercase character conversion using a programmer defined function
+funcprot(0);
+
+function[c2] = lower_to_upper (c1) //function definition
+//as conditional (?:) operater is not present in scilab, so we need to use if-else instead.
+ if (ascii(c1) >= ascii('a')) &(ascii(c1) <= ascii('z')) then
+ c2=ascii(ascii('A')+ascii(c1)- ascii('a'));
+ else
+ c2=c1;
+ end
+ return c2;
+endfunction
+
+function[] = main()
+ printf("Please enter a lowercase character: ");
+ lower= scanf("%c");
+ upper= lower_to_upper (lower); //function call
+ printf("\n The uppercase equivalent is %c\n\n",upper);
+endfunction
+
+//calling main function.
+main();
\ No newline at end of file diff --git a/662/CH7/EX7.10/ex7_10.sci b/662/CH7/EX7.10/ex7_10.sci new file mode 100755 index 000000000..a9dfacee9 --- /dev/null +++ b/662/CH7/EX7.10/ex7_10.sci @@ -0,0 +1,25 @@ + //Programming Example 7.10
+ clc
+//Calculating Factorials
+function[] = main()
+ //read in the integer quantity
+ printf("\nEnter integer value to find factorial of");
+ printf("\nn = ");
+ n = scanf("%d");
+ //Calculate and display the factorial
+ printf("\n n! = %d", Factorial(n));
+endfunction
+
+function[prod] =Factorial(n)
+ //calculate the factorial of n
+ prod = 1;
+ if n > 1 then
+ for i = 2: 1: n
+ prod=prod*i;
+ end
+ end
+ return prod
+endfunction
+
+//calling routine
+main();
diff --git a/662/CH7/EX7.11/ex7_11.sci b/662/CH7/EX7.11/ex7_11.sci new file mode 100755 index 000000000..625c6a348 --- /dev/null +++ b/662/CH7/EX7.11/ex7_11.sci @@ -0,0 +1,102 @@ +//Programming Example 7.11
+//Simulation of a game of chance (Shooting Craps)
+
+funcprot(0);
+SEED = 12345;
+
+function[]=main()
+ 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
+
+//Simulate one complete game
+
+function[]= play()
+ printf("\n Please throw thw dice");
+ dummy=scanf("%c");
+ printf("\n");
+ score1=throw(); //function call
+ printf("\n %2d", score1);
+ select(score1)
+
+ //win on first throw
+ case 7
+ printf("\n Congratulations! you WIN on the first throw\n");
+ case 11
+ printf(" \nCongratulations! you WIN on the first throw\n");
+
+ //lose on first throw
+ case 2
+ printf("\nSorry, you LOSE on the first throw\n ");
+ case 3
+ printf("\nSorry, you LOSE on the first throw\n ");
+ case 12
+ printf("\nSorry, you LOSE on the first throw\n ");
+
+ //additional throws are required
+ case 4
+ code();
+ case 5
+ code();
+ case 6
+ code();
+ case 7
+ code();
+ case 8
+ code();
+ case 9
+ code();
+ case 10
+ code();
+ else
+ code();
+ end
+ return;
+endfunction
+
+ //Simulate one throw of a pair of dice
+function[n] = throw()
+ x1 = rand(SEED);
+ x2 = rand(SEED);
+ n1 = 1 + int16(6* x1);
+ n2 = 1 + int16(6* x2);
+ n=n1+n2;
+ printf("\n\n n= %d\n\n", n);
+ return n;
+endfunction
+
+//code for the cases requiring additional throws
+// as in scilab we need to write code for each case seperately
+//so as not to repeat the same code again and again.
+function[] = code()
+ first="true";
+ score2=throw();
+ while((score2<> score1 & score2<>7) | first=="true")
+ first="false";
+ printf("\n Throw the dice again..");
+ dummy=scanf("%c");
+ score2=throw();
+ printf("\n%2d", score2);
+ end
+ if(score2 == score1) then
+ printf(" You WIN by matching your first score\n")
+ else
+ printf(" You LOSE by failing to match your first score\n");
+ end
+endfunction
+
+//calling routine
+main();
diff --git a/662/CH7/EX7.12/ex7_12.jpeg b/662/CH7/EX7.12/ex7_12.jpeg Binary files differnew file mode 100755 index 000000000..c27eab0bd --- /dev/null +++ b/662/CH7/EX7.12/ex7_12.jpeg diff --git a/662/CH7/EX7.12/ex7_12.sci b/662/CH7/EX7.12/ex7_12.sci new file mode 100755 index 000000000..3c04633b6 --- /dev/null +++ b/662/CH7/EX7.12/ex7_12.sci @@ -0,0 +1,18 @@ + //Example 7.12
+//function that alters the value of its argument
+function[] = main()
+ a=2;
+ printf("\na = %d (from main, before calling the function)", a);
+ modify(a);
+ printf("\n\na = %d (from main, after calling the function)", a);
+endfunction
+
+function[] = modify(a)
+ a=a*3;
+ printf("\n\na = %d(from the function, after being modified)", a);
+ return;
+endfunction
+
+//calling main() first
+funcprot(0);
+main();
diff --git a/662/CH7/EX7.13/ex7_13.sci b/662/CH7/EX7.13/ex7_13.sci new file mode 100755 index 000000000..4d422af76 --- /dev/null +++ b/662/CH7/EX7.13/ex7_13.sci @@ -0,0 +1,74 @@ + //Programming Example 7.13
+ //calculating depreciation
+function[] = main()
+ choice = 0;
+ answer1 = 'Y';
+ answer2 = 'Y';
+ while (convstr(answer1, 'u') ~= 'N')
+ //read input data
+ if (convstr(answer2, 'u') ~= 'N') then
+ printf("\n Original Value: ");
+ val = scanf("%f");
+ printf("Number of years: ");
+ n = scanf("%d");
+ end
+ printf("\n Method: (1-SL 2-DDB 3-SYD) ");
+ choice = scanf("%d");
+ select (choice)
+ case 1 then //straight-line method
+ printf("\nStraight Line Method\n\n");
+ sl(val,n);
+ case 2 then //Double declining balance method
+ printf("\nDouble-Declining-Balance Method\n\n");
+ ddb(val,n);
+ case 3 then //Sum of the years - digits method
+ printf("\nSum Of The Years - Digits Method\n\n");
+ syd(val,n);
+ end
+ printf("\n\nAnother Calculation? (Y/N) ");
+ answer1=scanf("%1s");
+ if (convstr(answer1, 'u') ~= 'N') then
+ printf("Enter a new set of data? (Y/N) ");
+ answer2 = scanf("%1s");
+ end
+ end
+ printf("\nGoodbye, have a nice day!\n");
+endfunction
+
+function[] = sl(val,n) //straight line method
+ deprec = val/n;
+ for year = 1:1:n
+ val = val-deprec;
+ writeoutput(year, deprec, val);
+ end
+ return;
+endfunction
+
+function[] = ddb(val,n) //double declining balance method
+ for year = 1:1:n
+ deprec = 2*val/n;
+ val= val-deprec;
+ writeoutput(year, deprec, val);
+ end
+ return;
+endfunction
+
+function[] = syd(val,n) //Sum of the years - digits method
+ tag= val;
+ for year = 1:1:n
+ deprec = (n-year+1)*tag/(n*(n+1)/2);
+ val = val-deprec;
+ writeoutput(year, deprec, val);
+ end
+ return;
+endfunction
+
+function[] = writeoutput(year,depreciation,value) //display output data
+ printf("End of Year %2d", year);
+ printf(" Depreciation: %7.2f", depreciation);
+ printf(" Current Value: %8.2f\n", value);
+ return;
+endfunction
+
+//calling main()
+main();
diff --git a/662/CH7/EX7.14/ex7_14.JPG b/662/CH7/EX7.14/ex7_14.JPG Binary files differnew file mode 100755 index 000000000..a5317d553 --- /dev/null +++ b/662/CH7/EX7.14/ex7_14.JPG diff --git a/662/CH7/EX7.14/ex7_14.sci b/662/CH7/EX7.14/ex7_14.sci new file mode 100755 index 000000000..68e11e2b2 --- /dev/null +++ b/662/CH7/EX7.14/ex7_14.sci @@ -0,0 +1,20 @@ + //Programming Example 7.14
+ //factorial of an integer using recursion
+function[] = main()
+ //read in the integer quantity
+ printf("n = ");
+ n=scanf("%d");
+ //calculate and display the factorial
+ printf("n! = %d\n", Factorial(n));
+endfunction
+
+function[f] = Factorial(n)
+ if (n <= 1) then
+ return (1);
+ else
+ f = n*factorial(n-1);
+ return f;
+ end
+endfunction
+//calling main()
+main();
\ No newline at end of file diff --git a/662/CH7/EX7.16/ex7_16.jpeg b/662/CH7/EX7.16/ex7_16.jpeg Binary files differnew file mode 100755 index 000000000..fce743f20 --- /dev/null +++ b/662/CH7/EX7.16/ex7_16.jpeg diff --git a/662/CH7/EX7.16/ex7_16.sci b/662/CH7/EX7.16/ex7_16.sci new file mode 100755 index 000000000..cf1862534 --- /dev/null +++ b/662/CH7/EX7.16/ex7_16.sci @@ -0,0 +1,28 @@ + //Programming Example 7.16
+ //The towers of hanoi
+function[]= main()
+ printf("Welcome to theTOWERS OF HANOI\n\n");
+ printf("How many disks? ");
+ n= scanf("%d");
+ printf("\n");
+ transfer(n,'L', 'R','C');
+endfunction
+
+function[] = transfer(n,from,to,temp)
+ //transfer n disks from one pole to another
+ //n=number of disks
+ //from=origin
+ //to=destination
+ //temp=temporary storage
+ if(n >0) then
+ //move n-1 disks frim origin to temporary
+ transfer(n-1, from, temp, to);
+ //move nth disk from origin to temporary
+ printf("Move disk %d from %c to %c \n",n, from, to);
+ //move n-1 disks from temporary to destination
+ transfer(n-1, temp,to, from);
+ end
+ return;
+endfunction
+//calling main() to start execution
+main();
diff --git a/662/CH7/EX7.4/ex7_4.sci b/662/CH7/EX7.4/ex7_4.sci new file mode 100755 index 000000000..4a5ecb022 --- /dev/null +++ b/662/CH7/EX7.4/ex7_4.sci @@ -0,0 +1,9 @@ + //Example 7.4
+//determine the larger of two quantities
+
+function[] = maximum(x, y)
+ if (x >=y) then z=x; else z=y;
+ end
+ printf("\n\nMaximum value %d", z);
+ return;
+endfunction
\ No newline at end of file diff --git a/662/CH7/EX7.5/ex7_5.sce b/662/CH7/EX7.5/ex7_5.sce new file mode 100755 index 000000000..4e68ecf07 --- /dev/null +++ b/662/CH7/EX7.5/ex7_5.sce @@ -0,0 +1,12 @@ + //Example 7.5
+//factorial calculation
+
+function[prod] = Factorial(n) //factorial is a reserve word
+ prod=1;
+ if n >1 then
+ for i = 2 :1: n
+ prod=prod*i;
+ end
+ end
+ return prod;
+endfunction
diff --git a/662/CH7/EX7.7/ex7_7.sci b/662/CH7/EX7.7/ex7_7.sci new file mode 100755 index 000000000..2e6087b22 --- /dev/null +++ b/662/CH7/EX7.7/ex7_7.sci @@ -0,0 +1,9 @@ + //Example 7.7
+ clc
+//determine the larger of two integer quantities
+function[] = maximum(x, y)
+ if (x >= y) then z=x; else z=y;
+ end
+ printf("\n\nMaximum value = %d", z);
+ return;
+endfunction
diff --git a/662/CH7/EX7.8/ex7_8.sci b/662/CH7/EX7.8/ex7_8.sci new file mode 100755 index 000000000..3e9058dba --- /dev/null +++ b/662/CH7/EX7.8/ex7_8.sci @@ -0,0 +1,23 @@ + //Example 7.8
+//convert a lowercase character to uppercase using a programmer defined function
+funcprot(0);
+
+function[c2] = lower_to_upper (c1) //function definition
+//as conditional (?:) operater is not present in scilab, so we need to use if-else instead.
+ if (ascii(c1) >= ascii('a')) &(ascii(c1) <= ascii('z')) then
+ c2=ascii(ascii('A')+ascii(c1)- ascii('a'));
+ else
+ c2=c1;
+ end
+ return c2;
+endfunction
+
+function[] = main()
+ printf("Please enter a lowercase character: ");
+ lower= scanf("%c");
+ upper= lower_to_upper (lower); //function call
+ printf("\n The uppercase equivalent is %c\n\n",upper);
+endfunction
+
+//calling main function.
+main();
\ No newline at end of file diff --git a/662/CH7/EX7.9/ex7_9.sci b/662/CH7/EX7.9/ex7_9.sci new file mode 100755 index 000000000..8332df5b2 --- /dev/null +++ b/662/CH7/EX7.9/ex7_9.sci @@ -0,0 +1,27 @@ + //Programming Example 7.9
+//determine the largest of three integer quantities
+
+function[z] = maximum(x ,y)
+ if (x >= y) then
+ z = x;
+ else
+ z = y;
+ end
+endfunction
+
+function[] = main()
+//read the integer quantities
+printf("\nEnter integer quantities ");
+ printf("\na= ");
+ a= scanf("%d");
+ printf("\nb= ");
+ b= scanf("%d");
+ printf("\nc= ");
+ c= scanf("%d");
+//calculate and display the maximum value
+ d = maximum(a,b);
+ printf("\n\nmaximum = %d ", maximum(c,d));
+endfunction
+
+//calling main function.
+main();
|