I was writing a simple sorting program but scanf()
appears to be stuck and keeps taking infinite inputs. I know if I enter an invalid input this will happen. But, even when I enter int
this occurs.
Here is the code:
#include <stdio.h>
int main() {
int arr[5],temp,pos,i=0;
printf("Enter the elements\n");
while(i<5){
scanf("%d ",&arr[i]);
++i;
}
printf("Sorted Array:");
for(int i=0;i<4;++i){
pos = i;
for(int j=i+1;j<5;++i){
if(arr[pos]>arr[j])
pos = j;
}
temp = arr[i];
arr[i] = arr[pos];
arr[pos] = temp;
}
for(int i=0;i<5;++i){
printf("%d",arr[i]);
}
return 0;
}