/* 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 = 5;
int* expandArray(int *a, int expFactor);
int main()
{
int origArr[NUM_ELEM], n;
for (int i = 0; i < NUM_ELEM; ++i)
origArr[i] = i;
cout << "Original array" <<
endl;
for (int i = 0; i < NUM_ELEM; ++i)
cout << origArr[i]
<< " ";
cout << endl;
cout << "By how many times should the new array grow" << endl;
cin >> n;
int *newArr = expandArray(origArr, n);
for (int i = 0; i < n*NUM_ELEM; ++i)
cout << newArr[i]
<< " ";
cout << endl;
delete [] newArr;
return 0;
}
int* expandArray(int *a, int expFactor)
{
// Your code goes
here
}
Dear Student,
below i have completed the given C++ program as per the requirement.
Please note that the below program is tested on ubuntu 14.04 sytem and compiled in g++ compiler. This program will also work on other IDE's
================================================================
Program:
================================================================
#include <iostream>
using namespace std;
const int NUM_ELEM = 5;
int* expandArray(int *a, int expFactor);
int main()
{
int origArr[NUM_ELEM], n;
for (int i = 0; i < NUM_ELEM; ++i)
origArr[i] = i;
cout << "Original array" << endl;
for (int i = 0; i < NUM_ELEM; ++i)
cout << origArr[i] << " ";
cout << endl;
cout << "By how many times should the new array grow" << endl;
cin >> n;
int *newArr = expandArray(origArr, n);
for (int i = 0; i < n*NUM_ELEM; i++)
cout << *(newArr+i) << " ";
cout << endl;
//delete [] newArr;
return 0;
}
//complete defintion
int* expandArray(int *a, int expFactor)
{
// Your code goes
here
int *copy_array = new int[NUM_ELEM * expFactor];
for(int i = 0 ; i< NUM_ELEM; i++)
{
*(copy_array+i) = *(a+i);
}
return copy_array;
}
================================================================
Output:
================================================================
Kindly Check and Verify Thanks...!!!
/* Array expander: Write a function that accepts an int array as argument. The function should...
(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....
(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...
Who could write the array.cpp file ? //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...
One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << ; cout << endl; return 0; }
#include <iostream> using namespace std; int main(void) { int SIZE; cout<<"Enter the size of the array"<<endl; cin>>SIZE; int *numlist = new int[SIZE]; // Read SIZE integers from the keyboard for (int i = 0; i<SIZE; i++ ) { cout << "Enter value #" << i+1 << ": "; cin >> numlist[i]; } // Display the numbers in a reverse order for (int i = SIZE; i > 0; i--...
1. The following program calls the function countLarger that accepts three arguments: an integer array, an integer size that indicates how many elements are in the array, and an integer n. The function countLarger should return the number of integers in the array that are greater than the value of the argument n. Update the program to include the definition of the function countLarger. #include <iostream> using namespace std; int countLarger(int[], int, int); // Function prototype int main() const int...
When running the program at the destructor an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public: varArray(); // void constructor int arraySize() const { return size; } // returns the size of the array int check(double number); // returns index of element containg "number" or -1 if none void addNumber(double); // adds number to the array void removeNumber(double); // deletes the number from the array ...
Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){ int rows = 5; int cols = 5; int x; int** arr = new int[rows][cols] cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x]; return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){ int rows; int cols; int x;...
Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array and // its size and returns the index of the first occurrence of the largest element II in the array. Also, write a function to display the array #include ·peh.h" #include <iostream> using namespace std const int ARRAY_SIZE = 15; int main int list[ARRAY SIZE56, 34, 67, 54, 23, 87, 66, 92. 15, 32, 5, 54, 88, 92, 30 cout < List elements: "...
Make the pointer point at the 5th element of an array 1 #include <iostream> 3 using namespace std; 4 //make the pointer point at the 5th element of an array 6 int main) 7 int x[10]; 9 cin x[i]; 10 11 int*ptr; 12 13 14 cout <<*ptr << endl; 15 1 #include 3 using namespace std; 4 //make the pointer point at the 5th element of an array 6 int main) 7 int x[10]; 9 cin x[i]; 10 11 int*ptr;...