1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <fstream>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
void importTable(string tabledata, int** &table, int& rowCount, int& colCount);
//This function reads the table stored in the file at the specified location. The file is expected to have the following form
//-The first line consists of a single integer, R, that indicates the number of rows in the table.
//-The second line consists of a single integer, C, that indicates the number of columns in the table.
//-The remainder of the file will contain the table values.The file consists of R more lines and each of those lines with have C integers separated by spaces, potentially more than one.
//-No two rows will share the same initial value(think of them as unique ids)
void sort(int** rowReferences, int rowCount, int colCount, int sortColumn);
//This function sorts the row references such that the values in column number sortColumn are in ascending order.
//The sorting is ascending and is performed using a select sort.This function assumes 0 ? sortColumn < colCount; that is, the column is an actual column in the table.
void showTable(int** table, int rowCount, int colCount);
//This function displays the table.There is no specific format for the output, just make the output look tidy - evenly spaced columns and such.
int main(){
//int& rowNum;
//int& colNum;
//string tablestring;
int rows = 0;
int columns = 0;
int ** my_table;
int ** reference1;
int sorting = 0;
//importTable(string tabledata, int** &table, int& rowCount, int& colCount);
cout << "Table;" << endl;
importTable("v:\\Desktop\\tabledata.txt", my_table, rows, columns);
importTable("tabledata.txt", reference1, rows, columns);
sort(reference1, rows, columns, sorting);
showTable(my_table, rows, columns);
//cout << my_table[0][1] << " " << endl;
//cout << &table;
showTable;
cout << endl;
cout << "Program success" << endl;
return 0;
}
void importTable(string tabledata, int** &table, int& rowCount, int& colCount){ //int** &table
ifstream in;
in.open(tabledata);
in >> rowCount;
in >> colCount;
table = new int*[rowCount];
for (int d = 0; d < rowCount; ++d){
table[d] = new int[colCount];
for (int e = 0; e < colCount; ++e){
in >> table[d][e];
}
};
in.close();
}
void sort(int** rowReferences, int rowCount, int colCount, int sortColumn){
int startScan;
int minIndexC;
int minElemC;
for (startScan = 0; (startScan < colCount - 1); ++startScan){
minIndexC = startScan;
minElemC = rowReferences[startScan][sortColumn];
for (int indexC = startScan + 1; indexC < colCount; ++indexC)
{
if (rowReferences[indexC][sortColumn] < minElemC)
{
minElemC = rowReferences[indexC][sortColumn];
minIndexC = indexC;
}
}
rowReferences[minIndexC][sortColumn] = rowReferences[startScan][sortColumn];
rowReferences[startScan][sortColumn] = minElemC;
}
}
void showTable(int** my_table, int rowCount, int colCount){
for (int count = 0; (count < rowCount); ++count)
{
for (int twocount = 0; (twocount < colCount); ++twocount)
{
cout << setw(4) << my_table[count][twocount] << " ";
}
cout << endl;
}
}
|