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

Objectives Practice program design Practice defining and using structures Practi

ID: 3816710 • Letter: O

Question

Objectives

Practice program design

Practice defining and using structures

Practice using pointers

Practice using arrays

Practice manipulating strings

Overview

You are to write a program that will read in data about elements from the periodic table of elements (via user input), store that data, and finally print it back out in a nicely formatted way.

First, define a structure type element_t to represent one element from the periodic table of elements. Components (variables that the structure will hold) of the structure 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 - Reads in data from keyboard input to populate an instance of an element_t structure. (Using fgets, details below.)

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. If the user does not provide the correct number of arguments (hint: use argc), 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.

NOTE: See Lab 8 for an example of how to convert the input argument to an integer. I also show two other conversion functions below, under the scan_element function.

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. (Details on the scan_elementfunction are below.)

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

Functions

scan_element

This function will read in a line of user input using fgets, and then parse out the specific pieces of information needed to build an element_t struct from that line. It will then return that element_t. The user will enter informatino about an elemnt in a specific format and order:

Atomic number

Name

Chemical symbol

Class

Atomic weight

7 numbers, each representing the number of electrons is a given shell

Each piece of information will be separated by a single space. For example, the following data would be entered by the user to define an element_t structure for Sodium:


You should use the fgets function to read this line in and store it in a char array. You will then need to iterate through the string, assigning each space-separated piece to its proper component of an element_t strucutre.

The strtok function is a perfect fit for the job. Make sure you include <string.h> to have access to it. Here's are the official docs for the function, and here is a walkthrough I made for using it in this lab. Basically, strtok lets you split a string by some delimiter--in this case a space. It will let you iterate through each space-separated "chunk" of the input line.

For each of the chunks, assign it to the proper component of an element_t strucutre. Remember that in some cases you will need to use atoi to convert the string chunk to an integer or atof to convert a string to a double: (Include the <stdlib.h> library to us ethese functions)

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:

Compile & Test

Compile your program using this gcc command. c99 is a shortcut for running gcc -std=c99, which uses the C99 standard instead of the default C89 standard.

Explanation / Answer

#include #include #include #include #include //--------------------------------------------------------------------------- void Table(string Name, double Number, int Group, int Period, string Formula, string Charge, double Mass, int Neutrons, string Special) { Neutrons = (Mass - Number); clrscr(); gotoxy(1,2); cout