Thread: Arrays, and Functions dealing with them

1. Arrays, and Functions dealing with them

I am using Dev-C++ 4.9.9.2 and have just started to teach myself c++ (using the tutorials on this site and C++ for dummies

First, I thought you could initialize all the elements in an array to a commont value (like int block[4][4]={1}) but in the following code, it only sets block[0][0] to 1, and leaves the rest untouched. I get the output:
::::::
.# .
. .
. # .
. .
::::::
When the whole block should be '#' (if all the elements in block were set to 1)

Code:
```#include <iostream>
using namespace std;
void display(int matrix[4][4]);
main()
{
int block[4][4]={1};
block[2][2]=1;   //to make sure it isn't the display function at fault
display(block);
cin.get();
}
//Display function
void display(int matrix[4][4])
{
for(int c=0,x=4;c<=x+1;c++)
{cout << ":";}
cout << "\n";
for ( int y = 0; y < 4;y++ )
{
cout <<".";
for ( int x = 0; x < 4; x++ )
{
if (matrix[x][y]==1 || matrix[x][y]==2){cout <<'#';}
else {cout <<' ';}
}
cout << ".";
cout<<"\n";
}
for(int c=0,x=4;c<=x+1;c++){cout << ":";}
}```
Now my main question is how to make functions that deal with arrays. For example I want to take a 4 by 4 array and "turn" it 90 degrees. I think I kow how to do it:
Code:
```int rotate_array(int array[4][4])
{   int new_array[4][4];
for (int y;y<4;y++)
{
for(int x;x<4;x++)
{
new_array[abs(y-3)][x]=array[x][y];
}
}
return new_array;
}```
or something like that (i dont know if that will work; havn't tested it). I just dont no how to put an array in and get one out. Can I do something like I just did and then put:

anyarray=rotate_array(anyarray)

to change anyarray to its itself rotated,
or can I make a function that will change whatever array is put into
it so I just need:
rotate_array(anyarray)
to change whatever array is put into it, or what?

I have looked through alot of posts already (and learned some stuff from them to) but i havn't figured any of this out. I also noticed that alot of questions deal with arrays, perhaps someone could make a more detailed tutorial on them?
thanks

2. Code:
`for(int x;x<4;x++)`
->
Code:
`for(int x = 0;x<4;x++)`
Any extra elements are initalized to zero.
Code:
`int array[4] = {1};`
->
Code:
`int array[4] = {1, 0, 0, 0};`

3. Code:
`for(int x = 0;x<4;x++)`
I had the x set to zero (forgot it when posting) thats not my problem.
So
Code:
`int array[4] = {1};`
only sets the first element to 1?
can I initialize the whole array to something?

Really what I want to know though, Is how to return arrays from functions.
Code:
```int somefunction(int array[4][4])
{
int array2[4][4]
...(code dealing with arrays)
return array2
}```
or
Code:
`return array2[4][4]`
?

and can you simply set an array equal to another?
Code:
```int array1[4]={0,1,2,3}
int array2[4]
array2=array1```
?

4. First, I thought you could initialize all the elements in an array to a commont value(like int block[4][4]={1})
No.

but in the following code, it only sets block[0][0] to 1, and leaves the rest untouched.
Not true. The elements of an array may contain random junk values after the array is declared. The rule is: if you use an initializer list, and you don't specify values for every element in the array, then the remaining elements of the array will be initialized to 0. You can use that fact to initialize an entire array to 0 like this:
Code:
`int block[4][4] = {0};`
That initializes the first element with 0, and since no values are specified for the other elements, they will automatically be initialized to 0. If you do this:
Code:
`int block[4][4] = {1};`
the first element is initialized to 1, and the rest are automatically initialized to 0.

Can I do something like I just did and then put:
Code:
```int rotate_array(int array[4][4])
{
...
}
anyarray=rotate_array(anyarray)```
Code:

'anyarray' has to be a type that matches the type of the parameter variable 'array' in the function definition. The type you specified for the parameter variable is int[][4]. The leftmost dimension is not part of the type. So you are restricted to sending arguments that are int arrays with 4 columns.

You have a problem with this line:
Code:
`int new_array[4][4];`
All variables declared inside a function are destroyed when the function ends. Therefore, trying to return that array won't work. One solution to that problem is to pass new_array to the function as a second parameter. Then you don't even have to return anything. Another solution is to create new_array inside the function using the new operator.

5. In C++, vectors are generally preferred over C-style arrays. You could learn vectors, which allow you to initalize all entries to a specific non-zero value in a single line:
Code:
`vector<vector<int> > block(4, vector<int>(4, 1));`

Ok I pretty much solved my prooblem ( or at least can work aroud it)
Still wandering though, can you return an array? Seems to me it was only returning the address or something, so if you declared the array in the function, you get back garbage.
Can you return the array itself? Without having to declare anything outside the function?

And, bytheway
...For example, the following initializes all 25 locations in floatArray to 1.0
Code:
`float floatArray[25] = {1.0};`
-C++ for dummies 5th edition (bold added)
This is incorrect?
Should I get a new book?

7. As you said, if you return an array you have to return its address, which will be garbage if the array is declared locally. You could return a pointer to a dynamically allocated array, but that isn't very good practice, since the calling code must then delete it to avoid memory leaks. Generally, you have to declare the array in the calling code and pass it as an argument to the function. Arrays are automatically passed by reference, meaning that any changes you make to the array in the function will be appear in the version in the calling code.

>> This is incorrect?
I thought so as well, maybe somebody can quote the standard.

>> Should I get a new book?
Not necessarily because of an error (many books have minor errors). That book is not exactly highly recommended though, so you could consider getting a new book like Accelerated C++, which is highly recommended.