Return to Topic Menu | Computer Science Main Page | MathBits.com  | Terms of Use | Resource CD

Demo Program - String Array and Functions


Task:
 Create a function to assign the names of 5 movie stars into an array.  Create a function to print the array. 
 

#include <iostream.h>
#include <stdlib.h>
#include <apvector.h>
#include "apstring.cpp"

void fillActors(apvector <apstring>&stars);
void printActors(apvector <apstring>&stars);

int main(void)
{
     system ("CLS");
     apvector <apstring> stars(5);

     //Call function to fill the array
     fillActors(stars);

     //Call function to print the array
     printActors(stars);

     cout<<endl<<endl;
     return 0;
}
//Function to "drudge" fill array
void fillActors(apvector <apstring>&stars)
{
     stars[0]="Keanu Reeves";
     stars[1]="Patrick Stewart";
     stars[2]="Julia Roberts";
     stars[3]="William Shatner";
     stars[4]="Leonard Nimoy";
     return;
}
//Function to print array
void printActors(apvector <apstring>&stars)
{
     int len = stars.length();
     for(int i = 0; i < len; i++)
     {
          cout<<stars[i] << endl;
     }
     return;
}