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

Write a program that displays the speed of sound through a pre-calculated select

ID: 3801563 • Letter: W

Question

Write a program that displays the speed of sound through a pre-calculated selection of mediums. (in C++)

(Please take your time and include comments so that I can follow your program more easily.)


Given:
Files containing tab-delimited lists of the speed of sound through different mediums: gasses.dat,
liquids.dat, and solids.dat. (These are just plain text.)
Deliverables:
main.cpp
Interaction sequence:
1. Part one: selecting a substance
a. The program prompts the user to select “gasses”, “liquids”, or “solids” via a numbered
menu.
b. The user makes a choice. The program opens the corresponding file, and displays the names
(not including the speeds) of the substances inside as a new numbered menu.
c. The user makes a choice. The program displays the speed of that substance.
d. Program quits.
2. Part two: mixing substances
a. Add a fourth option, “mix”, to the top of each matter-state menu.
b. When selected, this option asks for a formula in the format
PARTS:NUMBER,PARTS:NUMBER. For example, “5:1,11:2” is five parts hydrogen,
eleven parts helium (not the atomic numbers, but the order they appear in the list/file). (No
requirement for mixes beyond two substances.)
c. Math (for the above mix, (5*1284+11*965)/16).
d. Display that average.
e. Program quits.
3. Part three: add a substance
a. Add a fourth option, “add” to the main menu. When selected
b. When selected, this option prompts the user with the same numbers to select the matter state
of the new substance.
c. The user makes a choice. The program prompts to enter the substance name.
d. The user enters the substance name, and the program prompts for the speed.
e. The user enters the substance speed.
f. The program appends the new substance to the bottom of the appropriate file (for example,
“argon 308” in gasses.dat (remember that that whitespace is a tab)).
g. Program quits.
Notes:
• Speeds are in meters per second.

Contents of the plain text files are as follows.

gasses.dat :

hydrogen   1284
helium       965
nitrogen   334
oxygen       316

liquids.dat :

glycerol   1904
water       1493
mercury       1450
kerosene   1324

solids.dat :

diamond       12000
iron       5960
aluminum   5100
brass       4700

Explanation / Answer

Didnt got time to add comments. PLease comment if you are unable to understand.

#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<stdlib.h>
using namespace std;

class states
{
private:
vector<string> elements;
vector<int> speeds;

public:
int size;
void pushNewElement(string ele, int speed){
elements.push_back(ele);
speeds.push_back(speed);
}

string getElements(int index){
return elements.at(index);
}

int getSpeeds(int index){
return speeds.at(index);
}

};


int main()
{
string element ="", val = "";
int speed = 0, option = -1, i =0, ele ;
int part1 = 0, part2 = 0;
int element1 = 0 , element2 = 0;
int temp1 ,temp2 ,state;
states solid, liquid , gas;
ifstream solidFile("./solids.dat");
ifstream liquidFile("./liquids.dat");
ifstream gasFile("./gasses.dat");

solid.size = 0;
liquid.size = 0;
gas.size = 0;

/*Read the solids.txt file*/
while(solidFile >> element){
solidFile >> speed;
solid.pushNewElement(element,speed);
solid.size++;
}

/*Read the liquids.txt file*/
while(liquidFile >> element){
liquidFile >> speed;
liquid.pushNewElement(element,speed);
liquid.size++;
}

/*Read the gasses.txt file*/
while(gasFile >> element){
gasFile >> speed;
gas.pushNewElement(element,speed);
gas.size++;
}

solidFile.close();
liquidFile.close();
gasFile.close();

do{
   cout<< "Please enter the substance state from the menu :" <<endl;
   cout<< "1. Solid" <<endl;
   cout<< "2. Liquid" <<endl;
   cout<< "3. Gas" <<endl;
   cout<< "4. Add" <<endl;
cout<< "5. End" <<endl;
cin >> option;

   switch(option){
       case 1:
           for(i=0; i < solid.size ; i++){
               cout<< i+1 << ": " << solid.getElements(i)<<endl;
           }
cout<< i + 1 << ". Mix" <<endl;
cin>> ele;
if(ele <= gas.size)
   cout<< "Speed : " << solid.getSpeeds(i-1) <<endl;
           else{
               cout << "Enter the two elements info <part:element1,part:element2>" <<endl;
               cin>> val;
for(i=0;i<val.find(",");i++){
                   if(val.at(i) == ':'){
                       part1 = atoi(val.substr(0,i).c_str());
                       element1 = atoi(val.substr(i+1,val.find(",")).c_str());
                       break;
                   }
               }
               i = val.find(",");
               int j = 0;
           for(j = i;j < val.size() ; j++){
                   if(val[j] == ':'){
                       part2 = atoi(val.substr(i+1,j).c_str());
                       element2 = atoi(val.substr(j+1,val.size()).c_str());
                       break;
                   }
               }  
               cout<<"Speed : " << (part1 * solid.getSpeeds(element1 -1) + part2 * solid.getSpeeds(element2 - 1))/(part1 + part2) <<endl;
                  
           }
           break;
       case 2:
           for(i=0; i < liquid.size ; i++){
               cout<< i+1 << ": " << liquid.getElements(i)<<endl;
           }
cout<< i + 1 << ". Mix" <<endl;
cin>> ele;
if(ele <= gas.size)
   cout<< "Speed : " << liquid.getSpeeds(i-1) <<endl;
           else{
               cout << "Enter the two elements info <part:element1,part:element2>" <<endl;
               cin>> val;

for(i=0;i<val.find(",");i++){
                   if(val.at(i) == ':'){
                       part1 = atoi(val.substr(0,i).c_str());
                       element1 = atoi(val.substr(i+1,val.find(",")).c_str());
                       break;
                   }
               }
               i = val.find(",");
               int j = 0;
           for(j = i;j < val.size() ; j++){
                   if(val[j] == ':'){
                       part2 = atoi(val.substr(i+1,j).c_str());
                       element2 = atoi(val.substr(j+1,val.size()).c_str());
                       break;
                   }
               }  
               cout<<"Speed : " << (part1 * liquid.getSpeeds(element1 -1) + part2 * liquid.getSpeeds(element2 - 1))/(part1 + part2) <<endl;
           }
           break;
       case 3:
           for(i=0; i < gas.size ; i++){
               cout<< i+1 << ": " << gas.getElements(i)<<endl;
           }
cout<< i + 1 << ". Mix" <<endl;
cin>> ele;
if(ele <= gas.size)
   cout<< "Speed : " << gas.getSpeeds(i-1) <<endl;
           else{
               cout << "Enter the two elements info <part:element1,part:element2>" <<endl;
               cin>> val;

for(i=0;i<val.find(",");i++){
                   if(val.at(i) == ':'){
                       part1 = atoi(val.substr(0,i).c_str());
                       element1 = atoi(val.substr(i+1,val.find(",")).c_str());
                       break;
                   }
               }
               i = val.find(",");
               int j = 0;
           for(j = i;j < val.size() ; j++){
                   if(val[j] == ':'){
                       part2 = atoi(val.substr(i+1,j).c_str());
                       element2 = atoi(val.substr(j+1,val.size()).c_str());
                       break;
                   }
               }  
               cout<<"Speed : " << (part1 * gas.getSpeeds(element1 -1) + part2 * gas.getSpeeds(element2 - 1))/(part1 + part2) <<endl;
           }
           break;
       case 4:
           cout<< "Please enter the substance state from the menu :" <<endl;
           cout<< "1. Solid" <<endl;
           cout<< "2. Liquid" <<endl;
           cout<< "3. Gas" <<endl;
           cin >> state;
           if(state == 1){
               string filename = "solids.dat";
               cout<<"Enter the element name and speed" <<endl;
               cin>>element >>speed;
ofstream out(filename.c_str(),std::ios_base::app | std::ios_base::out);
out <<element << " " <<speed ;
               out.close();
           }
           else if(state == 2){
               string filename = "liquids.dat";
               cout<<"Enter the element name and speed" <<endl;
               cin>>element >>speed;
ofstream out(filename.c_str(),std::ios_base::app | std::ios_base::out);
out << element << " " <<speed ;
               out.close();
           }
           else{
               string filename = "gasses.dat";
               cout<<"Enter the element name and speed" <<endl;
               cin>>element >>speed;
ofstream out(filename.c_str(),std::ios_base::app | std::ios_base::out);
out << element << " " <<speed ;
               out.close();
           }

       case 5:
   return 0;
           break;
       default:
           break;
   }

}while(option!=4);


return 0;
}

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