Program crash while trying to print a bidimensional array

212 views Asked by At

as the title says, my program crashes when i try to print a bidimensional array. The error is surely printf in function printarray, but i couldn't understand why it lead to crash.

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define COL 40
#define ROW 40
void printarray(int array[COL][ROW], int col, int row);

main (){
    int n,m,p,q;
    int array[COL][ROW];
    printf("Dammi 2 numeri \n"); scanf("%d",&n); scanf("%d",&m);
    do{
        printf("Dammi 2 p[<n] e q[<m] \n"); scanf("%d",&p); scanf("%d",&q);
    }while(p>=n || q>=m);

    printf("Mi hai dato: n= %d m= %d p= %d q= %d \n",n,m,p,q);

         int i,j;
         int random;
         srand(time(NULL));
         for(i=0; i<=n;i++){
              for(j=0; j<=m;j++){
                 do{   
                       random = rand() % 10;
                 }while(random == 0);
                 printf("\n i am at array[%d][%d] with number: %d\n",i,j,random);
                 array[i][j] = random;
              }
         }

    //printf("lol0 ->>>>>>>>>>>%d<--------",array[0][0]);
    printarray(array[n][m],n,m);

system("PAUSE");
}

void printarray(int array[COL][ROW], int col, int row){
     int i,j;
     for(i=0; i<=col;i++){
              //printf("lol3 %d",i);
              for(j=0; j<=row; j++){
                       printf("%d",array[i][j]);
              }
              printf("\n");
     }

}
3

There are 3 answers

0
suren On BEST ANSWER

here i am attaching the your program without any error. the problem was to calling the printarray function.

     #include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define COL 40
#define ROW 40
void printarray(int array[COL][ROW], int col, int row);

main (){
    int n,m,p,q;
    int array[COL][ROW];
    printf("Dammi 2 numeri \n"); scanf("%d",&n); scanf("%d",&m);
    do{
        printf("Dammi 2 p[<n] e q[<m] \n"); scanf("%d",&p); scanf("%d",&q);
    }while(p>=n || q>=m);

    printf("Mi hai dato: n= %d m= %d p= %d q= %d \n",n,m,p,q);

         int i,j;
         int random;
         srand(time(NULL));
         for(i=0; i<=n;i++){
              for(j=0; j<=m;j++){
                 do{   
                       random = rand() % 10;
                 }while(random == 0);
                 printf("\n i am at array[%d][%d] with number: %d\n",i,j,random);
                 array[i][j] = random;
              }
         }

    //printf("lol0 ->>>>>>>>>>>%d<--------",array[0][0]);
    printarray(array,n,m);

system("PAUSE");
}

void printarray(int array[COL][ROW], int col, int row){
     int i,j;
     for(i=0; i<=col;i++){
              //printf("lol3 %d",i);
              for(j=0; j<=row; j++){
                       printf("%d",array[i][j]);
              }
              printf("\n");
     }

}
0
user3629249 On

The posted code does not cleanly compile, for several reasons.

1) missing

 #include <time.h>  

2) function main() ALWAYS returns 'int' so a proper main statement would be: 'int main( void )'

3) the first parameter to 'printarray()' is defined to be a pointer to a 2 dimensional array.

However,

a) the actual call passes the contents of a specific 'cell' in the array.

b) the passed 'cell' is beyond the end of the array.

Suggest: 'printarray( array, n, m )' as the calling statement.

Note, in C, an array name degrades to the address of the first entry in the array.

4) an array should be defined as arrayName[numRows][numColumns].

This will become much more important when declaring an array of pointers where each pointer will point to the contents of the associated row.

In the posted code, the definition/naming is backwards from typical.

in Memory, an array is laid out left to right (the columns) then top to bottom (the rows).

The 'biggest' index is rows and should be listed first.

5) to avoid text replacement problems, numeric values in macros should be wrapped in parens.

6) as a suggestion, appropriate vertical spacing (blank line) between code blocks makes the code much clearer and more understandable by us humans

7) when calling scanf() (and family of functions) always check the returned value (not the parameter values) to assure the operation was successful

8) consistent indentation makes the code much easier to read/understand by us humans, suggest: indent 4 spaces after every opening brace '{' and un-indent before every closing brace '}'. Note: Never use tabs for indenting. Different environments have different tab widths and/or different tab stops.

9) for readability, and to make debug much easier, only place one statement per line of code. This applies to declaring variables, so they can be easily commented and applies to executable statements

2
Vlad from Moscow On

If an array dimension has size N then the valid range of indices is [0, N - 1]. So the loops in the function should look like

void printarray(int array[COL][ROW], int col, int row){
     int i,j;
     for(i=0; i< col;i++){
              //printf("lol3 %d",i);
              for( j=0; j<row; j++){
                       printf("%d",array[i][j]);
              }
              printf("\n");
     }

}

The same is valid for loops in main.

This call of the function invalid

printarray(array[n][m],n,m);

There shall be

printarray(array,n,m);

Take into account that such a declaration of an array like

int array[COL][ROW];

is very confusing. It would be more correctly to write

int array[ROW][COL];
          ^^^  ^^^

Also it is not clear what is the meaning of variables p and q in this loop

does not make sense
    printf("Dammi 2 numeri \n"); scanf("%d",&n); scanf("%d",&m);
    do{
        printf("Dammi 2 p[<n] e q[<m] \n"); scanf("%d",&p); scanf("%d",&q);
    }while(p>=n || q>=m);

It seems that this loop is from some other program.:)

And you have to check that n and m are not greater than ROW and COL.

Also it is a bad idea to mix l'Italian with English.:)