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

Write a program that will use partially filled parallel arrays to store the peri

ID: 3678175 • Letter: W

Question

Write a program that will use partially filled parallel arrays to store the periodic table information. Your main program will call the function read_periodic_table to read the periodic table from the periodictable.dat file. Each line in this file consists of: atomic-number abbreviation name atomic-weight The function will return the actual number of elements read from the file. After reading the file into the array, your main program will provide the user with the ability to search for element with atomic number or abbreviation and a print operation. The menu will repeat until the user selects exit. All data must be passed between the functions. NO global variables or arrays.

Explanation / Answer

Here is the code for you:

#include <iostream>
#include <fstream>
using namespace std;
int searchByNumber(int atomicNumber[], int num, int numOfEle)
{
for(int i = 0; i < numOfEle; i++)
if(atomicNumber[i] == num)
return i;
return -1;
}
int searchByAbbreviation(string abbreviation[], string abb, int numOfEle)
{
for(int i = 0; i < numOfEle; i++)
if(abbreviation[i] == abb)
return i;
return -1;
}
int read_periodic_table(int number[], string abb[], string name[], double weight[])
{
int count = 0;
ifstream fileName;
fileName.open("PeriodicTable.dat");
while(!fileName.eof())
{
fileName>>number[count];
fileName>>abb[count];
fileName>>name[count];
fileName>>weight[count];
count++;
cout<<number[count]<<" "<<abb[count]<<" "<<name[count]<<" "<<weight[count]<<" "<<count<<endl;
}
return count;
}
int main()
{
int atomicNumber[100];
string abbreviation[100];
string name[100];
double atomicWeight[100];
int numOfElements = read_periodic_table(atomicNumber, abbreviation, name, atomicWeight);
while(true)
{
cout<<"1.Search element with atomic number."<<endl;
cout<<"2.Search element with abbreviation."<<endl;
cout<<"3.Print table."<<endl;
cout<<"4.Exit."<<endl;
cout<<"Enter your choice: ";
int choice, pos;
string abb;
cin>>choice;
switch(choice)
{
case 1 :
           cout<<"Enter the atomic number: ";
           int number;
           cin>>number;
           pos = searchByNumber(atomicNumber, number, numOfElements);
           if(pos == -1)
           {
           cout<<"Atomic number not found."<<endl;
           break;
           }
           cout<<"Number: "<<atomicNumber[pos]<<endl<<"Abbreviation: "<<abbreviation[pos]<<endl;
           cout<<"Name: "<<name[pos]<<endl<<"Weight: "<<atomicWeight[pos]<<endl;
           break;
case 2 :
           cout<<"Enter the abbreviation: ";
           cin>>abb;
           pos = searchByAbbreviation(abbreviation, abb, numOfElements);
           if(pos == -1)
           {
           cout<<"Abbreviation not found."<<endl;
           break;
           }
           cout<<"Number: "<<atomicNumber[pos]<<endl<<"Abbreviation: "<<abbreviation[pos]<<endl;
           cout<<"Name: "<<name[pos]<<endl<<"Weight: "<<atomicWeight[pos]<<endl;
           break;
case 3 :
           for(int i = 0; i < numOfElements; i++)
           {
           cout<<atomicNumber[i]<<" "<<abbreviation[i]<<" ";
           cout<<name[i]<<" "<<atomicWeight[i]<<endl;
           }
           break;
case 4 : return 0;
default: cout<<"Invalid menu choice. Please follow the menu."<<endl;
}
}
}

If you need any refinements, just get back to me.

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