Student Project
2024-10-29 04:30:57 UTC
Do you guys use dynamic arrays to create arrays at compile time (without
using vector class)? For example:
/********************************************************************************/
#include <iostream>
void Generate_Random(size_t size);
int main(void)
{
// generate dynamic array by changing 20 to something else!
Generate_Random(20);
}
void Generate_Random(size_t size)
{
srand((unsigned)time(NULL));
int *numbers = new int[size]{0};
/* initialize the first number to 0 then don't use it
* start the for loop from 1 as usual to make it easy
*/
numbers[0] = 0;
for (size_t i = 1; i <= size; i++)
{
numbers[i] = 100 + rand() % 101;
}
for (size_t i = 1; i <= size; i++)
{
std::cout << numbers[i] << ", ";
}
}
/********************************************************************************/
using vector class)? For example:
/********************************************************************************/
#include <iostream>
void Generate_Random(size_t size);
int main(void)
{
// generate dynamic array by changing 20 to something else!
Generate_Random(20);
}
void Generate_Random(size_t size)
{
srand((unsigned)time(NULL));
int *numbers = new int[size]{0};
/* initialize the first number to 0 then don't use it
* start the for loop from 1 as usual to make it easy
*/
numbers[0] = 0;
for (size_t i = 1; i <= size; i++)
{
numbers[i] = 100 + rand() % 101;
}
for (size_t i = 1; i <= size; i++)
{
std::cout << numbers[i] << ", ";
}
}
/********************************************************************************/