0% found this document useful (0 votes)
23 views3 pages

Dandu 2 DArray

The document provides instructions for a Java programming assignment to: 1) Declare and populate a 4x3 two-dimensional array from data in a text file 2) Print the array contents in a table 3) Calculate and print the average of array values 4) Print values greater than the average The Java code solution declares the array, populates it from a file, prints the array and average, and prints values above the average.
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)
23 views3 pages

Dandu 2 DArray

The document provides instructions for a Java programming assignment to: 1) Declare and populate a 4x3 two-dimensional array from data in a text file 2) Print the array contents in a table 3) Calculate and print the average of array values 4) Print values greater than the average The Java code solution declares the array, populates it from a file, prints the array and average, and prints values above the average.
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
You are on page 1/ 3

44-241 Computer Programming II

Worksheet: TwoDArray

Copy and paste your code solution under the problem below and highlight your code.
Save this word document with yourLastName2DArray.docx (Ex: Smith2DArray.docx).

1) Write a complete Java program to do the following:


a) Declare an array named numArray2D as a two-dimensional array of int values
with four rows and three columns.
b) Fill the array with data read from the text file 2DArrayData.txt. The first three
items in the file will be used to fill the first row of the array, the second three for
the second row, the next three for the third row, and the last three for the fourth
row.
c) Print the contents of the array in table format with four rows and three columns.
d) Calculate the average value of the numbers stored in the array.
e) Print out all values in the array that are greater than the average.

Output of program:
25 10 14
4 5 3
9 8 15
17 12 20

average = 11.833333333333334
Array values greater than average:
25 14 15 17 12 20

Contents of 2DArrayData.txt
25
10
14
4
5
3
9
8
15
17
12
20

import java.io.*;
import java.util.*;
Two-Dimensional Arrays Page 2 of 3

public class JavaApplication35 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
// TODO code application logic here
Scanner myFile = new Scanner(new
File("2DArrayData.txt"));

int[][] numArray2D = new int[4][3];


for (int row = 0; row < 4; row++){
for (int col = 0; col < 3; col++){
numArray2D[row][col] = myFile.nextInt();
}
}
for (int row = 0; row < 4; row++){
for (int col = 0; col < 3; col++){
System.out.print(numArray2D[row][col] + "
");
}
}
int sum = 0;
int count = 0;
int rows = 0;
int coloums = 0;
System.out.println();
count = rows * coloums;
for (int row = 0; row < rows; row++){
for (int col = 0; col < coloums; col++){
sum += numArray2D[row][col];
}
}
int average;
average = sum/count;
System.out.println("average = " + average);
System.out.println("Array values greater than
average:");
for (int row = 0; row < rows; row++){
for (int col = 0; col < coloums; col++){
if(numArray2D[row][col] > average){
System.out.print(numArray2D[row][col]);
}
}
}
System.out.println();

2
Two-Dimensional Arrays Page 3 of 3

}
}

You might also like