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

17th Apr 2021, 9:00 PM
Yami Francø
Yami Francø - avatar
11 odpowiedzi
+ 14
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! :)
17th Apr 2021, 9:11 PM
RKK
RKK - avatar
+ 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
17th Apr 2021, 9:42 PM
Ipang
+ 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; }
17th Apr 2021, 10:06 PM
Flash
+ 1
Arrays You are given an array of size 12. Write a program to print only the first, fifth and last letters of the array sequentially. #include <iostream> #include <string> using namespace std; int main() { char word[] = {'c', 'h', 'a', 'm', 'p', 'i', 'o', 'n', 's', 'h', 'i', 'p'}; //insert code here// cout << word[0] << word[4] << word[11] << endl; return 0; }
15th Nov 2021, 9:26 PM
Joyal Joseph
Joyal Joseph - avatar
+ 1
#include <iostream> #include <string> using namespace std; int main() { char word[] = {'c', 'h', 'a', 'm', 'p', 'i', 'o', 'n', 's', 'h', 'i', 'p'}; //insert code here// cout << word[0] << word[4] << word[11] << endl; return 0; } Good Luck
25th Jan 2022, 4:33 PM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
0
ohhhh damm... thank u very much both
17th Apr 2021, 9:56 PM
Yami Francø
Yami Francø - avatar
0
my mind just broke because of that code
17th Apr 2021, 10:09 PM
Yami Francø
Yami Francø - avatar
0
You hace to write a function to print what the array stores
19th Apr 2021, 9:23 AM
Aureo
Aureo - avatar
0
There are already some nice answer, may be you can learn a lot
19th Apr 2021, 3:16 PM
吴砖家
吴砖家 - avatar
0
Arr52 just name you must print arr52[num] For example for(int i = 0; i > 8; i++){ cout << arr52[i] << endl; }
19th Apr 2021, 4:21 PM
Алексей Шаламов
Алексей Шаламов - avatar
0
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 😊
19th Apr 2021, 5:58 PM
Debottam Das
Debottam Das - avatar