Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Chemical Elements-- Programming in C Write a program elements.c that loads the c

ID: 3714808 • Letter: C

Question

Chemical Elements-- Programming in C

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.

In the main() procedure, do the following:

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 the typedef keyword. An example of the properties stored is as follows:

Name: Aluminum

Symbol: Al

Atomic Number: 13

Atomic Mass: 26

Declare a variable (object) called element1 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 (e.g., element1) as parameter and prints it member variables in the format above.

Declare another element called element2.

Write a function initElement() that takes as parameters
(1) a pointer to an element (e.g., element2) as parameter,
(2) a string as the name of the element,
(3) a string as the symbol of the element,
(4) an integer as the atomic number,
(5) an integer as the atomic mass, and
store the corresponding four member values into the element.
Use the function initElement() to initialize its members with iron (Iron, Fe, 56, 26).

After initializing each element, print it using the printElement() function.

Explanation / Answer

Solution:

c program is:-

#include<stdio.h>   //for input and output
#include<string.h>   //for string operations
typedef struct   //defination of structure for elements
{
   char name[30];   //for element name
   char symbol[5];   // for symbol
   int atomic_number;   //for atomic number
   int mass;   //for atomic mass
}element;   // name for structure

void printElement(element e)   //printing function
{
   printf(" Name: %s ",e.name);   //printing name
   printf("Symbol: %s ",e.symbol);   //printing symbol
   printf("Atomic Number: %d ",e.atomic_number);   //printing atomic number
   printf("Atomic Mass: %d ",e.mass);   //printing mass
}

element initElement(element &o)   //function for initilizing elements reads pointer of empty element
{
   char name[30];   //for name
   char symbol[5];   //for symbol
   int atomic_number;   //for atomic number
   int mass;   //for mass
   printf(" Enter Name of Element: ");
   scanf("%s",&name);   //reading name from user
   strcpy(o.name,name);   //assigning name from user
   printf("Enter Symbol of %s: ",name);
   scanf("%s",&symbol);   //reading symbol from user
   strcpy(o.symbol,symbol);   //assigning symbol from user
   printf("Enter Atomic Number of %s: ",name);
   scanf("%d",&atomic_number);   //reading atomic number from user
   o.atomic_number=atomic_number;   //assigning atomic number from user
   printf("Enter Atomic Mass of %s: ",name);
   scanf("%d",&mass);   //reading mass from user
   o.mass=mass;   //assigning mass from user
   return o;   //returns filled element
}

int main()
{
   FILE *fPtr;   //file pointer
   element e,o,f[118];   //declaring elements
   int i=0,n;
  
   if ((fPtr = fopen("elements.txt", "r")) == NULL)   //checking ile exist or not
   {
       puts("cant open file to read");   //if file not exist or opened print error and return
       return 0;
   }
  
   while (!feof(fPtr))   //read opened file until eof
   {
       fscanf(fPtr, "%s%s%d%d", &f[i].name, &f[i].symbol, &f[i].atomic_number, &f[i].mass);   //storing values to structure
       i++;
   }
   fclose(fPtr);   //close opened file
  
   n=i;
   printf("Total %d Elements are read from file",n);
   printf(" Printing file Elements...");   //printing elements from file
   for(i=0;i<n;i++)
   {
       printf(" Element %d is",i+1);
       printElement(f[i]);
   }
  
   //reading user defined elements
   printf(" *************Elements from user input***************");
   printf(" Enter Name of Element: ");
   scanf("%s",&e.name);
   printf("Enter Symbol of %s: ",e.name);
   scanf("%s",&e.symbol);
   printf("Enter Atomic Number of %s: ",e.name);
   scanf("%d",&e.atomic_number);
   printf("Enter Atomic Mass of %s: ",e.name);
   scanf("%d",&e.mass);
   printElement(e);   //printing user defined element
  
   printf(" Initializing empty Element...");
   //initilize empty element
   strcpy( o.name, "");
    strcpy( o.symbol, " ");
    o.atomic_number=0;
   o.mass=0;
   printElement(o);   //printing empty element
   initElement(o);       //passing empty element
   printElement(o);   //printing of filled element
   return 0;
}


elements.txt is:-

Hydrogen H 1 1
Helium He 2 4
Lithium Li 3 6
Beryllium Be 4 9

OutPut is:--
Total 4 Elements are read from file
Printing file Elements...
Element 1 is
Name: Hydrogen
Symbol: H
Atomic Number: 1
Atomic Mass: 1

Element 2 is
Name: Helium
Symbol: He
Atomic Number: 2
Atomic Mass: 4

Element 3 is
Name: Lithium
Symbol: Li
Atomic Number: 3
Atomic Mass: 6

Element 4 is
Name: Beryllium
Symbol: Be
Atomic Number: 4
Atomic Mass: 9

*************Elements from user input***************
Enter Name of Element: Aluminum
Enter Symbol of Aluminum: Al
Enter Atomic Number of Aluminum: 13
Enter Atomic Mass of Aluminum: 26

Name: Aluminum
Symbol: Al
Atomic Number: 13
Atomic Mass: 26

Initializing empty Element...
Name:
Symbol:
Atomic Number: 0
Atomic Mass: 0


Enter Name of Element: Boron
Enter Symbol of Boron: B
Enter Atomic Number of Boron: 5
Enter Atomic Mass of Boron: 10

Name: Boron
Symbol: B
Atomic Number: 5
Atomic Mass: 10


Process exited normally.
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote