Arrays
Which of the following is the correct usage?
- int a[-40]
- int a[40]
- float a[0 - 40]
- None
Answer
int a[40]
Reason — The declaration of array int a[40]
follows correct syntax.
Which element is represented by a[10]?
- 10th
- 9th
- 11th
- None
Answer
11th
Reason — Array indexes start from 0. So, a[10] refers to the 11th element of the array.
Cell numbers of a dimensional array are also known as:
- packets
- blocks
- subscripts
- compartments
Answer
subscripts
Reason — Cell numbers of a dimensional array are also known as subscripts.
A dimensional array is also known as:
- subscripted variable
- actual variable
- compound variable
- none
Answer
subscripted variable
Reason — A dimensional array is also known as subscripted variable because each element of the array is represented by using a common variable name along with a single subscript.
An array element can be accessed through:
- dots
- element name
- index number
- none
Answer
index number
Reason — An array element can be accessed through the index number.
Indicate the error message which displays, if the following statement is executed :int a[5] = {28,32,45,68,12};
- Insufficient cells
- Array index out of bounds
- Elements exceeding cells
- None
Answer
None
Reason — The statement int a[5] = {28,32,45,68,12};
is perfectly correct and will not generate any error message.
The following statement :int code[ ]= {25,37,38,42};
- assigns 37 to code[1]
- assigns 25 to code[1]
- assigns 38 to code[3]
- assigns 42 to code[0]
Answer
assigns 37 to code[1]
Reason — The statement int code[ ]= {25,37,38,42};
assigns 37 to code[1] as the array index begins from 0. So the second value (37) will be assigned to code[1].
The elements of array[50] are numbered:
- from 1 to 50
- from 0 to 49
- from 1 to 51
- none
Answer
from 0 to 49
Reason — The elements of array[50] are numbered from 0 to 49 as the array index begins from 0.
Which of the following function finds the size of array
char m[] = {'R', 'A', 'J', 'E', 'N', 'D', 'R', 'A' };?
- m.sizeof (a)
- m.elementsof (m)
- m.length
- None
Answer
m.length
Reason — m.length will find the size of array m[].
A Single Dimensional array contains N elements. What will be the last subscript?
- N-1
- N
- N+1
- None
Answer
N-1
Reason — The array subscripts range from 0 to (n-1), where n is the number of elements in the array.
Each element of a single dimensional array is accessed using variable along with subscript enclosed within square bracket.
A double dimensional array uses two types of indices.
To access the elements in reverse order, the single dimensional array is traversed starting with last index.
Complete the following statement:
int a[ ] = new int a[15]
The last index of a single dimensional array will be 7, if it is declared to store 8 elements.
int m[] = {2,4,6,8};
System.out.println(m[1] + " " + m[2]);
4 6
m[1] gives the second element of the array which is 4.
m[2] gives the third element of the array which is 6.
int a[] = {2,4,6,8,10};
a[0] = 23;
a[3] = a[1];
int c = a[0] + a[1];
System.out.println("Sum = "+c);
Sum = 27
a[0]=23 assigns 23 to the first element of the array. a[3]=a[1] assigns the value of second element of the array which is 4 to the fourth element of the array. After the execution of these two statements array looks like this:
{23, 4, 6, 4, 10}
a[0] + a[1] ⇒ 23 + 4 ⇒ 27
int a[] = new int [5];
a[0] = 4; a[1] = 8; a[2] = 7; a[3] = 12; a[4] = 3;
System.out.println(a[2+1]);
12
a[2+1] ⇒ a[3] ⇒ 12
int a[4] = {2,4,6,8};
for(i = 0; i <= 1; i++)
{
s = a[i] + a[3-i];
System.out.println(s);
}
10
10
i | Output | Remark |
---|---|---|
0 | a[0] + a[3] ⇒ 2 + 8 ⇒10 | First Iteration |
1 | a[1] + a[2] ⇒ 4 + 6 ⇒10 | Second Iteration |
Java allows storing a number of similar type of data using a common variable name such that each element is accessed with the help of a subscript or cell number. This is done with a purpose that any operation can be carried commonly on all the data items. You can search a number by dividing the sorted elements in two halves in each iteration. Beyond searching, the elements can also be sorted using different techniques. One technique allows us to choose the lowest element starting with a specific subscript in each pass and place it in appropriate position whereas, in other technique, consecutive elements are compared and a number of exchanges are made in each pass. We can also join two or more arrays to form a single set.
Based on the above discussion, answer the following questions:
(a) What is the term used for a common variable storing a number of similar data?
- Numeric variable
- Static variable
- Subscripted variable
- Local variable
(b) Which of the following techniques is used for searching an element by dividing the sorted list of elements into two halves in each iteration?
- Linear search
- Binary search
- Sequential search
- None of the above
(c) Which of the following sorting techniques finds the lowest element in each pass and places it in appropriate position?
- Selection sort
- Binary sort
- Bubble sort
- Insertion sort
(d) Which of the following term is used for joining two or more arrays to form a single array?
- Joining
- Collecting
- Merging
- Sorting
Answer
(a) Subscripted variable
(b) Binary search
(c) Selection sort
(d) Merging
What is meant by Dimensional Array?
Answer
A dimensional array is a structure created in the memory to represent a number of data values of the same data type having the same variable name along with different subscripts.
Name the two types of Dimensional Array.
Answer
The two types of dimensional array are:
- Single Dimensional Array
- Double Dimensional Array
What is the need of Dimensional Array? Explain.
Answer
Variables are useful for keeping track of a single piece of information but as we collect more and more information, keeping the variables organized can be complicated. In such situations, we need arrays to solve the problems in a much better and efficient way.
'Array is a composite data type'. Explain this statement.
Answer
The data type that represents a number of similar or different data under single declaration is called as composite data type. An array is the concept of using a number of variables declared with the same data type. Hence, an array is said to be a composite data type.
Define the following with their constructs:
(a) Single Dimensional Array
(b) Double Dimensional Array
Answer
(a) Single Dimensional Array — A single dimensional array is a structure that is represented along X-axis by using a number of variables with the same name having different subscripts. Each element of the array is represented by using a common variable name along with a single subscript.
The syntax of declaring a single dimensional array is as follows:
<data type> <array-variable>[] = new <data type>[<number of values to be stored>];
OR<data type> [] <array-variable> = new <data type>[<number of values to be stored>];
(b) Double Dimensional Array — A double dimensional array is a structure that is represented along X-axis and Y-axis, where X-axis represents the number of rows and Y-axis represents the number of columns respectively. Each cell in a double dimensional array can be addressed by using row number and column number using two separate square brackets along with array variable.
The syntax of declaring a double dimensional array is:
<data type> <array-variable>[][] = new <data type>[<Number of rows>][<Number of columns>];
OR<data type> [][] <array-variable> = new <data type>[<Number of rows>][<Number of columns>];
Subscript and Subscripted variable
Answer
Subscript | Subscripted variable |
---|---|
Subscript is the index of the element in the array. | Subscripted variable is the name of the array when it is used with a subscript to access a single element of the array. |
Char a[5] and int a[5]
Answer
Char a[5] | int a[5] |
---|---|
char a[5] is an array of char data type. | int a[5] is an array of int data type. |
This array can hold 5 characters. | This array can hold 5 integer values. |
Ordinary variable and array variable
Answer
Ordinary variable | Array variable |
---|---|
An ordinary variable can hold only one value. | An array variable can refer to a group of values of the same data type by using a subscript. |
For example: int a; Here variable a holds one int type value. | For example: int a[ ] = new int[5]; Here variable a[ ] can store 5 integer type values which will be accessed as a[0], a[1], a[2], a[3] and a[4]; |
Sorting and Searching
Answer
Sorting | Searching |
---|---|
Sorting means to arrange the elements of the array in ascending or descending order. | Searching means to search for a term or value in an array. |
Bubble sort and Selection sort are examples of sorting techniques. | Linear search and Binary search are examples of search techniques. |
Linear search and Binary search
Answer
Linear Search | Binary Search |
---|---|
Linear search works on sorted and unsorted arrays. | Binary search works only on sorted arrays (ascending or descending.) |
Each element of the array is checked against the target value until the element is found or end of the array is reached. | Array is successively divided into 2 halves and the target element is searched either in the first half or in the second half. |
Linear Search is slower. | Binary Search is faster. |
Selection sort and Bubble sort
Answer
Selection sort | Bubble sort |
---|---|
Selection Sort selects the smallest element from unsorted sub-array and swaps it with the leftmost unsorted element. | Bubble Sort compares adjacent elements and swaps them if they are in wrong order. |
Performs lesser number of swaps to sort the same array relative to Bubble Sort. | Performs more number of swaps to sort the array. |
Selection Sort is faster. | Bubble Sort is slower. |
length and length()
Answer
length | length() |
---|---|
length is an attribute i.e. a data member of array. | length() is a member method of String class. |
It gives the length of an array i.e. the number of elements stored in an array. | It gives the number of characters present in a string. |
Write a program in Java to store 20 numbers (even and odd numbers) in a Single Dimensional Array (SDA). Calculate and display the sum of all even numbers and all odd numbers separately.
import java.util.Scanner;
public class KboatSDAOddEvenSum
{
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 < arr.length; i++) {
arr[i] = in.nextInt();
}
int oddSum = 0, evenSum = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0)
evenSum += arr[i];
else
oddSum += arr[i];
}
System.out.println("Sum of Odd numbers = " + oddSum);
System.out.println("Sum of Even numbers = " + evenSum);
}
}
Write a program in Java to store 20 temperatures in °F in a Single Dimensional Array (SDA) and display all the temperatures after converting them into °C.
Hint: (c/5) = (f - 32) / 9
import java.util.Scanner;
public class KboatSDAF2C
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double arr[] = new double[20];
System.out.println("Enter 20 temperatures in degree Fahrenheit");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextDouble();
}
System.out.println("Temperatures in degree Celsius");
for (int i = 0; i < arr.length; i++) {
double tc = 5 * ((arr[i] - 32) / 9);
System.out.println(tc);
}
}
}
Write a program in Java to store 10 numbers (including positive and negative numbers) in a Single Dimensional Array (SDA). Display all the negative numbers followed by the positive numbers without changing the order of the numbers.
Sample Input:
n[0] | n[1] | n[2] | n[3] | n[4] | n[5] | n[6] | n[7] | n[8] | n[9] |
---|---|---|---|---|---|---|---|---|---|
15 | 21 | -32 | -41 | 54 | 61 | 71 | -19 | -44 | 52 |
Sample Output: -32, -41, -19, 44, 15, 21, 54, 61, 71, 52
import java.util.Scanner;
public class KboatSDANumbers
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[10];
System.out.println("Enter 10 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] < 0)
System.out.print(arr[i] + ", ");
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= 0)
System.out.print(arr[i] + ", ");
}
}
}
Write a program in Java to store 20 numbers in a Single Dimensional Array (SDA). Display the numbers which are prime.
Sample Input:
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 |
Sample Output: 71, 67, 19, 31
import java.util.Scanner;
public class KboatSDAPrimeNumbers
{
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 < 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] + ", ");
}
}
}
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:
(a) The average of the total marks obtained by N number of students.
[average = (sum of total marks of all the students)/N]
(b) Deviation of each student's total marks with the average.
[deviation = total marks of a student - average]
import java.util.Scanner;
public class KboatSDAMarks
{
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));
}
}
}
Write a program in Java using arrays:
(a) To store the Roll No., Name and marks in six subjects for 100 students.
(b) Calculate the percentage of marks obtained by each candidate. The maximum marks in each subject are 100.
(c) Calculate the Grade as per the given criteria:
Percentage Marks | Grade |
---|---|
From 80 to 100 | A |
From 60 to 79 | B |
From 40 to 59 | C |
Less than 40 | D |
import java.util.Scanner;
public class KboatSDAGrades
{
public static void main(String args[]) {
final int TOTAL_STUDENTS = 100;
Scanner in = new Scanner(System.in);
int rollNo[] = new int[TOTAL_STUDENTS];
String name[] = new String[TOTAL_STUDENTS];
int s1[] = new int[TOTAL_STUDENTS];
int s2[] = new int[TOTAL_STUDENTS];
int s3[] = new int[TOTAL_STUDENTS];
int s4[] = new int[TOTAL_STUDENTS];
int s5[] = new int[TOTAL_STUDENTS];
int s6[] = new int[TOTAL_STUDENTS];
double p[] = new double[TOTAL_STUDENTS];
char g[] = new char[TOTAL_STUDENTS];
for (int i = 0; i < TOTAL_STUDENTS; i++) {
System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
in.nextLine();
System.out.print("Name: ");
name[i] = in.nextLine();
System.out.print("Subject 1 Marks: ");
s1[i] = in.nextInt();
System.out.print("Subject 2 Marks: ");
s2[i] = in.nextInt();
System.out.print("Subject 3 Marks: ");
s3[i] = in.nextInt();
System.out.print("Subject 4 Marks: ");
s4[i] = in.nextInt();
System.out.print("Subject 5 Marks: ");
s5[i] = in.nextInt();
System.out.print("Subject 6 Marks: ");
s6[i] = in.nextInt();
p[i] = (((s1[i] + s2[i] + s3[i] + s4[i]
+ s5[i] + s6[i]) / 600.0) * 100);
if (p[i] < 40)
g[i] = 'D';
else if (p[i] < 60)
g[i] = 'C';
else if (p[i] < 80)
g[i] = 'B';
else
g[i] = 'A';
}
System.out.println();
for (int i = 0; i < TOTAL_STUDENTS; i++) {
System.out.println(rollNo[i] + "\t"
+ name[i] + "\t"
+ p[i] + "\t"
+ g[i]);
}
}
}
Write a program to accept a list of 20 integers. Sort the first 10 numbers in ascending order and next the 10 numbers in descending order by using 'Bubble Sort' technique. Finally, print the complete list of integers.
import java.util.Scanner;
public class KboatSDABubbleSort
{
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 < arr.length; i++) {
arr[i] = in.nextInt();
}
//Sort first half in ascending order
for (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = 0; j < arr.length / 2 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}
//Sort second half in descending order
for (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = arr.length / 2; j < arr.length - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}
//Print the final sorted array
System.out.println("\nSorted Array:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
Write a program in Java to accept 20 numbers in a single dimensional array arr[20]. Transfer and store all the even numbers in an array even[ ] and all the odd numbers in another array odd[ ]. Finally, print the elements of both the arrays.
import java.util.Scanner;
public class KboatSDAEvenOdd
{
public static void main(String args[]) {
final int NUM_COUNT = 20;
Scanner in = new Scanner(System.in);
int i = 0;
int arr[] = new int[NUM_COUNT];
int even[] = new int[NUM_COUNT];
int odd[] = new int[NUM_COUNT];
System.out.println("Enter 20 numbers:");
for (i = 0; i < NUM_COUNT; i++) {
arr[i] = in.nextInt();
}
int eIdx = 0, oIdx = 0;
for (i = 0; i < NUM_COUNT; i++) {
if (arr[i] % 2 == 0)
even[eIdx++] = arr[i];
else
odd[oIdx++] = arr[i];
}
System.out.println("Even Numbers:");
for (i = 0; i < eIdx; i++) {
System.out.print(even[i] + " ");
}
System.out.println("\nOdd Numbers:");
for (i = 0; i < oIdx; i++) {
System.out.print(odd[i] + " ");
}
}
}
Write a program to store 20 numbers in a Single Dimensional Array (SDA). Now, display only those numbers that are perfect squares.
n[0] | n[1] | n[2] | n[3] | n[4] | n[5] | ... | n[16] | n[17] | n[18] | n[19] |
---|---|---|---|---|---|---|---|---|---|---|
12 | 45 | 49 | 78 | 64 | 77 | ... | 81 | 99 | 45 | 33 |
Sample Output: 49, 64, 81
import java.util.Scanner;
public class KboatSDASquares
{
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 < arr.length; i++) {
arr[i] = in.nextInt();
}
System.out.println("Perfect Squares are:");
for (int i = 0; i < arr.length; i++) {
double sr = Math.sqrt(arr[i]);
if ((sr - Math.floor(sr)) == 0)
System.out.print(arr[i] + ", ");
}
}
}
To get promotion in a Science stream, a student must pass in English and should pass in any of the two subjects (i.e.; Physics, Chemistry or Maths). The passing mark in each subject is 35. Write a program in a Single Dimension Array to accept the roll numbers and marks secured in the subjects for all the students. The program should check and display the roll numbers along with a message whether "Promotion is Granted" or "Promotion is not Granted". Assume that there are 40 students in the class.
import java.util.Scanner;
public class KboatStudent
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int studentDetails[] = new int[200]; //40 * 5 = 200
System.out.println("Enter student details");
for (int i = 0, idx = 1; i < 200; i = i + 5, idx++) {
System.out.print("Student " + idx + " roll number: ");
studentDetails[i] = in.nextInt();
System.out.print("Student " + idx + " English Marks: ");
studentDetails[i+1] = in.nextInt();
System.out.print("Student " + idx + " Maths Marks: ");
studentDetails[i+2] = in.nextInt();
System.out.print("Student " + idx + " Physics Marks: ");
studentDetails[i+3] = in.nextInt();
System.out.print("Student " + idx + " Chemistry Marks: ");
studentDetails[i+4] = in.nextInt();
}
for (int i = 0; i < 200; i = i + 5) {
System.out.println("Roll No: " + studentDetails[i]);
if (studentDetails[i+1] > 34 &&
((studentDetails[i+2] > 34 && studentDetails[i+3] > 34) ||
(studentDetails[i+2] > 34 && studentDetails[i+4] > 34) ||
(studentDetails[i+3] > 34 && studentDetails[i+4] > 34))) {
System.out.println("Promotion is granted.");
}
else {
System.out.println("Promotion is not granted.");
}
}
}
}
The annual examination result of 50 students in a class is tabulated in a Single Dimensional Array (SDA) is as follows:
Roll No. | Subject A | Subject B | Subject C |
---|---|---|---|
....... | ....... | ....... | ....... |
....... | ....... | ....... | ....... |
....... | ....... | ....... | ....... |
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;
public class KboatExamResult
{
public static void main(String args[]) {
final int TOTAL_STUDENTS = 50;
Scanner in = new Scanner(System.in);
int rollNo[] = new int[TOTAL_STUDENTS];
int sA[] = new int[TOTAL_STUDENTS];
int sB[] = new int[TOTAL_STUDENTS];
int sC[] = new int[TOTAL_STUDENTS];
double avg[] = new double[TOTAL_STUDENTS];
for (int i = 0; i < TOTAL_STUDENTS; i++) {
System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
System.out.print("Subject A Marks: ");
sA[i] = in.nextInt();
System.out.print("Subject B Marks: ");
sB[i] = in.nextInt();
System.out.print("Subject C Marks: ");
sC[i] = in.nextInt();
avg[i] = (sA[i] + sB[i] + sC[i]) / 3.0;
}
System.out.println("\nRoll No\tAverage Marks");
for (int i = 0; i < TOTAL_STUDENTS; i++) {
System.out.println(rollNo[i] + "\t" + avg[i]);
}
System.out.println("\nStudents with Average above 80:");
for (int i = 0; i < TOTAL_STUDENTS; i++) {
if (avg[i] > 80)
System.out.println(rollNo[i] + "\t" + avg[i]);
}
System.out.println("\nStudents with Average below 40:");
for (int i = 0; i < TOTAL_STUDENTS; i++) {
if (avg[i] < 40)
System.out.println(rollNo[i] + "\t" + avg[i]);
}
}
}
Write a program to store 6 elements in an array P and 4 elements in an array Q. Now, produce a third array R, containing all the elements of array P and Q. Display the resultant array.
Input | Input | Output |
---|---|---|
P[ ] | Q[ ] | R[ ] |
4 | 19 | 4 |
6 | 23 | 6 |
1 | 7 | 1 |
2 | 8 | 2 |
3 | 3 | |
10 | 10 | |
19 | ||
23 | ||
7 | ||
8 |
import java.util.Scanner;
public class Kboat3Arrays
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int P[] = new int[6];
int Q[] = new int[4];
int R[] = new int[10];
int i = 0;
System.out.println("Enter 6 elements of array P:");
for (i = 0; i < P.length; i++) {
P[i] = in.nextInt();
}
System.out.println("Enter 4 elements of array Q:");
for (i = 0; i < Q.length; i++) {
Q[i] = in.nextInt();
}
i = 0;
while(i < P.length) {
R[i] = P[i];
i++;
}
int j = 0;
while(j < Q.length) {
R[i++] = Q[j++];
}
System.out.println("Elements of Array R:");
for (i = 0; i < R.length; i++) {
System.out.print(R[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".
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 |
import java.util.Scanner;
public class KboatGraduationYear
{
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");
}
}
Write a program to input and store 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:
Average Marks | Remark |
---|---|
85 - 100 | Excellent |
75 - 84 | Distinction |
60 - 74 | First Class |
40 - 59 | Pass |
Less than 40 | Poor |
import java.util.Scanner;
public class KboatAvgMarks
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = in.nextInt();
int rollNo[] = new int[n];
String name[] = new String[n];
int s1[] = new int[n];
int s2[] = new int[n];
int s3[] = new int[n];
double avg[] = new double[n];
for (int i = 0; i < n; i++) {
System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
in.nextLine();
System.out.print("Name: ");
name[i] = in.nextLine();
System.out.print("Subject 1 Marks: ");
s1[i] = in.nextInt();
System.out.print("Subject 2 Marks: ");
s2[i] = in.nextInt();
System.out.print("Subject 3 Marks: ");
s3[i] = in.nextInt();
avg[i] = (s1[i] + s2[i] + s3[i]) / 3.0;
}
System.out.println("Roll No\tName\tRemark");
for (int i = 0; i < n; i++) {
String remark;
if (avg[i] < 40)
remark = "Poor";
else if (avg[i] < 60)
remark = "Pass";
else if (avg[i] < 75)
remark = "First Class";
else if (avg[i] < 85)
remark = "Distinction";
else
remark = "Excellent";
System.out.println(rollNo[i] + "\t"
+ name[i] + "\t"
+ remark);
}
}
}
A double dimensional array is defined as N[4][4] to store numbers. Write a program to find the sum of all even numbers and product of all odd numbers of the elements stored in Double Dimensional Array (DDA).
Sample Input:
12 10 15 17
30 11 32 71
17 14 29 31
41 33 40 51
Sample Output:
Sum of all even numbers: ..........
Product of all odd numbers: ..........
import java.util.Scanner;
public class KboatDDAEvenOdd
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N[][] = new int[4][4];
long evenSum = 0, oddProd = 1;
System.out.println("Enter the elements of 4x4 DDA: ");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
N[i][j] = in.nextInt();
if (N[i][j] % 2 == 0)
evenSum += N[i][j];
else
oddProd *= N[i][j];
}
}
System.out.println("Sum of all even numbers = " + evenSum);
System.out.println("Product of all odd numbers = " + oddProd);
}
}
A Departmental Shop has 5 stores and 6 departments. The monthly sale of the department is kept in the Double Dimensional Array (DDA) as m[5][6].
The Manager of the shop wants to know the total monthly sale of each store and each department at any time. Write a program to perform the given task.
(Hint: Number of stores as rows and Number of departments as columns.)
import java.util.Scanner;
public class KboatDepartmentalStore
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
long m[][] = new long[5][6];
for (int i = 0; i < 5; i++) {
System.out.println("Store " + (i + 1) + " Sales");
for (int j = 0; j < 6; j++) {
System.out.print("Enter monthly sale of department " +
(j + 1) + ": ");
m[i][j] = in.nextInt();
}
}
System.out.println("\nMonthly Sale by store: ");
for (int i = 0; i < 5; i++) {
long storeSale = 0;
for (int j = 0; j < 6; j++) {
storeSale += m[i][j];
}
System.out.println("Store " + (i + 1)
+ " Sales: " + storeSale);
}
System.out.println("\nMonthly Sale by Department: ");
for (int i = 0; i < 6; i++) {
long deptSale = 0;
for (int j = 0; j < 5; j++) {
deptSale += m[j][i];
}
System.out.println("Department " + (i + 1)
+ " Sales: " + deptSale);
}
}
}
A Metropolitan Hotel has 5 floors and 10 rooms in each floor. The names of the visitors are entered in a Double Dimensional Array (DDA) as M[5][10].The Hotel Manager wants to know from the "Enquiry" about the position of a visitor (i.e. floor number and room number) as soon as he enters the name of the visitor. Write a program in Java to perform the above task.
import java.util.Scanner;
public class KboatHotelEnquiry
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String M[][] = new String[5][10];
int i = 0, j = 0;
for (i = 0; i < 5; i++) {
System.out.println("Enter floor " + (i + 1)
+ " guest details:");
for (j = 0; j < 10; j++) {
System.out.print("Guest in room " +
(j + 1) + ": ");
M[i][j] = in.nextLine();
}
}
boolean found = false;
System.out.print("\nEnter guest name to search: ");
String guest = in.nextLine();
for (i = 0; i < 5; i++) {
for (j = 0; j < 10; j++) {
if (M[i][j].equals(guest)) {
found = true;
break;
}
}
if (found)
break;
}
if (found)
System.out.println(guest + " is in room number "
+ (j + 1) + " on floor number " + (i + 1));
else
System.out.println(guest +
" is not staying at this hotel");
}
}
A Class Teacher wants to keep the records of 40 students of her class along with their names and marks obtained in English, Hindi, Maths, Science and Computer Science in a Double Dimensional Array (DDA) as M[40][5].
When the teacher enters the name of a student as an input, the program must display the name, marks obtained in the 5 subjects and the total. Write a program in Java to perform the task.
import java.util.Scanner;
public class KboatDDAStudentRecord
{
public static void main(String args[]) {
final int TOTAL_STUDENTS = 40;
final int TOTAL_SUBJECTS = 5;
final String SUBJECT_NAMES[] = {"English", "Hindi",
"Maths", "Science", "Computer Science"};
Scanner in = new Scanner(System.in);
String names[] = new String[TOTAL_STUDENTS];
int marks[][] = new int[TOTAL_STUDENTS][TOTAL_SUBJECTS];
int i = 0, j = 0;
for (i = 0; i < TOTAL_STUDENTS; i++) {
System.out.println("Student " + (i + 1) + " details:");
System.out.print("Name: ");
names[i] = in.nextLine();
for (j = 0; j < TOTAL_SUBJECTS; j++) {
System.out.print("Enter marks in " +
SUBJECT_NAMES[j] + ": ");
marks[i][j] = in.nextInt();
}
in.nextLine();
}
System.out.print("\nEnter name of the student to search: ");
String studentName = in.nextLine();
for (i = 0; i < TOTAL_STUDENTS; i++) {
if (names[i].equals(studentName))
break;
}
if (i == TOTAL_STUDENTS) {
System.out.println("Record for " + studentName
+ " not found");
}
else {
System.out.println("Name: " + names[i]);
int total = 0;
for (j = 0; j < TOTAL_SUBJECTS; j++) {
System.out.println("Marks in " +
SUBJECT_NAMES[j] + ": " + marks[i][j]);
total += marks[i][j];
}
System.out.println("Total Marks: " + total);
}
}
}
If arrays M and M + N are as shown below, write a program in Java to find the array N.
M = {{-1, 0, 2}, M + N = {{-6, 9, 4},
{-3, -1, 6}, {4, 5, 0},
{4, 3, -1}} {1, -2, -3}}
public class KboatSubtractDDA
{
public static void main(String args[]) {
int arrM[][] = {
{-1, 0, 2},
{-3, -1, 6},
{4, 3, -1}
};
int arrSum[][] = {
{-6, 9, 4},
{4, 5, 0},
{1, -2, -3}
};
int arrN[][] = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
arrN[i][j] = arrSum[i][j] - arrM[i][j];
}
}
System.out.println("Array N:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(arrN[i][j]);
System.out.print(' ');
}
System.out.println();
}
}
}