CLASS - X : PROGRAM BASED ON ARRAYS
Question 1:- Define a class pin code and store the given pin codes in a single dimensional array. Sort these pin codes in ascending order using the Selection Sort technique only. Display the sorted array. [ICSE 2024]
110061, 110001, 110029, 110023, 110055, 110006, 110019, 110033
Solution:-
public class Pincode
{
public static void main(String args[]) {
int[] arr = {110061, 110001,
110029, 110023,
110055, 110006,
110019, 110033};
for (int i = 0; i < 7; i++)
{
int idx = i;
for (int j = i + 1; j < 8;
j++)
{
if (arr[j] <
arr[idx])
idx
= j;
}
int t = arr[i];
arr[i] = arr[idx];
arr[idx] = t;
}
System.out.println("Sorted
Array:");
for (int i = 0; i < 8; i++)
{
System.out.print(arr[i] +
" ");
}
}
}
}
Question 2:- Define a class to accept values into a 3 × 3 array and check if it is a special array. An array is a special array if the sum of the even elements = sum of the odd elements.
[ICSE Speciman 2024]
Example:
A[ ][ ]={{ 4 ,5, 6}, { 5 ,3, 2}, { 4, 2, 5}};
Sum of even elements = 4 + 6 + 2 + 4 + 2 = 18
Sum of odd elements = 5 + 5 + 3 + 5 = 18
Solution:-
import java.util.Scanner;
public class SpecialArray
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int arr[][] = new int[3][3];
long evenSum = 0, oddSum = 0;
System.out.println("Enter the elements of 3 x 3 DDA: ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
arr[i][j] = in.nextInt();
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (arr[i][j] % 2 == 0)
evenSum += arr[i][j];
else
oddSum += arr[i][j];
}
}
System.out.println("Sum of even elements = " + evenSum);
System.out.println("Sum of odd elements = " + oddSum);
if (evenSum == oddSum)
System.out.println("Special Array");
else
System.out.println("Not a Special Array");
}
}
Question 3:- Define a class to accept values into an array of double data type of size 20. Accept a double value from user and search in the array using linear search method. If value is found display message "Found" with its position where it is present in the array. Otherwise display message "not found".
Solution:-
import java.util.Scanner;
public class ExLinearSearch
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
double arr[] = new double[20];
int l = arr.length;
int i = 0;
System.out.println("Enter array elements: ");
for (i = 0; i < l; i++)
{
arr[i] = in.nextDouble();
}
System.out.print("Enter the number to search: ");
double n = in.nextDouble();
for (i = 0; i < l; i++)
{
if (arr[i] == n)
{
break;
}
}
if (i == l)
{
System.out.println("Not found");
}
else
{
System.out.println(n + " found at index " + i);
}
}
}
Question 4: Define a class to declare an array to accept and store ten words. Display only those words which begin with the letter 'A' or 'a' and also end with the letter 'A' or 'a'.
EXAMPLE :
Input : Hari, Anita, Akash, Amrita, Alina, Devi Rishab, John, Farha, AMITHA
Output: Anita
Amrita
Alina
AMITHA
Soluton:-
import java.util.Scanner;
public class KboatWords
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String names[] = new String[10];
int l = names.length;
System.out.println("Enter 10 names : ");
for (int i = 0; i < l; i++)
{
names[i] = in.nextLine();
}
System.out.println("Names that begin and end with letter A are:");
for(int i = 0; i < l; i++)
{
String str = names[i];
int len = str.length();
char begin = Character.toUpperCase(str.charAt(0));
char end = Character.toUpperCase(str.charAt(len - 1));
if (begin == 'A' && end == 'A') {
System.out.println(str);
}
}
}
}
Question 5 :- Write a program to input 15 integer elements in an array and sort them in ascending order using the bubble sort technique.
Solution:-
import java.util.Scanner;
public class ExBubbleSort
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = 15;
int arr[] = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
//Bubble Sort
for (int i = 0; i < n - 1; i++) {
for (int j = i+1; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
System.out.println("Sorted Array:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
Question 6 :- Write a program to accept name and total marks of N number of students in two single subscript array name[] and totalmarks[].
Calculate and print:
The average of the total marks obtained by N number of students.
[average = (sum of total marks of all the students)/N]
Deviation of each student’s total marks with the average.
[deviation = total marks of a student – average]
Solution:-
import java.util.Scanner;
public class StudentMarksPercentage
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = in.nextInt();
String name[] = new String[n];
int totalmarks[] = new int[n];
int grandTotal = 0;
for (int i = 0; i < n; i++) {
in.nextLine();
System.out.print("Enter name of student " + (i+1) + ": ");
name[i] = in.nextLine();
System.out.print("Enter total marks of student " + (i+1) + ": ");
totalmarks[i] = in.nextInt();
grandTotal += totalmarks[i];
}
double avg = grandTotal / (double)n;
System.out.println("Average = " + avg);
for (int i = 0; i < n; i++) {
System.out.println("Deviation for " + name[i] + " = "
+ (totalmarks[i] - avg));
}
}
}
Question 7 :- Write a program to input integer elements into an array of size 20 and perform the following operations:
Display largest number from the array.
Display smallest number from the array.
Display sum of all the elements of the array
Solution:-
import java.util.Scanner;
public class MinimumMaximumSum
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];
System.out.println("Enter 20 numbers:");
for (int i = 0; i < 20; i++) {
arr[i] = in.nextInt();
}
int min = arr[0], max = arr[0], sum = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] < min)
min = arr[i];
if (arr[i] > max)
max = arr[i];
sum += arr[i];
}
System.out.println("Largest Number = " + max);
System.out.println("Smallest Number = " + min);
System.out.println("Sum = " + sum);
}
}
Question 8 :-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!".
Seven Wonders:
CHICHEN ITZA, CHRIST THE REDEEMER, TAJ MAHAL, GREAT WALL OF CHINA, MACHU PICCHU, PETRA, COLOSSEUM
Locations:
MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALYExamples:
Country name: INDIA
Output: TAJ MAHAL
Country name: USA
Output: Sorry Not found!
Solution:-
import java.util.Scanner;
public class SevenWonders
{
public static void main(String args[]) {
String wonders[] = {"CHICHEN ITZA", "CHRIST THE REDEEMER", "TAJMAHAL",
"GREAT WALL OF CHINA", "MACHU PICCHU", "PETRA", "COLOSSEUM"};
String locations[] = {"MEXICO", "BRAZIL", "INDIA", "CHINA", "PERU", "JORDAN",
"ITALY"};
Scanner in = new Scanner(System.in);
System.out.print("Enter country: ");
String c = in.nextLine();
int i;
for (i = 0; i < locations.length; i++) {
if (locations[i].equals(c)) {
System.out.println(locations[i] + " - " + wonders[i]);
break;
}
}
if (i == locations.length)
System.out.println("Sorry Not Found!");
}
}
Question 9 :- Write a program to input twenty names in an array. Arrange these names in descending order of letters, using the bubble sort technique.
Solution:-
import java.util.Scanner;
public class KboatArrangeNames
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String names[] = new String[20];
System.out.println("Enter 20 names:");
for (int i = 0; i < names.length; i++) {
names[i] = in.nextLine();
}
//Bubble Sort
for (int i = 0; i < names.length - 1; i++) {
for (int j = 0; j < names.length - 1 - i; j++) {
if (names[j].compareToIgnoreCase(names[j + 1]) < 0) {
String temp = names[j + 1];
names[j + 1] = names[j];
names[j] = temp;
}
}
}
System.out.println("\nSorted Names");
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
}
}
Question 10 :-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".
Sample Input:
n[0] | n[1] | n[2] | n[3] | n[4] | n[5] | n[6] | n[7] | n[8] | n[9] |
---|---|---|---|---|---|---|---|---|---|
1982 | 1987 | 1993 | 1996 | 1999 | 2003 | 2006 | 2007 | 2009 | 2010 |
Solution:-
import java.util.Scanner;
public class ExampleGraduationYear
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n[] = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};
System.out.print("Enter graduation year to search: ");
int year = in.nextInt();
int l = 0, h = n.length - 1, idx = -1;
while (l <= h) {
int m = (l + h) / 2;
if (n[m] == year) {
idx = m;
break;
}
else if (n[m] < year) {
l = m + 1;
}
else {
h = m - 1;
}
}
if (idx == -1)
System.out.println("Record does not exist");
else
System.out.println("Record exists");
}
}
No comments:
Post a Comment