Skip to main content

Sample Solutions

Visual Basic.NET

Module Module1
 
  
 
  Sub Main()
 
    Dim line As String, n As Integer, m As Integer, i As Integer, _
        BIG As Integer, small As Integer, cnt As Integer = 0
 
    line = Console.ReadLine()
 
    While Not line Is Nothing
      n = CInt(line)
      m = CInt(Console.ReadLine())
      cnt = cnt + 1
      small = m
      BIG = m
 
      For i = 1 To n - 1
        m = CInt(Console.ReadLine())
        If m > BIG Then
          BIG = m
        ElseIf m < small Then
          small = m
        End If
      Next
 
      Console.WriteLine("Data set " & cnt & " has range = " & _
        Math.Abs(BIG - small))
 
      Console.WriteLine()
 
      line = Console.ReadLine()
 
    End While
 
  End Sub
 
End Module

C#

using System;
 
class Class1
{
 
  static void Main()
  {
    string line;
    int n, m, big, small, cnt = 0;
 
    line = Console.ReadLine();
 
    while (line != null)
    {
 
      n = Convert.ToInt32(line);
      m = Convert.ToInt32(Console.ReadLine());
 
      cnt = cnt + 1;
      small = m;
      big = m;
 
      for(int i = 1; i <= n - 1; i++)
      {
 
        m = Convert.ToInt32(Console.ReadLine());
        if (m > big)
        {
          big = m;
        }
        else if (m < small)
        {
          small = m;
        }
 
      }
 
      Console.WriteLine("Data set " + cnt + " has range = " +
        Math.Abs(big - small));
 
      Console.WriteLine();
 
      line = Console.ReadLine();
 
    }
  }
}

C++

#include <iostream.h>
#include <stdlib.h>
#include <math.h>
 
using namespace std;
 
main() {
 
  int n, m, i, BIG, small, cnt=0;
 
  cin >> n;
  while (cin) {
    cin >> m;
    ++cnt;
    BIG = small = m;
    for (i=0; i<n-1; ++i) {
      cin >> m;
      if (m > BIG) {
        BIG = m;
      } else if (m < small) {
        small = m;
      }
    }
 
    cout << "Data set " << cnt << " has range = " << abs(BIG - small) << endl << endl;
 
    cin >> n;
 
  }
}