Finding the Sum of Rows and Columns in a Two-Dimensional Array (Java)

preview_player
Показать описание
This program shows you how to find the sum of rows and columns in a two dimensional array, and also how to use a method to calculate the sum of every element inside of a 2d array.
Рекомендации по теме
Комментарии
Автор

thank you. I cannot express how much this short video has helped me.

ravindrapersaud
Автор

You need to do it like this:

int rowTotal=0;
for (int i=0;i<a[1].length;i++){
rowTotal+=a[1][i];
}
System.out.println(rowTotal);
you need to add the row into the bracket in the for loop which you want to add up or it doesn't work.

ozzDeveveloperOpenForWork
Автор

Man, I think I love you right now... Thank you so much! You saved me, I can't thank you enough! <3

natasastojanovic
Автор

So, I've got a final tomorrow and this helped me finish an overdue assignment. Thanks a million!

wolfboy
Автор

Can we get this guy to commentate over every programming tutorial ever?

umaid
Автор

Thank you! This helped me a lot. Keep up the good work.

richardmui
Автор

Wow dude, I spent like two hours trying to do what you just did in minutes. FML

kingbran
Автор

Anyway to do it with out a nested loop? Like vectorizing the array, I did this in matlab but im new to java

hellowill
Автор

when i try to find to average of the numbers in my 2D array and I use "arrrayname".length to perform the calculation the length that is used is just the length of the row and not the number of elements in the 2D array. why is that?

jprasta
Автор

can you help me with one question please?

I need to give an employee 3 sales inputs. how do i OUTput EACH total of the 3 sales in the for loop. I do get the GRAND total, but not each individuals sales.


public static void main(String[] args)
{
DecimalFormat newF = new DecimalFormat("#.00");
int count;
int countSales = 3;

String tempCount = JOptionPane.showInputDialog(null, "How many sales entries are there ?", "SALES INPUT", JOptionPane.INFORMATION_MESSAGE);
count = Integer.parseInt(tempCount);

String empID[] = new String[count];
String names[] = new String[count];
double sales[][] = new double[count][countSales];
int j;
int i;
double tempSales = 0;
for ( i = 0; i < count; i++)
{
empID[i] = JOptionPane.showInputDialog(null, "Please enter Employee ID for Employee : " + ((i)+1), "EMPLOYEE ID", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "Employee ID : "+ empID[i] + " added!", "New Entry", JOptionPane.INFORMATION_MESSAGE);
names[i] = JOptionPane.showInputDialog(null, "Please enter Employee : " + ((i)+1) + "'s name:", "EMPLOYEE : " + empID[i], JOptionPane.INFORMATION_MESSAGE);
for ( j = 0; j < countSales; j++)
{
String tempMark = JOptionPane.showInputDialog(null, "Enter " + names[i] + "'s "+ ((j)+1)+ "'s sale", "EMPLOYEE : " + empID[i], JOptionPane.INFORMATION_MESSAGE);
sales[i][j] = Double.parseDouble(tempMark);
tempSales+=sales[i][j];
}

}


String salesReport = "";
double empTotal = 0;
for ( i = 0; i < count; i++)
{
for ( j = 0; j < countSales; j++)
{
salesReport+="Employee: "+empID[i] +" - "+ names[i].toUpperCase() +"'s "+((j)+1)+" sale is : R" + (newF.format(sales[i][j]))+ "\n";

}
}


double total = totalSales(sales);
JOptionPane.showMessageDialog(null, salesReport + "\n"+
"Total R" + total + "\n");


}
public static double
{
double total = 0;
for (int i = 0; i < arraySales.length; i++)
{
for (int j = 0; j < arraySales[i].length; j++)
{
total+=arraySales[i][j];
}
} return total;
}
}

Nyhmii
Автор

Then why when I do it your way I end up getting 6 instead of 10 ??


int[][] a = {
{5, 3, 3, 7},
{1, 5, 2, 2},
{1, 2, 3, 4}
};

int rowTotal=0;
for (int i=0;i<a.length;i++){
rowTotal+=a[2][i];
}
System.out.println(rowTotal);

ozzDeveveloperOpenForWork
Автор

What IDE are you using? looks alot easier than textpad

apprientice
Автор

what if i want to print the sum of all rows at once

rukiyaomar
Автор

do you have an explanation to this for c#?

anorajohndheriea.
Автор

The total number of elements is suppose to be 40 not 43 I believe

andrewwallo
Автор

Mine doesnt work? I guess you have to do .length of something?

ozzDeveveloperOpenForWork
Автор

How do you get the autocorrect in eclipse? 1) the for loop 2) the println into automatically writing System.out.println(" "); //so cool

melissabernstein
Автор

Column total works, row total does not work.

EDIT: My friend helped me figure it out. You are going to want to do a.length - (enter number) in the code when finding the rows. That number depends on the array that you have set up. I hope this helps at least a little.

SixHundredAndSixtySix
Автор

But what if I just want to get the total of just two rows?

Achilliz