Write a program elements.c that loads the chemical elements from a file into a s
ID: 3821048 • Letter: W
Question
Write a program elements.c that loads the chemical elements from a file into a struct array and allows the user to search it and edit it. First, declare a struct to store properties of chemical elements. Include the following members: name, symbol, atomic number, atomic mass. Define it as a type called Element using typedef keyword. An example of the properties stored is as follows: Name: Aluminum Symbol: Al Atomic Number: 13 Atomic Mass: 26 Declare a variable of type Element and initialize all its members from standard input (i.e, ask the user to enter each value). Write a function printElement that takes an element as parameter and prints it member variable in the format above. Write a function initElement that takes a pointer to an Element as parameter, two strings and two integers and stores the strings as the name and symbol of the element and the integers as its atomic number and mass. Declare another Element and use the function initElement to initialize its members with values of your choice. After initializing each element, print it using printElement function.Explanation / Answer
#include <stdio.h>
typedef struct {
char *name;
char *symbol;
int atomic_number;
int atomic_mass;
} element;
/* This function is for taking input from user written for the requirement of the question*/
void enterElement(element item){
printf("Enter name: ");
scanf("%s", item.name);
printf("Enter symbol: ");
scanf("%s", item.symbol);
printf("Enter atomic number: ");
scanf("%d", item.atomic_number);
printf("Enter atomic mass: ");
scanf("%d", item.atomic_mass)
}
void printElement(element item){
printf("%s %s","Name:", item.name);
printf("%s %s","Symbol:", item.symbol);
printf("%s %d","Atomic Number:", item.atomic_number);
printf("%s %d","Atomic Mass:", item.atomic_mass);
}
void initElement(element *item, char name[], char symbol[], int atomic_number, int atomic_mass){
item->name = name;
item->symbol = symbol;
item->atomic_number = atomic_number;
item->atomic_mass = atomic_mass;
}
void main(){
element data[10], item1, item2;
FILE *fp;
int i, choice;
char *name;
int found;
/* The below code is as required in the question.declaring one element and taking input from user, declaring
second element and initializing it with your choice */
/*--------------------------------------------------------------------*/
enterElement(item1);
initElement(&item2,"Almunium", "Al",13,26);
printElement(item2);
/*---------------------------------------------------------------------*/
fp = fopen("input.txt", "r");
if (fp == NULL){
printf("File not found ");
return;
}
/* Assuming there are 10 entries in the file and all the entries are in one line and the order is
Name, Symbol, atomic number, atomic mass*/
for (i=0; i<10; i++)
{
fscanf(fp, "%s %s %d %d", element[i].name, element[i].symbol, &element[i].atomic_number, &element[i].atomic_mass);
}
fclose(fp);
while (choice !=3){
printf("1.Search an element ");
printf("2.Edit an element ");
printf("3.Quit ");
printf("Enter your choice: ");
scanf("%d",&choice);
switch (choice) {
case 1:
printf("Enter element Name: ");
scanf("%s", name);
found = 0;
for (i=0; i<10; i++){
if (*(element[i].name) == *name){
printElement(*element[i]);
found = 1;
}
}
if (found == 0)
printf("Element Not found ");
case 2:
printf("Enter element Name: ");
scanf("%s", name);
found = 0;
for (i=0; i<10; i++){
if (*(element[i].name) == *name){
printf("Enter symbol: ");
scanf("%s", element[i].symbol);
printf("Enter atomic number: ");
scanf("%d", element[i].atomic_number);
printf("Enter atomic mass: ");
scanf("%d", element[i].atomic_mass)
found = 1;
}
}
if (found == 0)
printf("Element Not found ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.