Thursday 4 August 2011

ARRAY 6


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 pascal
{
  public static void main(int n)
  {
    int pas[]=new int[n+1];
    pas[0]=1;
    for(int i=0;i<n;i++)
    {
      for(int j=0;j<=i;j++)
        System.out.print(pas[j]+" ");
      System.out.println();
      for(int j=i+1;j>0;j--)
        pas[j]=pas[j]+pas[j-1];
    }
  }
} 2. Assign numbers into an array of size 4X4. Then calculate and print the sum of individual diagonals, sum of each row and sum of each column. 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. Write a Program to Store 10 numbers in (Descending order) an array. Then ask the users to enter a Number and search it in array (using binary search method). Display the position if number is found otherwise display "not found". public class binarySearchRev
{
  public static void main(int x)
  {
    int a[]={20,18,15,13,10,6,4};
    int l=0,h=a.length-1;
    boolean flag=false;
    int m=(l+h)/2;
    while(h>=l)
    {
      m=(l+h)/2;
      if(a[m]>x)
        l=m+1;
      else
        if(a[m]<x)
          h=m-1;
        else
        {
          flag=true;
          break;
        }
    }
    if(flag)
      System.out.println("found in position "+(m+1));
    else
      System.out.println("number not found in array");
  }
} 4. Accept the name and total marks of 15 students from user. Then display the list according to their merit. (Use Bubble sort) import java.io.*;
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());
    }
    String x="";
    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;
        }
      }
    }
    System.out.println("Name \t\t Marks");
    for(i=0;i<5;i++)
      System.out.println(n[i]+"\t\t"+m[i]);
  }
} 5. Write a Program to accept Names of 20 students (from user) in an Array. Then arrange names alphabetically using Selection sort method. import java.io.*;
public class nameSort
{
  public static void main() throws IOException
  {
    DataInputStream stdin = new DataInputStream(System.in);
    String n[]=new String[5];
    String small,k;
    int i,j,pos,l=n.length;
    for(i=0;i<5;i++)
    {
      System.out.print("Enter Name : ");
      n[i]=stdin.readLine();
    }
    for(i=0;i<l-1;i++)
    {
      small=n[i];
      pos=i;
      for(j=i+1;j<l;j++)
      {
        if(n[j].compareTo(small)<0)
        {
          small=n[j];
          pos=j;
        }
      }
      k=n[i];
      n[i]=n[pos];
      n[pos]=k;
    }
    for(j=0;j<5;j++)
      System.out.println(n[j]);
  }
} 6. A metropolitan hotel has ten floors and each floor having fifty rooms. Write a program that will display the room and floor number of a given Visitor. import java.io.*;
public class Hotel
{
  public static void main() throws IOException
  {
    DataInputStream stdin = new DataInputStream(System.in);
    String n[][]=new String[10][50];
    String ser;
    int i,j;
    for(i=0;i<10;i++)
    {
      for(j=0;j<50;j++)
      {
        System.out.print("Enter Visitor Name : ");
        n[i][j]=stdin.readLine();
      }
    }
    System.out.print("Enter Name to be Searched : ");
    ser=stdin.readLine();
    for(i=0;i<10;i++)
      for(j=0;j<50;j++)
        if(n[i][j].compareTo(ser)==0)
          System.out.println("Vistor stay in floor "+(i+1)+" Room No. "+(j+1));
  }
}  

No comments:

Post a Comment