problem in two d array

7 views
Skip to first unread message

Ankush Singhal

unread,
Jul 3, 2011, 5:20:03 AM7/3/11
to ducat_java_9
hey guys plz help.....
i m facing some prblm in the assignment which was given my mayur sir
in 2-d array.....
i m nt able to find the max average from d 5 arrays, i knw d logic bt
i m nt able to implement it...
plz help...
THANYOU

Deepmala Kushwaha

unread,
Jul 4, 2011, 7:56:36 AM7/4/11
to ducat_...@googlegroups.com
hi,

below is the code-----



//average of rows

class twoArray2
{
    public static void main(String s[])
    {
    int i,j;
    int a[][]=new int[5][];

     a[0]=new int[Integer.parseInt(s[0])];
     a[1]=new int[Integer.parseInt(s[1])];
     a[2]=new int[Integer.parseInt(s[2])];
     a[3]=new int[Integer.parseInt(s[3])];
     a[4]=new int[Integer.parseInt(s[4])];
   
    for(i=0;i<a.length;i++)
    {
        for(j=0;j<a[i].length;j++)
        {
        a[i][j]=(int)(Math.random()*100);
        }
    }

    System.out.println("Two Dim Array:");

     for(i=0;i<a.length;i++)
    {
        for(j=0;j<a[i].length;j++)
          {
        System.out.print("   "+a[i][j]);
        }
        System.out.println();
    }

    System.out.println("Two Dim Avg Array:");

    int avg[]=new int[a.length];

     for(i=0;i<a.length;i++)
     {

    avg[i]=0;
                   
    for(j=0;j<a[i].length;j++)
          {
               avg[i] += a[i][j];
              
        }

        avg[i]=avg[i]/a[i].length;   

        System.out.println(" Average row[" +i+ "]\t"+avg[i]);
    }

//max of avg 
    int max=0;
    int max_row=0;

    for(i=0;i<avg.length;i++)
    {
        if(max<avg[i])
               {
            max=avg[i];
            max_row=i;
        }


    }//for   

    System.out.println("\n   Max Avg value="+max+"\t row="+max_row);
}
}


Deepmala

Ankush Singhal

unread,
Jul 5, 2011, 6:24:25 AM7/5/11
to ducat_...@googlegroups.com
thanku :)

manish

unread,
Jul 5, 2011, 6:55:07 AM7/5/11
to ducat_java_9



class TwoDArrayTest
{
public static void main(String s[])
{
int a[][];
int nor=6;
int noc=5;
int sum=0;
int avg=0;
a=new int[nor][noc];
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
a[i][j]=(int)(Math.random()*100);
sum+=a[i][j];
avg =sum/noc;
System.out.print("\t"+a[i][j]);
}
System.out.print("\t"+sum);
System.out.print("\t"+avg);
System.out.println();
sum=0;
avg=0;
}
}
}
Reply all
Reply to author
Forward
0 new messages