lab 2: sizes of many grids

EN47/COMP9, Fall 2009

Out: September 24, 3:00pm
Due: October 1, 3:00pm

the problem

In this lab, you will write a program to calculate the sizes of a series of rectangular grids, as well the average grid size. To accomplish your goal, you will need to use a loop, integer and float variables, and if/else statements.

The user interaction with your program should look something like below. Note: The underlined text is entered by the user; you have no control over the formatting of user input.

     How many grids? 3

     How many lines in grid 1? 10
     How many collumns? 3
     The grid size is 30 cells.

     How many lines in grid 2? 5
     How many columns? 3
     The grid size is 15 cells.
     The average grid size so far is 22.5 cells.

     How many lines in grid 3? 9
     How many columns? 7
     The grid size is 63 cells.

     The average grid size so far is 36 cells.
     The sizes of 3 grids were computed.

If no area is calculated, the output should look like:

     How many grids? 0
     No grid size was computed.

the algorithm

Here is one possible approach to this problem. You are welcome to use this algorithm as a guide in developing your solution.

  1. Start out with the total number of cells (sizes) of all grids and the number of grids so far, both set to 0
  2. Read in the number of grids to be given.
  3. For each grid:

    1. Read in its number of lines.
    2. Read in its number of columns.
    3. Calculate its size.
    4. Report its size.
    5. Add its size to the total number of cells of all grids.
    6. Add 1 to the number of grids seen so far.
    7. If the dimensions of more than 1 grid have been given:

      1. Calculate the average size of grids so far.
      2. Report the average size of grids so far.
  4. If no grids at all were given, report that no grid size was computed.

the program

After reading the problem description and sample algorithm, work out your program on paper. You can write out the plan in C++ code or in pseudocode, the informal description style we have seen in lecture which follows the structure of a program but may omit detailed syntax. You may like to refer to your grid_size.cpp code from last week. Some points to keep in mind:

  • Note that for the first grid, there is no output of average area.
  • The average size of the grids can be a real number (e.g. 11.5). Make sure that integer division doesn't mess up any of your answers! Use the float casting operator, if necessary, to make sure that when you compute the average, either the numerator or the denominator in the division is a float.
  • If you divide by zero, your program will crash!
  • Check your loop. Will it stop? (Control-C will exit a program if it is caught in an infinite loop).

Once you have what seems to be a working program on paper, it's time to type it in and try it out.

creating a skeleton program

Create a new directory called lab2. In lab2, create a new file using Emacs:

emacs grids_avg.cpp &

Now type in a skeleton program for grids_avg.cpp:

     // File: grids_avg.cpp 
     // Name: [put your name here]
     // Date: [put the date here]
     // Lab 2: Calculate the sizes and average size of n grids 

     #include <iostream>
     #include <string> 

     using namespace std; 

     int main() {

       return 0;
     }

compiling the program

Save your code, then bring the Terminal window to the foreground and use ls to confirm that your file grids_avg.cpp is listed. Compile your program using g++:

g++ grids_avg.cpp

If all goes well, you will see nothing, and the shell will give you another Linux prompt. The compiler translated your C++ program into machine language, and put the result in an executable file called a.out. This should be familiar from last week's lab.

Now let's try something a little different. a.out is not a very informative name for a program, is it? Let's run the compiler again:

g++ -o grids_avg grids_avg.cpp

With the extra information in the command above (the "-o grids_avg" option), the compiler will generate an executable file called grids_avg instead of using the default name a.out. You can then run the compiled program by typing

./grids_avg

coding and debugging

Now, fill in the details of your program following your plan on paper. Save your file, and compile your program.

Test the performance of your program carefully, using a few normal and extreme cases. (e.g. Try one run with no grids, and another with one of the grids being of size 0.)

When you are satisfied and confident that your program works as you expect, submit the code using provide, and print a copy for the TA as well.

provide comp9 lab2 grids_avg.cpp

extra credit

Modify your program so that the user does not have to enter the number of grids. Instead, ask the user if he or she has another grid to enter before collecting the numbers of lines and columns. For example:

     Is there another grid? (y/n) y 
     How many lines? 10
     How many columns? 3
     The grid size is 30 cells.

     Is there another grid? (y/n) y
     How many lines? 5
     How many columns? 3
     The grid size is 15 cells.
     The average grid size so far is 22.5 cells.

     Is there another grid? (y/n) n 
     The sizes of 2 grids were computed.

Do not modify your grids_avg.cpp file (which you should have submitted). Rather, make a copy to a new file named grids_avg_extra.cpp using the command:

cp grids_avg.cpp grids_avg_extra.cpp

When you are satisfied and confident that your extra credit program works correctly, submit it using this command:

provide comp9 lab2ec grids_avg_extra.cpp