+ 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.
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
+ 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
/*
+ 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
+ 1
it's not working that way Sandeep I tried it
+ 1
Could you please post the error message, you told us occurs by running your program?
+ 1
gj @nikhil thanks for formatting the answer
+ 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 :)
+ 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.
0
it's not working Nikhil dharma .I tried that also.
0
okkk