Thread: need help with arrays!

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    4

    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?

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    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
    Last edited by rogster001; 12-01-2009 at 11:59 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Last edited by laserlight; 12-01-2009 at 12:01 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    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.
    Last edited by since; 12-02-2009 at 09:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create and manipulate Terabyte size Arrays with Win32API
    By KrishnaPG in forum Windows Programming
    Replies: 1
    Last Post: 11-05-2009, 04:08 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM