Categories
C++

Learning Goal: I’m working on a c++ question and need an explanation and answer

Swamped with your writing assignments? Take the weight off your shoulder!

Submit your assignment instructions

Learning Goal: I’m working on a c++ question and need an explanation and answer to help me learn.
All the instructions are in the images.
This is the sample homework assignment.
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 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
using namespace std;
int main( )
{
//Declare variables
int performerNumber = 0;
int ticketsSold = 0;
int ticketsArray[6] = {0};

//Prompt the user for performer number
cout <> performerNumber;

//Loop until done
while (performerNumber != 999)
{
if (performerNumber 5)
cout << "Sorry, there is no performer with that number" <<endl;
else
{
cout <> ticketsSold;
ticketsArray[performerNumber] += ticketsSold;
}//end if

// Prompt the user for performer number
cout <> 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
#include
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 <> performerName;

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

if (searchIndex < 6)
{
cout <> ticketsSold;
ticketsArray[searchIndex] += ticketsSold;
}//end if
else
cout << "Sorry, there is no performer with that name" <<endl;

//Prompt the user for performer name
cout <> 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

Swamped with your writing assignments? Take the weight off your shoulder!

Submit your assignment instructions

Leave a Reply