I'm struggling with a particular function I'm being tasked with. Severe beginner here, trying hard to understand this prompt but I'm failing miserably. Any and all help would be appreciated. I've included the instructions inside of the code for better understanding. Basically, I am completely backwards with my interpretation of how arrays are compiled, understood and then accessed and how they are made to output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//Using a loop with error trapping/validation, ask user for a number between 10-15.
//Allocate an array with the user provided number of elements and fill it with a random number
//between 1 and 100 using a single for-loop.
//Output the array in reverse order using another for-loop. Dispose of the array when done.
kUserPrompt ("Please enter a number between 10-15:")
kUserError ("User Error: Please input a value between 10-15");
int funcC(){
cout << kUserPrompt << endl;
cin >> userValue >> endl;
if (userValue < 11 && userValue > 14){
cout << kUserError << endl;
cin >> userValue >> endl;}
}
I understand declaring the array and returning the memory afterwards, but I am lost on filling it with a random number and the proper syntax. Any guidance would be appreciated.
#include <iostream>
#include <cstdlib> //for srand()
#include <ctime> //for time()
usingnamespace std;
int main()
{
//Using a loop with error trapping/validation, ask user for a number between 10-15.
do
{
cout<<"Enter an integer between 10 and 25: ";
cin>>size;
}while(size < 10 || size > 25);
//Allocate an array with the user provided number of elements and fill it with a random number
srand(time(0)); //seed with current time
int element;
int *array = newint[size]; //dynamically allocate array of size integers.
for(int i = 0; i < size; i++)
{
element = rand()%100 + 1 //element E[1,100]
array[i] = element;
}
//Output the array in reverse order using another for-loop. Dispose of the array when done.
for(int i = size - 1; i >= 0; i--)
cout<<array[i]<<" ";
cout<<endl;
delete [] array1;
cin.ignore();
return 0;
}