Difficulty Range: Freshmen

This is a simple, easy way to make your own subarray system. It is important to understand these to get a grasp on how they work and it’s a fun simple problem. Create an array from another array from one position to the end position you select. This smaller array will have all the elements in that range. 

Input: 1,5,78,13,8,23,8,73,75,138,2 Subarray(arr,3,7)

Output: 78,13,8,23

Solution : 

This is a generic solution, but if you don’t understand just substitute T for any datatype you prefer.

T Subarray<T>[](T[] array,int start,int end){

T[] sub = new T[];

Int x =0;

for(int i = start;i < end;i ++ ){

Sub[x] = array[start];

x++;

}

Return sub;

}

Subscribe to Blog via Email

Enter your email address to subscribe to Layers Media and receive notifications of new posts by email.

Join 82 other subscribers

David Kozdra

David Kozdra is a computer science student at Florida Polytechnic University. He is the Treasurer of the Media Club, Head of Outreach of the Baked Bean Bois improv group, and the Cohost of the Poly Pod podcast.

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.