Benutzer-Werkzeuge

Webseiten-Werkzeuge


papierkorb:test1-1.blk

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

papierkorb:test1-1.blk [2025-08-16 17:50] – ↷ Seite von projects:test1-1.blk nach papierkorb:test1-1.blk verschoben mkapapierkorb:test1-1.blk [Unbekanntes Datum] (aktuell) – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1
Zeile 1: Zeile 1:
-=== MID TERM EXAM === 
-<code> 
-Screen 0 not modified      
-                                                                  
- 1 \ Last change:   Screen  005                   16:49JWB10/27/85  
-                                                                  
-                      MATHEMATICS  495                            
-                                                                  
-                                                                  
-         INTRODUCTION TO THE FORTH PROGRAMMING LANGUAGE           
-                                                                  
-                                                                  
-                       MID TERM EXAM                              
-10                                                                  
-11                     TOTAL  MARKS = 25                            
-12                                                                  
-13                                                                  
-14                                                                  
-15                                                                  
  
- 
-Screen 1 not modified      
- 0 \ QUESTION 1 ( 5 MARKS )                       16:48JWB10/27/85  
- 1 Show the resulting stack after return is pressed.                
-                                                                  
- 3 Example:   54  21  13   SWAP                                     
-  Answer:   54  13  21    <top of stack.                          
-                                                                  
- 6 a)  13  17  23  31   ROT                                         
-                                                                  
- 8 b)  13  31  17  23   OVER                                        
-                                                                  
-10 c)  23  17  13     PICK                                        
-11                                                                  
-12 d)  11  5  -14  -5   /MOD                                        
-13                                                                  
-14 e)  10  20       */                                          
-15                                                                  
- 
- 
-Screen 2 not modified      
- 0 \ QUESTION 2 ( 5 MARKS )                       16:48JWB10/27/85  
- 1 Design a FORTH word SQUARE that will draw  the outline of a      
- 2 square on the display.  The outline will be made by using the    
- 3 word  WHITE  ( or WW )  from out CHECKER-BOARD  program.         
- 4 Note:  Center of the square should be black!!                    
-                                                                  
- 6 : WHITE   177  EMIT  ;  ( Fills  character cell with white )     
-                                                                  
-         DO EITHER  a)  OR  b)   BELOW                            
-                                                                  
-10 a) Easy Version:    SQUARE  takes no stack parameters and draws  
-11    a square   20 horizontal cells by 10 vertical cells.          
-12                                                                  
-13 b) Harder Version:  SQUARE  uses top stack number, n, and draws  
-14    a square 2n cells horizontal  and n cells vertical.           
-15                                                                  
- 
- 
-Screen 3 not modified      
- 0 \ QUESTION 3 ( 5 MARKS )                       16:47JWB10/27/85  
- 1 An open fish tank has a square base ( b inches by b inches )     
- 2 and is h inches high.  Design the word  TANK  ,  which takes     
- 3 two stack inputs,  height and base,  and  computes the tank      
- 4 surface area and tank volume.   V = bbh    S = 4bh + bb          
- 5 Output display for tank should be similar to the sample below:   
-                                                                  
- 7 18  24   TANK                                                    
-                                                                  
- 9 Base  of  tank is  24 inches.                                    
-10 Height of tank is  18 inches.                                    
-11                                                                  
-12 Surface  area  is   2304 square inches.                          
-13 Volume of tank is  10368 cubic  inches.                          
-14                                                                  
-15                                                                  
- 
- 
-Screen 4 not modified      
- 0 \ QUESTION 4 ( 5 MARKS )                       16:49JWB10/27/85  
- 1 a) Show how you would create a FORTH variable called  BALANCE    
-    that will contain the number of dollars in your bank account. 
-                                                                  
- 4 b) What are the FORTH words for fetching,  storing and           
-    incrementing the contents of variables?  Give examples of     
-    their use with  BALANCE  .                                    
-                                                                  
- 8 c) Design the word  BALANCE?  that displays current balance.     
-    ie    BALANCE?    displays:   You now have  XXXX   dollars.   
-10 d) Write  OPENING-BALANCE   that sets  variable   BALANCE        
-11    to the top stack number.                                      
-12                                                                  
-13 e) Design the word DEPOSITE  which adds  top of stack to BALANCE 
-14    and word WITHDRAW which subtracts top of stack from BALANCE . 
-15    Can you over draw your account?  Fix WITHDRAW so you can't.   
- 
- 
-Screen 5 not modified      
- 0 \ QUESTION 5 ( 5 MARKS )                       16:49JWB10/27/85  
- 1 a) Create and array called DATA that will hold 50 16bit numbers. 
-                                                                  
- 3 b) Write a word called CLEAR-DATA which initializes              
-    each cell of the array DATA to zero.                          
-                                                                  
- 6 c) Write a word called SUM-DATA accumulates the sum of           
-    the positive DATA values in the variable +SUM and the sum of  
-    the negative DATA values in the variable -SUM .               
-                                                                  
-10 d) Write a word called ENTER-DATA that uses a indefinite loop    
-11    ( ...  BEGIN ...  WHILE ... REPEAT .. )  to prompt the        
-12    user to enter data and stores it in succesive cells of the    
-13    DATA  array.  Fix it so that an entry of zero terminates      
-14    data entry.   Assume you have the word #IN in your system.    
-15                                                                  
- 
- 
-Screen 6 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 7 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 1  ( 5 MARKS )                                
- 1 Show the resulting stack after return is pressed.                
-                                                                  
- 3 Example:   54  21  13   SWAP                                     
-  Answer:   54  13  21    <top of stack.                          
-                                 Answers below:                   
- 6 a)  13  17  23  31   ROT        13  23  31  17                   
-                                                                  
- 8 b)  13  31  17  23   OVER       13  31  17  23  17               
-                                                                  
-10 c)  23  17  13     PICK       23  17  13  23                   
-11                                                                  
-12 d)  11  5  -14  -5   /MOD       11    -4                     
-13                                                                  
-14 e)  10  20       */         10  28                           
-15                                                                  
- 
- 
-Screen 12 not modified      
- 0 \ ANSWER TO QUESTION 2  ( 5 MARKS )                              
- 1 : WW   177  EMIT  ;  ( Fills  character cell with white )        
- 2 \  a) Easy Version:                                              
-    : TOP   CR 20 0 DO  WW  LOOP ;                                
-    : ROW   CR WW 18 0 DO SPACE LOOP WW ;                         
-    : SQUARE  TOP  8 0 DO ROW LOOP TOP CR CR ;                    
-                                                                  
- 7 \  b) Harder Version:                                            
-    : .TOP  ( n  -- )                                             
-         CR 2* 0 ?DO WW LOOP ;                                    
-10    : .ROW  ( n  -- )                                             
-11         CR WW 2* 2- 0 ?DO SPACE LOOP WW ;                        
-12    : .SQUARE ( n  -- )                                           
-13          DUP 2 < ABORT" Can't do it!!"                           
-14          DUP .TOP  DUP  2- 0                                     
-15          ?DO  DUP .ROW LOOP .TOP CR CR ;                         
- 
- 
-Screen 13 not modified      
- 0 \ ANSWER TO QUESTION 3  ( 5 MARKS )                              
-                                                                  
-  : .BH  ( h b  -- )                                              
-      CR ." Base  of  tank is"  4 .R    ."  inches."              
-      CR ." Height of tank is"  4 .R    ."  inches."   ;          
-  : .AREA ( h b  -- )                                             
-       DUP DUP *  ROT ROT  * 4 * +                                
-       CR ." Surface  area  is"  8 .R    ."  square inches."  ;   
-  : .VOL  ( h b  -- )                                             
-       DUP * *                                                    
-10       CR ." Volume of tank is" 8 .R ."  cubic  inches."  ;       
-11  :  TANK ( h b  --  )                                            
-12         CR 2DUP .BH  CR 2DUP .AREA .VOL CR CR ;                  
-13     18  24   TANK                                                
-14                                                                  
-15                                                                  
- 
- 
-Screen 14 not modified      
- 0 \ ANSWER TO QUESTION 4  ( 5 MARKS )                              
- 1 ( a)    VARIABLE  BALANCE                                        
- 2 \ b  fetching  @   storing !   incrementing  +!                  
- 3 \    BALANCE  @    ( leave current value of BALANCE on stack )   
- 4 \  5 BALANCE  !    ( initialize BALANCE to 5 dollars )           
- 5 \  2 BALANCE +!    ( increment  BALANCE by 2 dollars )           
- 6 ( c) : BALANCE?  ( --   -- )                                     
-          BALANCE @  CR  ."  You now have "  .  ." dollars." ;    
- 8 ( d) : OPENING-BALANCE  ( n  -- )                                
-          BALANCE ! ;                                             
-10 ( e)  : DEPOSITE ( n  -- )                                       
-11             BALANCE +!   ;                                       
-12       : WITHDRAW ( n  -- )                                       
-13          BALANCE @  OVER - 0<                                    
-14          IF DROP ." Insufficient funds!"                         
-15          ELSE  NEGATE BALANCE +! THEN   ;                        
- 
- 
-Screen 15 not modified      
- 0 \ ANSWER TO QUESTION 5:  ( 5 MARKS )                             
- 1 ( a) CREATE  DATA  100 ALLOT                                     
- 2 ( b)  : CLEAR-DATA ( --  -- )                                    
-           DATA  100 0  FILL ;                                    
- 4 ( a)  VARIABLE +SUM     VARIABLE -SUM                            
-  : SUM-DATA  ( --   -- )                                         
-    0 +SUM !  0 -SUM !                                            
-    50 0 DO DATA I 2* + @  DUP 0= IF DROP LEAVE THEN              
-            DUP  0< IF -SUM ELSE +SUM THEN +!  LOOP ;             
-                                                                  
-10      : #IN  QUERY  INTERPRET ;                                   
-11 ( d) : ENTER-DATA ( --  -- )                                     
-12        CLEAR-DATA  0  BEGIN                                      
-13        CR DUP . ASCII > EMIT  #IN DUP 0<>                        
-14        WHILE  OVER 2* DATA + ! 1+ REPEAT  2DROP ;                
-15                                                                  
- 
-</code> 
papierkorb/test1-1.blk.1755359401.txt.gz · Zuletzt geändert: 2025-08-16 17:50 von mka