2D Array Lab Tasks

.pdf
School
National University of Modern Language, Islamabad**We aren't endorsed by this school
Course
COMPUTER 111
Subject
Computer Science
Date
Dec 20, 2024
Pages
10
Uploaded by ConstableOtter17407
Lab Task 1: Cinema Seat Booking SystemScenario: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 1represents a booked seat and 0an available seat.Steps:1.Understand the Array Representation:oRows represent seat rows in the cinema.oColumns represent individual seats in each row.oExample: 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:oUse 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.oExample: Set seats[0][4] = 1;to indicate seat 4 in row 0 is booked.3.Display Seat Arrangement:oWrite a function that uses a loop to display the seating arrangement in a human-readable format.oStep: Loop through the array, and for each row, print all seats (0for available, 1for booked).4.Book a Seat:oTake input from the user for row and seat number they wish to book.oCheck if the seat is available by accessing seats[row][seat].oIf 0, mark it as 1(booked). If already 1, display a message that the seat is booked.5.Cancel a Booking:oSimilar to booking, take input from the user to cancel a booking.oCheck if seats[row][seat] == 1. If yes, mark it as 0(available).6.Loop Until User Exits:oCreate 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 TrackerScenario:
Background image
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. 1means present, 0means absent.Steps:1.Declare the 2D Array:oRepresent 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:oInitially, set all entries to 0(absent). Allow the teacher to enter attendance daily, updating the array.3.Input Attendance:oPrompt the teacher for daily attendance. Use nested loops to update the attendance of all students for each day.oExample: 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:oWrite a function to print the attendance matrix where rows represent students and columns represent days.oExample: Print each row followed by the attendance for each day.5.Calculate and Display Total Present Days for a Student:oPrompt 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:oAllow 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 GameScenario:Build a simple Tic-Tac-Toe game where two players take turns marking Xor Oon a 3x3 grid. The game ends when one player wins or all cells are filled.Steps:1.Array Declaration:oDeclare a 3x3 chararray to store the board state.
Background image
oInitialize the array with spaces (' '), representing empty cells.2.Display the Board:oWrite a function to display the current state of the board.oLoop through the 2D array and print the value of each cell (X, O, or space).3.Input Player Move:oAlternately prompt players for row and column input to place their move (Xor O).oValidate the input to ensure the selected cell is empty.4.Check for Win:oWrite a function to check if there is a winning condition: three matching Xs or Os in a row, column, or diagonal.oUse loops to check each row, each column, and the two diagonals.5.Detect Draw:oAfter each move, check if the board is completely filled and no player has won.6.Loop Until Game Ends:oCreate 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 GrayscaleScenario: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:oRepresent the image as a 10x10 2D array where each element is an integer between 0 and 255.oExample: int image[10][10];2.Initialize the Image:oInitialize the array with random values between 0 and 255 to simulate pixel intensities.3.Display the Image:oWrite a function to display the array, printing each pixel value in rows and columns, just like an image grid.4.Adjust Brightness:
Background image
oCreate a function that increases or decreases brightness by adding or subtracting a value from each pixel.oEnsure the pixel values stay within the range 0-255 (use if statements or the min/maxfunctions).5.Update and Display the New Image:oAfter adjusting brightness, display the updated image.6.Loop for Multiple Adjustments:oAllow 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 RecordScenario: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:oDeclare an array int grades[5][4];for 5 students and 4 subjects.2.Input Grades:oUse a nested loop to input grades for each student and subject.oExample: Loop through each student and ask for grades in 4 subjects.3.Display Grades:oWrite 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:oWrite a function to calculate and display the average grade for each student.oSum the values in each row and divide by the number of subjects.5.Identify the Highest Scorer:oWrite a function to find and display the student with the highest average.6.Provide Feedback:oFor 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).
Background image
Practice calculating sums and averages from 2D array data.Explore real-world educational applications of arrays.Lab Task 6: Weather Data LoggerScenario:Log temperatures for 7 days across 5 cities.Steps:1.Declare the Array:otemperature[5][7]for 5 cities over 7 days.2.Input Data:oFill in the array with daily temperatures for each city.3.Display Data:oPrint out a daily log for each city.4.Calculate Average Temp for Each City:oWrite a function to compute the average temperature for each city.Lab Task 7: Library Book Availability TrackerScenario:Track book availability in 3 library branches for 10 books.Steps:1.Declare Array:obooks[10][3]where rows represent books and columns represent branches.2.Input Availability:oUse 1for available, 0for unavailable.3.Display Book Status:oPrint which branches have the books.Lab Task 8: Sales Tracking System
Background image
Scenario:Track daily sales for 5 products over 7 days.Steps:1.Declare Array:osales[5][7]where products are rows and days are columns.2.Input Daily Sales:oInput sales data for each product.3.Total Sales Calculation:oWrite a function to calculate total sales for each product.Lab Task 9: Hospital Patient MonitoringScenario:Record vital signs for 6 patients every hour for 12 hours.Steps:1.Declare Array:ovitals[6][12]for 6 patients and 12 readings.2.Input Data:oInput vitals (heart rate, temperature, etc.).3.Average Calculation:oCalculate the average vital signs for each patient.Lab Task 10: School Exam ScoresScenario:Track scores for 5 students across 6 subjects.Steps:1.Declare Array:oscores[5][6]where rows are students and columns are subjects.2.Input Scores:oInput exam scores for each subject.3.Display Scores:oPrint the score table.
Background image
Lab Task 11: Chess Board RepresentationScenario:Simulate a chess board using a 2D array.Steps:1.Declare Array:oboard[8][8]where each position is either Wfor white piece, Bfor black, or Efor empty.2.Initialize Board:oSet initial chess positions.Lab Task 12: Daily Attendance Tracking SystemScenario:Track attendance for 15 employees for 30 days.Steps:1.Declare Array:oattendance[15][30].2.Input Data:oRecord 1for present, 0for absent each day.Lab Task 13: Hospital Bed AvailabilityScenario:Track bed availability in 5 wards for 7 days.Steps:1.Declare Array:obeds[5][7]where rows represent wards and columns represent days.2.Input Data:oRecord bed availability (1for available, 0for unavailable).
Background image
Lab Task 14: Monthly Rainfall DataScenario:Track rainfall for 4 cities over 30 days.Steps:1.Declare Array:orainfall[4][30].2.Input Data:oRecord daily rainfall amounts.Lab Task 15: School Grades Record SystemScenario:Track marks for 10 students across 5 subjects.Steps:1.Declare Array:omarks[10][5].2.Input Marks:oRecord marks for each subject.Lab Task 16: Flight Seat Booking SystemScenario:Track seat bookings in a flight for 6 rows and 4 columns.Steps:1.Declare Array:oseats[6][4].2.Input Booking Data:oRecord 1for booked, 0for available.Lab Task 17: Tracking Exam Results
Background image
Scenario:Track results for students in 3 subjects.Steps:1.Declare Array:oresults[20][3]where rows represent students and columns represent subjects.Lab Task 18: Stock Inventory ManagementScenario:Track stock levels for 10 products across 3 warehouses.Steps:1.Declare Array:ostock[10][3].Lab Task 19: Game Scoreboard SystemScenario:Track player scores in 5 rounds for 6 players.Steps:1.Declare Array:oscores[6][5].Lab Task 20: Student Attendance in ClassroomsScenario:Track attendance of 25 students over 10 days.Steps:1.Declare Array:
Background image
oattendance[25][10].
Background image