Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with assigning 1D array to a 2D array

Reply
Thread Tools

Problem with assigning 1D array to a 2D array

 
 
bintom
Guest
Posts: n/a
 
      10-12-2012
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-12-2012
On 10/12/2012 6:22 AM, bintom wrote:
> 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' is an array of 10 pointers to int. 'a' is an array of 10 ints.

> a[0]=1; a[1]=2; a[2]=3; a[3]=4; a[4]=5;


Here you assign ints to ints. That's perfectly fine.

> size = 5;
>
> One2Two(a, size, A);


Here you pass an array of 10 pointers to int (10 pointers) to the
function. That function will dereference those pointers. Where do
those pointers point to?

>
> 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???


You dereference uninitialized pointers. Your program has undefined
behavior. Anything is allowed to happen. Read up on "nasal demons".

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
bintom
Guest
Posts: n/a
 
      10-12-2012
I initialized A to 0, yet the problem persists. Can somebody else help with a solution? Thanks.
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      10-12-2012
On 2012-10-12, Victor Bazarov <> wrote:
> On 10/12/2012 6:22 AM, bintom wrote:
>> int main()
>> { int a[10], (*A)[10], size, i, j;

>
> 'A' is an array of 10 pointers to int. 'a' is an array of 10 ints.


'A' is a pointer to an array of 10 ints.

An array of 10 pointers to int is declared as 'int *A[10];',
or, with explicit parentheses, 'int *(A[10]);'.
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      10-12-2012
On 2012-10-12, bintom <> wrote:
> I am trying to write a program with a function int** One2Two(int *a,
> int size, int (*A)[10]) that takes the address of a 1D int array (A1),
> the size of A1 and the address of a 2D int array (A2).


You are confused.

The parameter 'a' (or 'A1') is a pointer-to-int, or, likewise, the
address of an int; it's not the address of a 1D int array (but it
could be the address of the first element of such an array).

'A' (or 'A2') is a pointer to a 1D array (more precisely, a pointer
to an array of 10 ints), or, likewise, the address of such an array;
it's not the address of a 2D array.

Why is the return type of One2Two pointer-to-pointer-to-int when
the function does not return anything?

Have a look chapter 6 of the C language FAQ,

http://c-faq.com/aryptr/index.html
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-12-2012
On 10/12/2012 1:39 PM, Ike Naar wrote:
> On 2012-10-12, Victor Bazarov <> wrote:
>> On 10/12/2012 6:22 AM, bintom wrote:
>>> int main()
>>> { int a[10], (*A)[10], size, i, j;

>>
>> 'A' is an array of 10 pointers to int. 'a' is an array of 10 ints.

>
> 'A' is a pointer to an array of 10 ints.


You're right, of course. I totally missed the parentheses; shows how
much I've been lately using naked pointers and arrays of them.

> An array of 10 pointers to int is declared as 'int *A[10];',
> or, with explicit parentheses, 'int *(A[10]);'.
>


V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
bintom
Guest
Posts: n/a
 
      10-12-2012
I too am coming to the conclusion that I AM confused. But given the problem I amim to solve, it would be great if u could hold my hand and show me the correct way of coding in this case.

And if u could explain why you chose that style of coding, that would be a bonus.
 
Reply With Quote
 
Luca Risolia
Guest
Posts: n/a
 
      10-13-2012
On 12/10/2012 12:22, bintom wrote:
> 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


Use valarrays for numerical calculations:

#include <iostream>
#include <valarray>

using namespace std;

template<class T>
valarray<T> One2Two(const valarray<T>& v) {
const auto n = v.size();
valarray<T> m(n * n);
for (decltype(v.size()) i = 0; i < n;
m[slice(n * i + i, n - i, n)] = v[i], ++i);
return m;
}

int main() {
valarray<int> a = {1, 2, 3, 4, 5};
auto m = One2Two(a);
for (const auto& e : m)
cout << e << (((&e - &m[0]) + 1) % a.size() ? ' ' : '\n');
return 0;
}

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

 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      10-13-2012
A. Bolmarcich <> wrote:
> Although to be more in the spirit of C++, you should use C++
> vectors rather than arrays.


There's nothing in the "spirit of C++" that would require using vectors
instead of static arrays. If anything, std::array would be the one to be
used instead of static C arrays.
 
Reply With Quote
 
Luca Risolia
Guest
Posts: n/a
 
      10-13-2012
On 13/10/2012 22:36, Juha Nieminen wrote:
> A. Bolmarcich <> wrote:
>> Although to be more in the spirit of C++, you should use C++
>> vectors rather than arrays.

>
> There's nothing in the "spirit of C++" that would require using vectors
> instead of static arrays. If anything, std::array would be the one to be
> used instead of static C arrays.


std::valarray would be even more in the "spirit of C++" with regard to
the OP problem.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assigning an array to another array using C's assignment operator Myth__Buster C Programming 13 02-26-2013 04:29 PM
Assigning an array to another array using C's assignment operator Myth__Buster C Programming 1 02-01-2013 12:27 AM
Assigning an array to another array using C's assignment operator Myth__Buster C Programming 0 02-01-2013 12:05 AM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
Problem assigning an Array object to an Array-subclass object Richard Lionheart Ruby 27 05-04-2004 06:42 AM



Advertisments