Arrays and Assignment 8

Arrays

See pages 360-369 in A Computer Science Tapestry for more information

Computer programs are often used to keep track of large amounts of data.  The way we have kept track of data so far, we would have to make a new variable for each piece of data.  For example, suppose you were creating a school database that kept track of students and their years of graduation.   You could create variables like this to perform this task:

apstring name1, name2, name3, name4...

int gradyear1, gradyear2, gradyear3, gradyear4...

You can see that doing this for an entire school of 1500 students would not be particularly effective.  Fortunately, there is another way.  Variables can be tracked in lists called arrays.  We have used this same idea in Visual Basic.  Essentially, an array is a list of related variables of the same type.  This could be a list of names, ages, grades etc. 

The way we will be using arrays requires the use of a new header file. Download apvector.cpp and apvector.h by right clicking on these files and saving them in the APClasses folder which should be on your H drive.  While C++ has built in arrays, using arrays via the AP Vector class has some advantages, and will be the way that we use arrays.  To declare and apvector array you use the following line:

apvector<variabletype> variablename(numberofelements);

Where variabletype is the type of variable used int the array (e.g. int, apstring, double), variablename is the name you will use to address the array, and numberof elements is the maximum number of values in the array.  Note that arrays are numbered from 0, so that an array with four elements has them numbered 0, 1, 2, 3.

The following example shows the use of some arrays.   It defines three people's names in one array and keeps their years of high school graduation in another array.  The two arrays are linked by making sure that the first person in the name array has their year of graduation first in the gradyear array, the second person in the name array has their year of graduation second in the gradyear array etc.  It is useful to think about the arrays as lists as shown here before converting them to arrays:

Array Value Name GradYear
1 Eric 1988
2 Nina 1998
3 Isaac 2001

The program then asks for a person's name and will print out their graduation year if their name is in the list.  If their name is not in the list it lets the user know this.

Here is the code for the program.  You may simply cut and paste it into a new console application cpp file and try it out.

#include <apvector.h>

#include <apstring.h>

#include <iostream.h>



int main(void)

{

    apvector<int> gradyear(4);

    apvector<apstring> name(4);

    apstring whatname;

    int i, found;

    

    found=0;

    name[1]="Eric";

    gradyear[1]=1988;

    name[2]="Nina";

    gradyear[2]=1998;

    name[3]="Isaac";

    gradyear[3]=2001;



    cout<<"What person do you wish to look up? ";

    cin>>whatname;

    

    for(i=1;i<=3;i++)

    {

        if(name[i]==whatname)

	{

            cout<<"Graduation year is "<<gradyear[i]<<endl;

            found=1;

	}

    } 

    if(found=0) cout<<"Sorry, unknown person"<<endl; 

    return 0;

} 

Notice how it gets the year of graduation once it finds the person's name.  The for loop counts from one to three.  As it does this it looks up the person's name so that the first time through the loop, i equals 1, and the name is name[1] which is Eric.  If that is the name that was entered, it prints out gradyear[1].  If not it continues with i equal to 2.  Try making modifications to this program to understand what each of the lines do.

Assignment 8

Persons magazine is publishing a list of the 10 richest people in show business.  They would like a program that would allow them to enter the 10 people on the list and will then tell them out of those people who made the most money and who made the least.  They would also like this program to tell them the average salary of the people on the list.

Fortunately they have contracted the best programmers int the business for this task (you).  Your assignment is to write the program as defined above.  If you cruise through the program also add the facility to edit names on the list and lookup names on the list.  If you are having difficulty with the program just start with the maximum salary part and add others as necessary.  Passing arrays into functions is a bit tricky, so you can write the whole program in main for now.   We will cover passing arrays to functions later.  If you are interested now, you can get some information here.

Click Here to Download a DOS Version of the Program comptr9.gif (546 bytes)

This assignment is due Thursday April 16.  It should be put in Assignment 8 folder by that time.  It will be graded on the following factors.

  1. Does it work
  2. How well is the program commented and how well variables are named
  3. How well are arrays and for loops used
  4. How appropriate were the number of functions
  5. Creativity

Back to Home Page House3.wmf (25540 bytes)