Вы находитесь на странице: 1из 11

15 Questions on Arrays – Kp

(Downloadable from www.kishorepandit.com)

Program 1. Write a program to input 10 numbers in an array. Display all alternate values in reverse, starting from the last value. If the array holds 1 7 5 9 3, then the output should be 3 5 and 1.

//Display Alternate elements in reverse import java.io.*; public class ARR

{ public static void main(String args[])throws IOException

{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int arr[]=new int[10]; System.out.println("Enter 10 values: "); for(int i=0; i<arr.length; i++)

 { arr[i]=Integer.parseInt(br.readLine()); }

System.out.println("Alternate elements in reverse : ");

for(int i=arr.length-1; i>=0 ; i-=2)

 { System.out.println(arr[i]); }

}//main

}//class

Program 2. Write a program to input marks in 7 subjects of a student in an integer array. Now display the total marks, average marks and the percentage. Assume the maximum total marks to be 700.

//Sum and average import java.io.*; public class ARR

{ public static void main(String args[])throws IOException

{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int arr[]=new int[7]; System.out.println("Enter marks in 7 subjects: "); int total=0; double average=0; for(int i=0; i<arr.length; i++)

{

}

arr[i]=Integer.parseInt(br.readLine());

total=total+arr[i];

average=(double)total/arr.length; System.out.println("Total marks = "+total); System.out.println("Average marks = "+average); }//main }//class

Program 3. Input 10 values in an array. Display all the entered numbers and the frequency of odd numbers present in them.

//Frequency of odd numbers import java.io.*; public class ARR

15 Questions on Arrays – Kp

(Downloadable from www.kishorepandit.com)

{ public static void main(String args[])throws IOException

{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int arr[]=new int[10]; System.out.println("Enter 10 values: "); for(int i=0; i<arr.length; i++)

 { arr[i]=Integer.parseInt(br.readLine()); } int count=0;

System.out.println("Numbers entered: "); for(int i=0; i<arr.length; i++)

{ System.out.println(arr[i]);

if(arr[i]%2==1)

{ count++;

}//if }//i System.out.println("Frequency of odd numbers = "+count); }//main

}//class

Program 4. Input 10 values in an array. Display only the even numbers in it. Also display the average of these numbers (even numbers). Keep in mind that the sum of even numbers should be divided by the number of even numbers and not the length of the array.

//Average of even numbers import java.io.*; public class ARR

{ public static void main(String args[])throws IOException

{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int arr[]=new int[10]; System.out.println("Enter 10 values: "); for(int i=0; i<arr.length; i++)

 { arr[i]=Integer.parseInt(br.readLine()); }

int count=0; int sum=0; System.out.println("Even numbers entered: "); for(int i=0; i<arr.length; i++)

{ if(arr[i]%2==0)

{ System.out.println(arr[i]); count++; sum+=arr[i]; }//if }//i double average=(double)sum/count; System.out.println("Average of even numbers = "+average); }//main

}//class

Program 5. Write a Java class called ARRAYS with the following specifications:

15 Questions on Arrays – Kp

(Downloadable from www.kishorepandit.com)

Instance variables:

arr[] – integer array to store 10 values high1, high2 – integer variables to store the largest number and the second largest number respectively. smallest- integer variables to store the smallest number in the array. Methods:

Constructor to instantiate the array. void input() – To input values in the array. void highLow() – to store the largest number in high1and the smallest in smallest. void secondLargest() – to store the second largest number in high2. void display() – to display the highest, second highest and the smallest number. Do not use any sorting technique in your program. Also write main to call the functions declared above.

//highest and second highest import java.io.*; class ARRAYS

{ int arr[]; int high1; int high2; int smallest; ARRAYS()

{ arr=new int[10];

high1=0;

high2=0;

smallest=0;

}

void input()throws IOException

{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter values"); for(int i=0; i<arr.length; i++)

{ arr[i]=Integer.parseInt(br.readLine());

}

}//input void highLow()

{ high1=arr[0];

smallest=arr[0];

for(int i=1;i<arr.length;i++)

{ if(arr[i]>high1)

{ high1=arr[i];

}//if

if(arr[i]<smallest)

{ smallest=arr[i];

}//if

}//i

}//highest

void secondHighest()

{ high2=smallest;

for(int i=0;i<arr.length;i++)

{ if(arr[i]>high2 && arr[i]<high1)

{ high2=arr[i];

15 Questions on Arrays – Kp

(Downloadable from www.kishorepandit.com)

}//if }//i }//second highest void display()

{ System.out.println("Highest Value = "+high1); System.out.println("Second Highest Value = "+high2); System.out.println("Smallest Value = "+smallest); }//display

public static void main(String args[])throws IOException

{ ARRAYS obj=new ARRAYS(); obj.input(); obj.highLow(); obj.secondHighest(); obj.display(); }//main }//class

Program 6.

Input 10 numbers in an array from the user. These numbers can be positive, negative or neutral but cannot contain a decimal point. Display the following facts about the input:

a) Number of negative numbers present in the array.

b) Number of prime numbers present in the array.

//Count in array import java.io.*; public class ARR

{ public static void main(String args[])throws IOException

{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int arr[]=new int[10]; System.out.println("Enter 10 integers : "); for(int i=0; i<arr.length; i++)

 { arr[i]=Integer.parseInt(br.readLine()); }

int countNegative=0;

int countPrime=0;

for(int i=0; i<arr.length; i++)

{ if(arr[i]<0)

 { countNegative++; } int factors=0;

//abs() used below to obtain positive equivalent of the number for(int j=1; j<=(int)Math.abs(arr[i]); j++)

{ if(arr[i]%j==0)

 { factors++; } }//j if(factors==2) { countPrime++; }

15 Questions on Arrays – Kp

(Downloadable from www.kishorepandit.com)

}//i System.out.println("Negative numbers = "+countNegative); System.out.println("Prime numbers = "+countPrime); }//main }//class

Program 7. Write a Java program to input 10 values in an array and display if the highest values comes before or after the smallest value, or is at the same place(in case all the values in the array are same).

//high and low import java.io.*; public class ARR

{ public static void main(String args[])throws IOException

{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int arr[]=new int[10]; System.out.println("Enter 10 values: "); for(int i=0; i<arr.length; i++)

 { arr[i]=Integer.parseInt(br.readLine()); }

int high=arr[0]; int highPos=0; int low=arr[0]; int lowPos=0; for(int i=1; i<arr.length; i++)

{ if(arr[i]>high)

{ high=arr[i]; highPos=i; }//if if(arr[i]<low)

{ low=arr[i]; lowPos=i; }//if }//i if(highPos>lowPos)

 { System.out.println("Highest values comes after the smallest value"); }

else if(highPos<lowPos)

 { System.out.println("Highest values comes before the smallest value"); } else { System.out.println("Both are at the same place"); }

}//main

}//class

Program 8. Write a main function in a Java class to input 10 positive integers in an array. Replace all repetitions of the numbers by a zero. Display all the unique (non-zero) values of the array.

//Unique Values

15 Questions on Arrays – Kp

(Downloadable from www.kishorepandit.com)

import java.io.*; public class ARR

{ public static void main(String args[])throws IOException

{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int arr[]=new int[10]; System.out.println("Enter 10 values: "); for(int i=0; i<arr.length; i++)

 { arr[i]=Integer.parseInt(br.readLine()); }

//the nested loop below will match an element with all the elements after it for(int i=0; i<arr.length; i++)

{ for(int j=i+1; j<arr.length; j++)

{ if(arr[i]==arr[j])

{ arr[j]=0;

}//if

}//j

}//i

System.out.println("Unique array elements are : "); for(int i=0; i<arr.length; i++)

 { if(arr[i]!=0) { System.out.println(arr[i]); } }

}//main

}//class

Program 9. Input 10 integers in an array. Display the frequency of each number present in the array. There should be no repetitions in the display of numbers and their frequencies.

//Frequency import java.io.*; public class ARR

{ public static void main(String args[])throws IOException

{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int arr[]=new int[10]; System.out.println("Enter 10 values: "); for(int i=0; i<arr.length; i++)

 { arr[i]=Integer.parseInt(br.readLine()); }

for(int i=0; i<arr.length; i++)

{ int count=0;

//count arr[i] in the whole array for(int j=0; j<arr.length; j++)

{ if(arr[i]==arr[j])

{ count++;

}//if }//j //count arr[i] before the index [i] int before=0;

15 Questions on Arrays – Kp

(Downloadable from www.kishorepandit.com)

for(int j=i-1; j>=0; j--)

{ if(arr[i]==arr[j])

{

before++;

}//if }//j if(before==0)//the number hasn't appeared before

 { System.out.println("Number = "+arr[i]+"\tFrequency ="+count); } }//i

}//main

}//class

Program 10. Write a Java program to ask 5 values from the user and store them in an array named a[]. Reverse these values in another array called b[]. Check for the equality of these arrays. If the contents are the same display “Palindrome array”, otherwise display “Not a palindrome array”.

//Palindrome array import java.io.*;

public class ARR

{ public static void main(String args[])throws IOException

{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int a[]=new int[5]; int b[]=new int[5]; System.out.println("Enter 5 values "); for(int i=0; i<a.length; i++)

 { a[i]=Integer.parseInt(br.readLine()); }

//Reversing the array in b[] int index=0;// for b[] for(int i=a.length-1; i>=0; i--)

{ b[index]=a[i];

index++;

}

//checking for equality int count=0;

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

{ if(a[i]==b[i])

{ count++;

}//if }//i if(count==a.length)

 { System.out.println("Palindrome array"); } else { System.out.println("Not a Palindrome array"); }

}//main

}//class

Program 11.

15 Questions on Arrays – Kp

(Downloadable from www.kishorepandit.com)

Initialize an array with any 10 numbers. Now ask a value from the user and display if it is present in the array or not. Also display the position of the value if it is present in the array. All the occurrences should be displayed if the value is present more than once in the array. Use the sequential (or linear) search technique. Make sure that your program prints “not found” just once if the value is not present in the array.

//Sequential Search import java.io.*;

public class ARR

{ public static void main(String args[])throws IOException

{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int arr[]={34,65,68,53,23,84,31,50,16,57,39,95}; System.out.println("Enter value to search : "); int search=Integer.parseInt(br.readLine()); int flag=0; for(int i=0; i<arr.length; i++)

{ if(search==arr[i])

{ System.out.println("Found the value at position : "+(i+1));

flag=1;

//break not put so that all the occurances are displayed }//if }//i

if(flag==0)

 { System.out.println("Value not present in the array"); }

}//main

}//class

Program 12. Write a object oriented program to sort an array. Create and use the class specified below – Class Name – SORT Instance Variables – arr[](integer) Methods:

Parameterized constructor which accepts the size of the array & creates it. void input() – Runs loops and makes the user enter values in the array. void sort() – Sort the array in descending order using the selection sort technique. void display() – Runs loops and displays the contents of the array.

//Sorting import java.io.*; class SORT

{ int arr[]; SORT(int size)

 { arr=new int[size]; }

void input()throws IOException

{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter values"); for(int i=0; i<arr.length; i++)

 { arr[i]=Integer.parseInt(br.readLine()); }

15 Questions on Arrays – Kp

(Downloadable from www.kishorepandit.com)

}//input void sort()

{ for(int i=0;i<arr.length-1;i++)

{ int highest=arr[i]; int pos=i;

for(int j=i+1;j<arr.length;j++)

if(arr[j]>highest) highest=arr[j]; pos=j; }//if }//j int temp=arr[i]; arr[i]=arr[pos]; arr[pos]=temp; }//i }//sort void display()

{ System.out.println("Values in descending order"); for(int i=0;i<arr.length;i++)

{

{

 { System.out.println(arr[i]); }

}//display public static void main(String args[])throws IOException

{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter size for the array"); int size=Integer.parseInt(br.readLine()); SORT obj=new SORT(size); obj.input(); obj.sort(); obj.display(); }//main

}//class

Program 13. Write a Java program to initialize an array with some numbers. Make sure that the numbers are in ascending order. Now input a value from the user and display its index in the array. Use the binary search technique in the program. Also display an appropriate message if the value is not present in the array.

//Binary search import java.io.*; public class ARR

{ public static void main(String args[])throws IOException

{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int arr[]={1,3,5,6,9,10,11,15,20,25};//The array is sorted System.out.println("Enter search value : "); int s=Integer.parseInt(br.readLine()); int b=0; int e=arr.length-1; while(b<=e)

{ int m=(b+e)/2;

15 Questions on Arrays – Kp

(Downloadable from www.kishorepandit.com)

if(s==arr[m])

 { System.out.println("Found at index "+m); break; } else if(s>arr[m]) { b=m+1; } else if(s

}

}//while if(b>e)

 { System.out.println("Not found in the array "); }

}//main

}//class

Program 14. Write a Java program to input the names(String) and prices(Double) of 10 items in two different arrays. Now sort and display this list in ascending order of the prices. Use the bubble sorting technique in your program.

//Bubble Sort

import java.io.*;

public class ARR

{ public static void main(String args[])throws IOException

{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String item[]=new String[5]; double price[]=new double[5]; for(int i=0; i<item.length; i++)

 { System.out.println("Enter Item name "); item[i]=br.readLine(); System.out.println("Enter its price"); price[i]=Double.parseDouble(br.readLine()); } //Bubble sorting

for(int i=0; i<price.length-1; i++)

{ for(int j=0; j<price.length-1-i; j++)

{ if(price[j]>price[j+1])

{ double temp=price[j];

price[j]=price[j+1];

price[j+1]=temp;

String temps=item[j];

item[j]=item[j+1];

item[j+1]=temps;

}//if

}//j }//i System.out.println("List sorted on price : "); for(int i=0; i<item.length; i++)

{ System.out.println(item[i]+"\t"+price[i]);

15 Questions on Arrays – Kp

(Downloadable from www.kishorepandit.com)

}

}//main

}//class

Program 15. Write a Java program to simulate a dictionary. Using an initializer list, store some words in an array and their meanings in corresponding indexes of another array. Name them word[] and mean[]. Also take care to store words in ascending order in the array. Now input a word from the user and display its meaning from the arrays using the binary search technique. Make the search case insensitive. Display "Word not found" if the word is not present in the array.

// Dictionary import java.io.*; public class ARR

{ public static void main(String args[])throws IOException

{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String word[]={"Fable","Volatile","Nazi","Opaque","Unleash"}; String mean[]={"A short moral story","Easily evaporating","A member of NSGW","Impenetrable by light","To release"}; System.out.println("Enter a word : "); String s=br.readLine(); int b=0; int e=word.length-1; while(b<=e)

{ int m=(b+e)/2; if(s.equalsIgnoreCase(word[m]))

 { System.out.println("Meaning = "+mean[m]); break; }

else if(s.compareTo(word[m])>0)

 { b=m+1; }

else if(s.compareTo(word[m])<0)

{

e=m-1;

}

}//while if(b>e)

 { System.out.println("Word not found"); }

}//main

}//class

[ END ]