c# bmi calculator NaN issue when I enter weight and height - c#-2.0

I enter the weight and height and end up getting Nan as a result for BMI. Thank you for your help and consideration on this question
double height = 0.0;
double weight = 0.0;
double bmi = height / weight;
double bmi = height / weight;
Console.Write("Enter your height in m:");
height = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter your weight in kg:");
weight = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Your BMI is:{0}", Math.Round(bmi,4));
bmi = Convert.ToDouble(Console.ReadLine());
Console.ReadKey();

You're calculating bmi before height and weight have been assigned. They have their initial value of 0, so you have a divide-by-zero situation, which results in NaN:
double height = 0.0;
double weight = 0.0;
double bmi = height / weight;
bmi doesn't get automatically updated. You need to assign height and weight first, and then calculate bmi.
Console.Write("Enter your height in m:");
double height = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter your weight in kg:");
double weight = Convert.ToDouble(Console.ReadLine());
double bmi = height / weight;
Console.WriteLine("Your BMI is:{0}", Math.Round(bmi,4));

Related

Get area surface and ceiling height with RoomPlan

Can't see it it the doc but, do you know/think it's possible to get the surface of a room in square meter and the ceiling heigh in meter ? Thanks
let length = 4.0 // Length of the room in meters
let width = 5.0 // Width of the room in meters
let height = 2.5 // Height of the room in meters
let surfaceArea = length * width * height
let ceilingHeight = height
print("The surface area of the room is \(surfaceArea) square meters.")
print("The height of the ceiling is \(ceilingHeight) meters.")

How do I convert UISlider value without changing current minimum and maximum slider value

I have 2 sliders, both sliders have a minimum and maximum value set to 0 and 1. In the pic, they simulate changing ramp values at a maximum of 300 total frames combined.
For the first slider, I get the range of 0-150 frames correctly with
int selectedFrameCount = 300;
int currentSelectedFrameValue = sender.value * (selectedFrameCount/2);
I'd like to know how to get a range of 150-300 frames with the second slider without changing the minimum and maximum UISlider values because I need those values for a separate calculation.
For a separate function, I'll need to convert that value back to from selected 150-300 value into a float based on the 0-1 value selected.
example: 285 = x * (300/2) + 150;
Isn't it as simple as
int currentSelectedFrameValue = (sender == firstSlider ? 0 : selectedFrameCount/2)
+ sender.value * (selectedFrameCount/2);
?
You have to imagine your calc as a function (it is, so you haven't to imagine it :D )
y is the int you want
x is the sender.vale
y(x) = x * (selectedFrame/2)
If you want to translate your line on y-axis by selectedFrame/2 you just have to add it to as q value on linear equation y(x) = mx + q
So, the solution is
int value = sender.value * (selectedFrame/2) + (selectedFrame/2)
To convert the int value into a float value in 0-1 range, use this
multipliedMaxValue = selectedFrame
multipliedSelectedValue = value
multipliedMaxValue : 1 = multipliedSelectedValue : x
if you want x, just do
float toRangeValue = (float)multipliedSelectedValue / (float)multipliedMaxValue;

convert Lat/Long to X,Y Coordinates in iOS [duplicate]

I am showing an image in an UIImageView and i'd like to convert coordinates to x/y values so i can show cities on this image.
This is what i tried based on my research:
CGFloat height = mapView.frame.size.height;
CGFloat width = mapView.frame.size.width;
int x = (int) ((width/360.0) * (180 + 8.242493)); // Mainz lon
int y = (int) ((height/180.0) * (90 - 49.993615)); // Mainz lat
NSLog(#"x: %i y: %i", x, y);
PinView *pinView = [[PinView alloc]initPinViewWithPoint:x andY:y];
[self.view addSubview:pinView];
which gives me 167 as x and y=104 but this example should have the values x=73 and y=294.
mapView is my UIImageView, just for clarification.
So my second try was to use the MKMapKit:
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(49.993615, 8.242493);
MKMapPoint point = MKMapPointForCoordinate(coord);
NSLog(#"x is %f and y is %f",point.x,point.y);
But this gives me some really strange values:
x = 140363776.241755 and y is 91045888.536491.
So do you have an idea what i have to do to get this working ?
Thanks so much!
To make this work you need to know 4 pieces of data:
Latitude and longitude of the top left corner of the image.
Latitude and longitude of the bottom right corner of the image.
Width and height of the image (in points).
Latitude and longitude of the data point.
With that info you can do the following:
// These should roughly box Germany - use the actual values appropriate to your image
double minLat = 54.8;
double minLong = 5.5;
double maxLat = 47.2;
double maxLong = 15.1;
// Map image size (in points)
CGSize mapSize = mapView.frame.size;
// Determine the map scale (points per degree)
double xScale = mapSize.width / (maxLong - minLong);
double yScale = mapSize.height / (maxLat - minLat);
// Latitude and longitude of city
double spotLat = 49.993615;
double spotLong = 8.242493;
// position of map image for point
CGFloat x = (spotLong - minLong) * xScale;
CGFloat y = (spotLat - minLat) * yScale;
If x or y are negative or greater than the image's size, then the point is off of the map.
This simple solution assumes the map image uses the basic cylindrical projection (Mercator) where all lines of latitude and longitude are straight lines.
Edit:
To convert an image point back to a coordinate, just reverse the calculation:
double pointLong = pointX / xScale + minLong;
double pointLat = pointY / yScale + minLat;
where pointX and pointY represent a point on the image in screen points. (0, 0) is the top left corner of the image.

How can I operate on a size_t and end up with a CGFloat?

To determine the ratio at which to scale an image, I'm using the following code (borrowed from Trevor Harmon's UIImage+Resize):
CGFloat horizontalRatio = 600 / CGImageGetWidth(imageRef);
CGFloat verticalRatio = 600 / CGImageGetHeight(imageRef);
CGFloat ratio = MAX(horizontalRatio, verticalRatio);
The 600 represents the maximum size I want for the scaled image. CGImageGetWidth and CGImageGetHeight return a size_t which, according to ARC, evaluate to an unsigned long on the iPhone platform (iOS 5).
The problem with the present code is that ratio always evaluates 0.0000.
The width and height of imageRef are actually w=768, h=780, so the ratio should be MAX(0.781, 0.769) = 0.78. How do I this?
P.S. When I used the code above for UIImage's initWithCGImage:scale:orientation: I found that scale works differently than I'd expected: passing in a ratio of 0.78 enlarged the image. Dividing the width or height by the desired size (as in CGImageGetWidth(imageRef) /600, etc.) fixed the problem.
You need one value to be a float to do proper division. Integer division always truncates the floating point numbers. The easiest solution is to turn your numbers into float literals.
CGFloat horizontalRatio = 600.0f / CGImageGetWidth(imageRef);
CGFloat verticalRatio = 600.0f / CGImageGetHeight(imageRef);
CGFloat ratio = MAX(horizontalRatio, verticalRatio);

Arc4random float?

Doing this:
float x = arc4random() % 100;
returns a decent result of a number between 0 and 100.
But doing this:
float x = (arc4random() % 100)/100;
Returns 0. How can I get it to return a float value?
Simply, you are doing integer division, instead of floating point division, so you are just getting a truncated result (.123 is truncated to 0, for example). Try
float x = (arc4random() % 100)/100.0f;
You are dividing an int by an int, which gives an int. You need to cast either to a float:
float x = (arc4random() % 100)/(float)100;
Also see my comment about the modulo operator.
To get a float division instead of an integer division:
float x = arc4random() % 100 / 100.f;
But be careful, using % 100 will only give you a value between 0 and 99, so dividing it by 100.f will only produce a random value between 0.00f and 0.99f.
Better, to get a random float between 0 and 1:
float x = arc4random() % 101 / 100.f;
Even better, to avoid modulus bias:
float x = arc4random_uniform(101) / 100.f;
Alternatively, to avoid a two digits precision bias:
float x = (float)arc4random() / UINT32_MAX;
And in Swift 4.2+, you get built-in support for ranges:
let x = Float.random(in: 0.0...1.0)
In Swift:
Float(arc4random() % 100) / 100

Resources