The discussion focuses on using C++ to calculate the molar mass of organic molecules based on user input. Participants emphasize the importance of understanding the algorithm before coding, particularly how to parse a string to identify elements and their quantities. The need for arrays and switch statements is highlighted, as well as the correct handling of numerical values associated with chemical symbols. Issues with the initial code are pointed out, including incorrect data types and logic errors in loops and conditionals. Overall, the conversation stresses the significance of pseudocode and algorithm development in programming before implementation.
You are not told you HAVE to use them, it is up to you to solve the problem any way suits you.
#3
asz304
107
0
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.
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
asz304
107
0
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.
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.
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.
int moleculeValues [4] = { 12.0107, 15.9994, 1.0079, 14.0067 };
#10
asz304
107
0
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.
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.
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
asz304
107
0
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.
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
asz304
107
0
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.
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
asz304
107
0
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
asz304
107
0
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
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
asz304
107
0
@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".
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
asz304
107
0
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
asz304
107
0
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++ )
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
asz304
107
0
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
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.