Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Java Array Program Help

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Java Array Program Help

    Hi, I was wondering whether anyone could help me with this program?

    Write a program that accepts two integer command-line arguments: x and k. THe program must construct an array of x random double values between -5..5. Then your program must repeat the following two steps k times:

    1) Mutate the array
    2)Display the array

    To mutate the array, implement a routine

    void mutate(double[] delta)

    The method must accept a double array, pick a random element, and change it to a random double value -5..5.

    You must print out all the arrays (it will be z x k number of arrays)

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Array Program Help

    What have you tried?
    Please wrap all posted code in code tags.

    Do you have any specific java programming questions?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    chocolate_avocado (March 10th, 2019)

  4. #3
    Junior Member
    Join Date
    Mar 2019
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Array Program Help

    I'm not sure how to add the post tags but

    I'm trying to figure out how to change the value of the array in the mutate function and then use that new value to replace the value in the main static void.
    e.g. if x is 5 and it mutate the 3 array, I'm not sure how to replace the 3rd array in the main function with the mutated answer

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Array Program Help

    To wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    how to change the value of the array
    If you are talking about changing the contents of an array, use an assignment statement:
       theArray[theIndex] = newValue;

    to replace the value in the main static void.
    Again an assignment statement.
      new1DimArray = old1DimArray;   //  replace value
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Mar 2019
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Array Program Help

    import java.util.Arrays;
    public class BeeMutate {
    	public static int random;
    	public static int n;
    	public static int i;
     
    	public static void main(String args[]) {
    		n = Integer.parseInt(args[0]);
    		int k = Integer.parseInt(args[1]);
    		double mutated;
    		double[] value = new double[n];
    		for (int z = 0; z < k; z++) {
    			for (int i = 0; i < value.length; i++) {
    				value[i] = (Math.random() * 10 - 5);
     
    			}
    			mutated = mutate(value);
    			value[random] = mutated;
    			System.out.println(String.format("%.2f", Arrays.toString(value)));
    			}
    		}
     
    	public static double mutate(double[] delta) {
    		random = ((int) (Math.random() * n));
    		delta[random] = (Math.random() * 10 - 5);
    		return delta[random];
    	}
    }


    --- Update ---

    At the moment, it is obviously not going to be able to print out all the values[i] because the for loop closes. But I'm not sure how to run the for loop, assign a random value to each array box, then only mutate one, print them and run that entire function for the value of k

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Array Program Help

    how to run the for loop,
    assign a random value to each array box,
    then only mutate one,
    print them
    and run that entire function for the value of k
    Take the problems one at a time:
    What problem do you have with the loop? I see 2 for loops, which is a problem?

    What are the values held in the two variables: n and k? A Variables should have a name that describes what it contains.

    For debugging the code to see what is in an array, use the Arrays class's toString method:
       System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Mar 2019
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Array Program Help

    Essentially, how do I run the mutate program once in the for loop, even though n will run the loop mutiple times

    for (int z = 0; z < k; z++) {
    			for (int i = 0; i < value.length; i++) {
    				value[i] = (Math.random() * 10 - 5);
    					mutated = mutate(value);
    			value[random] = mutated;
    			System.out.println(String.format("%.2f", Arrays.toString(value)));
    			}
    			}
    		}


    --- Update ---

    n and k are any positive integers that the user can enter. For the second loop (i), I'm confused as how to only run the mutate function once, as the loop will continue for each value up to n

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Array Program Help

    n and k are any positive integers
    What is their description? What is in n? What is in k?

    how to only run the mutate function once
    Why is it inside of a loop, if it should only be executed one time?
    What is the purpose of the loop?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Mar 2019
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Array Program Help

    for this case, lets say n = 5 and k = 10. The foor loop is used to assign a random variable to every array "box" that exists. So, to put a random number in value[0]....value[5]

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Array Program Help

    What is the meaning of the contents of n and k?
    For example:
    if n contains the number of slots in the array. A better name would be arraySize.
    if k is the number of times to call mutate. A better name might be nbrOfMutates

    for loop is used to assign a random variable to every array "box" that exists.
    Yes, that is a common use of a for loop - to iterate over the contents of an array

    But, Why are there two for loops? One inside the other.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Mar 2019
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Array Program Help

    there are two loops in order to run entire program multiple times. So since k is 10, the loop will complete 10 times and print out the 5 different arrays 10 times.

    --- Update ---

    for (int z = 0; z < k; z++) {
    				for (int i = 0; i < value.length; i++) {
    				value[i] = (Math.random() * 10 - 5);
    				while (i != (n-1)) {
    					continue;
    				}
    					mutated = mutate(value);
    					value[random] = mutated;
    					System.out.println(String.format("%.2f", Arrays.toString(value)));
    				}
    			}
    		}

    i thought that this would fix it, but now the program doesn't print anything.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Array Program Help

    the program doesn't print anything.
    What does the program do when it executes? Does it ever end or is it in an infinite loop?
    What is the purpose of the while loop?
    It should show an error because the println statement is not correct.

    Do you know about program comments? They are a very useful way to describe what a program is trying to do and how it is going to do it.
    If the posted code had proper comments I would not have to ask questions like:
    What is the purpose of the while loop?
    What is the meaning of the contents of n and k?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member
    Join Date
    Mar 2019
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Array Program Help

    Quote Originally Posted by Norm View Post
    What does the program do when it executes? Does it ever end or is it in an infinite loop?
    What is the purpose of the while loop?
    It should show an error because the println statement is not correct.

    Do you know about program comments? They are a very useful way to describe what a program is trying to do and how it is going to do it.
    If the posted code had proper comments I would not have to ask questions like:
    What is the purpose of the while loop?
    What is the meaning of the contents of n and k?
    How would i go around printing the entire array[i] in the loop? Thank you, I will try to add comments

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Array Program Help

    How would i go around printing the entire array[i]
    use the Arrays class's toString method:
       System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Junior Member
    Join Date
    Mar 2019
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Array Program Help

    Thank you so much! I think that I got it right!

Similar Threads

  1. Replies: 3
    Last Post: December 1st, 2018, 03:39 PM
  2. [SOLVED] Re: Blue Pelican Java- Array of Hope; char array for loops?
    By ZaneDarklace in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 14th, 2014, 11:46 AM
  3. Java 1.4 Array issues - controlling array size for dynamic usage
    By doonan79 in forum Collections and Generics
    Replies: 5
    Last Post: June 18th, 2013, 11:53 AM
  4. array program help
    By joseph.fruit in forum Collections and Generics
    Replies: 1
    Last Post: February 27th, 2012, 09:13 AM
  5. help me to do this Array Program
    By fahdsaad in forum Member Introductions
    Replies: 1
    Last Post: June 30th, 2009, 02:19 AM