Need to change position of decimal point.

jacob_1988

Member
Joined
Mar 3, 2016
Messages
9
Programming Experience
Beginner
I have written a small C# program to calculate height and speed (of degrees) but when I output the code - the numbers don't read correctly (or as my course work intended). Example: 45 degrees should read: 1020.408 but the output is: 0.10204080, moreover, the other example (input speed 100) should read: 255.102 but instead outputs: 0.0255102?

I have updated the code (as below).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace rrr
{
    class Program
    {
        static void Main(string[] args)
        {
            
            //Prompt user for angle in degrees
            Console.Write("Enter initial angle in degrees ");
            float theta = float.Parse(Console.ReadLine());
            Console.WriteLine();

            //Math.PI / 180
            float DtoR = theta * ((float)Math.PI) / 180;

            //Calculate vox using the Math.Cos method
            float cos = (float)Math.Cos(DtoR);

            //Calculate voy using the Math.Sin method
            float sin = (float)Math.Sin(DtoR);

            //Time until shell reaches apex
            float t = sin / (float)9.8;

            //Height of shell apex
            float h = sin * sin / (2 * (float)9.8);

            //Distance shell travels horizontally
            float dx = cos * 2 * sin / (float)9.8;

            //Ouput: Distance of the shell
            Console.WriteLine("Horizontal distance of the shell {0} Meters. \r\n ", dx.ToString("N8"));

            //Prompt user for initial Speed
            Console.Write("Enter initial Speed ");
            float speed = float.Parse(Console.ReadLine());
            Console.WriteLine();

            //Output: distance of the shell
            Console.WriteLine("Vertical distance of the shell {0} Meters. \r\n ", h);
        }

    }
}
 
Last edited by a moderator:
The code you have provided is just displaying the data you give it. If it's not displaying what you want then it's because you're giving it the wrong data. That means that the issue is in the code that is calculating 'dx' and 'h', not in the code that displays those values.

So, first of all, provide us with the actual formulae you're trying to implement, then provide us with your implementation and we can probably work out what you're doing wrong.
 
I've fixed the problem (the alternative code is highlighted in red - as below).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace rrr
{
class Program
{
static void Main(string[] args)
{
//Prompt user for angle in degrees
Console.Write("Enter initial angle in degrees ");
float theta = float.Parse(Console.ReadLine());


//Prompt user for initial Speed
Console.Write("Enter initial Speed ");
float speed = float.Parse(Console.ReadLine());
Console.WriteLine();

//Degree * PI / 180
theta = theta * ((float)Math.PI) / 180;


//Calculate vox using the Math.Cos method
float cos = speed * (float)Math.Cos(theta);

//Calculate voy using the Math.Sin method
float sin = speed * (float)Math.Sin(theta);


//Time until shell reaches apex
float t = sin / (float)9.8;

//Height of shell apex
float h = sin * sin / (2 * (float)9.8);

//Distance shell travels horizontally
float dx = cos * 2 * sin / (float)9.8;

//Ouput: Distance of the shell
Console.WriteLine("Horizontal distance of the shell {0} Meters. ", dx);

//Output: distance of the shell
Console.WriteLine("Vertical distance of the shell {0} Meters. \r\n ", h);
}

}
}
 
Back
Top Bottom