Print Theater Seats in C++: numRows & numCols

  • Context: C/C++ 
  • Thread starter Thread starter aberlan
  • Start date Start date
  • Tags Tags
    C++ Loops
Click For Summary
SUMMARY

This discussion focuses on printing theater seat labels in C++ using specified row and column counts. The final solution involves using nested loops where the outer loop iterates through the rows and the inner loop iterates through the columns, utilizing character arithmetic to convert column indices into letters. The recommended code structure includes declaring separate variables for row and column counters and using type casting to ensure correct character output. The final code snippet provided effectively prints seat labels in the format "1A 1B 1C 2A 2B 2C".

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with loops and conditional statements in C++
  • Knowledge of character data types and type casting in C++
  • Basic experience with the C++ Standard Library, specifically cout and printf
NEXT STEPS
  • Study C++ character arithmetic and type casting techniques
  • Learn about nested loops and their applications in C++
  • Explore the differences between cout and printf for output formatting
  • Investigate best practices for variable declaration and scope management in C++
USEFUL FOR

C++ developers, computer science students, and anyone interested in learning efficient coding practices for generating formatted output in programming.

aberlan
Messages
1
Reaction score
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
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;
  }
 
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++;
}
}
 

Similar threads

  • · Replies 33 ·
2
Replies
33
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
6K
Replies
7
Views
2K
  • · Replies 28 ·
Replies
28
Views
30K
Replies
1
Views
647
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K