Im sorry for the length of the question, however it looks easy for an expert. QU
ID: 3633421 • Letter: I
Question
Im sorry for the length of the question, however it looks easy for an expert.QUESTION
Calculating molecular weight of a compound and the wt% of each element in the compound.
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 a 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.
Calculate the molecular weight of the compound using the dot product.
Use fprintf to output the molecular weight of the compound.
Calculate the wt% of each element in the compound and store in a new array.
Create a table containing the elemental symbols, the number of the those elements in the compound and the wt% of that element in compound (these should be in columns
Use the disp function to display column headings for the table and the table itself (you will need two commands).
A sample of possible output is given below.
Enter atomic symbols in single quotes.
When the sumbol is a single letter, precede the letter with a space.
Enter the atomic symbol ' K'
Are there more elements? y/n y
Enter the atomic symbol 'Al'
Are there more elements? y/n Y
Enter the atomic symbol 'Si'
Are there more elements? y/n y
Enter the atomic symbol ' O'
Are there more elements? y/n n
For element K
Please enter the weight 39.098
Enter the number of these atoms in the compound 4
For element Al
Please enter the weight 26.982
Enter the number of these atoms in the compound 4
For element Si
Please enter the weight 28.086
Enter the number of these atoms in the compound 1
For element O
Please enter the weight 15.999
Enter the number of these atoms in the compound 8
The total weight is 420.398
symbol weight number wt %
K 39.098 4 0.372009
Al 26.982 4 0.256728
Si 28.086 1 0.0668081
O 15.999 8 0.304454
Explanation / Answer
Note : Save this code to a separate file TestScript.m. Then at matlab commanprompt, type TestScript and press enter. it will start executing it. inputsymbol = []; atomicweights = []; numAtoms = []; while 1>0, inpsymbol = input('Enter the atomic symbol ', 's'); inputsymbol = [inputsymbol; inpsymbol]; continues = input('Are there more elements? y/n ', 's'); if isempty(continues) continues = 'Y'; end if toupper(continues) ~= 'Y' break; end; end; pause; for i = 1:length(inputsymbol), printf('For element ');disp(inputsymbol(i,:)); atomicweight = input('Please enter the weight ', 's'); numberofatoms = input('Enter the number of these atoms in the compound ', 's'); atomicweights =[ atomicweights; atomicweight]; numAtoms =[ numAtoms; numberofatoms]; end; pause; atomicweights = str2double(atomicweights); numAtoms = str2num(numAtoms); vsum = numAtoms .* atomicweights; wtp = vsum./sum(vsum); printf('Total weight is %s ',num2str(sum(vsum))); result = [inputsymbol, num2str(atomicweights), num2str(numAtoms), num2str(wtp)]; disp('symbol weight number wt %'); disp(result);