[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Java Coding Help

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 16
Thread images: 2

File: 8ae.png (18KB, 486x485px) Image search: [Google]
8ae.png
18KB, 486x485px
I just started a new section of my coding class and I'm stumped on the very first one and just get errors. Can anyone provide an explanation? Puzzle: http://codingbat.com/prob/p208692
>>
just tried it and got it working in like 1 minute, this is some basic stuff

what do you have so far that's giving you an error my man
>>
n(n+1)/2
>>
This is the only hint I get..

for (int n = 1; n < 5; n++) {
sum = sum + n;
}
>>
>>222435
that code (provided sum = 0 before the for loop) would sum all numbers up to and including 4.
>>
>>222473
int m2s6_p01(int n) {
for (n = 1; n<6; n++);
sum = sum + n;
}

still not understanding this... or maybe something is wrong?
>>
>>222480
that code (provided sum = 0 before the for loop) would sum all numbers up to and including 5.


hint think about the number 6
>>
>>222480

Why the fuck are you redefining n? n is an argument and you need that to compute the output. Do this instead

int m2s6_p01(int n) {
...int x;
...int sum;
...sum = 0;
...for (x = 0; x < n; x++);
......sum = sum + x;
}
>>
>>222491

It should be x <= n;
>>
>>222491
that still wouldn't produce the desired output, but it is definitely better than what he had.
>>
>>222429
this would work much better than those loops
>>
File: 200011.jpg (36KB, 358x292px) Image search: [Google]
200011.jpg
36KB, 358x292px
>>222482
int sum = 0;
for (n = 1; n <5; n++) {
sum= sum + n;
}
return sum;

this is genuinely killing me rn
>>
>>222504
you gave it configured for only 1 input, change the 5 so you specify what input you want (ie a variable)
>>
>>222493

I'm pretty sure it produces the desired output with my fix here >>222492

Also forgot to "return sum;" but I'm sure he gets the idea.
>>
>>222429
>>222494

return( (n*(n+1))/2 )
this will always work
>>
>>222513
yeah, it does work with that fix, you could also set x =1, but it only makes it a tiny bit more efficient
Thread posts: 16
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.