My answer, I know I could've done it simpler but fuck the police:Code: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.
Code: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); } }
Results 1 to 25 of 25
- 18 Feb. 2013 04:59pm #1
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 3.33
Lol, programming homework is easy
- 18 Feb. 2013 05:58pm #2
For easy homework your code sure is hard to look at.
- 18 Feb. 2013 06:00pm #3
I think having the brackets unaligned is what bugs me aesthetically.
- 18 Feb. 2013 06:17pm #4
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 0.78
- 18 Feb. 2013 06:21pm #5Ya Bish
__________Contributions-
[How to make a FMP] • [FLP Guide] • [Gaia Gold FLP] • [Exchanging Guide]
[My Store] • [My Forum]
- 18 Feb. 2013 06:23pm #6
Yours
Code: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); } }
Code: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); } }
- 18 Feb. 2013 07:08pm #7
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 0.74
- 18 Feb. 2013 07:16pm #8
- 18 Feb. 2013 07:18pm #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] should be able to be reduced to sum += val[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.
- 18 Feb. 2013 07:35pm #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.
- 18 Feb. 2013 07:43pm #11
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
- 18 Feb. 2013 07:45pm #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.
- 18 Feb. 2013 08:05pm #13
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 5.65
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 :/
- 18 Feb. 2013 10:00pm #14
- 18 Feb. 2013 10:35pm #15
Oh yeah. Why is this in 'the junkpile.' Mooved.
- 19 Feb. 2013 03:29am #16
: l
This is your homework?
Whuttttttttt
- 19 Feb. 2013 07:24pm #17
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 1.62
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.
- 19 Feb. 2013 07:39pm #18Ya Bish
__________Contributions-
[How to make a FMP] • [FLP Guide] • [Gaia Gold FLP] • [Exchanging Guide]
[My Store] • [My Forum]
- 19 Feb. 2013 08:27pm #19
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 0.64
- 19 Feb. 2013 08:33pm #20Ya Bish
__________Contributions-
[How to make a FMP] • [FLP Guide] • [Gaia Gold FLP] • [Exchanging Guide]
[My Store] • [My Forum]
- 20 Feb. 2013 04:38am #21
9th or 10th
- 20 Feb. 2013 05:28pm #22
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 1.03
- 20 Feb. 2013 05:55pm #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.
- 20 Feb. 2013 09:56pm #24
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 3.46
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.
- 20 Feb. 2013 10:01pm #25
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96