University of Calgary

simple arrays

Simple Arrays 

Description:

This assignment entails writing three simple programs aimed at helping you better understand arrays.

Question 1:

    Write a program that sums up the numbers in an array

  • At the beginning of the program, ask the user how many numbers he wishes to enter. There should be no limit to the size of this number. (Hint, use dynamic memory to create an array)
  • Sum up the numbers in the array and display it using standard out.

Question 2:

    Write a program that will print out all possible combinations of a number

  • prompt the user to enter 5 numbers
  • print out all possible combinations of these numbers to the screen

Question 3:

    Take the line of code given below:

        char array[]="Hello There";

  • What is the size of the array?
  • Trace through the two functions given below, what will they each display. After you have your answer, take the code and run it to see if your correct.

    NOTE: max = 11 for both functions

Without compiling and running the code, what will this function display

 void printShowNull(char array[], int max)
  {
    for(int index=0; index<max+1; index++)
        cout<<"index "<<index<<"---> '"<<array[index]<<"' -- '"
              <<array[max-index]<<"' <--- index "<<max-index<<endl;
  }

What will this function display

void printSkipNull(char array[],int max)
{
    int index=0;

    while(index<max)
    {
       cout<<"index "<<index<<"---> '"<<array[index]<<"' -- '"
             <<array[max-(index+1)]<<"' <--- index "<<max-index-1<<endl;

       index ++;
   }
}

Concepts:

  • Arrays
  • Dynamic memory
  • Looping structures


Updated: August 7, 2005 05:20 PM