Question I really need help with some assignments

Bip#6693

New member
Joined
Mar 4, 2022
Messages
2
Programming Experience
Beginner
I recently started learning C# at my uni , it's my first semester , fast forward 3 weeks later , I'm pretty lost , I try learn more about it online but it still doesn't help me with my hw ,
If someone could give me a hand it would really be appreciated .
Here's my discord : Bip#6693
 
You need to to schedule time in your teacher's office hours to to talk to them. They may offer you some suggestions regarding some tutoring with TAs or other resources available at your school.

We will not do your homework for you. You can post your questions here, show us your code, and tell us what specific problem you are running into. We can guide you at that point.
 
You need to to schedule time in your teacher's office hours to to talk to them. They may offer you some suggestions regarding some tutoring with TAs or other resources available at your school.

We will not do your homework for you. You can post your questions here, show us your code, and tell us what specific problem you are running into. We can guide you at that point.
Thanks for the advices , I'm not looking for straight up answers or to copy paste , I would just get screw in the long run if I would do it.
It's more small bit of code that won't do what I except them to do even though , it should (from what I've read). Like here the output isn't right .
It won't get divided by 5 , I've tried different thing , I just don't understand why , it's weird.
C#:
using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {

        

        double temp = Convert.ToDouble(Console.ReadLine());
        double ans = (temp * 9/ 5 + 35);
        //Console.WriteLine(Math.Round(ans));
        Console.WriteLine(ans);
      


    }

}


//input
//25
//output
//- 3.89


//input
//81
//output
//27.22
 
Try changing the 5 to 5.0

Then after that read about the difference between integer division and floating point division.
 
I get the correct result and the expression produces a double, but add 32 instead of 35 if you want to convert C to F. Paranthesis is not needed around the whole expression.
C#:
var ans = temp * 9 / 5 + 32; //temp is double
Here you can read about operator precedence and associativity, and operand evaluation: C# operators and expressions - C# reference
In short
  • multiplicative before additive (* and / before +)
  • associativity left to right (here * before /)
If the data types are different they need to be converted, implicit conversion happens if that is possible, else you get a compiler error and can apply an explicit conversion if available. Here implicit conversion exists for int to double, and explicit conversion from double to int, so when you have a double/int or int/double expression the int is converted to double implicitly. In implicit conversions the value is converted without losing precision, for example int 9 to double 9.0. In explicit conversions precision may be lost, for example double 9.5 to int 9. See Conversions - C# language specification

So the expression is evaluated like this reduced down to result:
C#:
((double * int) / int) + int
(double / int) + int
double + int
double
As you can see ints 9/5 is never evaluated and the case of integer division is not an issue. 9 is converted to double and multiplied with temp producing a new double, before that is divided by 5, again implicitly converting 5 to double first. Then 32 is converted to double and added to previous result.
 
I didn't even see the 35. My brain kept on reading it as 32.
 
Back
Top Bottom