2D Array elements to be stored in 1D Array

how can i store a 2d array elementS in a single dimension array. I am stuck in the loops

codechef sucks !!!

Well, firstly, codechef is probably one of the best programming initiatives on the web!! So please watch about what you write, as that might persuade people NOT to help you!! It’s an advice to the future…

Regarding your question, what you want is a pseudo multi dimensional array and there’s plenty of sites on the web on implement it, as cplusplus:

Code for a normal multidimensional array

#define WIDTH 5
#define HEIGHT 3

int jimmy [HEIGHT][WIDTH];
int n,m;

int main ()
{
  for (n=0;n<HEIGHT;n++)
    for (m=0;m<WIDTH;m++)
    {
      jimmy[n][m]=(n+1)*(m+1);
    }
  return 0;
}

Equivalent code using a 1d array

#define WIDTH 5
 #define HEIGHT 3

int jimmy [HEIGHT * WIDTH];
int n,m;

int main ()
{
  for (n=0;n<HEIGHT;n++)
    for (m=0;m<WIDTH;m++)
    {
      jimmy[n*WIDTH+m]=(n+1)*(m+1);
    }
  return 0;
}

Hope this helps!

//