Skip to main content

Command Palette

Search for a command to run...

Lab Sheet 4: Exploring Array Basics Exercises

Published
4 min read
Lab Sheet 4: Exploring Array Basics Exercises
A
I co-founded🫆digizen (fighting internet chaos 🧠), building an AI app to tackle misinformation, and running ideaGeek to turn “I have an idea” into real startups. I also share tech + travel on YouTube (TechNomad & Rz Omar). I touch grass too 🌱… but mostly to debug life 💻

Part 1: Array Declaration and Initialization

Exercise 1.1: Creating Your First Array

  1. Create a new C# Windows Forms Application project named "ArrayPracticals"

  2. Add the following controls to Form1:

    • 1 Button (btnDisplay)

    • 1 ListBox (lstOutput)

    • 1 Label (lblTitle) - Set text as "Array Display"

  3. Write code in the button click event to:

    • Declare a string array named fruits with values: "Mango", "Banana", "Apple", "Orange", "Papaya"

    • Display each element in the listbox using a for loop

Expected Output: All fruit names should appear in the listbox when button is clicked.

Exercise 1.2: Integer Array Operations

  1. Add another button (btnNumbers) with text "Display Numbers"

  2. Declare an integer array: int[] numbers = {45, 12, 78, 23, 56, 89, 34}

  3. 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

  1. Add a new button (btnMarks) with text "Show Marks"

  2. Clear the listbox before adding new items

  3. Create an integer array for student marks: {67, 45, 89, 92, 73, 55, 81}

  4. Use a foreach loop to display all marks in the listbox

  5. Calculate 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

  1. 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"

  2. Declare a string array: {"Colombo", "Kandy", "Galle", "Jaffna", "Anuradhapura", "Batticaloa", "Trincomalee"}

  3. When btnOriginal is clicked:

    • Clear the listbox

    • Display cities in original order

  4. 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()

  1. 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"

  2. Create an array: string[] colors = {"Red", "Green", "Blue", "Yellow", "Purple", "Orange"}

  3. 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

  1. 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"

  2. Declare two arrays:

     string[] cars = {"Toyota", "Honda", "Nissan", "Suzuki"};
     string[] bikes = {"Yamaha", "Bajaj", "TVS", "Hero"};
    
  3. Implement:

    • btnCars: Use AddRange() to add all cars to listbox

    • btnBikes: 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:

  1. Use a List or create a dynamic solution to store multiple mark entries

  2. Validate that marks are between 0-100

  3. Display all marks in the listbox after each operation

  4. Use Array.Sort() for sorting functionality

  5. Show 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!

More from this blog

T

TechNomad

46 posts

TechNomad is mostly coding exercises, not long boring explanations 😌. I share what I teach and learn through hands-on problems—mainly for my students, but anyone can jump in. If you learn by doing (and breaking things), you’ll fit right in. Less theory, more “try this and see what happens.”