India's Largest Free Online Education Platform

C program to find the address of 2-D array element

C program to find the address of 2-D array element

#include <stdio.h>     // header files
#include <stdlib.h>
#include<math.h>
int main()     // main function
{ 
  long int LocR,LocC,B,W,M,N,i,j,Lr,Ur,Lc,Uc,check;  // declaration of variables
  printf("This is a simple program to find the address the address location of 'a[i][j]' in 2D array\n");
  
  printf("Enter W that is size of one array element \n");
  scanf("%d",&W);

  printf("Enter Base address\n");
  scanf("%d",&B);

  printf("Enter Lower Limit Of Row\n");
  scanf("%d",&Lr);

  printf("Enter Upper Limit Of Row\n");
  scanf("%d",&Ur);

  printf("Enter Lower Limit Of Column\n");
  scanf("%d",&Lc);

  printf("Enter Upper Limit of Column\n");
  scanf("%d",&Uc);

  printf("Enter Row Subscript whose address to be found\n");
  scanf("%d",&i);

  printf("Enter Colum Subscript whose address to be found\n");
  scanf("%d",&j);

  M=(Ur-Lr)+1;     // "M" is no. of Rows
  N=(Uc-Lc)+1;     // "N" is no. of Columns
  
  LocR=B+(W*(N*(i-Lr)+(j-Lc)));  // formula for row major system
  LocC=B+(W*((i-Lr)+M*(j-Lc)));  // formula for column major system

  label: printf("Select 1 to follow RMS & select 2 to follow CMS\n");  // Label is placed here beacuse goto uses it furthur
  scanf("%d",&check);

switch(check)
  {
 case 1:printf("Accoring to RMS system address of arr[%ld][%ld] is %ld",i,j,LocR);
 break;
 case 2:printf("\n Accoring to CMS system address of arr[%ld][%ld] is %ld",i,j,LocC);
 break;
 default:printf("Please enter valid System Number indicated above\n");
 goto label;    // goto fuction is used here
  }
return 0;
}    // end of main function

RESULT – Output

C program to find the address of 2-D array element
C program to find the address of 2-D array element using row major order
C program to find the address of 2-D array element using column major
C program to find the address of 2-D array element using column major order

The above code can’t be directly copy-pasted. You must have to type with your own hand. This is made for your better future. 

Are you made for copy paste?

Absolutely “NO”

Then, try hard to understand this code and type with your own.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!