B
bintom
I am trying to write a program with a function int** One2Two(int *a, int size, int (*A)[10]) that takes the address of a1D int array (A1) , the size of A1 and the address of a 2D int array (A2). The function should copy the values of A1 and store them in A2 as suggested by the pattern below:
If A1 is 1, 2, 3:
then A2 should be 1 0 0
1 2 0
1 2 3
If A1 is 1, 2, 3, 4, 5:
then A2 should be 1 0 0 0 0
1 2 0 0 0
1 2 3 0 0
1 2 3 4 0
1 2 3 4 5
My program is given below:
#include <iostream>
using namespaces std;
int** One2Two(int *a, int size, int (*A)[10])
{ int i, j;
for(i=0; i<size; i++)
{ for(j=0; j<=i; j++)
**(A+i*size+j) = *(a+i);
for(; j<size; j++)
**(A+i*size+j) = *(a+i);
}
}
int main()
{ int a[10], (*A)[10], size, i, j;
a[0]=1; a[1]=2; a[2]=3; a[3]=4; a[4]=5;
size = 5;
One2Two(a, size, A);
for(i=0; i<size; i++)
{ for(j=0; j<size; j++)
cout << **(A+i*size+j) << " ";
cout << "\n";
}
}
However, my program hangs in the inner loop of the function and I have to close the program run. Can anybody help me figure out what the problem is???
Thanks in advance.
T. V. Thomas
If A1 is 1, 2, 3:
then A2 should be 1 0 0
1 2 0
1 2 3
If A1 is 1, 2, 3, 4, 5:
then A2 should be 1 0 0 0 0
1 2 0 0 0
1 2 3 0 0
1 2 3 4 0
1 2 3 4 5
My program is given below:
#include <iostream>
using namespaces std;
int** One2Two(int *a, int size, int (*A)[10])
{ int i, j;
for(i=0; i<size; i++)
{ for(j=0; j<=i; j++)
**(A+i*size+j) = *(a+i);
for(; j<size; j++)
**(A+i*size+j) = *(a+i);
}
}
int main()
{ int a[10], (*A)[10], size, i, j;
a[0]=1; a[1]=2; a[2]=3; a[3]=4; a[4]=5;
size = 5;
One2Two(a, size, A);
for(i=0; i<size; i++)
{ for(j=0; j<size; j++)
cout << **(A+i*size+j) << " ";
cout << "\n";
}
}
However, my program hangs in the inner loop of the function and I have to close the program run. Can anybody help me figure out what the problem is???
Thanks in advance.
T. V. Thomas