

InterviewSolution
1. |
Solve : body mass index calculator? |
Answer» Are you fat? - Try catching NumberFormat exception first, etc. up to Exception, the most general one. That's more of a type cast issue then optimization... I should probably remove exceptions altogether, by using TryParse, actually.heh thanks guys I'll have to download a C# compiler to try out your code BC, it doesn't seem to want to compile with MS Visual C++, maybe I'm doing something wrong........ I know C and C++ are compatible with each other, but I'm NEW to this game don't know how C# fits in.. I could have actually written the the app. much differently, It was a practice assignment out the book I'm reading.. The assignment instructs to write a program that calculates BMI and to use particular things in the program.. For instance, I could have written it without using const, but the assignment called for storing my conversion factors using it.. Also, I could of had the user display TOTAL height in inches only, and saved the trouble of converting the FT entered, into inches, and then adding the total inches together later (necessary for the bmi formula).. I haven't even gotten to the heart of C++ yet (OOP)...that doesn't come for quite a few more chapters.. I myself am a little over weight I did mine the other day and it was 28.something. It's funny, I couldn't tell that my app. was working until I googled my result, because I had no table or chart to compare my result to... I've never calculated my bmi before.. When I start working soon, as I plan, I'm sure I'll loose some weight. On a side note, the following code is an interesting little maneuver I pulled off while messing around. It simply convert inches that you enter, into total FT, with exact numbers of inches left over on the side, in decimal format (if a float type is needed).. It took me a while to figure out how to display the FT and exact inches separately. I was using: INCHES / 12 (for total number of feet, which was assigned to a variable) and a separate expression to extract the remainder of INCHES / 12, which would give me the number of left over inches. I used the modulus operator (%), to extract the remainder and simply assigned that that value a variable and used it for the total left over inches.. The problem was that the modulus operator doesn't work with floating numbers, so if you enter a decimal number in inches, the program output would only be x FT and x inches (with any fractional part discarded) What I did instead to overcome this was use two separate expressions the exact same way (not using the %): 1. INCHES / 12 2. INCHES / 12 - BO //notice how it is in the code Since the INCHES / FT declaration is a long type (not floating) it cuts of any fractional part that is assigned to its variable... When that variable (BO) is plugged into the second expression, what you have left over is a decimal number in FT, which can then be converted back to inches (VO * 12---also assigned a variable), and will display the exact remaining inches including any fractional part (because the 2nd declaration IS a FLOAT type) Although I have to admit, I cannot take credit for all of this, an experienced c++ programmer from the cplusplus.com suggested the INCHES / FT - BO idea... I find it cool how it works, that you count on the long type to drop the decimal part of the output so yo can subtract BO from the second expression.. Note the data types I used... You can enter a pretty big number, up to 64 bit (unsigned long long) in inches and get an exact output in FT , with exact number in inches on the side... Of course you could just enter a number in inches and convert it to a decimal number in FT, but I wanted something that would display the inches on the side... Code: [Select] #include <iostream> using namespace std; int main() { long double variable; const int VOOB = 12; cout << "Enter a height in inches:____ \b\b\b\b\b"; cin >> variable; unsigned long long BO = variable / VOOB; long double VO = variable / VOOB - BO; long double WO = VO * VOOB; cout << "Your equivelent height in FT/INCHES is: " << BO << " FEET, and " << WO << " inches."; cin.get(); cin.get(); return 0; } C# and C++ are quite different (way they work, methods, types, whatevers). =P height: 5'8" lbs: 141.75 waist: around 70 cm (which is 35 cm something like that width) bodyfat: 5% Check out my cool programming wrist: wrist =POh, As far as the double types (in the INCHES to FT/inches converter) , I should have used just "double" instead of long double to be more efficient in saving memory being used.... Long double wouldn't make any difference anyway, becuase of the long long bottleneck, so to speak..errrrrrrr. I'm very picky about making sure I explain myself correctly and I don't think I did in a couple of cases.. These two statements need to be rectified: Quote Note the data types I used... You can enter a pretty big number, up to 64 bit (unsigned long long) in inches and get an exact output in FT , with exact number in inches on the side... Of course you could just enter a number in inches and convert it to a decimal number in FT, but I wanted something that would display the inches on the side.. Quote As far as the double types (in the INCHES to FT/inches converter) , I should have used just "double" instead of long double to be more efficient in saving memory being used.... Long double wouldn't make any difference anyway, becuase of the long long bottleneck, so to speak.. To ensure the largest possible output in the INCHES to FT/INCHES converter app, a long double should be used.. 1. Because you want to be able to display exact inches left over in decimal FORM 2. You want to leave as much room as possible for the LONG LONG type to work to its full potential, yielding the largest number possilbe. Dividing your INCHES entered, by 12, will yield a number far less than the number of "inches" that you type into the keyboard... So, the largest possible output number is a 64bit number.. The input number being long double can potentially be higher than 64bit. I hope that makes sense.. Is it me or does min/max come into mind?I didn't use fractions at all- the formula I used requires input in the form of total number of inches, which means you just add the number of feet * 12 to the number of inches. Also, even if you are describing inches as fractional feet, the number will always be rational and within the range of a float/single. Really it makes more sense to allow feet'Inches" to be input, and then parse those out of the string. C# is a totally different language from C/C++ and uses a different compiler; really, C# is a lot closer to Visual Basic then it is to C/C++.Quote from: BC_Programmer on March 01, 2010, 01:28:42 PM Really it makes more sense to allow feet'Inches" to be input, and then parse those out of the string. yes that does make more sense, I don't think I've read about parsing yet, so I'm not sure how to do it.. I'm sure I'll get there, thoughQuote from: EEVIAC on March 01, 2010, 01:36:53 PM yes that does make more sense, I don't think I've read about parsing yet, so I'm not sure how to do it.. I'm sure I'll get there, though Yeah, it would take me a while to remember how to do that in C/C++... When I think of parsing I immediately think of the VB6 functions that are used for this purpose and have to "translate" from that for the language I'm using. (with .NET it's easier thanks to intellisense, just plop a dot at the end of the string variable for all the methods that can be performed on a string) |
|