Question

(C++) Write a function that accepts an int array and the array’s size as arguments. The function...

(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 than 50) from standard input and then reads N integers from a file named data into an array.

The program then passes the array to your array expander function, and prints the values of the new expanded array on standard output, one value per line.

You may assume that the file data has at least N values.

There are no prompts for the integer and no labels for the expandedreversed array that is printed out.

If the integer read in from standard input exceeds 50 or is less than 0 the program terminates silently.

1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

#include <iostream>

#include <fstream>

using namespace std;

int * arrayExpander(int *arr,int size)

{

   int doubledSize=size*2;

     

   int* arrExpanded = new int[doubledSize];

   for(int i=0;i<size;i++)

      {

         arrExpanded[i]=arr[i];

      }

   for(int i=size;i<2*size;i++)

      {

         arrExpanded[i]=0;

      }

   return arrExpanded;

}

int main()

{

      cout<<"Enter N value: ";

      int N;

      cin>>N;

      cout<<"Enter "<<N<<" values:

";

      int arr[N];

      for(int i=0;i<N;i++)

         cin>>arr[i];

      int* answer=new int[N*2];

      answer=arrayExpander(arr,N);

      for(int i=0;i< 2*N;i++)

         {

            cout<<answer[i]< < endl;

         }

      return 0;

}


Add a comment
Answer #2

     

  

#include <iostream>

#include <fstream>

using namespace std;

int * arrayExpander(int *arr,int size)

{

   int doubledSize=size*2;


 intarrExpanded = new int[doubledSize];

   for(int i=0;i<size;i++)

      {

         arrExpanded[i]=arr[i];

      }

   for(int i=size;i<2*size;i++)

      {

         arrExpanded[i]=0;

      }

   return arrExpanded;

}

int main()

{

      cout<<"Enter N value: ";

      int N;

      cin>>N;

      cout<<"Enter "<<N<<" values:

";

      int arr[N];

      for(int i=0;i<N;i++)

         cin>>arr[i];

      intanswer=new int[N*2];

      answer=arrayExpander(arr,N);

      for(int i=0;i2*N;i++)

         {

            cout<<answer[i]< < endl;

         }

      return 0;

}



answered by: Shahryar Khan
Add a comment
Know the answer?
Add Answer to:
(C++) Write a function that accepts an int array and the array’s size as arguments. The function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT