Question

In: Computer Science

I need you to do both of the programs. Please specify which program is which! Array...

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.

Solutions

Expert Solution

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:


Related Solutions

Please do the math by hand, do not use a program, I need to see the...
Please do the math by hand, do not use a program, I need to see the procedure, the answer itself is less important. Comparison of peak expiratory flow rate (PEFR) before and after a walk on a cold winter's day for a random sample of 9 asthmatics. Use the following data to determine if the patients conditioned changed after a walk. Present your results and make some interpretations. Subject Before After 1 312 300 2 242 201 3 340 232...
Choose one of the three following programs to do as your first program. You need to...
Choose one of the three following programs to do as your first program. You need to turn in a flowchart and program documentation with the .cpp program (this can be done within the program) and a copy of your output. The Program I chose is : #19. Monthly Payments The monthly payment on a loan may be calculated by the following formula: Payment= Rate × (1+Rate)N (1+Rate)N−1×L Rate is the monthly interest rate, which is the annual interest rate divided...
C++ PLEASE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- In this program, you will analyze an array
C++ PLEASE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- In this program, you will analyze an array of 10 characters storing a gene sequence. You will be given a subsequence of 3 characters to look for within this array. If the subsequence is found, print the message: Subsequence <XXX> found at index <i>. Where i is the starting index of the subsequence in the array. Otherwise, print Subsequence <XXX> not found. The array of characters and the subsequence will be given through standard input. Read them and...
I need to update this program to follow the these requirements please and thank you :...
I need to update this program to follow the these requirements please and thank you : Do not use packages Every class but the main routine class must have a toString method . The toString method for Quadrilateral should display its fields: 4 Points. All instance variables should be declared explicitly private . Area incorrect : enter points no: 1 1 3 enter points no: 2 6 3 enter points no: 3 0 0 enter points no: 4 5 0...
I need the code for a C++ program that creates an array of 5000 String objects...
I need the code for a C++ program that creates an array of 5000 String objects that will store each word from a text file. The program will read in each word from a file, and store the first 5000 words in the array. The text file should be read in from the command line.
can you please create the code program in PYTHON for me. i want to create array...
can you please create the code program in PYTHON for me. i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this: if N = 16, matrix is [ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4] if N = 64, matrix is [8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8...
// JavaLanguage . You need to write a program that asks the user for an array...
// JavaLanguage . You need to write a program that asks the user for an array size, and then asks the user to enter that many integers. Your program will then print out the sum , average, largest and smallest of the values in the array.
How do you use header files on a program? I need to separate my program into...
How do you use header files on a program? I need to separate my program into header files/need to use header files for this program.Their needs to be 2-3 files one of which is the menu. thanks! #include #include #include using namespace std; const int maxrecs = 5; struct Teletype { string name; string phoneNo; Teletype *nextaddr; }; void display(Teletype *); void populate(Teletype *); void modify(Teletype *head, string name); void insertAtMid(Teletype *, string, string); void deleteAtMid(Teletype *, string); int find(Teletype...
I need a scholarship essay for my nursing program. please and thank you.
I need a scholarship essay for my nursing program. please and thank you.
hi i need to do a C++ program. You are going to practice the use of...
hi i need to do a C++ program. You are going to practice the use of array by implementing the interface of Shuttle Puzzle. Here is an example of how you play the Shuttle Puzzle. Say that you start with a board with 7 holes and there are 3 black and 3 white marbles on the board in this configuration: W W W . B B B The dot (.) represents the empty hole withouth any marble. The objective of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT