0% found this document useful (0 votes)
22 views12 pages

Java Array Operations and Searches

The document contains multiple Java programming tasks, including defining classes to handle 4x4 arrays, searching for names in an array, calculating percentages based on student marks, and performing various operations on arrays. Each task includes code snippets and explanations of the intended functionality, such as summing rows, searching for names, and finding diagonal sums. The document serves as a guide for implementing basic data structures and algorithms in Java.

Uploaded by

gamergamingsingh
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)
22 views12 pages

Java Array Operations and Searches

The document contains multiple Java programming tasks, including defining classes to handle 4x4 arrays, searching for names in an array, calculating percentages based on student marks, and performing various operations on arrays. Each task includes code snippets and explanations of the intended functionality, such as summing rows, searching for names, and finding diagonal sums. The document serves as a guide for implementing basic data structures and algorithms in Java.

Uploaded by

gamergamingsingh
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

-

Sl i~ o n 6
Define a class to accept
0 values into 4x4 array an
' Example: d find and display the su
)b
' . m of each
Arr[ ][ ]={{l,2,3,4},{5,6,7,8 row
D Output:
}, {l,3,5,7},{2,5,3,1}}
)() su m of the elements ro w
1 =10 (1 +2+3+4)
,5 su m of the elements ro w
2= 26 (5+6+7+8)
su m of the elements ro w
tr) 3=16 (1+3+5+7)
su m of the elements ro w
4= 11(2+5+3+1)
~ Ans. / / To find the su m
of rows in a do ub le dim
0
im po rt [Link].*; ensional array
:IS
class Sum_row
{
public static vo id main(St
ring args[ ])
{
Scanner in =n ew Scanner(S
[Link]);
in t i,j,r,c;
in t m[ ][ ]=new int[4][4];
[Link]("Enter
,, th e nu m be rs in a 4*4 mat
for(i=0;i<4;i++) rix:");
{
forG=0~<4~++)
m[i][j]=[Link]( );
}
[Link]("The el
ements of th e m at rix are:"
for(i=0;i<4;i++) );
{
forQ=0~<4~++)
{
[Link](m[i][j]+"
} ");
[Link]( );

\ static String valueOf( all primitive types)
I
}Syst em.o [Link]•mtln("T h
e sum of the elem ents of each row:");
for(i=O;i<4;i++) .
{
r=O;
forQ=0~<4~++)
{
r=r+m[i][j];
} .
Syst em.o [Link] intln ("Su m of the elements of row "+(i+l)+"· = "+r);
} '

}
}
Question s @
. , · ' " ·' · • , [1
Define a class to accept the_ n~ es ~f lO s~.~~~
t~ in an ~a y and check for the existence of t
given name in the array '?sing line ar [Link]:
_I~. f~~~, pnn t the ~os itio n of the name and
found, print the app rop nat e me ssa ge. Als o pnn if n
Ans. //a pro gra m to sea rch a nam e in an arra t . t~e .n~ es whl ~h beg in wit h the word "SR
y < ,,.

import [Link].*;

class [Link]
{
public static voi d mai n(S trin g arg s[ ])
{
Scanner in= new Scanner([Link]);
String arr[ ] = new Stri ng[ l0];
inti ;
String nm ;
[Link]("Enter 10 nam es in the arra
y one by one: ");
for(i=0; i<l0 ; i++)
·~ ·--,.
{
arr[i]=[Link]( );
}
[Link]("Enter a nam e to sea rch in .. '

the array: ");


nm = [Link]( );
I I Linear sea rch
int k = 0, pos = 0, c =0;
for(i = 0; i<[Link]; i++)
r
if([Link](art[i]))
{
pos = i;
k = 1!
}
if(arr[i].startsWith("SRI")) ..
c++;
}

.,. ___ ,_n__ ... ◄ l~ftlu


-7
if (k======== 1)
syste [Link] [Link] tln("S tring is found at positi on: ""+(pos+l));
else
Syste [Link] [Link] tln("S tring is not found!");
Syste [Link] [Link] tln("N umbe r of name s begin s with SRI: "+c);
(fori = O; i<arr. lengt h; i++)
{
if(arr[i] .start sWith("SRI"))
{
Syste [Link] [Link] tln(ar r[i]);
}
}
}
}
Variable Descr iption
Data type Descr iption
Variable
arr[] String to store name s irt the array
.
1 int to run the loop
nm String to enter a name to search in the array
k int a flag to check the presen ce of a name in the array
pos int to store the positi on of the search ed name , if found
C int to count the name s that starts with SRI in .the array

~
Question 7
Rohit has go
Pi_3. .
• ma rks in the ICS E Ex am ina tio ns as:
Eng Hindi HCG Maths Science CApp
94 95 96 92 96 98
He wants to calculate his per cen tag e ma rks on
the basis of English plus best four.
Write a program in Jav a to acc ept the nam e and
ma rks of all the subjects in a Single D
Array. Calculate and dis pla y the per cen tag e obt
ain ed along wit h the name.
Sample Output: You hav e got 95.8% on the bas is
of Eng. plus best four
Ans. I I To calculate the per cen tag e ma rks obt ain
ed in ICSE Examination
import [Link].*;
public class Ma rks
{
public static voi d ma in( Str ing args[ ])
{
Scanner in= new Sca nne r ([Link]);
int i, j, t=O, sum=O;
float avg = O;
String str;
int m[ ] = new int[6];
[Link]("Enter nam e: ");
str=[Link]( ); .
[Link]("Enter ma rks in all the stx . ." .
sub1ects. ),
for(i=O; i<6; i++)
{
m[i]=[Link]( );
}
for(i=l; i<S; i++)
Sample Paper - 2 (Sol
&NCZJ ,(i
I torG==i+1; ;<6; i++)
1
if(m[iJ<mlj])
{
t=:m[i1;
m[i1=m[j1;
m{j]= t;
)
)
)
for(i= l; i<=4; i++)
l
sum= sum+ m[i];
l
avg=( float)(m[O]+sum) / 5;
[Link](str+" has got" + avg+ "% on the basis of Eng. plus best four");
l
l
Variable Descr iption
Variable Data type Description
str String to accept the name the studen t
m(] int to store the marks of six subjects in an array
i int to run the loop for storing marks as well as for sortin g
int to run the inner loop for sorting the marks
j
t int a tempo rary variable
sum int to store the marks of best four subjects
avg float to store the marks of English plus best four subjec ts
c ~"\. count the deleted characters
Question 6 ~Y . {1S]
The school o c---- eeps the records of all the students of a class by entering admission numbers
and the names of the students. Write a program to store all names along with their corres\'londin~
admission numbers. Now, enter admission number of a student and search whether the name is
present or not. If the name is present then display the name along with the admission number,
otherwise display an appropriate message using 'Linear Search' technique.
1 An~. / / To search a name using admission number
[Link]. \..&.VU. V.&.c; LV \..Q.1.\.. u.J.Q LC a.I. LU ;;:, LVJ. C 1..1. LC V .1..1..1. U.1..1. L'-' u i L 1,

/.~ /
Que stio n 4 • f'tS
Wri te a pro ~~ o sear ch for a float valu e inp ut by the
use r in the· sor ted list :by usin g b.
sea rch tech .niq ue. If fou nd disp lay "Sea rch Suc cess ful"
and prin t th·e ind ex of the elem1
:the arra y, othe rwi se disp lay "No such elem ent pres ent
in :th-e· tist!!,n
Sam ple Inp ut: ",
m[O] m[l] m[2] m[3] m[4] m[S] m[6 ] m[7 ] m[9] · ,
36.7 45.6
m[8J
31.4 50.2 60.5 72.8 75.6 81.8 86.2 90.4
sam ple outp ut: Inp ut a valu e to search: 81.8
- 1 - • -
Question 8 Al . .
Write a pro~n to input and sto~ mteger elements ih a double dimensio nal arra o
x and find the sum of element s m the left diagonal . Y
3 3
For example: 1 ,3 5
4 6,8
9 2 4·
Output: Sum of the left diagonal elements = (1 + 6 + 4) = 11
Ans. ; / To find the sum of left diagonal elements
import [Link].*;
public class LDiagon als
{
public static void main(Str ing args[ ])
{
Scanner in=new Scanner( System.i n);
int i,j,ld=0;
int m[ ][ ]=new int[3][3];
System.o [Link] ("Enter the elements in the matrix:");
for(i=0;i<3;i++)
{
forQ=0~<3~++)
m[i][j]=[Link]( );
}
System.o [Link] ("Elemen ts of the matrix are:");
for(i=0;i<3;i++)
{
. forQ=0~<3~++)
System.o [Link](m [i][j]+" ");
System.o [Link] ( );
}
for(i=0;i<3;i++)
ld=ld+m[i][i];
System.o [Link] ("The sum of the left diagona l elements = "+Id);
}
}
Variable Description
V. • b Data type
arta le . Description
in( ][ ] int
.
1 to accept the elements in the array
int to run the outer loop
j . s

int to run the inner loop


ld
int
to store the sum of left diagonal elements
t.1 tt: 1uup
A~
- - - ~ ......... &. •• .1. \..I. l

Question 6

Wnte .......... t • t .• . ( . . . [15]
~ pro~~.... o tnpu integer elements into an array of size 20 and perform the followin
operations: ·· g
(i) Display the largest number from the array
(ii) Display the smallest number from the array
(iii) Display the sum of all the elenlents of the array
Ans. / / To find the largest, smallest and sum of array elements
import [Link].*;
p ll I.V 01.V.l.~ UL'-,. VJ.J.c,.LLL'-4.1. . L L ~ . . _ .. . . , _ ...

\
I

Question 8 \ ~ v J \lS\
·l
Write a pro~o initialise the 'Seven Wonders' of the World along with their locations in\
two different arrays. Search for a name of the country input by the user. If found, display tht
I country along with its Wonder, otherwise display 'Sorry Not Found!'.
l \ Seven Wonders: Chichen Itza, Christ the Redeemer, Taj Mahal, Great Wall of China, Machu Picchu,
h : '\ Petra, Colosseum
1· .... ':
Locations: Mexico, Brazil, India, China, Peru, Jordan, Italy
Examples:
, I
Country Name: India
I I
Outout: Tai Mahal
V,
0

~
0 ,-.
(1
0
--c
-
C
(10
-,
0,)

3
,--r
0
("')
~
(1)
~
(1'
,-..J
I
0
0)

-
~
CJ
1ft
g.
0)

-
9-
~
~
,.
:,
1
9-
0,

~
-

You might also like