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++;
}
}
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top