Two Dimensional Array - C# Programming Exercise

In this C# exercise, you are asked to write a program that asks the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and then displays the average for each group.

This exercise is an excellent opportunity to practice using two-dimensional arrays in C#, as it allows you to store and process data in an organized manner. The program should ask for the marks of the 20 pupils, organize them into two groups of 10 students, and then calculate the average of the marks for each group. The average calculation should display the arithmetic mean of the marks for each group of pupils.

This exercise will help improve your understanding of how to work with arrays in C#, as well as with average calculations and data handling in more complex structures.

 Category

Arrays, Structures and Strings

 Exercise

Two Dimensional Array

 Objective

Write a C# program to ask the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and display the average for each group.

 Write Your C# Exercise

using System;  // Import the System namespace for basic functionality

class Program  // Define the main class
{
    static void Main()  // The entry point of the program
    {
        // Create a two-dimensional array to store marks for 2 groups of 10 pupils
        double[,] marks = new double[2, 10];  
        double group1Sum = 0, group2Sum = 0;  // Variables to store the sum of marks for each group
        double group1Average, group2Average;  // Variables to store the average marks for each group
        
        // Ask the user to enter marks for the pupils
        Console.WriteLine("Enter marks for 20 pupils (2 groups of 10):");
        
        // Input marks for Group 1 (10 pupils)
        for (int i = 0; i < 10; i++)
        {
            Console.Write($"Enter mark for pupil {i + 1} in Group 1: ");
            while (!double.TryParse(Console.ReadLine(), out marks[0, i]) || marks[0, i] < 0)
            {
                Console.WriteLine("Invalid input. Please enter a valid mark (non-negative number).");
                Console.Write($"Enter mark for pupil {i + 1} in Group 1: ");
            }
            group1Sum += marks[0, i];  // Add the mark to the sum for Group 1
        }

        // Input marks for Group 2 (10 pupils)
        for (int i = 0; i < 10; i++)
        {
            Console.Write($"Enter mark for pupil {i + 1} in Group 2: ");
            while (!double.TryParse(Console.ReadLine(), out marks[1, i]) || marks[1, i] < 0)
            {
                Console.WriteLine("Invalid input. Please enter a valid mark (non-negative number).");
                Console.Write($"Enter mark for pupil {i + 1} in Group 2: ");
            }
            group2Sum += marks[1, i];  // Add the mark to the sum for Group 2
        }

        // Calculate the average marks for each group
        group1Average = group1Sum / 10;
        group2Average = group2Sum / 10;

        // Display the averages for each group
        Console.WriteLine($"\nAverage mark for Group 1: {group1Average:F2}");
        Console.WriteLine($"Average mark for Group 2: {group2Average:F2}");
    }
}

 Share this C# exercise

 More C# Programming Exercises of Arrays, Structures and Strings

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Statistics V2

    In this C# exercise, you are asked to create a statistical program that allows the user to perform the following actions: - Add new data - See all data ...

  •  Struct

    In this C# exercise, you are asked to create a struct to store data of 2D points. The fields for each point will be: - x coordinate (short)

  •  Array of struct

    In this C# exercise, you are asked to expand the previous exercise that used a struct to store 2D points. Now, you need to store up to 1,000 points usin...

  •  Array of struct and menu

    In this C# exercise, you are asked to expand the previous exercise (array of points) so that the program displays an interactive menu. The menu should a...

  •  Books database

    In this C# exercise, you are asked to create a small database that will be used to store information about books. For each book, the following data should be kept:

  •  Triangle V2

    In this C# exercise, you are asked to create a program that asks the user for their name and then displays a triangle made from that name, starting with 1 letter and ...