Make a loop to calculate molecular weight
조회 수: 4 (최근 30일)
이전 댓글 표시
"Use a while loop to ask the user to enter the elemental symbols of the elements in the compound and store these in a padded character array. The user should be asked if there are any more elements to be added and the loop should continue as long as the user enters an upper or lower case y.
Use a second loop that will prompt the user to enter the atomic mass and number of atoms in the compound for each element. These should be stored in separate arrays."
Please help I can't get it to word.
댓글 수: 2
dpb
2013년 11월 15일
Well, haven't even seen your start at what doesn't work...show that and a specific question may elicit hints. Full solutions are rarely, if ever, forthcoming.
답변 (3개)
Walter Roberson
2013년 11월 17일
more = input('Are there more elements? y/n','s');
while strcmpi(more, 'y')
element{end+1} = input('Enter the atomic symbol: ', 's);
more = input('Are there more elements? y/n','s');
end
댓글 수: 5
Marc
2013년 11월 19일
I have to say that this question becomes redundant when you look at molmass() in the file exchange. The logic used in that function DOES what you are asking the user to input....
So if you want the molecular weight for C6H6 or Pt(NH3)4Cl2.... It figures out the atoms and the amounts, along with "stuff" in parenthesis...
Thus the weight percent becomes trivial...
%C in benzene = 100*6*MolMass('C')/MolMass('C6H6)
Marc
2013년 11월 19일
I really hate doing others homework...
But... This works really well and shows one how to use recursive programming.
Next time try google...
댓글 수: 0
Ngoc Tran
2016년 10월 8일
I just ran into this today and this is the code that I have. I don't use a loop.
Create a container map (similar to Python's dictionary):
key = {'C','H','O'};
value = [12,1,16];
atom = containers.Map(key,value);
molecule = {'C','H','H','H','H'} %CH4
MW = sum(cellfun(@(x) atom(x), molecule))
This will go through every element in the "molecule" list, get the atom mass. You will have an array of atom mass, in this case [12 1 1 1 1], and then sum them up.
To get a more extensive key/value pair you can find .csv files online. I'm just giving an example.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!