A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.
What Jeff said. The use of the K&R style convention is not prevalent in Java. In Java syntax, aligned brackets make for much clearer logic and readability.
Also exercise1_ArraySum is redundant. It is already known that it's an exercise. Never state what your code already states for you. "ArraySum" would have sufficed.
sum = sum + val[x][/x] should be able to be reduced to sum += val[x][/x].
Moral of the Story: You shouldn't call something easy if you aren't able to do it optimally. This is sort of a "brag" type of thread.
for (int x = 0; x <= val[3]; x++) {
// new lines for open brackets is boss }
I didn't used to like it, but I think it's a necessity now.
EDIT: Also can't you declare all variables of the same type at once in C++?
int[] val = {0, 1, 2, 3};
int sum = 0, x;
for (x = 0; x <= val[3]; x++)
sum+= val[x];
Also x <= val[3]? That seems like an improper coding concept. That should be something more like sizeof(val) / sizeof(int) or however it is C++ handles counting array lengths.[/3][/x][/3][/3]
for (int x = 0; x <= val[3]; x++) {
// new lines for open brackets is boss }
I didn't used to like it, but I think it's a necessity now.
EDIT: Also can't you declare all variables of the same type at once in C++?
int[] val = {0, 1, 2, 3};
int sum = 0, x;
for (x = 0; x <= val[3]; x++)
sum+= val[x];
Also x <= val[3]? That seems like an improper coding concept. That should be something more like sizeof(val) / sizeof(int) or however it is C++ handles counting array lengths.[/3][/x][/3][/3]
As long as nothing will ever be added to the array it's not bad but using a function to get the length is always a good idea.
Yeah, this totally works in this scenario, which is why I mentioned it's an improper coding concept. I think it's weird that a teacher would teach something like that, since it will give the impression to many novice students that arrayName[arraylength] is what you want to use in the loop argument, when what you really want is just arrayLength.[/arraylength]
I would have loved to use a function to get the length of the array, because I've needed that in programs before, but never knew that there was an actual easy way to do it. I'll have to look into it.
@unintelligible:
For the exercise1 thing, it's because I had a large list of exercises I had to do, and if I had just named them "ArraySum" and "2DArray" and all of that, I would forget which exercise is which, and then I might turn in the wrong exercise. That was the only reason for that, but I understand what you mean. I could have done "1_ArraySum" "2_2DArray" etc.
Also, I should've done that instead of the "sum = sum +", I forgot about that. Thank you.
And I wasn't aware that in Java you should be using new lines for brackets, I guess I'll try it haha. I've just always used a same-line first bracket whenever doing C++, so I figured it was the same for Java.
Thanks for the pointers everyone! Also, any chance this could be moved to the programming section? I realize that this is the wrong forum now, and I can't move it myself as I've had all of my powers stripped :/
I would have loved to use a function to get the length of the array, because I've needed that in programs before, but never knew that there was an actual easy way to do it. I'll have to look into it.
@unintelligible:
For the exercise1 thing, it's because I had a large list of exercises I had to do, and if I had just named them "ArraySum" and "2DArray" and all of that, I would forget which exercise is which, and then I might turn in the wrong exercise. That was the only reason for that, but I understand what you mean. I could have done "1_ArraySum" "2_2DArray" etc.
Also, I should've done that instead of the "sum = sum +", I forgot about that. Thank you.
And I wasn't aware that in Java you should be using new lines for brackets, I guess I'll try it haha. I've just always used a same-line first bracket whenever doing C++, so I figured it was the same for Java.
Thanks for the pointers everyone! Also, any chance this could be moved to the programming section? I realize that this is the wrong forum now, and I can't move it myself as I've had all of my powers stripped :/
The junkpile is the section for everything dont be silly
I have a Computer Science class I'm taking at school. It's funny though, it's soooo simple and I'm already way past everyone else. They wont let me test into the Comp Sci AP class either, sucks. I'm apparently already past the AP class too though, according to the teacher.
I have a Computer Science class I'm taking at school. It's funny though, it's soooo simple and I'm already way past everyone else. They wont let me test into the Comp Sci AP class either, sucks. I'm apparently already past the AP class too though, according to the teacher.
Lucky you, I wish they did this in my school, well they do it but its an after school program. What grade are you in? If you don't mind me asking.
That's not a funny thing.
Take them anyway. You'll most likely get college credit classes your senior year.
I took C++/11th-grade-course in 10th grade. My teacher used to shit bricks because I was a better programmer than he was.
I would love to take them, actually. Hell, my teachers are saying I should just sit in at Calc classes at my local college. And my teacher does that too, one time I was showing a friend of mine a program I wrote in Java that let you post whatever high-scores you wanted to an online PacMan game (I reverse-engineered the .swf of the game, and found out it just posted the game name, score, and your player name to a php file. So, my program just posted whatever score I wanted and whatever name I wanted, lol) and he was like "Wow, how did you figure that out?" super impressed and confused that you could interact with the web in Java, lol.
My high school was pretty bad for programming. I've never been taught how to program until I started college and all the professors are pretty surprised I can do some of the stuff and do and how quickly I can do it.