Thursday 4 August 2011

ARRAY 1


1. Write a program to generate a Pascal Triangle in which each number in the row is the sum of the two numbers immediately above it. The program should print the first 5 rows of the triangle. public class ques39
{
  public static void main()
  {
    int size=5;
    int a[][]=new int[size][size];
    int i,j;
    a[0][0]=1;
    // prepare the pascal triangle //
    for(i=1;i<size;i++)
    {
      for(j=0;j<size;j++)
      {
        if((i==j) || (j==0))
          a[i][j]=1;
        else
          a[i][j]=a[i-1][j-1]+a[i-1][j];
      }
    }
    // display the pascal triangle //
    for(i=0;i<size;i++)
    {
      for(j=1;j<=15-i;j++)
        System.out.print(" ");
      for(j=0;j<=i;j++)
        System.out.print(a[i][j]+" ");
      System.out.println();
    }
  }
} 2. Assign numbers into an array of size 4X4. Then calculate the sum of numbers present in the diagonals position, also calculate the sum of each row total and column total. public class RowColDiagTotal
{
  public static void main()
  {
    int a[][]={{4,5,8,1},
                   {2,8,7,4},
                   {3,4,5,6},
                   {1,2,7,8}};
    int i,j,Row=0,Col=0,Diag1=0,Diag2=0;
    for(i=0;i<4;i++)
    {
      for(j=0;j<4;j++)
      {
        if(i==j)
          Diag1+=a[i][j];
        if((i+j)==3)
          Diag2+=a[i][j];
      }
    }
    System.out.println("Sum of Left diagonals :"+Diag1);
    System.out.println("Sum of RIght diagonals :"+Diag2);
    for(i=0;i<4;i++)
    {
      Row=0;
      for(j=0;j<4;j++)
      {
        Row+=a[i][j];
      }
      System.out.println("Row "+(i+1)+" total is : "+Row);
    }
    for(i=0;i<4;i++)
    {
      Col=0;
      for(j=0;j<4;j++)
      {
        Col+=a[j][i];
      }
      System.out.println("Column "+(i+1)+" total is : "+Col);
    }
  }
} 3. Assign 10 numbers into an array and then find the Highest and Lowest value in the array and also find their position in array. public class MaxMinPos
{
  public static void main()
  {
    int i;
    int a[]={3,7,2,4,1,6,7,10,9,8};
    int max=a[0],min=a[0];
    int maxPos=0,minPos=0;
    for(i=0;i<10;i++)
    {
      if(a[i]>max)
      {
        max=a[i];
        maxPos=i;
      }
      if(a[i]<min)
      {
        min=a[i];
        minPos=i;
      }
    }
    System.out.println("Maximum No. = "+max);
    System.out.println("Position = "+maxPos);
    System.out.println("Minimum No. = "+min);
    System.out.println("Position = "+minPos);
  }
} 4. Accept the name and total marks of 15 students from user. Then display the list according to their merit. (Use Bubble sort technique) import java.io.DataInputStream;
import java.io.IOException;
public class ques19
{
  public static void main() throws IOException
  {
    DataInputStream stdin = new DataInputStream(System.in);
    String n[]=new String[5];
    int m[]=new int[5];
    int i,j,k;
    for(i=0;i<5;i++)
    {
      System.out.print("Enter Name : ");
      n[i]=stdin.readLine();
      System.out.print("Enter Comp. Sc. Marks : ");
      m[i]=Integer.parseInt(stdin.readLine());
    }
    // Sort the array in Descending order
    String x=null;
    for(i=0;i<4;i++)
    {
      for(j=i+1;j<5;j++)
      {
        if(m[i]<m[j])
        {
          k=m[i]; m[i]=m[j]; m[j]=k;
          x=n[i]; n[i]=n[j]; n[j]=x;
        }
      }
    }
    // display Merit List
    System.out.println("Name \t\t Marks");
    for(i=0;i<5;i++)
      System.out.println(n[i]+"\t\t"+m[i]);
  } // end of method
} 5. WAP in Java to accept names of the 10 politicians and their party to which they belong to in two separate arrays. Ask the users to enter the name of the party. Printout all the names of the politicians who belong to that party. import java.io.*;
public class ques23
{
  public static void main() throws IOException
  {
    DataInputStream stdin = new DataInputStream(System.in);
    String party[]=new String[10];
    String poli[]=new String[10];
    String search;
    boolean flag=false;
    for(int i=0;i<10;i++)
    {
      System.out.print("Enter Party Name : ");
      party[i]=stdin.readLine();
      System.out.print("Enter Politician Name : ");
      poli[i]=stdin.readLine();
    }
    System.out.print("Enter Searching Party Name : ");
    search=stdin.readLine();
    System.out.println("Name of Politician's");
    for(int i=0;i<5;i++)
      if(search.compareTo(party[i])==0)
        System.out.println(poli[i]);
  }
}

No comments:

Post a Comment