I have wrote the program and it worked fine until I had to include one more array into it. As you can see below the two arrays of volume and mass are working properly now I need to include a third array which reads in the sample name that coincides with the mass and the volume. I have tried the getline function and that didnt work. This is my first class in computer programming and im very new to it so if i have made obviouse mistakes i apologize. I tried as much as i can myself and Im not exactly sure how to proceed. Please help
Here is the program:
Here is the program:
Code:
# include <string> # include <iostream> # include <fstream> using namespace std; void readinput (double[], double[], string, int, ifstream &infile); void printvalues (double[], double[], string, int, ofstream &outfile); void linearsort (double[], double[], string, int); void bubblesort (double[], double[], string, int); int line_search (double[], double, int); int main() { ofstream outfile ("prog7.txt"); ifstream infile ("assignment7.txt"); int n, position; if (infile.bad()) { cout<<"unable to read from file"; exit(1); } infile>>n; double mass[n], volume[n], newnumber; string name; readinput(mass, volume, name, n, infile); cout<<name[0]<<name[1]<<name[2]<<name[3]<<name[4]<<name[5]; printvalues(mass, volume, name, n, outfile); linearsort(mass, volume, name, n); printvalues(mass, volume, name, n, outfile); bubblesort(volume, mass, name, n); printvalues(mass, volume, name, n, outfile); while(infile>>newnumber) { position=line_search(mass, newnumber, n); if (position == -1) { cout<<"The number "<<newnumber <<" does not occure in the mass array"<<endl; } else { cout<<newnumber<<" is found at position "<<position <<". The volume of that sample is "<<volume[position]<<endl; } position=line_search(volume, newnumber, n); if (position == -1) { cout<<"The number "<<newnumber <<" does not occure in the volume array"<<endl; } else { cout<<newnumber<<" is found at position "<<position <<". The mass of that sample is "<<mass[position]<<endl; } cout<<endl; } infile.close(); outfile.close(); getchar(); return 0; } //Function readinput: //Input: 2 arrays, int n, and infile //Process: Read the data values from the input values into 2 arrays. void readinput (double a[], double b[], string c[], int n, ifstream &infile) { for (int i=0; i<n; i++) infile>>a[i]>>b[i]>>c[i]; return; }//read input //Function bubblesort: //Input: 2 arrays //Process: Sorts the arrays in ascending order of volume void bubblesort (double a[], double b[], string c[], int n) { double temp, temp2; string temp3; bool swapped; do { swapped = false; for (int i=0; i<n-1; i++) if (a[i] > a[i+1]) { temp = a[i]; temp2 = b[i]; temp3 = c[i]; a[i] = a[i+1]; b[i] = b[i+1]; c[i] = c[i+1]; a[i+1] = temp; b[i+1] = temp2; c[i+1] = temp3; swapped = true; } } while (swapped); return; }//bubblesort //Function linearsort: //Input: 2 arrays //Process: Sorts the arrays in descending order of mass void linearsort (double a[], double b[], string c[], int n) { double temp, temp2; string temp3; for (int i=0; i<n-1; i++) for (int j=i+1; j<n; j++) if (a[i] < a[j]) { temp = a[i]; temp2 = b[i]; temp3 = c[i]; a[i] = a[j]; b[i] = b[j]; c[i] = c[j]; a[j] = temp; b[j] = temp2; c[j] = temp3; } return; }// linearsort //Function linsearch: //Input: double array, number to be searched for and n //Process: Determines whether or not the input number occurs in the array //Output: The position of that number or -1 if it cannot be found int line_search (double a[], double newnumber, int n) { for (int position=0; position<n; position++) if (a[position] == newnumber) return position; return -1; }//line_search //Function printvalues: //Input: 2 arrays, int n and outfile //Process: To print the arrays into a table. void printvalues (double a[], double b[], string c[], int n, ofstream &outfile) { outfile<<" Collected Data"<<endl; outfile<<"Mineral Mass Volume"<<endl; for (int i=0; i<n; i++) { outfile.setf(ios_base::fixed, ios_base::floatfield); outfile.precision(2); outfile.setf(ios::left); outfile.width(12); outfile<<c[i]<<a[i]<<b[i]<<endl; } outfile<<endl<<endl; return; }//printvalues
Comment