Is it possible to reassign an array to another array? Like this: This is the function e02:
void e02(){
int a[] = {15, 9, 8, 4, 3};
int n = 5;
int x = 5;
fn02(a, n, x);
cout << endl;
for(int i = 0; i < n; i++){
cout << a[i] << " ";
}
}
And this is the function fn02:
void fn02(int* a, int &n, int x){
n += 1;
int* b = new int[n];
int j = 0;
bool put = false;
for(int i = 0; i < n; i++){
if(x > a[i] && put == false){
b[j] = x;
j++;
a--;
put = true;
} else{
b[j] = a[i];
j++;
} //else
} //i loop
a = b;
}
This is supposed to put the variable n into the array but the array still needs to be descending. If I just assign a = b like this then I get the output 15, 9, 8, 4, 3, 5 with 5 being a garbage value. So my question is: Is there a way to reassign an array to a different array like here? If I use a pointer like
int* &p1;
and then put it in the function I get what I want but the function has to have those parameters and has to be void
I will provide some concepts that will potentially help you derive a solution to your problem. Then provide a quick solution that can help you with your final solution.
...If I just assign a = b like this then I get the output 15, 9, 8, 4, 3, 5 with 5 being a garbage value.
The output of the number 5 has nothing to do with your assignment of a=b in fcn02. The value 5 is actually the value of x in the calling function. You are accessing the array outside the bounds of it's original allocation thus accessing the value of next address of the size of int. In this case it's the value of x. if you spit out the address of x and the address of a[6] you will see that they are equal.
Your assignment in fcn02 of a=b does not work as you intended due to the fundamental concept of passing values to a function. When you call the function fcn02(a) the value "a" (the address of the beginning of the array) is copied to the value of "a" in fcn02. Changing "a" in fcn02 does not change "a" in the calling function.
Example for clarification NOTE( Using the same value "a" can be confusing so I changed it a bit).:
int func02( int* b ) // address of a is copied to b
{
... // some code...c is dynamically allocated and has an address of 0x80004d30
b = c; // b is set to address of c; thus, b address is now 0x80004d30
// a is unchanged and you now have a memory leak since you didn't delete b.
}
int main()
{
int a[5] = {1,2,3,4}; // address is 0x28cc58
func02(a); // complier will make a copy of the value of a to b
// address (i.e. value) of a is still 0x28cc58
}
Memory layout of why you see 5:
int a[5] = {1,2,3,4,5}; // 0x28cc64
int x = 7; // 0x28cc5c
{ array a }{ x }
---- ---- ---- ---- ---- ----
| 1 | 2 | 3 | 4 | 5 | 7 |
---- ---- ---- ---- ---- ----
However to answer you question you can NOT assign one array to another.
int a[5] = {1,2,3,4,5};
int b[5];
b = a;
for ( int i = 0; i<5; ++i )
{
cout << b[i] << endl;
}
The compiler will not allow this.
Here is quick and dirty solution keeping your function parameters the same for guidance:
void e02(){
int a[6] = {15, 9, 8, 4, 3, 0};
int sizeofA = 5;
int numToAdd = 5;
fn02(a, sizeofA, numToAdd);
cout << endl;
for(int i = 0; i < n; i++) {
cout << a[i] << " ";
}
}
void fn02(int* a, int &n, int x) {
n += 1;
int i = 0;
while( i < n ) {
if ( a[i] > x ) {
++i;
}
else {
int tmp = a[i];
a[i] = x;
x = tmp;
}
}
}