0% found this document useful (0 votes)
10 views10 pages

Java Array Operations and Searches

The document contains multiple Java classes that perform various operations on arrays, including printing negative numbers, counting specific divisible numbers, finding products of even integers, and handling student marks. It also includes functionalities for searching characters using binary search, sorting temperatures and UID numbers, and checking for sparse matrices. Each class demonstrates different array manipulation techniques such as linear search, bubble sort, selection sort, and matrix operations.

Uploaded by

shoebraza559
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Java Array Operations and Searches

The document contains multiple Java classes that perform various operations on arrays, including printing negative numbers, counting specific divisible numbers, finding products of even integers, and handling student marks. It also includes functionalities for searching characters using binary search, sorting temperatures and UID numbers, and checking for sparse matrices. Each class demonstrates different array manipulation techniques such as linear search, bubble sort, selection sort, and matrix operations.

Uploaded by

shoebraza559
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

/**

* 1. Define a class to accept ‘n’ integers in a single subscripted variable and


perform the
following tasks:-
1) Print Negative numbers list.
2) Print the count of numbers that are divisible by 7 or endsWith 7.
3) Find and print the product of EVEN integers.
*/
import [Link].*;
class Arrays_P1
{
static void main()
{
Scanner sc=new Scanner ([Link]);
[Link]("Enter n");
int n=[Link]();/**stores the size of array*/
int a[]=new int[n];/**stores n integers*/
[Link]("Enter "+n+" integers");
for(int i=0;i<[Link];i++)/**i is used as index number*/
a[i]=[Link]();
int count=0;/**count of numbers divisible 7 or ends with 7*/
int p=1;/**product of even integers*/
[Link]("Negtive numbers list....");
for(int i=0;i<[Link];i++)
{
if(a[i]<0)
[Link](a[i]);
if(a[i]%7==0 || a[i]%10==7)
count++;
if(a[i]%2==0)
p=p*a[i];
}
[Link]("Count of numbers divisible 7 or ends with 7 ="+count);
[Link]("Product of even integers="+p);
}
}
/**
* 2. Define a class to accept and store ‘n’ students’ names and marks in a subject
SCIENCE
out of 100 in two different single dimensional arrays and print the following list.
1) Print the student name and marks who have secured in the range 40 to 60.
2) Print the student name and marks who have secured more than or equal to 80
*/
import [Link].*;
class Arrays_P2
{
static void main()
{
Scanner sc=new Scanner ([Link]);
[Link]("Enter n");
int n=[Link]();/**stores the size of array*/
int marks[]=new int[n];/**stores n integers*/
String names[]=new String[n];
[Link]("Enter "+n+" students marks and names");
for(int i=0;i<[Link];i++)
{ /**i is used as index number*/

names[i]=[Link]();
marks[i]=[Link]();
}
[Link]("students securing 40-60");
for(int i=0;i<[Link];i++)
{
if(marks[i]>=40 && marks[i]<=60)
{
[Link](names[i]+" "+marks[i]);
}
}
[Link]("students securing >=80");
for(int i=0;i<[Link];i++)
{
if(marks[i]>=80)
{
[Link](names[i]+" "+marks[i]);
}
} }}
/**
* 3. Define a class to accept 10 students names and their percentage marks in two
different
arrays and search for a name and display the corresponding percentage marks
along with
the name if found in the array, otherwise display the message “Data not found”
using
Linear Search technique.
*/
import [Link].*;
class Arrays_P3
{
void main()
{
Scanner sc=new Scanner([Link]);
double marks[]=new double[10];/**stores 10 marks*/
String names[]=new String[10];/**stores 10 names*/
String search;/**name to search*/
[Link]("Enter 10 names and % marks");
for(int i=0;i<[Link];i++)/**i-index number*/
{
names[i]=[Link]();
marks[i]=[Link]();

}
[Link]("Enter a name to be searched");
search=[Link]();
boolean found=false;/**flag variable*/
for(int i=0;i<[Link];i++)
{
if([Link](names[i]))
{
found=true;
[Link]("Name :"+names[i]);
[Link]("Marks :"+marks[i]);
break;
}
}
if(found==false)
[Link]("Data not found"); }}
/**
* 4. Define a class to search for a character from the list of characters given below
and display
the message “character found” along with the position if found in the array,
otherwise
display the message “character not found” using binary search technique.
Y, W, U, S, P, N, L, K, G, C, A
*/
import [Link].*;
public class Arrays_P4
{
static void main() {
Scanner sc=new Scanner([Link]);
char a[]={'Y','W','U','S','P','N','L','K','G','C','A'};
// 0 1 2 3 4 5 6 7 8 9 10
[Link]("Enter the character to search");
char search= [Link]().charAt(0); /**stores a character*/
int lb=0; /**lower boundary*/
int ub=[Link]-1; /**upper boundary*/
int m; /**middle index*/
boolean found=false; /**flag variable*/
while(lb<=ub) //9<=10
{
m=(lb+ub)/2;
if(search<a[m])
lb=m+1; //lb=9
else if(search>a[m])
ub=m-1;
else if(search==a[m])
{
found=true;
[Link]("Character found at index number "+m);
break;
}
}
if(found==false)
[Link]("Data not found");
}
}
/**
* 5. Define a class to accept 10 temperature values in double type array and sort
the
temperatures in descending order using bubble sort technique. Display the
sorted array.
*/
import [Link].*;
class Arrays_P5
{
void main()
{
Scanner sc=new Scanner([Link]);
double temp[]=new double[10];/** stores the temperatures*/
[Link]("Enter 10 temperatures");
for(int i=0;i<[Link];i++) /**i-index number*/
{
temp[i]=[Link]();
}
for(int i=0;i<[Link]-1;i++)
{
for(int j=0;j<[Link]-i-1;j++)
{
if(temp[j]<temp[j+1])
{
double t=temp[j]; /**t-temporary variable*/
temp[j]=temp[j+1];
temp[j+1]=t;
}
}
}
[Link]("Temperatures in descending order");
for(int i=0;i<[Link];i++)
[Link](temp[i]);
}
}
/**
* 6. Define a class to accept 10 students UID numbers in an array. Sort the
numbers in
ascending order using selection sort technique. Display the sorted array.
*/
import [Link].*;
class Arrays_P6
{
void main()
{
Scanner sc=new Scanner([Link]);
int uid[]=new int[10];/**stores 10 uid numbers*/
[Link]("Enter 10 UID numbers");
for(int i=0;i<[Link];i++)/**i-index number*/
uid[i]=[Link]();
int min;/**least number*/
int pos;/**position of least number*/
int t;/**temporary variable*/
for(int i=0;i<[Link]-1;i++)
{
min=uid[i];
pos=i;
for(int j=i+1;j<[Link];j++)
{
if(uid[j]<min)
{
min=uid[j];
pos=j;
}
}
t=uid[i];
uid[i]=min;
uid[pos]=t;
}
[Link]("UID numbers in order");
for(int i=0;i<[Link];i++)
[Link](uid[i]);
}
}
/**
* 7. Define a class to accept integer values in 3x3 array and print the array in
matrix format
and also find and display the minimum integer value.
Example: Output:
Input: A[][]={{1,5,3},{2,8,4},{9,2,5}} Array in Matrix format
153
284
925
Minimum value is 1
*/
import [Link].*;
class Arrays_P7
{
void main()
{
Scanner sc=new Scanner([Link]);
int a[][]=new int[3][3];/**stores 9 values*/
[Link]("Enter 9 values");
for(int i=0;i<[Link];i++)/**i-row index*/
{
for(int j=0;j<[Link];j++)/**j-column index*/
{
a[i][j]=[Link]();
}
}
int min=a[0][0];/**stores minimum value*/
[Link]("Array in matrix format");
for(int i=0;i<[Link];i++)/**i-row index*/
{
for(int j=0;j<[Link];j++)/**j-column index*/
{
[Link](a[i][j]+" ");
if(a[i][j]<min)
min=a[i][j];
}
[Link]();
}
[Link]("Minimum value is "+min);
}}
/**
* 8. Define a class to accept integer values in 4x4 array and find and display the
sum of primary
and secondary diagonal values
Example: Input: Output:
1 5 3 2
2 8 4 3 Sum of Primary diagonal values = 19 (1+8+5+5)
9 2 5 4 Sum of Secondary diagonal values = 13 (2+4+2+5)
5 2 8 5

*/
import [Link].*;
class Arrays_P8{
void main() {
Scanner sc=new Scanner([Link]);
int a[][]=new int[4][4];/**stores 16 values*/
[Link]("Enter 16 values");
for(int i=0;i<[Link];i++)/**i-row index*/
{
for(int j=0;j<[Link];j++)/**j-column index*/
{
a[i][j]=[Link]();
}
}
int sum1=0;/**sum of primary diagonal values*/
int sum2=0;/**sum of secondary diagonal values*/
for(int i=0;i<[Link];i++)/**i-row index*/
{
for(int j=0;j<[Link];j++)/**j-column index*/
{
[Link](a[i][j]+"\t");
if(i==j)
sum1=sum1+a[i][j];
else if(i+j==3)
sum2=sum2+a[i][j];
}
[Link]();
}
[Link]("sum of primary diagonal values="+sum1);
[Link]("sum of secondary diagonal values="+sum2); }}
/**
* 9. Define a class to accept integer values in 3 x 3 array and display the sum of
each row.
Example:
Input: A[][]={{1,5,3},{2,8,4},{9,2,5}}
Output:
Sum of row 1 = 9
Sum of row 2 = 14
Sum of row 3 = 16

*/
import [Link].*;
class Arrays_P9
{
void main()
{
Scanner sc=new Scanner([Link]);
int a[][]=new int[3][3];/**stores 9 values*/
[Link]("Enter 9 values");
for(int i=0;i<[Link];i++)/**i-row index*/
{
for(int j=0;j<[Link];j++)/**j-column index*/
{
a[i][j]=[Link]();
}
}
int sum=0;/**sum of each row*/
for(int i=0;i<[Link];i++)/**i-row index*/
{
for(int j=0;j<[Link];j++)/**j-column index*/
{
sum=sum+a[i][j];
}
[Link]("Sum of row "+(i+1)+"="+sum);
sum=0;
}
}
}
/**
* 10. Define a class to accept integer values in 3x3 array and check and print
whether it is an
sparse matrix or not. A sparse matrix is a matrix which most of the elements are
zero.
Example 1:
Input: A[][]={4,5,6},{7,0,0},{0,0,0}}
Output: Sparse Matrix
Example 2:
Input: A[][]={1.2,3},{4,5,6},{0,0,1}}
Output: Not an sparse Matrix

*/
import [Link].*;
class Arrays_P10
{
void main()
{
Scanner sc=new Scanner([Link]);
int a[][]=new int[3][3];/**stores 9 values*/
int count=0;/**count of 0's*/
[Link]("Enter 9 values");
for(int i=0;i<[Link];i++)/**i-row index*/
{
for(int j=0;j<[Link];j++)/**j-column index*/
{
a[i][j]=[Link]();
if(a[i][j]==0)
count++;
}
}
if(count>(9-count))
[Link]("Sparse Matrix");
else
[Link]("Not a Sparse Matrix");
}
}

You might also like