Hi Gang! I am new to the forums, and programming in general, so I apologize forthright if I am slow or just plain dense on anything/everything. With that disclaimer gotten outta the way, I have an assignment and I am not sure what exactly how to execute it. The problem is as follows:

Given Atomic Weights:

Oxygen = 15.9994
Carbon = 12.011
Nitrogen = 14.00674
Sulfur = 32.066
Hydrogen = 1.00794

Problem Description:

Write a program that asks the user to enter the number of atoms of each of the five elements for an amino acid. Then compute and print the average weight of the atoms in the amino acid.

So far, my program code consists of asking the user to input the number of atoms for each of the five elements. I also have an obviously wrong formula to find the average of values entered. I am unsure of how to declare the weights, and how to write the formula to find the average weights. My coding is at this point is as follows:

``````#include<stdio.h>
#include<math.h>

int main(void)
{
int O, C, N, S, H ;
double Weight ;

printf("Enter number of atoms for O: \n") ;
scanf("%d", &O);
printf("Enter number of atoms for C: \n") ;
scanf("%d", &C) ;
printf("Enter number of atoms for N: \n") ;
scanf("%d", &N) ;
printf("Enter number of atoms for S: \n") ;
scanf("%d", &S) ;
printf("Enter number of atoms for H: \n") ;
scanf("%d", &H) ;

Weight = (O+C+N+S+H)/5 ;
printf("The average weight of the amino acid: %\n" , (O+C+N+S+H)/5 ) ;

system("PAUSE") ;
return (0) ;
}``````

Any help would be appreciated.

You will need to save the atomic weights for each element. For instance

double OxygenAtom = 15.994

Then compute the total atomic weight for each element depending on the number of atoms, and then the average.

double TotalOxygenAtomWeight = OxygenAtom * O

Then compute TotalAtomicWeight and TotalNumberofAtoms.

Your average weight will be TotalAtomWeight/TotalNumberofAtoms

ie if you have CO2, it will be
(12.011 + (15.9994 * 2))/3

UPDATE:

I have been tinkering around and this is what I have come up with:

``````#include<stdio.h>
#include<math.h>
#define o 15.9994
#define c 12.011
#define n 14.00674
#define s 32.066
#define h 1.00794

int main(void)
{
int O, C, N, S, H ;
double Weight ;

printf("Enter number of atoms for O: \n") ;
scanf("%d", &O);
printf("Enter number of atoms for C: \n") ;
scanf("%d", &C) ;
printf("Enter number of atoms for N: \n") ;
scanf("%d", &N) ;
printf("Enter number of atoms for S: \n") ;
scanf("%d", &S) ;
printf("Enter number of atoms for H: \n") ;
scanf("%d", &H) ;

printf("Total weight of Amino Acid: %lf\n", (O*o)+(C*c)+(N*n)+(S*s)+(H*h) ) ;
printf("The average weight of the amino acid: %lf\n" ,
(O*o)+(C*c)+(N*n)+(S*s)+(H*h)/(O+C+N+S+H) ) ;

system("PAUSE") ;
return (0) ;
}``````

I think I am getting closer to a solution, but something tells me this is really sloppy. In addition, I think I am not quite understanding what I should divide by to find the average weight. Putting in arbitrary values yields these results:

``````Enter number of atoms for O:
3
Enter number of atoms for C:
2
Enter number of atoms for N:
5
Enter number of atoms for S:
1
Enter number of atoms for H:
2
Total weight of Amino Acid: 176.135780
The average weight of the amino acid: 174.274968
Press any key to continue . . .``````

To compute the average atomic weight, you have to compute the total atomic weight and the total number of atoms.

For example :

``````totalAtomicWeight = (O*o)+(C*c)+(N*n)+(S*s)+(H*h);
totalNumberOfAtoms = (O+C+N+S+H);
averageAtomicWeight = totalAtomicWeight/totalNumberOfAtoms;``````

Don't do your calculations inside the printf statement.

Also try giving your variables more meaningful names. for ex: OxygenAtomicWt etc. It will be easier for you or someone else to read and understand your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.