Dynamic 2D Allocated Arrays and helper functions.

This is a discussion on Dynamic 2D Allocated Arrays and helper functions. within the C Programming forums, part of the General Programming Boards category; I'm writing a program which reads a .txt file and creates a array from it. The first integer in the ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    16

    Dynamic 2D Allocated Arrays and helper functions.

    I'm writing a program which reads a .txt file and creates a array from it. The first integer in the file is the number which represents what the dimensions of the 2D array is going to be. I've succesfully have read the integers into the array but I'm having trouble with the helper function.

    My txt file is:
    7
    1 2 3 4 5 6 8
    1 1 3 4 5 6 9
    1 6 1 8 5 4 3
    8 3 4 1 6 7 5
    4 5 6 3 1 9 7
    2 5 6 8 3 1 8
    3 4 5 6 7 8 1
    and my code is:

    Code:
    #include <stdio.h>
    int checkdiag (int matrix[], int col, int row);
    
    
    int
    main (void)
    
    
    {
    FILE *f1;
    int col,row,row2,both,both2, *matrix, i, j,q;
    
    /*Opened up my .txt file (named Matrix8.txt)*/
    f1= fopen("Matrix8.txt", "r");
    
    /*got the integer value in the beginning*/
    fscanf (f1, "%d", &both);
    printf ("The dimensions of the matrix is %dX%d\n\n", both,both);
    
    
    /*if the dimensions is greater than 100, than it is invalid*/
    
    
    if (both > 100)
    {
    printf ("The matrix size is invalid. It has to be shorter than: 100X100.\n\n");
    }
    
    
    /*made it into a dynamic allocated array*/
    col=both;
    row=both+1;
    row2=both;
    matrix = (int *) calloc (row2*col, sizeof(int));
    
    
    
    
    /*scaned the file and printed it on the screen*/
    printf ("The array is: \n");
    for (i=1; i<row; i++)
    {
        for (j=0; j<col; j++)
        {
            fscanf (f1, "%d", &matrix[i*col+j]);
        }
    
    
    }
        
    
    
    
    
    for (i=1; i<row; i++)
    {
        for (j=0; j<col; j++)
        {
            printf ("%d ", matrix[i*col+j]);
        }
    printf("\n");
    }
        
    printf ("\n\n");
    
    
    /*This is where i am mainly have trouble. From this point my program has an issue*/
    printf ("%d\n\n", checkdiag (matrix,col,row));
    
    
    free (matrix);
    fclose(f1);
    return (0);
    
    
    
    
    }
    
    
    /*main program ends*/
    
    
    
    
    /*helper function to read the diagonal and display 1 if all the integers are the same or display 0 if they are not*/
    int checkdiag (int matrix[], int col, int row)
    
    
    {
    int both,i,j,t,row2,c,c1;
    FILE *f1;
    
    
    f1= fopen("Matrix8.txt", "r");
    fscanf (f1, "%d", &both);
    
    
    
    
    row=both+1;
    col=both;
    row2=both;
    
    
    
    
    /*I am pretty sure that this is where the problem is occuring but I do not know how to fix it.*/
    for (i=1; i<row; i=j)
    {
    j=0;
        for (j=0; j<col; j++)
        {
            
            c1=matrix[i * col+j];
            
        }
        c=matrix[i*col+j];
                if (c==c1)
                
                        {
                        t=1;
                        }
                        else
                            {
                            t=0;
                            }
    }
    
    
    
    
    
    
    
    
    fclose(f1);
    return(t);
    
    
    }
    The purpose of my program is to read the diagonal of the matrix in the txt file and if the diagonal is the same (has the same integers throughout the main diagonal) it will return a 1 but if it is not than it will return a 0.
    Last edited by zeon9881; 03-29-2012 at 08:00 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's not really a dynamic 2D array. If you do want that, you matrix should actually be a int **, and that's what your function argument should be as well. Search the forums if you want to see how to actually do that. Otherwise you need to describe better what your problem is.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    The purpose of my program is to read the diagonal of my matrix, determine if the integers in the diagonal are the same and if they are; it will display a 1. If the integers are not the same than it will display a zero.

    If this isn't a dynamic array than what is this?
    Also, I was wondering if i declare the array in my main function do i have to repeat the whole process all over again in my helper functions?

    I.E. I made my array in my main function, i made it read the file, scan it and put all the values into "matrix", my array. Do I have to redo this whole procedure in my helper function? Will I have to read the file again, scan it again to input the values into the array or is it that when I declare the array in my argument, it will just read my main function and store the values into it.
    Last edited by zeon9881; 03-29-2012 at 08:37 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's a dynamic array. It's just a one dimensional array. Doing math to figure out what element to access doesn't actually make it multi-dimensional.

    If you already have the array filled, just walk through it diagonally and see if the numbers match. Increment row and column by equal amounts to walk diagonally.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    Alright, though I still have the issue where my file stops responding when I hit run. I've been trying to fix it for a while now but I can't locate the problem. It says that the program has stopped working every time I run it.

    The issue is in my helper function.

    I changed the arguments in my helper function : int checkdiag (int *matrix, int col, int row)but it's still returning an error.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    I don't understand how to make the program only go through the diagonal of the matrix.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing Dynamic (Allocated) Array?
    By Mekeor in forum C Programming
    Replies: 7
    Last Post: 02-18-2011, 11:27 AM
  2. Freeing dynamic allocated memory of structure
    By darekg11 in forum C Programming
    Replies: 14
    Last Post: 01-10-2011, 09:17 AM
  3. Freeing Dynamic allocated memory
    By TiNkiN in forum C Programming
    Replies: 10
    Last Post: 10-26-2010, 05:39 AM
  4. Global acess to dynamic allocated data
    By dereach in forum C Programming
    Replies: 2
    Last Post: 02-26-2008, 12:10 AM
  5. Helper functions: design issue
    By anon in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 03:44 PM

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