Jump to content
  • Advertisement
Sign in to follow this  
Adderly

What Am I Doing Wrong Here?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!