Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Pointers and Functions a. Create a function called resize that can be used to in

ID: 3819960 • Letter: P

Question

Pointers and Functions

a. Create a function called resize that can be used to increase the size of integer arrays dynamically. The function takes three parameters. The first parameter is the original array, the second parameter is the size of this array, and the third parameter is the size of the larger array to be created by this function. Make sure that you allocate memory from the heap inside this function. After allocating memory for the second array the function must copy the elements from the first array into the larger array. Finally, the function must return a pointer to the new array.

b. In main, allocate an array on the heap that is just large enough to store the integers 5, 7, 3, and 1.

c. Resize the array to store 10 integers by calling the resize function created in step a. Remove the old (smaller) array from the heap. Add the numbers 4, 2, and 8 to the end of the new array.

d. Write a sort function that sorts any integer array in increasing order.

e. Use the sort function to sort the array of numbers in c above. Display the sorted numbers.

Explanation / Answer

#include<stdio.h>
#include <time.h>
#include <stdlib.h>
#define size 4

int * resize(int *p_arr, int old_size, int new_size)
{
   int *p_arr2,i,n;
   time_t t;
   n=new_size-3;
  
   p_arr2 = (int *)malloc(sizeof(int)*new_size);   //allocating memory from heap
   srand((unsigned) time(&t));
   for(i=0;i<old_size;i++)
       p_arr2[i] = p_arr[i];

   for( i = old_size ; i < n ; i++ )   //initialising the remaining values in array2 randomly
   {
      p_arr2[i] = 1 + rand() % 10;
   }
  
   p_arr2[new_size-3] = 4;   // initialising last three values as given
   p_arr2[new_size-2] = 2;
   p_arr2[new_size-1] = 8;

   printf(" New Array is: ");
       for(i=0;i<new_size;i++)
   {
       printf("%d ",p_arr2[i]);
   }
   free(p_arr); //   removing array1 from heap
  
   return p_arr2;   //returning array2 pointer
}

void sort_arr(int *p_arr2,int n) // bubble sort to sort array2
{

   int i,j,temp;
   for (i = 0 ; i < ( n - 1 ); i++)
   {
       for (j = 0 ; j < n - i - 1; j++)
       {
          if (p_arr2[j] > p_arr2[j+1])
          {
           temp       = p_arr2[j];
           p_arr2[j]   = p_arr2[j+1];
           p_arr2[j+1] = temp;
          }
       }
   }
}

int main()
{
   int *p_arr ,i;
   int *p_arr2;
   int new_size;
   p_arr = (int *)malloc(sizeof(int)*size); //allocating array1 from heap

   printf("Values in original array: ");   //initialising array1 with given values
   p_arr[0] = 5;
   p_arr[1] = 7;
   p_arr[2] = 3;
   p_arr[3] = 1;
   for(i=0;i<size;i++)
   {
       printf("%d ",p_arr[i]);
   }
  
   printf(" Enter the increased size for new array(greater than 4): ");
   scanf("%d",&new_size);
  
   p_arr2 = resize(p_arr,size,new_size);   //calling resize
  
   sort_arr(p_arr2,new_size);   //sorting the new array2
  
   printf(" New array after sorting is: ");  
   for(i=0;i<new_size;i++)   //printing sorted array2
   {
       printf("%d ",p_arr2[i]);
   }
  

return 0;
}