Q:

Write a C++ program to create a new array taking the first and last elements of a given array of integers and length 1 or more

0

Write a C++ program to create a new array taking the first and last elements of a given array of integers and length 1 or more

Sample Output:

Original array:
10 20 -30 -40 30
New array:
10 30

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
using namespace std;

int *test(int nums[], int arr_length) {
   static int  r_array[] 
= {nums[0], nums[arr_length - 1] }; 
    return r_array;
}

int main () {
   // a pointer to an int.
   int *p;
   int nums[] = { 10, 20, -30, -40, 30 };	
   
   int arr_length = sizeof(nums) / sizeof(nums[0]);
   cout << "Original array: " << endl;   
   for ( int i = 0; i < arr_length; i++ ) {    
      cout << nums[i] << " ";
   }
   
   p = test(nums, arr_length);
   arr_length = sizeof(p) / sizeof(p[0]);
   cout << "\nNew array: " << endl;
   for ( int i = 0; i < arr_length; i++ ) {
      cout << *(p + i) << " ";
   }

   return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now