I need to create the following in my main method. Create an array of size 100 doubles. Write a loop to store the number 150.0 - 249.0 in the 100 location.
public class Lab6_2 {
public static void main(String[] args) {
double[] values;
values = new double [100];
double i = 0;
for(double i = 150.0; i<249.0;i++){
System.out.println(values[99]);
}
}
}
Firstly, filling the array and printing elements are unrelated concerns, so I would separate them in the code by first filling the array, then printing it. Java offers a way to print an entire array, so remove the current print statement and put this line after the loop:
System.out.println(Arrays.toString(array));
Even if you decide to still print elements one per line (as you are now), you should still do this in a separate loop.
Next, you need to assign a value to each element inside your loop, which you're currently not doing. You must calculate the element value from its index, which in this case is easy:
array[i] = i + 150;
You can do this:
public static void whatever() {
double value = 150.0d;
double array[] = new double [100];
for (int i = 0; i < array.length; i++) {
array[i] = value;
System.out.println("Index: " + i + " Value: " + value++);
}
}
There's only one problem with your code. You're correctly generating the floating point values, but your code doesn't store those values in the array.
What do you need to change in order to store the floating point values in the right position in the array?
Couple of things wrong with your code. You're declaring i
twice, which you shouldn't do. Also you're using i
as an iterator so it should be an int
, not a double
and you should declare it inside the for statement. It should also start at 0, not 150, and continue as long as it is less than the length of the array, because while the length of the array is 100, the last element is actually 99.
Also your homework isn't entirely clear, but it sounds like you're supposed to store the numbers 150.0 - 249.0 in each element of the array? But all your for loop is doing is printing out the last element of the array 100 times, and that element is empty because you never put anything in there.
To assign a number to the array you would simply write:
myArray[0] = somevalue;
That would assign somevalue to the first element in the array. But you want to iterate through the elements of the array so instead of 0
you would use i
.
I'm not totally familiar with java, but I know C++ and they're very similar.
double array[] = new double[100];
double value = 150.0;
for (int i = 0; i < 100; i++) {
array[i] = value+i;
System.out.println(value+i);
}
hope that helps.
your problem above was that you started i as 150.0. You need to use a counter variable to increment through your array. You're just printing the 99th element of the array (which you never set) 99 times.
cout <<
. Also, the way you define arrays is different between Java and C++. The OP also wouldn't know why you're returning 0. Not the downvoter either, but your answer does have quite a few flaws - K Mehta 2012-04-03 23:36
[]
after the type, not after the variable name - Dave Newton 2012-04-03 23:49