You are creating a ticketing program for a small movie theater. The seats are represented using a 2-dimensional array. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

You are creating a ticketing program for a small movie theater. The seats are represented using a 2-dimensional array.

import java.util.Scanner; public class Program { public static void main(String[] args) { int[][] seats = { {0, 0, 0, 1, 1, 1, 0, 0, 1, 1}, {1, 1, 0, 1, 0, 1, 1, 0, 0, 0}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {0, 0, 0, 1, 1, 1, 1, 0, 0, 0}, {0, 1, 1, 1, 0, 0, 0, 1, 1, 1} }; Scanner sc = new Scanner(System.in); int row = sc.nextInt(); int col = sc.nextInt(); if (row < 1 || row > seats.length || col < 1 || col > seats[0].length) { System.out.println("Invalid seat position"); } else { if (seats[row - 1][col - 1] == 0) { System.out.println("Sold"); } else { System.out.println("Free"); } } sc.close(); This is my code it passing the 3 test cases but giving error for two

8th Feb 2024, 4:30 AM
Divyanshu Singh
Divyanshu Singh - avatar
2 Answers
+ 1
Index start from 0......
8th Feb 2024, 9:44 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
import java.util.Scanner; import java.util.Scanner; public class Program { public static void main(String[] args) { int[][] seats = { {0, 0, 0, 1, 1, 1, 0, 0, 1, 1}, {1, 1, 0, 1, 0, 1, 1, 0, 0, 0}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {0, 0, 0, 1, 1, 1, 1, 0, 0, 0}, {0, 1, 1, 1, 0, 0, 0, 1, 1, 1} }; Scanner sc = new Scanner(System.in); int row = sc.nextInt(); int col = sc.nextInt(); if(seats[row][col] == 1) { System.out.println("Sold"); } else { System.out.println("Free"); } } }
11th Feb 2024, 4:35 PM
daud123 shafi