![]() |
|
#1
|
||||
|
||||
No output from loop functionIn my program, I need to output both the row averages of a tabular file and the column averages. I can get the row averages just fine but not the column averages. The column function appears to not do a thing; no output at all, even from my debug statements. I thought that, since the idea is the same, I could just use a modified version of the row function but I guess I'm wrong.
As usual, I'm sure there's a simple explanation for it but I can't seem to figure it out. Anyone else have a suggestion? Thanks. Input file Code:
Main.cc CPP / C++ / C Code:
Table.h CPP / C++ / C Code:
Table.cc [c] /************************************************** ************************ *Purpose: Table.cpp defines various Table operations. ************************************************** *************************/ #include "Table.h" #include <fstream> // ifstream #include <cassert> // assert() using namespace std; void print(ostream& out, const Table& aTable) { for (int row = 0; row < aTable.size(); row++) { for (int col = 0; col < aTable[row].size(); col++) out << aTable[row][col] << '\t'; out << endl; } } void fill(const string& fileName, Table& aTable) { ifstream in(fileName.data()); // open stream to file assert(in.is_open()); // verify int students, scores; // input variables for dimensions string student_num, score_num; //input variables for text values in file in >>student_num >> students >> score_num >> scores; // read dimensions int rows = students, cols = scores/students; Table locTable(rows, TableRow(cols)); // construct local table with // correct # rows and columns for (int r = 0; r < rows; r++) // for each row for (int c = 0; c < cols; c++) // input cols values into row in >> locTable[r] CPP / C++ / C Code:
__________________
Start Programming with Python-A beginner's guide to programming and the Python language. ------------- Common Sense v2.0-Striving to make the world a little bit smarter. |
||||
|
#2
|
||||
|
||||
|
First of all, I think it is a mistake to loop through the array from 0 up to and including array.size(); you should get an access violation there.
Second, I modified your colAvg to this: CPP / C++ / C Code:
You see, with every while loop, row gets incremented; when you get to the second loop, you test aTable[row].size(), but row is now greater than the last "good" row in the matrix. It probably evaluates to 0. Your code works now. Try it! Best regards, Luci __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein Last edited by LuciWiz : 21-Dec-2004 at 01:05.
Reason: there != their (I know that :))
|
|
#3
|
||||
|
||||
|
You're right, it was evaluating to '0'. It works now.
Thanks!__________________
Start Programming with Python-A beginner's guide to programming and the Python language. ------------- Common Sense v2.0-Striving to make the world a little bit smarter. |
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 14:12 |
| Nested for loop with function | Tori | C++ Forum | 11 | 08-Nov-2004 14:02 |
| using vector or array | oshiotse | C++ Forum | 4 | 16-Apr-2004 11:59 |
| Revising Script style ?????? | pepee | MySQL / PHP Forum | 4 | 14-Apr-2004 05:59 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The