Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    New to Java! Initialize elements of array to 10. HELP!

    Hello.
    I have been trying to complete this homework assignment for a long time, but I can't figure this part out. Please help! Here are the instructions.

    "Write Java code to complete the task below using a “for each” loop.
    Task: Initialize each element of an array of real numbers to the value 10."

    Ok, so I don't quite understand what this is telling me to do. Am i supposed to create an array and then set each element in that array to the number 10? How do I do that using a for loop?
    Thanks!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

    Please look at Forum Rule #1.5 re homework. If you make a serious effort yourself then someone will doubtless correct/improve your code. But we will not do your homework for you.


    It is your responsibility to die() if necessary….. - PHP Manual

  • #3
    New Coder
    Join Date
    Feb 2011
    Posts
    48
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Let's say the number of elements in the array is n, an int value.

    Code:
    double[] myArray = new double[n]; //double is the type for real numbers, this row creates an array of n elements
    
    for (int i = 0; i < myArray.length; i++)
        myArray[i] = 10;


  •  

    Posting Permissions

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