-
A little assignment involving arrays.
Hello everyone. :)
I'm having a little problem with an activity I have to do. I have to "Write a program that stores the first 400 numbers that are multiples of 13 in an array".
My Current code:
Code JAVA:
class ThirteenArray {
public static void main(String[] args){
//Hour 9, Second activity.
int thirteen, multiply;
for(thirteen = 13, multiply = 1; multiply * thirteen <= 400; multiply++){
int[] sum = new int[multiply] sum;
System.out.print(" " + sum[multiply]);
}
}
}
Output:
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - not a statement
at ThirteenArray.main(ThirteenArray.java:7)
Java Result: 1
BUILD SUCCESSFUL (total time: 15 seconds)
IDE Flaged Line 7:
Code java:
int[] sum = new int[multiply] sum;
-
Re: A little assignment involving arrays.
-
Re: A little assignment involving arrays.
Changed it up a bit.
Code java:
class ThirteenArray {
public static void main(String[] args){
//Hour 9, Second activity.
int multiply = 13;
int[] sum = new int[1200];
for(int thirteen = 1; thirteen <= 400; thirteen++){
multiply = multiply * thirteen;
sum[multiply] = multiply;
System.out.print(" " + sum[multiply]);
}
}
}
Output: run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1560
at ThirteenArray.main(ThirteenArray.java:8)
13 26 78 312Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
If your wondering why I set the sum array to 1200 elements, it's because I was getting the ArrayIndexOutOfBounds error message. So I raised it up a few times but still get that error message.
-
Re: A little assignment involving arrays.
The Exception is pretty self-explanatory. Your array has 1200 elements, so 1199 is the last index. You're trying to access index 1560, which is way off. Why are you holding such a small number of items in such a large array like that?
-
Re: A little assignment involving arrays.
To be honest I am not quite sure of everything that is happening. I get about everything expect line 8 to be exact. I'm just experimenting, trying to find a way that works. And when I do, understand why and how.:confused:
-
Re: A little assignment involving arrays.
First off, you only have to store 400 numbers. So your array should have 400 indexes. And you don't want to store the values to the array in indexes equal to the value, you store them sequentially (first value at index 0, second value at index 1, etc.).
-
Re: A little assignment involving arrays.
I see, is there a way I can make the for loop do that?
-
Re: A little assignment involving arrays.
I don't understand the question. You already have a loop that goes from 1 to 400 in your original post. It shouldn't be too hard to loop from 0 to 399.
-
Re: A little assignment involving arrays.
I meant to make each element of the sum array equal to the first 400 multiples of 13.
Instead of doing it line by line like this.
Code Java:
sum[0] = 13;
sum[1] = 26;
sum[2] = 39;
sum[3] = 52;
// and so on
-
Re: A little assignment involving arrays.
I understand that part, I just don't understand why you were asking about making a loop when you already have almost exactly what you needed in your original post.
Think about the relationship between the index and the value you want to store at that position.
-
Re: A little assignment involving arrays.
:/ Everything I've been trying gave me an error message.
-
Re: A little assignment involving arrays.
I'm guessing this is what you meant.
Code java:
for (int i=0; i < 400; i++)
{
sum[i] = 13 * (i+1);
}
-
Re: A little assignment involving arrays.
Once again, I'm forced to tell you that spoonfeeding is NOT helping. You have robbed yet another OP of the process of figuring out the problem, which is extremely important for a programmer to learn, and then having that "ah-ha!" moment, which programmers love.
-
Re: A little assignment involving arrays.
Is there any reason for Net beans to run a file and display the output with no problems? And display an error message the second time the file was run even though there were no changes made to the file...:confused::mad:
-
Re: A little assignment involving arrays.
Quote:
Originally Posted by
Melawe
Is there any reason for Net beans to run a file and display the output with no problems? And display an error message the second time the file was run even though there were no changes made to the file...:confused::mad:
What is the error?
-
Re: A little assignment involving arrays.
Object.
Code Java:
public class Virus {
//Hour 11, Workshop, object.
static int virusCount = 0;
public Virus() {
virusCount++;
}
static int getVirusCount() {
return virusCount;
}
}
Main class.
Code java:
class ZTesting {
public static void main(String[] args) {
int numViruses = Integer.parseInt(args[0]);
if (numViruses > 0) {
Virus[] virii = new Virus[numViruses];
for (int i = 0; i < numViruses; i++) {
virii[i] = new Virus();
}
System.out.println("There are " + Virus.getVirusCount()
+ " viruses.");
}
}
}
Argument set as : 36.
Error displayed:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at ZTesting.main(ZTesting.java:4)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
-
Re: A little assignment involving arrays.
Updated code:
Code java:
class ThirteenArray {
public static void main(String[] args){
//Hour 9, Second activity.
int multiply = 13;
int[] sum = new int[400];
for(int thirteen = 1; thirteen <= 400; thirteen++){
//multiply = multiply * thirteen;
sum[thirteen] = 13 * (thirteen + 1);
System.out.print(" " + sum[thirteen]);
}
}
}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 400
26 39 52 65 78 91 104 117 130 143 156 169 182 195 208.... 5122 5135 5148 5161 5174 5187 5200 at ThirteenArray.main(ThirteenArray.java:8)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
Line 8:
Code java:
sum[thirteen] = 13 * (thirteen + 1);
-
Re: A little assignment involving arrays.
You never increment the variable in your loop. I'm actually a little surprised that compiles.
But even when you fix that, you're still accessing the indexes wrongly- remember, the indexes go from 0-399, which gives you 400 indexes. Array indexes don't start at 1, and they don't go all the way to the size of the array.
-
Re: A little assignment involving arrays.
Excuse that, removed it to try something and forgot to add it later on, I updated the post please take a look at it again.
Oh and I change the for loop to the way JavaPenguin said to do it, what's wrong with the loop?
-
Re: A little assignment involving arrays.
So, what does your program do now? You need to provide more information if you want any help. I explained how you're going out of the bounds of the array. What about that don't you understand?
-
Re: A little assignment involving arrays.
hmmm, so I have to set the highest element to 401?
-
Re: A little assignment involving arrays.
Update code:
Code java:
class ThirteenArray {
public static void main(String[] args){
//Hour 9, Second activity.
int multiply = 13;
int[] sum = new int[401];
for(int thirteen = 1; thirteen <= 399; thirteen++){
//multiply = multiply * thirteen;
sum[thirteen] = 13 * (thirteen + 1);
System.out.print(" " + sum[thirteen]);
}
}
}
Output:
26 39 52 65 78 91 104 117 130 143 156 169 182 195 208 5096 5109 5122 5135 5148 5161 5174 5187 5200 5200BUILD SUCCESSFUL (total time: 3 seconds)
:D Thanks everyone! :)):-bd>:D<
-
Re: A little assignment involving arrays.
Quote:
Originally Posted by
Melawe
hmmm, so I have to set the highest element to 401?
No. Like I told you, like javapenguin spoonfed you, and like it undoubtedly says on every array tutorial out there- indexes start at zero, not one. So you start at zero and go up to the number of indexes minus one. What you have now does not print out 400 numbers.
-
Re: A little assignment involving arrays.
Is that comment for the updated code too?
-
Re: A little assignment involving arrays.
Quote:
Originally Posted by
Melawe
Is that comment for the updated code too?
Yes. If you don't believe me, try figuring out what's in the first index (which is index zero). Or try printing out the number of elements in the array. Hint- increment a variable each time you add something to the array, then print it out after your loop completes.