Machine Problem 5.6. Write a program using two-‐dimensional arrays that calculates the sum and average of the twelve input values which theuser would type from the keyboard and prints the calculated sum and average.
using System;
using System.Collections.Generic;
using System.Globalization;
namespace App
{
class Program
{
static void Main(string[] args)
{
int[,] twoDimensionalArray = new int[4, 3];
int sum=0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write("Enter value [{0}][{1}]: ", i,j);
twoDimensionalArray[i, j] = int.Parse(Console.ReadLine());
}
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
sum += twoDimensionalArray[i, j];
}
}
double average = (double)sum / 12.0;
Console.WriteLine("The sum is: {0} ", sum);
Console.WriteLine("The average is: {0} ", average);
Console.ReadLine();
}
}
}
Comments
Leave a comment