943,718 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3668
  • Java RSS
May 4th, 2008
0

Make math operations with some array elements

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xandres is offline Offline
6 posts
since May 2008
May 5th, 2008
0

Re: Make math operations with some array elements

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:
JAVA Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
May 5th, 2008
0

Re: Make math operations with some array elements

Thanks, sounds logic.
Here is my final code:

java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xandres is offline Offline
6 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: delete objects
Next Thread in Java Forum Timeline: Socket Programming





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC