Hello Everyone,
This is my first time posting to the forums so please let me know if I have made an mistakes.
I am having an issue completing an assignment that was given to me at my college, I'm currently enrolled in a introductory C++ class which is very fun and exciting for me. I have come to find that the professor has left out a lot of which I believe should be reviewed.
I am not looking for someone to give me the code to this problem, but possibly guide me in the correct path? So far this website has proven to be very helpful and makes a lot of the basics much easier for me to comprehend.
The problem ask the following:
1. Read this csv file into a two dimensional array called grades such that
grades[0][0] is 10 grades[0] [1] is 0 grades[3] [5] is 8 etc.
2. You have to calculate the average for every row omitting lowest two scores
3. Store this average is a one dimensional array called avg
4. Calculate the average of all the averages stored in avg. This is the class average.
5. Write out the original data with the rwo average two columns after the last grade of the row
The file is an excel file of 27 rows and 7 columns. all integers and doubles.
So far this is the code that I have gone over with my professor, and was wondering what I am missing, I'm not seeing the picture clearly because I just seem to be stuck:
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
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
//Declare an input file
string fileName;
cout << "Enter the file name: ";
getline(cin, fileName);
ifstream MyInputFile (fileName);
if (!MyInputFile) {
cout << "Enable to open file: " << fileName << endl;
system("PAUSE");
exit(1);
}
int Mat[50][10];
int row = 0, colFinal = 0;
while (MyInputFile.eof() != true) {
int Val;
char *temp; //this is a pointer to a character same as string
char MyStr[128];
string str;
getline(MyInputFile, str);
strcpy_s(MyStr, str.c_str());
int col = 0;
temp = MyStr;
char *temp1 = temp;
while (temp1 != NULL){
temp1 = strchr(temp, ',');
if (temp1 == NULL) {
Mat[row][col] = atoi(temp);
col++;
if (colFinal == 0)
colFinal = col;
break;
}
*temp1 = '\0';
Mat[row][col] = atoi(temp);
temp = temp1 + 1;
col++;
}
row = row + 1;
}
for (int i = 0; i < row-1; i++) {
for (int j = 0; j < colFinal; j++) {
cout << Mat[i][j] << " " ;
}
cout << endl;
}
system("PAUSE");
return 0;
}
| |
I have also added more code that my instructor has hinted to:
The code for Sorting Rows:( I honestly do not understand this code )
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
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int findIndexSmallest(int A[][7], int row, int col, int start) {
int ind = start;
for (int i = start + 1; i < col; i++) {
if (A[row][i] < A[row][ind])
ind = i;
}
return ind;
}
void SortRows(int A[][7], int rows, int col) {
//sort one row
for (int thisRow = 0; thisRow < rows; thisRow++) {
for (int start = 0; start < col - 1; start++) {
int indexSmallest = findIndexSmallest(A, thisRow, col, start);
//swap A[thisRow][start] with A[thisRow][indexSmallest]
int temp = A[thisRow][start];
A[thisRow][start] = A[thisRow][indexSmallest];
A[thisRow][indexSmallest] = temp;
}
}
}
| |
I also have another part of code that was gone over by the prof when i was absent, I'm not sure how to import a screen shot but I will type it out if I do receive some help from anyone. Please, I really want to understand this program because apparently, my instructor told me that this is the kind of problem that everday programmers run into. I must solve this problem so that I can take the next steps to becoming a decent programmer.
Any help is greatly appreciated.
Your friend,
Zealot91