hello guys,
the output of the program should be like this:
With how many numbers do you love to work with? 3
Enter no 1: 23.4
Enter no 2: 15.54
Enter no3: 55.48
In ascending order the numbers are:
15.54
23.40
55.48
I've written this program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,limit,flag;
double numbers[100],temp;
char inputstr[50];
printf("With how many numbers do you love to work with? ");
fgets(inputstr,49,stdin);
limit=atoi(inputstr);
for(i=0;i<limit;i++);
{
printf("Enter number %d: ",i+1);
fflush(stdout);
fgets(inputstr,49,stdin);
numbers[i]=atof(inputstr);
}
while(1)
{
flag=0;
for(i=0;i<limit-1;i++)
{
if(numbers[i]>numbers[i+1])
{
temp=numbers[i];
numbers[i]=numbers[i+1];
numbers[i+1]=temp;
flag=1; //flag=1 if the atleast one number get swapped in the array
}
}
if(flag==0){break;} //no swapping anymone, i.e. the numbers are in order already
} //outer while loop end
printf("\nIn Ascending order the numbers are:\n");
for(i=0;i<limit;i++)
{
printf(" %.2lf\n",numbers[i]);
}
return 0;
}
The output is like:
With how many numbers do you love to work with? 3
Enter no 4: 23.4
In ascending order the numbers are:
0.00
1.#R
1.#R
I changed the for for loop into while loop in line 11:
while(i<limit)
{
printf("Enter number %d: ",i+1);
fflush(stdout);
fgets(inputstr,49,stdin);
numbers[i]=atof(inputstr);
i++;
}
Now it's working perfect. But how it is possible? And also how come the program was asking me for "Enter no 4" where i just set the limit to 3 numbers!!?:-/
Thanks in advance