help please with arrays in c++ | Sololearn: Learn to code for FREE!

+2

help please with arrays in c++

im having two arrays and im trying to pass them to a new one like array1 and array2 become one in array3 but first with the numbers of the first and then the numbers of the second,like: #include <iostream> using namespace std; int main() { int arr[4]={1,2,3,4}; int arr1[4]={5,6,7,8}; int arr52[8]; for (int i=0;i<4;i++){ arr52[i]+=arr[i]; } for (int i=0;i<4;i++){ arr52[i]+=arr1[i]; } cout<<arr52<<endl; return 0; } but the problem is that the numbers of the arrays doesnt pass and the third array print his variable memory adress. Why is that?

4/17/2021 9:00:23 PM

Miss Yamileth

9 Answers

New Answer

+10

The reason you you got the memory address was because you did print the whole array at once instead of its' elements. Second issue notice how the value of "i" changes in the first two for loops of yours. For the first time in first loop, everything is fine that is the first array is being copied to arr52 successfully but in the second for loop your i is again set to zero and this is the issue - which mean you are replacing the values which you assigned from arr1 with arr2. And so that's why I have added another variable called len in the code to avoid this issue. Third issue, arr52[i] += arr1[i] This is not how you assign value(check the code to see how to assign values) (+= is use for adding values, example - arr52[i] = arr52[i] + arr1[i]) The issue is in the first for loop when arr52 has no elements initially and this is when Garbage values come in since you were adding! I hope these help, happy learning! :)

+5

Local arrays are not initialized by default values, array <arr52> has unpredictable contents. The way you use += operator in the loops when assigning values for <arr52> elements will produce undesirable final result. You need to use simple = operator to copy array elements there. You also need to manipulate index for <arr52> when copying elements from <arr1>, you do this by adding number of elements that are already copied from array <arr> (4 items) to the loop counter variable <i>. // copy from <arr> for ( int i = 0; i < 4; i++ ) { arr52[i] = arr[i]; } // copy from <arr1> for ( int i = 0; i < 4; i++ ) { arr52[i + 4] = arr1[i]; } // display contents of <arr52> for( int value : arr52 ) { std::cout << value << std::endl; } Later on, if you want to, and are allowed for, you may use std::copy from header <algorithm>. https://en.cppreference.com/w/cpp/algorithm/copy

+3

nice solutions! btw if you don't want that many loops #include <iostream> #include <cstring> #include <iterator> int main() { int arr[4]={1,2,3,4}; int arr1[4]={5,6,7,8}; int arr52[8]{}; std::memcpy(arr52, arr, sizeof(int) * 4); std::memcpy(arr52 + 4, arr1, sizeof(int) * 4); auto b = std::begin(arr52); auto e = std::end(arr52); while (b != e) { std::cout << *b << '\n'; b++; } return 0; }

+1

ohhhh damm... thank u very much both

+1

You hace to write a function to print what the array stores

+1

There are already some nice answer, may be you can learn a lot

+1

Arr52 just name you must print arr52[num] For example for(int i = 0; i > 8; i++){ cout << arr52[i] << endl; }

+1

There are a few errors in your program... 1st : in your 1st loop, you cannot write " arr52[i] += arr[i]; ". This is because the statement "arr52[i] += arr[i];" means arr52[i] = arr52[i] + arr[i]. But you see, you haven't initialised the elements of arr52[] , so it contains garbage values and further operations with it results in errors. So you can just write "arr52[i] = arr[i] ". This goes same for the 2nd loop. The only difference is that, you may write " arr52[i+ 4] = arr1[i]" 2 nd : in your 2nd loop, if you write " arr52[i] += arr[i] " , the first 4 elements of arr52 [] gets replaced by other elements. This results in complete error. Instead you shall write " arr52[i+ 4] = arr1[i]" . By writing this, in the first iteration, the fifth position of the arr52[] gets occupied by the 1st element of arr1[] and so on. This goes on till the 2nd loop executes. I hope that my suggestion will help you in understanding why the error occurred. Thank you 😊

0

my mind just broke because of that code