+ 3

We put a limit in an array like a[5] or a[4].in a program we need the user to input the limits.how can we do it.

I asked it because I am creating a program in which I want the user to set the array limits.when I m using an variable.it is giving an error.how to fix it.or sole it.the error is you need an constant as a limits.

28th Nov 2016, 7:55 AM
Adnan Aatif
Adnan Aatif - avatar
10 Respostas
+ 2
yes Nikhil it will work the reason of error might be null value in his code the variable should have value first then it can be used to create array as yours code used but if array uses the variable before getting value it will surely error
28th Nov 2016, 10:36 AM
Sandeep Chatterjee
+ 1
#include<iostream.h> #include<conio.h> void main() { int I,r=0,k,b,n,c,z=1; cout<<"enter a number"; cin>>n; b=n; c=n/2; int a[n][b]; for(i=1;i<=c;++i) { for(j=1;i<=4;++j) { if(j==1) { for(k=r;i<b;++k) { a[r][k]=z; z=z+1; } else if(j==2) for(k=r+1;k<b-1;++k) { a[k][b-1]=z; z=z+1; } } else if(j==3) { for(k=b-2;k=>r;++k) { a[b-1][k]=z; z=z+1; } } else if(j==4) { for(k=b-2;k=>r;++k) { a[k][r]=z; z=z+1; } } r=r+1; b=b+1; } if(n%2==1) { a[n/2][n/2]=z; } for(i=0;k<n;i++) { for(j=0;j<n;j++) cout<<a[i][j]; } cout<<endl; } } */ if user input 4 output 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 /*
28th Nov 2016, 8:36 AM
Adnan Aatif
Adnan Aatif - avatar
+ 1
you can make array even with no of element as variable like enter no of elements then take input and then array[x] it is not that it has to definite no always like int days[7] or arrays[6] yes it is said correct that you can ask user to input no of elements to be present there you can see my answers for matrixes to see the codes
28th Nov 2016, 9:36 AM
Sandeep Chatterjee
+ 1
it's not working that way Sandeep I tried it
28th Nov 2016, 9:41 AM
Adnan Aatif
Adnan Aatif - avatar
+ 1
Could you please post the error message, you told us occurs by running your program?
28th Nov 2016, 9:51 AM
Andreas K
Andreas K - avatar
+ 1
gj @nikhil thanks for formatting the answer
28th Nov 2016, 10:07 AM
Andreas K
Andreas K - avatar
+ 1
ok Adnan I got it but reason it didn't work might be that the variable had no declared value you can see Nikhils code he declared n as the input no. and used n to create array similarity variable can be used to create array :)
28th Nov 2016, 10:29 AM
Sandeep Chatterjee
+ 1
you can use vectors instead of standard arrays so that you can use a user defined value as it's size. you can even change the size of array during runtime.
30th Nov 2016, 10:51 AM
Vishnu Poothery
Vishnu Poothery - avatar
0
it's not working Nikhil dharma .I tried that also.
28th Nov 2016, 8:03 AM
Adnan Aatif
Adnan Aatif - avatar
0
okkk
28th Nov 2016, 8:05 AM
Adnan Aatif
Adnan Aatif - avatar