// modified by Hosung Leo Kim // Name: // Email: // Lab Day: // Lab Time: // TA: // // Lab 10 #include #include #include using namespace std; // The typedef for the Array class typedef double arrElem; // put your Array class here class Array { private: const static int declared_size = 100; arrElem array[ declared_size ]; int numberUsed; public: void readFromFile( char fileName[] ) { ifstream ifs( fileName ); if ( ifs.fail() ) { cout << "Opening file failed. Goodbye." << endl; exit(1); } numberUsed = 0; while ( ifs >> array[ numberUsed ] ) numberUsed++; ifs.close(); } int getSize() { return numberUsed; } void printArray() { for ( int i = 0; i < numberUsed; i++ ) cout << array[i] << endl; } }; // class Array int main() { Array ar1, ar2, ar3; arrElem value; int pos, newPos; int i; // store the data from file lab10a.data into ar1 ar1.readFromFile("lab10a.data"); // print out the data in ar1 cout << "Array ar1 contains " << ar1.getSize() << " values:" << endl; ar1.printArray(); /* // Sort array ar1 ar1.sort(); // print the sorted array cout << "Sorted array ar1 contains " << ar1.getSize() << " values:" << endl; ar1.printArray(); // store the same data into ar2 and ar3 ar2.readFromFile("lab10b.data"); ar3.readFromFile("lab10b.data"); // print out the data in ar2 cout << "Array ar2 contains " << ar2.getSize() << " values:" << endl; ar2.printArray(); // Sort array ar3 ar3.sort(); // print the sorted array cout << "Sorted array ar3 contains " << ar3.getSize() << " values:" << endl; ar3.printArray(); // find where the value are now located in the sorted array cout << " Value Position in ar2 Position in ar3" << endl; cout << " ----- --------------- ---------------" << endl; for (i = 0; i < 10; i++) { pos = i * 2; if (pos < ar2.getSize()) { value = ar2.getValue (pos); newPos = ar3.find(value); cout << setw(10) << value << setw(20) << pos << setw(20) << newPos << endl; } } */ } /* /homes/home30/hkim16/107b> a.out Array ar1 contains 80 values: 838.758 113.515 51.627 10.419 212.086 749.767 84.06 225.543 89.183 137.566 966.978 495.311 367.054 31.145 882.736 524.505 394.102 851.067 754.653 561.096 628.188 85.143 967.406 165.403 562.834 353.92 444.803 962.318 422.327 457.945 479.983 751.894 670.259 248.757 629.306 606.99 738.516 414.262 116.825 181.134 343.022 233.536 760.979 71.201 336.061 160.005 729.644 475.693 514.139 88.521 202.171 434.317 582.815 586.653 306.174 451.448 473.434 193.11 748.21 320.049 956.162 166.997 793.31 391.799 926.905 885.582 610 52.965 120.38 639.204 385.475 725.265 214.471 376.697 543.297 619.087 123.549 65.256 973.901 613.157 /homes/home30/hkim16/107b> */