SUNDAS RIAZ

SUNDAS RIAZ

  • NA
  • 11
  • 28.6k

two-dimensional array

Jun 14 2012 9:18 AM
I can't understand that how can we take two values in array and get average....


a program that uses a two-dimensional array to store the highest and lowest temperatures read from the user for each
month of the year. The program should output the following:
1. Average high temperature for the year
2. Average low temperature for the year
3. Highest high temperature for the year
4. Lowest low temperature for the year

please help me.

Answers (1)

0
Vulpes

Vulpes

  • 0
  • 96k
  • 2.6m
Jun 14 2012 10:12 AM
Try this:

using System;

class Program
{
   static void Main()
   {
      string[] months =
      {
         "January",
         "February",
         "March",
         "April",
         "May",
         "June",
         "July",
         "August",
         "September",
         "October",
         "November",
         "December"
      };   
 
      double[,] temperatures = new double[12, 2];
      for(int i = 0; i < 12; i++)
      {
         Console.WriteLine("Input temperatures for {0}", months[i]);
         Console.Write("  Highest :  ");
         temperatures[i, 0] = double.Parse(Console.ReadLine());  
         Console.Write("  Lowest  :  ");
         temperatures[i, 1] = double.Parse(Console.ReadLine());
         Console.WriteLine();
      }  

      // work out average high temperature
      double total = 0;
      for(int i = 0; i < 12; i++) total += temperatures[i, 0];
      Console.WriteLine ("Average high temperature = {0:F2}", total/12);

      // work out average low temperature
      total = 0;
      for(int i = 0; i < 12; i++) total += temperatures[i, 1];
      Console.WriteLine ("Average low temperature = {0:F2}", total/12);

      // work out highest high temperature
      double highest = temperatures[0,0];
      for(int i = 1; i < 12; i++) 
        if (highest < temperatures[i, 0]) highest = temperatures[i, 0];
      Console.WriteLine ("Highest high temperature = {0}", highest);

      // work out lowest low temperature
      double lowest = temperatures[0,1];
      for(int i = 1; i < 12; i++) 
        if (lowest > temperatures[i, 1]) lowest = temperatures[i, 1];
      Console.WriteLine ("Lowest low temperature = {0}", lowest);
 
      Console.ReadKey();
   }
}