(C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function should return a pointer to the new array.
Hint: You must create a “new” array in the freestore ( the heap ) for this to work correctly.
Here is the required starting code below.
// Element Shifter
#include
using namespace std;
// Prototype
int *shift(int[], int);
void showArray(int[], int);
int main()
{
const int SIZE = 5;
int values[SIZE] = { 1,2,3,4,5 };
// Display the contents of the array.
cout << "The contents of the original array are:\n";
showArray(values, SIZE);
// Call the shift function to make a copy of
// the array, with the elements shifted one
// position toward the end if the array.
int *arrCopy = shift(values, SIZE); //Gets address of the new array in freestor
// Display the contents of the original array
//to prove it was not damaged
cout << "The contents of the original array are:\n";
showArray(values, SIZE);
// Display the contents of the new array.
cout << "The contents of the new array are:\n";
showArray(arrCopy, SIZE+1);
return 0;
}
// The shift function accepts an int
// array and an int indicating the array's
// size. The function returns a pointer to
// an array that is one element larger than
// the array that was passed as an argument.
// The elements of the argument array are
// copied to the new array, shifted one
// position toward the end of the array.
int *shift(int arr[], int size)
{
int junk = 0; //Simkin temp junk code so it will compile
return &junk; //Simkin temp junk code so it will compile
}
// The showArray function displays the contents
// of an int array.
void showArray(int arr[], int size)
{
cout << "inside showArray()" << endl; //Simkin temp junk code so it will compile
}
/* proof
The contents of the original array are:
1 2 3 4 5
The contents of the original array are:
1 2 3 4 5
The contents of the new array are:
0 1 2 3 4 5
*/
Answer :- The C code can be written as-
#include <iostream>
using namespace std;
int * shift(int arr[], int size); /* prototype of the shift function */
void showArray(int arr[], int size); /* prototype of the showArray function */
int main()
{
int numbers[5], *new_arr;
cout << "Enter 5 numbers: ";
// Storing 5 number entered by user in an array
for (int i = 0; i < 5; ++i)
{
cin >> numbers[i];
}
//showArray(numbers, 5);
new_arr = shift(numbers, 5);
cout<<"The new array content is-"<<endl;
showArray(new_arr, 6);
return 0;
}
int * shift(int arr1[], int size) /* definition of the shift function */
{
int *arr2;
arr2 = (int*) malloc((size+1)*sizeof(int));
arr2[0] = 0;
for(int i = 1; i < (size+1); i++){
arr2[i] = arr1[i-1];
}
return arr2;
}
void showArray(int arr[], int size)
{
for(int i=0; i<size;i++){
cout<<" "<<arr[i];
}
}
OUTPUT :-
Enter 5 numbers: 1
12
13
14
15
The new array content is-
0 1 12 13 14 15
(C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...
/* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...
- Write a program that performs several operations on an array of positive ints. The program should prompt the user for the size of the array (validate the size) and dynamically allocate it. Allow the user to enter the values, do not accept negative values in the array. - Your program should then define the functions described below, call them in order, and display the results of each operation: a) reverse: This function accepts a pointer to an int array...
C++ Array Expander and Element Shifter Thank you so much to anybody that can help! Here are our two homework prompts: PART A: Array Expander Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0....
(C++)Write a function that accepts an int array and the array’s size as arguments.The function should create a new array that is twice the size of the argument array.The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0.The function should return a pointer to the new array.Demonstrate the function by using it in a main program that reads an integer N (that is not more...
9.10: 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 by using it in the main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named...
Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random numbers between 0 and 100. The array is sent to a function that finds the indices of all items > 50. A new array is created and the indices are stored inside it. The size of the new arrays MUST BE the same as the number of items > 50. The function returns the new array which is then printed out by main. Here...
Can someone help me understand this step by step (C++)
Array Shifter and Array Reversal Write a function that accepts an array of doubles and the array's size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. The function should find all numbers in the array that are greater or equal to the average and fill out the second array with these numbers. You need to design the function. I tried this code but the errors returned were: expression must have a constant value #include<iostream> using...
C++. Write a program that copies the contents of one array into another array but in reverse order using pointers. - in main() - define 2 arrays of type int, 10 elements each - initialize one array with numbers 1 through 10 - initialize all elements of the second array to 0 - call function reverseCopy with both arrays as arguments - display the values of array number 2 (reversed order) - reverseCopy...
c++ program8. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of a file, a pointer to an int array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to the file, and then close the file. Write another function named fileToArray. This function should accept three argu- ments: the name of a file, a pointer to an int array, and the size...