Lab Sheet 4: Exploring Array Basics Exercises

Part 1: Array Declaration and Initialization
Exercise 1.1: Creating Your First Array
Create a new C# Windows Forms Application project named "ArrayPracticals"
Add the following controls to Form1:
1 Button (btnDisplay)
1 ListBox (lstOutput)
1 Label (lblTitle) - Set text as "Array Display"
Write code in the button click event to:
Declare a string array named
fruitswith values: "Mango", "Banana", "Apple", "Orange", "Papaya"Display each element in the listbox using a
forloop
Expected Output: All fruit names should appear in the listbox when button is clicked.
Exercise 1.2: Integer Array Operations
Add another button (btnNumbers) with text "Display Numbers"
Declare an integer array:
int[] numbers = {45, 12, 78, 23, 56, 89, 34}Display the following in the listbox:
Total number of elements
Each number with its index (e.g., "Index 0: 45")
Part 2: Using foreach Loop
Exercise 2.1: Student Marks Display
Add a new button (btnMarks) with text "Show Marks"
Clear the listbox before adding new items
Create an integer array for student marks:
{67, 45, 89, 92, 73, 55, 81}Use a
foreachloop to display all marks in the listboxCalculate and display the average marks at the end
Hint: Sum all marks in the foreach loop and divide by array length
Part 3: Array Sorting
Exercise 3.1: Sorting Cities
Design a new form with:
1 ListBox (lstCities) - for displaying cities
1 Button (btnOriginal) - text: "Original Order"
1 Button (btnSort) - text: "Sort Alphabetically"
1 Label - title: "Sri Lankan Cities"
Declare a string array:
{"Colombo", "Kandy", "Galle", "Jaffna", "Anuradhapura", "Batticaloa", "Trincomalee"}When btnOriginal is clicked:
Clear the listbox
Display cities in original order
When btnSort is clicked:
Clear the listbox
Sort the array using
Array.Sort()Display sorted cities
Part 4: Array Reversal
Exercise 4.1: Working with Array.Reverse()
Add the following controls to your form:
1 Button (btnReverse) - text: "Reverse All"
1 Button (btnReversePartial) - text: "Reverse First 3"
1 Button (btnReset) - text: "Reset"
Create an array:
string[] colors = {"Red", "Green", "Blue", "Yellow", "Purple", "Orange"}Implement the following functionality:
btnReverse: Reverse all elements and display
btnReversePartial: Reverse only the first 3 elements using
Array.Reverse(colors, 0, 3)btnReset: Display original order
Challenge: After reversing, what happens to the original array? Test and observe.
Part 5: Using AddRange() Method
Exercise 5.1: Multiple Arrays to ListBox
Create a new form with:
1 ListBox (lstVehicles)
1 Button (btnCars) - text: "Show Cars"
1 Button (btnBikes) - text: "Show Bikes"
1 Button (btnAll) - text: "Show All"
Declare two arrays:
string[] cars = {"Toyota", "Honda", "Nissan", "Suzuki"}; string[] bikes = {"Yamaha", "Bajaj", "TVS", "Hero"};Implement:
btnCars: Use
AddRange()to add all cars to listboxbtnBikes: Clear listbox and add all bikes
btnAll: Add both arrays to listbox (cars first, then bikes)
Part 6: Practical Application
Exercise 6.1: Student Grade Management System
Create a comprehensive application with the following features:
Form Design:
TextBox (txtMarks) - for entering marks
Button (btnAdd) - to add marks to array
Button (btnSort) - to sort marks in ascending order
Button (btnHighest) - to display highest mark
Button (btnLowest) - to display lowest mark
Button (btnClear) - to clear all data
ListBox (lstMarks) - to display marks
Label (lblCount) - to show total number of marks entered
Requirements:
Use a List or create a dynamic solution to store multiple mark entries
Validate that marks are between 0-100
Display all marks in the listbox after each operation
Use
Array.Sort()for sorting functionalityShow appropriate messages using
MessageBox.Show()
Bonus Challenge: Add a button to calculate and display:
Average marks
Pass percentage (marks >= 50)
Grade distribution (A: 75-100, B: 65-74, C: 50-64, F: <50)
Tips for Success
Always clear the listbox before adding new items to avoid duplication
Use meaningful variable names
Add comments to explain your code logic
Test each functionality thoroughly before moving to the next exercise
Good Luck with Your Lab Work!



