KEMBAR78
Array Question | PDF | Algorithms | Computing
0% found this document useful (0 votes)
31 views16 pages

Array Question

Uploaded by

Sanjib Das
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)
31 views16 pages

Array Question

Uploaded by

Sanjib Das
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
You are on page 1/ 16

1

Write a program to initialize an array of 5 names and initialize another array


with their respective telephone numbers. Search for a name input by the
User, in the list. If found, display "Search Successful " and print the name
along with the telephone number, otherwise display "Search unsuccessful.
Name not enlisted". [ICSE 2005]
Ans. import java.util.*;
class Question72
{
static void search()
{
Scanner sc=new Scanner(System.in);
String name[]={"Amit","Deb","Somesh","Trina","Ashish"};
long
phone[]={9832164084L,9476278923L,8936745126L,9866733497L,9832349667L
};
String n;
boolean f=false;
int i;
System.out.println("Enter the name to search:");
n=sc.nextLine();
for(i=0;i<name.length;i++)
{
if(n.equalsIgnoreCase(name[i]))
{
System.out.println("Search Successful");
System.out.println(name[i]+"\t"+phone[i]);
f=true;
break;
}
}
if(!f)
System.out.println("Search unsuccessful. Name not enlisted");
}
}
****************************
The marks obtained by 50 students in a subject are tabulated as follows: -
Name Marks
Write a program to input the names and the marks of the students in the
subject. Calculate and display:
i) The subject average marks (subject average marks = subject total / 50)
ii) The highest mark in the subject and the name of the student. (The
maximum marks in the subject are 100). [ICSE 2006]
2

Ans. import java.util.*;


class Question73
{
static void main()
{
String name[]=new String[50],n="";
int marks[]=new int[50];
int i,tot=0,max=0;
float avg;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the names and marks for 50 students:");
for(i=0;i<name.length;i++)
{
name[i]=sc.nextLine();
marks[i]=sc.nextInt();
sc.nextLine();//dummy input
}
for(i=0;i<name.length;i++)
{
tot=tot+ marks[i];
}
avg=(float)tot/50;
System.out.println("Subject Average Marks:"+avg);
for(i=0;i<name.length;i++)
{
if(i==0)
{
max=marks[i];
n=name[i];
}
if(marks[i]>max)
{
max=marks[i];
n=name[i];
}
3

}
System.out.println("Highest mark:"+max+" Name:"+n);
}
}
///////////////

Question 6:
Write a program to initialize the given data in an array and find the minimum ;
and maximum values along with the sum of the given elements.(2007)
Numbers : 2 5 4 1 3
Output :
Minimum value : 1
Maximum value: 5
Sum of the elements : 15 [15]

Answer:
class maxmin
{
public static void main()
{
int [ ]n = {2,5,4,1,3};
int h=n[0];
int l=n[0];
int s=0;
for(int i=0; i<n.length; i++)
{
if(h<n[i])
{
h=n[i]
}
if(l<n[i])
{
l=n[i];
}
s=s+n[i];
}
System.out.println(“Minimum Value :”+l);
System.out.println(“Maximum Value :”+h);
4

System.out.println(“The Sum of the elements :”+s);


}
}

Define a class and store the given city names in a single dimensional array.
Sort these names in alphabetical order using the Bubble Sort technique
only.
INPUT: Delhi, Bangalore, Agra, Mumbai, Calcutta
OUTPUT: Agra, Bangalore, Calcutta, Delhi, Mumbai [ICSE 2008]
Ans. class Question74
{
static void main()
{
String a[]={"Delhi","Bangalore","Agra","Mumbai","Calcutta"};
int i,j;
String t="";
for(i=a.length-1;i>=0;i--)
{
for(j=0;j<i;j++)
{
if(a[j].compareTo(a[j+1])>0)
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
System.out.println("Sorted Array:");
for(i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}

The annual examination results of 50 students in a class are tabulated as


follows:
Roll no. Subject A Subject B Subject C
5

--------- ------------ ------------ ------------


Write a program to read the data, calculate and display the following: (a)
Average mark obtained by each student, (b) Print the roll number and
average marks of the students whose average mark is above 80, (c) Print
the roll number and average marks of the students whose average mark is
below 40. [ICSE 2009]
Ans. import java.util.*;
class Question76
{
static void main()
{
Scanner sc=new Scanner(System.in);
int rollno[]=new int[50];
int subA[]=new int[50];
int subB[]=new int[50];
int subC[]=new int[50];
float avg[]=new float[50];
int i;
System.out.println("Enter the roll no. and marks in 3 subjects for 50 students:");
for(i=0;i<rollno.length;i++)
{
rollno[i]=sc.nextInt();
subA[i]=sc.nextInt();
subB[i]=sc.nextInt();
subC[i]=sc.nextInt();
sc.nextLine();//dummy input
}
System.out.println("Average of each student:");
for(i=0;i<rollno.length;i++)
{
avg[i]=(float)(subA[i]+subB[i]+subC[i])/3;
System.out.println(rollno[i]+"\t\t"+avg[i]);
}
System.out.println("Students whose average is more than 80:");
for(i=0;i<rollno.length;i++)
{
if(avg[i]>80)
System.out.println(rollno[i]+"\t\t"+avg[i]);
}
System.out.println("Students whose average is less than 40:");
for(i=0;i<rollno.length;i++)
{
if(avg[i]<40)
6

System.out.println(rollno[i]+"\t\t"+avg[i]);
}
}
}
//////////////////////////////////////
Write a program to perform binary search on a list of integers given below,
to search for an element input by the user. If it is found display the element
along with its position, otherwise display the message “Search element not
found”. 5,7,9,11,15,20,30,45,89,97 [ICSE 2010]
Ans. import java.util.*;
class Question75
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a[]={5,7,9,11,15,20,30,45,89,97};
int n,l=0,u=a.length-1,m;
boolean f=false;
System.out.println("Enter the number to search:");
n=sc.nextInt();
while(l<=u)
{
m=(l+u)/2;
if(a[m]==n)
{
System.out.println("Element:"+a[m]);
System.out.println("Position:"+m);
f=true;
break;
}
else if(a[m]>n)
u=m-1;
else
l=m+1;
}
if(!f)
System.out.println("Search element not found");
}
}
*****************************
7

Question 5.
Write a program to input and sort the weight of ten people. Sort and display
them in descending order using the selection sort technique. [15]2011
Ans.
import java.util.Scanner;
public class SelectionSort
{
public void program()
{
Scanner scanner = new Scanner(System.in);
int[] weights = new int[10];
System.out.println("Enter weights: ");
for (int i = 0; i < 10; i++)
{
weights[i] = scanner.nextInt();
}
for (int i = 0; i < 10; i++)
{
int highest = i;
for (int j = i + 1; j < 10; j++)
{
if (weights[j] > weights[highest])
{
highest = j;
}
}
int temp = weights[highest];

weights[highest] = weights[i];
weights[i] = temp;

}
System.out.println("Sorted weights:");

for (int i = 0; i < 10; i++)


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

}
}

*******************
8

Question 9

Write a program to accept the names of 10 cities in a single dimension string array and
their STD (Subscribers Trunk Dialing) codes in another single dimension integer array.
Search for a name of a city input by the user in the list. If found, display “Search Successful”
and print the name of the city along with its STD code, or else display the message “Search
Unsuccessful, No such city in the list’. [15]2012

Ans.

import java.util.Scanner;

public class Cities {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

String[] cities = new String[10];

int[] std = new int[10];

for (int i = 0; i < 10; i++) {

System.out.print("Enter city: ");

cities[i] = scanner.next();

System.out.print("Enter std code: ");

std[i] = scanner.nextInt();

System.out.print("Enter city name to search: ");

String target = scanner.next();

boolean searchSuccessful = false;

for (int i = 0; i < 10; i++) {

if (cities[i].equals(target)) {

System.out.println("Search successful");

System.out.println("City : " + cities[i]);

System.out.println("STD code : " + std[i]);

searchSuccessful = true;

break;

if (!searchSuccessful) {
9

System.out.println("Search Unsuccessful, No such city in the list");

Write a program to input 10 integer elements in an array and sort them


In descending order using bubble sort technique. [15]2013
Ans.

import java.util.Scanner;

public class BubbleSort {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Enter ten numbers:");


int[] numbers = new int[10];

for (int i = 0; i < 10; i++) {


numbers[i] = scanner.nextInt();
}
for (int i = 0; i < 10; i++) {

for (int j = 0; j < 10 - i - 1; j++) {


if (numbers[j] < numbers[j + 1]) {

int temp = numbers[j];

numbers[j] = numbers[j + 1];

numbers[j + 1] = temp;
}

}
}

System.out.println("Sorted Numbers:");
for (int i = 0; i < 10; i++) {
10

System.out.println(numbers[i]);
}

}
}
*****************

Write a program to accept the year of graduation from school as an integer value from the
user. Using the Binary Search technique on the sorted array of integers given below, output
the message ‘Record exists’ if the value input is located in the array. If not, output the
message Record does not exist”.[2014]
(1982, 1987, 1993. 1996, 1999, 2003, 2006, 2007, 2009, 2010)

Ans.

import java.util.Scanner;

public class Search


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

System.out.print("Enter year of graduation: ");


int graduationYear = scanner.nextInt();

int[] years = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009,
2010};
boolean found = false;

int left = 0;
int right = years.length - 1;

while (left <= right)


{
int middle = (left + right) / 2;

if (years[middle] == graduationYear)
{
found = true;

break;
}
11

else if (years[middle] < graduationYear) {

left = middle + 1;
}
Else
{

right = middle - 1;
}

}
if (found)
{

System.out.println("Record exists");
}
Else
{

System.out.println("Record does not exist");


}

}
}
*****************
Write a program in to input and store all roll numbers, names and marks in 3
subjects of n number of students in five single dimensional arrays and display
the remark based on average marks as given below: [15]2015
Average marks = total marks/3
Average marks Remark
85 – 100 EXCELLENT
75 – 84 DISTINCTION
60 – 74 FIRST CLASS
40 – 59 PASS
Less than 40 POOR
Ans.

import java.util.Scanner;

public class Marks


{
public static void main(String[] args) {
12

Scanner scanner = new Scanner(System.in);


System.out.print("Enter number of students: ");
int n = scanner.nextInt();
int[] rollNumbers = new int[n];
String[] names = new String[n];
int[] subject1Marks = new int[n];
int[] subject2Marks = new int[n];
int[] subject3Marks = new int[n];
for (int i = 0; i < n; i++)
{
System.out.println("Student " + (i + 1));
System.out.print("Enter roll number: ");
rollNumbers[i] = scanner.nextInt();
System.out.print("Enter name: ");
scanner.nextLine(); // To consume the new line produced on
hitting
// enter after entering roll number
names[i] = scanner.nextLine();
System.out.print("Enter marks in subject 1: ");
subject1Marks[i] = scanner.nextInt();
System.out.print("Enter marks in subject 2: ");
subject2Marks[i] = scanner.nextInt();
System.out.print("Enter marks in subject 3: ");
subject3Marks[i] = scanner.nextInt();

for (int i = 0; i < n; i++) {


System.out.println("Roll number = " + rollNumbers[i] + ", Name = " + names[i]);
int averageMarks = (subject1Marks[i] + subject2Marks[i] +
subject3Marks[i]) / 3;
if (averageMarks >= 85 && averageMarks <= 100) {
System.out.println("EXCELLENT");
} else if (averageMarks >= 75 && averageMarks <= 84) {
System.out.println("DISTINCTION");
} else if (averageMarks >= 60 && averageMarks <= 74) {
System.out.println("FIRST CLASS");
} else if (averageMarks >= 40 && averageMarks <= 59) {

System.out.println("PASS");
} else if (averageMarks < 40) {
System.out.println("POOR");
}

}
13

}
************************
Question 9:
Write a program to initialize 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
the name of the country along with its Wonder, otherwise display “Sorry Not Found!”
[15]2016
Seven wonders – CHICHEN ITZA, CHRIST THE RDEEEMER, TAJMAHAL, GREAT WALL OF
CHINA, MACHU PICCHU, PETRA, COLOSSEUM
Locations – MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY
Example – Country Name: INDIA Output: INDIA – TAJMAHAL
Country Name: USA Output: Sorry Not Found!

Ans.

import java.util.Scanner;
public class SevenWonders {
public static void main(String[] args) {
String[] sevenWonders = { "CHICHEN ITZA", "CHRIST THE RDEEEMER",
"TAJMAHAL", "GREAT WALL OF CHINA",
"MACHU PICCHU", "PETRA", "COLOSSEUM" };
String[] locations = { "MEXICO", "BRAZIL", "INDIA", "CHINA", "PERU",
"JORDAN", "ITALY" };
Scanner scanner = new Scanner(System.in);
System.out.print("Enter country: ");

String country = scanner.next();


// Search country in the array using linear search
int index = -1;
for (int i = 0; i < locations.length; i++) {
if (locations[i].equals(country)) {
index = i;
14

}
}
if (index != -1) {
System.out.println(locations[index] + " - " + sevenWonders[index]);
} else {
System.out.println("Sorry Not Found!");
}
}
}
************************
Write a program to input integer elements into an array of size 20 and perform the
following operations: [15]2017
i) Display largest number from the array
ii) Display smallest number from the array
iii) Display sum of all the elements of the array

Ans.

import java.util.Scanner;

public class ArrayOperations {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[20];

System.out.print("Enter 20 numbers: ");


for (int i = 0; i < 20; i++) {

numbers[i] = scanner.nextInt();
}

int smallest = numbers[0];

int largest = numbers[0];


int sum = 0;
for (int i = 0; i < 20; i++) {
if (numbers[i] < smallest) {
15

smallest = numbers[i];
}

if (numbers[i] > largest) {


largest = numbers[i];

}
sum = sum + numbers[i];

}
System.out.println("Smallest = " + smallest);

System.out.println("Largest = " + largest);


System.out.println("Sum = " + sum);

}
}
**************************
Question 9
Write a program to accept name and total marks of N number of students in
two single subscripts array 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][2018]
Ans.
import java.util.Scanner;
public class Student {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = scanner.nextInt();
String[] name = new String[n];
int[] totalmarks = new int[n];
for (int i = 0; i < n; i++)
{
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");

name[i] = scanner.next();
System.out.print("Enter marks: ");
totalmarks[i] = scanner.nextInt();
}
16

int sum = 0;
for (int i = 0; i < n; i++) {
sum = sum + totalmarks[i];
}
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++) {
double deviation = totalmarks[i] - average;

System.out.println("Deviation of " + name[i] + " is " + deviation);


}
}
}

You might also like