Aid in completing the code

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

Last edited on
Way to use the code blocks!
Last edited on
Thanks? lol, I was trying to figure it out when I was running the preview, nothing populated until after I submitted the post. @keanedawg
A csv file is actually a form of Text.

You probably want to open it in notepad to see how it's formatted and start there.
I don't have actual access a windows based OS, I am utilizing a very old Macbook and was wondering how else I would be able to continue this work? anyone have any ideas? I tried downloading xCode for mac, but it is only supported by the New OS, and if I upgrade my current OS, my laptop won't be able to handle the processing speeds.
I didn't walk through your code but according to some of your questions/info the file is 27 rows and 7 columns? so I can help you with structuring your input file into the 2D array.

Within a while loop where eof() is not true:
put an if statement if current char != '\n' (which is the last character in any line text)

Therefore, start with a variable x = 0 and y = 0 (or whatever) and if the current is not a new line then you still have to feed the first row [y][x] in the array and increment x on each character, else you increment your y and reset x to zero and start all over again.

The rest is just accessing this array (putting into another temporary array if it's difficult for you to do in on the same array) and doing your computations. Hope this helps
for the SortFunction, it's simply finding the smallest in the whole array and putting it in the first index and whatever was in the first index will be swapped with it, then on the next iteration it's finding the smallest of the rest of the array and swapping it with the second index .. etc
Last edited on
Topic archived. No new replies allowed.