Example arr = {5, 2, 1} n=3
Newarr = {5, 2, 1, 5, 2, 1, 5, 2, 1} (c++)
The Program Code is :
///Cpp Program
///To Copy one array to new array n times
#include<iostream>
using namespace std;
///Function definition
void copy_array_to_New_array(int arr[],int sz,int n)
{
int i,j;
int new_arr[sz*n];///New array
for(i = 0, j = 0 ;i < n*sz; i++,j++)
{
new_arr[i] = arr[j];
/// Here copying arr vales to New array
if(j == n-1)
{
j = -1;
}
}
cout<<"The new array elements are :
"<<endl;
for(i = 0; i < n*sz; i++)
{
cout<<new_arr[i]<<"\t";
}
cout<<endl;
}
int main()
{
int sz;
cout<<"Enter the size of array : ";
cin>>sz;
int arr[sz]; /// Integer array
int i;
cout<<"Enter the array values :
"<<endl;
for(i = 0; i < sz; i++)
{
cin>>arr[i];///Reading array elements
}
int n;
cout<<"Enter how many times copy the array
vales to new array :"<<endl;
cin>>n;
copy_array_to_New_array(arr,sz,n);///Function calling
}
Get Answers For Free
Most questions answered within 1 hours.