简体   繁体   中英

VSCode doesn't show all warnings/errors

I have this code:

#include <stdio.h>
#include <stdlib.h>

FILE *openFile(char const * , char const *);
int readDim(FILE *);
double **allocMatrix(int , int );
void printMatrix(int , int , double **);
void readMatrix(FILE *, int , int , double **);
void mediana(int , int , double **);
void printVect(int , double *);

FILE* openFile(char const* file_name, char const* mode)
{
  FILE* fp = fopen(file_name, mode);
  printf("File name: %s\n", file_name);
  if (fp == NULL)
  {
    perror(file_name); 
    exit(EXIT_FAILURE); 
  } 
  else 
    printf("File read correctly\n");

  return fp; 
}

int main()
{
    FILE *fp=openFile("matrice.txt", "r");
    int nrows=readDim(fp), ncols=readDim(fp);
    printf("Rows: %d\tColumns: %d\n", nrows, ncols);
    double **matrix=allocMatrix(nrows, ncols);
    readMatrix(fp,nrows, ncols, matrix);
    printMatrix(nrows, ncols, matrix);
    mediana(nrows, ncols, matrix);
    return 0;
}

void mediana(int nrows, int ncols, double **matrix)
{
    int i, col=0;
    double temp;
    double *row=calloc(ncols, sizeof(double));
    for(i=0; i<ncols; i++)
    {
        row[i]=matrix[i][col];
    }
    printVect(ncols, row);
  /*  for(i=0; i<ncols; i++)
    {
        if(row[i]>row[i+1])
        {
            row[i]=temp;
            row[i]=row[i+1];
            row[i+1]=temp;
        }
    }    
    printVect(ncols, row);*/

}
void printVect(int ncols, double *row)
{
    int i;
    printf("Vector:\n");
    for(i=0; i<ncols; i++)
    {
        printf("%lf", row[i]);
    }
    
}
void readMatrix(FILE *fp, int nrows, int ncols, double **matrix)
{
    int i, j;
    for(i=0; i<nrows; i++)
    {
        for(j=0; j<ncols; j++)
        {
            fscanf(fp, "%lf", &matrix[i][j]);
        }
    }
}

void printMatrix(int nrows, int ncols, double **matrix)
{
    int i, j;
    printf("Matrix:\n");
    for(i=0; i<nrows; i++)
    {
        for(j=0; j<ncols; j++)
        {
            printf("%.4lf\t", matrix[i][j]);
        }
        printf("\n");
    }
}
double **allocMatrix(int nrows, int ncols)
{
    int i;
    double **matrix;
    matrix=calloc(nrows, sizeof(double));

    for(i=0; i<nrows; i++)
    {
        matrix[i]=calloc(ncols, sizeof(double));
    }

    if(matrix==NULL)
    {
        perror("matrix");
    }
    else
    printf("Memory allocated correctly\n");
    return matrix;
}

int readDim(FILE *fp)
{
    int num;
    fscanf(fp, "%d", &num);
    return num;
}

I am sure there is some sort of error because the output i get from VSCode is:

File name: matrice.txt
File read correctly
Rows: 6 Columns: 8
Memory allocated correctly
Matrix:
0.8147  0.2785  0.9572  0.7922  0.6787  0.7060  0.6948  0.7655
0.9058  0.5469  0.4854  0.9595  0.7577  0.0318  0.3171  0.7952
0.1270  0.9575  0.8003  0.6557  0.7431  0.2769  0.9502  0.1869
0.9134  0.9649  0.1419  0.0357  0.3922  0.0462  0.0344  0.4898
0.6324  0.1576  0.4218  0.8491  0.6555  0.0971  0.4387  0.4456
0.0975  0.9706  0.9157  0.9340  0.1712  0.8235  0.3816  0.6463

TL;DR

Everything is fine until the program has to execute the "mediana" function, I think the error is row[i]=matrix[i][col]; but the compiler doensn't show any error or warning, what should I do?

Input file:

6 8
0.8147    0.2785    0.9572    0.7922    0.6787    0.7060    0.6948    0.7655
0.9058    0.5469    0.4854    0.9595    0.7577    0.0318    0.3171    0.7952
0.1270    0.9575    0.8003    0.6557    0.7431    0.2769    0.9502    0.1869
0.9134    0.9649    0.1419    0.0357    0.3922    0.0462    0.0344    0.4898
0.6324    0.1576    0.4218    0.8491    0.6555    0.0971    0.4387    0.4456
0.0975    0.9706    0.9157    0.9340    0.1712    0.8235    0.3816    0.6463

The problem is here:

row[i] = matrix[i][col];

which should rather be:

row[i] = matrix[col][i];

You mixed up something.

You also should free the memory at the end of mediana with:

free(row);

BTW if you run your code with your debugger (don't ask me how to do this on your platform), your program would most likely have crashed into the debugger showing you the exact line where the crash happended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM