Thread: 2D array project help

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    1

    Angry 2D array project help

    Well heres the story...i have this problem im about to mention and i need alot of help with it....i read the Faq and i've done the best i could to get as much of the program done on my own and now i need somewhere to turn for help...nobody i know in person can help (even my professor is hard to get in touch with) and im praying somebody here will take the time to go through this with me. basically im writing a program as if i ran a restaurant and needed to assign people to thier seats (represented by a 2D array) I have 4 arrays (booth, table, smoking booth, smoking table) when the user goes in, they are asked if they want booth or table, then asked smoking or non. From there i have to go into an array (which is allready set up and filled with 'O's to mean its empty if i did that correctly) and change the O's to X's meaning someones there. When the next user request a table i have to make sure they are assigned to a whole new section of the array meaning i dont just pile them on the same table with others. Im trying to make it so the tables hold 4 people and the booths 6. I have ALOT of problems here...for one i dont have a C compiler at home and the school lab is down. If any of you can, please go through this and tell me what i should do to fix it or make it better. I know im missing alot and it needs to be cleaned up...im desperate and out of ideas. Thanks for any help....
    Code:

    #include<stdio.h>

    char table;
    char booth;
    char smoketable;
    char smokebooth;

    // Fill the array with O's to show all seats are empty

    for(I=0; I<=24; I++)
    {for (J=0; J<= 3; J++)
    {table[I][J] = 'O';
    }
    }

    for(I=0; I<=9; I++)
    {for (J=0; J<= 3; J++)
    {booth[I][J] = 'O';
    }
    }

    for(I=0; I<=24; I++)
    {for (J=0; J<= 3; J++)
    {smoketable[I][J] = 'O';
    }
    }

    for(I=0; I<=9; I++)
    {for (J=0; J<= 3; J++)
    {smokebooth[I][J] = 'O';
    }
    }

    void main(void)
    {
    int num1;
    int num2;

    printf("Hello and welcome to Empire Diner! Would you like a booth or table? Enter 1 for table, 2 for booth")
    scanf("%d", &num1);
    printf("Will that smoking or nonsmoking for tonight? Enter 1 for smoking, 2 for nonsmoking.");
    scanf("%d", &num2);

    case 1:
    num1 = 1 && num2 = 1;
    table(); //would this be calling the table function?

    case2:
    num1 = 1 && num2 = 2;
    smoketable();

    case3:
    num1 = 2 && num2 = 1;
    booth();

    case4:
    num1 = 2 && num2 = 2;
    smokebooth();

    //here is where i'd like to ask if its another customer (start over again without clearing array) or to just end the entire program. Ideas?

    }

    void table (void)
    {
    for (table = 0, table <= 24, table++)
    {
    if table[I][J] != 'X' //X will be used to indicate that seat is occupied
    while seats < counter
    {
    if [J] > 4 //make sure you are not over the limit of seats for table
    table[I][J] = 'X'; //still have available seat so fill it
    counter++; //adds one to the counter

    else

    table[I][ ]++; //need new table to increment your table index (I)
    table[][J] = 0; //initialize seat
    table[I][J] = 'X';
    counter++;
    }
    }
    }
    The rest of the functions will look like the above one, but i wont bother writing those if this one isnt even correct yet...once i see how one should look i'll make the rest. Thanks again if you straighton this mess out....

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char table;
    char booth;
    char smoketable;
    char smokebooth;
    Those should be local variables, in the 'main' function.

    [quote]

    // Fill the array with O's to show all seats are empty

    for(I=0; I<=24; I++)
    {for (J=0; J<= 3; J++)
    {table[I][J] = 'O';
    }
    }

    for(I=0; I<=9; I++)
    {for (J=0; J<= 3; J++)
    {booth[I][J] = 'O';
    }
    }

    for(I=0; I<=24; I++)
    {for (J=0; J<= 3; J++)
    {smoketable[I][J] = 'O';
    }
    }

    for(I=0; I<=9; I++)
    {for (J=0; J<= 3; J++)
    {smokebooth[I][J] = 'O';
    }
    }
    [/code]

    All of this code is invalid. None of it is contained in a function. You cannot have global code like this. You can declare global variables, but you cannot have global code blocks, they must be contained in a function.

    Additionally, you have not declared 'smokebooth', or any of the other arrays at this time.

    void main(void)
    {
    This is wrong. 'main' ALWAYS returns an 'int'. Thus, at the end of your 'main' block, you ad the following:

    return 0; //or whatever integer you wish to return. zero is fine.

    case 1:
    num1 = 1 && num2 = 1;
    table(); //would this be calling the table function?

    case2:
    The line 'num1 = 1 && num2 = 1' is valid C code, and, luckily for you, you end up with the result you expect, but not for the reason you expect.

    This line evaluates to:

    num2 = 1
    num1 = (1 && num2) //wich turns out to be "true"

    Since thie "&&" check turns out to be true, it assigns "true" (which is 1 in this case) to "num1". If it doesn't work, it's because of an 'lvalue' error.

    I will assume you really want to make 'num1' and 'num2' both equal 1. In this case, there are a few ways to do it:

    num1 = num2 = 1;

    This assigns 1 to 'num2', and then the value of 'num2' to 'num1'. The end result is everything equals 1.

    You could also do:

    num1 = 1;
    num2 = 1; //or num2 = num1;

    The next problem in that block of code is your failure to use a 'break' statement at the end of your case.

    The way a switch works is this: it matches the case, then it executes all code in the block, and stops once it encounters the end of the entire switch, or a 'break', whatever comes first. Thus:
    Code:
    switch( x )
    {
        case 1:
            do_something( );
        case 2:
            do_something_else( );
        case 3:
            do_more( );
        break;
        case 4:
            do_more_more( );
        break;
    }
    If x is 1, then case 1 executes, then since no break is encountered, case 2 executes, then since no break is encountered, case 3 executes, then since a break is encountered, it stops.

    You want to be sure you're using a 'break' before the next 'case' block, otherwise you'll get this sort of "drop through" effect.
    }

    //here is where i'd like to ask if its another customer (start over again without clearing array) or to just end the entire program. Ideas?
    Wrap the process in a loop that exits if the valid is incorrect. Your switch can accomidate this. Consider the following example:

    Code:
    #include <stdio.h>
    
    int main ( void )
    {
        int choice = 0;
    
        while( choice != 'q' && choice != 'Q' )
        {
    
            puts("The menu text goes here."
            choice = getchar( ); //read the input
            getchar( ); //avoid the extra input read from the enter key
    
            switch( choice )
            {
                case '1':
                    do_menu1( );
                break;
                case '2':
                    do_menu2( );
                break;
    
                default:
                    if( choice == 'Q' || choice == 'q' )
                        puts("Good bye.");
                    else
                        puts("Invalid choice.");
            }
        }
    
        return 0;
    }
    This would give you a simple menu. You should be able to get something working from here.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Bit late but I think you should use int arrays for this.
    I find int's easier to work with than characters and this is mainly a TRUE / FALSE question 'Is the seat full?', 'Is there room in non-smoking?'.

    Watch your use of certain 'words'.
    For example you use 'table' too much.
    ie the function table() has a loop counter called table, an array called table.

    Try TableAlloc() iTableIndex, and TableArray or that sort of thing. In the long run the extra typing will pay off. (after you have spent a few hours trying to find the place you used the wrong variable)

    You have to give the arrays size.

    So if you want 10 tables of 4 you will need to declare

    char TableArray[10][4];

    I would use a hash define and int array as easier to read in the long run.

    Code:
    #define    TABLE_SEATS      4
    #define    NUM_TABLES       10
    
    int    iTableArray[NUM_TABLES][TABLE_SEATS];
    
    for(i=0;i<NUM_TABLES;i++)
    {
        for(j=0;j<TABLE_SEATS;j++)
        {
            iTableArray[i][j]=0;
            //cTableArray[i][j]='O';
        }
    }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM