Chemistry Empirical formula answer

Hello,

I am a high school student who has gotten interested in C++ language. For exercises I try to write programs for lessons that I learn in class (For ex. for dimensional analysis in chemistry I wrote a unit converter). I have decided that I am going to try writing a formula to change from percent composition to the empirical formula (For anyone who doesn't know what that means http://dl.clackamas.cc.or.us/ch104-04/empirica.htm). I'm planning out the logical progression on paper before I start writing code, and the problem I foresee myself having is this:

As soon as you get the ratio, where the lowest one is 1.00, and say the other is 1.33 - How can the computer figure out what to multiply to simplify it? How can you let the computer know "if its about 1/3, then multiply it by 3, if its about 3/4 multiply it by 4, etc.," Because I think it would be improbable to check it against every possible fraction.

TL;DR How do would you write "if it's 0.33, multiply it by 3" and also how would you say if it was 1.01 or something, round it down and count it as one.
Last edited on
0.333333... == 1/3. You're trying to multiply by the reciprocal of this, so would multiply by 3. The reciprocal of any number n is just 1/n. So in this situation, you would multiply it by 1/(1/3) or as the computer knows, 1/.3333...

and also how would you say if it was 1.01 or something, round it down and count it as zero

Why you would you want to round it down? It's not zero, so why treat it as zero?

EDIT:
Link to WolframAlpha: http://wolfr.am/ZdXjr8
Illustrates better what's going on here. As computers are finite, it will have to round off this number. If you more trailing 3s to the calculation, you will see there will be more 0s immediately following the 3. in the answer. After rounding/truncation, you'll be left with 3. This will work for any number you're trying to get like this.
Last edited on
true. And oops typo I meant to say 1. and vice versa, like having it know that 1.32 is also one third.
Well if you have something like 1:1.33 you could try simply divide by the lone decimal
1
2
(1 : 1.33) / 0.33
=3 : 3.99

1
2
(1 : 2.25) / 0.25
=4 : 9

and then just round. but if you have anything with multiple ratios (such as 1 : 1.2 : 1.3 that could be problematic... also for things that aren't as nice numbers.

1
2
3
4
(1 : 1.2 : 1.3) / 0.2
= 5 : 6 : 6.5
(5 : 6 : 6.5) / 0.3
= 16.66 : 20 : 21.66
Last edited on
Yes good point. I'm trying something easier to begin with, going from the chemical formula to the percent composition. However, I'm having prolems with this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
struct element
{
	string name;
	double weight;
} data;

element elementinfo (string element)
{
	if(element == "H")
	{
		element hydrogen = {"Hydrogen", 1.00794};
		return hydrogen;
	}
	else if(element == "He")
	{
		element helium = {"Helium", 4.002602};
		return helium;
	}
}

int main ()
{
	cout << "Welcome to the percent composition calculator!" << endl;
	string input;
	cin >> input;
	data = elementinfo(input);
	cout << data.weight << endl;
	return 0;
}


this is just some test code to work on structs, but its giving me a huge amount of compiler errors. can you see what's wrong?

EDIT:
IF the header files are important, heres the overlap of the code:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <cmath.h>
using namespace std;

struct element
{
	string name;
	double weight;
} data;
Last edited on
Topic archived. No new replies allowed.