Getting sum of each column in 2D array

hi,
i want to get sum of each column from 2D array. i did public static void main(String[] args) {
          double sum=0.0;
               int[][] arr={
                         {1,2,8},
                         {2,2,3},
                         {2,2,5},
                         {1,2,8},
                         {1,1,9},
                         {2,2}
               int i=0,j=0;
               int len=arr.length;
               while(j!=len){
                    sum=0;
                    for(i=0;i<arr.length;i++){
                    sum+=arr[i][j];
                    System.out.println("sum is:" +sum);
                    j++;
     }but it doesnot work for jagged array...somebody please help!
thankx                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Stop assuming the rows in your matrix are all the same length. Arrays know how long they are. Use that information to determine if the current column exists in that row.

Similar Messages

  • How to get sum for each currency's in ALV Report

    Hi,
    A column has amounts with various currency's.
    May I know how to get sum quantity for each currency in ALV Report?
    Thanks in advance.

    Hi,
    Currency value column should have reference to currency code column.
    Regards,
    Wojciech

  • Sum of the column in one array

    Hi, I am a new user of Labview.
    I am using a simulator composed by different for loops. This simulator saves in an unique txt file one column for each step.
    Each colomn has 1024 elements. Thus, for example,  if the number of steps is 10, the final txt file will has 10 columns of 1024 elements each. How is it possible to sum all these columns obtaining a unique final column with again 1024 elements and then to calculate a column which represents the mean of all the others?
    Mario
    PS. I am using Labview 7.1

    Here's a simple solution. It should work with any size of 2D array and simply add two more columns with the desired values (sum and mean)
    Message Edited by altenbach on 11-20-2007 06:07 PM
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    SumArray.png ‏21 KB

  • How to get sum of quantity column in table control

    Hi, expert ,
    Someone can guide me how to get som of qty column in table control ?
    I created a screen with a table control  and has a  qty quantity field .
    I want to show total of qty  in a  field .
    my code like this ...
    PROCESS AFTER INPUT.
      LOOP AT I_tab.
        CHAIN.
           FIELD I_tab-COUNTQTY MODULE  caculate_total_qty .
        endchain.
        MODULE UPDATE_TOTAL_QTY  .
      ENDLOOP.
    but, when I Back to the previous screen
    It's caculate again so I always get wrong Total quantity .
    anyone can help me ...

    Hi , Dhina
    I have another question .
    I use internal table data in table control to update  database table .
    The data in the db will be change ,
    but each record I want to delete in table control , it's still exist in database .
    my code is
    WHEN 'SAVE'
         MODIFY YDSGBT53 FROM TABLE I_YDSGBT53 .
         COMMIT WORK .
      END CASE
    How  to fix my code and let it can change and delete data in database  ?
    or
    when I delete record  in table control I must to delete data in the database too .
    Edited by: TitusFang on Jun 16, 2011 3:21 AM

  • How to get sum of highlighted column values.

    Is there currently any functionality that allows you to highlight a certain number of column values in the returned result set and to see the sum of the values highlighted.

    Hi,
    Not directly, sorry.
    You could always try highlighting the desired cells, copy/paste them into a column in a spreadsheet like MS Excel or Open Office Calc, then add a sum function into a nearby open cell. The spreadsheet can be added to SQL Developer's Tools|External Tools menu for convenience.
    Regards,
    Gary Graham
    SQL Developer Team

  • Problem in Oracle Discoverer  Getting Sum for a column

    HI! All,
    I am using oracle discoverer, while calculating totals for a particular column, the sum does not appear for multiple row, where as it appears for 1 row,
    invoice number itemdescription amount
    1001 ABC 20
    DEF 30
    GHI 40
    SUM:(NOTHING DISPLAYED)
    1002 DEF 190
    SUM:190
    CAN ANY 1 PLEASE HELP IN THIS MATTER?
    Regards
    ritesh Khuller

    can you post the complete SQL statements? thanks.

  • Getting number for each column value based on Date order

    Hi,
    I have Data look like:
    I need to create new column highlighted in red (Team Number). Even if same team comes in several number of times, it should be tracked as new team with whatever is the next number. For eg. team2 comes second time, its is called as 3. If Team 2 comes again
    then it will be 4.
    Any help is appreciated.
    Thanks,
    Punia 

    Thanks for you reply Ricardo. It does work (but only if select 1 ticket).
    My bad I only posted one Ticket Number. Actually there are lots of them ( as below). Your code take all the tickets as one tickets.
    TicketNumber
    OwningTeam
    Status
    Date
    Team Number
    123
    TEAM 1
    Pick Up
    11/12/2014
    1
    123
    TEAM 1
    Complete
    11/12/2014
    1
    123
    TEAM 2
    Pick Up
    11/12/2014
    2
    123
    TEAM 2
    Complete
    11/12/2014
    2
    123
    TEAM 2
    Resolve
    11/17/2014
    2
    123
    TEAM 2
    Complete
    11/24/2014
    2
    123
    TEAM 2
    Pick Up
    12/8/2014
    2
    123
    TEAM 2
    Complete
    12/9/2014
    2
    123
    TEAM 2
    Provide Info
    12/17/2014
    2
    123
    TEAM 1
    Pick Up
    1/8/2015
    3
    123
    TEAM 1
    Resoved
    1/8/2015
    3
    456
    TEAM 1
    Pick Up
    11/12/2014
    1
    456
    TEAM 1
    Complete
    11/12/2014
    1
    456
    TEAM 2
    Complete
    11/24/2014
    2
    456
    TEAM 2
    Pick Up
    12/8/2014
    2
    456
    TEAM 2
    Complete
    12/9/2014
    2
    456
    TEAM 3
    Pick Up
    12/17/2014
    3
    456
    TEAM 3
    Working
    12/18/2014
    3
    456
    TEAM 1
    Pick Up
    1/8/2015
    4
    456
    TEAM 1
    Resoved
    1/8/2015
    4
    789
    TEAM 1
    Pick Up
    11/12/2014
    1
    789
    TEAM 1
    Complete
    11/12/2014
    1
    789
    TEAM 2
    Complete
    11/24/2014
    2
    789
    TEAM 2
    Pick Up
    12/8/2014
    2
    789
    TEAM 2
    Complete
    12/9/2014
    2
    789
    TEAM 1
    Complete
    12/12/2014
    3
    Any work around is really appreciated.
    Thanks,
    Rajneet

  • Header for each column of data

    I am struggling to get header for each column in attached code. I would like to see this data in excel with header at the top. Also I wonder how to know which column belongs to which plot and which axis.
    Solved!
    Go to Solution.
    Attachments:
    saving for excel.vi ‏130 KB

    I would keep this in the original thread.

  • Summing the Rows and Columns in an Array

    I am importing a 2-dimensional array of integers.(which is held in a 2-dimensional array)
    I need to store the row sums and column sums in separate 1-dimensional arrays.
    I can get the integers in and print out a list along with the grand total(sum of all).
    But, how do I pass each row's and each column's value in to my sumRow and sumCol methods to get the sum for each row and each column?
    Can I do the row and column summing in the same "for" statement where I calculate the "grand total"? Or am I making this more difficult than it is?
    Would appreciate any help.
    This is what I have so far:
    import java.awt.Graphics;
    import java.applet.Applet;
    public class TwoWayTable extends Applet {
         int numRows;
         int numCols;
         int [] [] cell;
         int [] rowSum;
         int [] colSum;
         int grandTotal;
    public TwoWayTable(int [][] data){
    grandTotal = 0;
    cell = new int [data.length][data.length];
    for(int i = 0; i < data.length; i++)
    for(int j = 0; j < data.length; j++){
         cell[i][j] = data[i][j];
    grandTotal += cell[i][j];
         System.out.println(cell[i][j]);
    System.out.println(grandTotal);
    public int sumRow(int [] data2){
         int rowaccumulator=0;
         rowSum = new int[data2.length];
         for(int numRows = 0; numRows < rowSum.length; numRows++){
         rowaccumulator += rowaccumulator + rowSum[numRows];
              return(rowaccumulator);
    public int sumCol(int [] data3){
         int colaccumulator = 0;
         colSum = new int[data3.length];
         for(int numCols = 0; numCols < colSum.length; numCols++){
              colaccumulator += colaccumulator + colSum[numCols];
              return(colaccumulator);

    Thanks for your input.
    I'll make the changes that you suggest.(after this)
    My output prints:
    4 6 3 8 21
    9 1 5 3 18
    13 7 8 11 39
    numbers are right, but I need to format the table
    the output needs to look like this:
    int int int int | rowsum
    int int int int | rowsum
    colsum colsum colsum colsum | total
    How do I do this?
    I have no idea?
    I'm supposed to call a "void setMargins( )" method to line this up, without
    using the exotic formatting in the IO library.
    I'm also supposed to use "public String toString( )"
    This is what I have so far:
    import java.awt.Graphics;
    import java.applet.Applet;
    public class TwoWayTable extends Applet {
    int numRows;
         int numCols;
         int [] [] cell;
         int [] rowSum;
         int [] colSum;
         int grandTotal;
    public TwoWayTable(int [][] data){
    cell = new int [data.length][data.length];     
    for(int i = 0; i < data.length; i++){
    for(int j = 0; j < data.length; j++){
         cell[i][j] = data[i][j];
    calcTotals(cell);
    for(int i = 0; i < cell.length; ++i){
    for(int j = 0; j < cell.length; ++j){
    System.out.print(cell[i][j] + " ");
    System.out.println(rowSum[i] + " ");
    for(int j = 0; j < cell.length-1; ++j){
    System.out.print(colSum[j] + " ");
    System.out.println(colSum[cell.length-1] + " " + (grandTotal));
         public void calcTotals(int [][] data2){
              grandTotal = 0;
              rowSum = new int[data2.length];                         
              colSum = new int[data2.length];                         
              for(int numRows = 0; numRows < data2.length; numRows++){
              for(int numCols = 0; numCols < data2.length; numCols++){
                   grandTotal += data2[numRows][numCols];               
                   rowSum[numRows] += data2[numRows][numCols];
                   colSum[numCols] += data2[numRows][numCols];

  • I can't get the Sum of a Column

    I was hoping someone would be able to help me figure out what I'm doing wrong on a form that I'm unable to get a SUM of a column for?  I'm new to LiveCycle Designer and have poured over all that I can find on the internet to figure this out but it's been 10+ hours and I'm no closer now then when I started. Specifically, I added a table to a form that has a multiplication formula set up in each of the ROWs (item * cost = total kind of scenario). These formula's work and give me the the $ amount in the last column titled "total." The problem is I can't figure out how to get a sum of the totals for the "grandtotal" cell at the bottom? Each formula I've tried gives me a warning that "total[*] is unknown" so it makes me think I have my cell menu's set up incorrectly since it's not registering it the $ amount in my "total" cells?  This is work project and I'm at a stopping point until I can figure this out so any help is gratefully appreciated.  Donna
    Table/Row/Column Info:
    My table is: table 3
    My row is: cell1 (all are called cell1)
    My column is total1 (I've changed these to total 1, total2, total3, etc in my efforts to troubleshoot)
    FormCalc entries that I've tried:
    Sum(total[*])
    Sum(total[*]).amount[*])
    $ = Sum(total1.amount + total1.amount + total3.amount)
    Sum(table3.cell[*].total[*])

    HI,
    It appears you've phyically created 3 indivdual rows (correct me if I'm mistaken), so if that's the case...you can simply capature the GrandTotal by adding the fields using by their respective names. Thus, the GrandTotal script would be:  $ = Sum(total1+toal2+total3). Im not sure why you're using ".amount".
    [*] is used when you have repeating subforms. So, say you started out with a single row (Row1) with the following fields: Qty, UnitCost, Total... where Total (field in last column) had the follwoing script: $ = Qty * UnitCost
    If your form was "dynamic" and used an Add Row button, then you would only have to create one row and enable your user to "Add" additional rows, as needed.  As a result, you would need to use an array [*] to find all instances of Total on each row.
    In that case, your script for GrandTotal would look like this: $ = Sum(Page1.Items.Row1[*].Total). Note: Page1.Items is the absoulte location for Row1 on my form, so this would change for you.
    Going back to my previous comment, since your form doesn't appear to use an Add row button, you shoudl be able to add the fields names quite easily.
    Lastly, as an FYI,  you shouldn't need a [*] after total in......Sum(table3.cell[*].total[*])
    Let me know if this helps!
    Thanks
    Shaun

  • How to get the sum in appropriate column without a red triangle appearing?

    Hello,
    In 'Numbers" - How do get columns to add (calculate) the sum in each decending column on an Expense Report.
    When I highlight the decending column the total appears on the far left of the screen.  When I drag the sum amount from there to the appropriate column a red triangle with an ! appears instead of the amount.
    Thanks for your immediate help.

    The red triangle is an Error triangle. Clicking it will display the error message and tell you what error has occurred. From your description, my assumption is that you dragged the SUM() function from the quick calculations at the lower left and dropped it intto a cell in the column being summed.
    If that's the case, this is likely the error message you would see:
    When you highlighted the 'decending column,' you likely selected all of the cells in that column, including the one into which you dropped the function.
    Instead, do one of the following. These assume the column you want to sum is column B.
    If you want the sum at the top of the column:
    Make sure the row you want the sum to appear in is a Header row.
    Enter this formula into any Header Row cell in column B:   =SUM(B)
    If you want the sum at the bottom of the column:
    Add a Footer row to the table. (Go Table (menu) > Footer Rows > 1).
    Enter this formula into the Footer Row cell in column B:   =SUM(B)
    SUM (and other functions) that expect a range of cells will interpret a cell reference entered using only the column letter (B) as meaning 'all of the non-header, non-footer cells in column B', and will exclude those cells in header or footer rows.
    Regards,
    Barry

  • Get the intersection when u know row and column in 2D array

    I am trying to get the intersection of the row and column and I am not able to do so......
    for example:
    123456789 <<These in array indicate the row
    255555555
    388888888
    499997999
    1234 above first element of each row represent columns........
    I want to get 7 which is the intersection of column 6 and row 4............Please help

    Hi Marlin..
    I am reading this after having done it in a very similar way....... thank you so much
    The problem is that the letters in languages can also be characters so they can be a,b..Language over{a,b}..if characters are not in numbers I am trying convert a,b into int internally however I dont know any data structure in java which will accept character as key and int as value......
    So I am stuck at modifying the part highlighted with ???? below
         public void startRecognizing()
              System.out.println("Begun recognizing language....");
              //Convert the word entered to letter array
              char [] wordEntered = word.toCharArray();
              // initialize variables
              currentState = startState[0]; //In the beginning the current state is start state
              /***************    For each letter entered    ********************************/
                   for (int w =0; w < wordEntered.length ;w++)
                        //Print element entered
                        currentChar = wordEntered[w];
                        System.out.println("Char "+currentChar);
                        //Convert to int
                        int currentIntChar;
                        //System.out.println("Char "+currentIntChar);
                        //Print current state
                   int currentIntState = Character.getNumericValue(currentState);
                        //System.out.println(" State "+currentIntState);
                        System.out.println(" State "+currentState);
                   // if characters are numbers this is fine below
                   if((Character.getNumericValue(currentChar)) != -1)
                                          currentIntChar = Character.getNumericValue(currentChar);
    /*** Get the new state based on element entered with the current state and get a new state***/ // column/char entered & row/state// The intersection is the new state which is made the current state for further process
                        //We add 1 since array starts from 0
                   currentState = transitionTable[currentIntState+1][currentIntChar+1];
                        System.out.println("  NEW current state "+currentState);
                   else
         //if letters in the language are not in numbers and are char ie a,b then convert a,b into int internally
                        java.util.Hashtable h = new java.util.Hashtable();
                        // STUCK HERE !!******************??????????
                        h.put(wordEntered[w], new Integer(w));
                        currentIntChar = ((Integer)h.get(wordEntered[w])).intValue();
              System.out.println("currentIntChar non numbers "+currentIntChar);
                        //We add 1 since array starts from 0
                   currentState = transitionTable[currentIntState+1][currentIntChar+1];
                        System.out.println("  NEW current state "+currentState);
              }//for main          
              finalDecision();
         }//startRecognizingThe assignment is due tomm morning...... I havent slept all night !

  • How do you get the sum of two columns multipied together?

    I can't seem to figure out how to get the sum of two columns multiplied together without having to manually type out each location (example: A1*B1+A2*B2+A3*B3, etc..).  Though the idea seems to be rather simple, everything I have tried has only given me an error.  I know there must be an easier way of doing this, but I get lost in the explanations given in 'help' area of numbers, can someone help me?

    C3=SUMPRODUCT(A3:A18, B3:B18)
    the function SUMPRODUCT() takes ranges and multiplies corresponding cells in the ranges, then adds them together.
    I the case I show SUMPRODUCT() performs:
    A3*B3 + A4*B4 + A5*B5 + A6*B6 + ... +  A18*B18
    If you want to perform the same operation on the whole column (s) you could modify the formula:
    C3=SUMPRODUCT(A, B)

  • Get frequent data for each column of a table

    What is the best way to get the most frequent data in each column of a table.
    we have around 25 tables and each table has around 20 columns , so rather than writing group by for each column of table , is there any easy way to find this?
    example we have table
    Column A       Column B         Column C
    Apple             Monday             Red
    orange            Tuesday            Green
    Apple              Monday            Red
    Lemon            Wednesday       Green
    Apple               Thursday         Red
    in this table, column A's frequnt data is Apple , column B's frequent data is Monday , Column C's frequnt data is Red
    Apple 3          Monday 2                  Red 3
    Orange 2        Tuesday 1                 Green 2
    Lemon 1          Wednesday 1         
                          Thursday 1
    Group by kind of query will give this result, but with 20 tables each having 20 - 30 columns if we need similar kind of result ..  is there any way to get this data.

    Hi,
    GROUP BY (using aggregate functions) is the best way to do what you described.
    PIVOT and UNPIVOT are probably what you'd want in this case.  The phrase "GROUP BY" may not actually appear in your code, but you'll essentially be doing a GROUP BY.
    Analytic functions (rather than aggregate functions) can do the job, too, but they'll be less efficient.  Analytic functions get you results about the groups, without losing each row's identify, but in this case, losing each row's identity is eactly what you want to do.  You only want 'Apple' to appear 1 time, not 3 times.
    The fact that you need to do the same query on 20 different tables suggests that there's something wrong with your table design.  Wouldn't it be better to have 1 big table, with a new column that has 20 unique values instead?
    I hope this answers your question.
    If not, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables involved, so that the people who want to help you can re-create the problem and test their ideas.
    Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
    Simplify the problem as much as possible.  For example, instead of posting a problem with 20 tables, each having 25 columns, post a similar problem involving, say, 2 tables, each with 3 columns.
    Always say which version of Oracle you're using (for example, 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • I want to create an array that goes into a case structure where each element in the array is an individual case and gets done in order

    I want to create an array that goes into a case structure where each element in the array is an individual case and gets done in order. Any ideas, I've been playing with the idea but have had no luck, is this even possible?

    Hi,
    Please check it out the attached Vi.. Is this you need?
    Sasi.
    Certified LabVIEW Associate Developer
    If you can DREAM it, You can DO it - Walt Disney
    Attachments:
    Event.vi ‏11 KB

Maybe you are looking for

  • Download Dialog Box, From JSP

    I have sucessfully written a JSP page which allows the user to download a binary file to there drive, all that works fine. However the initial diag box that comes up in windows, prompting you to save, open, or cancel has the name of the jsp page inst

  • Retrieving Raw Files after import...?

    I shoot all RAW files and have imported them to my iPhoto. How do I retrieve the RAW file from iphoto. If I copy and drag them out of iPhoto they are .jpg... Is it too late and does iPhoto convert them upon import. Also and clue on if Aperture does t

  • Linking photo page to iweb?

    Can we add an alias to our photo page which will link us to our iweb? I love my photo page, bring back the feature.

  • Doc. series and warehouse relation ship

    Dear Friends, can we make this type of relationship. 1)I am having doc series- LABOR ,MANUFACTURING 2)Having warehouse- LABOR,MANUFACTURING If i select LABOR series, is it possible to come LABOR warehouse in row level data at S.O. & If i select MANUF

  • STMP Mail

    Hello, i want to write a simple mail program as java application with which I am able to connect to a SMTP Server and write a mail with a receiver and a sender. Has anybody here some good advice or explanation how I can realize that? regards