Benutzer-Werkzeuge

Webseiten-Werkzeuge


papierkorb:test1-3.blk

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

papierkorb:test1-3.blk [2025-08-16 17:50] – ↷ Seite von projects:test1-3.blk nach papierkorb:test1-3.blk verschoben mkapapierkorb:test1-3.blk [Unbekanntes Datum] (aktuell) – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1
Zeile 1: Zeile 1:
-=== MID TERM EXAM 3  === 
-<code> 
-Screen 0 not modified      
- 0 \                                              13:36JWB02/16/86  
- 1 \ Last change:   Screen  002                   11:21jwb10/22/87  
-                                                                  
-                      MATHEMATICS  495                            
-                                                                  
-                                                                  
-         INTRODUCTION TO THE FORTH PROGRAMMING LANGUAGE           
-                                                                  
-                                                                  
-                       MID TERM EXAM                              
-10                                                                  
-11                     OCTOBER 26, 1987                             
-12                                                                  
-13                     TOTAL  MARKS = 25                            
-14                                                                  
-15                                                                  
  
- 
-Screen 1 not modified      
- 0 \ QUESTION 1 ( 5 MARKS )                       11:02jwb10/22/87  
- 1 The following words are not part of the Forth 83 standard.       
- 2 Write short colon definitions for them using only words from     
- 3 your forth 83 reference sheet.                                   
-                                                                  
- 5 Example:      3DUP ( a b c   a b c a b c )                       
- 6 Solution:   : 3DUP  2 PICK 2 PICK 2 PICK ;                       
-             stack    stack                                       
-     word    before   after                                       
-     ----    ------   -------                                     
-10 a)  TUCK   ( a b c   a c b c )                                   
-11 b)  -ROT   ( a b c   c a b )                                     
-12 c)  2OVER1 ( a b c   a b c a b )                                 
-13 d)  SPIN   ( a b c   c b a )                                     
-14 e)  NIP    ( a b c   a c )                                       
-15                                                                  
- 
- 
-Screen 2 not modified      
- 0 \ QUESTION 2 ( 5 MARKS )                       11:21jwb10/22/87  
- 1 Design the FORTH word TRIANGLE that will draw the outline for    
- 2 a triangle on the display using X' TRIANGLE uses and consumes  
- 3 the top stack number to decide how many lines will be used to    
- 4 make the isosceles triangle. See the sample output below left.   
-                    | Notes:                                      
- 6 3 TRIANGLE                 Your solution should illustrate     
-                  | the concept of factoring ( more than one    
-  XXX               | word definition should be created).         
- 9 XXXXX              |                                             
-10 5 TRIANGLE              Your solution should illustrate either 
-11                  |  the use of the DO .... LOOP  construct     
-12    XXX              or  the     BEGIN .... UNTIL construct.    
-13   XXXXX            |                                             
-14  XXXXXXX                Your word should work for values from  
-15 XXXXXXXXX          | 1 thru 23.  No action results for others.   
- 
- 
-Screen 3 not modified      
- 0 \ QUESTION 3 ( 5 MARKS )                       16:15JWB02/16/86  
- 1 A open rectangular tank has a square ends, b meters by b meters, 
- 2 and is l meters long.                                            
- 3 a) Write the word AREA that takes two stack inputs base, and     
-    length and leaves the surface area, a= 2bb +3bh, on the stack 
-    AREA  ( b l   a )                                             
- 6 b) Write the word VOLUME that takes two stack inputs base, and   
-    length and leaves the volume, v=bbh, on the stack.            
-    VOLUME ( b l  v )                                             
- 9 c) Write the word TANK that takes two stack inputs base, and     
-10    length and uses the words AREA and VOLUME above to produce    
-11    a report. For example, 2 3 TANK should give the report below. 
-12    Square end of the tank is    meters.                        
-13    Length of the tank is    meters.                            
-14    The surface area of the tank is   26  square meters.          
-15    THe volume of the tank is   12  cubic meters.                 
- 
- 
-Screen 4 not modified      
- 0 \ QUESTION 4 ( 5 MARKS )                       16:49JWB10/27/85  
- 1 a) Show how you would create a variable called  VALVE-SETTING .  
-    The VALVE-SETTING will be used to contain the percentage      
-    that a valve is open ( 0 is closed, 100 full open).           
- 4 b) Design the word VALVE-STATUS to display the current value of  
-    this variable.   Execution of VALVE-STATUS would display:     
-    Valve is now  XX percent open.                                
- 7 c) Write  %VALVE-OPEN  that sets  variable   VALVE-SETTING       
-    to the top stack number.  Issue a warning and do nothing      
-    if an attempt  is made  to set it outside the range 0 - 100.  
-10 d) Write %VALVE-INC which increments VALVE-SETTING by top stack  
-11    number and %VALVE-DEC which decrements it by the top stack #. 
-12 e) Can the VALVE-SETTING ever become less than 0 or greater      
-13    than 100 using your words of (d).  If so fix them so they     
-14    do nothing and issue a warning message if an attempt to       
-15    increment or decrement out of range is made.                  
- 
- 
-Screen 5 not modified      
- 0 \ QUESTION 5 ( 5 MARKS )                       16:19JWB02/16/86  
- 1 a) Create an array called MARKS that will hold 5 16bit numbers.  
- 2 b) Write a word called CLEAR-MARKS which initializes             
-    each cell of the array MARKS to zero.                         
- 4 c) Write a word called SHOW-MARKS that displays and identifies   
-    each value  in the MARKS array in tabular format:             
-    1   1st mark                                                  
-    2   2nd mark   etc.                                           
- 8 d) Write a word called TOTAL  that computes the sum of all non   
-    zero entries in the array MARKs and leaves the result in      
-10    the variable SUM .  Save the non zero count in variable N .   
-11 e) Write a word called MARK-AVG that computes the average of     
-12    all non zero entries in the array MARKS and stores the        
-13    result in the variable AVERAGE and displays the result as:    
-14    Your current marks average is  nnn                            
-15    MARK-AVG should round correctly to the nearest integer.       
- 
-Screen 8 not modified      
- 0 \  32 bit square root KS  4TH DIM V4N1P9                         
-                                                                  
- 2 : EASY-BITS ( drem1 partial.root1 count   drem2  partial.root2 ) 
-      DO  >R  D2*  D2*                                          
-            R@  -  DUP  0<                                        
-            IF    R@ + R> 2*  1-                                  
-            ELSE       R> 2*  3  +                                
-            THEN  LOOP  ;                                         
-                                                                  
- 9 : 2'S-BIT ( drem2 proot2   drem3  proot3 ) \ get penultimate bit 
-10      >R  D2*  DUP  0<                                            
-11      IF   D2*  R@  -  R>  1+                                     
-12      ELSE D2*  R@  2DUP  U<                                      
-13           IF   DROP  R> 1-                                       
-14           ELSE  -    R> 1+                                       
-15      THEN THEN ;                                                 
- 
- 
-Screen 9 not modified      
- 0 \  32 bit square root KS  4TH DIM V4N1P9                         
- 1 : 1'S-BIT   ( drem3 proot3   fullroot )  \ remainder lost        
-      >R  DUP  0<                                                 
-      IF    2DROP  R>  1+                                         
-      ELSE  D2*  32768 R@  DU<  0=  R>  THEN ;                    
- 5 \ 32-bit unsigned radicand to 16-bit unsigned square root        
- 6 : SQRT     ( ud      )                                         
-          1 8 EASY-BITS  ROT  DROP  6 EASY-BITS                 
-         2'S-BIT  1'S-BIT  ;                                      
- 9 \ Display square root of 16-bit number with 3 decimal places.    
-10 : .SQRT  ( n   -- )  \ n  must be < 4096                         
-11         16 *  62500  UM*                                         
-12         SQRT  0 <#  # # #  ASCII . HOLD  #S #>                   
-13         TYPE  SPACE ;                                            
-14 : TEST  100 0 DO CR I 5 .R  SPACE I .SQRT  LOOP ;                
-15                                                                  
- 
- 
-Screen 11 not modified      
- 0 \ ANSWER TO QUESTION 5  ( 5 MARKS )            20:25JWB02/17/86  
- 1 a)  TUCK   ( a b c   a c b c )                                   
-         SWAP OVER ;                                              
-                                                                  
- 4 b)  -ROT   ( a b c   c a b )                                     
-         ROT ROT ;                                                
-                                                                  
- 7 c)  2OVER1 ( a b c   a b c a b )                                 
-         2 PICK 2 PICK ;                                          
-                                                                  
-10 d)  SPIN   ( a b c   c b a )                                     
-11          SWAP ROT ;                                              
-12                                                                  
-13 e)  NIP    ( a b c   a c )                                       
-14         SWAP DROP ;                                              
-15                                                                  
- 
- 
-Screen 12 not modified      
- 0 \ ANSWER TO QUESTION NUMBER 2  ( 5 MARKS )     20:25JWB02/17/86  
- 1 : X' ( n   -- )                                                
-       0 ?DO ASCII X EMIT LOOP ;                                  
-                                                                  
- 4 : .ROW ( n r   -- )                                              
-       2DUP - SPACES 2 * 1- X'S DROP ;                            
-                                                                  
- 7 : (TRIANGLE) ( n  -- )                                           
-       DUP 0 ?DO  CR DUP I 1+ .ROW LOOP DROP ;                    
-                                                                  
-10 : TRIANGLE  ( n   -- )                                           
-11       DUP 1 < IF   DROP ." Zero or negative not allowed"         
-12               ELSE DUP 23 <                                      
-13                    IF (TRIANGLE)                                 
-14                    ELSE DROP ." Larger than 23 not allowed"      
-15               THEN THEN ;                                        
- 
- 
-Screen 13 not modified      
- 0 \ ANSWER TO QUESTION 3  ( 5 MARKS )            18:04JWB02/16/86  
-  : AREA ( b l   a )                                              
-      OVER DUP *  2 * ROT ROT  * 3 * +  ;                         
-  : VOLUME ( b l   v )                                            
-      OVER * *  ;                                                 
-                                                                  
-  :  TANK ( b l  --  )                                            
-    CR OVER ." The square end of the tank is " . ." meters."      
-    CR DUP  ." The length of the tank is " . ." meters."          
-       OVER OVER                                                  
-10    CR ." The surface area of the tank is " AREA .                
-11       ." square meters."                                         
-12    CR ." The volume of the tank is " VOLUME .                    
-13       ." cubic meters."                                          
-14    CR  ;                                                         
-15          TANK                                                
- 
- 
-Screen 14 not modified      
- 0 \ ANSWER TO QUESTION 4 ( 5 MARKS)              20:22JWB02/17/86  
- 1 ( a) VARIABLE  VALVE-SETTING                                     
- 2 ( b) : VALVE-STATUS ( --  -- ) CR ." Valve is now  "             
-          VALVE-SETTING @ . ." percent open." ;                   
- 4 ( c) : [IN]  ( x a b   f)  2 PICK < NOT ROT ROT < NOT AND ;      
-      : WARN  ( --  -- ) ." Invalid operation attempted. " ;      
-      : %VALVE-OPEN  ( n  -- )                                    
-        DUP 0 100 [IN] IF  VALVE-SETTING ! ELSE DROP WARN THEN ;  
- 8 ( d) : %VALVE-INC   ( n  -- ) VALVE-SETTING +! ;                 
-      : %VALVE-DEC   ( n  -- ) NEGATE  VALVE-SETTING +! ;         
-10 ( e) : %VALVE-INC   ( n  -- )                                    
-11         VALVE-SETTING @ +  DUP 0 100 [IN]                        
-12         IF VALVE-SETTING ! ELSE DROP WARN THEN ;                 
-13      : %VALVE-DEC   ( n  -- )                                    
-14         VALVE-SETTING @ SWAP - DUP 0 100 [IN]                    
-15         IF VALVE-SETTING ! ELSE DROP WARN THEN ;                 
- 
- 
-Screen 15 not modified      
- 0 \ ANSWER TO QUESTION 5:  ( 5 MARKS )           18:36JWB02/16/86  
- 1 ( a) CREATE MARKS  70 ,  0 ,  80 , 0 , 65 ,   \ average=72       
- 2 ( b)  : CLEAR-MARKS ( --  -- )                                   
-          MARKS  10 0  FILL ;                                     
-       VARIABLE N   VARIABLE SUM     VARIABLE AVERAGE             
-  : MARKS@ ( i  -- )                                              
-     MARKS SWAP 2* + @ ;                                          
- 7 ( c)                                                             
-  : SHOW-MARKS ( --   -- )                                        
-    5 0 DO CR I 1+  . I MARKS@ 4 .R LOOP  ;                       
-10 ( d)                                                             
-11  : TOTAL ( --   -- ) 0 SUM ! 0 N !                               
-12    5 0 DO I MARKS@ ?DUP IF SUM +! 1 N +!  THEN  LOOP ;           
-13 ( e) : MARKS-AVG  ( --  -- )                                     
-14    TOTAL SUM @ 2 * N @ / 1+ 2 / AVERAGE !                        
-15    CR ." Your current marks average is " AVERAGE @ . ;           
- 
- 
-Screen 16 not modified      
- 0 \  Used for testing solution.                  18:34JWB02/16/86  
-                                                                  
-      : #IN  QUERY  INTERPRET ;                                   
-      : ENTER-MARKS ( --  -- )                                    
-        CLEAR-MARKS  5 0  DO                                      
-        CR I 1+ . ASCII > EMIT  #IN                               
-        I 2* MARKS + ! LOOP ;                                     
-                                                                  
-                                                                  
-                                                                  
-10                                                                  
-11                                                                  
-12                                                                  
-13                                                                  
-14                                                                  
-15                                                                  
- 
-</code> 
papierkorb/test1-3.blk.1755359401.txt.gz · Zuletzt geändert: 2025-08-16 17:50 von mka