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

I have a code in C i need some help with, here is the question You are to write

ID: 3688312 • Letter: I

Question

I have a code in C i need some help with, here is the question

You are to write a program that will scan in data about elements from the periodic table of elements, and print that data back out in a nicely formatted way.

Define a structure type element_t to represent one element from the periodic table of elements. Components should include:

Atomic number (integer)

Name (string)

Chemical Symbol (string)

Class (string)

Atomic weight (decimal number)

Number of electrons in each shell ( seven-element array of integers )

Implement functions to manipulate this element type:

scan_element - Scans in data from keyboard input to populate an instance of an element_t structure.

print_element - Prints out the information about the element.

Program Details

Define a constant called MAX_ELEMENTS with a value of 20, and an array of element_t structures of that length.

Your program must accept a single command-line argument, which is the number of elements that are to be input using the keyboard. If the user does not provide the correct number of arguments, the program must print this error message and exit:

Convert this argument to an integer, and check that the value provided is greater than 0 and less than MAX_ELEMENTS.

If the number is less than or equal to zero, the program must print this error message and exit:

If the user has provided a valid number, call scan_element that many times, and store each scanned structure in the array of element_t structures.

Once all of the structures have been scanned in, do the following:

Print out the total number of elements scanned in

Print out the name of the element with the smallest atomic number

Print out the name of the element with the largest atomic number

Call print_element on each element scanned in

scan_element

The input data format for the scan_element function should be included on a single line, with each component separated by spaces. The input data will appear in this format and in this order. For example, the following data defines the components of an element_t structure for Sodium:

print_element

This function should print out the information about an element in a nicely formatted way. For example, calling print_element on the sodium element described above would yield this:

The top and bottom of the box are simply a series of dash - characters.

The left side of the box is the pipe | character.

The atomic number is in the upper-left, followed by a tab character ' ', then the atomic weight to 4 decimal places.

The chemical symbol appears on the next line, followed by a tab character ' ', then the element name.

The number of electrons in each shell should appear from left to right, as the last row of the output. NOTE: If the number of electrons for a particular shell is 0, omit that number from the output.

Example Execution

Invalid inputs:

One input:

Two inputs:

Four inputs:

Explanation / Answer

Here is the code and output for the question. Please don't forget to rate the answer if it helped. Thank you very much.

#include <stdio.h>
#include <stdlib.h>
#define MAX_ELEMENTS 20
typedef struct
{
int atomicNo;
char name[20];
char symbol[5];
char class[50];
double atomicWeight;
int electrons[7];
}element_t;

element_t scan_element()
{
element_t ele;
int i;
scanf("%d %s %s %s %lf", &ele.atomicNo, ele.name, ele.symbol, ele.class, &ele.atomicWeight);
for(i = 0; i < 7; ++i)
scanf("%d",&ele.electrons[i]);

return ele;
}

void print_element(element_t e)
{
int i;
printf(" ---------------");
printf(" | %-5d %.4f",e.atomicNo, e.atomicWeight);
printf(" | %-5s %s |",e.symbol, e.name);
for(i = 0; i < 7; ++i)
{
if(e.electrons[i] == 0)
break;
printf(" %d",e.electrons[i]);
}
printf(" ---------------");
}


int main(int argc, char *argv[])
{
int n, i, minIdx, maxIdx;
element_t elems[MAX_ELEMENTS];

if(argc != 2)
{
printf(" ERROR: You must provide exactly one argument to this program. ");
exit(1);
}

//convert the command line argument to int and check for valid range
n = atoi(argv[1]);

if(n <= 0 || n > MAX_ELEMENTS)
{
printf(" ERROR: You must provide an integer greater than 0 and less than or equal to %d ",MAX_ELEMENTS);
exit(1);
}

//read all elements from user
for(i = 0; i < n; ++i)
{
elems[i] = scan_element();
}

minIdx = maxIdx = 0;

//find the elements with minimum and maximum atomic no.
for(i = 0; i < n; ++i)
{
if(elems[i].atomicNo < elems[minIdx].atomicNo)
minIdx = i;
if(elems[i].atomicNo > elems[maxIdx].atomicNo)
maxIdx = i;
}

printf(" %d total elements.",n);
printf(" %s had the smallest atomic number.",elems[minIdx].name);
printf(" %s had the largest atomic number. ",elems[maxIdx].name);

for(i = 0; i < n; ++i)
print_element(elems[i]);
}

output

./a.out 4
6 Carbon C other_nonmetals 12.011 2 4 0 0 0 0 0
13 Aluminum Al post_transition_metals 26.9815 2 8 3 0 0 0 0
26 Iron Fe transition_metals 55.845 2 8 14 2 0 0 0
20 Calcium Ca alkaline_earth_metals 40.078 2 8 8 2 0 0 0 0


4 total elements.
Carbon had the smallest atomic number.
Iron had the largest atomic number.


---------------
| 6 12.0110
| C Carbon
| 2 4
---------------

---------------
| 13 26.9815
| Al Aluminum
| 2 8 3
---------------

---------------
| 26 55.8450
| Fe Iron
| 2 8 14 2
---------------

---------------
| 20 40.0780
| Ca Calcium
| 2 8 8 2
---------------