Home »
Code Snippets »
C/C++ Code Snippets
C program to copy elements from one array's specified from and to index to second array.
By: IncludeHelp, On 27 OCT 2016
In this c program, we will learn learn how to copy elements from one array’s specified from and to index to second array?
This program is asked from our regular reader Mr. SHAKTI TRIPATHI.
His question was:
(1)Suppose there are two array from [ ] and to [ ].
(2) I want to copy n numbers from the array from [ ] starting at index [ ] i, into array to [ ] starting at index j.
Reference- Youtube NPTEL c-programming lecture no.35 Example copies a sub array.
Here is the solution of this question.
In this program there are two arrays arr1 and arr2, we will read from as starting index and to as end index. Elements from one array will be copied into second array where from and to index value will be given by the user.
C program - Copy One Array’s elements from Specified from and to Index to Second Array
Let's consider the example
#include <stdio.h>
int main()
{
int arr1[10]={11,22,33,44,55,66,77,88,99,111};
int arr2[10]; //10 is max. elements
int from,to,i,j;
printf("Enter starting index: ");
scanf("%d",&from);
printf("Enter end index: ");
scanf("%d",&to);
if(from<0 || to>9)
{
printf("Invalid from/to index!!!\n");
return -1;
}
//copy from first array to second array
for(i=from,j=0; i<=to; i++,j++)
arr2[j]=arr1[i];
printf("first array is: ");
for(i=0;i<9;i++)
printf("%d ",arr1[i]);
printf("\n");
printf("second array is: ");
for(i=0;i<j;i++)
printf("%d ",arr2[i]);
printf("\n");
return 0;
}
Output
First Run:
Enter starting index: 2
Enter end index: 6
first array is: 11 22 33 44 55 66 77 88 99
second array is: 33 44 55 66 77
Second Run:
Enter starting index: 6
Enter end index: 15
Invalid from/to index!!!
Copy array with specified index using user define function
Here we are creating a function that will copy first array’s elements from specified indexes.
Function will return last index of second array that will be stored and lastIndex variable, so that we can use lastIndex while printing the elements of second array.
In the function copyArrayByFromToIndex we are passing array as integer pointers so that whatever changes will happen in arrays (especially in ar2) can be used in main function.
#include <stdio.h>
//function will copy one array's elements into second
//with specified from and to index
//return : int (last index of second array)
int copyArrayByFromToIndex(int *ar1, int *ar2, int from, int to)
{
int i,j;
for(i=from,j=0; i<=to; i++,j++)
*(ar2+j)=*(ar1+i);
return j; //last index of second array
}
int main()
{
int arr1[10]={11,22,33,44,55,66,77,88,99,111};
int arr2[10]; //10 is max. elements
int from,to,i;
int lastIndex=0; //store second array's last index
printf("Enter starting index: ");
scanf("%d",&from);
printf("Enter end index: ");
scanf("%d",&to);
if(from<0 || to>9)
{
printf("Invalid from/to index!!!\n");
return -1;
}
//copy from first array to second array
lastIndex=copyArrayByFromToIndex(arr1,arr2,from,to);
printf("first array is: ");
for(i=0;i<9;i++)
printf("%d ",arr1[i]);
printf("\n");
printf("second array is: ");
for(i=0;i<lastIndex;i++)
printf("%d ",arr2[i]);
printf("\n");
return 0;
}