Why there is runtime error ? What is wrong with the code?
Thanks
enter code here
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int main(void)
{
int t,n,i=1,ctr;
int arr[100000];
scanf("%d",&t);
while(t)
{
n=t;
ctr=1;
while(t--)
scanf("%d",&arr[i++]);
for(i=1;i<n;i++)
{
if(arr[arr[i]]!=i)
{
ctr=-1;
break;
}
}
if(ctr==1)
printf("ambiguous");
else if(ctr==-1)
printf("not ambiguous");
scanf("%d",&t);
}
return 0;
}
There are multiple errors :
- Run the for loop form 1 to <=n you are running it to < n.
- Increase the size of array , as the maximum elements can be 100000 , so index will get to 100001
- initialize i to 1 everytime .
- Print new line
Correcting above errors , here is your corrected
[1] .
[1]: http://www.codechef.com/viewsolution/4289666
There is many error in this program .
*in the header file h is missing <iostream.h>
*the array size is out of range,as it is integer data type