CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2011
    Location
    Missouri
    Posts
    1

    2D Bool Array Element Checking

    I have an assignment for class to make program that will allow the user to input a number and a letter to assign them to a seat on a plane. There are 15 rows and 5 columns (A,B,C,D,E). The first five rows are for first class the rest are economy. After all seats for First class are taken we are supposed to ask the user if they would like to sit in economy and vice versa. I keep writing and writing code to try and check the array from row [0] to row [5] to see if all the first class seats are taken, but I have not found a way to do it. If I could find the solution to that then I could find the solution to check and see if there are any economy seats open.

    I originally did this:

    for (int i = 0; i <= 4; i++)
    for (int j = 0; j <= SeatList.GetLength(1); j++)
    if (SeatList[i, j] == false)
    break;
    else
    {
    Console.WriteLine("First Class seats are unavailable");
    Console.WriteLine("Would you like a seat in Economy?");
    Console.ReadLine();
    }

    It kind of works but it will write the else however many times there is a true element

    If you need to see the whole program:


    static void Main(string[] args)
    {
    // int row;
    // int column;

    // Variables
    byte columnAlphabet;
    char columnAlphabet2;
    int intSeatRow;
    int intNumberSeatColumn;
    int columnForByte;
    string strLetterSeatColumn;
    string strQuit;
    string strQuit2;

    // Constants for array
    const int intFive = 5;
    const int intFifteen = 15;

    // Initializing Variables
    intNumberSeatColumn = 0;
    strQuit = "Quit";
    strQuit2 = null;

    // Seat Array
    bool[ , ] SeatList = new bool[intFifteen,intFive];

    // DO WHILE, Infinte loop because if statement checks when to exit
    do
    {
    // Initializing Variables
    intSeatRow = 0;
    strLetterSeatColumn = null;

    Console.Clear();

    // To display the seating chart
    for (int row = 0; row < SeatList.GetLength(0); row++)
    { // For converting column to ASCII for a Letter
    columnForByte = 63;
    for (int column = 0; column < SeatList.GetLength(1); column++)
    {
    // Converting column number to ASCII for a letter
    columnForByte = columnForByte + 1;
    columnAlphabet = Convert.ToByte(columnForByte);
    columnAlphabet2 = Convert.ToChar(columnAlphabet + 1);
    Console.Write("Seat {0,2}{1,1} {2,5}", (row + 1), (columnAlphabet2), SeatList[row, column]);
    Console.WriteLine();
    }
    }
    // Getting input from user for which row they would like or 999 to exit
    Console.WriteLine();
    Console.WriteLine("Rows 1-5 are First Class, Rows 6-15 are Coach");
    Console.WriteLine("Which Row would the passenger like? (Or Type 999 to exit)");
    intSeatRow = Convert.ToInt32(Console.ReadLine());

    // Checking to see if user wants to exit
    if (intSeatRow == 999)
    {
    break;
    }

    // Validating that good data is input
    if (intSeatRow > 15 || intSeatRow < 0)
    {
    Console.WriteLine();
    Console.WriteLine("INVALID, Please retype");
    Console.WriteLine("Press Enter when done");
    Console.ReadLine();
    continue;
    }

    // Only running if Row number was valid
    if (intSeatRow <= 15 || intSeatRow > 0)
    {
    Console.WriteLine("Which Letter seat would the passenger like?");
    strLetterSeatColumn = Console.ReadLine();
    strLetterSeatColumn = strLetterSeatColumn.ToUpper();
    }

    // Changing letter to a number for column index
    switch (strLetterSeatColumn)
    {
    case "A":
    intNumberSeatColumn = 0;
    break;
    case "B":
    intNumberSeatColumn = 1;
    break;
    case "C":
    intNumberSeatColumn = 2;
    break;
    case "D":
    intNumberSeatColumn = 3;
    break;
    case "E":
    intNumberSeatColumn = 4;
    break;
    default:
    Console.WriteLine("INVAILD, Please Retype");
    continue;
    }

    // Checking if desired seat is taken
    if (SeatList[intSeatRow - 1, intNumberSeatColumn] == true)
    {
    Console.WriteLine("We're sorry that seat is taken");
    Console.WriteLine("Press Enter when done");
    Console.ReadLine();

    for (int i = 0; i <= 4; i++)
    for (int j = 0; j <= SeatList.GetLength(1); j++)
    if (SeatList[i, j] == false)
    break;
    else
    {
    Console.WriteLine("First Class seats are unavailable");
    Console.WriteLine("Would you like a seat in Economy?");
    Console.ReadLine();
    }
    }

    // Reserving seat if seat not already taken
    if (SeatList[intSeatRow - 1, intNumberSeatColumn] == false)
    SeatList[intSeatRow - 1, intNumberSeatColumn] = true;

    Console.Clear();
    // Displaying Assigned seat
    Console.WriteLine("You are assigned to seat {0}{1}", intSeatRow, strLetterSeatColumn);
    Console.WriteLine("Press Enter when done");
    Console.ReadLine();
    } while (strQuit != strQuit2); // Infinite Loop because the If statement checks for Exit

    Console.WriteLine("\nPress Enter When Done");
    Console.ReadLine();
    }
    }
    }



    Any help is appreciated.

  2. #2
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Re: 2D Bool Array Element Checking

    Your break statement is breaking out of the inner for loop and not the outer for loop. This means you are still checking seats in first class even though you have found that there is a free seat. As you loop through you may find a seat that is taken and so conclude that there are no seats free in first class.

    Code:
    for (int i = 0; i <= 4; i++)
    for (int j = 0; j <= SeatList.GetLength(1); j++)
    if (SeatList[i, j] == false)
    break;
    else
    {
    Console.WriteLine("First Class seats are unavailable");
    Console.WriteLine("Would you like a seat in Economy?");
    Console.ReadLine(); 
    }
    Perhaps do something like this:
    Code:
    bool firstClassFullyOccupied = true;
    for (int i = 0; i <= 4; i++)
    {
        for (int j = 0; j <= SeatList.GetLength(1)-1; j++)
        {
            //first class is occupied if this seat
            //if filled & all previous checking has concluded
            //that first class is occupied
            firstClassFullyOccupied &= SeatList[i, j];
        }
    }
    
    if (firstClassFullyOccupied)
    {
        Console.WriteLine("First Class seats are unavailable");
        Console.WriteLine("Would you like a seat in Economy?");
        Console.ReadLine();
    }

  3. #3
    Join Date
    Apr 2011
    Location
    Missouri
    Posts
    1

    Re: 2D Bool Array Element Checking

    Thanks for the quick reply. I have never used the &= operator before how exactly does it work? Is it pretty much just to check if the bool is true or false? Unfortunately i'm still pretty new to code. Thank you for the help.

  4. #4
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Re: 2D Bool Array Element Checking

    firstClassFullyOccupied &= SeatList[i, j];

    is the same as writing

    firstClassFullyOccupied = firstClassFullyOccupied & SeatList[i, j];

    Please see
    http://msdn.microsoft.com/en-US/libr...(v=VS.80).aspx
    for more details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured