I would like to know, how to find out which column in the 2D-array has got the largest sum. How would I approach this?
public static void main(String args[]) {
int[][] array = {
{ 132, 154, 118 },
{ 355, 101, 50 },
{ 432, 143, 365 },
{ 462, 234, 185 }
};
}
You could go with a nested for loop
int maxCol = 0;
int valOfMaxCol = 0;
for(int i = 0; i < 3; i++){
int sum = 0;
for(int j = 0; j < array.length; j++){
sum += array[j][i];
}
if(sum > valOfMaxCol){
valOfMaxCol = sum;
maxCol = i;
}
}
System.out.println("Max Col is " + (maxCol + 1) + " with the value " + valOfMaxCol);
Related
Notice how the '0' of the collisions column in the 3rd line is shifted, as well as other numbers, what is a fix to that? [
This is my current output
System.out.println(""+capacity +"\t\t\t "+size()+"\t\t "+num+"\t\t"+ data + "\t\t\t" + collisions)
I would start by precomputing the lengths of all of the header fields. Then use that and formatted io to build a table programmatically. Something like,
String[] headings = {
"Capacity",
"Size",
"Num",
"data",
"Collisions"
};
int[] lengths = new int[headings.length];
for (int i = 0; i < headings.length; i++) {
lengths[i] = headings[i].length() + 4;
}
int[][] values = {
{ 100, 1, 1, 241, 0 },
{ 100, 2, 1, 289, 0 },
{ 100, 3, 1, 4, 0 }
};
for (int i = 0; i < headings.length; i++) {
System.out.printf("%-" + lengths[i] + "s", headings[i]);
}
System.out.println();
for (int i : lengths) {
for (int j = 0; j < i; j++) {
System.out.print("=");
}
}
System.out.println();
for (int[] arr : values) {
for (int i = 0; i < arr.length; i++) {
System.out.printf("%-" + lengths[i] + "s", arr[i]);
}
System.out.println();
}
Outputs
Capacity Size Num data Collisions
=================================================
100 1 1 241 0
100 2 1 289 0
100 3 1 4 0
What I need to do is make the main method give an array that the other two methods have. The code works, but the numbers are just in the wrong place.
Everything I've tried has given me an exception and I'm not sure what to do.
public static void main(String[] args) {
arrayTotalAndAverage(null); //not sure what to put in the parenthesis, it was corrected to be null
arrayTwoDimTotalAndAverage(null);
}
public static void arrayTotalAndAverage(int[] array) {
int[] numbers = {10, 4, 13, 29, 57, 92, 114, 212, 3, 88, 36, 101, 77, 42, 209}; //I need to move these numbers to the main method and I'm not sure how to leave it, is it "int[] numbers = {}"?
int total = 0;
double average = 0; //is this right?
int highest = numbers[0];
int lowest = numbers[0];
for (int i = 0; i < numbers.length; i++) {
total += numbers[i];
average = total / numbers.length;
if (numbers[i] > highest)
highest = numbers[i];
if (numbers[i] < lowest)
lowest = numbers[i];
}
System.out.println("Total");
System.out.println(total);
System.out.println("Average");
System.out.println(average);
System.out.println("Highest number");
System.out.println(highest);
System.out.println("Lowest number");
System.out.println(lowest);
}
public static void arrayTwoDimTotalAndAverage(int[][] array) {
int[][] numbers = { { 11, 17, 24 }, { 49, 57, 78 }, { 677, 173, 923 } };
int total = 0;
double average = 0;
int highest = numbers[0][0];
int lowest = numbers[0][0];
for (int row = 0; row < numbers.length; row++) {
for (int col = 0; col < numbers[row].length; col++) {
total += numbers[row][col];
average = total / numbers.length;
if (numbers[row][col] > highest)
highest = numbers[row][col];
if (numbers[row][col] < lowest)
lowest = numbers[row][col];
}
}
System.out.println("2D Total");
System.out.println(total);
System.out.println("2D Average");
System.out.println(average);
System.out.println("2D Highest number");
System.out.println(highest);
System.out.println("2D Lowest number");
System.out.println(lowest);
}
A simple example of how to send an array and receive them from another place is like this:
We have our array outside, then you pass it as parameter to your method, where you're expecting an int[] parameter, then you can make use of it with parameter variable name inside that method.
For example:
public class ArrayAsParametersExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
printNumbersFromArray(numbers);
}
private static void printNumbersFromArray(int numbersArray[]) {
for (int n : numbersArray) {
System.out.println(n);
}
}
}
You can make your main function look like:
public static void main(String[] args){
int[] numbers = {10, 4, 13, 29, 57, 92, 114, 212, 3, 88, 36, 101, 77, 42, 209};
arrayTotalAndAverage(numbers);
}
And do similarly for your other method.
Change arrayTwoDimTotalAndAverage like this.
public static void arrayTwoDimTotalAndAverage(int[][] array) {
int[][] numbers = { { 11, 17, 24 }, { 49, 57, 78 }, { 677, 173, 923 } };
int total = 0;
double average = 0;
int highest = numbers[0][0];
int lowest = numbers[0][0];
int count = 0;
for (int row = 0; row < numbers.length; row++) {
for (int col = 0; col < numbers[row].length; col++) {
total += numbers[row][col];
count += numbers[row].length;
if (numbers[row][col] > highest)
highest = numbers[row][col];
if (numbers[row][col] < lowest)
lowest = numbers[row][col];
}
}
average = total / count;
System.out.println("2D Total");
System.out.println(total);
System.out.println("2D Average");
System.out.println(average);
System.out.println("2D Highest number");
System.out.println(highest);
System.out.println("2D Lowest number");
System.out.println(lowest);
}
Like all Java objects, arrays are passed by value ... but the value is the reference to the array. So, when you assign something to a cell of the array in the called method, you will be assigning to the same array object that the caller sees.
So Instead of defining the array in each method, we can initialize the array in the main method, which we can then pass to the candidate methods as a parameter.
Try this:
public static void main(String[] args) {
int[] numbers = {10, 4, 13, 29, 57, 92, 114, 212, 3, 88, 36, 101, 77, 42, 209};
int[][] numbers2 = { { 11, 17, 24 }, { 49, 57, 78 }, { 677, 173, 923 } };
arrayTotalAndAverage(numbers);
arrayTwoDimTotalAndAverage(numbers2);
}
public static void arrayTotalAndAverage(int[] numbers) {
int total = 0;
double average = 0; //is this right?
int highest = numbers[0];
int lowest = numbers[0];
for (int i = 0; i < numbers.length; i++) {
total += numbers[i];
average = total / numbers.length;
if (numbers[i] > highest)
highest = numbers[i];
if (numbers[i] < lowest)
lowest = numbers[i];
}
System.out.println("Total");
System.out.println(total);
System.out.println("Average");
System.out.println(average);
System.out.println("Highest number");
System.out.println(highest);
System.out.println("Lowest number");
System.out.println(lowest);
}
public static void arrayTwoDimTotalAndAverage(int[][] numbers) {
int total = 0;
double average = 0;
int highest = numbers[0][0];
int lowest = numbers[0][0];
for (int row = 0; row < numbers.length; row++) {
for (int col = 0; col < numbers[row].length; col++) {
total += numbers[row][col];
average = total / numbers.length;
if (numbers[row][col] > highest)
highest = numbers[row][col];
if (numbers[row][col] < lowest)
lowest = numbers[row][col];
}
}
System.out.println("2D Total");
System.out.println(total);
System.out.println("2D Average");
System.out.println(average);
System.out.println("2D Highest number");
System.out.println(highest);
System.out.println("2D Lowest number");
System.out.println(lowest);
}
here fundememtal,database are two subjects. same element's on both
arrays are for the same student. ( fundemental[0],database[0]--> this
is for the student 1 ) . i need to find highest(high rank) to lowest
rank students from this program.i declare a sort method and pass the
total array and create that array total as ascending.just check it out my attachment photo.
i need to find this rank
here is my code.if anybody have unclear, please ask me.
import java.util.*;
class Remove{
public static void main(String args[]){
int [] fundemental={54,34,35,65,87,37};
int database[]={67,56,45,57,78,89};
int[] total=new int[database.length];
for (int i = 0; i < database.length; i++){
total[i]=fundemental[i]+database[i];
}
int [] arrayTot=sort(total);
int index=0;
for (int i = 0; i < total.length; i++){
for (int j = 0; j < total.length; j++){
if(total[i]==(fundemental[j]+database[j]))
index=i;
}
}
}
public static int[] sort(int[]total){
for (int i = total.length; i >0; i--){
int min=total[0];
int index=0;
for (int j =1; j < i; j++)
{
if(total[j]<min){
min=total[j];
index=j;
}
}
total[index]=total[i-1];
total[i-1]=min;
}
return total;
}
}
if anybody has an another idea for find highest rank to lower rand from
student's two subject, please code me.
You can use insertion sort to the small array size and swap all arrays
public static void main(String[] args) {
int[] fundamentals = { 54, 34, 35, 65, 87, 37 };
int database[] = { 67, 56, 45, 57, 78, 89 };
int len = database.length;
int[] total = new int[len];
for (int i = 0; i < len; i++) {
total[i] = fundamentals[i] + database[i];
}
for (int i = 1; i < len; i++) {
for (int j = i; j > 0 && total[j] > total[j - 1]; j--) {
swap(total, fundamentals, database, j, j - 1);
}
}
}
static void swap(int[] total, int[] fundamentals, int[] database, int i, int j) {
int temp = total[j];
total[j] = total[i];
total[i] = temp;
temp = fundamentals[j];
fundamentals[j] = fundamentals[i];
fundamentals[i] = temp;
temp = database[i - 1];
database[i - 1] = database[i];
database[i] = temp;
}
, output
[165, 126, 122, 121, 90, 80]
[87, 37, 65, 54, 34, 35]
[78, 89, 57, 67, 56, 45]
I don't understand the labelled break statement, which for statement. Does it stop the one just below it or the one furthest from it?
Also, I do not understand why j is initialized twice, why not just initialize it inside the for statement? Furthermore, when j < arrayOfInts[i].length can't j be an element from array 0 (that is until it increments) because j can be an element from an array lower that arrayOfInt[i] right?
class BreakWithLabelDemo {
public static void main(String[] args) {
int[][] arrayOfInts = {
{ 32, 87, 3, 589 },
{ 12, 1076, 2000, 8 },
{ 622, 127, 77, 955 }
};
int searchfor = 12;
int i;
int j = 0;
boolean foundIt = false;
search:
for (i = 0; i < arrayOfInts.length; i++) {
for (j = 0; j < arrayOfInts[i].length;
j++) {
if (arrayOfInts[i][j] == searchfor) {
foundIt = true;
break search;
}
}
}
if (foundIt) {
System.out.println("Found " + searchfor + " at " + i + ", " + j);
} else {
System.out.println(searchfor + " not in the array");
}
}
}
Sorting a 2-dimensional can be done in two ways :
Converting 2-dimensional to 1-dimensional and then sorting it.
Directly sorting the 2-dimensional array.
I would like to know the second method. Sorting it directly.
Try this. You can sort two dimensional array Directly
int array[][] = { { 12, 43, 21, 87, 32 }, { 43, 75, 21, 45, 65 } };
int t = 0;
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 5; y++) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 5; j++) {
if (array[i][j] > array[x][y]) {
t = array[x][y];
array[x][y] = array[i][j];
array[i][j] = t;
}
}
}
}
}
System.out.println("The Sorted Array:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(array[i][j] + "\t");
}
System.out.println();
}