#Beginner

100 messages · Page 1 of 1 (latest)

cloud pasture
#

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?

slow zephyrBOT
#

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!

barren cradle
#

9 / 5 does integer division, which means it rounds towards 0 and becomes 1

#

by the way, this would have been better in #1013104018739974194

cloud pasture
#

oh, so I would have to use float for my faktor there?

cloud pasture
barren cradle
#

you would change the type of faktor to float or, better, double

#

and you'd use 9.0 instead of 9

cloud pasture
#

Ah okay thank you 🙌

barren cradle
#

no problem

slow zephyrBOT
#

@cloud pasture Has your question been resolved? If so, type !solved :)

barren cradle
#

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

cloud pasture
#

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?

barren cradle
#

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)

echo python
#

All german here?

#

Nice

#

🇩🇪

#

You could also write a function instead of using multiple times printf. But both works

barren cradle
cloud pasture
#

so if one is integer and the other is float ?

barren cradle
#

then it converts the integer to a floating point number

cloud pasture
#

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

barren cradle
#

what's the type of faktor?

cloud pasture
#

double

barren cradle
#

hmm can you show the code?

#

as text in 3 backticks, not as a screenshot

#

like this:
```c
the code
```

cloud pasture
#
    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); ````
barren cradle
#

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;```
cloud pasture
#

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

barren cradle
#

ah also I don't think this equation is correct

cloud pasture
#

yeah but where

echo python
#

i think the + 32

cloud pasture
#

im trying to figure out what went wrong but i cant see it

barren cradle
#

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

echo python
barren cradle
#

yeah

cloud pasture
#

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?

barren cradle
#

yup

echo python
#

But what is the -40?

cloud pasture
#

so i would either have to integer 32 or just add it to the end but would that be the same?

echo python
#

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);
    }

}```
cloud pasture
barren cradle
echo python
#

Or im not getting it

barren cradle
#

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

echo python
#

-4 - 7 works

cloud pasture
#

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.

barren cradle
#

yeah so that means you should write a conversion function

echo python
#

Yeah then just call the function multiple times

cloud pasture
#

😭

echo python
#

What does he want

#

Without loops

cloud pasture
#

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

echo python
#

yeah and the startwert is also flexible. So it should be fine

cloud pasture
#

my prof wants me to do things he never even spoke about or in the papers he gave us

echo python
#

Normal prof moment

cloud pasture
#

he couldnt even help me in the first place with the code i had first

echo python
#

lol

#

I had the same issue in the berufsausbildung

#

Now i study and the profs are competent

cloud pasture
#

all he gave us is a link to wikibooks

echo python
#

nice

cloud pasture
#

i read 1 example and still dont know anything

echo python
#

Maybe use other resources then?

#

But u also do c++ or only c

cloud pasture
#

only c

echo python
#

rip bro

cloud pasture
#

why hahah

#

its old right?

echo python
#

you will see. Dies in memory leakage

cloud pasture
#

nice

echo python
#

You have to do all by yourself. But maybe if u only do C its okay

cloud pasture
#

🙂

echo python
#

Its fun 🫠

#

(I should also study)

cloud pasture
#

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👍

echo python
#

np

cloud pasture
#

!solved