0% found this document useful (0 votes)
1 views7 pages

Computer Applications Array Programs

The document contains multiple Java programming problems involving arrays, including calculating average marks for students, merging two arrays, performing binary search, and handling double dimensional arrays for sales data. Each problem is accompanied by a Java code solution that demonstrates how to implement the required functionality. The problems cover a range of topics such as single and double dimensional arrays, user input, and basic arithmetic operations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views7 pages

Computer Applications Array Programs

The document contains multiple Java programming problems involving arrays, including calculating average marks for students, merging two arrays, performing binary search, and handling double dimensional arrays for sales data. Each problem is accompanied by a Java code solution that demonstrates how to implement the required functionality. The problems cover a range of topics such as single and double dimensional arrays, user input, and basic arithmetic operations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Page 114 - Single Dimensional Array (SDA) Problem

Q.11 The annual examination result of 50 students in a class is tabulated in a Single


Dimensional Array (SDA) as follows:

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 [Link].*;
public class Exam {
public static void main(String args[]) {
int i, ns = 50;
Scanner in = new Scanner([Link]);
int rollNo[] = new int[ns];
int sub1[] = new int[ns];
int sub2[] = new int[ns];
int sub3[] = new int[ns];
double avg[] = new double[ns];
for (i = 0; i < ns; i++) {
[Link]("Enter student " + (i+1) + " details:");
[Link]("Roll No: ");
rollNo[i] = [Link]();
[Link]("Subject A Marks: ");
sub1[i] = [Link]();
[Link]("Subject B Marks: ");
sub2[i] = [Link]();
[Link]("Subject C Marks: ");
sub3[i] = [Link]();
avg[i] = (sub1[i] + sub2[i] + sub3[i])/3.0;
}
[Link]("\nRoll No\tAverage Marks");
for (i = 0; i < ns; i++) {
[Link](rollNo[i] + "\t" + avg[i]);
}
[Link]("\nStudents with average above 80:");
for (i = 0; i < ns; i++) {
if (avg[i] > 80)
[Link](rollNo[i] + "\t" + avg[i]);
}
[Link]("\nStudents with average below 40:");
for (i = 0; i < ns; i++) {
if (avg[i] < 40)
[Link](rollNo[i] + "\t" + avg[i]);
}
}
}
Page 115 - Array Merge Problem
Q.12 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 arrays P and Q. Display the resultant
array.

import [Link].*;
public class Arrays {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int P[] = new int[6];
int Q[] = new int[4];
int R[] = new int[10];
int i, j;
[Link]("Enter 6 elements of array P:");
for (i = 0; i < [Link]; i++) {
P[i] = [Link]();
}
[Link]("Enter 4 elements of array Q:");
for (i = 0; i < [Link]; i++) {
Q[i] = [Link]();
}
i = 0;
while(i < [Link]) {
R[i] = P[i];
i++;
}
j = 0;
while(j < [Link]) {
R[i++] = Q[j++];
}
[Link]("Elements of Array R:");
for (i = 0; i < [Link]; i++) {
[Link](R[i] + " ");
}
}
}
Page 117 - Binary Search and Student Remarks
Q.13 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" and the position, if the input value is located in the
array. If not, output the message "Record does not exist".

import [Link].*;
public class Year {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int n[] = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};
[Link]("Enter graduation year to search: ");
int year = [Link]();
int low = 0, high = [Link] - 1, idx = -1;
while (low <= high) {
int m = (low + high)/2;
if (n[m] == year) {
idx = m;
break;
} else if (n[m] < year) {
low = m + 1;
} else {
high = m - 1;
}
}
if (idx == -1)
[Link]("Record does not exist");
else
[Link]("Record exists at position: " + (idx + 1));
}
}

Q.14 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 [Link].*;
public class AvgMarks {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int n, i;
[Link]("Enter number of students: ");
n = [Link]();
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 (i = 0; i < n; i++) {
[Link]("Enter student " + (i+1) + " details:");
[Link]("Roll No: ");
rollNo[i] = [Link]();
[Link]();
[Link]("Name: ");
name[i] = [Link]();
[Link]("Subject 1 Marks: ");
s1[i] = [Link]();
[Link]("Subject 2 Marks: ");
s2[i] = [Link]();
[Link]("Subject 3 Marks: ");
s3[i] = [Link]();
avg[i] = (s1[i] + s2[i] + s3[i])/3.0;
}
[Link]("Roll No\tName\tRemark");
for (i = 0; i < n; i++) {
String rm;
if (avg[i] < 40) rm = "Poor";
else if (avg[i] < 60) rm = "Pass";
else if (avg[i] < 75) rm = "First Class";
else if (avg[i] < 85) rm = "Distinction";
else rm = "Excellent";
[Link](rollNo[i] + "\t" + name[i] + "\t" + rm);
}
}
}
Page 119 - Double Dimensional Array (DDA)
Q.15 A double dimensional array is defined as dd[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).

import [Link].*;
public class DDAEvenOdd {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int i, j;
int dd[][] = new int[4][4];
int evenSum = 0;
long oddProd = 1;
[Link]("Enter the elements of 4x4 DDA: ");
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
dd[i][j] = [Link]();
if (dd[i][j] % 2 == 0)
evenSum += dd[i][j];
else
oddProd *= dd[i][j];
}
}
[Link]("Sum of all even numbers = " + evenSum);
[Link]("Product of all odd numbers = " + oddProd);
}
}

Q.16 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 [Link].*;
public class Store {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
long m[][] = new long[5][6];
int i, j;
for (i = 0; i < 5; i++) {
[Link]("Store " + (i + 1) + " Sales");
for (j = 0; j < 6; j++) {
[Link]("Enter monthly sale of department " + (j + 1) + ": ");
m[i][j] = [Link]();
}
}
[Link]("\nMonthly Sale by store: ");
for (i = 0; i < 5; i++) {
long storeSale = 0;
for (j = 0; j < 6; j++) {
storeSale += m[i][j];
}
[Link]("Store " + (i + 1) + " Sales: " + storeSale);
}
[Link]("\nMonthly Sale by Department: ");
for (i = 0; i < 6; i++) {
long deptSale = 0;
for (j = 0; j < 5; j++) {
deptSale += m[j][i];
}
[Link]("Department " + (i + 1) + " Sales: " + deptSale);
}
}
}

You might also like