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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
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...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...
Back
Top