Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.util.Scanner;
- public class Main {
- public static void ReadFromFile(int[][] Matrixx, String FName, int Row, int Col) throws
- Exception {
- String line;
- String[] Numb;
- BufferedReader buff = new BufferedReader(new FileReader(FName));
- for (int i = 0; i <= Row; i++) {
- line = buff.readLine();
- Numb = line.split(" ");
- for (int j = 0; j <= Col; j++) {
- Matrixx[i][j] = Integer.parseInt(Numb[j]);
- }
- }
- buff.close();
- }
- public static int SizeOfMatrix(String fileName) throws Exception {
- Boolean isFirst = true, isCorrect = true;
- int columnCount = 0, rowCount = 1;
- String line;
- String[] stringRow;
- BufferedReader reader = new BufferedReader(new FileReader(fileName));
- line = reader.readLine();
- while (line != null && isCorrect) {
- stringRow = line.split(" ");
- rowCount++;
- if (isFirst) {
- columnCount = stringRow.length;
- isFirst = false;
- }
- if (!isFirst && stringRow.length != columnCount) {
- isCorrect = false;
- }
- line = reader.readLine();
- }
- if (isCorrect && columnCount == rowCount) {
- return columnCount;
- }
- return 0;
- }
- public static int MatrixRowSize(String FName) throws Exception {
- int count = 0;
- FileReader MyFile = new FileReader(FName);
- BufferedReader buff = new BufferedReader(MyFile);
- while (buff.readLine() != null) {
- count++;
- }
- MyFile.close();
- return (count);
- }
- public static int MatrixColSize(String FName) throws Exception {
- String line;
- String[] Numb;
- int count = 0;
- FileReader MyFile = new FileReader(FName);
- BufferedReader buff = new BufferedReader(MyFile);
- line = buff.readLine();
- Numb = line.split(" ");
- for (int i = 0; i < Numb.length; i++)
- count++;
- MyFile.close();
- return (count);
- }
- public static void WriteToFile(float Arr[], String FName, int Row, int Col) throws Exception {
- FileWriter FileOut = new FileWriter(FName);
- for (int i = 0; i <= Row; i++) {
- FileOut.write(Arr[i] + " ");
- }
- FileOut.write("\n");
- FileOut.close();
- System.out.println("Saved to file: " + FName);
- }
- public static float[] Average(int[][] Matrixx, int Col, int Row) {
- float Sum, Amount;
- float[] Arr = new float[Col];
- for (int i = 0; i < Row; i++) {
- Sum = 0;
- Amount = 0;
- for (int j = 0; j < Col; j++) {
- if (Matrixx[j][i] > 0) {
- Sum += Matrixx[j][i];
- Amount += 1;
- }
- Arr[i] = Sum / Amount;
- }
- }
- return Arr;
- }
- public static void AverageOutput(int[] Arr) {
- float avg;
- for (int i = 0; i < Arr.length; i++){
- System.out.printf("%.4f ", Arr[i]);
- }
- }
- public static void MatrixOutput(int[][] Matrixx, int Row, int Col){
- for (int i = 0; i <= Row; i++) {
- for (int j = 0; j <= Col; j++) {
- System.out.printf("%d ", Matrixx[i][j]);
- }
- System.out.println();
- }
- System.out.println();
- }
- public static void main (String[]args) throws Exception {
- int[][] Matrixx;
- float[] Arr;
- int Rows, Cols;
- String FileNameIn, FileNameOut;
- Rows = MatrixRowSize(FileNameIn);
- Cols = MatrixColSize(FileNameIn);
- Matrixx = new int[Rows][Cols];
- Rows--;
- Cols--;
- ReadFromFile(Matrixx, FileNameIn, Rows, Cols);
- System.out.println();
- System.out.println("Given matrix::");
- MatrixOutput(Matrixx, Rows, Cols);
- Arr = Average;
- System.out.println("The result after addition:");
- WriteToFile(Matrixx, FileNameOut, Rows, Cols);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment