Make math operations with some array elements

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 6
Reputation: xandres is an unknown quantity at this point 
Solved Threads: 0
xandres xandres is offline Offline
Newbie Poster

Make math operations with some array elements

 
0
  #1
May 4th, 2008
Hi.
First of all, I am new to java and I am having some problems in understanding arrays.

I was asked to do the following program.

2 5 9 8 7
3 4 8 5 3
7 6 9 6 3
8 9 5 3 2
1 3 1 5 6

That is a multidimensional array.
I need to sum the elements from the array that are right from the diagonal
(5 9 8 7 8 5 3 6 3 2)
and
sum the elements from the array that are left from the diagonal
(3 7 6 8 9 5 1 3 1 5)

I know how to sum all the elements from the array, but not certains like in that case.

I want to understand arrays. I would like that you give me just hints of how to do it, not the code itself.

Thanks in advance

PS: Sorry for the English, it isn't my natural language
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Make math operations with some array elements

 
0
  #2
May 5th, 2008
Well, you have a row index and a column index (call them i and j respectively), and you know the number of rows and the number of columns. For the left side, the elements to the left of the diagonal are the ones you are interested in and those are defined as those array elements where the row index is greater than the column index. For the right side of the diagonal, you are interested in those elements where the row index is less than the column index. So you're going to have a nested loop:
  1. for (int i = ?; i < ?; i++) // rows
  2. {
  3. for (int j = ?; ? < ?; j++)
  4. {
  5. // add element (i,j) to the sum
  6. }
  7. }
Fill in the question marks for the correct loop parameters. These question marks will depend on whether you are doing the left or right side. Note the elements you are interested in above and design your loops so that all of those index combinations and only those index combinations go through the loop.
Last edited by VernonDozier; May 5th, 2008 at 12:10 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 6
Reputation: xandres is an unknown quantity at this point 
Solved Threads: 0
xandres xandres is offline Offline
Newbie Poster

Re: Make math operations with some array elements

 
0
  #3
May 5th, 2008
Thanks, sounds logic.
Here is my final code:

  1. class ArraysM{
  2.  
  3. public static void main (String args[]) {
  4. ArraysM. fillArrayB ();
  5. }
  6. public static void fillArrayB(){
  7. int a[][] = new int [3][3];
  8. for(int i=0;i<3;i++){
  9. for (int j=0;j<3;j++){
  10. System.out.println("Enter the element "+i+ "," +j);
  11. a[i][j] = Lectura.readInt();
  12. }
  13. }
  14. ArraysM.sum2(a);
  15. ArraysM.sum1(a);
  16.  
  17. }
  18. public static void sum2 (int a[][]){
  19. int acum = 0;
  20. for (int i=0;i<3;i++){
  21. for (int j=0;i>j;j++){
  22. acum = acum+a[i][j];
  23.  
  24. }
  25. }
  26. System.out.println("");
  27. System.out.println("Elements of region 1: "+acum);
  28.  
  29. }
  30. public static void sum1 (int a[][]){
  31. int acum = 0;
  32. for (int j=0;j<3;j++){
  33. for (int i=0;i<j;i++){
  34. acum = acum+a[i][j];
  35.  
  36. }
  37. }
  38. System.out.println("");
  39. System.out.println("Elements of region 2: "+acum);
  40.  
  41. }
  42. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC