I'm still relatively new to c++. I am trying to write a program that takes an array of numbers and reverses the order of those numbers in the array with a function. The program is as follows:
#include <iostream>
using namespace std;
void reverse(int *array, int size);
int main() {
int Array[] = { 1, 2, 3, 4, 5 };
int size = sizeof(Array) / 4;
reverse(Array, size);
return 0;
}
void reverse(int *array, int size) {
int Array2[5];
for (int i = 0; i < size; i++) {
Array2[i + size] = array[i];
array[i + size] = Array2[i + size];
};
}
When I run this program, it crashes and I'm not sure why. If anyone can help my figure out why it would be much appreciated. Thank you.
Zenith has it, but there are a few points and quick hacks worth noting to help you out.
#include <iostream>
//using namespace std; don't need this, and using namespace std is overkill and often
// causes problems. It pulls in a lot of stuff that may conflict, case in point
// std::reverse now becomes reverse. Which reverse will you get? Your reverse or the standard
// library's reverse? Only pull in what you need, for example
using std::cout; // still not used, but makes a good example.
void reverse(int *array, int size)
{
// no need for the other array and another loop. You can swap each element for
//it's counterpart in the upper half of the array.
for (int i = 0; i < size /2 ; i++) // only need to go half way. Other half was
// already swapped doing the first half.
{
int temp = array[i]; // store a temporary copy of element i
array[i] = array[size-1-i]; // replace element i with it's counterpart
// from the second half of the array
array[size-1-i] = temp; // replace the counterpart of i with the copy of i
// or call std::swap(array[i], array[size-1-i]);
};
}
int main() {
int Array[] = { 1, 2, 3, 4, 5 };
// int size = sizeof(Array) / 4; using 4 here can trip you up on a computer with
// a different sized int
int size = sizeof(Array) / sizeof(Array[0]);
// dividing the size of the array by the size of an element in the array will always
// get you the correct size
reverse(Array, size);
return 0;
}
Array2[i + size]
You're accessing out-of-bounds, no matter the value of i.
You probably meant Array2[size - 1 - i] to iterate the array backwards. (size - 1 is the index of the last element.)
by using swap you will get a much nicer solution which is also more efficient
void reverse(int *array, int size) {
for (int i = 0; i < size/2; i++) {
std::swap(array[i],array[size-1-i]);
};
}
When you say int size = sizeof(Array) / 4;, size is now (5 * sizeof(int)) / 4. That's how the sizeof operator works (at least when applied to arrays).
So size is probably 5, assuming a 4-byte int. Now, you get to reverse and its argument size is also 5.
You get to the for loop, and even at the first iteration, you have Array2[5] = /* something */ and array[5] = /* something */, both of which are buffer overruns.
Also, your reverse function doesn't actually do any reversing. Try this:
void reverse(int *arr, int size);
int main()
{
int Array[] = { 1, 2, 3, 4, 5 };
int size = sizeof(Array) / sizeof(int);
reverse(Array, size);
return 0;
}
void reverse(int *arr, int size)
{
int temp[5];
for (int i = 0; i < size; i++)
temp[size - 1 - i] = arr[i];
for (int i = 0; i < size; i++)
arr[i] = temp[i];
}
Related
I'm a C++ beginner and I'm stuck on my course assignment because I can't wrap my head around how to modify values of an array passed to a function.
Honestly, we haven't reached the topic about arrays yet in class, but I recall some basic functionalities related to them and I wanted to solve the exercise using arrays because it's surely a smarter way to do it then the easy version of the assignment without them (that I've already solved). I've looked online for some references but clearly I'm missing something important.
The assignment states:
In this exercise you’ll investigate a simple scheme for encrypting and decrypting data. A company that wants to send data over the Internet has asked you to write a program that will encrypt the data so that it may be transmitted more securely. All the data is transmitted as four-digit integers. Your application should read a four-digit integer entered by the user and encrypt it as follows:
Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10.
Then swap the first digit with the third, and swap the second digit with the fourth.
Then print the encrypted integer.
And my attempt is this:
main.cpp
#include "encrypt.h"
#include <iostream>
const size_t size{4};
int main () {
int arrEncrypt[size] = {1, 2, 3, 4};
swap(arrEncrypt[size]);
encrypt(arrEncrypt[size]);
printArr(arrEncrypt[size]);
}
encrypt.h
#ifndef ENCRYPT_H
#define ENCRYPT_H
void encrypt(int a[]);
void swap(int a[]);
void printArr(int a[]);
#endif
encrypt.cpp
#include <iostream>
#include "encrypt.h"
const size_t num{4};
// Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10.
void encrypt(int arr[::num]) {
for (int k = 0; k < ::num; k++) {
arr[k] = (arr[k] + 7) % 10;
}
}
// Then swap the first digit with the third, and swap the second digit with the fourth.
void swap(int arr[::num]) {
int box[1] = {0};
for (int k = 0; k < (::num / 2); k++) {
box[1] = arr[k];
arr[k] = arr[k+2];
arr[k+2] = box[1];
}
box[1] = arr[0];
arr[0] = arr[2];
arr[2] = box[1];
box[1] = arr[1];
arr[1] = arr[3];
arr[3] = box[1];
}
// Then print the encrypted integer.
void printArr(int arr[::num]) {
for (int k = 0; k < ::num; k++) {
std::cout << arr[k] << " ";
}
}
The swap function has some problems in the algorithm probably because I tried printing the for loop output and I get just one random value, while the bit manually written is working fine. I don't know why.
This program clearly doesn't work because every time I pass the array to a function it is always the one declared in main and not an array modified by the functions. I tried pointing to the array passed to the functions but I think I messed it up because I got a bunch of errors - moreover, I've read that taking the address of a built-in array to pass it to a function is not needed and I can simply pass the built-in array’s name because the called function can modify all the elements of an array in the caller, unless the function precedes the corresponding built-in array parameter with const to indicate that the elements should not be modified. But I don't get why in my program it doesn't work.
Thanks in advance for anyone who would take their time to help me out, I really appreciate it.
An array can't be passed to a function by value, only by reference or pointer. In your case, a parameter like int a[] is really just syntax sugar for a pointer int *a. But you are not actually calling the functions with a pointer, you are indexing into arrEncrypt and passing an individual int instead (using an index that is out of range!).
Get rid of the array indexing at the call sites. An array decays into a pointer to its 1st element when it is referred to by just its name.
Also, such parameters don't carry size information, so you have to pass the array size explicitly in a separate parameter.
Try this instead:
#include "encrypt.h"
const size_t size{4};
int main () {
int arrEncrypt[size] = {1, 2, 3, 4};
swap(arrEncrypt, size);
encrypt(arrEncrypt, size);
printArr(arrEncrypt, size);
}
encrypt.h
#ifndef ENCRYPT_H
#define ENCRYPT_H
void encrypt(int a[], int size);
void swap(int a[], int size);
void printArr(int a[], int size);
#endif
encrypt.cpp
#include <iostream>
#include <stdexcept>
#include "encrypt.h"
// Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10.
void encrypt(int arr[], int size) {
if (!arr || size < 0) throw invalid_argument("");
for (int k = 0; k < size; k++) {
arr[k] = (arr[k] + 7) % 10;
}
}
// Then swap the first digit with the third, and swap the second digit with the fourth.
void swap(int arr[], int size) {
if (!arr || size < 4) throw invalid_argument("");
int box;
for (int k = 0; k < (size / 2); k++) {
box = arr[k];
arr[k] = arr[k+2];
arr[k+2] = box;
}
box = arr[0];
arr[0] = arr[2];
arr[2] = box;
box = arr[1];
arr[1] = arr[3];
arr[3] = box;
}
// Then print the encrypted integer.
void printArr(int arr[], int size) {
if (!arr) throw invalid_argument("");
for (int k = 0; k < size; k++) {
std::cout << arr[k] << " ";
}
}
That being said, consider using std::vector instead, eg:
#include "encrypt.h"
int main () {
std::vector<int> arrEncrypt = {1, 2, 3, 4};
swap(arrEncrypt);
encrypt(arrEncrypt);
printArr(arrEncrypt);
}
encrypt.h
#ifndef ENCRYPT_H
#define ENCRYPT_H
#include <vector>
void encrypt(std::vector<int> &a);
void swap(std::vector<int> &a);
void printArr(const std::vector<int> &a);
#endif
encrypt.cpp
#include <iostream>
#include <utility>
#include <stdexcept>
#include "encrypt.h"
// Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10.
void encrypt(std::vector<int> &arr) {
for (size_t k = 0; k < arr.size(); k++) {
arr[k] = (arr[k] + 7) % 10;
}
}
// Then swap the first digit with the third, and swap the second digit with the fourth.
void swap(std::vector<int> &arr) {
if (arr.size() < 4) throw invalid_argument("");
for (size_t k = 0; k < (a.size() / 2); k++) {
std::swap(arr[k], arr[k+2]);
}
std::swap(arr[0], arr[2]);
std::swap(arr[1], arr[3]);
}
// Then print the encrypted integer.
void printArr(const std::vector<int> &arr) {
for (size_t k = 0; k < arr.size(); k++) {
std::cout << arr[k] << " ";
}
}
Or std::array, eg:
#include "encrypt.h"
int main () {
std::array<int, size> arrEncrypt = {1, 2, 3, 4};
swap(arrEncrypt);
encrypt(arrEncrypt);
printArr(arrEncrypt);
}
encrypt.h
#ifndef ENCRYPT_H
#define ENCRYPT_H
#include <array>
#include <iostream>
#include <utility>
// Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10.
template<typename T, size_t N>
void encrypt(std::array<T, N> &arr) {
for (size_t k = 0; k < N; k++) {
arr[k] = (arr[k] + 7) % 10;
}
}
// Then swap the first digit with the third, and swap the second digit with the fourth.
template<typename T, size_t N>
void swap(std::array<T, N> &arr) {
if (N < 4) throw invalid_argument("");
for (size_t k = 0; k < (N / 2); k++) {
std::swap(arr[k], arr[k+2]);
}
std::swap(arr[0], arr[2]);
std::swap(arr[1], arr[3]);
}
// Then print the encrypted integer.
template<typename T, size_t N>
void printArr(const std::array<T, N> &arr) {
for (size_t k = 0; k < N; k++) {
std::cout << arr[k] << " ";
}
}
#endif
The arguments arrEncrypt[size] in the function main() is bad because
It is out-of-range because the array arrEncrypt has only size elements and its valid indice are 0 to size-1.
The functions are expecting pointers, but an integer is passed.
The element box[1] used in the function swap() is out-of-range because the array box has only one element. Since it looks you are using only the element box[1] from the array box, you should stop using array here and instead of that you should use simple variable box.
In the function main(), you should pass the array (or a pointer to the first element of the array converted from the array) to the function:
int main () {
int arrEncrypt[size] = {1, 2, 3, 4};
swap(arrEncrypt);
encrypt(arrEncrypt);
printArr(arrEncrypt);
}
In the function swap(), you should stop using out-of-range "element":
void swap(int arr[::num]) {
int box = 0;
for (int k = 0; k < (::num / 2); k++) {
box = arr[k];
arr[k] = arr[k+2];
arr[k+2] = box;
}
box = arr[0];
arr[0] = arr[2];
arr[2] = box;
box = arr[1];
arr[1] = arr[3];
arr[3] = box;
}
Is there a way to take two int arrays in C++
int * arr1;
int * arr2;
//pretend that in the lines below, we fill these two arrays with different
//int values
and then combine them into one larger array that contains both arrays' values?
Use std::copy defined in the header <algorithm>. The args are a pointer to the first element of the input, a pointer to one past the last element of the input, and a pointer to the first element of the output.
( https://en.cppreference.com/w/cpp/algorithm/copy )
int * result = new int[size1 + size2];
std::copy(arr1, arr1 + size1, result);
std::copy(arr2, arr2 + size2, result + size1);
Just suggestion, vector will do better as a dynamic array rather than pointer
If you're using arrays, you need to allocate a new array large enough to store all of the values, then copy the values into the arrays. This would require knowing the array sizes, etc.
If you use std::vector instead of arrays (which has other benefits), this becomes simpler:
std::vector<int> results;
results.reserve(arr1.size() + arr2.size());
results.insert(results.end(), arr1.begin(), arr1.end());
results.insert(results.end(), arr2.begin(), arr2.end());
Another alternative is to use expression templates and pretend the two are concatenated (lazy evaluation). Some links (you can do additional googling):
http://www10.informatik.uni-erlangen.de/~pflaum/pflaum/ProSeminar/
http://www.altdevblogaday.com/2012/01/23/abusing-c-with-expression-templates/
http://aszt.inf.elte.hu/~gsd/halado_cpp/ch06s06.html
If you are looking for ease of use, try:
#include <iostream>
#include <string>
int main()
{
int arr1[] = {1, 2, 3};
int arr2[] = {3, 4, 6};
std::basic_string<int> s1(arr1, 3);
std::basic_string<int> s2(arr2, 3);
std::basic_string<int> concat(s1 + s2);
for (std::basic_string<int>::const_iterator i(concat.begin());
i != concat.end();
++i)
{
std::cout << *i << " ";
}
std::cout << std::endl;
return 0;
}
Here is the solution for the same-
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void Concatenate(char *s1,char *s2)
{
char s[200];
int len1=strlen(s1);
int len2=strlen(s2);
int j;
///Define k to store the values on Kth address Kstart from 0 to len1+len2;
int k=0;
for(j=0;j<len1;j++)
{
s[k]=s1[j];
k++;
}
for(j=0;j<len2;j++)
{
s[k]=s2[j];
k++;
}
///To place a null at the last of the concatenated string to prevent Printing Garbage value;
s[k]='\n';
cout<<s;
}
int main()
{
char s1[100];
char s2[100];
cin.getline(s1,100);
cin.getline(s2,100);
Concatenate(s1,s2);
return 0;
}
Hope it helps.
for (int i = 0; i< arraySize * 2; i++)
{
if (i < aSize)
{
*(array3 + i) = *(array1 + i);
}
else if (i >= arraySize)
{
*(array3 + i) = *(array2 + (i - arraySize));
}
}
This might help you along, it doesn't require vectors. I had a similar problem in my programming class. I hope this helps, it was required that I used pointer arithmetic. This assumes that array1 and array2 are initialized dynamically to the size "aSize"
Given int * arr1 and int * arr2, this program Concatenates in int * arr3 the elements of the both arrays. Unfortunately, in C++ you need to know the sizes of each arrays you want to copy. But this is no impediment to choose how many elements you want to copy from arr1 and how many from arr2.
#include <iostream>
using namespace std;
int main(){
int temp[] = {1,2,3,4};
int temp2[] = {33,55,22};
int * arr1, * arr2, *arr3;
int size1(4), size2(3); //size1 and size2 is how many elements you
//want to copy from the first and second array. In our case all.
//arr1 = new int[size1]; // optional
//arr2 = new int[size2];
arr1=temp;
arr2=temp2;
arr3 = new int;
//or if you know the size: arr3 = new int[size1+size2];
for(int i=0; i<size1+size2; i++){
if (i<size1)
arr3[i]=arr1[i];
else
arr3[i] = arr2[i-size1];
}
cout<<endl;
for (int i=0; i<size1+size2; i++) {
cout<<arr3[i]<<", ";
}
}
I am trying to get the nth largest number of an array, I tried to sort the array then access the nth number by indexing; I have written this code:
#include <iostream>
using namespace std;
int largest(int a[],int k){
for (int i=0;i<=(sizeof(a)/sizeof(*a));i++){
for (int j=i+1;j<=(sizeof(a)/sizeof(*a));j++){
if(a[j]>a[i]){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
return a[k-1];
}
int main()
{
int a[]={3,2,1,0,5,10};
int m=largest(a,4);
cout<<m<<endl;
return 0;
}
and when I print out m it appears to be 5 while it is expected to be 2, when I tried to replace int m=largest(a,4); with m=largest(a,1); it printed 2 so it appears that he prints the index of the array a but without sorting it, any idea?
The problem lies in the use of sizeof(a)/sizeof(*a) to get the number of elements of the array. sizeof(a) is the size of a pointer.
You need to pass the size of the array to the function.
int largest(int a[], int size, int k){
for (int i=0;i<size;i++){
for (int j=i+1;j<size;j++){
...
}
}
}
and call it with
int m = largest(a, 6, 4);
There are three problems with your code.
First, when you pass the array as an argument to your function, it decays into a pointer. So the function can never know the size of the array without additional information. It is main()'s job to find the size of the array and pass that information along to largest().
Second, you have an off-by-one error in your code, because you are attempting to iterate from 0 to the number of elements in the array.
The following will work:
#include <iostream>
using namespace std;
int largest(int a[],int k, int n){
for (int i = 0; i < n; i++){
for (int j = i + 1; j < n; j++){
if (a[j] > a[i]){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[k-1];
}
int main()
{
int a[] = {3, 2, 1, 0, 5, 10};
int k = 4;
int m = largest(a, k, sizeof a/ sizeof *a);
cout << m << endl;
}
At last but not least, you have nasty side effects in your function. If you have a function that is supposed to find the largest element of the array, it shouldn't modify the entire array in order to do so.
You can make a copy of the original array and sort it. Or you can implement a k-element sort algorithm. Either way you shouldn't change user data just to find some statistic from it.
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[]={3,2,1,0,5,10};
std::sort(a, a+6, greater<int>() ); // Sort an array of 6 elements in greatest-first order.
cout<<a[3]<<endl; // Show the 4th element from the front.
return 0;
}
UPD: There are STL algorithm to use: std::nth_element.
#include <iostream>
#include <algorithm>
int main(){
int arr[] = {54, 897, 87, 4, 6,987};
size_t length = 6, k = 3;
std::cout<<std::nth_element(arr, arr + k, arr + length, std::greater<int>());
}
Also, if you want to implement it by yourselt you can do such thing based on quick sort:
#include <iostream>
#include <algorithm>
template<class T>
T largest_k(T* a, size_t left, size_t right, size_t k) {
if(left>=right)
return a[k-1];
size_t i = left, j = right;
T middle = a[ i + (j-i) / 2 ];
do {
while ( a[i] > middle ) i++;
while ( a[j] < middle ) j--;
if (i <= j) {
std::swap(a[i], a[j]);
i++; j--;
}
} while ( i<=j );
// We need to go deeper only for needed part of a
if ( k<=j+1 )
return largest_k(a, left, j, k);
if ( k>= i )
return largest_k(a, i, right, k);
}
int main()
{
int arr[] = {54, 897, 87, 4, 6,987};
size_t length = 6, k = 3;
std::cout<<largest_k<int>(arr, 0, length-1, k);
}
I was reading through this thread Array slicing in c++ and came close to what I was looking for but not exactly. I want to remove the first 3 elements from an int array.
What I Have Now:
void shiftArray(int *arr, int size) {
for (int i = 0; i < size - 3; i++) {
arr[i] = arr[i + 3];
arr[i + 3] = -1; // can't re size array so set them to -1
}
}
What I Want:
int* shiftArray(int *arr, int size) { // return type can stay void if possible
return arr[3::]; // python
}
Is it possible to have a non iterative approach to shift the first three elements to the end with value -1 and move the rest of the elements up to take their place?
You can use std::rotate and std::fill:
std::fill( std::rotate(arr, std::begin(arr) + 3, std::end(arr)), std::end(arr), -1);
http://coliru.stacked-crooked.com/a/b3e0557ee0481162
... but, its not as elegant as php :-)
[edit]
above requires C++11 below is compatible with older C++ standard versions:
template<typename T, size_t n>
void left_shift(T (&arr)[n], size_t left_shift){
assert(left_shift < n);
std::fill( std::rotate(arr, arr + left_shift, arr + n), arr + n, -1);
}
left_shift(arr, 3);
http://coliru.stacked-crooked.com/a/c09c27e3ebd60952
It sounds like you want to remove the first 3 elements in-place, using a resizeable array.
A resizeable array in C++ is called std::vector. The constructs std::array, and C-style arrays, are not resizeable.
In generic form the code could be:
template<size_t N, typename T>
void erase_start( std::vector<T> &arr )
{
if ( arr.size() <= N )
arr.clear();
else
arr.erase( arr.begin(), arr.begin() + N );
}
There isn't a pre-potted vector member function to erase based on count, it only takes iterators. But you could make such a function easily enough.
The invocation could look like:
std::vector<int> vec = { 1, 2, 3, 4, 5, 6, 7 };
erase_start<3>(vec);
use this function it simply delete the old one and return new int array pointer by shifted cells, I want just mention something shiftStartIndex is where you want to shift from until end of array so the new size of array will be (size - shiftStartIndex) this prevent array to get out of index, I hope this be useful.
int *shiftArray(int *, int, int);
int main()
{
int size = 6;
int *b = new int[size]{ 1, 2, 3, 4, 5, 6 };
int shiftStartIndex = 3;
b = shiftArray(b, size, shiftStartIndex);
int newSize = size - shiftStartIndex;
for (int i = 0; i < newSize; i++)
{
cout << b[i] << " ";
}
cin.ignore();
return 0;
}
int *shiftArray(int *arr, int size, int stIndex)
{
int *a = new int[size - stIndex];
for (int i = 0; i < size - stIndex; i++)
{
a[i] = arr[i + stIndex];
}
delete[] arr;
return a;
}
Is there a way to take two int arrays in C++
int * arr1;
int * arr2;
//pretend that in the lines below, we fill these two arrays with different
//int values
and then combine them into one larger array that contains both arrays' values?
Use std::copy defined in the header <algorithm>. The args are a pointer to the first element of the input, a pointer to one past the last element of the input, and a pointer to the first element of the output.
( https://en.cppreference.com/w/cpp/algorithm/copy )
int * result = new int[size1 + size2];
std::copy(arr1, arr1 + size1, result);
std::copy(arr2, arr2 + size2, result + size1);
Just suggestion, vector will do better as a dynamic array rather than pointer
If you're using arrays, you need to allocate a new array large enough to store all of the values, then copy the values into the arrays. This would require knowing the array sizes, etc.
If you use std::vector instead of arrays (which has other benefits), this becomes simpler:
std::vector<int> results;
results.reserve(arr1.size() + arr2.size());
results.insert(results.end(), arr1.begin(), arr1.end());
results.insert(results.end(), arr2.begin(), arr2.end());
Another alternative is to use expression templates and pretend the two are concatenated (lazy evaluation). Some links (you can do additional googling):
http://www10.informatik.uni-erlangen.de/~pflaum/pflaum/ProSeminar/
http://www.altdevblogaday.com/2012/01/23/abusing-c-with-expression-templates/
http://aszt.inf.elte.hu/~gsd/halado_cpp/ch06s06.html
If you are looking for ease of use, try:
#include <iostream>
#include <string>
int main()
{
int arr1[] = {1, 2, 3};
int arr2[] = {3, 4, 6};
std::basic_string<int> s1(arr1, 3);
std::basic_string<int> s2(arr2, 3);
std::basic_string<int> concat(s1 + s2);
for (std::basic_string<int>::const_iterator i(concat.begin());
i != concat.end();
++i)
{
std::cout << *i << " ";
}
std::cout << std::endl;
return 0;
}
Here is the solution for the same-
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void Concatenate(char *s1,char *s2)
{
char s[200];
int len1=strlen(s1);
int len2=strlen(s2);
int j;
///Define k to store the values on Kth address Kstart from 0 to len1+len2;
int k=0;
for(j=0;j<len1;j++)
{
s[k]=s1[j];
k++;
}
for(j=0;j<len2;j++)
{
s[k]=s2[j];
k++;
}
///To place a null at the last of the concatenated string to prevent Printing Garbage value;
s[k]='\n';
cout<<s;
}
int main()
{
char s1[100];
char s2[100];
cin.getline(s1,100);
cin.getline(s2,100);
Concatenate(s1,s2);
return 0;
}
Hope it helps.
for (int i = 0; i< arraySize * 2; i++)
{
if (i < aSize)
{
*(array3 + i) = *(array1 + i);
}
else if (i >= arraySize)
{
*(array3 + i) = *(array2 + (i - arraySize));
}
}
This might help you along, it doesn't require vectors. I had a similar problem in my programming class. I hope this helps, it was required that I used pointer arithmetic. This assumes that array1 and array2 are initialized dynamically to the size "aSize"
Given int * arr1 and int * arr2, this program Concatenates in int * arr3 the elements of the both arrays. Unfortunately, in C++ you need to know the sizes of each arrays you want to copy. But this is no impediment to choose how many elements you want to copy from arr1 and how many from arr2.
#include <iostream>
using namespace std;
int main(){
int temp[] = {1,2,3,4};
int temp2[] = {33,55,22};
int * arr1, * arr2, *arr3;
int size1(4), size2(3); //size1 and size2 is how many elements you
//want to copy from the first and second array. In our case all.
//arr1 = new int[size1]; // optional
//arr2 = new int[size2];
arr1=temp;
arr2=temp2;
arr3 = new int;
//or if you know the size: arr3 = new int[size1+size2];
for(int i=0; i<size1+size2; i++){
if (i<size1)
arr3[i]=arr1[i];
else
arr3[i] = arr2[i-size1];
}
cout<<endl;
for (int i=0; i<size1+size2; i++) {
cout<<arr3[i]<<", ";
}
}