C-Programming Problem. Can someone help me create a C-program that insertion sor
ID: 3573195 • Letter: C
Question
C-Programming Problem. Can someone help me create a C-program that insertion sorts a 10 number array (inputted into the program from a file)? As the insertion sort works, have it print out every step of the sort.
Ex. Array {-5.2, 3.7, -11.9, 4.4, 3.7, 20.2, -6.0, 8.2, 14.6, -20.6}
Prints:
-5.2
3.7 -5.2
3.7 -5.2 -11.9
4.4 3.7 -5.2 -11.9
4.4 3.7 3.7 -5.2 -11.9
20.2 4.4 3.7 3.7 -5.2 -11.9
20.2 4.4 3.7 3.7 -5.2 -6.0 -11.9
20.2 8.2 4.4 3.7 3.7 -5.2 -6.0 -11.9
20.2 14.6 8.2 4.4 3.7 3.7 -5.2 -6.0 -11.9
20.2 14.6 8.2 4.4 3.7 3.7 -5.2 -6.0 -11.9 -20.6
Explanation / Answer
#include<stdio.h>
#include<conio.h>
void main()
{
// float a[]={-5.2,3.7,-11.9,4.4,3.7,20.2,-6.0,8.2,14.6,-20.6},t;
float a[10],t;
int i,j;
//create a text file some.txt and type these number or otherwise take direct input .
FILE *myfile;
myfile=fopen("some.txt","r");
for (i = 0; i < 10; i++)
{
fscanf(myfile, "%f", &a[i]);
}
for(i=0;i<10;i++)
{
for(j=0;j<=i;j++)
{
if(a[j]<a[i])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
printf("%0.1f ",a[j]);
}
printf(" ");
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.