3 Replies - 184 Views - Last Post: 13 May 2014 - 02:12 PM Rate Topic: -----

#1 smalld01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 25-February 14

Question regarding arrays

Posted 13 May 2014 - 01:54 PM

I am working on a study guide for my final and it is mainly focusing on arrays. I need to write a method that accepts an array of ints and squares each element of the array. No creating new arrays and no returning any values. Does this look correct?:

public void squareInts(int[] ints) {
    for(int i = 0; i < ints.length; i++) {
        ints[i] = (ints[i] * ints[i]);
    }
}



Any suggestions on what Im doing wrong? (if anything?)

Thanks

This post has been edited by jon.kiparsky: 13 May 2014 - 01:57 PM
Reason for edit:: fixed [code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: Question regarding arrays

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: Question regarding arrays

Posted 13 May 2014 - 01:58 PM

Have you tested it? Does it seem to work?
(it looks okay to me, but I'm more than usually obtuse, and I might miss something. the compiler doesn't miss stuff)
Was This Post Helpful? 0
  • +
  • -

#3 smalld01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 25-February 14

Re: Question regarding arrays

Posted 13 May 2014 - 01:59 PM

I am just writing all these out by hand because thats how our final test will be. So I am just looking to see if it looks correct is all. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: Question regarding arrays

Posted 13 May 2014 - 02:12 PM

Good practice that.
Go ahead and test it then. Then fix it if it's broken, and try some other functions. Rotate the element by 1 space - move array[0] to [1], and [1] to [2], and so forth, all the way around (treat the array as circular, so the last element ends up at zero). Do the same, but allow the user to specify how many elements to rotate by. Add each element to the following, again treating the array as circular. Swap the values from two arrays. That sort of thing - just write a lot of methods and see what happens.

If you want to get a little meta, try this: Replace each element array[i] with the value it points to (so if array[0] == 3, then replace the value in array[0] with the value in array[3]) (think about what to use for a default if the value is out of range) (this one is actually a little interesting, and I'm leaving it intentionally underspecified... :) )
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1