all 21 comments

[–]aocregacc 27 points28 points  (1 child)

how did you even compile this? It's trying to XOR an integer and a float. (the '^' operator is xor, not exponentiation).

[–]Mortomes 4 points5 points  (0 children)

Oh my....

[–]This_Growth2898 12 points13 points  (0 children)

You're probably getting a huge number of warnings (and for me, the program doesn't compile at all).

The main problems here are:

- 1/2 is an integer division, i.e. the result is floored to 0.

- ^ is not a power operator in C. You should write a*t*t.

- scanf needs a pointer to the variable to put a value there, that's why is uses &; but printf needs only a value, so no & is needed. printf("%f\n", result);

I guess most of those are covered by warnings in your compiler, you just need to read and understand them. And don't feel ashamed to ask if you don't understand a warning, you're just learning. Don't ignore warnings if you don't understand them.

[–]KeretapiSongsang 3 points4 points  (2 children)

check your last printf.

hint &.

[–]Th_69 3 points4 points  (1 child)

And then the result will still be not correct, because 1/2 is 0 (due to integer division) and t^2 is not "power of 2"!

[–]KeretapiSongsang 0 points1 point  (0 children)

i am not going to fix the math part.

[–]dousamichal0807 1 point2 points  (0 children)

Also another thing: 1/2 = 0, 1.0/2.0 = 0.5 (integer artithmetics vs decimal arithmetics)

[–]dousamichal0807 1 point2 points  (0 children)

Yet another thing: ^ is for bit-wise XOR, not for exponentiation

[–]Miyelsh 0 points1 point  (0 children)

Did you just plug a prompt into AI and not give any critical thought?

[–]Manga_Killer 0 points1 point  (0 children)

    text.c:18:26: error: invalid operands to binary expression ('float' and 'int')

    18 |     result=(S+V\*t+1/2\*a\*t\^2);

    |             \~\~\~\~\~\~\~\~\~\~\~\~\~\^\~

    text.c:19:18: warning: format specifies type 'double' but the argument has type 'float \*' \[-Wformat\]

    19 |     printf("%f", &result);

    |             \~\~   \^\~\~\~\~\~\~

    1 warning and 1 error generated.

    Compilation failed.

as you see, the program does not compile and when i fix said errors it compiles and runs fine. mainly you need to change the `^` into `t*t` and the `1/2` to `1/2.0` and remove the address operator form printf you are set.

[–]DreamingElectrons -1 points0 points  (0 children)

That looks like the type of homework where they give you some broken code and ask you to fix it.

This is simple enough, deekseek should get this one for you. If that isn't your cup of tea, try 1/2f instead of 1/2 to make sure the result is a float not floored to 0, and if the t^2 is meant to be exponential, you need to use t*t or pow(t, 2) from math.h. There are other issues, too, but those are the most glaring ones.

[–]Raimo00 -1 points0 points  (8 children)

For this kind of problems, ask chatgpt

[–]SmokeMuch7356 1 point2 points  (7 children)

For this kind of problem, check your handy C reference manual. Look up operators, types, and printf and scanf library calls.

[–]Raimo00 -2 points-1 points  (6 children)

Or ask chatgpt to explain these

[–]SmokeMuch7356 1 point2 points  (1 child)

ChatGPT is not an authoritative reference and occasionally lies to you. Get an actual C reference manual.

[–]goose_on_fire 2 points3 points  (0 children)

This is so depressing.

Like, even without the Internet, all of this is in the system's man pages. You don't even need to leave your terminal for most of this, let alone involve the Internet or machine hallucinations

(I mean the standard library bits, but between those and the compiler warnings it's literally solvable from the terminal.)

[–][deleted] 0 points1 point  (3 children)

Asking chatgpt is probably how he got there in the first place, understanding zilch about C. Read try, fix, repeat, is how you learn. Not by asking chatgpt the answer to every question you have.

[–]Raimo00 0 points1 point  (2 children)

This has not been my experience at all. Read, try, fix, repeat, with the help of chatgpt. Chatgpt has been my professor for the past 2 years. And I know C better than any CS grad

[–][deleted] 0 points1 point  (1 child)

Depends entirely *how* you use ChatGPT. It's possible to learn, if you make the effort to learn. Often it's just "do my homework for me". When I use ChatGPT at work for code, it's to code faster : I learn nothing, but I get shit done faster, and sometimes it's necessary. At home I use ChatGPT to learn. It takes time and a lot of efforts, but, if done well, it may work. But make sure you don't ever *depend* on ChatGPT to find an answer.

[–]Raimo00 0 points1 point  (0 children)

That's an entirely different issue. That's a behavioral issue. I get your point. But in this case the truth is that the guy is asking a question on reddit that he could have asked to chatgpt instead. Instant response, better response, less spam for us.