BU CAS CS 113
Introduction to Computer Science II with Intensive C
Fall 1997


Assignment 2: Arrays and Pointers in C


Last Modified: Tue Sep 23 18:40:37 1997

Deadline

September 25, 1997

What to Submit

You should submit five files. Be sure the files you submit have exactly the following names: hw2-util.h, hw2-util.c, hw2-stats.h, hw2-stats.c, hw2-bank.c .

These programs are to be electronically submitted by using the submit program on csa. The code you submit should conform with the program assignment guidelines.

Assignment

The first two programs are based on exercises in the book, but note the changes I have made. The third is a modification of one of the programs from assignment 1.
  1. Dynamic Arrays (see Programming Abstractions in C Chapter 2, Exercise 15)

    Write a function

        int *GetDynamicIntArray(int sentinal, string message, int *sizePtr);
    
    which repeatedly displays message, reads an integer from standard input, and adds that integer to the array until a value of sentinal is given. The function should return the dynamically allocated array (i.e., a pointer to an int) and set the value of *sizePtr to the effective size of the array.

    Since you don't know how large the array will be until the user has been inputing values, you will need to start with an array (dynamically allocated) of a certain size and copy its contents to a new, larger array if more values are entered than fit in the array. Your initial size should be 5, and every time the array is full, build a new one which is twice as large. Don't forget to deallocate the memory for the old array.

    Since this is a function which might be useful for many applications, we will put it into a small package called hw2-util. The function prototype (listed above) and comments about how to use the function go in hw2-util.h, the implementation and comments regarding the implementation go in hw2-util.c

    Files: hw2-util.h, hw2-util.c.

  2. Histograms (see Programming Abstractions in C Chapter 2, Exercise 6)

    Write a function which produces a histogram for an array of integer values.

    void PrintHistogram(int *scoresArray, int size, int lo, int hi, int step);
    
    scoresArray is to be treated as an array of integers of size size. Your histogram should have a row for scores below lo, then as many as are needed to have subdivisions of size step, and finally one for values which are greater than the values in the last subdivision. The valuehi should be contained in the last subdivision, but might not actually be the highest value in it. (In the example in the book, the first and last row are missing.)

    This function should go into a package called hw2-stats. Once hw2-util and h2-stats have been finished, you should be able to compile hw2-histogram.c which reads in a list of integer scores and prints a histogram. You can test this using the makefile (see below).

    Files: hw2-stats.h, hw2-stats.c.

  3. Savings and Loan: Comparing plans

    Make two changes to your bank program from assignemnt 1.

    Often when getting a loan or initiating a savings program, you have choices about interest rates, size of loans, size of payments, initial amounts, etc. In order to facilitate a comparison rewrite your bank program so that it can handle up to 4 plans.

    Your program should display a suitable message informing the user about what the program does and what sorts of inputs will be required and then repeatedly:

    A sample run should look something like the following:

    
    Instructions go here.
    
    Be sure to add comments about negative input values.
    
    
    Enter interest rate 1 (negative value to quit):  7
    Enter initial amount 1:  $-90000
    Enter payment amount 1:  $650
    
    Enter interest rate 2 (negative value to quit):  8
    Enter initial amount 2:  $-90000
    Enter payment amount 2:  $650
    
    Enter interest rate 3 (negative value to quit):  9
    Enter initial amount 3:  $-90000
    Enter payment amount 3:  $650
    
    Enter interest rate 4 (negative value to quit):  -2
    Enter number of years:  30
    
    1: init amnt: $-90000.00; rate: 7.0000%; payment: $650.00
    
    2: init amnt: $-90000.00; rate: 8.0000%; payment: $650.00
    
    3: init amnt: $-90000.00; rate: 9.0000%; payment: $650.00
    
    yrs mos    balance 1    balance 2    balance 3
    --- ---    ---------    ---------    ---------
      0   0    -90000.00    -90000.00    -90000.00 
      0   1    -89875.00    -89950.00    -90025.00 
      0   2    -89749.27    -89899.67    -90050.19 
      0   3    -89622.81    -89849.00    -90075.56 
      0   4    -89495.61    -89797.99    -90101.13 
      0   5    -89367.67    -89746.64    -90126.89 
      0   6    -89238.98    -89694.96    -90152.84 
      0   7    -89109.54    -89642.92    -90178.99 
      0   8    -88979.34    -89590.54    -90205.33 
      0   9    -88848.39    -89537.81    -90231.87 
      0  10    -88716.67    -89484.73    -90258.61 
      0  11    -88584.19    -89431.30    -90285.55 
      1   0    -88450.93    -89377.50    -90312.69 
      2   0    -86789.87    -88703.34    -90654.71 
      3   0    -85008.74    -87973.22    -91028.82 
      4   0    -83098.85    -87182.50    -91438.02 
      5   0    -81050.89    -86326.16    -91885.60 
      6   0    -78854.88    -85398.73    -92375.18 
      7   0    -76500.13    -84394.33    -92910.67 
      8   0    -73975.15    -83306.57    -93496.40 
      9   0    -71267.64    -82128.52    -94137.08 
     10   0    -68364.40    -80852.70    -94837.86 
     11   0    -65251.29    -79470.98    -95604.37 
     12   0    -61913.13    -77974.58    -96442.79 
     13   0    -58333.65    -76353.98    -97359.86 
     14   0    -54495.42    -74598.87    -98362.95 
     15   0    -50379.71    -72698.09    -99460.14 
     16   0    -45966.49    -70639.54   -100660.26 
     17   0    -41234.23    -68410.14   -101972.96 
     18   0    -36159.87    -65995.69   -103408.79 
     19   0    -30718.69    -63380.85   -104979.32 
     20   0    -24884.17    -60548.98   -106697.17 
     21   0    -18627.87    -57482.06   -108576.17 
     22   0    -11919.30    -54160.59   -110631.43 
     23   0     -4725.76    -50563.44   -112879.49 
     24   0      2987.79    -46667.73   -115338.44 
     25   0     11258.96    -42448.68   -118028.05 
     26   0     20128.05    -37879.45   -120969.96 
     27   0     29638.29    -32930.97   -124187.85 
     28   0     39836.03    -27571.77   -127707.59 
     29   0     50770.96    -21767.77   -131557.52 
     30   0     62496.37    -15482.03   -135768.59 
    
    
    Enter interest rate 1 (negative value to quit):  -2
    
    

    To get you started, here is a shell you may (but are not required to) use. It includes some of the basic structure, but you need to fill in most of the comments and code. Remember that you are now to move all of the input phase into a separate function.

    Files: hw2-bank.c

    Executables

    There is an executable for the banking problem. Type ex2-bank to run it.

    Makefile

    To check that your assignment compiles correctly, use the Makefile for this assignment:
    1. Copy this Makefile into the directory where your programs are.
    2. Name it Makefile.
      • Note:If you named it Makefile.a2, you will have to use the mv (short for move) command to rename it by typing
        mv Makefile.a2 Makefile
        or the cp command to copy it by typing
        cp Makefile.a2 Makefile
    3. type: make.

    You can also use this Makefile to compile individual programs by typing, for example, make hw2-bank. The obvious generalization works for the other files as well.

    If you type make test , your programs will be given a minimal test to see if they handle input correctly. Tests similar to this will be used for grading, so if this doesn't work correctly, find out why.

    Can you figure out by looking at the Makefile what make tidy and make scratch do? Try it and find out.


    Academic Honesty and Collaboration

    It is reasonable to discuss with others possible general approaches to problems. It is unreasonable to work together on a detailed solution, to copy a solution, or to give away a solution. If your common discussion can be detected by looking at the solutions, then there is too much collaboration. Such instances of academic dishonesty may result in a course grade of F or expulsion from Boston University.

    Do not allow your work to be used by others:

    Warning: If someone cheats by using your work, you will also be penalized.