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

This is C programing on X2Go. Please only use and keep it as simple as possible.

ID: 3716784 • Letter: T

Question

This is C programing on X2Go. Please only use and keep it as simple as possible. Explanations along the code would be appreciated. Please dont forget to answer the questions along the directions.

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.

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

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.

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?

Explanation / Answer

elementOps.h

#ifndef ELEMENTSOPS_H

typedef struct{

char name[50];

char symbol[5];

int atomic_number;

int atomic_mass;

} Element;

void printElement(Element *el);

void initElement(Element *el, char *name, char *symbol, int atomic_number, int atomic_mass);

Element *findByMass(Element *el, int n, int mass);

Element *findBySymbol(Element *el, int n, char *symbol);

#endif

elementOps.c

#include<stdio.h>

#include<string.h>

#include "elementOps.h"

void printElement(Element *el){

printf("Name: %s Symbol: %s Atomic Number: %d Atomic Mass: %d ", el->name, el->symbol, el->atomic_number, el->atomic_mass);

}

void initElement(Element *el, char *name, char *symbol, int atomic_number, int atomic_mass){

strcpy(el->name, name);

strcpy(el->symbol, symbol);

el->atomic_number = atomic_number;

el->atomic_mass = atomic_mass;

}

Element *findByMass(Element el[], int n, int mass){

int i=0;

for(i=0; i<n; i++){

if(el[i].atomic_mass==mass)

return (el+i);

}

return NULL;

}

Element *findBySymbol(Element el[], int n, char *symbol){

int i=0;

for(i=0; i<n; i++){

if(strcmp(el[i].symbol,symbol)==0)

return (el+i);

}

return NULL;

}

element.c

#include<stdio.h>

#include "elementOps.h"

int main(){

Element element1;

printf("Name of Element: ");

scanf("%s", element1.name);

printf("Symbol of Element: ");

scanf("%s", element1.symbol);

printf("Atomic Number: ");

scanf("%d", &element1.atomic_number);

printf("Atomic Mass: ");

scanf("%d", &element1.atomic_mass);

printElement(&element1);

Element element2;

initElement(&element2, "Iron", "Fe", 56, 26);

printElement(&element2);

Element elementArray[10];

initElement(&elementArray[0], "Helium", "He", 4, 2);

initElement(&elementArray[1], "Lithium", "Li", 7, 3);

initElement(&elementArray[2], "Oxygen", "O", 16,8);

initElement(&elementArray[3], "Chlorine", "Cl", 35, 17);

initElement(&elementArray[4], "Calcium", "Ca", 40, 20);

Element *searchByMass = findByMass(elementArray, 5, 20);

if(searchByMass==NULL)

printf("the element with the given mass was not found. ");

else

printElement(searchByMass);

char symbol[5];

printf("Enter Element Search symbol: ");

scanf(" %s", symbol);

Element *searchBySymbol = findBySymbol(elementArray, 5, symbol);

if(searchBySymbol==NULL)

printf("the element with the given symbol was not found. ");

else

printElement(searchBySymbol);

return 0;

}

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