0

Note : Actually the question is to print diagonal of the matrix in zigzag way Given a 2D matrix, print all elements of the given matrix in diagonal order. For example, consider the following 5 X 4 input matrix.

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

Diagonal printing of the above matrix is

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

Now i'm just printing the subscripts of the array

#include<stdio.h>
#include<stdlib.h>
int main(){
   int rows,cols,ind,inner,outer;
    scanf("%d %d",&rows,&cols);
   int **ptr=(int**)malloc(rows*sizeof(int*));
   for(ind=0;ind<rows;ind++)
          *(ptr+ind)=(int*)malloc(cols*sizeof(int));
for(outer=0;outer<cols;outer++){
         for(inner=0;inner<rows;inner++){
                  scanf("%d ",(*(ptr+outer)+inner));
            }
     }
inner=0,outer=0;
for(ind=1;ind<rows+cols;ind++){
     printf("%d",ind);
    while(*(*(ptr+outer)+inner)!=0)    {
            printf("%d %d",outer,inner);
               inner++;
            outer--;
            }printf("\n");
    }
    return 0;
}

Also for Arrays without malloc

#include<stdio.h>
#include<stdlib.h>
int main(){
    int rows,cols,ind,inner,outer;
    scanf("%d %d",&rows,&cols);
    int arr[rows][cols];
    for(outer=0;outer<rows;outer++){
        for(inner=0;inner<cols;inner++){
            scanf("%d ",&arr[outer][inner]);
        }
    }
    inner=0,outer=0;
    for(ind=0;ind<rows;ind++){
    printf("%d",ind);
    while(arr[outer][inner]){
        //printf("%d %d",outer,inner);
        inner++;
        outer--;
        }
    printf("\n");
    outer=ind;
    inner=0;
    }
    return 0;
}
  • Indent your code. Do not cast the return value of malloc. Use array notation instead of `ptr+ind`. Use debugger. – DYZ Jun 23 '18 at 06:18
  • https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb – Yunnosch Jun 23 '18 at 06:23
  • Please delete as much of your code as possible without loosing the described problem. That is part of making a [mcve]. – Yunnosch Jun 23 '18 at 06:28
  • Add lots of `printf("I am here .\n");` lines, to identify where the segfault occurs. – Yunnosch Jun 23 '18 at 06:30
  • 1
    You seem to have `cols` and `rows` reversed in the data entry loop. Hence the segfault because the arrays are not contiguous. – Weather Vane Jun 23 '18 at 07:00
  • . . . you are either accessing beyond the bounds of each data row (`rows > cols`), or using an unallocated pointer by breaking the pointers array (`cols > rows`). – Weather Vane Jun 23 '18 at 07:08
  • And unlike a 2-D array you can't flow from one row to the next. – Weather Vane Jun 23 '18 at 07:11
  • i m only getting error in the below condition of while loop , while(*(*(ptr+outer)+inner)!=0) *how to check whether there is an array element * – SRIARAVIND VADUGANATHASAMY Jun 23 '18 at 07:12
  • problem is only with the malloc use. – HarshitMadhav Jun 23 '18 at 07:21
  • now also i'm getting errors sorry i don't know how to block this code in separate lines #include #include int main(){ int rows,cols,ind,inner,outer; scanf("%d %d",&rows,&cols); int arr[rows][cols] for(outer=0;outer – SRIARAVIND VADUGANATHASAMY Jun 23 '18 at 07:28
  • Questions that ask "please help me" tend to be looking for highly localized guidance, or in some cases, ongoing or private assistance, which is not suited to our Q&A format. It is also rather vague, and is better replaced with a more specific question. Please read [Why is “Can someone help me?” not an actual question?](//meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question). – Stargateur Jun 23 '18 at 07:44

1 Answers1

0

Firstly, casting of malloc() is not needed, read this Do I cast the result of malloc? .

int **ptr=malloc(rows*sizeof(*ptr));
for(ind=0;ind<rows;ind++)
     *(ptr+ind)=malloc(cols*sizeof(**ptr));

And outer for loop should rotate rows times and inner for loop should rotate cols times as you allocate memory for ptr[rows][cols] not for ptr[cols][rows].

Changes it like

for(outer=0;outer<rows;outer++){
    for(inner=0;inner<cols;inner++){ 
        scanf("%d ",(*(ptr+outer)+inner)); 
    } 
}
Achal
  • 11,821
  • 2
  • 15
  • 37