0

Alright so this is probably a really simple question, I'm just really new to c++ and am struggling to understand most things. I've been asked to create an array that stores random numbers but the user is to define the size of the array. I then have to display all the numbers in the array. What I'm struggling with is displaying all elements of the array. At the moment, I'm pretty sure I've got the array part down, but I can only seem to display one number. Here's my code so far:

#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <ctime>

using namespace std;
using std::srand;
using std::rand;
using std::time;

void Display(int *, int);

int main(void)
{
    int userSize;
    cout << "Please enter the size of the array: ";
    cin >> userSize;

    int* randArray = new int[userSize];

    srand(time(NULL));

        for (int i = 0; i < userSize; i++) {
        randArray[i] = rand() % 20 + 1;
        }

        //Disregard the next few lines, I was just testing to see if anything was actually in the array. 
        /*cout << randArray[0] << endl;
          cout << randArray[1] << endl;
          cout << randArray[2] << endl;
          cout << randArray[19] << endl;*/

    Display(randArray, sizeof(randArray) / sizeof(int));
    return 0;

    delete[] randArray;
}

void Display(int *arrayData, int numElements)
{
    for (int counter = 0; counter < numElements; counter++)
    {
    std::cout << *(arrayData + counter) << std::endl;
    }
    return;
}

I should also mention that the teacher provided us with the code after the line that deletes the array.

This is the question I have to answer: Ask the user for the number of elements to be stored in the array. You should then dynamically allocate the memory to hold this array, which will proceed to be used in the same way as the previous task (populating the array with random data, displaying the data to the screen).

Garfield
  • 23
  • 5
  • 1
    What do you mean? The array has as many elements as the user chooses. – gsamaras Sep 17 '15 at 13:07
  • ^Right, if the user inputs 10, then you'll have 10 elements in the array. What are you asking? – Luke Xu Sep 17 '15 at 13:08
  • 2
    `randArray` is a pointer, so `sizeof(randArray)` is the size of a pointer... – user253751 Sep 17 '15 at 13:09
  • Sorry, my lack of understanding of c++ seems to impact the way I explain things. What I mean is that the user first gets prompted to choose the size of the array. So if the user chooses 20, then the array is made up of 20 'boxes' and each of them is filled with a random number between 1-20. Hope that makes it a little clearer. – Garfield Sep 17 '15 at 22:46

2 Answers2

2

The problem is sizeof. It gives you the size of the type of the argument, not of what is behind. Your should pass userSize to Display().

You should also delete the array before you return. Code behind return never gets executed.

Gombat
  • 1,994
  • 16
  • 21
2

sizeof(randArray) does not tell you the number of bytes that you've allocated. Rather, it tells you the size of a pointer, which on your system happens to be the same as the size of an integer, so sizeof(randArray) / sizeof(int) returns 1 always. Instead use userSize as your second parameter in the function call to Display.

Also, you delete[] randArray after return 0. This is incorrect; nothing after the return 0 will be executed. You want it above instead.

Further, consider the use of std::vector instead (unless you are required to use a raw pointer for this assignment)

R_Kapp
  • 2,818
  • 1
  • 18
  • 32
  • I need to display all elements of the array. So say the user enters 20, the array then has 20 'boxes' with a random number in them (between 1-20). I can only seem to display one number when it should display all 20. Apparently it has something to do with how I'm calling Display(). I'll also update OP with the exact question. Also, I don't think I'm allowed to use `std:vector`. – Garfield Sep 18 '15 at 05:06
  • As I mention in the answer, you are calling `Display` incorrectly. What you want to do is `Display(randArray, userSize);`. – R_Kapp Sep 18 '15 at 17:42
  • Ahhhh I see what I was doing wrong now. Thank you so much, you have no idea how relieving it was when I saw all 20 numbers displayed. – Garfield Sep 19 '15 at 01:41