381 views
6 votes
6 votes

Consider the following C program fragment:

#include<stdio.h>
int my_array[20][10];
int main()
{
    int *p = my_array, i;
    for(i = 0; i < 10*20; i++)
    {
       *(p+X) = i;
    }
    ...
}

The correct replacement for 'X' so that every position in the array my_array gets initialized is

  1. (i / 20) * 10 + (i% 20) * 10;
  2. (i % 20) * 10 + (i / 20) * 10;
  3. (i / 10) * 20 + (i% 10) * 20;
  4. (i % 10) + (i / 10) * 10;

2 Answers

Best answer
4 votes
4 votes
This is linearization of a $2$-D array.

$i/10$ gives the row number in $2$D and $i\%10$ gives the column number.

Correct Option: D.
selected by
Answer:

Related questions

9 votes
9 votes
1 answer
2
gatecse asked Jul 26, 2020
640 views
What will be the output of the following C program:#include<stdio.h int main() { char* disease = "COVID-19"; (*disease)++; printf("%s",disease); }DPWJE-$20$DOVID-$19$COVI...