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, whereXis the number row, then the numbered seats, starting from1.
Hints
- Recall that you can get an
intfrom a user withget_int, which is declared incs50.h. - Recall that you can print formatted output, including numbers padded with spaces, using format specifiers like
%4iinprintf.
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:
- Prompt the user for the number of rows, re-prompting if the input is less than
1 - Prompt the user for the number of seats per row, re-prompting if the input is less than
1 - 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:
-
1row and1seat? - A small number of rows and seats, like
3and5? - A large number of rows and seats, like
20and20? - 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
After you submit, wait a few minutes and then be sure to check your autograder results. If you see SUBMISSION ERROR: missing files (0.0/1.0), MissingFilesError, or the like, it means your file was not named exactly as prescribed (or you uploaded it to the wrong problem). Correctness in submissions entails everything from reading the specification, writing code that is compliant with it, and submitting files with the correct name. If you see this error, you should resubmit right away, making sure your submission is fully compliant with the specification. The staff will not adjust your filenames for you after the fact!
- Download your
seats.cfile by control-clicking or right-clicking on the file in your codespace’s file browser and choosing Download. - Go to CS50’s Gradescope page.
- Click Problem Set 1: Seats (Less).
- Drag and drop your
seats.cfile 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! - Click Upload. You should see a message that says “Problem Set 1: Seats (Less) submitted successfully!”
Don’t forget that, starting this week, problems will be graded along the axis of Design also, which is done manually by your TF after the submission deadline. The autograder only awards a portion of the available points, and the rest will be awarded at the discretion of your TF when providing qualitative feedback.