I need to create a main function with a one dimension dynamic array with float data type. The total number of array elements must be controlled by a user input from the keyboard. Test data is three different lengths 3,6,9 of the array. The lengths have to be set up at run time from users input. I understand how to create dynamic array but not where the user inputs the length of the array. How would I implement this?
argc is the number of parameters on the command line (including the program name itself). argv contains the parameters themselves, so argv[0] is the program name, argv[1] is the first parameter etc.
You could use atoi() to convert the string to a number.
Alternatively, if the input comes after the user starts the program then you'll need to use some IO. Look at some other threads to see how this is done.
To create a dynamic array of 5 ints, you'd say: int *arr = newint[5];
To clarify dhayden's second code example, they mean to say:
1 2 3 4 5 6 7 8 9 10 11
//Code code code
int ArrSize = 0;
std::cout << "Enter Size Of Array\n";
std::cin >> ArrSize;
//Probably A Good Idea To Check The Users Input Here
float* arr = newfloat[ArrSize];
//Code code code
It has been deallocated because you have called delete[] on line 20. There is no way to programmatically check whether you have remembered to do that however.