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
>>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)
>>222513
yeah, it does work with that fix, you could also set x =1, but it only makes it a tiny bit more efficient