Before doing exercise must watch all above recorded video.
Question 1
int m[] = {2,4,6,8};
System.out.println(m[1] + " " + m[2]);
Output
4 6
Question 2
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);
Output
Sum = 27
Question 3
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]);
Output
12
Question 1
What is meant by Dimensional Array?
A dimensional array is a structure created in the memory to represent a number of values of the same data type with the variables having the same variable name along with different subscripts.
Question 2
Name the two types of Dimensional Array.
Single Dimensional Array
Double Dimensional Array
Question 3
What is the need of Dimensional Array? Explain.
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.
Question 4
'Array is a composite data type'. Explain this statement.
The data type that represents a number of similar or different data under single declaration is called as composite data type. An array is a group or a collection of same type of variables. Hence, Array is a composite data type.
Question 5
Define the following with their constructs:
(a) Single Dimensional Array
A Single Dimensional Array contains one row and one or more columns. The syntax of declaring a Single Dimensional Array is:
<type> <array-variable>[] = new <type>[<size>];
OR
<type> [] <array-variable> = new <type>[<size>];
(b) Double Dimensional Array
Double Dimensional Array contains multiple rows and multiple columns. The syntax of declaring a Double Dimensional Array is:
<type> <array-variable>[][] = new <type>[<rows>][<columns>];
OR
<type> [][] <array-variable> = new <type>[<rows>][<columns>];
Question 1
Subscript and Subscripted variable
Subscript is the index of the element in the array whereas Subscripted variable is the name of the array when it is used with a subscript to access a single element of the array.
Question 2
char a[5] and int a[5]
char a[5] is an array of char data type that can hold 5 characters whereas int a[5] is an array of int data type that can hold 5 integer values.
Question 3
Ordinary variable and array variable
An ordinary variable can hold only one value whereas an array variable can refer to a group of values of the same data type by using a subscript.
Question 4
Sorting and Searching
Sorting
Sorting means to arrange the elements of the array in ascending or descending order.
Linear search and Binary search are examples of search techniques.
Searching
Searching means to search for a term or value in an array.
Bubble sort and Selection sort are examples of sorting techniques.
Question 5
Linear search and Binary search
Linear Search
Linear search works on sorted and unsorted arrays
Each element of the array is checked against the target value until the element is found or end of the array is reached
Linear Search is slower
Binary Search
Binary search works on only sorted arrays (ascending or descending)
Array is successively divided into 2 halves and the target element is searched either in the first half or in the second half
Binary Search is faster
Question 6
Selection sort and Bubble sort
Selection sort
Selection Sort selects the smallest element from unsorted sub-array and swaps it with the leftmost unsorted element.
Performs lesser number of swaps to sort the same array relative to Bubble Sort
Selection Sort is faster
Bubble sort
Bubble Sort compares adjacent elements and swaps them if they are in wrong order.
Performs more number of swaps to sort the array
Bubble Sort is slower
Question 7
length and length()
length
length is an attribute i.e. a data member of array.
It gives the length of an array i.e. the number of elements stored in an array.
length()
length() is a member method of String class.
It gives the number of characters present in a string.
Question 1
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 OddEvenSum
{
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);
}
}
Question 2
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 manobalFC
{
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);
}
}
}
Question 3
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.
import java.util.Scanner;
public class Numbers
{
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] + ", ");
}
}
}
Question 4
import java.util.Scanner;
public class PrimeNumbers
{
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++) {
if (arr[i] == 1)
continue;
boolean isPrime = true;
for (int j = 2; j <= arr[i] / 2; j++) {
if (arr[i] % j == 0) {
isPrime = false;
break;
}
}
if (isPrime)
System.out.print(arr[i] + ", ");
}
}
}
Question 5
import java.util.Scanner;
public class Marks
{
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 6
import java.util.Scanner;
public class Marks
{
public static void main(String [] args) {
final int TOTAL_STUDENTS = 50;
Scanner in = new Scanner(System.in);
String name[] = new String[TOTAL_STUDENTS];
int marks[] = new int[TOTAL_STUDENTS];
int total = 0;
for (int i = 0; i < name.length; i++) {
System.out.print("Enter name of student " + (i+1) + ": ");
name[i] = in.nextLine();
System.out.print("Enter marks of student " + (i+1) + ": ");
marks[i] = in.nextInt();
total += marks[i];
in.nextLine();
}
double avg = (double)total / TOTAL_STUDENTS;
System.out.println("Subject Average Marks = " + avg);
int hIdx = 0;
for (int i = 1; i < marks.length; i++) {
if (marks[i] > marks[hIdx])
hIdx = i;
}
System.out.println("Highest Marks = " + marks[hIdx]);
System.out.println("Name = " + name[hIdx]);
}
}
Question 7
import java.util.Scanner;
public class Grades
{
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]);
}
}
}
Question 8
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 BubbleSort
{
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] + " ");
}
}
}
Question 9
The class teacher wants to store the marks obtained in English, Maths and Science of her class having 40 students. Write a program to input marks in Eng, Science and Maths by using three single dimensional arrays. Calculate and print the following information:
(i) Average marks secured by each student.
(ii) Class average in each subject.
[Hint: Class average is the average marks obtained by 40 students in a particular subject.]
import java.util.Scanner;
public class Marks
{
public static void main(String [] args) {
final int TOTAL_STUDENTS = 40;
Scanner in = new Scanner(System.in);
int english[] = new int[TOTAL_STUDENTS];
int maths[] = new int[TOTAL_STUDENTS];
int science[] = new int[TOTAL_STUDENTS];
double avgMarks[] = new double[TOTAL_STUDENTS];
for (int i = 0; i < TOTAL_STUDENTS; i++) {
System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Marks in English: ");
english[i] = in.nextInt();
System.out.print("Marks in Maths: ");
maths[i] = in.nextInt();
System.out.print("Marks in Science: ");
science[i] = in.nextInt();
avgMarks[i] = (english[i] + maths[i] + science[i]) / 3.0;
}
int engTotal = 0, mathsTotal = 0, sciTotal = 0;
for (int i = 0; i < TOTAL_STUDENTS; i++) {
System.out.println("Average marks of student " + (i+1) + " = " + avgMarks[i]);
engTotal += english[i];
mathsTotal += maths[i];
sciTotal += science[i];
}
System.out.println("Class Average in English = " + ((double)engTotal / TOTAL_STUDENTS));
System.out.println("Class Average in Maths = " + ((double)mathsTotal / TOTAL_STUDENTS));
System.out.println("Class Average in Science = " + ((double)sciTotal / TOTAL_STUDENTS));
}
}
Question 10
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 EvenOdd
{
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] + " ");
}
}
}
Question 11
import java.util.Scanner;
public class Squares
{
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] + ", ");
}
}
}
Question 12
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 Student
{
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.");
}
}
}
}
Question 13
Write a program to accept 10 different decimal numbers (double data type) in a Single Dimensional Array (say, A). Truncate the fractional part of each number of the array A and store their integer part in another array (say, B).
import java.util.Scanner;
public class Truncate
{
public static void main(String [] args){
Scanner in = new Scanner(System.in);
double a[] = new double[10];
int b[] = new int[10];
System.out.println("Enter 10 decimal numbers");
for (int i = 0; i < a.length; i++) {
a[i] = in.nextDouble();
b[i] = (int)a[i];
}
System.out.println("Truncated numbers");
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + ", ");
}
}
}
Question 14
import java.util.Scanner;
public class ExamResult
{
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]);
}
}
}
Question 15
import java.util.Scanner;
public class Arr
{
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] + " ");
}
}
}
Question 16
import java.util.Scanner;
public class GraduationYear
{
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");
}
}
Question 17
import java.util.Scanner;
public class AvgMarks
{
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);
}
}
}
Question 18
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 EvenOdd
{
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);
}
}
Question 19
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 DepartmentalStore
{
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);
}
}
}
Question 20
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 HotelEnquiry
{
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");
}
}
Question 21
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 s_record
{
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);
}
}
}
Question 22
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 Sub
{
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();
}
}
}