Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Adding vertical array in java

Tue Oct 21, 2008 4:38 pm

This code sum array in vertical
Code:
public
   {
       public static void main(String[ ] args)
         {
            int[][] table = { {1,2,3}, {1,2,3}};
            int row, column;
            for (row = 0; row <table.length; row++)
               {
                    for(column = 0; column<table[ row].length; column++)
                         System.out.print( table[row][column]) ;
                   System.out.println( );
               }
            int[] val = sum1(table);
            for(int k=0; k<val.length; k++)
                    System.out.print(val[k]+" ");
         }
 
     public static int
[] sum1 (int[][] numbers)
        {
           int sum[] = new int[numbers[0].length];    /* Change1: set sum[] 's
 length to (numbers[0].length)  */
                       
           for 
(int i = 0; i < numbers[0].length;
i++)                           /*Change 2: change the cond'n */
              {                                                                /
*(
k<numbers.length) to (k<numbers[0].length)  */  
                  int total 
= 0;
                  for (int j = 0; j < numbers.length;
j++)                          /*Change 3: change the cond'n
(k<numbers[0].length) to (k<numbers.length)  */
                  total += numbers[j][i]
;
                                                    //Change 4  : replacce 
[i] each other [j]
                  //System.out.print( total);
                  sum[i]=total;
                  //System.out.println( );
                }
           return sum;
       }
}
 




Post a reply
  Related Posts  to : Adding vertical array in java
 vertical align of the image     -  
 vertical alignment of span tag     -  
 Array Casting in java     -  
 Java Two Dimensional Array     -  
 Need Help with Java Array Sorting Logic     -  
 quicksort algorithm implementation java code- array sorting     -  
 Bubble Sort Algorithm Java Implementation Code-Sorting Array     -  
 adding abbreviations to your html     -  
 Need automatic adding to catalogs     -  
 Adding a New Node to contact.xml     -  

Topic Tags

Java Arrays