Question: Chemical Elements Write a program elements.c that loads the chemical elements from a file into a struct array and allows the user to search it

Chemical Elements

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 (ie, 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.

Mulitple Files

In this step, you need to move the functions printElement and initElement to a separate source file:

- Create a header file elementOps.h.

- Protect it using the #ifndef macro definition.

- Add the struct typedef definition to elementOps.h

- Add the prototype of the functions printElement and initElement to it.

- Create the corresponding elementOps.c and add the function definitions to it

- Modify your main program to use the declarations of elementOps.h and remove the duplicate definitions from elements.c

Struct Array

Declare an array 10 Elements and initialize the first 5 elements to the following:

Helium He 4 2

Lithium Li 7 3

Oxygen O 16 8

Chlorine Cl 35 17

Calcium Ca 40 20

Write a function findByMass that takes an input an Element array, the number of elements in the array, and a mass and returns a pointer to the element of the array with the given mass. If no such element is found, the function should return NULL. Declare and define your function in elementOps.h and elementOps.c. Ask the user to enter an atomic mass use findByMass to find the element with the given mass then print it if it is not null. If null, print a message that the element was not found. Write a similar function findBySymbol that looks for an element by its given symbol. Read the symbol from standard input first. Print the element found if it is not null. If null, print a message that the element was not found.

Makefile

Create a makefile to build your program. Add the following rule:

elements: elements.o elementOps.o

gcc -o elements elements.o elementOps.o

Make sure the second line of your rule starts with tab and not spaces. Build your program by executing the makefile: make

Does your program build successfully? The building process needs to know about the dependencies. Add the following rule to your makefile:

elements.o: elements.c elementOps.h

gcc c elements.c

Try to build your program. Is it successful? Add the last missing rule to be able to build your program successfully. (The rule to generate elementOps.o is missing) Build your program again. You should get a message similar to:

make: elements is up to date

Try the following command to simulate modifying elements.o. Remember, the touch command sets the timestamp of the file to the current time.

touch elements.o

Now build your program again. What files are regenerated? Now touch elementOps.c, then build your program. What files are regenerated? Now touch elementOps.h, then build your program. What files are regenerated?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!