I ran this code and input float values in array 's' but after sorting , new values of array elements are slightly different from input values. Why is it so ? This is the code I ran:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <conio.h>
void main()
{
int N,sorted,i;
printf("How many students?\n");
scanf("%d",&N);
float s[N],temp;
for(i=0;i<N;i++)
{
printf("Marks of student %d?\n",i+1);
scanf("%f",&s[i]);
}
//bubble sorting ---
while(1)
{
sorted=0;
for(i=0;i<N-1;i++)
{
if(s[i]<s[i+1])
{
temp=s[i];
s[i]=s[i+1];
s[i+1]=temp;
sorted=1;
}
}
if(sorted==0)
break;
}
printf("\nSorted Marks - \n\n");
for(i=0;i<N;i++)
{
printf("%f\n",s[i]);
}
}
Input:
N=5
Marks = 34.53,54,34,56.76,87.567
Output:
Sorted Marks -
87.567001
56.759998
54.000000
34.529999
34.000000