Hello, I am really new into C never had any other experience in coding but now as i started to study I have a course where we start with the language c.
So my task was to make a small calculation of transfering celsius from -40 degree to 70 into fahrenheit without using if and the steps to be +10 degree. For some reason here the multiplication -> ts+ 1* x * faktor isnt working how i want it to, anybody got a good source for me where to find a explanation for my problem?
#Beginner
100 messages · Page 1 of 1 (latest)
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 use !howto ask.
@cloud pasture
Screenshots!
Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!
9 / 5 does integer division, which means it rounds towards 0 and becomes 1
by the way, this would have been better in #1013104018739974194
oh, so I would have to use float for my faktor there?
oh thank you was meant to go there didnt double check the chat
you would change the type of faktor to float or, better, double
and you'd use 9.0 instead of 9
Ah okay thank you 🙌
no problem
@cloud pasture Has your question been resolved? If so, type !solved :)
maybe what I've written is a bit misleading
9.0 or 9. is a double literal
9.0f would be the float literal
but you should use double by default, not float
also one more quesion, if i have a equation like in my code that is -40 + 2 * 10 * 9/5 + 32 do I have to make any () like in math so it doesnt mess anything up?
C has the normal precedence rules ("Punkt vor Strich")
and it's left associative, so 10 * 9 / 5 is the same as (10 * 9) / 5, not 10 * (9 / 5)
All german here?
Nice
🇩🇪
You could also write a function instead of using multiple times printf. But both works
so to come back to this, () can matter: 10 * 9 / 5 is the same as 90 / 5, and 90 is divisible by 5 so that does what you'd expect.
But if you had written 9 / 5 * 10, or 10 * (9 / 5), etc, that would actually compute 9 / 5, and that's just 1 because C does integer division if both arguments are integers
so if one is integer and the other is float ?
then it converts the integer to a floating point number
so after running it with faktor as double it still runs in my terminal that -30 Grad celsius is 298.00 Fahrenheit = ts+ 1 * x * faktor but in my calculations -30.0 +1 * 10.0* 9.0/5.0 +32.0 does not equal that
what's the type of faktor?
double
hmm can you show the code?
as text in 3 backticks, not as a screenshot
like this:
```c
the code
```
ts = -40.0;
float x;
x = 10.0;
double faktor;
faktor = 9.0/5.0 + 32.0;
printf("\nTemperatur in verschiedenen Einheiten :\nIn Grad Celsisus In Fahrenheit");
printf("\n%fGrad Celsius | %.2f Fahrenheit",ts+ 1* x, ts+ 1 * x * faktor); ````
ah that's just because x is 10, and 10.0 * 9.0 / 5.0 is actually an integer
(as in, it still has type double, but the number is an integer)
so that's the correct result for this formula
by the way, float ts should be double ts, and you can initialize a variable right where you create it: ```diff
- float ts;
- ts = -40.0;
- double ts = -40.0;```
ah okay, but how is the code saying 298.00 for -30 degree but if i put it exactly like i did in my code in my calculator its a different number
ah also I don't think this equation is correct
yeah but where
i think the + 32
im trying to figure out what went wrong but i cant see it
you want the factor to be 9.0 / 5.0, and then add +32 to the result
although I think you need to subtract 32, not add it?
ah wait no nevermind
yeah
ah so 32 is just meant to be added to the overall result of my faktor 9/5 and not be part of the faktor?
yup
But what is the -40?
so i would either have to integer 32 or just add it to the end but would that be the same?
Just as example
const double X = 10.0;
const double FAKTOR = 9.0/5.0;
const double ADD = 32.0;
void print(const int number)
{
double grad = number * X;
double fahrenheit = number * X * FAKTOR + ADD;
printf("\n%.2f Grad Celsius %.2f Fahrenheit", grad, fahrenheit);
}
int main()
{
for (int i = 0; i < 12; i++)
{
print(i);
}
}```
beginning of tabelle
^ I think this is a very good point. You should write a function that takes a temparature in celsius and converts it to a temperature in fahrenheit
But then the anwers are wrong?
Or im not getting it
you need to add it at the end
I think they want to have the temperatures -40, -30, -20, etc, celsius
and convert those to fahrenheit
so I would add one function that does the conversion betwen celsius and fahrenheit
it doesn't care about X because that's part of how you calculate the number to convert
-4 - 7 works
Hinweis: Sie dürfen keine Schleifen verwenden. Versuchen Sie, möglichst nicht mit festen Werten zu arbeiten, so dass das Programm flexibel an einer Stelle für Start- und Endwert sowie Schrittweite für die Temperatur abgeändert werden kann.
was zur hölle haha
yeah so that means you should write a conversion function
Yeah then just call the function multiple times
😭
The end is kinda confusing lol
What does he want
Without loops
x = 10 in my code is schrittweite für temp, he wants that can be changed
i i want jumps of + 20 then my code should do that instead of 10 but still be right
yeah and the startwert is also flexible. So it should be fine
my prof wants me to do things he never even spoke about or in the papers he gave us
Normal prof moment
he couldnt even help me in the first place with the code i had first

lol
I had the same issue in the berufsausbildung
Now i study and the profs are competent
all he gave us is a link to wikibooks
nice
i read 1 example and still dont know anything
only c
rip bro
you will see. Dies in memory leakage
nice
You have to do all by yourself. But maybe if u only do C its okay
🙂
ive sit at this simple task for overall 3 hours now i think
fun.
now tomorrow i gotta read something about how to do functions and watch videos but atleast i got help thank you guys👍
np
!solved