Results 1 to 3 of 3
  1. #1
    mhaider2 is offline Member
    Join Date
    May 2012
    Location
    Malaysia
    Posts
    3
    Rep Power
    0

    Default Array Element Problems.

    Hi...

    I have question on how to put value or element in array that its value is increment of previous element. I have created 50 array and need to fill it with this example of data:

    1,5,9,13,17,21 and so on..(increment of 4)

    Java Code:
    public static void main(String args[]){
    
    int x[] = new int[50];
    
    
    for(int i=0; i<=49; i++)
    System.out.println("x["+i+"] = "+x[i]);
    
    for(int i=0; i<=49;i++)
    x[i] = i; 
    
    
    for(int i=0; i<=49; i++)
    System.out.println("x["+i+"] = "+x[i]);
    
    
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Array Element Problems.

    Draw two axes, an X axis that represents the index value (i.e. 0,1,2,3 etc) and a Y axis that representsa the corresponding values (i.e. 1,5,9,13 etc.) That makes a straight line f(x) == 1+4*x. The x value is the index and the f(x) value is the value of the array element; subtituting i for x makes:

    Java Code:
    for (int i= 0; i < 50; i++)
       x[i]=1+4*i;
    This is geometric interpretation; you can also solve it algebraically: every element of the array is four more than the elements to its left. The first element equals one; so:

    Java Code:
    x[0]= 1;
    for (int i= 1; i < 50; i++)
       x[i]= x[i-1]+4;
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    mhaider2 is offline Member
    Join Date
    May 2012
    Location
    Malaysia
    Posts
    3
    Rep Power
    0

    Default Re: Array Element Problems.

    thank you very much..before this i try something like this x[i] = i =+4 but apparently does not work out..your method however works and thank you again for your guidance..

Similar Threads

  1. [Help] .. How To remove an element of an array ?
    By Matty3z in forum New To Java
    Replies: 8
    Last Post: 12-27-2011, 07:14 PM
  2. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  5. Max element in an Array
    By mew in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 05:26 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •