• 13
• 15
• 27
• 9
• 9

# What Am I Doing Wrong Here?

This topic is 2504 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

int[] arrayThree = new int[20]; int total = 3; for (int i = 0; i < 20; i++) total = total*arrayThree; TextIO.putln(total); 

Please don't mind the fact that I haven't commented any of the code, but it's a very small program that I'm sure should be much simpler than I'm making it out to be. Basically, what I'm trying to do is create an array of multiples of three so I can add everything within the array after everything is said and done. I'm a straight up newbie here and have been reading as much as I can, but I learn most by applying concepts I read.

I imagine most of you are familiar with Project Euler and for those who are, I'm trying to find the solution to Problem 1. I DO NOT WANT THE ANSWER. Just point me in the right direction, if you could. =) Thanks, in advance.

##### Share on other sites
Well, youre not initializing the array at all. Depending on your language (you didnt specify) thats either going to result in random numbers or all zeroes. Whered you get 20 from?

##### Share on other sites

Well, youre not initializing the array at all. Depending on your language (you didnt specify) thats either going to result in random numbers or all zeroes. Whered you get 20 from?

My apologies. I'm working in Java and 0 is exactly what I'm getting. Perhaps I've misinterpreted how to initialize an array. I'll look into that. I pulled 20 out of the air because I was skeptical about using 1000. I was initially gonna test 20 just to make sure everything was working properly. In any case, I'll check my references on initializing an array, make some adjustments to the code and get back to you. Thanks!

##### Share on other sites
arrayTree is not initialized, which means total*<uninitializedVariable> leads to undefined behavior. You should set all members of the array to 1 for the code below to work correctly, or did I say too much now?

##### Share on other sites

arrayTree is not initialized, which means total*<uninitializedVariable> leads to undefined behavior. You should set all members of the array to 1 for the code below to work correctly, or did I say too much now?

That would just end up with 3 as the answer, wouldnt it?

##### Share on other sites

[quote name='vleugel' timestamp='1304800327' post='4807789']
arrayTree is not initialized, which means total*<uninitializedVariable> leads to undefined behavior. You should set all members of the array to 1 for the code below to work correctly, or did I say too much now?

That would just end up with 3 as the answer, wouldnt it?
[/quote]

haha yeah but I just wanted to give him another puzzle.
To be honest, I'm not realy sure what you want to achieve, if you just want to multiply total 20 times use the following code:

const int multiplier = 3;
int total = 0;

for (int x = 20 ; x < 20 ; x++)
{
total *= multiplier;

}

If you want to add the multiplications in the array use this:

const int multiplier = 3;
int total = 0;
int arrayThree[20] = {1};

for (int x = 20 ; x < 20 ; x++)
{
total *= multiplier;
arrayThree[x] = total;
}

##### Share on other sites
Project Euler goal #1 is to get the sum of all the multiples of 3 and 5 below 1000. Im assuming his code is to find the 3s.

##### Share on other sites

Project Euler goal #1 is to get the sum of all the multiples of 3 and 5 below 1000. Im assuming his code is to find the 3s.

Ah I see, havent heard of poject euler before but seems like something I should check out as well.
Without testing it myself, I assume the code below should do the job right?

edit: tested and works, I hided the solution so you can solve it yourself

[spoiler]
total = 0;
for(int x = 0 ; x < 1000 ; x++)
{
if ((x % 5) == 0 || (x % 3) == 0)
total += x;

}
[/spoiler]

Non spoiler advice: try to use the modulo operator '% '

##### Share on other sites
Looks like it would work, but the op didnt want the answer given to him. Also,[spoiler]it would probably be faster to:
 int total=0; for(int i=0;i<1000;i+=3) total+=i; etc

[/spoiler]

##### Share on other sites
[font="arial, verdana, tahoma, sans-serif"]

int[] arrayThree = new int[20]; int total = 3; for (int i = 0; i < 20; i++) total = total*arrayThree; TextIO.putln(total); 

Please don't mind the fact that I haven't commented any of the code, but it's a very small program that I'm sure should be much simpler than I'm making it out to be. Basically, what I'm trying to do is create an array of multiples of three so I can add everything within the array after everything is said and done. I'm a straight up newbie here and have been reading as much as I can, but I learn most by applying concepts I read.

I imagine most of you are familiar with Project Euler and for those who are, I'm trying to find the solution to Problem 1. I DO NOT WANT THE ANSWER. Just point me in the right direction, if you could. =) Thanks, in advance.

[/font][font="arial, verdana, tahoma, sans-serif"]"int[] arrayThree" declares an array of integers named arrayThree[/font]
[font="arial, verdana, tahoma, sans-serif"] [/font]
[font="arial, verdana, tahoma, sans-serif"] [/font][font="arial, verdana, tahoma, sans-serif"]"new int[20]" allocates a space in memory for an array, made up of 20 elements, with each element being an integer[/font]
[font="arial, verdana, tahoma, sans-serif"] [/font]
[font="arial, verdana, tahoma, sans-serif"] [/font][font="arial, verdana, tahoma, sans-serif"]"arrayThree" here you are trying to access the i[sup]th[/sup] element of arrayThree. [/font]
[font="arial, verdana, tahoma, sans-serif"] [/font]
[font="arial, verdana, tahoma, sans-serif"]the problem is that element i of arrayThree contains... well, we don't know what it contains! you never assigned an integer to that element, so doing any calculations on it will cause an error when you try to run your program. [/font]this video is a good explanation, helped me learn the concept of arrays and visualise it! http://www.youtube.c...&feature=relmfu
[font="arial, verdana, tahoma, sans-serif"] [/font]
[font="arial, verdana, tahoma, sans-serif"]also you should become familiar with the modulus (%) operator, it gives you the remainder left after dividing a number. it should help you solve that problem rather easily! project euler is awesome![/font]
[font="arial, verdana, tahoma, sans-serif"] [/font]
[font="arial, verdana, tahoma, sans-serif"] [/font][font="arial, verdana, tahoma, sans-serif"]e.g.[/font]
[font="arial, verdana, tahoma, sans-serif"] [/font][font="arial, verdana, tahoma, sans-serif"]int y = 50;[/font]
[font="arial, verdana, tahoma, sans-serif"] [/font][font="arial, verdana, tahoma, sans-serif"]boolean is_Y_Even = (y % 2) == 0;[/font]