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

For this lab, you will implement a program that can output information about any

ID: 3823867 • Letter: F

Question

For this lab, you will implement a program that can output information about any element in the periodic table of elements.

Requirements:

Read in a comma-separated values text file that contains the information about the elements in the periodic table.

Save those elements in a data structure for further processing.

Accept command-line parameters:

Program arguments shall be the chemical symbols for wish the user wants more information

Optionally, the user may provide as the first program argument, the name of a file to write the information out to.

Print out, or write to a file, information about the elements requested.

Data structure for elements

You will need to declare a data structure to hold the information about the chemical elements. Your structure will have the following components:

Atomic Number (integer)

Chemical Symbol (string)

Name (string)

Atomic Weight (floating point number)

State at room temp (string)

Bonding type (string)

Year discovered (string)

Incomplete Sample Code

You can download an incomplete sample code, though you don't have to completely follow it: incomplete sample code.

Read in file

First, download the element database file by clicking here: element_db.csv.

Save this file in the same directory as the source code for your lab.

The first thing your program should do is read in the element_db.csv file, line by line. Every line of the file represents one element in the periodic table of elements.

Open the .csv file for reading a text file, and read in every line of the file using the function fgets.

The file in csv format, which means the data fields are separated by commas. Use the function strtok to extract the data field by field.

You can use the function atoi to convert a string to an integer.

The function atof can be used to convert a string to a floating point number.

Save all of the elements scanned in into an array for further processing.

Remember to close the file once you have read in the contents.

Process program arguments

Your program needs to accept one or more command line program arguments. If the program is called with no arguments, it should print this error message and then exit:

Each of the arguments provided to the program should be the chemical symbol of a chemical to look up information about (with one exception, mentioned below).

The program should check the first argument provided to see if it contains a . character. If it does, that argument may be assumed to be a filename, and the program should write the output to that file. You may want to use the function strchr to search for the . character.

For every chemical symbol argument, the program should look up the relevant information about that element.

You can do this by looping through the array of elements, using strcmp to check if the chemical symbol matches the program argument.

When you find a matching element, either print the information to the screen or write it out to the output file, depending if that first argument was a filename or not.

Note that you should be able to use the same function to accomplish this regardless of the target output stream, by using fprintf with the output file pointer or the built-in file pointer stdout.

If no element was found for the input symbol, print a message like this:

(replace [symbol] with the name of the symbol that could not be found).

Print out the element in a similar format to the one used for Lab 9:

Print the atomic number and atomic weight on the top line, separated by a ' ' character.

Print the chemical symbol and name on the next line, separated by a ' ' character.

Print the physical state on the next line

Print the bonding type on the line after that

Print the date discovered on the last line, after the word "Found: ".

Print the top and bottom with the dash - character, and side of the box using the pipe | character.

Remember to close the output file once you have finished processing all of the command-line arguments.

Example Execution

No program arguments

Output to screen: Iron, Silver, Carbon, Potassium

Output to screen: Mercury, Phosphorus, Flourine

Output to file: Lithium, Aluminum, Iodine saved to file output.txt (note: no output is printed to screen).

Content of output.txt:

Program arguments have invalid symbol name:

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef struct
{
int AtomicNo; /*atomic number*/
char* ChemSymbol; /*chemical symbol*/
char* ChemName; /*chemical name*/
float AtomWeight; /*atomic weight*/
char* roomState; /*state at room temp*/
char* Bonding; /*Bonding type*/
char* YearDisc; /*year discovered*/
}ELEMENT;

void printrecord(FILE *fp, ELEMENT *arr, int i) /*print the record*/
{
fprintf(fp,"-------------------- ");
fprintf(fp,"| %d %f ",arr[i].AtomicNo,arr[i].AtomWeight);
fprintf(fp,"| %s %s",arr[i].ChemSymbol,arr[i].ChemName);
fprintf(fp,"| %s ",arr[i].roomState);
fprintf(fp,"| %s ",arr[i].Bonding);
fprintf(fp,"| Found: %s ",arr[i].YearDisc);
fprintf(fp,"-------------------- ");
}
int main(int argc, char* argv[])
{

FILE *fpi,*fpo; /*input and output files*/
char* line; /*each line read from input file*/
char* elems[argc-1]; /*array to hold entered chem symbols*/
ELEMENT arr[118]; /*array to hold read data; there are currently 118 known elements in the periodic table*/
int i,j;
int elemslen;

if(argc<2) /*if no commandline arguments entered*/
{
printf("ERROR: please provide at least one program argument ");
exit(1);
}
if(argc>2) /*if more than one argument has been entered at the command line*/
{
if(strchr(argv[1],'.')!=NULL) /*if the 1st argument contains a dot, it must be the output file name*/
{
if(!(fpo=fopen(argv[1],"w"))==NULL)
{
printf("Unable to open output file, %s, for writing. Exiting...",argv[1]);
exit(1);
}
for(i=2;i<argc;i++) /*copy entered elements into array, elems*/
strcpy(elems[i-2],argv[i]);
}
else/*if no output file specified*/
for(i=1;i<argc;i++) /*copy entered elements into array, elems*/
strcpy(elems[i-1],argv[i]);
}
else /*only one element entered at the commandline*/
strcpy(elems[0],argv[1]);

if((fpi=fopen("element_db.csv","r"))==NULL) /*open the elements file for reading*/
{
printf("Unable to open file. Exiting...");
exit(1);
}
/*read the elements file line by line until EOF*/
i=0;
while(fgets(line,sizeof(line),fpi)!=NULL)
{
arr[i].AtomicNo=atoi(strtok(line,",")); /*read atomic no*/
strcpy(arr[i].ChemSymbol,(strtok(NULL,","))); /*read the chem symbol*/
strcpy(arr[i].ChemName,(strtok(NULL,","))); /*read the chem name*/
arr[i].AtomWeight=atof((strtok(NULL,",")));
strcpy(arr[i].roomState,(strtok(NULL,",")));
strcpy(arr[i].Bonding,(strtok(NULL,",")));
strcpy(arr[i].YearDisc,(strtok(NULL,",")));
i++;
}
fclose(fpi); /*close the elements db file*/

elemslen=sizeof(elems)/sizeof(elems[0]); /* length of the array elems; this is the number of symbols entered at command line*/

if(fpo==NULL)/*if no output file specified, output to stdout*/
fpo=stdout;
/*compare entered elements with saved elements*/
for(i=0;i<elemslen;i++)
{
for(j=0;j<118;j++)
if(strcmp(elems[i],arr[j].ChemSymbol)==0)
break;
if(j<118) /*if element found*/
{
printrecord(stdout,arr,j);
}
else/*if symbol not found in DB*/
{
fprintf(fpo,"WARNING: No such element:[%s] ",elems[i]);
}
} /* end for*/

if(fpo!=NULL && fpo!=stdout) /*close the output file if it exists*/
fclose(fpo);

return 0;
}

Hope this helps!

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