Print Theater Seats in C++: numRows & numCols

  • C/C++
  • Thread starter aberlan
  • Start date
  • Tags
    C++ Loops
In summary, the code prints the list of seats in a theater in the following order: 1A, 1B, 1C, 2A, 2B, 2C.
  • #1
aberlan
1
0
Given numRows and numCols, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat, including after the last. Ex: numRows = 2 and numCols = 3 prints:
1A 1B 1C 2A 2B 2C

This is what I have so far, but I can't figure out how to turn the second number into a letter.

for (numRows = 1; numRows <= 2; numRows = numRows +1) {
col = 'A';
for (numCols = 0; numCols <= 2; numCols = numCols +1)
{
printf("%d%d ", numRows, numCols);
}
}
 
Technology news on Phys.org
  • #2
Hi, and welcome to the forum!

It's best to use the [CODE]...[/CODE] tags (# button on the toolbar) to type program text and either [TEXTDRAW]...[/TEXTDRAW] or [CODE]...[/CODE] for preformatted text, for example,
[TEXTDRAW]
1A 1B 1C
2A 2B 2C
[/TEXTDRAW]

First, I would declare new variables for row and column counters instead of modifying numRows and numCols. It is possible to reuse numRows and decrement it until it becomes zero, but it may be required later in the code. The column number can be declared as a [m]char[/m]. You can use a [m]char[/m] expression (for example, a constant like 'A') as a one-byte integer; in particular, you can add the column counter to it. However, for technical reasons when arithmetic is performed on [m]char[/m]s, the result becomes an [m]int[/m]. So to print the result as a [m]char[/m], you can type cast the result of an arithmetic operation back to [m]char[/m]. So this is how you can write your code.

Code:
  for (int r = 0; r < numRows; r++) {
    for (char c = 0; c < numCols; c++)
      cout << r << char('A' + c) << " ";
    cout << endl;
  }
 
  • #3
Had something similar to Intro to C at my school. Took a while to figure out but one part of it was making the 1 row but second row went 2A 2A 2A to a second line. So basically anyone whos on C or knows C and on C++ currently i trimmed the fat on the code and the result is this. cheers

int currRow=0, currCol=1;
char currColLet;

while(currRow<numRows)
{
currRow++;
currCol=1;
currColLet='A';
while(currCol<=numCols)
{
printf("%d%c ", currRow, currColLet);
currColLet++;
currCol++;
}
}
 

Related to Print Theater Seats in C++: numRows & numCols

1. What are numRows and numCols in "Print Theater Seats in C++"?

numRows and numCols refer to the number of rows and columns, respectively, in the theater seating arrangement. These variables are used to determine the size of the seating chart and allow for the program to print the correct number of seats.

2. How are numRows and numCols used in the "Print Theater Seats in C++" program?

numRows and numCols are used to create a 2D array that represents the theater seating chart. The size of this array is determined by the values of numRows and numCols, and the program uses this array to print the seating chart with the correct number of rows and columns.

3. Can numRows and numCols be changed in the "Print Theater Seats in C++" program?

Yes, the values of numRows and numCols can be changed in the program. This allows for the seating chart to be adjusted for different sized theaters or seating arrangements.

4. What happens if numRows or numCols is set to a negative value in the "Print Theater Seats in C++" program?

If either numRows or numCols is set to a negative value, the program will not be able to create a valid seating chart. It is important to ensure that both variables are set to positive values to avoid errors.

5. How can I print the theater seating chart with specific labels using the "Print Theater Seats in C++" program?

To print the theater seating chart with specific labels, you can use the numRows and numCols variables to create a 2D array that represents the seating chart. Then, you can use nested for loops to iterate through the array and assign labels to each seat. Finally, you can print the array to display the seating chart with the desired labels.

Similar threads

  • Programming and Computer Science
Replies
33
Views
6K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
28
Views
29K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
2
Views
8K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top