The question is to write C++ program using a function ( is_sorted() ) which takes a 1-D array and size as arguments, returns 1 if it is in ascending order, -1 if in descending order or else 0.
I used int* a[10]; as i was getting an error with just int a[10] when calling the function, can anyone explain to me why i need int* and mostly how to get rid of the illegal structure operation?
This is my code:
#
int is_sorted(int a[10], int n)
{int ct1=0, ct2=0;
for(int i=0;i<n;++i)
{
if(a[i]<=a[i+1])
ct1++;
else if(a[i]>=a[i+1])
ct2++;
}
if(ct1==n)
return 1;
else if(ct2==n)
return -1;
else return 0;
}
void main()
{int n;
int* a[10];
cin>>n; //the number of elements in an array
for(int i=0;i<n;++i) //to enter the array
cin>>a[i]; //the error
is_sorted(a[10], n);
getch();
}
I used int* a[10]; as i was getting an error with just int a[10] when calling the function, can anyone explain to me why i need int* and mostly how to get rid of the illegal structure operation?
This is my code:
#
int is_sorted(int a[10], int n)
{int ct1=0, ct2=0;
for(int i=0;i<n;++i)
{
if(a[i]<=a[i+1])
ct1++;
else if(a[i]>=a[i+1])
ct2++;
}
if(ct1==n)
return 1;
else if(ct2==n)
return -1;
else return 0;
}
void main()
{int n;
int* a[10];
cin>>n; //the number of elements in an array
for(int i=0;i<n;++i) //to enter the array
cin>>a[i]; //the error
is_sorted(a[10], n);
getch();
}
Comment