Chemicals Symbol and Atomic Weight
Begin this lab by defining a structure type that is capable of holding information about a chemical element or compound. For the sake of simplicity, we can call it element_t. (For element type.) This type will have a string with a length of 10 characters for the element or compound’s chemical symbol (i.e. Na for Sodium), an integer for the atomic number, and a double for the atomic weight. You will define this type above the main.
Now in the main declare three variables of your new type. Write a C assignment program that fill two variables by asking the user for the information:
Enter the Chemical Symbol: Na
Enter the atomic number: 11
Enter the atomic weight: 22.9897
Do the same for the second element.
Then output your two elements so they look something like this.
Sym: Na
No. 11
Wt. 22.9897
After you have written and tested this little main add to it a function that takes as parameters two element_t variables and which returns the sum of those two. The value returned will also be of type element_t. Remember that you can assign the return value of the function to your third element_t variable in main. The function will have a local variable of element_t type, which you will “construct” in the function by adding the various components of the two elements you have received. (Note you can use strcat, if you #include at the top, to put the names of the elements together.)This local variable will then be returned to the main – just the like the functions we learned about early in the semester.
So the user interaction might look like this:
Enter the first element:
Enter the Chemical Symbol: Na
Enter the atomic number: 11
Enter the atomic weight: 22.9897
Enter the second element:
Enter the Chemical Symbol: Cl
Enter the atomic number: 17
Enter the atomic weight: 35.453
The resulting compound is:
Sym: NaCl
No. 28
Wt. 58.4427
(*User input is shown in bold.)
Once you have this working create a sample run that shows two runs of your program, the first for Sodium Chloride as shown above, and the second for Cadmium Telluride (CdTe).
Solution
#include < stdio.h>
#include < string.h>
/* Hold an element info */
typedef struct element_s
{
char symbol[20];
int number;
double weight;
} element_t;
/* Display element */
void print(element_t element)
{
printf("Sym: %s\n", element.symbol);
printf("No. %d\n", element.number);
printf("Wt. %.4f\n", element.weight);
}
/* Read an element */
element_t read_element()
{
element_t element;
printf("Enter the Chemical Symbol: ");
scanf("%s", element.symbol);
printf("Enter the atomic number: ");
scanf("%d", &element.number);
printf("Enter the atomic weight: ");
scanf("%lf", &element.weight);
return element;
}
/* Entry point of the program */
int main(int argc, char *argv[])
{
element_t el1, el2, el3;
/* Read 2 elements */
printf("Enter first element:");
el1 = read_element();
printf("Enter second element: ");
el2 = read_element();
/* Add them up */
strcpy(el3.symbol, el1.symbol);
strcat(el3.symbol, el2.symbol);
el3.number = el1.number + el2.number;
el3.weight = el1.weight + el2.weight;
printf("The resulting compound is:\n");
print(el3);
return 0;
}
Similar Samples
Browse through our comprehensive selection of programming homework samples at ProgrammingHomeworkHelp.com. Each example demonstrates our expertise in coding, problem-solving, and delivering top-notch solutions across various languages and complexities. These samples serve as a testament to our commitment to excellence and can guide you in understanding our approach to tackling programming challenges effectively
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C