Infinite Loop

This is a discussion on Infinite Loop within the C Programming forums, part of the General Programming Boards category; I think that is my problem but I cannot seem to find it. The program is to take a file ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    17

    Infinite Loop

    I think that is my problem but I cannot seem to find it. The program is to take a file in that contains a cost matrix, and print out a table for the shortest path. I used a second array to store the three columns of visited, cost, and previous node(row in my case). I've given up on using multiple files / methods for now focusing on the actual task.

    Code:
    #include "prog5.h"
    
    int main(int argc, char *argv[]){
    
      int size, i, j, k, matrix_length, prims_length, row, column;
    
      int *matrix, *prims;
    
      FILE *infile;
    
      infile = fopen("prog5.in", "r");
    
      fscanf(infile, "%d", &size);
    
      matrix = (int *)malloc(size*size*sizeof(int));    /*Create an array of
                                                        integers large enough to
                                                        hold the matrix*/
    
      prims = (int *)calloc(size*3,sizeof(int));        /*Create an array of
                                                        integers large enough to
                                                        hold Prim's 3 column chart
                                                        and sets initial to 0*/
    
      matrix_length = size*size;                        /*gets the length of the
                                                        matrix array*/
    
      prims_length = size*3;         /*gets the length of the
                                                        matrix array*/
    
      while(!feof(infile)){                             /*while End of File is not
                                                        true*/
    
    
          for(i=0; i<matrix_length; i++){
           fscanf(infile, "%d", &matrix[i]);            /*fills the matrix array*/
    
    
          }/*for(i=0; i<length; i++){...*/
    
      }/*while(!feof(infile)){...*/
    
      for(i=0; i<prims_length; i++){
    
            prims[i] = 0;
    
      }
    
    
    for(j=0; j<matrix_length; j++){
    
            printf("%d ", matrix[j]);
    
            if(j && j%size == 0)
            printf("\n");
    
    }
    
         printf("\n");
    
    
    
         for(j=0; j<prims_length; j=j+3){
    
              printf("%d %d %d\n", prims[j],prims[j+1],prims[j+2]);
    
         }/*for(j=0; j<prim_length; j=j+3){...*/
    
    
         printf("\n");
    
    
      fclose(infile);
    
      row = 0;
     for(i=0; i < size; i++){
    
          prims[3*row] = 1;   /*set current row to visited*/
    
          for(column = 0; column < size; column++){
    
                if(matrix[(size*row)+column] != 0){
    
                     if(matrix[(size*row)+column] < prims[(3*row)+1]){
    
                             prims[(size*column)+1] = matrix[(size*row)+column];
                             prims[(size*column)+2] = row;
    
                     }else if(prims[(3*row)+1] == 0){
    
                             prims[(size*column)+1] = matrix[(size*row)+column];
                             prims[(size*column)+2] = row;
    
                     }
    
                                 /*sets cost and previous column in the prims array
                                 for the current column in the matrix if the cost
                                 is lower or zero*/
    
                }/*if(matrix[size*row+column] != 0){...*/
    
          }/*for(column = 1; column < size; column++){...*/
    
    
          for(j=0; j<size; j++){
    
            if(prims[3*j] == 0){ /*if the row isn't visited*/
    
                    for(k=0; k<size; k++){
    
                            if(k=j){ /*if the row is being compared to itself*/
    
                                    /*do nothing*/
                            }else if(prims[3*k] == 0){      /*if the row being compared to has not been visited*/
    
                                    if(prims[3*j+1]<prims[3*k+1]){ /*if the cost is less than the row being compared to*/
    
                                            row = 3*j;
    
                                    }/*if(prims[3*j+1]<prims[3*k+1]){...*/
    
                            }/*else if(prims[3*k] == 0){...*/
    
                    }/*for(k=0; k<size; k++){...*/
    
            }/* if(prims[3*j] == 0){...*/
    
          }/*for*/
    
    
         for(j=0; j<prims_length; j=j+3){
    
              printf("%d %d %d\n", prims[j],prims[j+1],prims[j+2]);
    
         }/*for(j=0; j<prim_length; j=j+3){...*/
    
    
         printf("\n");
    
    
      }/*while(i=0; i<size; i++){...*/
    
    
      free(matrix);
    
      free(prims);
    
      return 0;
    
    }/*int main(int argc, char *argv[]){...*/

  2. #2
    Registered User
    Join Date
    Jul 2009
    Posts
    3
    Have you stepped into it with a debugger yet? Which loop is it getting stuck in?

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    17
    Well I downloaded codeblocks and found a couple errors, but now I'm having trouble freeing the memory allocated to my two arrays. I added a couple loops near the end but that doesn't seem to fix it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[]){
    
      int size, i, j, k, matrix_length, prims_length, row, column;
    
      int *matrix, *prims;
    
      FILE *infile;
    
      infile = fopen("prog5.in", "r");
    
      fscanf(infile, "%d", &size);
    
      matrix = (int *)malloc(size*size*sizeof(int));    /*Create an array of
                                                        integers large enough to
                                                        hold the matrix*/
    
      prims = (int *)calloc(size*3,sizeof(int));        /*Create an array of
                                                        integers large enough to
                                                        hold Prim's 3 column chart
                                                        and sets initial to 0*/
    
      matrix_length = size*size;                        /*gets the length of the
                                                        matrix array*/
    
      prims_length = size*3;         /*gets the length of the
                                                        matrix array*/
    
      while(!feof(infile)){                             /*while End of File is not
                                                        true*/
    
    
          for(i=0; i<matrix_length; i++){
           fscanf(infile, "%d", &matrix[i]);            /*fills the matrix array*/
    
    
          }/*for(i=0; i<length; i++){...*/
    
      }/*while(!feof(infile)){...*/
    
      for(i=0; i<prims_length; i++){
    
            prims[i] = 0;
    
      }
    
    
    for(j=0; j<matrix_length; j++){
    
            printf("%d ", matrix[j]);
    
            if(j && j%size == 0)
            printf("\n");
    
    }
    
         printf("\n");
    
    
    
         for(j=0; j<prims_length; j=j+3){
    
              printf("%d %d %d\n", prims[j],prims[j+1],prims[j+2]);
    
         }/*for(j=0; j<prim_length; j=j+3){...*/
    
    
         printf("\n");
    
    
      fclose(infile);
    
      row = 0;
     for(i=0; i < size; i++){
    
          prims[3*row] = 1;   /*set current row to visited*/
    
          for(column = 0; column < size; column++){
    
                if(matrix[(size*row)+column] != 0){
    
                     if(matrix[(size*row)+column] < prims[(3*row)+1]){
    
                             prims[(size*column)+1] = matrix[(size*row)+column];
                             prims[(size*column)+2] = row;
    
                     }else if(prims[(3*row)+1] == 0){
    
                             prims[(size*column)+1] = matrix[(size*row)+column];
                             prims[(size*column)+2] = row;
    
                     }
    
                                 /*sets cost and previous column in the prims array
                                 for the current column in the matrix if the cost
                                 is lower or zero*/
    
                }/*if(matrix[size*row+column] != 0){...*/
    
          }/*for(column = 1; column < size; column++){...*/
    
    
          for(j=0; j<size; j++){
    
            if(prims[3*j] == 0){ /*if the row isn't visited*/
    
                    for(k=0; k<size; k++){
    
                            if(k==j){ /*if the row is being compared to itself*/
    
                                    /*do nothing*/
                            }else if(prims[3*k] == 0){      /*if the row being compared to has not been visited*/
    
                                    if(prims[3*j+1]<prims[3*k+1]){ /*if the cost is less than the row being compared to*/
    
                                            row = 3*j;
    
                                    }/*if(prims[3*j+1]<prims[3*k+1]){...*/
    
                            }/*else if(prims[3*k] == 0){...*/
    
                    }/*for(k=0; k<size; k++){...*/
    
            }/* if(prims[3*j] == 0){...*/
    
          }/*for*/
    
    
         for(j=0; j<prims_length; j=j+3){
    
              printf("%d %d %d\n", prims[j],prims[j+1],prims[j+2]);
    
         }/*for(j=0; j<prim_length; j=j+3){...*/
    
    
         printf("\n");
    
    
      }/*while(i=0; i<size; i++){...*/
    
    
    
    for(i = 0; i < matrix_length;  i++) {
    
        free(matrix[i]);
    
    }
    
    for(i = 0; i < prims_length;  i++) {
    
        free(prims[i]);
    
    }
    
      free(matrix);
    
      free(prims);
    
      return 0;
    
    }/*int main(int argc, char *argv[]){...*/

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    17
    This is almost complete, but for some reason it is not updating the unchanged rows to visited if the values in the cost column do not change. Any ideas?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[]){
    
        int size, i, j, k, matrix_length, prims_length, row, column;
    
        int *matrix, *prims;
    
        FILE *infile;
    
        infile = fopen("prog5.in", "r");
    
        fscanf(infile, "%d", &size);
    
        matrix = (int *)malloc(size*size*sizeof(int));  /*Create an array of
                                                        integers large enough to
                                                        hold the matrix*/
    
        prims = (int *)calloc(size*3,sizeof(int));  /*Create an array of
                                                    integers large enough to
                                                    hold Prim's 3 column chart
                                                    and sets initial to 0*/
    
        matrix_length = size*size;                  /*gets the length of the
                                                    matrix array*/
    
        prims_length = size*3;                      /*gets the length of the
                                                    matrix array*/
    
        while(!feof(infile)){                       /*while End of File is not
                                                    true*/
    
            for(i=0; i<matrix_length; i++){
    
                fscanf(infile, "%d", &matrix[i]);   /*fills the matrix array*/
    
            }/*for(i=0; i<length; i++){...*/
    
        }/*while(!feof(infile)){...*/
    
        fclose(infile);                             /*close the infile*/
    
        row = 0;                                    /*initialize row to zero*/
    
        for(i=0; i < size; i++){
    
            prims[3*row] = 1;                       /*update the visited status
                                                    of the current row*/
    
            for(column = 0; column < size; column++){
    
                if(matrix[(size*row)+column] > 0){
    
                                                    /*checks to see if the current
                                                    row is connected to the column*/
    
                     if(matrix[(size*row)+column] < prims[(3*row)+1]){
    
                                                    /*if the weight of the connection
                                                    is less than what is recorded*/
    
                             prims[(3*column)+1] = matrix[(size*row)+column];
                             prims[(3*column)+2] = row;
    
                                                    /*change the weight in prims to
                                                    match and set previous row to
                                                    current row*/
    
                     }else if(prims[(3*row)+1] == 0){
    
                                                    /*if the recorded weight is zero*/
    
                             prims[(3*column)+1] = matrix[(size*row)+column];
                             prims[(3*column)+2] = row;
    
                                                    /*change the weight in prims to
                                                    match and set previous row to
                                                    current row*/
    
                     }/*else if(prims[(3*row)+1] == 0){...*/
    
                }/*if(matrix[size*row+column] != 0){...*/
    
          }/*for(column = 1; column < size; column++){...*/
    
    
          for(j=0; j<size; j++){
    
            if(prims[3*j] == 0){ /*if the row isn't visited*/
    
                    for(k=0; k<size; k++){
    
                            if(prims[3*k] == 0 && k!=j){ /*if the row is being compared to itself*/
    
                                if(prims[3*j+1]<prims[3*k+1]){ /*if the cost is less than the row being compared to*/
    
                                    row = j;
    
                                }/*if(prims[3*j+1]<prims[3*k+1]){...*/
    
                            }/*else if(prims[3*k] == 0){...*/
    
                    }/*for(k=0; k<size; k++){...*/
    
            }/* if(prims[3*j] == 0){...*/
    
          }/*for*/
    
            for(j=0; j<prims_length; j=j+3){
    
                printf("%d %d %d\n", prims[j],prims[j+1],prims[j+2]);
    
            }/*for(j=0; j<prim_length; j=j+3){...*/
    
            printf("\n");
    
        }/*while(i=0; i<size; i++){...*/
    
    
    
    
    
      free(matrix);
    
      free(prims);
    
      return 0;
    
    }/*int main(int argc, char *argv[]){...*/
    Code:
    prog5.in
    
    4
    0 1 3 4
    1 1 2 2
    3 2 0 2
    4 2 2 0
    Code:
    output
    
    1 0 0
    0 1 0
    0 3 0
    0 4 0
    
    1 0 0
    0 2 2
    1 3 0
    0 2 2
    
    1 0 0
    0 2 2
    1 3 0
    0 2 2
    
    1 0 0
    0 2 2
    1 3 0
    0 2 2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 03:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21