in Programming in C
662 views
0 votes
0 votes

A matrix $X$ of order $(N \times N)$ is stored in an array indexed from $0$ to $2N-2$, by first storing the diagonal, followed by anti diagonal.

Fill up the blank in the following code for retrieving the element.

  if(i==j)      return A[i-1];    else if(i+j == N+1)      return __________

 

a) $A[N+i]$

b) $A[i-1]$

c) $A[N+i-1]$

d) $A[N-i+1]$

in Programming in C
by
662 views

2 Answers

0 votes
0 votes

it guess it would be  option c)  A[N+i-1]   . i did it by taking an array by trying out.my assumption was anti diagonal element was from upper right corner to lower  left corner as by assuming the other way options it was not coming as per options

by

3 Comments

if we take 3X3 array then A[2][2] element will be present at index 1 of array but

A[N+i-1]=A[3+2-1]=4 which is wrong
0
0

notice when i==j a[i-1] is returned not  A[N+i-1]

0
0
okay got it
0
0
0 votes
0 votes
Question is nothing but store all diagonal(principal as well as anti diagonal) element in an array. So total n diagonal + n-1 anti diagonal element. we have to store total 2n-1element. Array size should be 2n-1 but starting from 0 It will go upto 2n-1. Final array is something like A[0.1.2........2n-2].

now question,

If (i==j) return (A[i-1]) ......   //Principal diagonal

Else if (i+j == n+1) return A[n+i-1]      //Anti-digonal

1 comment

i think for 2X2 it will not work because diagonal and antidiagonal both require 2,2 elements so total size will be 2N but array size given is 2N-1
0
0

Related questions