Home > OS >  C program is ending by itself during nested loop
C program is ending by itself during nested loop

Time:11-06

My C program is ending on it's own and I can't figure out the issue. I have added a comment above the loop where it is stopping.

I am creating a program to match dna samples in arrays with each other. The dna samples are floats and I am reading them from a file.

This is the file I am reading from:

2.3 3.3 4.5 6.7 7.8 2.1 3.2 4.3 5.2 6.5
5
2.3 3.3 4.5 6.7 7.8 2.1 3.2 4.3 5.2 6.5
1.3 0.3 9.5 8.7 5.8 4.1 3.2 2.3 6.2 6.9
6.3 9.3 4.3 6.4 7.5 2.9 3.0 4.1 5.3 6.5
6.1 9.4 4.5 6.6 7.4 2.8 3.2 4.4 5.0 6.0
2.3 3.3 4.5 6.6 7.8 2.2 3.2 4.3 5.2 6.5

The expected output is all the values will be printed (5 lines for the criminal samples) and the first line will be matched and the rest will not. However this is my output:

Reading chromosomes of the suspect. 
2.3 3.3 4.5 6.7 7.8 2.1 3.2 4.3 5.2 6.5
Reading chromosomes of the criminals.
2.3 3.3 4.5 6.7 7.8 2.1 3.2 4.3 5.2 6.5
1.3 0.3 9.5 8.7 5.8 4.1 3.2 2.3 6.2

I have attempted debugging it and at one point it was randomly assigning my sizeR variable to like 10000 or something during the loop and I assumed that was the problem somewhere but now it's not I'm struggling to understand why it is still stopping for me.

Code:

#include <stdio.h>
#include <stdbool.h>

FILE *fp;

int main(){
    fp = fopen("dna_input.txt", "r");

    int sizeR = 0, sizeC = 10; // declare size variables
    float suspect[sizeC]; // declaring suspect array
    float criminal[sizeR][sizeC]; // declaring criminal array

    // reads 10 input values from first line of the file
    printf("Reading chromosomes of the suspect. \n");
    for (int i = 0; i < sizeC; i  ){
        fscanf(fp, " %f", &suspect[i]);
        printf("%.1f ", suspect[i]);
    }
    printf("\n");

    // reads integer from 2nd line of file for the amount of lines to read for next loop
    fscanf(fp, " %d", &sizeR);

    printf("Reading chromosomes of the criminals. \n"); 
    // read 10 input values into 5 criminal arrays    
    // THIS LOOP IS WHERE MY PROGRAM IS STOPPING <---------------------------------------------
    for (int i = 0; i < sizeR; i  ){
        for (int j = 0; j < sizeC; j  ){
            fscanf(fp, " %f", &criminal[i][j]);
            printf("%.1f ", criminal[i][j]);
        }
        printf("\n");
    }        

    // check for match
    bool match = true;
    for (int i = 0; i < sizeR; i  ){
        for (int j = 0; j < sizeC; j  ){
            if (suspect[j] != criminal[i][j]){
                match = false;
            }  
        }    
        // display matching result
        if (match)
            printf("The two profiles match! \n");
        else
            printf("The two profiles don't match! \n");
    }

    fclose(fp);
    return 0;
}

CodePudding user response:

You need to dynamically allocate the criminals array.

//Here sizeR is 0 so you are declaring criminal[0][10]      
float criminal[sizeR][sizeC]; // declaring criminal array

Typical buffer overflow problem.

CodePudding user response:

Thank you for the comments, I missed that completely. I've moved my criminal array to be declared after the fscanf

// reads integer from file for the amount of lines to read
fscanf(fp, " %d", &sizeR);

// declare criminal aray
float criminal[sizeR][sizeC];

  • Related