Challenge 74

Description

The purpose of this challenge is to use arrays and functions.

Requirements

  1. Write a function void get_info(string & state, string & capital, int & population). This function will simply prompt the user to enter 3 separate values, respectively representing a State, a Capital city, and its population. Notice that the three parameters are passed by reference
  2. Write a function void show(string states[], string capitals[], int populations[], int count). This function will simply loop through each of the arrays, displaying each arrays’ elements. The count variable is used to determine how many elements of the array will actually be displayed. Sample output of this function should look as below (count is 2 in this example):
      CA, Sacramento (1000)
      OR, Salem (2000)
    
  3. Write a function int total(int array[], int size). This function will simply loop through array to calculate the total of the elements. The size parameter dictates how many of the elements are used in the calculation. The return value of the function is the calculated total.
  4. See the sample main below, copying the starting code given. Write the additional code in main as follows:
    1. Write a loop that runs SIZE iterations. Within this loop:
      1. Call the get_info() function passing in state, capital and pop appropriately. Because get_info() passes parameters by reference, after the function call, the variables passed in will contain the user’s inputs
      2. Fill in the states, capitals and populations arrays with these three user inputs at the proper element locations. Your loop’s counter variable is perfectly suited to use as an index for these arrays.
      3. Increment count. This count variable represents  how many sets of information have been entered (In this case, one set comprises a state, a capital and a population)
      4. Call the show() function passing in the appropriate parameters
      5. Display a message showing the total population entered so far (See the sample interaction). Remember that you wrote the total() function that can be used to calculate the total population.

Sample Main

int main()
{
  const int SIZE = 5;
  string states[SIZE], capitals[SIZE];
  int populations[SIZE];
  int count = 0, pop;
  string state, capital;

  // More code here (See Step 4 instructions)

  return 0;
}

Sample Interaction

Enter state: CA
Enter capital: Sacramento
Enter population for Sacramento: 1000

CA, Sacramento (1000)
Total population so far: 1000

Enter state: OR
Enter capital: Salem
Enter population for Salem: 2000

CA, Sacramento (1000)
OR, Salem (2000)
Total population so far: 3000

Enter state: TX
Enter capital: Austin
Enter population for Austin: 4000

CA, Sacramento (1000)
OR, Salem (2000)
TX, Austin (4000)
Total population so far: 7000

...and so on...

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL00074

Print Requirements