Wednesday, July 14, 2010

Two dimensional or 2 d array in BlueJ programs - Marks of Students

Two -dimensional array is actually array of arrays.  To declare a two-dimensional array variable, specify each additional index using another set of square brackets.  For example, to create a two-dimensional integer array   named arr, the syntax is:int arr [][]=new int[x][y];  Unlike C Programming Language , Java arrays are dynamic.

This means that an array of size 'x' will be created, while within each index there will be another array of size 'y'.

Consider the following program:-
Marks obtained in three subjects by three students is

Roll no. 1
Math     78
Physics  65
Chemistry 60

Roll no. 2
Math     87
Physics  56
Chemistry 65


Roll no. 3
Math     77
Physics  78
Chemistry 70

If we were asked to store the marks obtained by Roll no.1, then we can simply create an one dimensional array of size 4. Then the Roll no and three subject marks can be stored in the array variable. But here the case is different. We have to store the values in the tabular form in a variable.  Here comes the utility of two-dimensional array.  We have to declare a 3 by 4 array.

The program using BlueJ array is as follows.


import java.io.*;
class Arr
  {
   int result [][]=new int[3][4];
   int i, j;
   BufferedReader br=new BufferedReader(new InputStreamReader(Systm.in));
public void take() throws IOException
{
   for (i=0;i< 3;i++)
   {
     j=0;
System.out.println ("Enter the Roll number:-");
 result [i][j]=Integer.parseInt(br.readLine());
    for (j=1;j< 4;j++)
    {
     System.out.println ("Enter the marks of subject no “ +  j + “:”);
     result [i][j]=Integer.parseInt(br.readLine());
    }
  }
 
  System.out.println ("The stored values are:");
  for (i=0;i< 3;i++)
  {
   for (j=0;j< 4;j++)
   {
    System.out.println (" "+ result [i][j]);
   }
System.out.println();
 }
}
  }


Here is another program on two dimensional array using BlueJ.

 The daily maximum temperature of 4 cities for 5 dates are recorded during the month of January.  Write a program to find the day and city corresponding to @. highest temperature and @. lowest temperature


import java.io.*;
class Arr
  {
   int temp [][]=new int[5][4];
   int i, j;
   BufferedReader br=new BufferedReader(new InputStreamReader(Systm.in));
 int maxc,minc,maxd,mind,maxt,mint,i,j;
public void take() throws IOException
{

for(i=0;i< 5;i++)
 {
  for(j=0;j< 4;j++)
  {
   if(j==0)
System.out.println ("Enter temp. of Calcutta on “ + (i+1) + “ January:”);
   else if(j==1)
System.out.println ("Enter temp. of Chennai on “ + (i+1) + “ January:”);
   else if(j==2)
   System.out.println ("Enter temp. of Mumbai on “ + (i+1) + “ January:”);
   else if(j==3)
System.out.println ("Enter temp. for Delhi on “ + (i+1) + “ January:”);
    temp [i][j]=Integer.parseInt(br.readLine());
  }
  }
System.out.println ("The recorded temperature:-");
System.out.println ("Calcutta      Madras    Mumbai     Delhi");
    for(i=0;i< 5;i++)
 {
  for(j=0;j< 4;j++)
  {
System.out.println ("  ",temp[i][j]);
  }
System.out.println ();
  }
System.out.println ("**************");
  for(i=0;i< 5;i++)
 {
  for(j=0;j< 4;j++)
  {
   if((i==0)&&(j==0))
   {
    maxt=temp[i][j];
    maxd=i;
    maxc=j;
    mint=temp[i][j];
    mind=i;
    minc=j;
   }
   else
   {
    if(maxt< temp[i][j])
    {
     maxt=temp[i][j];
    maxd=i+1;
    maxc=j+1;
    }
    if(mint >temp[i][j])
    {
     mint=temp[i][j];
    mind=i+1;
    minc=j+1;
   }
  }
  }
  }
System.out.println ("The maximum temperature is :" + maxt);
System.out.println ("And the corresponding date is :"+ maxd);
System.out.println ("City number: "+ maxc);
System.out.println ("The minimum temperature is :" + mint);
System.out.println ("And the corresponding date is: " + mind);
System.out.println ("City number: " +minc);
}
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner