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

Write a program using C++ to solve the following.: A program is needed to store

ID: 3702291 • Letter: W

Question

Write a program using C++ to solve the following.: A program is needed to store the periodic table that is always growing (use a std: vector). The following data needs to be stored for each element Atomic number (int) Element Name (string) Symbol (string) Atomic Mass (float) A struct would work perfect for this. However, for this to be even more useful, your class (make sure you use a h file as well the cpp file) should have a member function (private), which is called by the constructor to populate the first four elements with the correct data (element data is available online). There also needs to be the following public functions that will be called by your main: A function to print out entire table in order(formatted nicely) A function to add an element (prompts for the data in your main) Finally, since the function to print out needs to have the data in order (by atomic number) need to sort you vector. See http://www.cplusplus.com/reference/algorithm/sort/ for an example on using the sort command. Specifically from the link above: // using object as comp std: :sort (myvector.begin), myvector.end (), myobject) Where myobject is your own compare function; Note the myobject at the end of the struct definition (before the :) is a shorthand way to declare an object of this type (at the link above) Note: You will have to implement your own compare function based on the atomic number (make sure you pass your objects (struct) into the "bool operator()" by reference Zip up your entire netbeans project and submit. Also, include sample output from a run

Explanation / Answer



Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you

PeriodicTable.h
=============

#ifndef PeriodicTable_h
#define PeriodicTable_h
#include <vector>
#include <iostream>
using std::vector;
using std::string;
struct Element
{
int atomicNum;
string name;
string symbol;
float mass;
};
class PeriodicTable
{
private:
vector<Element> elements;
void fillInitialData();
public:
PeriodicTable();
void print();
void addElement(int atomicNum, string name, string symbol, float mass);
};

bool compareElements(Element a, Element b);
#endif /* PeriodicTable_h */

PeriodicTable.cpp
===========

#include "PeriodicTable.h"
#include <iomanip>
#include <iostream>
using namespace std;
PeriodicTable::PeriodicTable()
{
fillInitialData();
}
void PeriodicTable::fillInitialData()
{
addElement(1, "Hydrogen", "H", 1.008);
addElement(2, "Helium", "He", 4.0026);
addElement(3, "Lithium", "Li", 6.94);
addElement(4, "Berylium", "Be", 9.0122);
}
void PeriodicTable::print()
{
sort(elements.begin(), elements.end(), compareElements);
cout << left << setw(15) << "Atomic Number" << setw(15) << "Element Name"
<< setw(15) << "Symbol" << setw(15) << "Atomic Mass" << endl;
for(int i = 0 ; i < elements.size(); i++)
cout << left << setw(15) << elements[i].atomicNum << setw(15) << elements[i].name
<< setw(15) << elements[i].symbol << setw(15) << elements[i].mass << endl;
cout << endl << endl;
}
void PeriodicTable::addElement(int atomicNum, string name, string symbol, float mass)
{
Element e;
e.atomicNum = atomicNum;
e.name = name;
e.symbol = symbol;
e.mass = mass;
elements.push_back(e);
}
bool compareElements(Element a, Element b)
{
return a.atomicNum < b.atomicNum;
}
main.cpp
========
#include "PeriodicTable.h"
#include <iostream>
using namespace std;
int main()
{
PeriodicTable table;
int choice = 0;
int num;
string name, symb;
float mass;
while(choice != 3)
{
cout << "1. Add an element" << endl;
cout << "2. Print table" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
cout << "Enter atomic number: ";
cin >> num;
cout << "Enter element name: ";
cin >> name;
cout << "Enter element symbol: ";
cin >> symb;
cout << "Enter element mass: ";
cin >> mass;
table.addElement(num, name, symb, mass);
break;
case 2:
table.print();
break;
case 3:
break;
default:
cout << "Invalid choice !" << endl;
}
}
}

output
=====
1. Add an element
2. Print table
3. Exit
Enter your choice: 2
Atomic Number Element Name Symbol Atomic Mass
1 Hydrogen H 1.008
2 Helium He 4.0026
3 Lithium Li 6.94
4 Berylium Be 9.0122

1. Add an element
2. Print table
3. Exit
Enter your choice: 1
Enter atomic number: 20
Enter element name: Calcium
Enter element symbol: Ca
Enter element mass: 40.078
1. Add an element
2. Print table
3. Exit
Enter your choice: 1
Enter atomic number: 11
Enter element name: Sodium
Enter element symbol: Na
Enter element mass: 22.990
1. Add an element
2. Print table
3. Exit
Enter your choice: 1
Enter atomic number: 53
Enter element name: Iodine
Enter element symbol: I
Enter element mass: 126.90
1. Add an element
2. Print table
3. Exit
Enter your choice: 1
Enter atomic number: 26
Enter element name: Iron
Enter element symbol: Fe
Enter element mass: 55.845
1. Add an element
2. Print table
3. Exit
Enter your choice: 2
Atomic Number Element Name Symbol Atomic Mass
1 Hydrogen H 1.008
2 Helium He 4.0026
3 Lithium Li 6.94
4 Berylium Be 9.0122
11 Sodium Na 22.99
20 Calcium Ca 40.078
26 Iron Fe 55.845
53 Iodine I 126.9

1. Add an element
2. Print table
3. Exit
Enter your choice: 3

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