Question

Exit Full Screen code.cpp New 1 #include <iostream> 3 using namespace std; 4 5 int main) 6 int *ptr 7 ptr -new int[5]; 8 int

I have to write a loop that moves the content of the dynamic array to the static array but I can't seem to figure it out.

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

Answer.) As requested in the question, providing the implementation of the for loop to move elements from dynamic to static array.

Code:

#include <iostream>

using namespace std;

int main()
{
int *ptr;
ptr = new int[5];
int arr[5];
int x;
cin>>x;
for(int i=0;i<5;i++) //initialization
{
ptr[i]=x;
}
//moving elements from dynamic to static array
for(int i=0;i<5;i++)
{
arr[i]=ptr[i];
}
for(int i=0;i<5;i++)//printing array
{
cout<<arr[i];
}
  
delete [] ptr;

return 0;
}

Output:

5                                                                                                                                                                                  

55555

Add a comment
Know the answer?
Add Answer to:
I have to write a loop that moves the content of the dynamic array to the...
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
  • Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...

    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 loop(s) that print ALL the content of the following 2D array #include <iostream> using namespace...

    Write loop(s) that print ALL the content of the following 2D array #include <iostream> using namespace std; int main(){    int rows; int cols; int offset; cin >> rows; cin >> cols; cin >> offset;    int** arr = new int*[rows]; for(int i = 0; i < rows; ++i) arr[i] = new int[cols];    for(int i = 0; i < rows; i++) // initialization for(int j = 0; j < cols; j++) arr[i][j] = offset + i;    // printing, your code goes here   ...

  • Complete the mult() and printArr() functions #include <iostream> using namespace std; void init(int arr]05], int x)t //initialization loop for row 1 and 2 for(int i = 0; i < 2; i++){ arr i]...

    Complete the mult() and printArr() functions #include <iostream> using namespace std; void init(int arr]05], int x)t //initialization loop for row 1 and 2 for(int i = 0; i < 2; i++){ arr i][j] x; void mult(int arr JSD //multiply the elements of row 2 by row 1, and store result in row3 void printArr int arr ][5]) // a loop to print out all the elements of the array int main) int arr[3105] 31 int x 32 34 // initialize...

  • C++ language Write a function, int flip(string a[], int n); It reverses the order of the...

    C++ language Write a function, int flip(string a[], int n); It reverses the order of the elements of the array and returns n. The variable n will be greater than or equal to zero. Example: string array[6] = { "a", "b", "", "c", "d", "e" }; int q = flip(array, 4); // returns 4 // array now contains: "C" "" "" "a" "d" "e" O Full Screen 1 code.cpp + New #include <string> 2 using namespace std; 3 4- int...

  • Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify...

    Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify the class by adding sort member function (refer to Algorithm Analysis notes for sorting algorithms) Sample usage: a. sort(); Add the needed code in main to test your function. template <class T> 1/you can use keyword typename instead of class class Array { private: T *ptr; int size; public: Array(T arr[], int s); void print(); template <class T> Array<T>:: Array (T arr[], int s)...

  • In the blank below, write the contents of array arr after the following code is executed...

    In the blank below, write the contents of array arr after the following code is executed up to the comment indicated in the main procedure below? #include <iostream> #include <algorithm> using namespace std; void do something(int list[], const int size); int main() { const int SIZE(10); int arr[SIZE] = { 5, 8, 1, 7, 3, 9, 4, 6, 10, 2 }; do somethingCarr, SIZE); ** Write the contents of array arr in the space below when execution reaches here. */...

  • Make the pointer point at the 5th element of an array 1 #include <iostream> 3 using namespace std; 4 //make the p...

    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;...

  • Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random...

    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...

  • /* Array expander: Write a function that accepts an int array as argument. The function should...

    /* 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...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

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