Продолжайте получать ошибку сегментации (дамп ядра) при программировании C, и я пытаюсь передать 2d-массив - PullRequest
0 голосов
/ 11 июля 2020
#include <stdlib.h>
#include <stdio.h>

double **transpose(double **arr, int r, int c);

double **transpose(double **arr, int r, int c){
                //will hold the transposed matrix
                double **arrT;
                arrT = (double **)malloc(c * sizeof(double *));
        for(int i = 0; i < c; i++){
                                arrT[i] = (double *)malloc(r * sizeof(double));
                }

        //transpose the matrix
        for(int i = 0; i < c; i++){
                for(int j = 0; j < r; j++){
                        arrT[j][i] = arr[i][j];
                }
        }

        return arrT;


}




int main(int argc, char *argv[]){
        

        //if there is no argument
        if(argc < 2){
            printf("There is no argument\n");
            return -1;


        }

          //training data
                char *fname = argv[1];

                //test data
                char *fname2 = argv[2];

                //pointer for training data
                FILE *fptr;

                //pointer for test data
            FILE *fptr2;

        //number of columns
                int column;

                //number of rows
                int row;

                //opens training data file and reads it
                fptr = fopen(fname, "r");
        
         //will scan the first line of training data
                //to store column of attributes and we add
                //1 more column for price
                fscanf(fptr, "%d\n", &column);
                //adds column for prices
                column++;


         printf("Number of columns: %d\n", column);

                //will scan the second line of training data
                //to store number of rows
                fscanf(fptr, "%d\n", &row);

         printf("Number of rows: %d\n", row);

        

        //allocate array X that will hold attributes
        double **X;
        X = (double **)malloc(row * sizeof(double *));
        
        //allocate array that will hold everything attributes
        //and prices
        double **arr;
        arr = (double **)malloc(row * sizeof(double *));

        //allocate array that will hold prices
        double *Y;
        Y = (double *)malloc(row * sizeof(double *));
        for(int i = 0; i < row; i++){
            X[i] = (double *)malloc(column * sizeof(double));
            arr[i] = (double *)malloc(column * sizeof(double));
        }
        
        //will store the whole training data into arr
        printf("This is training data: \n");
        for(int i = 0; i < row; i++){
            for(int j = 0; j < column; j++){
                fscanf(fptr, "%lf,", &arr[i][j]);
                printf("%lf\t", arr[i][j]); 
            }
            printf("\n");

        }
        
        //copies training data for the attribute array
        for(int i = 0; i < row; i++){
            for(int j = 0; j < column; j++){
                X[i][j] = arr[i][j];
            }
        
        }
        
        
         //will set attributes first column
                //to all 1's
                for(int i = 0; i < row; i++){
                        X[i][0] = 1;
                }
        
        printf("This is attributes:\n");
         for(int i = 0; i < row; i++){
                        for(int j = 0; j < column; j++){
                                printf("%lf\t", X[i][j]);
                        }
            printf("\n");
                        
                }

         //will set the first column or training
         //data to the prices array
         printf("This is prices: \n");
         for(int i = 0; i < row; i++){
            Y[i] = arr[i][0];
            printf("%lf\n", Y[i]);
         }
        
         //matrix will hold transposed
         double **trans;
        
        transpose(X, row, column);
/*
         printf("This is transposed of attributes\n");
         for(int i = 0; i < row; i++){
                        for(int j = 0; j < column; j++){
                                printf("%lf\t", trans[i][j]);
                        }
                        printf("\n");

                }


*/
        return 0;
    }


double **transpose(double **arr, int r, int c){
                //will hold the transposed matrix
                double **arrT;
                arrT = (double **)malloc(c * sizeof(double *));
        for(int i = 0; i < c; i++){
                                arrT[i] = (double *)malloc(r * sizeof(double));
                }

        //transpose the matrix
        for(int i = 0; i < c; i++){
                for(int j = 0; j < r; j++){
                        arrT[j][i] = arr[i][j];
                }
        }

        return arrT;


}

Привет, я еще не завершил код, но код все равно должен работать нормально. Проблема в том, что я пытаюсь использовать метод транспонирования матрицы, и когда он это делает, он говорит, что ядро ​​ошибки сегментации сброшено. Если вы скажете мне, что не так, я буду очень признателен. Я пытаюсь передать 2-мерный массив.

Текстовый файл, который он должен прочитать, должен быть input.txt: где 4 - первая строка, а 10 - вторая строка.

4
10
221900,3,1,1180,1955
538000,3,2.25,2570,1951
180000,2,1,770,1933
604000,4,3,1960,1965
510000,3,2,1680,1987
1230000,4,4.5,5420,2001
257500,3,2.25,1715,1995
291850,3,1.5,1060,1963
229500,3,1,1780,1960
323000,3,2.5,1890,2003
...