• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with an exercise problem  RSS feed

 
omer Shiman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Array a has complete numbers, I need to make an arry b that has 3 adjacent cells for every 2 adjacent cells at array a.
Array b is built like this: if the sum of 2 adjacent cells in array b can be divided by 3, then 2 cells in array b will contain each one, and the third will be their sum, if they can't be divided by 3, then the third cell will be 0.
An example: a - | 1 | 5 | 9 | 5 |
b - |1 | 5 | 6 | 9 | 5 | 0 |

here's my code:



can anyone help me correct it? I am a bit puzzled by methods and how to use them
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omer,
Welcome to CodeRanch!

Can you describe the problem you are having? Does something not compile? Are you getting different output than you'd expect?
 
omer Shiman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Omer,
Welcome to CodeRanch!

Can you describe the problem you are having? Does something not compile? Are you getting different output than you'd expect?


Well, the "return c" doesn't work, and when I try to insert values into the second array from the mehtod (Which I'm not certain how to do) it doesn't work.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi omer,

there are some problems indeed.

First of all: your method 'makeb' is declared to return an integer, yet in your code you (try to) return an array.

Then, in the body of 'makeb', array c is declared to have size N. However, if you do the counting, you get:

a[0] and a[1] => c[0], c[1] and c[2].

Next, a[1] and a[2] => c3, c4 and c5 (omitting the [] for brievety)

So, for each entry in array a[] you get three entries in array b[]. except for the last element in a[], since
that element has no successor. So you need to be careful when you determine the size of array b[].

Greetings,
Piet
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!