#Why is the sum of the numbers up to the number we entered as the output?

76 messages ยท Page 1 of 1 (latest)

queen creekBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

quiet quest
queen creekBOT
#

@quiet quest

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

quiet quest
#

if we enter 2 as a new variable of N, S should be 2 I think

fallen cliff
#

how come

#

also you should initialize i to 0

hasty tangle
#

hm

#

yeah, what @fallen cliff said

#

also, don't do printf("Enter a number: ", N), that N is irrelevant and at best won't do anything

#

(at worst, it could screw with the stack itself; but virtually every compiler will pass it as a register argument so it wont; but it's possible)

quiet quest
fallen cliff
#

you do until <=

#

that means i stops at 2

#

not 1

quiet quest
#

I didn't understand anything :/

fallen cliff
#

i <= N

hasty tangle
#

i doens't start at 0, it starts at an undefined number

#

since you didnt initialize it, it doesnt start with any particular value

velvet shell
#

Might be wrong, but I am thinking the OP thinks that

int N, i, S = 0;

Initialises all those variables.

quiet quest
#

if we said that N is 2 at first , 0 <= 2 it will work then S is 0 at first and S+=i so S has a new variable which is 1 and in this case i will be 1. After that, there is i++, then the new variable of i is gonna be 2. Am I wrong?

#

and so on

velvet shell
#

@quiet quest
Try

int N, i = 0, S = 0;
hasty tangle
#

because C's syntax is not terrific

#

int N, i, S = 0 is the same as ```c
int N;
int i;
int S = 0;

quiet quest
#

what is the difference from that, int N, i , S = 0;

hasty tangle
#

because int N, i = 0, s = 0 initializes i too

quiet quest
#

oh

#

in this case we determined the value of S only

hasty tangle
#

exactly

#

C's syntax is wonky

quiet quest
#

it's complex

#

I couldn't like it

quiet quest
hasty tangle
#

it doens't

#

the compiler can do whatever it want, and what it's doing is not what you expect, hence the error

quiet quest
#

interesting

#

it is very interesting that there is a rule when the compiler did what ever it wants

quiet quest
hasty tangle
#

just omit , N

quiet quest
#

still it gives us 3 when we say that N is 2

#

how c ome

quiet quest
hasty tangle
#

correct, i think you were just confused there

hasty tangle
velvet shell
#

or:

while (i < N)
hasty tangle
#

either or i think

#

this is where it becomes math and not c errors, and im not great at math lol

velvet shell
#

Let N = 3
i will then increment through [0, 1, 2]

hasty tangle
#

you had the right idea

#

it's just C being a what, 50, 60 year old langauge? fucked you pu

quiet quest
#

I am really confused now

#

๐Ÿ˜„

hasty tangle
#

51 years ago

#

put every declaration on a separate line lol

quiet quest
#

๐Ÿ˜‚

quiet quest
velvet shell
quiet quest
#

3+2+1+0= 6

velvet shell
#

I mean, you said

if we enter 2 as a new variable of N, S should be 2 I think

I am not sure how you arrive at 2.
It would either be 0 + 1 = 1 or 1 + 2 = 3.

quiet quest
#

why 1 + 2?

#

should it be 1 + 1?

velvet shell
#

You need to tell us what the algorithm is.
We've seen the code, but we have no way of knowing if the code fulfills the requirements of your assignment.

quiet quest
#

it comes from my teacher's slide

#

it's just an example

#

damn

#

I forgot to write somethings

#

I'm a fool person sorry guys

#

but still it had to work ๐Ÿ˜„

hasty tangle
#

the fact that they put spaces after the function hurts

velvet shell
#

"until N"
Possibly me, but I find constructs like these ambiguous.
The code is not however.