Description

Sample Problem

Let’s do the concert ticket program mentioned in the Instructor Comments for 6 performers. Enter a performer number (0 to 5) and a number of tickets sold and it will accumulate the tickets for that performer into an array. This will keep going until “999” is entered for the performer number, at which time the inputting will stop and the totals for all 6 performers will display.

Solution

1) The IPO for this program is:

Ticket Counter Program
Input Variables:
performerNumber
ticketsSold

Processing Variables:
index

Output Variables:
ticketsArray[6]

Algorithm:
1) enter the performerNumber
2) while performerNumber ! = 999
      if performerNumber < 0 or > 5
           display error message
      else
           input ticketsSold
           ticketsArray[performerNumber] = ticketsSold
          enter the performerNumber
       end if     
    end loop
3) for index = 0 to 5
      display index, ticketsArray[index]
   end loop
end

2) The C++ code is:

// Ticket Counter Program
// Intro C++, Lesson 10
// Written by Bill H, Nov 2013

#include <iostream>
using namespace std;

int main( )
{
   //Declare variables
  int performerNumber = 0;
  int ticketsSold = 0;
  int ticketsArray[6] = {0};
 

   //Prompt the user for performer number
   cout << “Enter the performer number (0-5): “;
   cin >> performerNumber;
 

   //Loop until done
  while (performerNumber != 999)
  {
       if (performerNumber < 0  || performerNumber > 5)
             cout <<  “Sorry, there is no performer with that number” <<endl;
      else
       {
cout << “Enter the number of tickets sold: “;
         cin >> ticketsSold;
         ticketsArray[performerNumber]  +=  ticketsSold;
        }//end if
 

      // Prompt the user for performer number
      cout << “Enter the performer number (0-5): “;
      cin >> performerNumber;
 

   }//end loop
 

//Display the results
       for (int index = 0; indx <6; index++)
       cout << “Performer ”  << index << ” sold ” << ticketsArray[index] << ” tickets. ” << endl;
 

   system(“pause”);
   return 0;
} //end of main

3) Now, let’s modify it. Instead of entering a performer number, let’s enter a performer name.  This makes the code more complicated, as we have to add a parallel array and search it, but it’s easier on the users who would be more able to remember a name than a digit. This time the loop will stop by entering the word “end” instead of 999.

4) The IPO for this program is

Better Ticket Counter Program
Input Variables:
performerName
ticketsSold

Processing Variables:
nameArray[6]
searchIndex
displayIndex

Output Variables:
ticketsArray[6]

Algorithm:
1) enter the performerName
2) while performerName ! = “end”
 

      searchIndex = 0   
      while searchIndex < 6  && performerName ! = nameArray[searchIndex]
            searchIndex++
      end loop                

       if searchIndex < 6
            input ticketsSold
            ticketsArray[searchIndex] = ticketsSold
       else
           display error message
       end if
 

        enter the performerName
   end loop

3)for displayIndex = 0 to 5
      display nameArray[displayIndex], ticketsArray[displayIndex]
  end loop
end
 

5) The C++ code is

// Better Ticket Counter Program
// Intro C++, Lesson 10
// Written by Bill H, Nov 2013

#include <iostream>
#include <string>
using namespace std;

int main( )
{
   //Declare variables
  string performerName = ” “;
  int searchIndex = 0;
  int ticketsSold = 0;
  int ticketsArray[6] = {0};
  string nameArray[6] = {“U2”, “Clarkson”, “Adele”, “Underwood”, “Perry”, “Green”};

   //Prompt the user for performer name
   cout << “Enter the performer name (type  end  to quit): “;
   cin >> performerName;
 

   //Loop until done
  while (performerName != “end”)
  {
int searchIndex = 0;   
     while (searchIndex < 6  && performerName != nameArray[searchIndex])
             searchIndex++;
 

     if (searchIndex < 6)
         {
            cout << “Enter the number of tickets sold: “;
            cin >> ticketsSold;
             ticketsArray[searchIndex]  +=  ticketsSold;
         }//end if
       else
           cout <<  “Sorry, there is no performer with that name” <<endl;
 

//Prompt the user for performer name
      cout << “Enter the performer name (type  end  to quit): “;
      cin >> performerName;
 }//end loop
 

//Display the results
       cout << endl;
       for (int displayIndex = 0; displayIndex <6; displayIndex++)
         cout << “Performer ”  << nameArray[displayIndex] << ” sold ” << ticketsArray[displayIndex] << ” tickets. ” << endl;
 

   system(“pause”);
   return 0;
} //end of main