Question

In: Computer Science

Does anyone know how to sort these in C++: n-Butane C4H10 Propyne C3H3 1,3-Butadiyne C4H2 Hexane...

Does anyone know how to sort these in C++:

n-Butane C4H10
Propyne C3H3
1,3-Butadiyne C4H2
Hexane C6H14
Butane C4H10
iso-Butane C4H10
Pentane C5H12

By using this guideline:
Each molecular formula will be kept in a struct with three fields:

-the name(s) for formula. Note that is a collection of names
-the number of carbon atoms
-the number of hydrogen atoms
We will be storing all these structs in a vector.


The output must be this:
C3H3 Propyne
C4H2 1,3-Butadiyne
C4H10 n-Butane Butane iso-Butane
C5H12 Pentane
C6H14 Hexane

Solutions

Expert Solution

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

// Defines structure
struct MolecularFormula
{
// Data members to store data
string name;
string carbonAtoms;
string hydrogenAtoms;

// Function to return true if current object carbonAtoms is less than parameter object carbonAtoms
// Otherwise returns false
bool operator < (const MolecularFormula& str) const
{
return (carbonAtoms < str.carbonAtoms);
}// End of function
};// End of structure

// Defines a structure findGreater
struct findGreater
{
// Returns true if first object carbonAtoms is less than second object carbonAtoms
// Otherwise returns false
bool operator()( const MolecularFormula& first, const MolecularFormula& second) const
{
return first.carbonAtoms < second.carbonAtoms;
}// End of function
};// End of structure

// Function to display the vector contents
void show(vector <MolecularFormula> mfv)
{
// Loops till end of the vector
for(int c = 0; c < mfv.size(); c++)
cout<<mfv.at(c).carbonAtoms<<mfv.at(c).hydrogenAtoms<<" "<<mfv.at(c).name<<endl;
}// End of function

// Function to remove duplicate
void removeDuplicate(MolecularFormula mf [], int &len)
{
// Loops till number of elements in MolecularFormula array of object
for(int c = 0; c < len; c++)
{
// Loops next of c position till number of elements in MolecularFormula array of object
for(int d = c + 1; d < len; d++)
{
// Checks if c index position carbonAtoms hydrogenAtoms is equals to
// d index position carbonAtoms hydrogenAtoms
if(mf[c].carbonAtoms == mf[d].carbonAtoms && mf[c].hydrogenAtoms == mf[d].hydrogenAtoms)
{
// Concatenates c and d index position name
string name = mf[c].name + " " + mf[d].name;
// Assign the name to d index position name
mf[d].name = name;

// Shift each record to left from c index position to end
for(int e = c; e < len-1; e++)
mf[e] = mf[e + 1];
// Decrease the length by one
len--;
// Come out of the loop
break;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
}// End of function

// main function definition
int main()
{
// Declares a vector to store MolecularFormula objects
vector <MolecularFormula> mfv;

// Creates an array of objects of MolecularFormula
MolecularFormula mf [] =
{
{"n-Butane", "C4", "H10"},
{"Propyne", "C3", "H3"},
{"1,3-Butadiyne", "C4", "H2"},
{"Hexane", "C6", "H14"},
{"Butane", "C4", "H10"},
{"iso-Butane", "C4", "H10"},
{"Pentane", "C5", "H12"}
};// End of assignment

// Calls the function to display data before sorting
cout<<"\n Before Removing duplicate\n";

// Loops to add MolecularFormula objects to vector
for(int c = 0; c < 7; c++)
mfv.push_back(mf[c]);

// Calls the function to display data
show(mfv);

// Stores the length of the vector
int len = mfv.size();

cout<<"\n After removing duplicate\n";
// Calls the function to remove duplicate and merge
removeDuplicate(mf, len);

// Clears the vector contents
mfv.clear();

// Loops till current length of the array of object after removing and merging
for(int c = 0; c < len; c++)
// Assigns the current object to vector
mfv.push_back(mf[c]);
// Calls the function to display data after removing duplicate
show(mfv);

// Calls the function to sort data
sort( mfv.begin(), mfv.end(), findGreater() );

// Calls the function to display data after sorting
cout<<"\n After Sorting \n";
show(mfv);

return 0;
}// End of main function

Sample Output:

Before Removing duplicate
C4H10 n-Butane
C3H3 Propyne
C4H2 1,3-Butadiyne
C6H14 Hexane
C4H10 Butane
C4H10 iso-Butane
C5H12 Pentane

After removing duplicate
C3H3 Propyne
C4H2 1,3-Butadiyne
C6H14 Hexane
C4H10 n-Butane Butane iso-Butane
C5H12 Pentane

After Sorting
C3H3 Propyne
C4H2 1,3-Butadiyne
C4H10 n-Butane Butane iso-Butane
C5H12 Pentane
C6H14 Hexane


Related Solutions

Does anyone know how to do it on EXCEL? Does anyone know how to do it...
Does anyone know how to do it on EXCEL? Does anyone know how to do it on EXCEL? Does anyone know how to do it on EXCEL? Does anyone know how to do it on EXCEL? Anyone know how to do it on EXCEL? The Statistical Abstract of the United States published by the U.S. Census Bureau reports that the average annual consumption of fresh fruit per person is 99.9 pounds. The standard deviation of fresh fruit consumption is about...
In production of 1,3-Butadiene .The feed that will be used is n-butane,i-butane,propane and other hydrocarbon which...
In production of 1,3-Butadiene .The feed that will be used is n-butane,i-butane,propane and other hydrocarbon which is C5+, right now I want to know what is the chemical and physical properties of C5+ hydrocarbon,because I am doing for Material and Energy Balance please help.ASAP
#3 Consider a system that is 30 % n-Butane, 40 % n-pentane and 30 % n-hexane....
#3 Consider a system that is 30 % n-Butane, 40 % n-pentane and 30 % n-hexane. a. Estimate the bubble pressure at a temperature of 80 C. What would be the composition of the vapor formed? b. Estimate the dew pressure at 80 C. What would be the composition of the liquid formed? c. Estimate the bubble temperature at a pressure of 2 atm? What would be the composition of the vapor formed? d. If this mixture was at 100...
Calculate the mass fraction stochiometric ratio for the combustion of n-Butane (C4H10) in air ( 20%...
Calculate the mass fraction stochiometric ratio for the combustion of n-Butane (C4H10) in air ( 20% v/v O2   80% v/v N2)* ratio 4:1. Given the following Atomic Weights for the elements and their valencies.* Element            Symbol         Atomic Weight           Valency Hydrogen              H                          1                             1 Carbon                  C                         12                            4 Nitrogen                N                         14                            3 Oxygen                  O                         16                            2 *Note these numbers have been rounded for ease of calculation.
Consider a mixture of 30% n-butane, 35% n-hexane, 35% n-heptane. If the pressure is 202 kPa,...
Consider a mixture of 30% n-butane, 35% n-hexane, 35% n-heptane. If the pressure is 202 kPa, calculate the bubble point temperature in ?C and the unknown mole fractions. Specify whether the unknown mole fractions are vapor or liquid. .
A 200-L home heater tank contains butane gas (C4H10) at 26.0 °C. When some of the...
A 200-L home heater tank contains butane gas (C4H10) at 26.0 °C. When some of the butane is removed and burned in excess air, the pressure in the tank drops from 2.34 atm to 1.10 atm. The energy generated in the combustion is then used to raise the temperature of 132.5 L of water from 19.0°C to 71.3 °C. Useful Information: ΔH°f (CO2 (g)) = –393.5 kJ/mol ΔH°f (H2O (l)) = –285.8 kJ/mol Density of water = 1 g/mL →Calculate...
Does anyone know how to access altitude and azimuth for different objects on Stellarium?
Does anyone know how to access altitude and azimuth for different objects on Stellarium?
Does anyone know how to do pseudocode on this topic? Write a program to perform the...
Does anyone know how to do pseudocode on this topic? Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop...
Does anyone know how to find if a certain String is available in an ArrayList? I...
Does anyone know how to find if a certain String is available in an ArrayList? I was asked by my instructor to make a certain program about Student Record In the area where I need to add some student in the ArrayList it won't add if there is the same name in the ArrayList at first I used the equals method and contains method but later I found it was prohibited to use such method. Is there a way to...
With the Taft-Hartley act how does anyone know how much is too much relative to union...
With the Taft-Hartley act how does anyone know how much is too much relative to union dues? What is featherbedding and why is that a problem?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT