In: Computer Science
C++
split_list(Lst,N): split lst into two parts with the first part
having N elements, and return a list that contains these two
parts.
yes it is an array
In this C++ program, we create an integer array Lst of 10 elements from 1 to 10. Then an integer N for the split size.
Then we call the Split_lst() function passing Lst and N as parameters.
In the function, we create a static array arr and using a for loop till N elements, copy the elements from Lst to arr.
Note: arr is a static array so that it can be passed between functions.
CODE:
#include <iostream>
using namespace std;
int* Split_list(int Lst[], int N)
{
static int arr[50]; //create static int array
for(int i=0; i<N; i++)
arr[i] = Lst[i]; //copy the split array
return arr; //return the resultant array
}
int main()
{
int Lst[10] = {1,2,3,4,5,6,7,8,9,10}; //initial array
int N = 4; //split size
cout<<"Original array: \n";
for(int i=0; i<10; i++)
cout<<Lst[i]<<" "; //display original
int* newLst = Split_list(Lst,N); //function call
cout<<"\n\nSplit array: \n";
for(int i=0; i<N; i++)
cout<<newLst[i]<<" "; //display split array
return 0;
}
OUTPUT: