Initializing Arrays

I have a few quick questions related to arrays. I have two files below. arithmetic.cpp will eventually become a header containing functions for base two calculations.

Test of Arithmetic:


// The purpose of this program is only to perform a test case of the add function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "arithmetic.cpp"

using namespace std;

int main ()
{
     __hyper i;
     bool a[] = {1, 1, 0, 1};
     bool b[] = {1, 0, 1, 1, 0};
     bool c[] = add(a,b);
     for (i=0; i<sizeof(c); i++)
         cout << c[i];
     return 0;
}


arithmetic.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef arithmetic
#define arithmetic

//function to calculate the sume of two base two numbers

bool add(bool a[], bool b[])
{
         long long i; // index variable
         // establish size of array c based on the maximum potential size needed
         bool c[ (sizeof(a) >= sizeof(b)) ? (sizeof(a)+1) : (sizeof(b)+1) ] = {0};
         for (i=0; i<sizeof(c); i++)
         {
             if (a[i] || b[i])      // rules out instance of both being 0
                if (a[i] && b[i]) // specifies instance of both being 1
                   c[i+1]++;     // overflows to the next index
                else if (c[i])     // if c[i] is 1 and only one of a[i] or b[i] is 1
                   c[i+1]++;     // overflows to the next index
                else                // if c[i] is not 1 and only one of a[i] or b[i] is one
                   c[i]++;
         }
         return c;                // return pointer? What is the easiest way to do this.
}

#endif 


My first question is as follows:

Why am I allowed to do this:
1
2
3
bool c[sizeof(b)+1] = {0};
if (sizeof(a) >= sizeof(b))
   bool c[sizeof(a)+1] = {0};


But not this:
1
2
3
4
if (sizeof(a) >= sizeof(b))
  bool c[sizeof(a)+1] = {0};
else
  bool c[sizeof(b)+1] = {0};


Second, in my add function I tried to return an array. How would I do this properly?
Last edited on
The former is valid because you are initializing based upon a value known to the compiler.
The second is not valid because it is invalid C++.

Try this instead:

1
2
bool c[ (sizeof(a) >= sizeof(b)) ? (sizeof(a)+1) : (sizeof(b)+1) ] = {0};
// this doesn't do what you think, though... 

Both a and b are pointers, so their size is the same (probably 4 bytes long).

You need to explicitly pass the length of each array.

There is a problem, though: you cannot copy arrays automatically the way you are trying to. You'll need to use some dynamic memory allocation. And since you need to keep track of how long the arrays are, you'll probably want some form of structure to help. (If you aren't going to use something like a vector to help out.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
struct boolarray
{
     unsigned size;
     bool*    data;

     // Useful ctor and dtor
     boolarray( unsigned n ): size( n ), data( n ? (new bool[ n ]) : NULL ) { }
    ~boolarray() { if (data) delete [] data; }

     // Helper to construct from static data, see main() function
     boolarray( const bool* a, unsigned n )
     {
          size = n;
          data = new bool[ size ];
          for (unsigned x = 0; x < size; x++)
               data[ x ] = a[ x ];
     }

     // Alas, you need this copy ctor and assignment operator for
     // things to work properly with your add() function.
     boolarray( const boolarray& a )
     {
          data = NULL;
          copy( a );
     }
     boolarray& operator = ( const boolarray& a )
     {
          copy( a );
          return *this;
     }

     void copy( const boolarray& a )
     {
          if (data) delete [] data;
          size = a.size;
          data = new bool[ size ];
          for (unsigned n = 0; n < size; n++)
               data[ n ] = a.data[ n ];
     }

     // accessors
     const bool& operator [] ( unsigned n ) const { return data[ n ]; }
           bool& operator [] ( unsigned n )       { return data[ n ]; }
};

boolarray add( boolarray a, boolarray b )
{
     boolarray c( ((a.size > b.size) ? a.size : b.size) + 1 );

     for (unsigned n = 0; n < c.size; n++)
     {
          if (a[n] || b[n])
               ...
     }

     return c;
}

int main()
{
     bool _a[] = {1, 1, 0, 1};
     bool _b[] = {1, 0, 1, 1, 0};

     boolarray a( _a, sizeof(_a)/sizeof(bool) );
     boolarray b( _b, sizeof(_b)/sizeof(bool) );
     boolarray c = add( a, b );

     for (unsigned n = 0; n < c.size; n++)
          cout << c[n] << " ";

     return 0;
}

BTW, your addition algorithm is incorrect. You need to consider how to handle carries.

Good luck!
Thank you Duas. On your answer to the first question I'm not familiar with some of the syntax. What do '?' and ':' do?

BTW, your addition algorithm is incorrect. You need to consider how to handle carries.


Actually, I do have carries, or at least I did when I conceived the problem. I highly doubt there are errors in my math, but there may be errors in my code. I haven't been able to try my test case yet, as my program will not yet compile.
I see some of the problems in my code. I'll add comments and try to fix some mistakes. Meanwhile I have a question. I'm trying to return an array to the main function. Can I just return the pointer to the array and still use it to access array data from the main function?

Also when I use the sizeof function, am I getting the size of the array or of the pointer?
Last edited on
Yes, but you also want to return the new size of the array. That's why I used a struct (or class). You can also just pass it as a return argument:

1
2
3
4
bool* add( const bool* a, unsigned alen, const bool* b, unsigned blen, unsigned* clen )
{
     ...
}
1
2
3
4
unsigned clen;
bool* c = add( a, sizeof(a), b, sizeof(b), &clen );
...
delete [] c;

Hope this helps.
I finally got this to compile, but it gives an incorrect answer. I made several changes.

Test_of_arithmetic.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include "arithmetic.cpp"

using namespace std;

int main ()
{
     __hyper i;
     bool a[] = {1, 1, 0, 1};
     bool b[] = {1, 0, 1, 1, 0};
     unsigned long clen;
     bool* c = add(a, sizeof(a), b, sizeof(b), &clen);
     for (i=clen; i>=0; i--)
         cout << c[i] << '\n';
     delete [] c;
     system("pause");
     return 0;
}


arithmetic.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef arithmetic
#define arithmetic

bool* add( const bool* a, unsigned alen, const bool* b, unsigned blen, unsigned long* clen )
{
         long long i;
         bool c[ (sizeof(a) >= sizeof(b)) ? (sizeof(a)+1) : (sizeof(b)+1) ] = {0};
         *clen = sizeof(c);
         for (i=0; i<*clen; i++)
         {
             if (a[i] || b[i])
                if (a[i] && b[i])
                   c[i+1]++;
                else if (c[i])
                   c[i+1]++;
                else
                   c[i]++;
         }
         return c;
}

#endif 


For my test case I get this:

1
192
1
1
1
1
Press any key to continue . . .


What I don't understand how the 192 got there.

Edit: The purpose of the '\n' in my output loop for the array is to isolate the index of the 192.
Last edited on
That 192 got there by magic. You are lucky your program didn't just crash.

Line 7 is very wrong. It makes an array of length = (size of a pointer) + 1. Also, it uses a compiler extension. Finally, you are trying to allocate and return a local variable, which you cannot do. Try this instead:

7
8
9
         *clen = (alen >= blen) ? (alen + 1) : (blen + 1);
         bool* c = new bool[ clen ];
         ...

That'll fix most everything. You've got a fencepost error in main on line 13. The value of clen is one past the end of the array. (Remember that arrays are indexed starting at zero.)

Hope this helps. Now you can debug your addition algorithm.
That 192 got there by magic. You are lucky your program didn't just crash.


Thank you Duas, now I know how these errors get there. They aren't created because of my inexperience or lack of knowledge. They actually there because of my friend Harry P.

Line 7 is very wrong.


But I thought you gave me this little piece in your first reply? BTW, I'd still like to know the meaning of '?' and ':'.

I'll think about the things you've said and see if they work. Remember in your replys that I would like to know the why just as much as the what. I will learn a lot more that way, and be able to apply this information to other sitiuations. If you're willing to, you can really get down to the nitty-gritty technical details.
Topic archived. No new replies allowed.