Professional Documents
Culture Documents
Chapter 7 Arrays
Chapter 7 Arrays
CLASS X
COMPUTER APPLICATIONS
Ch.7 : Arrays
1. It works both for sorted and 1. It takes place only on sorted data items.
unsorted arrays.
2. It begins from start of an 2. Array is divided into two halves and then
array. desired data item is matched with the
middle element in the array. If it does not
match, then the search is restarted either
in first half or in second half.
Q.18. Write a Java statement to compare two Strings m[i] and m[j] in a single
dimensional array.
Ans. If(m[i].compareTo(m[j])>0)
Q.2. WAP to create an array to enter age of five persons (integer value), store
them in an array and display.
Ans. import java.util.*;
class Arr2
{
int a[];
Scanner in = new Scanner (System.in);
Arr2
{
a=new int[n];
for(int i=0;i<n;i++)
{
System.out.println(“Enter an Age=”);
a[i]=in.nextInt());
}
}
void display()
{
System.out.println(“\nValues are as follows“);
for(int i=0;i<n;i++)
{
System.out.print(““+a[i]);
}
}
}
Q.3. WAP to accept 10 numbers by the user and display only odd numbers
Ans. import java.util.*;
public class Arr3
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
{
int a=new int[n];
int b=new int[n];
int oddnum=0;
for(int i=0;i<10;i++)
{
System.out.println(“Enter 10 numbers”);
a[i]=in.nextInt());
if(a[i]%2!=0)
{
b[oddnum]=x[i];
Oddnum++;
}
}
for(int j=0;i<oddnum;j++)
{
System.out.print(b[j]);
}
}
Q.5. WAP to accept 10 numbers in a SDA and linear search that given no is
present with position or not and display the relevantmessage.(linear search
for integer)
Ans. import java.util.*;
public class Linear_pos
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
int i,pos,sh;pos=0;
int m[]=new int[10];
for(i=0;i<=9;i++)
{
System.out.print("Ënter number in the cell");
m[i]=in.nexInt();
}
Q.6. WAP to accept 5 string in a SDA and check a given no is present or not and
display the relevant message(linear search for string).
Ans. import java.util.*;
public class Linear_string
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
String names[]={"Hiten","Swastik","Navneet","Äkansha","Shweta"};
int pos;pos=-1;
System.out.println("Ënter the name to be searched");
String sh=in.next();
for(inti=0;i<=4;i++)
{
if(sh.equalsIgnoreCase(names[i]))
{
pos=i;
break;
}
}
if(pos>=0)
System.out.println("The name "+sh+" ïs present");
else
System.out.println("The name "+sh+" is not present");
}
}
Q.7. WAP to accept number in an SDA and check that the given no is present or
not and display the relevant message. (binarysearchforinteger)
Ans. import java.util.*;
public class Binary_int
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
int i,lb,ub,pos,mid,searchno;
searchno=0;pos=0;lb=0;ub=9;mid=0;
int m[]=new int[10];
for(i=0;i<=9;i++)
{
System.out.print("Enter number in ascending order in the cell");
m[i]=in.nextInt();
}
while(lb<=ub)
{
pos=(lb+ub)/2;
if(searchno==m[mid])
{
pos=mid+1;
break;
}
elseif(searchno>m[mid])
lb=mid+1;
else
ub=mid-1;
}
if(pos==0)
System.out.println("The search is unsuccessful");
else
System.out.println(“The"+searchno+"is found at"+pos+"position");
}
}
Q.8. WAP to accept 5 string in a SDA and search whether the selected name is
present or not and display the relevant message.(binary search for string)
Ans. import java.util.*;
public class Binary_string
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
int i,lb,ub,pos,k;k=0;
String str2;
pos=0;lb=0;ub=4;
String str[]=new String[5];
System.out.print("Ënter 5 names");
for(i=0;i<=4;i++)
{
str[i]=in.next();
}
while(lb<=ub)
{
pos=(lb+ub)/2;
if(str[pos].compareTo(str2)<0)
lb=pos+1;
if(str[pos].compareTo(str2)>0)
ub=pos-1;
if(str[pos].compareTo(str2)==0)
{
k=1;
break;
}
}
if(k==1)
System.out.println("The"+str2+" is found at"+pos+"position");
else
System.out.println("The search is unsuccessful");
}
}
Q.9. WAP to sort an array using bubble sort in ascending order (on integer value)
Ans. import java.util.*;
public class Bubble_ascend
{
public static void main(String args[])
{
Q.10. WAP to sort an array using bubble sort in descending order. (on integer value)
Ans. import java.util.*;
public class Bubble_descend
{
public static void main(String args[])
{
Q.11. WAP to sort an array using bubble sort in ascending order. (on String value)
Ans. import java.util.*;
public class Bubble_st_ascend
{
public static void main(String args[])
{
Q.12. WAP to sort an array using selection sort in ascending order. (on integer
value)
Ans. import java.util.*;
public class Seleca_scend
{
public static void main(String args[])
{
System.out.println(“Enter elements”);
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
m[i][j]=in.nextInt();
}
}
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
sumr=sumr+m[i][j];
}
System.out.println(“The sum of row “+(i+1)+” = “+sumr);
sumr=0;
}
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
sumc=sumc+m[j][i];
}
System.out.println(“The sum of column “+(i+1)+” = “+sumc);
sumc=0;
}
}
}
Q.14. WAP to initialize a Double Dimension Array(DDA) of 3 rows and 3 columns
and display the diagonal elements.
Ans. import java.util.*;
public class Diagonal
{
public static void main(String args[])
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
m[i][j]=in.nextInt();
}
}
System.out.println(“The array is:”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(m[i][j]+” “);
} System.out.println();
}
n[0] n[1] n[2] n[3] n[4] n[5] ... n[16] n[17] n[18] n[19]
45 65 77 71 90 67 ... 82 19 31 52
System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
System.out.println("Prime Numbers:");
for (int i = 0; i < arr.length; i++) {
int c = 0;
for (int j = 1; j <= arr[i]; j++) {
if (arr[i] % j == 0) {
c++;
}
}
if (c == 2)
System.out.print(arr[i] + ", ");
}
}
}
Q.16. Write a program to accept name and total marks of N number of students in
two single subscript arrays name[ ] and totalmarks[ ].
Calculate and print:
(i) The average of the total marks obtained by N number of students.
[average = (sum of total marks of all the students)/N]
(ii) Deviation of each student's total marks with the average.
[deviation = total marks of a student - average]
import java.util.Scanner;
Write a program to read the data, calculate and display the following:
(a) Average marks obtained by each student.
(b) Print the roll number and the average marks of the students whose average is
above. 80.
(c) Print the roll number and the average marks of the students whose average is
below 40.
import java.util.Scanner;