C++, where Programming Meets Chemistry.

In summary: C;float molarMass;...organic molecule. #include <iostream>#include <string>#include <cstdlib>using namespace std;/*************************************************************************************************** * * @params: none * * @descript: Asks inputs from the user * * @returns: 0 for succession. * ***************************************************************************************************/int main(){ char C = "12.0107"; char c = "12.0107"; char O = "15.9994"; char H = "1.0079"; char N =
  • #1
asz304
108
0

Homework Statement



http://www.engr.mun.ca/~markevans/engr1020/Eng1020Labs/Assignments/Entries/2010/11/16_Assignment_8__Computers!_Science!.html

The Attempt at a Solution


I'm not sure how to use array in this assignment, and I'm not sure where to use switch statement...

My Code
Code:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

/***************************************************************************************************
 *
 * @params: none
 *
 * @descript: Asks inputs from the user
 *
 * @returns: 0 for succession.
 *
 ***************************************************************************************************/
int main(){
 char C = "12.0107";
 char c = "12.0107";
 char O = "15.9994";
 char H = "1.0079";
 char N = "14.0067";
string molecule;
int index, valueC;
float molarMass;
while ( molecule != "q" || molecule!= "Q"){

	int moleculeValues [4] = { 12.0107, 15.9994, 1.0079, 14.0067 };
	cout << "Enter the organic molecule: ";
	getline (cin, molecule);

	indexC = molecule.find ( 'C', 0 );
	int carbon = atoi (C);

	indexO = molecule.find ( 'O', 0 );
	int oxygen = atoi ( O );

	indexN = molecule.find ( 'N', O );
	int nitrogen = atoi ( N );

molarMass = carbon + oxygen + hydrogen + nitrogen;
}
	return 0;
}

Could you please tell me where to use array? thanks
 
Physics news on Phys.org
  • #2
You are not told you HAVE to use them, it is up to you to solve the problem any way suits you.
 
  • #3
But the only possible way that I can think of is array. That's also what my lab instructor told me to do, since that's the only way ( array ) that I've recently learned from my course.
 
  • #4
Can't say I understand what you mean. You can think of a way of doing the problem using an array, but you don't know where to use it?
 
  • #5
You would probably want to store in the input characters in a string.

Before you start writing code, you should understand how to convert a string of characters into a value that give the molecular weight. For example, if the input string were H2O2q, how would you calculate the molecular weight? If you don't understand how to do this, you absolutely WON'T be able to write code to do it.
 
  • #6
To answer your example,
Code:
char H = "1.0067";
int Hvalue = atoi ( H );

How could I do it if there's a number like 2? do I need to use "find" function to multiply the mass of an element by a number ? Please correct my answer to your example if I'm wrong. What do you mean by "probably want to store in the input characters in a string". I can't see where array can be used in this assignment.

Thanks
 
  • #7
Before you start writing code, you should understand how to convert a string of characters into a value that give the molecular weight.

Your main problem, as I see it, is that you want to write code before you understand what your algorithm needs to be. An old saying amongst programmers is: "The sooner you sit down to the keyboard, the longer it will take you to write your program."

I don't want to see any code! Pretend that all you have to work with is a piece of paper and a pen or pencil. How would you calculate the molecular weight of H2O2?
Same for CH4.
 
  • #8
asz304 said:
char H = "1.0067";

This is almost certainly not what you want to do. It defines a single character (byte) and assigns the result of casting the string literal "1.0067" to it. On most systems this means that it will take the address of the string literal and give you the least significant byte of that pointer.
 
  • #9
P.S. Do these look like integers to you?

int moleculeValues [4] = { 12.0107, 15.9994, 1.0079, 14.0067 };
 
  • #10
Yes, Mark. You're right. I didn't do any "pseudocode" or pre-coding. But to get the molar mass of H2O2, I would normally get the weight of H and multiply it by 2, and get the weight of O and multiply it by 2, then add the product of both elements. I noticed that I didn't include O in my previous post.

@jbunniii
I think I considered them as arrays.
 
  • #11
asz304 said:
Yes, Mark. You're right. I didn't do any "pseudocode" or pre-coding. But to get the molar mass of H2O2, I would normally get the weight of H and multiply it by 2, and get the weight of O and multiply it by 2, then add the product of both elements. I noticed that I didn't include O in my previous post.

@jbunniii
I think I considered them as arrays.

12.0107 is not an integer
12.0107 is not an array
 
  • #12
Float?
 
  • #13
Yes, float or double, depending on how much precision you need.
 
  • #14
asz304 said:
Yes, Mark. You're right. I didn't do any "pseudocode" or pre-coding. But to get the molar mass of H2O2, I would normally get the weight of H and multiply it by 2, and get the weight of O and multiply it by 2, then add the product of both elements. I noticed that I didn't include O in my previous post.
OK, that's a good start. Your formulas will consist of the characters H, C, O, N, and Q (or their lower-case forms h, c, o, n, and q), and optionially an integer that is 2 or larger.

Your algorithm needs to count how many H's, C's, O's, and N's, taking into account repeat factors as in H2O2, which could also be written as HOOH or HO-OH if my dusty chemistry memories are correct. (H2O2 happens to be hydrogen peroxide.) Do you have any ideas on how you might do that? Again, no code.
 
  • #15
I'm not sure if I understood your question, but I think you're asking me the number of a single element in a compound. So, 2 which is after H represents the number of H's in the compound, and 2 after O represents the number O's in the compound.
 
  • #16
Right. So your algorithm needs to take an input string such as H2O2 or HOOH and figure out how many H's, how many O's, how many C's, and how many N's are in a given input string. It needs to be smart enough to figure out that H2 means two H atoms and O2 means two O atoms. It should be smart enough to reproduce the results in the sample runs your instructor gave.

Do you have any ideas of how you might do that? Explain in words with no code, please.
 
  • #17
I'm guessing that H would be a character, and use a function such as "Getvalue" and use loops or incrementation of indexes to find H's.
 
  • #18
Well, H would be a character in the input string, as would O, N, C, Q, 2, 3, 4, ...

The question is, how would you find out how many O's, N's, etc. there are in the string?
 
  • #19
O would also be a character, and use a function such as "Getvalue" and use loops or incrementation of indexes to find O's.
 
  • #20
Sigh...spent my whole day just making minor changes...

Code:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

/***************************************************************************************************
 *
 * @params: none
 *
 * @descript: Asks molecule inputs from the user and the string finds the number of a repeated element
 * 			  in the inputed molecule, and does some calculations then adds the total of the calculations
 * 			  to give the total mass( molar mass ) of the molecule.
 *
 * @returns: 0 for succession.
 *
 ***************************************************************************************************/
int main(){
float C = 12.0107;
float O = 15.9994;
float H = 1.0079;
float N = 14.0067;
string molecule;
int index, valueC;
float carbonTotal, oxygenTotal, hydrogenTotal, nitrogenTotal, molarMass, element ;
int num;	while ( molecule != "q" || molecule!= "Q"){

		cout << "Enter the organic molecule: ";
		getline (cin, molecule);

		for ( int i = 0; i  ; i++ ){

		   organicElement =  molecule [i];

		   case 'C' :
		   case 'c':

			   organicElement = C;

		   case 'H':
		   case 'h':

			    organicElement = H;

		   case 'O':
		   case 'o':

			    organicElement = O;

		   case 'N':
		   case 'n':

			    organicElement = N;		  num = i+1;

		  if ( i++ = num ){
		      organicElement *= num;
		  }
			}
			molarMass = carbonTotal + oxygenTotal + hydrogenTotal + nitrogenTotal;
		}
	return 0;
}

And..I'm not sure with my loop with i.
Code:
for ( int i = 0; i  ; i++ )
What should I do next? if I want an element multiply by a number, which can be seen here
Code:
  if ( i++ = num ){
		      organicElement *= num;
		  }
Again..I'm not sure if it was right.
 
  • #21
What is organicElement? It is not defined anywhere. How does this code even compile?

Your for loop doesn't make any sense, either:

for ( int i = 0; i ; i++ )

This loop will terminate before it even executes the first iteration, because your test expression is simply

i

which is equivalent in this context to

i != 0

which is false on the first iteration.
 
  • #22
P.S. You never set carbonTotal, oxygenTotal, etc. to anything, so the result of this instruction is undefined:

molarMass = carbonTotal + oxygenTotal + hydrogenTotal + nitrogenTotal;
 
  • #23
As I asked before, how would you find out how many O's, N's, etc. there are in the string? Please explain in words, not code! Stop trying to write code before you understand what the code needs to do!
 
  • #24
@jubinni

I know they don't do anything, I just placed them for later.@Mark

I would count the number of O's in the compound. In HOOH or in H2O2 get the number 2 after O. Sorry about my other previous post that included codes + explanation, didn't see that you said "no code".
 
Last edited:
  • #25
You also need to count the C's and N's. By saying "no code" I'm trying to get you to think about what you're going to do before you start writing code.

One of the examples given was glucose, which can be represented as CH2OHCH2OCH2OCH2OCH2OCH=O (according to your instructor). Your program can ignore the = character, but it needs to find the 2's and recognize that they affect the character to the left of each.

It might be useful to have a function that has two array parameters: one for string representing a given chemical, and another that the function sets with the index and value of number digits in the chemical formula. Using CH2OHCH2OCH2OCH2OCH2OCH=O as an example, the function could set an array like this: {0,0,2,0,0,0,0,2, etc.}

This would indicate that at index 2 there is a 2 digit, and at index 7, there is another 2 digit.
 
  • #26
Mark44 said:
It might be useful to have a function that has two array parameters: one for string representing a given chemical, and another that the function sets with the index and value of number digits in the chemical formula. Using CH2OHCH2OCH2OCH2OCH2OCH=O as an example, the function could set an array like this: {0,0,2,0,0,0,0,2, etc.}

This would indicate that at index 2 there is a 2 digit, and at index 7, there is another 2 digit.

So the function that has an array parameter is similar to my code?
Code:
for ( int i = 0; i  ; i++ ){

		   organicElement =  molecule [i];

and the switch statements represents which char the program is dealing with. I noticed that I'm missing a switch function and break; also with "==" in a part of my code.
Code:
 num = i+1;

		  if ( i++ = num ){
		      organicElement *= num;
		  }
So num = i+1 represents the integer after an element such as H2, and 2 is the number while i is the index? am I right? What am I missing? Thanks
 
  • #27
I decided to show you my pseudocode, those marked in red are the stuff that I'm not sure in how to do:

declare and initialize variables (including input string)
while input string is not q or Q
get new input string
reset current molecular weight to 0.0 for each char in current input
if it's one of the recognized elements
then set the current weight to the weight of the recognized element
otherwise if it's a digit
then convert from ASCII to int By atoi function?
Should I subtract or add one from that int ?
otherwise (it wasn't a recognized element and it wasn't a digit) set the current number to 0 (since current number means how many of the current element, I set it to zero since it's not a recognized element)
once outside of the switch-statement that processes the characters of the input string (but not out of the for-loop), take the total molecular weight that I've been adding up, and add the current number times the current weight (e.g. H or C etc.) to it.
output the calculations.Edit: Is this loop better than the previous?
Code:
for ( int i = 0; inputString[i] && tolower( inputString[i] ) != 'q'; i++ )
 
Last edited:
  • #28
asz304 said:
I decided to show you my pseudocode, those marked in red are the stuff that I'm not sure in how to do:

declare and initialize variables (including input string)
There should also be constants for the mol. wt. of one hydrogen atom, one oxygen atom, one carbon, and one nitrogen.
asz304 said:
while input string is not q or Q
get new input string
reset current molecular weight to 0.0 for each char in current input
For each input string there should be only one value of the variable for the total mol. wt. of the compound represented by the input string. There should be individual int variables for hydrogen count, oxygen count, carbon count, and nitrogen count. These variables should be set to zero at the beginning of the while loop. All that you should do with the individual characters in the string is determine which atom it represents and increment the appropriate variable.

Inside the while loop I would have a for loop that looks at each character in the input string.
If the current character is 'C' or 'c', increment the carbon count. Do the same if the character represents hydrogen, oxygen, or nitrogen. Don't worry about the weight until you have gone through all of the characters in the string. Then you can calculate the total mol. wt., which will be (no. of carbons)*carbon_wt + (no. of oxygens)*oxygen_wt + (no. of nitrogens)*nitrogen_wt + (no. of hydrogens)*hydrogen_wt.

In each iteration of the loop you will also need to look at the next character in the array (but be careful - if you're already at the last character in the array, don't try to read the next one). If the next character is a digit '1,', '2', '3',...'0' you will need to see how large is the number following a 'C', 'H', 'O', or 'N'. For example, the array could have a sequence like this: ..., O, 1, 2, H, ... This would mean that there are 12 oxygen atoms, so your program logic will need to parse the 1 and 2 characters and figure out that this means 12, so that your oxygen count is updated accordingly. The atoi() function could be used to pull integer values from a string at a specified position in the string.

You will almost certainly need to use the member functions of the std::string class, such as substr().

If your code is working correctly, after you have examined the last character in the string, the count variables for each of the four elements can be used to calculate the total molecular weight of the compound represented by the string, using the formula a few paragraphs above.
asz304 said:
if it's one of the recognized elements
then set the current weight to the weight of the recognized element
otherwise if it's a digit
then convert from ASCII to int By atoi function?
Should I subtract or add one from that int ?
otherwise (it wasn't a recognized element and it wasn't a digit) set the current number to 0 (since current number means how many of the current element, I set it to zero since it's not a recognized element)
once outside of the switch-statement that processes the characters of the input string (but not out of the for-loop), take the total molecular weight that I've been adding up, and add the current number times the current weight (e.g. H or C etc.) to it.
output the calculations.


Edit: Is this loop better than the previous?
Code:
for ( int i = 0; inputString[i] && tolower( inputString[i] ) != 'q'; i++ )
 
  • #29
Mark44 said:
There should be individual int variables for hydrogen count, oxygen count, carbon count, and nitrogen count. These variables should be set to zero at the beginning of the while loop. All that you should do with the individual characters in the string is determine which atom it represents and increment the appropriate variable.

I can't get how the loop will look like, or how do I determine which atom it represents? does that mean I need one loop for each element? And if I have the for loop how will my program store the number that it got from the first element and going to it's next similar element and adding them up
Code:
for ( int hydrogen_count = 0; hydrogen_count = 'H && 'h'; i++ );
          getValh = ...;

Mark44 said:
Inside the while loop I would have a for loop that looks at each character in the input string.
If the current character is 'C' or 'c', increment the carbon count. Do the same if the character represents hydrogen, oxygen, or nitrogen. Don't worry about the weight until you have gone through all of the characters in the string. Then you can calculate the total mol. wt., which will be (no. of carbons)*carbon_wt + (no. of oxygens)*oxygen_wt + (no. of nitrogens)*nitrogen_wt + (no. of hydrogens)*hydrogen_wt.
Code:
for ( int i = 0; i = 'C' || 'c'; i++ );
        what to do here?  and what do I do so that it stores the number of C's that my loops looks at the string Ex. COOHC 2

Thanks
 
Last edited:
  • #30
In post #28, I have described one possible solution, and given you some ideas for program structure. It's not my responsibility to write your code for you - that's what you need to do.
 
  • #31
I appreciate your great effort for helping me in my last assignment, I compiled everything and they worked fine except for the example "C12". Thanks again
 
  • #32
Check what atoi() does.

Calling it chemistry is completely off, especially the idea of treating upper case and lower case interchangeably. Co and CO are two completely different things. I know it is not your fault; it doesn't make the assignment any better.
 
  • #33
I passed my assignment online, I was almost done but decided to ignore inputs such as C12. But my atoi works fine for the other inputs.
 
  • #34
Suppose you're at index 3 of a string and find that the character C is there. I.e., str[3] == 'C'. If the characters '1' and '2' follow 'C', you can get them with this expression: atoi(&str[4]).

&str[4] is the address of the character at index 4 of the string. atoi would take any numeric characters starting at index 4, and convert them to an int that it returns.
 
  • #35
Shameless ad plug: if you want to see (reasonably) well designed and well written program that does real analysis of chemical formulas, go to my site (www.chembuddy.com), download EBAS, install it (risk free trial), start, click on "%" icon on the toolbar and enter any formula.

Note that the list of elements is editable - you can add moieties like "Acetate" so that your formulas are more readable (far easier to understand NaAcetate than NaC2H3O2). The real elements (those present in periodic table) are protected against edition. But it is not because they are treated differently internally, it required additional coding to prevent them from being modified.
 

1. What is C++?

C++ is a high-level programming language that is widely used in the field of computer science. It was developed in the 1980s by Bjarne Stroustrup as an extension of the C language. C++ is known for its efficiency, versatility, and ability to support both high-level and low-level programming.

2. How is C++ used in chemistry?

C++ is often used in chemistry for data analysis, simulation, and modeling. It allows for the creation of complex algorithms and data structures, making it a valuable tool for chemists in their research and analysis.

3. What are some key features of C++?

C++ has many features that make it a popular choice for programming, including its object-oriented design, strong type system, and support for generic programming. It also has a large standard library and is highly portable, making it a versatile language for various applications.

4. How does C++ compare to other programming languages?

C++ is often compared to other high-level languages such as Java and Python. While these languages are also widely used, C++ is known for its speed and efficiency, making it a popular choice for applications that require high performance. It also offers more control over memory management compared to languages like Java.

5. Is C++ difficult to learn?

C++ can be challenging to learn, especially for beginners. It has a steep learning curve and requires a strong understanding of computer science concepts such as pointers and memory management. However, with practice and dedication, it can be a powerful language to master for both programming and chemistry applications.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Back
Top