Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

can we ask a user to input the size of array plz tell how with help of code ??????

+3 votes
asked Jul 6 by AADI JAIN (340 points)
int main()

int arr[n],

cin>>n;

can we do this ?????

1 Answer

0 votes
answered Jul 6 by Peter Minarik (55,660 points)
edited Jul 6 by Peter Minarik

Only if you input n first, then declare your array later.

In your code, you declare your arr to be n element long. However, at the time of execution at that point, the value of n is undefined. Hence the result is undetermined and your code is wrong.

Would you input the value of n first, the program would have arr initialized to the right size.

Here's an example code below:

#include <iostream>

using namespace std;

int main()
{
    size_t size;
    cout << "Please, enter the size of the array: ";
    cin >> size;
    
    int array[size];
    
    // Have fun using your array
    cout << "The actual size of array is: " << (sizeof(array) / sizeof(array[0])) << endl;
    
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...