Help with JAVA Programming! 1. Find the maximum value in a 2D array 3 3 4 5 7 12
ID: 3740113 • Letter: H
Question
Help with JAVA Programming!
1. Find the maximum value in a 2D array
3 3 4
5 7 12
3 2 6
Answer: 12
2. Given:
int[][] items =
{ {0, 1, 3, 4},
{4, 3, 99, 0, 7 },
{3, 2} } ;
Print all the numbers in the matrix
3. Find the average of all values in a 2D array
4. Write a program that reads students' quizzes scores from a static array. Find the average of all the student’s quiz scores
5. Write a program that reads students' quizzes scores from a user populated Dynamic array, drop the lowest quiz grade for the whole class and compute the new average. The user must first input the scores and then type -1 to let the program know that the user is done typing in scores.
6. Write a program that reads students' quizzes scores from a User populated arraylist. The user must first input the scores and then type -1 to let the program know that the user is done typing in scores. Find how many students there are and separate the students quiz scores into 2 arrays. Low scores and high scores, where half the class with the lower scores is put into low scores and high scores where the other half is put here.
Explanation / Answer
1.
import java.util.*;
class Max
{
public static void main (String[] args)
{
int arr[][] = {{3,3,4},{5,7,12},{3,2,6}};
int max = 0;// assign mimimum value to max
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(arr[i][j] > max)// if any element of array is greater tham max
max = arr[i][j];// assign that element to max
}
}
System.out.println("Maximum value in arr : "+max);
}
}
Output:
Maximum value in arr : 12
2.
class Print
{
public static void main (String[] args)
{
int[][] items = { {0,1,3,4},{4,3,99,0,7},{3,2} } ;
for(int i=0;i<items.length;i++)// number of rows
{
for(int j=0;j<items[i].length;j++)// number of columns
{
System.out.print(items[i][j]+" ");
}
System.out.println();
}
}
}
Output:
0 1 3 4
4 3 99 0 7
3 2
3.
import java.util.*;
class Average
{
public static void main (String[] args)
{
double sum =0;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of rows : ");
int rows = input.nextInt();
System.out.println("Enter the number of columns : ");
int cols = input.nextInt();
int[][] arr = new int[rows][cols];
//input elements of 2D array
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
arr[i][j] = input.nextInt();
}
}
//sum of all elements of array
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
sum = sum +arr[i][j];
}
}
System.out.println("Average of all elements of array : "+sum/(rows*cols));
}
}
Output:
Enter the number of rows :2
Enter the number of columns :3
2 4
5 6
8 1
Average of all elements of array : 4.333333333333333
4.
class QuizScoreAverage
{
public static void main (String[] args)
{
double sum =0;
double[] quizScore = {5.6,7.8,4.8,6,4,4.9};//static array
//sum of all Scores of array
for(int i=0;i<quizScore.length;i++)
sum = sum +quizScore[i];
System.out.printf("Average score : %.2f",sum/quizScore.length);
}
}
Output:
Average score : 5.52
5.
import java.util.*;
class QuizScoreAverage
{
public static void main (String[] args)
{
double sum =0;
Scanner input = new Scanner(System.in);
System.out.println("Enter the scores of students<1-10> -1 to end: ");
double[] quizScore = new double[20];
int i =0;
do
{
quizScore[i] = input.nextDouble();
if(quizScore[i] == -1)
break;
i++;
}while(quizScore[i] != -1);
int n = i;
double min = 10;
for( i=0;i<n;i++)
{
if(quizScore[i] < min)
min = quizScore[i];
}
//sum of all Scores of array after removing min
for( i=0;i<quizScore.length;i++)
sum = sum +quizScore[i];
sum = sum -min;
System.out.printf("Average score after removing min : %.2f",sum/(n-1));
}
}
Output:
Enter the scores of students<1-10> -1 to end:
5.6
7.8
9.1
4.9
6.6
-1
Average score after removing min : 7.03
6.
import java.util.*;
class QuizScoreAverage
{
public static void main (String[] args)
{
double score,sum =0;
Scanner input = new Scanner(System.in);
//3 arraylists of all quiz scores,low scores and high scores
ArrayList<Double> quizScore = new ArrayList<Double>();
ArrayList<Double> lowScore = new ArrayList<Double>();
ArrayList<Double>highScore = new ArrayList<Double>();
System.out.println("Enter the scores of students<1-10> -1 to end: ");
int i =0;
do
{
score = input.nextDouble();
if(score == -1)
break;
quizScore.add(score);
i++;
}while(score != -1);
System.out.println("Number of students : "+i);
double avg = 0;
//compute average acore
for(double scores: quizScore)
{
avg = avg+ scores;
}
avg = avg/quizScore.size();
//make two separate lists of higher and lower scores
for(double scores: quizScore)
{
if(scores > avg)
highScore.add(scores);
else
lowScore.add(scores);
}
//print higher and lower scores lists
System.out.println("Students with higher scores : ");
for(double scores: highScore)
{
System.out.println(scores+ " ");
}
System.out.println("Students with lower scores : ");
for(double scores: lowScore)
{
System.out.println(scores+ " ");
}
}
}
output:
Enter the scores of students<1-10> -1 to end:
5.6
7.8
9.1
7.9
6.6
5.8
-1
Number of students : 6
Students with higher scores :
7.8
9.1
7.9
Students with lower scores :
5.6
6.6
5.8
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.