CSE 221 Lab 8


Table of Contents


CSE 221 Lab 8-Part 1


Table of Contents


Objectives


The Problem

Your job is to create another operation for the TA-8500 creating a Root operation. In case you did not complete Lab 7 successfully, you will be provided with a procedure body for Natural_Power_2::Power after 4:00 pm, Monday. You can start this assignment with your own solution to Lab 7 and continue to use that, or you may switch to the provided solution.


Set Up

  1. In an xterm window, create a new directory (folder) for this lab by typing the "make directory" command:
         mkdir lab08-part1
  2. Change or move to the new directory (folder) for this lab by typing the "change directory" command:
         cd lab08-part1
  3. Copy the catalog for this lab by typing the "copy" command:
        cp -R /class/sce/now/221/labs/catalogs/lab08-part1/* . 
    NOTE: The period . is part of the command. It denotes the current directory (folder).

    This "copy command" will copy recursively (because of the -R) all the files and directories from the shared class directory into your directory, effectively duplicating the entire substructure from the class directory into your directory.



  4. To see what was copied, type the "list" command:
         ls
    You should see

    In the directories AI and CI, you will see a Natural subdirectory. Within AI/Natural you will see two files: Power.h and Root.h. Within CI/Natural you will see one file: Root_1.h.


VERY IMPORTANT NOTE

If you did not successfully complete the implementation of Natural_Power_2::Power, you'll need to get a copy of this component after 4:00 pm, Monday. You can do this as follows:
  1. Using the "change directory" cd command, move to the lab08-part1/CI/Natural directory.

  2. Copy the Power_2.h file by typing the "copy" command:
        cp  /class/sce/now/221/labs/catalogs/lab08-part1/CI/Natural/Power_2.h . 
    NOTE: The period . is part of the command. It denotes the current directory (folder).


Method

Step 1 - Coding Root as a global operation

  1. Examine the file Root_Test.cpp, which is a simple test driver for global_procedure Root specified below:
    1. global_procedure Root (
              alters Natural_Power_2& n,
              preserves Integer r
          );
      /*!
          requires
              r > 0
          ensures
              n^(r) <= #n < (n+1)^(r)
      !*/
  2. Write a body for the Root operation that uses the interval halving algorithm as illustrated in the "Interval Halving" worksheet. This body goes into Root_Test.cpp. Notice that n is of type Natural_Power_2, so in addition to all the Natural_Number operations you may also invoke the Power operation on n -- and you'll need to do this in order to implement the interval halving algorithm!

  3.  
  4. Test the above implementation, revising as necessary until you're convinced that this implementation of Root works. Remember, to do this you'll need the file Power_2.h that contains a correct body for Power. So if you haven't got this from Lab 7, you'll need to complete this step after 5:00 pm, Saturday, May 30. The grader will look at the source code of Root_Test.cpp and the output of Root_Test to grade this section of the lab.

Step 2 - Making Root an extension

  1. Examine the file Root_1_Test.cpp, which is the same simple test driver as above but this time for Root as an extension in abstract_instance class Natural_Root, which is in the file AI/Natural/Root.h in your lab08-part1 directory.

  2.  
  3. Write a body for the Root operation in the file CI/Natural/Root_1.h, in your lab08-part1 directory, using the interval halving algorithm. DO NOT write the body in the test driver. This should be very staightforward if you completed the first part of the lab!

  4.  
  5. Test Natural_Root_1::Root, revising the operation body as necessary until you're convinced that this implementation of Root works. The grader will look at the source code of Root_1_Body.h and the output of Root_1_Test to grade this part.

What To Turn In

Use the rcpp-submit command to turn in: Make sure you are in your lab08-part1 directory. Then the submit command for this lab assignment is: where "xx" is to be replaced by lower case letters designating your section (see the course home page for a list).


Want To Do More?

Here are some possibilities: Any extra work is strictly optional, for your own benefit, and will not directly affect your grade.


CSE 221 Lab 8-Part 2


Table of Contents


Objectives


The Problem

Your job is to build on the ideas from closed lab 8 to create an even fancier display of natural numbers.


Set Up

  1. In an xterm window, create a new directory (folder) for this lab by typing the "make directory" command:
         mkdir lab08-part2
  2. Change or move to the new directory (folder) for this lab by typing the "change directory" command:
         cd lab08-part2
  3. Copy the catalog for this lab by typing the "copy" command:
        cp -R /class/sce/now/221/labs/catalogs/lab08-part2/* . 
    NOTE: The period . is part of the command. It denotes the current directory (folder).

    This "copy command" will copy recursively (because of the -R) all the files and directories from the shared class directory into your directory, effectively duplicating the entire substructure from the class directory into your directory.



  4. To see what was copied, type the "list" command:
         ls
    You should see

    In the directories AI and CI, you will see a Natural subdirectory. Within AI/Natural you will see the file Put_To_With_Commas.h and within CI/Natural you will see the file Put_To_With_Commas_1.h.


Method

The file Nice_Display.cpp is a slightly modified version of the test driver for closed lab 8. You will need to do the following.
  1. In the Global Context section of Nice_Display.cpp, fill in the two template instantiations creating


  2. Provide operation bodies implementing Create_Display_Representation and Display_Width.

  3. Modify program_body main () of Nice_Display.cpp so that it displays a "NICE_DISPLAY" of the natural numbers that are in the sequence object called s.

  4. After successfully compiling Nice_Display.cpp, you can test it by typing the command
    Nice_Display < data.txt
    in a terminal window.


What To Turn In

Use the rcpp-submit command to turn in the test driver Nice_Display.cpp. Make sure you are in your lab08-part2 directory. Then the submit command for this lab assignment is: where "xx" is to be replaced by lower case letters designating your section (see the course home page for a list).