Question

In C++ Create an array using pointers (and pointer notation) and fill it in with random...

In C++

  1. Create an array using pointers (and pointer notation) and fill it in with random numbers.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
    srand(time(NULL));
    int size;
    cout << "How many numbers: ";
    cin >> size;
    int **arr = new int*[size];
    for (int i = 0; i < size; ++i) {
        arr[i] = new int;
        *arr[i] = rand() % 100;
    }
    // print random array of pointers
    for (int i = 0; i < size; ++i) {
        cout << *arr[i] << " ";
    }
    cout << endl;
    // free memory
    for (int i = 0; i < size; ++i) {
        delete arr[i];
    }
    delete[] arr;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
In C++ Create an array using pointers (and pointer notation) and fill it in with random...
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