Question: Programming in C Chemical Elements Write a program elements.c that loads the chemical elements from a file into a struct array and allows the user

Programming in C

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.

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.

Mulitple Files

In this step, you need to move the functions printElement and initElement in your main() procedure 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 move all the function definitions from elements.c into it.

Modify your main program to use the declarations of elementOps.h and do not leave any duplicate function definitions with those in elements.c

To compile your program, you can use the following command:

gcc o elements elements.c elementOps.c

Struct Array

Declare an array called elementArray having 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 as parameters (1) an input an Element array, (2) the number of elements in the array, and (3) a given 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 findByMass in elementOps.h and elementOps.c, respectively.

Ask the user to enter an atomic mass and use findByMass to find the element with the given mass then print it using printElement() if it is not null. If null, print an error message through stderr that the element with the given mass was not found.

Write a similar function findBySymbol that looks for an element by its given symbol. Ask for and read the symbol from standard input first. Print the element found if it is not null. If null, print a message through stderr that the element with the given symbol 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 again using your makefile. Is it successful? Add the last missing rule to be able to build your program successfully. (Hint: The rule to generate elementOps.o is still missing)

Build your program again using your makefile. 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 using your makefile. What files are regenerated?

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

Now touch elementOps.h, then build your program again. 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!