Calculate Molecular Weight in C

Calculate Molecular Weight in C

Post by spencedj on Thu Aug 21, 2014 6:22 pm
([msg=82903]see Calculate Molecular Weight in C[/msg])

Hello everyone! :)

First, I should state that I am recently new to C programming. I have dabbled with Python in the past, but nothing formal. I am taking a C programming class, but it is an introductory course. As such, the instructor is moving extremely slow through the material (which I understand), but I would like to start utilizing the information to help me in other areas.

I am a chemistry major, and as such I tend to calculate molecular weights very often. It would help me a great deal to create a program to do it for me.

I expect the user entered molecular compound to be something like this: CH3COOH (An equivalent entry could be C2H4O2)
Also, it needs to be case sensitive because the chemical formulas are case sensitive.

The output will be "The molecular weight of CH3COOH is xxx.xxxxx grams"


I have not gotten to the actual code, and will work through that when it comes time. For now, I would like to write out my intentions on paper.

I have a list of all chemical symbols in one file and the associated atomic weights in another. My intention is to read each character of the user entered data into an array, and read each line of each file into two respective arrays as well. Then compare the user input to the chemical symbol array, find the same location in the molecular weight array and store that value in a variable. Then, I will need to multiply that variable by the following integer (if one is present). Finally, I will need to add all of the products together and display the result. To be honest, I do not know if this is the best way to accomplish this task.

There are several problems that I foresee:
The input data length is unknown.
I need to be able to separate letters and numbers in the user entered data.

For example:
CH3COOH -> [C, H, 3, C, O, O, H]

(abbreviated array sizes)
Chemical Symbols Array = [C, H, O]
Atomic Weight Array = [12.0107, 1.00794, 15.9994]

I guess, I should actually ask a question. What is the best process to accomplish my overall goal? For the time being, please refrain from recommending specific codes. I want to establish a base process before actually diving into the program code.

Thank you very much :)
spencedj
New User
New User
 
Posts: 5
Joined: Thu Jan 17, 2013 7:15 am
Blog: View Blog (0)


Re: Calculate Molecular Weight in C

Post by pretentious on Thu Aug 21, 2014 6:45 pm
([msg=82905]see Re: Calculate Molecular Weight in C[/msg])

spencedj wrote:
I guess, I should actually ask a question. What is the best process to accomplish my overall goal? For the time being, please refrain from recommending specific codes. I want to establish a base process before actually diving into the program code.

I actually would love to read your post again when i'm not running on fumes, Programming is really fun when you actually have something to make your life easier as a motivation.

C is essentially, devide and conquore. Go through everything you need to do and break it up into smaller parts and then break those parts up again until, your left with something you can be completed in simple steps.

Organize your different steps and ideas into files to keep things neat and make life easier for you once the project gets big. In my oppinion, C can sometimes be a pain in the ass when you have 50 seperate files, you have to keep track of, only to tell the preprocessor to just copy/paste them into a single executable anyway, but if you have a single function that you know works, you shouldn't risk messing with it again.

I would also reccomend using version control of some sort. It might be daunting for a newby to handle something like git, but once you get your head around it, it really makes you feel like you have control over your project. You have a history of what you have done in the past which is nice but most importantly, if you fuck something up, you can restore to a version that worked.

Also, comment and in general document your work. For some things i'm going to invest time into, I will create a wiki, just to doodle ideas and give a general overview of what I want and where I'm at
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
Can you say brainwashing It's a non stop disco
User avatar
pretentious
Addict
Addict
 
Posts: 1203
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: Calculate Molecular Weight in C

Post by spencedj on Fri Aug 22, 2014 11:32 am
([msg=82909]see Re: Calculate Molecular Weight in C[/msg])

pretentious wrote:
spencedj wrote:I guess, I should actually ask a question. What is the best process to accomplish my overall goal? For the time being, please refrain from recommending specific codes. I want to establish a base process before actually diving into the program code.
I actually would love to read your post again when i'm not running on fumes, Programming is really fun when you actually have something to make your life easier as a motivation.
I love working with computers, and learning to program has always been something of a hobby. I am looking forward to getting into it more through the class I am taking, as well as, other courses in the future.
pretentious wrote:C is essentially, devide and conquore. Go through everything you need to do and break it up into smaller parts and then break those parts up again until, your left with something you can be completed in simple steps.
That is my overall goal right now. Everything is still in the planning phase. Unfortunately, I am not yet familiar enough with C to know what the language can and cannot do. For example, I know that I can create either a char array or a int array, but I can't mix the two. I don't know what similar limitations I have yet.
pretentious wrote:Organize your different steps and ideas into files to keep things neat and make life easier for you once the project gets big.
So, do you mean break coding up into separate files, or draw data from from various files?[quote="pretentious]I would also reccomend using version control of some sort. It might be daunting for a newby to handle something like git, but once you get your head around it, it really makes you feel like you have control over your project. You have a history of what you have done in the past which is nice but most importantly, if you fuck something up, you can restore to a version that worked.[/quote]To be honest, I have absolutely no idea how to implement version control. Is it simply saving a copy of the file that I know works before trying to update the code?


Thank you very much for your response. I'm still working through some of the ideas, but your post helped point me in the right direction to find answers to some of those questions.
spencedj
New User
New User
 
Posts: 5
Joined: Thu Jan 17, 2013 7:15 am
Blog: View Blog (0)


Re: Calculate Molecular Weight in C

Post by pretentious on Sat Aug 23, 2014 10:20 am
([msg=82916]see Re: Calculate Molecular Weight in C[/msg])

spencedj wrote:I am not yet familiar enough with C to know what the language can and cannot do. For example, I know that I can create either a char array or a int array, but I can't mix the two. I don't know what similar limitations I have yet.

That's pretty much what learning a language is all about. You figure out what you can and can not do and learn to efficiently translate a real world concept into software.
spencedj wrote:
pretentious wrote:Organize your different steps and ideas into files to keep things neat and make life easier for you once the project gets big.
So, do you mean break coding up into separate files, or draw data from from various files?

Yeah put related things (several functions that work on a specific, related thing) into seperate files to keep everything structured.
spencedj wrote:To be honest, I have absolutely no idea how to implement version control. Is it simply saving a copy of the file that I know works before trying to update the code?

That's exactly why I reccomended it, because it's not that :P
That's what I used to do and it's messy and hard to keep track of.
If your project is small, you probably wont need it but I thought i'd mention it to put a seed in your mind for when you delete a block of code, thinking it's bad and then essentially rewrite the same thing again, and think to yourself, there must be a better way ;)
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
Can you say brainwashing It's a non stop disco
User avatar
pretentious
Addict
Addict
 
Posts: 1203
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: Calculate Molecular Weight in C

Post by cyberdrain on Sat Aug 23, 2014 11:48 am
([msg=82926]see Re: Calculate Molecular Weight in C[/msg])

spencedj wrote:There are several problems that I foresee:
The input data length is unknown.
I need to be able to separate letters and numbers in the user entered data.

I don't have any practical C programming experience, but my solution to these 2 would be a dynamic buffer for the data and a bunch of if statements/cases/type casting or checking (does that work in C?) going through the array. I recently did a similar project in Python, dictionaries (hashtables) are pretty useful in this case. I wonder how you would parse something like CoCl2·6H2O, as you need the · or the fact that the number comes before the molecule to identify it as a whole set, instead of it being part of the previous Cl. It's a bit challenging, but not too hard first project, I'd say :)
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: Calculate Molecular Weight in C

Post by pretentious on Mon Aug 25, 2014 12:04 pm
([msg=82972]see Re: Calculate Molecular Weight in C[/msg])

For the first one, I guess you'll need to reinvent the wheel unfortunately and use dynamic memory allocation with null characters or some other deliminator.

For the second one. I think c has a built in isdigit ()
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
Can you say brainwashing It's a non stop disco
User avatar
pretentious
Addict
Addict
 
Posts: 1203
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: Calculate Molecular Weight in C

Post by cyberdrain on Mon Aug 25, 2014 3:09 pm
([msg=82978]see Re: Calculate Molecular Weight in C[/msg])

Probably, but there's still the difference between 6H2O being that the 2 is part of the H, while the 6 is part of the H2O. Maybe fuzzy logic needs to be applied there or you have to indicate what times what somehow...
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests