Need help with C program Need to use buble sort to sort all
Need help with C program. Need to use buble sort to sort all my friends in ascending order for option 3. please help.
#include <stdio.h>
#include <stdlib.h>
//Struct to hold fraction data
typedef struct fraction
{
int numerator, denom;
}fraction;
int main()
{
//Array of 100 fractions
fraction arrFraction[100];
int i = 0;
int j;
int num = 1;
while (num == 1)
{
int choice;
printf(\"\ Press 1 to enter a fraction\ \");
printf(\"Press 2 to view stored fractions\ \");
printf(\"Press 3 to sort fractions\ \");
scanf(\"%d\", &choice);
if(choice == 1)
{
//Prompting user
printf(\"\ Enter your fraction, numerator followed by denominator\ \");
//Reading values from user
scanf(\"%d %d\", &arrFraction[i].numerator, &arrFraction[i].denom);
//Incrementing counter
i++;
}
if (choice == 2) {
printf(\"-------------------------\ \");
for (j = 0; j < i; j++)
{
printf(\"%d %d/%d \ \", arrFraction[j].numerator/arrFraction[j].denom, arrFraction[j].numerator%arrFraction[j].denom, arrFraction[j].denom);
}
printf(\"\ -------------------------\ \ \");
}
if (choice == 3)
{
}
}
system(\"pause\");
return(0);
}
Solution
// C code to take input and sort fraction
#include <stdio.h>
#include <stdlib.h>
//Struct to hold fraction data
typedef struct fraction
{
int numerator, denom;
}fraction;
int main()
{
//Array of 100 fractions
fraction arrFraction[100];
int i = 0;
int j;
int k;
int num = 1;
while (num == 1)
{
int choice;
printf(\"\ Press 1 to enter a fraction\ \");
printf(\"Press 2 to view stored fractions\ \");
printf(\"Press 3 to sort fractions\ \");
printf(\"Press 4 to Quit\ \");
scanf(\"%d\", &choice);
if(choice == 1)
{
//Prompting user
printf(\"\ Enter your fraction, numerator followed by denominator\ \");
//Reading values from user
scanf(\"%d %d\", &arrFraction[i].numerator, &arrFraction[i].denom);
//Incrementing counter
i++;
}
else if (choice == 2) {
printf(\"-------------------------\ \");
for (j = 0; j < i; j++)
{
printf(\"%f %d/%d \ \", (float)arrFraction[j].numerator/arrFraction[j].denom, arrFraction[j].numerator%arrFraction[j].denom, arrFraction[j].denom);
}
printf(\"\ -------------------------\ \ \");
}
else if (choice == 3)
{
for (j = 0; j < i; j++)
{
for (k = 0; k < i; k++)
{
float fraction1 = (float)arrFraction[j].numerator/arrFraction[j].denom;
float fraction2 = (float)arrFraction[k].numerator/arrFraction[k].denom;
if(fraction2 > fraction1)
{
int temp1 = arrFraction[j].numerator;
arrFraction[j].numerator = arrFraction[k].numerator;
arrFraction[k].numerator = temp1;
int temp2 = arrFraction[j].denom;
arrFraction[j].denom = arrFraction[k].denom;
arrFraction[k].denom = temp2;
}
}
}
printf(\"Fraction Sorted\ \");
}
else if (choice == 4)
break;
else
printf(\"Invalid Input\ \");
}
return(0);
}
/*
output:
Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
Press 4 to Quit
1
Enter your fraction, numerator followed by denominator
5
6
Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
Press 4 to Quit
1
Enter your fraction, numerator followed by denominator
1
2
Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
Press 4 to Quit
1
Enter your fraction, numerator followed by denominator
3
4
Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
Press 4 to Quit
1
Enter your fraction, numerator followed by denominator
9
10
Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
Press 4 to Quit
2
-------------------------
0.833333 5/6
0.500000 1/2
0.750000 3/4
0.900000 9/10
-------------------------
Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
Press 4 to Quit
3
Fractions Sorted
Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
Press 4 to Quit
2
-------------------------
0.500000 1/2
0.750000 3/4
0.833333 5/6
0.900000 9/10
-------------------------
Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
Press 4 to Quit
4
*/



