-
need help with arrays!
hey guyz i am try ing to write this following program:
A theatre seating chart is implemented as a two-dimensional array of ticket prices, like this:
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 20 20 20 20 20 20 10 10
10 10 20 20 20 20 20 20 10 10
10 10 20 20 20 20 20 20 10 10
20 20 30 30 40 40 30 30 20 20
20 30 30 40 50 50 40 30 30 20
30 40 50 50 50 50 50 50 40 30
Write a program that prompts users to pick either a seat or a price. Mark sold seats by changing the price to 0. When a user specifies a seat, make sure it is available. When a user specifies a price, find any seat with that price starting at the front and working to the back of the theatre. Make sure you write your program using well defined functions.
this is wat i have so far:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int ROWS = 9;
int ISLES = 10;
int counts[ROWS][ISLES] =
{
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 20, 20, 30, 30, 40, 40, 30, 30, 20, 20 },
{ 20, 30, 30, 40, 50, 50, 40, 30, 30, 20 },
{ 30, 40, 50, 50, 50, 50, 50, 50, 40, 30 },
};
}
void getdata (const counts[][], int ROW, int ISLE, int pick)
{
cout<<"Press (1) if you would like to pick a seat and press (2) if you would like to enter a price: "<<endl;
cin>>pick;
if (pick == 1)
{
cout<<"Please enter the row you would like to sit in: "<<endl;
cin>>ROW;
cout<<"Please enter the isle you would like to sit in: "<<endl;
cin>>ISLE;
}
else if (pick ==2)
{
cout<<"Please enter the price: "<<endl;
cin>>price;
}
}
i really dont know wat to do next. can somebody give me hints on how to go about doing this?
-
so you came back to the retards eh? NASA too busy today haha
my hint >
to do your 'find a seat at that price starting from front '
use two nested for loops and decrement the counters starting from the max aisles and max rows values,
this way you will count backwards through the 'theatre' from the bottom right corner until you hit the first one of the value that matches,
set this row, aisle = 0; then set your loops to the exit value -1 so you break them and drop into the outer control loop you will have already set up to send flow back to top and get more input whatever
-
For the getdata function, I suggest that you change it to:
Code:
void getdata (int counts[][AISLES])
{
cout << "Press (1) if you would like to pick a seat and press (2) if you would like to enter a price: " << endl;
int pick;
cin >> pick;
int row, aisle;
int price;
if (pick == 1)
{
cout << "Please enter the row you would like to sit in: " << endl;
cin >> row;
cout << "Please enter the isle you would like to sit in: " << endl;
cin >> aisle;
}
else if (pick == 2)
{
cout << "Please enter the price: " << endl;
cin >> price;
}
}
Now, you can implement the pick == 1 case by checking if row is in the range [0, ROWS) and aisle is in the range [0, AISLES). If so, you presumably would inform the user of the price, then mark the seat as taken by setting the price to 0.
To implement the pick == 2 case, I suggest using a loop. You could write this loop in another function, and pass row and aisle to this function by reference. When the function's loop has found a matching price, it sets the row and aisle and returns true. If no seat with that price exists, it returns false.
EDIT:
rogster001, read the forum guidelines, in particular #7. [Okay, this is resolved.]
EDIT #2:
I noticed something else: your array is actually a variable length array. Since ROWS and ISLES (AISLES, rather) are fixed, you should declare them as global constants instead.
-
ROWS and ISLES should be const in main. Otherwise you are using compiler extensions and not standard C++.
Also, const counts[][] will not compile. You need to specify at least the second dimension.
-
Pseudo-code
Code:
Two arrays, A[M][M] and B[M][M];
Let A hold the array of seats and associated price, initialize B with -1.
Ask for input -> Row, Isle
for j = 0, M
for k = 0, M
{
if ((j == Row) && (k == Isle))
B[j][k] = 0
else B[j][k] = A[j][k]
}
If you print B the given seat and isle will be purchased at the existing price, want to prompt the user if they want the price? Figure it out. ;)
Code:
Ask for input -> Price
for j = 0, M
for k = 0, M
{
if (A[j][k] == Price)
B[j][k] = 1
else B[j][k] = 0
}
If you print out B all seats that match given price will be marked '1' otherwise '0'.
Wanna know how to put the two together based on asking for input only once? Another challenge I'm sure you can handle.
Psuedo-code may contain errors, I just typed it up in the chat box... and this is far from the best approach, but it should be an intuitive one. I also included no conditions to check for already taken seats and the like, you can figure that out I'm sure.
Tip: maintain a 'source'/reference array that you never modify, ever, otherwise the application decays in to an incoherent and meaningless state.
Tip: go through C++ tutorials over and over again until you get it, you clearly refuse to learn, stop it.