Please correct the following code to output correctly, currenty it is not sortin
ID: 3730745 • Letter: P
Question
Please correct the following code to output correctly, currenty it is not sorting them. and please explain what is happening in the code, line by line. thanks.
struct g{
int id;
float hi;
};
int main() {
int i,done = 0,t,c;
float tf;
struct g info[]={{289,142.24},{66,115.54},{562,123.59},{194,179.73},
{276,157.52},{642,147.51},{31,122.64},{929,174.53},{862,127.73},{397,124.44},
{455,137.04},{56,170.17},{193,124.24},{339,160.68},{597,188.63},{268,162.64},
{251,166.65},{28,124.43},{518,138.58},{106,132.05},{983,197.38},{948,194.02},
{910,118.50},{635,120.05},{530,129.79},{689,115.83},{270,157.64},{857,183.94},
{627,147.94},{917,181.02},{850,197.91},{916,190.18},{983,196.56},{764,121.50},
{462,169.92},{611,154.07},{406,169.01},{493192.55},{892,128.60},{268,196.75},
{242,116.99},{348,165.64},{676,166.91},{435,141.23},{39,126.32},{625,155.54},
{56,103.87},{291,192.97},{653,179.97},{574,142.45},{749,125.02},{989,177.35},
{874,136.47},{659,143.52},{624,197.39},{404,166.26},{349,159.35},{247,155.03},
{614,150.20},{328,107.30},{516,136.05},{816,148.12},{244,197.48},{851,132.60},
{580,169.62},{58,167.39},{462,186.67},{338,138.64},{904,159.95},{707,115.28},
{958,135.38},{146,176.94},{55,180.92},{987,102.30},{934,118.16},{446,107.24},
{612,157.84},{990,122.03},{89,100.21},{618,137.80},{564,164.48},{839,125.23},
{959,115.15},{791,100.95},{498,168.75},{935,112.54},{195,167.21},{199,128.10},
{182,167.57},{161,117.41},{527,135.40},{50,103.62},{977,165.53},{123,132.88},
{901,136.22},{909,135.15},{534,100.27},{716,122.89},{247,173.79},{438,160.22}};
for (i = 0; i<100;i++)
printf("%d %f ", info[i].id, info[i].hi);
printf("___________________ ");
printf("If you want to sort Id enter 1 ");
printf("If you want to sort Hi enter 2 ");
scanf("%d",&c);
if (c==1)
{
while(done==0)
{
done=1;
for(i=0;i<100;i++)
if(info[i].id>info[i+1].id)
{
t=info[i].id;
info[i].id=info[i+1].id ;
info[i+1].id=t;
done=0;
}
}
}
}
Explanation / Answer
#include <stdio.h>
struct g{
int id;
float hi;
};
// A function to implement bubble sort
void bubbleSortId(struct g arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j].id > arr[j+1].id){
int tId = arr[j].id;
arr[j].id = arr[j+1].id;
arr[j+1].id = tId;
float tHi = arr[j].hi;
arr[j].hi = arr[j+1].hi;
arr[j+1].hi = tHi;
}
}
// A function to implement bubble sort
void bubbleSortHi(struct g arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j].hi > arr[j+1].hi){
int tId = arr[j].id;
arr[j].id = arr[j+1].id;
arr[j+1].id = tId;
float tHi = arr[j].hi;
arr[j].hi = arr[j+1].hi;
arr[j+1].hi = tHi;
}
}
int main() {
int i,done = 0,t,c;
float tf;
struct g info[]={{289,142.24},{66,115.54},{562,123.59},{194,179.73},
{276,157.52},{642,147.51},{31,122.64},{929,174.53},{862,127.73},{397,124.44},
{455,137.04},{56,170.17},{193,124.24},{339,160.68},{597,188.63},{268,162.64},
{251,166.65},{28,124.43},{518,138.58},{106,132.05},{983,197.38},{948,194.02},
{910,118.50},{635,120.05},{530,129.79},{689,115.83},{270,157.64},{857,183.94},
{627,147.94},{917,181.02},{850,197.91},{916,190.18},{983,196.56},{764,121.50},
{462,169.92},{611,154.07},{406,169.01},{493,192.55},{892,128.60},{268,196.75},
{242,116.99},{348,165.64},{676,166.91},{435,141.23},{39,126.32},{625,155.54},
{56,103.87},{291,192.97},{653,179.97},{574,142.45},{749,125.02},{989,177.35},
{874,136.47},{659,143.52},{624,197.39},{404,166.26},{349,159.35},{247,155.03},
{614,150.20},{328,107.30},{516,136.05},{816,148.12},{244,197.48},{851,132.60},
{580,169.62},{58,167.39},{462,186.67},{338,138.64},{904,159.95},{707,115.28},
{958,135.38},{146,176.94},{55,180.92},{987,102.30},{934,118.16},{446,107.24},
{612,157.84},{990,122.03},{89,100.21},{618,137.80},{564,164.48},{839,125.23},
{959,115.15},{791,100.95},{498,168.75},{935,112.54},{195,167.21},{199,128.10},
{182,167.57},{161,117.41},{527,135.40},{50,103.62},{977,165.53},{123,132.88},
{901,136.22},{909,135.15},{534,100.27},{716,122.89},{247,173.79},{438,160.22}};
for (i = 0; i<100;i++)
printf("%d %f ", info[i].id, info[i].hi);
printf("___________________ ");
printf("If you want to sort Id enter 1 ");
printf("If you want to sort Hi enter 2 ");
scanf("%d",&c);
if (c==1)
{
bubbleSortId(info, 100);
}else{
bubbleSortHi(info, 100);
}
printf(" ");
for (i = 0; i<100;i++)
printf("%d %f ", info[i].id, info[i].hi);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.