In: Computer Science
I need you to do both of the programs. Please specify which program is which!
Array Allocator:
Write a function that dynamically allocates an array of integers. The function should accept an integer argument indicating the number of elements to allocate. The function should return a pointer to the array. Call the function in a complete program.
Reverse Array:
Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.
PROGRAM 1ST:
Array Allocator:
#include<iostream>
using namespace std;
//numberInput is itself a argument of function allocating and
//number is argument of numberInput
int numberInput(int number);
//allocating with argument numberInput
int* allocating(int numberInput);
//main function
int main()
{
//initialize number is equal to 0
int number=0;
//array
int *array;
number = numberInput(number);
array=allocating(number);
//priting dynamically allocating array values at last
for(int i=0;i<number;i++)
{
cout<<array[i]<<endl;
}
}
//number is the array size
int numberInput(int number)
{
cout<<"enter the array size \n";
//inputting number value from user
cin>>number;
return number;
}
int* allocating(int numberInput)
{
//dynamic allocate to a new array
int *array = new int(numberInput);
for(int i=0;i<numberInput;i++)
{
//Enter number from user
cout<<"Enter number"<<i+1<<endl;
cin>>array[i];
}
//returning save values in new array
return array;
}
The output of the program is as:
Reverse Array:
#include<iostream>
using namespace std;
//reverseArray function
int* reverseArray(int *, int);
int main()
{
//declaring size of array using SIZE
const int SIZE = 10;
//a simple example to show a demonstrate view of array size 10
int array[SIZE] = {12,2,3,4,5,6,7,8,9,33};
// It will print the elements of array that is already declared
cout<< "Starting array: \n";
for(int i = 0; i<SIZE; i++)
cout<< *(array + i) << " ";
// taking arumments array, and SIZE
int *revArray = reverseArray(array, SIZE);
// Reversing array using revARR
cout<< "\n\nReversed array: \n";
for(int i = 0; i<SIZE; i++)
cout<<*(revArray + i) <<" ";
cout<< endl;
// Deleting memory allocation of revArray
delete [] revArray;
return 0;
}
//reverseArray function with arguments array and SIZE
int* reverseArray(int *array, int SIZE){
// allocating dynamically a new array
int *ptr = new int[SIZE];
//saving the value of original array in reverse order to new one
for(int i=0; i<SIZE; i++){
*(ptr + SIZE - 1 - i) = *(array + i);
}
//returning the pointer to new one
return ptr;
}
The Output of the program is as:
C Program of ReverseArray without function:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
/* malloc is used in a dynamic array to allocate memory to array/structure
and free is used to clear the memory or delete the allocated memory
*/
int main()
{
int n, sum=0; //n is for size of array
printf("Enter the size of array: \n");
scanf("%d", &n); //inputting array size
printf("Enter elements of array: \n");
int *value; //*value is array
value = (int*) malloc(n*sizeof(int)); // where int* is typecasting
for(int i=0; i<n; i++)
scanf("%d",&value[i]);
//for loop is used to print reverse array elements
for(int i=n-1; i>=0; i--)
printf("%d ",*(value+i)); //Printing array elements in reverse order
//clearing/deleting the memory
free(value);
}
The output of the program is as: