Seats (Less)

Problem to Solve

A certain nearby theater has N rows, with M seats in each row. Mavid Dalan, the theater manager, wants to print a seating chart showing the seat numbers for each row.

In a file called seats.c, in a folder called seats-less, write a C program to display the seating chart. The seats should be numbered consecutively starting from 1.

For example, if the theater has 10 rows with 8 seats per row, your program might behave as follows:

Enter number of rows: 10
Enter number of seats per row: 8
 
Row  1:   1   2   3   4   5   6   7   8
Row  2:   9  10  11  12  13  14  15  16
Row  3:  17  18  19  20  21  22  23  24
Row  4:  25  26  27  28  29  30  31  32
Row  5:  33  34  35  36  37  38  39  40
Row  6:  41  42  43  44  45  46  47  48
Row  7:  49  50  51  52  53  54  55  56
Row  8:  57  58  59  60  61  62  63  64
Row  9:  65  66  67  68  69  70  71  72
Row 10:  73  74  75  76  77  78  79  80

Requirements

  • The program should prompt the user for the number of rows and number of seats per row.
  • If the user enters a number less than 1 for either input, you should re-prompt the user until they give you a valid input.
  • Print Row X, where X is the number row, then the numbered seats, starting from 1.
Hints
  • Recall that you can get an int from a user with get_int, which is declared in cs50.h.
  • Recall that you can print formatted output, including numbers padded with spaces, using format specifiers like %4i in printf.

Advice

Write some code that you know will compile

Even though this program won’t do anything yet, it should at least compile with make!

#include <cs50.h>
#include <stdio.h>
 
int main(void)
{
 
}
Write some pseudocode before writing more code

Break the problem down into smaller pieces:

  1. Prompt the user for the number of rows, re-prompting if the input is less than 1
  2. Prompt the user for the number of seats per row, re-prompting if the input is less than 1
  3. Print a seating chart with that many rows and seats
#include <cs50.h>
#include <stdio.h>
 
int main(void)
{
    // Prompt the user for the number of rows
 
    // Prompt the user for the number of seats per row
 
    // Print the seating chart
}
Convert the pseudocode to code

get_int from cs50.h will prompt the user and hand you back an int. Think about what kind of loop lets you keep asking the same question again and again until the user gives you a value that’s at least 1.

Once you have valid values for rows and seats, think about how to number seats consecutively across rows rather than computing each seat number from its row and column. What if you kept a separate counter that starts at 1 and increases by 1 every time you print a seat, regardless of which row you’re in?

How to Test

Does your code work as prescribed when you input:

  • 1 row and 1 seat?
  • A small number of rows and seats, like 3 and 5?
  • A large number of rows and seats, like 20 and 20?
  • Rows and seats that produce seat numbers with differing numbers of digits (e.g., single digits versus double or triple digits), to confirm your columns still line up?

Correctness

check50 cs50/problems/2026/fall/seats/less

Style

style50 seats.c

How to Submit

  1. Download your seats.c file by control-clicking or right-clicking on the file in your codespace’s file browser and choosing Download.
  2. Go to CS50’s Gradescope page.
  3. Click Problem Set 1: Seats (Less).
  4. Drag and drop your seats.c file to the area that says Drag & Drop. Be sure it has that exact filename! If you upload a file with a different name, the autograder likely will fail when trying to run it. Ensuring you have uploaded files with the correct filename is your responsibility!
  5. Click Upload. You should see a message that says “Problem Set 1: Seats (Less) submitted successfully!”