C/C++ Print Theater Seats in C++: numRows & numCols

  • Thread starter Thread starter aberlan
  • Start date Start date
  • Tags Tags
    C++ Loops
AI Thread Summary
The discussion focuses on generating a list of theater seats based on given row and column numbers, formatted as "1A", "1B", etc. Participants suggest improvements to the initial code, emphasizing the importance of using separate variables for row and column counters rather than modifying the original parameters. A common solution involves using a character variable for columns, allowing for easy conversion from numeric indices to letters by adding the column index to 'A'. The proposed code snippets demonstrate effective looping structures to achieve the desired output while ensuring proper formatting with spaces after each seat. Overall, the conversation highlights best practices in coding for clarity and functionality in seat generation.
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++;
}
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top