Math formula not working

At one point this formula was working. I haven't messed with it, but I have added more information. More "If"'s and "else"'s.

Just so you know, this is my very first program and it is a homework question for class.

My problem is my second formula keeps giving the same answer of 0.5 and not actually solving the problem. Any help would be greatly appreciated!

Another minor problem, for my first question when the user needs to select 1 or 2, my last else (choosing something other than 1 or 2) works if they choose something less than 1 or greater than 2. But if they type in 1.5 or something it will read it as 1.

Sorry for the formatting, I had to retype everything on here so I hope it looks okay.

#include <iostream>
#include <cmath>
using namespace std;

int main() {
cout << "Would you like to compute the distance between two alleles or determine the recombination fraction between two alleles?" << endl;
cout << "Please enter 1 to compute the distance between two alleles or number 2 to determine the recombination fraction." << endl;

int choice;

cin >> choice;

if (choice == 1)
{
float C_ab, d_ab;

cout << "You have chosen to compute the distance between two alleles." << endl;
cout << "Please enter the known recombination fraction. Note: this must be less than or equal to 0.5" << endl;

cin >> C_ab ;

if (C_ab <= 0.5)
{

d_ab = 50 * log10(1.0 / (1.0 - 2.0 * C_ab));

cout << "The average distance between both alleles is "<< d_ab << " megabases." << endl;

return 0;}
else cout << "Data value is out of range." << endl;
}
else if (choice == 2 )
{
float C_ab, d_ab;

cout << "You have chosen to determine the recombination fraction between two alleles." << endl;
cout << "Please enter the known distance between two alleles. Note: this must be between 0 and 3000 megabases." << endl;

cin >> d_ab;

if (d_ab > 0 && d_ab < 3000){
(
C_ab = (1 / pow(10,d_ab/50)) - 1 / -2;

cout << "The recombination fraction is " <<C_ab << endl;

return 0;}
else cout << "The data value is not in range." << endl;
}
else if (choice !=1 && choice !=2)
{
cout << "You did not read the directions clearly. Please try again." << endl;
}
}


Edit: Found the solution. Edited some parentheses in my formula. Thanks for your help!!
Last edited on
Another minor problem, for my first question when the user needs to select 1 or 2, my last else (choosing something other than 1 or 2) works if they choose something less than 1 or greater than 2. But if they type in 1.5 or something it will read it as 1.


You need to change int choice to float choice, because int only works for whole numbers so if you put 1.5 as you said it converts it to the whole number 1.

You forgot this " here: cout << "Would you like to compute the distance between two alleles or determine the recombination fraction between two alleles? << endl;

Also this
if (choice > 0 && < 3000)
should be like this if (choice > 0 && choice < 3000). The same goes here
else if (choice !=1 && !=2)
.

I am guessing here
if (choice > 0 && < 3000)
you meant C_ab instead of choice.

And finally to use powers of ten you should use pow(10,d_ab) instead of 10^d_ab
Okay so couple of those were just mistakes made typing it in here.

The " in (You forgot this " here: cout << "Would you like to compute the distance between two alleles or determine the recombination fraction between two alleles? << endl; )

And the extra choices in
(if (choice > 0 && < 3000)
should be like this if (choice > 0 && choice < 3000). The same goes here
else if (choice !=1 && !=2))

(I am guessing here
if (choice > 0 && < 3000)
you meant C_ab instead of choice.)
- You are absolutely right (well d_ab). Either way, I fixed that so now when I give d_ab value out of that range it gives the correct error code.

I went ahead and fixed my original post to reflect these mistakes, thanks for pointing them out.

The float choice helped one of my problems! Thanks a lot for that.

However, I'm still getting the 0.5 answer for my second formula problem even when my d_ab is in range. Any ideas on that?

Edit: Also, I fixed the power function as well. Forgot to change that when I typed it in here. (I was copying and old sheet)

Found the solution, thanks!
Last edited on
The code is all correct, maybe you maid a mistake on the formula itself, but I don't know the formula so I can't help you with that unless you give me a link or something with the formula in it.

Sorry
Topic archived. No new replies allowed.