edited by
531 views
1 votes
1 votes
#include <stdio.h>
int main() {
    int A[20][30];

    int i,j,*p;
    
    p = &A[0][0];
    
    for(i=0;i<20;i++) {
    
      for(j=0;j<20;j++) {
    
        *(p+20*j+i) = i*30+j;
    
      }
    
    }
    
    printf("%d\n",A[6][10] - A[6][9]); 	
}

O/P ??

edited by

1 Answer

Best answer
4 votes
4 votes
printf("%d\n",A[6][10] - A[6][9]);

  • $\text{A[6][10]}$ - $7$ th rwo and $11$ th column. How many elements before $\text{A[6][10]}$ in array A
    • To count that, above this element, there are $6$ rows ($30*6 = 180$ $\text{elements}$) and in $7$th row we have $10$ elements before $\text{A[6][10]}$. So, total $190$ elments before $\text{A[6][10]}$ in matrix A.
    • $\Rightarrow$ *(p+190) will be the location of $\text{A[6][10]}$.
  • Similarly for  $\text{A[6][9]}$, *(p+189) will be the location.
  • Reason behind evaluating locations like this is row major order storage of C style arrays

Last thing to do: represent *(p+190) and *(p+189) in terms of the given assignment format i.e *(p+20*j+i)

  • *(p + 190)  = *( p + 20*9 +10) $\Rightarrow$ i = 10 and j = 9 $\Rightarrow$ A[6][10] = $10*30 + 9$ = $309$
  • *(p + 189)  = *( p + 20*9 + 9) $\Rightarrow$  i = 9 and j = 9  $\Rightarrow$ A[6][9] = $9*30 + 9$ = $279$
  • $\Rightarrow$ A[6][10] - A[6][9] $\rightarrow$ 30.

edited by

Related questions

5
views
1 answers
0 votes
shivamSK asked 5 hours ago
5 views
#include <stdio.h> int main () { int i, k; int a [8] = {33,34,35,36,37,40,50,38}; for(i = 0; i < 3; i++) { a[i] = a[i] + 1; i=i+1; } ... int i = k/2; a[i] = a[i] - 1; } printf ("%d",i+a[i]); }Numerical Answer ______________________________
108
views
1 answers
0 votes
shivamSK asked 6 days ago
108 views
what is output of c code : assume answer Is numerical...🙂
89
views
2 answers
0 votes
shivamSK asked Jun 8
89 views
answer the output#include <stdio.h> int main() { int x=0; x++; if(--x){ x=x++; if(x){ printf("hello"); }else{ printf("%d",x); } }else{ x=x+5; if(x){ printf("i win x times"); } else{ printf("you win 5 times"); } } return 0; } 
241
views
1 answers
0 votes
Debargha Mitra Roy asked Apr 16
241 views
#include <stdio.h> int main() { int a[3][2] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)The output is __________.