Skip to content

Instantly share code, notes, and snippets.

@JorgeOlvera
Last active August 29, 2015 14:00
Show Gist options
  • Save JorgeOlvera/11141527 to your computer and use it in GitHub Desktop.
Save JorgeOlvera/11141527 to your computer and use it in GitHub Desktop.
The App2
import java.util.*;
import java.io.*;
public class readFiles{
public static void main(String[] args) throws IOException{
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int[][] array = new int[a][b];
try {
Scanner file = new Scanner(new File("data.txt"));
for(int i = 0; i < array.length; i++){
for(int x = 0; x < array[0].length; x++){
array[i][x] = file.nextInt();
}
}
} catch (FileNotFoundException e) {
System.err.println("Some exception happened: " + e.getMessage());
}
maxValue(array);
minValue(array);
sum(array);
int[] anArray = newArray(array);
mostfreq(anArray);
leastfreq(anArray);
}
public static void sum(int[][] array){
int sum = 0;
for(int i = 0; i < array.length; i++){
for(int x = 0; x < array[0].length; x++){
sum = sum + array[i][x];
}
}
System.out.println(sum);
double average = sum/(array[0].length * array.length);
System.out.println(average);
}
public static void maxValue(int[][] array){
int max = array[0][0];
for(int i = 0; i < array.length; i++){
for(int x = 0; x < array[0].length; x++){
if(array[i][x] > max){
max = array[i][x];
}
}
}
System.out.println(max);
}
public static void minValue(int[][] array){
int min = array[0][0];
for(int i = 0; i < array.length; i++){
for(int x = 0; x < array[0].length; x++){
if(array[i][x] < min){
min = array[i][x];
}
}
}
System.out.println(min);
}
public static int[] newArray(int [][] array){
int size = array[0].length * array.length;
int[] anArray = new int[size];
int count = 0;
while(count < size){
for(int i = 0; i < array.length; i++){
for(int x = 0; x < array[0].length; x++){
anArray[count] = array[i][x];
count++;
}
}
}
boolean flag = true;
int temporal;
while (flag){
flag = false;
for (int j = 0; j < anArray.length - 1; j++) {
if (anArray[j] > anArray[j + 1]) {
temporal = anArray[j];
anArray[j] = anArray[j + 1];
anArray [j + 1] = temporal;
flag = true;
}
}
}
return anArray;
}
public static void mostfreq(int[] anArray){
int count = 1, tempCount;
int freq = anArray[0];
int temp = 0;
for(int i = 0; i < anArray.length-1; i++){
tempCount = 0;
for(int j = 1; j < anArray.length; j++){
if(anArray[i] == anArray[j]){
tempCount++;
}
}
if(tempCount > count){
count = tempCount;
freq = anArray[i];
}
}
System.out.println(freq);
}
public static void leastfreq(int[] anArray){
int count = 100, tempCount;
int least = anArray[0];
int temp = 0;
for(int i = 0; i < anArray.length-1; i++){
tempCount = 0;
for(int j = 1; j < anArray.length; j++){
if(anArray[i] == anArray[j]){
tempCount++;
}
}
if(tempCount < count){
count = tempCount;
least = anArray[i];
}
}
System.out.println(least);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment