First create a file which has 25 integers in one row. The integers are separated
ID: 3887922 • Letter: F
Question
First create a file which has 25 integers in one row. The integers are separated by spaces. The name of your data file should be “data.txt”. Then write a program to finish the following steps: 1) Declare a 5-by-5 array (a matrix with 5 rows and 5 columns) 2) Read data from data.txt and use them to fill the matrix created in the previous step. 3) Count the number of even integers in the matrix. 4) Calculate the sum of all integers in the columns with an even index value. Please note the column index begins with zero. 5) Calculate the number of integers which are smaller than 20. 6) Transpose the matrix - https://en.wikipedia.org/wiki/Transpose (You can find the definition of transpose of a matrix at the link.) Please make the transpose in the original array instead of creating another array. 7) Print the array in a table format. For each step except for Step (1) , please write a separate loop for it. First create a file which has 25 integers in one row. The integers are separated by spaces. The name of your data file should be “data.txt”. Then write a program to finish the following steps: 1) Declare a 5-by-5 array (a matrix with 5 rows and 5 columns) 2) Read data from data.txt and use them to fill the matrix created in the previous step. 3) Count the number of even integers in the matrix. 4) Calculate the sum of all integers in the columns with an even index value. Please note the column index begins with zero. 5) Calculate the number of integers which are smaller than 20. 6) Transpose the matrix - https://en.wikipedia.org/wiki/Transpose (You can find the definition of transpose of a matrix at the link.) Please make the transpose in the original array instead of creating another array. 7) Print the array in a table format. For each step except for Step (1) , please write a separate loop for it.Explanation / Answer
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Demo {
public static void main() throws IOException {
InputStream in = new FileInputStream("data.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
{
out.append(line+" ");
}
String result = out.toString(); //extracting data from data.txt
String[] arr = result.split(" "); //converting to string array
int[][] intarr=new int[5][5]; // 1. Declare a 5-by-5 array
for(int i=0,m=0;i<5;i++) // 2. reading data and initializizng to int[5][5]
for(int j=0;j<5;j++) {
intarr[i][j] = Integer.parseInt(arr[m]); // conversion of string array to int[][] array
m++;
}
System.out.println("Array elements:"); // print array elements in matrix form
for(int i = 0 ; i < 5 ; i++)
{
for(int j=0;j<5;j++){
System.out.println(intarr[i][j]+" ");
}
System.out.println(" ");
}
int count=0;
for(int i = 0 ; i < 5 ; i++)
for(int j=0;j<5;j++)
if(intarr[i][j] % 2 == 0) // 3. counting number of even integers in matrix
{
count++;
}
System.out.println("Number of Even Integers:"+count);
double temp[] = new double[5];
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
if(j%2==0)
temp[j] += intarr[i][j]; // 4. Calculate the sum of all integers in the columns with an even index value
}
}
System.out.println(Arrays.toString(temp));
count = 0;
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
if(intarr[i][j]<20)
count++; // 5. Calculate the number of integers which are smaller than 20
}
}
System.out.println("Number of Integers < 20:"+count);
int transpose[][] = new int[5][5];
for ( i = 0 ; i < 5 ; i++ ) // Transposing a matrix
{
for ( j = 0 ; j < 5 ; j++ )
transpose[j][i] = matrix[i][j];
}
System.out.println("Array elements of transpose matrix:"); // 7. print transpose array elements in matrix form
for(int i = 0 ; i < 5 ; i++)
{
for(int j=0;j<5;j++){
System.out.println(transpose[i][j]+" ");
}
System.out.println(" ");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.