Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

2D Array Lab Tasks

Uploaded by

arsalan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

2D Array Lab Tasks

Uploaded by

arsalan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Lab Task 1: Cinema Seat Booking System

Scenario:

You are tasked with creating a system to manage seat bookings in a cinema hall using a 2D
array. The seats are represented in rows and columns where 1 represents a booked seat and 0 an
available seat.

Steps:

1. Understand the Array Representation:


o Rows represent seat rows in the cinema.
o Columns represent individual seats in each row.
o Example: For a cinema with 5 rows and 10 seats in each row, declare a 2D array
as int seats[5][10];.
2. Initialize the Seats:
o Use a nested loop to initialize the array with default values. Initially, all seats can
be 0 (available), but you can also pre-book some seats for testing.
o Example: Set seats[0][4] = 1; to indicate seat 4 in row 0 is booked.
3. Display Seat Arrangement:
o Write a function that uses a loop to display the seating arrangement in a human-
readable format.
o Step: Loop through the array, and for each row, print all seats (0 for available, 1
for booked).
4. Book a Seat:
o Take input from the user for row and seat number they wish to book.
o Check if the seat is available by accessing seats[row][seat].
o If 0, mark it as 1 (booked). If already 1, display a message that the seat is booked.
5. Cancel a Booking:
o Similar to booking, take input from the user to cancel a booking.
o Check if seats[row][seat] == 1. If yes, mark it as 0 (available).
6. Loop Until User Exits:
o Create a menu-driven system to allow continuous booking, cancellation, or
viewing until the user decides to exit.

Key Learning Objectives:

 Understand array indexing for real-world scenarios.


 Learn how to manipulate arrays using loops.
 Practice taking and validating user input.

Lab Task 2: School Attendance Tracker

Scenario:
A teacher wants to track student attendance for a week. Store attendance using a 2D array where
each row represents a student and each column represents a day. 1 means present, 0 means
absent.

Steps:

1. Declare the 2D Array:


o Represent attendance for 10 students over 5 days. Declare a 2D array as int
attendance[10][5]; where rows represent students and columns represent days.
2. Initialize with Data:
o Initially, set all entries to 0 (absent). Allow the teacher to enter attendance daily,
updating the array.
3. Input Attendance:
o Prompt the teacher for daily attendance. Use nested loops to update the attendance
of all students for each day.
o Example: Use a loop that iterates over each student (for (int i = 0; i < 10;
i++)), then ask the teacher if the student was present (1) or absent (0).
4. Display Attendance for All Students:
o Write a function to print the attendance matrix where rows represent students and
columns represent days.
o Example: Print each row followed by the attendance for each day.
5. Calculate and Display Total Present Days for a Student:
o Prompt the user for a specific student’s ID and calculate the total number of
present days by summing up the values of the respective row.
6. Loop for Daily Updates:
o Allow the system to take attendance day by day and update the array accordingly.

Key Learning Objectives:

 Use 2D arrays to store multiple related data (students' attendance across multiple days).
 Practice array traversal and summation.
 Develop skills in building a user-interactive system for real-time data entry.

Lab Task 3: Tic-Tac-Toe Game

Scenario:

Build a simple Tic-Tac-Toe game where two players take turns marking X or O on a 3x3 grid.
The game ends when one player wins or all cells are filled.

Steps:

1. Array Declaration:
o Declare a 3x3 char array to store the board state.
o Initialize the array with spaces (' '), representing empty cells.
2. Display the Board:
o Write a function to display the current state of the board.
o Loop through the 2D array and print the value of each cell (X, O, or space).
3. Input Player Move:
o Alternately prompt players for row and column input to place their move (X or O).
o Validate the input to ensure the selected cell is empty.
4. Check for Win:
o Write a function to check if there is a winning condition: three matching Xs or Os
in a row, column, or diagonal.
o Use loops to check each row, each column, and the two diagonals.
5. Detect Draw:
o After each move, check if the board is completely filled and no player has won.
6. Loop Until Game Ends:
o Create a loop that alternates between Player 1 and Player 2. Exit the loop if a
player wins or if there is a draw.

Key Learning Objectives:

 Use 2D arrays to simulate a game board.


 Learn about turn-based systems and input validation.
 Develop skills in game logic and condition checking with arrays.

Lab Task 4: Image Representation in Grayscale

Scenario:

An image in grayscale can be represented by a 2D array where each element is a pixel intensity
between 0 and 255. Implement a basic image manipulation system that allows adjusting the
brightness.

Steps:

1. Declare a 2D Array for Image Pixels:


o Represent the image as a 10x10 2D array where each element is an integer
between 0 and 255.
o Example: int image[10][10];
2. Initialize the Image:
o Initialize the array with random values between 0 and 255 to simulate pixel
intensities.
3. Display the Image:
o Write a function to display the array, printing each pixel value in rows and
columns, just like an image grid.
4. Adjust Brightness:
Create a function that increases or decreases brightness by adding or subtracting a
o
value from each pixel.
o Ensure the pixel values stay within the range 0-255 (use if statements or the
min/max functions).
5. Update and Display the New Image:
o After adjusting brightness, display the updated image.
6. Loop for Multiple Adjustments:
o Allow the user to repeatedly adjust brightness until they decide to exit.

Key Learning Objectives:

 Simulate a real-world image processing task using arrays.


 Practice bounds checking (ensuring values remain within a certain range).
 Work with nested loops to manipulate large sets of data.

Lab Task 5: Student Grades Record

Scenario:

A teacher wants to store and analyze student grades. You will use a 2D array where each row
represents a student and each column represents a subject.

Steps:

1. Declare the 2D Array:


o Declare an array int grades[5][4]; for 5 students and 4 subjects.
2. Input Grades:
o Use a nested loop to input grades for each student and subject.
o Example: Loop through each student and ask for grades in 4 subjects.
3. Display Grades:
o Write a function to print the grades in a table format, where each row represents a
student and each column represents a subject.
4. Calculate Average for Each Student:
o Write a function to calculate and display the average grade for each student.
o Sum the values in each row and divide by the number of subjects.
5. Identify the Highest Scorer:
o Write a function to find and display the student with the highest average.
6. Provide Feedback:
o For students with an average below 50, display a message encouraging them to
improve.

Key Learning Objectives:

 Learn how to use 2D arrays to store structured data (grades per student).
 Practice calculating sums and averages from 2D array data.
 Explore real-world educational applications of arrays.

Lab Task 6: Weather Data Logger

Scenario:

Log temperatures for 7 days across 5 cities.

Steps:

1. Declare the Array:


o temperature[5][7] for 5 cities over 7 days.
2. Input Data:
o Fill in the array with daily temperatures for each city.
3. Display Data:
o Print out a daily log for each city.
4. Calculate Average Temp for Each City:
o Write a function to compute the average temperature for each city.

Lab Task 7: Library Book Availability Tracker

Scenario:

Track book availability in 3 library branches for 10 books.

Steps:

1. Declare Array:
o books[10][3] where rows represent books and columns represent branches.
2. Input Availability:
o Use 1 for available, 0 for unavailable.
3. Display Book Status:
o Print which branches have the books.

Lab Task 8: Sales Tracking System


Scenario:

Track daily sales for 5 products over 7 days.

Steps:

1. Declare Array:
o sales[5][7] where products are rows and days are columns.
2. Input Daily Sales:
o Input sales data for each product.
3. Total Sales Calculation:
o Write a function to calculate total sales for each product.

Lab Task 9: Hospital Patient Monitoring

Scenario:

Record vital signs for 6 patients every hour for 12 hours.

Steps:

1. Declare Array:
o vitals[6][12] for 6 patients and 12 readings.
2. Input Data:
o Input vitals (heart rate, temperature, etc.).
3. Average Calculation:
o Calculate the average vital signs for each patient.

Lab Task 10: School Exam Scores

Scenario:

Track scores for 5 students across 6 subjects.

Steps:

1. Declare Array:
o scores[5][6] where rows are students and columns are subjects.
2. Input Scores:
o Input exam scores for each subject.
3. Display Scores:
o Print the score table.
Lab Task 11: Chess Board Representation

Scenario:

Simulate a chess board using a 2D array.

Steps:

1. Declare Array:
o board[8][8] where each position is either W for white piece, B for black, or E for
empty.
2. Initialize Board:
o Set initial chess positions.

Lab Task 12: Daily Attendance Tracking System

Scenario:

Track attendance for 15 employees for 30 days.

Steps:

1. Declare Array:
o attendance[15][30].
2. Input Data:
o Record 1 for present, 0 for absent each day.

Lab Task 13: Hospital Bed Availability

Scenario:

Track bed availability in 5 wards for 7 days.

Steps:

1. Declare Array:
o beds[5][7] where rows represent wards and columns represent days.
2. Input Data:
o Record bed availability (1 for available, 0 for unavailable).
Lab Task 14: Monthly Rainfall Data

Scenario:

Track rainfall for 4 cities over 30 days.

Steps:

1. Declare Array:
o rainfall[4][30].
2. Input Data:
o Record daily rainfall amounts.

Lab Task 15: School Grades Record System

Scenario:

Track marks for 10 students across 5 subjects.

Steps:

1. Declare Array:
o marks[10][5].
2. Input Marks:
o Record marks for each subject.

Lab Task 16: Flight Seat Booking System

Scenario:

Track seat bookings in a flight for 6 rows and 4 columns.

Steps:

1. Declare Array:
o seats[6][4].
2. Input Booking Data:
o Record 1 for booked, 0 for available.

Lab Task 17: Tracking Exam Results


Scenario:

Track results for students in 3 subjects.

Steps:

1. Declare Array:
o results[20][3] where rows represent students and columns represent subjects.

Lab Task 18: Stock Inventory Management

Scenario:

Track stock levels for 10 products across 3 warehouses.

Steps:

1. Declare Array:
o stock[10][3].

Lab Task 19: Game Scoreboard System

Scenario:

Track player scores in 5 rounds for 6 players.

Steps:

1. Declare Array:
o scores[6][5].

Lab Task 20: Student Attendance in Classrooms

Scenario:

Track attendance of 25 students over 10 days.

Steps:

1. Declare Array:
o attendance[25][10].

You might also like