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.

Lol, programming homework is easy

2k views · started by 323 ·
#1
Lol, programming homework is easy

Examine the following program:

class Exercise1
{
public static void main ( String[] args )
{
int[] val = {0, 1, 2, 3};

sum =

System.out.println( "Sum of all numbers = " + sum );

}
}
Complete the assignment statement so that it computes the sum of all the numbers in the array.


My answer, I know I could've done it simpler but fuck the police:


class exercise1_ArraySum {
public static void main(String[] args) {
int[] val = {0, 1, 2, 3};
int x = 0;
int sum = 0;

while(x <= val[3]) {
sum = sum + val[x];
x++;
}

System.out.println("Sum of all numbers = " + sum);
}
}
[/x][/3]
#2
For easy homework your code sure is hard to look at.
#3
I think having the brackets unaligned is what bugs me aesthetically.
#4
For easy homework your code sure is hard to look at.


What do you mean? I thought it looked pretty clean, but then again I always do haha. What's wrong with it?
#5
What do you mean? I thought it looked pretty clean, but then again I always do haha. What's wrong with it?


The brackets are unaligned.
#6
What do you mean? I thought it looked pretty clean, but then again I always do haha. What's wrong with it?


Yours

class exercise1_ArraySum {
public static void main(String[] args) {
int[] val = {0, 1, 2, 3};
int x = 0;
int sum = 0;

while(x <= val[3]) {
sum = sum + val[x];
x++;
}

System.out.println("Sum of all numbers = " + sum);
}
}[/x][/3]


What it should look like (not a programmer, but this much I'm pretty sure).
class exercise1_ArraySum 
{
public static void main(String[] args)
{
int[] val = {0, 1, 2, 3};
int x = 0;
int sum = 0;

while(x <= val[3])
{
sum = sum + val[x];
x++;
}

System.out.println("Sum of all numbers = " + sum);
}
}[/x][/3]
#7
Yours

class exercise1_ArraySum {
public static void main(String[] args) {
int[] val = {0, 1, 2, 3};
int x = 0;
int sum = 0;

while(x <= val[3]) {
sum = sum + val[x];
x++;
}

System.out.println("Sum of all numbers = " + sum);
}
}[/x][/3]


What it should look like (not a programmer, but this much I'm pretty sure).
class exercise1_ArraySum 
{
public static void main(String[] args)
{
int[] val = {0, 1, 2, 3};
int x = 0;
int sum = 0;

while(x <= val[3])
{
sum = sum + val[x];
x++;
}

System.out.println("Sum of all numbers = " + sum);
}
}[/x][/3]


Nah, I used to program like that and it looks horrible to me haha. I don't like it at all, sorry.
#8
Nah, I used to program like that and it looks horrible to me haha. I don't like it at all, sorry.


No need to apologize, that's just how it's formatted by the common consensus
#9
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.
#10
>uses a while loop instead of for loop
what

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]
#11
GAMEchief wrote:
>uses a while loop instead of for loop
what

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.
#12
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]
#13
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 :/
#14
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
#15
Oh yeah. Why is this in 'the junkpile.' Mooved.
#16
: l

This is your homework?

Whuttttttttt
#17
Spammathon wrote:
: l

This is your homework?

Whuttttttttt


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.
#18
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.
#19
ya bish wrote:
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.


I'm in highschool haha, they offer the class for all grades at my high school.
#20
I'm in highschool haha, they offer the class for all grades at my high school.


I meant what grade in HS.
#21
9th or 10th
#22
GAMEchief wrote:
9th or 10th


LOL 9th? Nope nope nope.

10th, but I'm suggested for mostly 11th and 12th grade classes. It sucks though, because my parents don't let me take them hahaha.
#23
>hahaha

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.
#24
GAMEchief wrote:
>hahaha

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.
#25
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.