Dynamically creating an array of Ints

Nov 1, 2019 at 1:27am
Im supposed to write code that will dynamically create an array of ints. The size of the array should be determined by the number the user enters. Therefore, you must prompt the user to enter a number. After you have created the array, write the code to populate the array with random numbers between 1 and 100. Finally, print out the contents of the array on a SINGLE line, separated by TABS

Here is what I have so far. What am I doing wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
int arraySize;
cout << "Enter the size of the array: ";
cin >> arraySize;

int *array = new int[arraySize];

for (int j = 0; j < arraySize; j++)
{

	arraySize = rand() % 101;
	cout << array[j];

}
Last edited on Nov 1, 2019 at 1:28am
Nov 1, 2019 at 3:13am
Everything looks fine, but you haven't called srand() and set it to a seed like this:

srand(static_cast<unsigned int>(time(0)));

^Will need #include <ctime>
Nov 1, 2019 at 3:23am
@zapshe, the generated random number is not being assigned to the array element in line 10. The OP is assigning a random value to arraySize.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
   srand(static_cast<unsigned>(time(nullptr)));

   std::cout << "Enter the size of the array: ";
   size_t arraySize { };
   std::cin >> arraySize;
   std::cout << '\n';

   int* arr = new int[arraySize];

   for (size_t loop = 0; loop < arraySize; loop++)
   {
      arr[loop] = rand() % 100 + 1;

      std::cout << arr[loop] << '\t';
   }
   std::cout << '\n';
}

Enter the size of the array: 8

33      80      25      67      90      86      75      94


@Aneiron, if you are going to be writing C++ code use the <random> library to generate random numbers, std::vector for dynamic arrays (C++11):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <vector>
#include <random>

int main()
{
   std::cout << "How many numbers do you want? ";
   unsigned num { };
   std::cin >> num;
   std::cout << '\n';

   std::vector<int> vec(num);

   std::default_random_engine rng(std::random_device{}());

   std::uniform_int_distribution<int> dis(1, 100);

   for (auto& itr : vec)
   {
      itr = dis(rng);

      std::cout << itr << '\t';
   }
   std::cout << '\n';
}

How many numbers do you want? 9

78      29      26      19      85      46      5       16      6
Nov 1, 2019 at 3:31am
@zapshe, the generated random number is not being assigned to the array element in line 10. The OP is assigning a random value to arraySize.

LOL, completely missed that.
Nov 1, 2019 at 3:35am
Thanks all!
Nov 1, 2019 at 4:04am
@Aneiron,

Since C++11 array is the name of a new STL container, std::array.

http://www.cplusplus.com/reference/array/array/

Using array as the name of a variable can lead to certain complications, especially if you have using namespace std; in your source.
Topic archived. No new replies allowed.