- Course
- Arrays
- Two-dimensional array
Two-dimensional array
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Write a program in C# that asks the user for the grades of 10 students divided into 2 groups of 5 students in each. You must store them in a two-dimensional array and show the arithmetic average of each group of students.
Input
4
5
3
6
3
7
4
8
3
5
6
2
6
7
4
6
7
4
2
3
Output
Average for group 1 is 4,8
Average for group 2 is 4,7
Solution
using System;
public class TwoDimensionalArray
{
public static void Main(string[] args)
{
int totalByGroup = 5;
float[,] puntuations = new float[totalByGroup, totalByGroup];
float totalGroup1 = 0.0f, totalGroup2 = 0.0f;
// Get puntuations
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < totalByGroup; j++)
{
puntuations[i, j] = Convert.ToSingle(Console.ReadLine());
}
}
// Calculate average group 1
for (int i = 0; i < totalByGroup; i++)
{
totalGroup1 += puntuations[0, i];
}
Console.WriteLine("Average for group 1 is {0}", totalGroup1 / totalByGroup);
// Calculate average group 2
for (int i = 0; i < totalByGroup; i++)
{
totalGroup2 += puntuations[1, i];
}
Console.WriteLine("Average for group 2 is {0}", totalGroup2 / totalByGroup);
}
}