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

Bookmark Periodic table of elements Objectives Learn how to read from and write

ID: 3824842 • Letter: B

Question

Bookmark

Periodic table of elements

Objectives

Learn how to read from and write to files

Practice using data structures

Practice top-down program design

Practice re-using code

Overview

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:

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.

NOTE: Make sure you output what is expected, nothing more and nothing less.

******incomplete sample code*****

*******element_db.csv*******

1 H Hydrogen 1.00794 gas diatomic 1766 2 He Helium 4.002602 gas atomic 1868 3 Li Lithium 6.941 solid metallic 1817 4 Be Beryllium 9.012182 solid metallic 1798 5 B Boron 10.811 solid covalent network 1807 6 C Carbon 12.0107 solid covalent network Ancient 7 N Nitrogen 14.0067 gas diatomic 1772 8 O Oxygen 15.9994 gas diatomic 1774 9 F Fluorine 18.9984032 gas atomic 1670 10 Ne Neon 20.1797 gas atomic 1898 11 Na Sodium 22.98976928 solid metallic 1807 12 Mg Magnesium 24.305 solid metallic 1808 13 Al Aluminum 26.9815386 solid metallic Ancient 14 Si Silicon 28.0855 solid metallic 1854 15 P Phosphorus 30.973762 solid covalent network 1669 16 S Sulfur 32.065 solid covalent network Ancient 17 Cl Chlorine 35.453 gas covalent network 1774 18 Ar Argon 39.948 gas atomic 1894 19 K Potassium 39.0983 solid metallic 1807 20 Ca Calcium 40.078 solid metallic Ancient 21 Sc Scandium 44.955912 solid metallic 1876 22 Ti Titanium 47.867 solid metallic 1791 23 V Vanadium 50.9415 solid metallic 1803 24 Cr Chromium 51.9961 solid metallic Ancient 25 Mn Manganese 54.938045 solid metallic 1774 26 Fe Iron 55.845 solid metallic Ancient 27 Co Cobalt 58.933195 solid metallic Ancient 28 Ni Nickel 58.6934 solid metallic 1751 29 Cu Copper 63.546 solid metallic Ancient 30 Zn Zinc 65.38 solid metallic 1746 31 Ga Gallium 69.723 solid metallic 1875 32 Ge Germanium 72.64 solid metallic 1886 33 As Arsenic 74.9216 solid metallic Ancient 34 Se Selenium 78.96 solid metallic 1817 35 Br Bromine 79.904 liquid covalent network 1826 36 Kr Krypton 83.798 gas atomic 1898 37 Rb Rubidium 85.4678 solid metallic 1861 38 Sr Strontium 87.62 solid metallic 1790 39 Y Yttrium 88.90585 solid metallic 1794 40 Zr Zirconium 91.224 solid metallic 1789 41 Nb Niobium 92.90638 solid metallic 1801 42 Mo Molybdenum 95.96 solid metallic 1778 43 Tc Technetium 98 solid metallic 1937 44 Ru Ruthenium 101.07 solid metallic 1827 45 Rh Rhodium 102.9055 solid metallic 1803 46 Pd Palladium 106.42 solid metallic 1803 47 Ag Silver 107.8682 solid metallic Ancient 48 Cd Cadmium 112.411 solid metallic 1817 49 In Indium 114.818 solid metallic 1863 50 Sn Tin 118.71 solid metallic Ancient 51 Sb Antimony 121.76 solid metallic Ancient 52 Te Tellurium 127.6 solid metallic 1782 53 I Iodine 126.90447 solid covalent network 1811 54 Xe Xenon 131.293 gas atomic 1898 55 Cs Cesium 132.9054519 solid metallic 1860 56 Ba Barium 137.327 solid metallic 1808 57 La Lanthanum 138.90547 solid metallic 1839 58 Ce Cerium 140.116 solid metallic 1803 59 Pr Praseodymium 140.90765 solid metallic 1885 60 Nd Neodymium 144.242 solid metallic 1885 61 Pm Promethium 145 solid metallic 1947 62 Sm Samarium 150.36 solid metallic 1853 63 Eu Europium 151.964 solid metallic 1901 64 Gd Gadolinium 157.25 solid metallic 1880 65 Tb Terbium 158.92535 solid metallic 1843 66 Dy Dysprosium 162.5 solid metallic 1886 67 Ho Holmium 164.93032 solid metallic 1878 68 Er Erbium 167.259 solid metallic 1842 69 Tm Thulium 168.93421 solid metallic 1879 70 Yb Ytterbium 173.054 solid metallic 1878 71 Lu Lutetium 174.9668 solid metallic 1907 72 Hf Hafnium 178.49 solid metallic 1923 73 Ta Tantalum 180.94788 solid metallic 1802 74 W Tungsten 183.84 solid metallic 1783 75 Re Rhenium 186.207 solid metallic 1925 76 Os Osmium 190.23 solid metallic 1803 77 Ir Iridium 192.217 solid metallic 1803 78 Pt Platinum 195.084 solid metallic Ancient 79 Au Gold 196.966569 solid metallic Ancient 80 Hg Mercury 200.59 liquid metallic Ancient 81 Tl Thallium 204.3833 solid metallic 1861 82 Pb Lead 207.2 solid metallic Ancient 83 Bi Bismuth 208.9804 solid metallic Ancient 84 Po Polonium 209 solid metallic 1898 85 At Astatine 210 solid covalent network 1940 86 Rn Radon 222 gas atomic 1900 87 Fr Francium 223 solid metallic 1939 88 Ra Radium 226 solid metallic 1898 89 Ac Actinium 227 solid metallic 1899 90 Th Thorium 232.03806 solid metallic 1828 91 Pa Protactinium 231.03588 solid metallic 1913 92 U Uranium 238.02891 solid metallic 1789 93 Np Neptunium 237 solid metallic 1940 94 Pu Plutonium 244 solid metallic 1940 95 Am Americium 243 solid metallic 1944 96 Cm Curium 247 solid metallic 1944 97 Bk Berkelium 247 solid metallic 1949 98 Cf Californium 251 solid metallic 1950 99 Es Einsteinium 252 solid 1952 100 Fm Fermium 257 1952 101 Md Mendelevium 258 1955 102 No Nobelium 259 1957 103 Lr Lawrencium 262 1961 104 Rf Rutherfordium 267 1969 105 Db Dubnium 268 1967 106 Sg Seaborgium 271 1974 107 Bh Bohrium 272 1976 108 Hs Hassium 270 1984 109 Mt Meitnerium 276 1982 110 Ds Darmstadtium 281 1994 111 Rg Roentgenium 280 1994 112 Cn Copernicium 285 1996 113 Uut Ununtrium 284 2003 114 Uuq Ununquadium 289 1998 115 Uup Ununpentium 288 2003 116 Uuh Ununhexium 293 2000 117 Uus Ununseptium 294 2010 118 Uuo Ununoctium 294 2002

Explanation / Answer

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct element{
       char name[20];
       char sb[5];
       int atm;
       float atms;
       char block;
       char atc[20];
       char prop[250];
   }p,q;
int rw,cl;
FILE*fp;
void add();
void explor();
void print();
void mainscreen();
void main()
{
   int a,i,n,y;
   char c,d;
   char date1[15],date2[15],string1[20];
   unsigned int tsz;

   clrscr();
   mainscreen();
   label1:


   textcolor(15);
   gotoxy(22,15);textcolor(14);
   cprintf("Enter the corresponding no");gotoxy(22,19);textcolor(10);
   cprintf("1.Add new Element Information");gotoxy(22,21);
   cprintf("2.Explore");gotoxy(22,23);
   cprintf("3.Quit");gotoxy(22,25);
//   cprintf("4.Delete the records");gotoxy(22,27);
//   cprintf("5.Exit from the program");gotoxy(25,30);
   fflush(stdin);
   d=getch();
   switch(d)
   {
       case '1':
       {
           add();
       //   getch();
       //   print();
       //   getch();
           break;
       }
       case '2':
       {
           explor();
           break;

           }
       case '3':
       {
           clrscr();
           mainscreen();
           textcolor(14); gotoxy(30,24);
           cprintf("THANK U");gotoxy(30,26);
       //   cprintf("SAVING UR SETTINGS"); gotoxy(30,28);
           cprintf("BYE...........");
           getch();
           exit(1);
           break;
           }

       default:
       {
           clrscr();
           mainscreen();
           textcolor(12+128);gotoxy(22,11);
           cprintf("Wrong choice");gotoxy(22,13);textcolor(15);
           cprintf("Retype choice");
           goto label1;
           }

   }
   clrscr();
   mainscreen();
   goto label1;
}
void mainscreen()
{
   int i,j;
   clrscr();
   for(i=2,j=2;i<rw;j++)
   {
       if(j>15)
       j=2;

       textcolor(j);
       gotoxy(i,2);
       cprintf("%c",'*');
       gotoxy(i,cl-1);
       cprintf("%c",'*');
       i++;

   }
   for(i=2,j=2;i<cl;i++,j++)
   {
       if(j>15)
       j=2;
       textcolor(j);
       gotoxy(2,i);
       cprintf("%c",'*');
       gotoxy(rw-1,i);
       cprintf("%c",'*');

   }
   gotoxy(30,4);textcolor(3);
   cprintf("Modern Periodic Table");
   gotoxy(37,6);textcolor(6);
   cprintf("Digital");
   gotoxy(35,7); textcolor(15);
   cprintf("-----------");
}
void add()
{
   char ch;
   label1:
   clrscr();
   mainscreen();
       gotoxy(15,14);textcolor(10);
   cprintf("Enter the Information of Elements:");
//   printf(" %d",l);
   gotoxy(15,16);
   cprintf("Name:");

   gotoxy(15,18);
   cprintf("Symbol:");

   gotoxy(15,20);
   cprintf("Atomic No: ");

   gotoxy(15,22);
   cprintf("Atomic Wt: ");

   gotoxy(15,24);
   cprintf("Atomic Config:");

   gotoxy(15,26);
   cprintf("Block:");

   gotoxy(15,28);
   cprintf("Properties:");


   textcolor(15);
   fflush(stdin);
   gotoxy(20,16);
   scanf("%[^ ]",p.name);
   p.name[0]=toupper(p.name[0]);

   fflush(stdin);
   gotoxy(23,18);
   scanf("%[^ ]",p.sb);
   p.sb[0]=toupper(p.sb[0]);

   fflush(stdin);
   gotoxy(25,20);
   scanf("%d",&p.atm);

   fflush(stdin);
   gotoxy(25,22);
   scanf("%f",&p.atms);

   fflush(stdin);
   gotoxy(29,24);
   scanf("%[^ ]",p.atc);

   fflush(stdin);
   gotoxy(21,26);
   scanf("%c",&p.block);
   p.block=toupper(p.block);
   if(p.block!='S'&&p.block!='P'&&p.block!='D'&&p.block!='F')
   p.block=' ';
   fflush(stdin);

   gotoxy(26,28);
   scanf("%[^ ]",p.prop);
   p.prop[0]=toupper(p.prop[0]);

   /*   gotoxy(9,30);
   cprintf("(In Short)");

   gotoxy(5,33);
   cprintf("Reff. Specialist no:"); */
   if((fp=fopen("data","ab+"))==NULL)
   {
       printf("Cannot open the file f1");
       getch();
       exit(1);
   }
   fwrite(&p,sizeof(p),1,fp);
   fclose(fp);
   printf(" Enter 'y' for next record(y/n):");
   ch=getch();
   if(ch=='y')
   {
       goto label1;
   }

//   getch();
}
void explor()
{
   char d,c;
   FILE *f;
   int given_atmic_no,a,i,tsz,n;
   float given_atmic_mass;
   int flag;
   char string[20];
   startofexplore:
   clrscr();
   mainscreen();
   label6:
   gotoxy(22,15);textcolor(12);
   cprintf("Enter the corresponding no");gotoxy(22,19);textcolor(3);
   cprintf("1.Search by 'NAME'");gotoxy(22,21);
   cprintf("2.Search by SYMBOL");gotoxy(22,23);
   cprintf("3.Search by ATOMIC NUMBER");gotoxy(22,25);
   cprintf("4.Search by ATOMIC WEIGHT");gotoxy(22,27);
   cprintf("5.Elements of Diff. Blocks");gotoxy(22,29);
   cprintf("6.Return to main menu");
   gotoxy(25,32);
   fflush(stdin);
   d=getch();
   switch(d)
{
       case '1':
       {
           clrscr();
           mainscreen();
           gotoxy(15,25);
           textcolor(12);
           cprintf("Enter the Name of Element:");
           textcolor(3);
           fflush(stdin);
           scanf("%[^ ]",string);
           printf("%s",string);
           string[0]=toupper(string[0]);
           if((fp=fopen("data","rb+"))==NULL)
           {
           clrscr();

           printf(" cannot open the record file 1");
           getch();
           exit(1);
           }
           flag=1;
           while(fread(&p,sizeof(p),1,fp))
           {
               if(strcmp(p.name,string)==0)
               {
                   print();

                   flag=0;

                   break;
               }

           }
           if(flag==1)
           {
               clrscr();
               mainscreen();
               gotoxy(25,25);
               textcolor(12);
               cprintf("::No Element Available::");

           }

           fclose(fp);
           getch();
           break;
       }
       case '2':
       {
           clrscr();
           mainscreen();
           gotoxy(22,15);
           textcolor(12);
           cprintf("Enter the symbol:");
           textcolor(3);
           fflush(stdin);
           scanf("%[^ ]",string);
           printf("%s",string);
           string[0]=toupper(string[0]);
           if((fp=fopen("data","rb+"))==NULL)
           {
           clrscr();

           printf(" cannot open the record file 1");
           getch();
           exit(1);
           }
           flag=1;
           while(fread(&p,sizeof(p),1,fp))
           {
               if(strcmp(p.sb,string)==0)
               {
                   print();
                   flag=0;


                   break;
               }

           }
           if(flag==1)
           {
               clrscr();
               mainscreen();
               gotoxy(25,25);
               textcolor(12);
               cprintf("::No Element Available::");

           }

           fclose(fp);
           getch();
           break;
       }
       case '6':
       {
           return;
       }
       case '3':
       {
           clrscr();
           mainscreen();
           gotoxy(15,25);
           textcolor(12);
           cprintf("Enter the Atomic No. Element:");
           textcolor(3);
           fflush(stdin);
           scanf("%d",&given_atmic_no);
           if((fp=fopen("data","rb+"))==NULL)
           {
           clrscr();

           printf(" cannot open the record file 1");
           getch();
           exit(1);
           }
           flag=1;
           while(fread(&p,sizeof(p),1,fp))
           {
               if(p.atm==given_atmic_no)
               {
                   print();
                   flag=0;

                   break;
               }

           }
           if(flag==1)
           {
               clrscr();
               mainscreen();
               gotoxy(25,25);
               textcolor(12);
               cprintf("::No Element Available::");

           }

           fclose(fp);
           getch();
           break;
       }
       case '4':
       {
           clrscr();
           mainscreen();
           gotoxy(15,22);
           textcolor(12);
           cprintf("Enter the Atomic mass of Element:");
           textcolor(3);
           fflush(stdin);
           scanf("%f",&given_atmic_mass);
           if((fp=fopen("data","rb+"))==NULL)
           {
           clrscr();

           printf(" cannot open the record file 1");
           getch();
           exit(1);
           }
           flag=1;
           while(fread(&p,sizeof(p),1,fp))
           {
               if(p.atms==given_atmic_mass)
               {
                   print();
                   flag=0;

                   break;
               }

           }
           if(flag==1)
           {
               clrscr();
               mainscreen();
               gotoxy(25,25);
               textcolor(12);
               cprintf("::No Element Available::");

           }

           fclose(fp);
           getch();
           break;
       }
       case '5':
       {

           clrscr();
           mainscreen();
           gotoxy(15,25);
           textcolor(12);
           cprintf("Enter the Block:");
           textcolor(3);
           fflush(stdin);
           scanf("%c",&c);
           c=toupper(c);
           if((f=fopen("temp","wb+"))==NULL)
           {
           clrscr();

           printf(" cannot open the temp file 1");
           getch();
           exit(1);
           }


           if((fp=fopen("data","rb+"))==NULL)
           {
           clrscr();

           printf(" cannot open the record file 1");
           getch();
           exit(1);
           }
           flag=1;
           while(fread(&p,sizeof(p),1,fp))
           {
               if(p.block==c)
               {
                   fwrite(&p,sizeof(p),1,f);
               }

           }
           fclose(f);
           fclose(fp);

                   if((f=fopen("temp","rb+"))==NULL)
                   {
                       printf("Cannot open the file");
                       getch();
                       exit(1);
                   }
                   fseek(f,0,SEEK_END);
                   tsz=ftell(f);
                   n=(int)(tsz/sizeof(p));
                   for(i=0;i<(n-1);i++)
                   {
                       for(a=i+1;a<n;a++)
                       {
                       fseek(f,i*sizeof(p),SEEK_SET);
                       fread(&p,sizeof(p),1,f);
                       fseek(f,a*sizeof(p),SEEK_SET);
                       fread(&q,sizeof(p),1,f);
                       if((p.atm-q.atm)>0)
                           {
                           fseek(f,i*sizeof(p),SEEK_SET);
                           fwrite(&q,sizeof(p),1,f);
                           fseek(f,a*sizeof(p),SEEK_SET); fflush(stdin);
                           fwrite(&p,sizeof(p),1,fp);
                           }
                       }
                   }
                   rewind(f);
           while(fread(&p,sizeof(p),1,f))
           {

                   print();
                   getch();


           }


               clrscr();
               mainscreen();
               gotoxy(25,25);
               textcolor(12);
               cprintf("::No Element Available::");

           fclose(f);
           getch();
           break;
       }


       default:
       {
               clrscr();
               mainscreen();
               textcolor(12+128);gotoxy(22,11);
               cprintf("Wrong choice");gotoxy(22,13);textcolor(15);
               cprintf("Retype choice");
               goto label6;
       }
   }
   goto startofexplore;

}
void print()
{
   clrscr();
   mainscreen();
   gotoxy(15,16);
   cprintf("Name:");

   gotoxy(15,18);
   cprintf("Symbol:");

   gotoxy(15,20);
   cprintf("Atomic No: ");

   gotoxy(15,22);
   cprintf("Atomic Wt: ");

   gotoxy(15,24);
   cprintf("Atomic Config:");
   fflush(stdin);
   gotoxy(15,26);
   cprintf("Block:");

   gotoxy(15,28);
   cprintf("Properties:");


   textcolor(6);

   gotoxy(20,16);
   cprintf("%s",p.name);

   gotoxy(23,18);
   cprintf("%s",p.sb);

   fflush(stdin);
   gotoxy(25,20);
   cprintf("%d",p.atm);

   fflush(stdin);
   gotoxy(25,22);
   cprintf("%f",p.atms);

   fflush(stdin);
   gotoxy(29,24);
   cprintf("%s",p.atc);

   gotoxy(21,26);
   cprintf("%c",p.block);

   gotoxy(26,28);
   cprintf("%s",p.prop);
}

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