Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Element Structure

Max
Max 


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 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: ERROR: You must provide exactly one argument to this program. 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. If the number is less than or equal to zero, the program must print this error message and exit: ERROR: You must provide an integer greater than 0 and less than or equal to 20. 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: 11 Sodium Na alkali_metal 22.9898 2 8 1 0 0 0 0 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: --------------- | 11 22.9898 | Na Sodium | 2 8 1 --------------- 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 '\t', then the atomic weight to 4 decimal places. The chemical symbol appears on the next line, followed by a tab character '\t', 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: [esus] $ ./lab9 ERROR: You must provide exactly one argument to this program. [esus] $ ./lab9 21 ERROR: You must provide an integer greater than 0 and less than or equal to 20. [esus] $ ./lab9 0 ERROR: You must provide an integer greater than 0 and less than or equal to 20. [esus] $ ./lab9 -12 ERROR: You must provide an integer greater than 0 and less than or equal to 20. One input: [esus] $ ./lab9 1 11 Sodium Na alkali_metal 22.9898 2 8 1 0 0 0 0 1 total elements. Sodium had the smallest atomic number. Sodium had the largest atomic number. --------------- | 11 22.9898 | Na Sodium | 2 8 1 --------------- Two inputs: [esus] $ ./lab9 2 1 Hydrogen H other_nonmetals 1.008 1 0 0 0 0 0 0 82 Lead Pb post_transition_metal 207.2 2 8 18 32 18 4 0 2 total elements. Hydrogen had the smallest atomic number. Lead had the largest atomic number. --------------- | 1 1.0080 | H Hydrogen | 1 --------------- --------------- | 82 207.2000 | Pb Lead | 2 8 18 32 18 4 --------------- Four inputs: [esus] $ ./lab9 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 --------------- 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. $ c99 -Wall lab9.c -o lab9 NOTE: Make sure you output what is expected, nothing more and nothing less. Resources You should attend the Friday Lab session to seek assistance from the TAs and CAs. For specific questions, attend the weekly lab session or attend the TA's office hours. You are encouraged to use resources or tutorials on the internet to learn unix or C. Check the class resource list for some links to resources. Attachments Tags Clarifications

Answers