sorting of fractions in C language I need it so if the user
sorting of fractions in C language. I need it so if the user selects option 3 it will sort the fractions. Then when i go back to option 2 to view the fractions it will show them sorted.
This is my code:
#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
#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)
{
/* find greatest common divisor of a and b */
int gcd(int a, int b)
{
if (b == 0) return a;
return gcd(b, a%b);
}
int res = 1;
/* find least common divisor of all denominator*/
for (j = 0; j < i; j++)
{
res = res*arrFraction[j].denom/gcd(res, arrFraction[j].denom);
}
/* For loop to multiplication numerator and denominator*/
for (j = 0; j < i; j++)
{
int mul;
mul=res/arrFraction[j].denom;
arrFraction[j].numerator=arrFraction[j].numerator*mul;
arrFraction[j].denom=arrFraction[j].denom*mul;
}
int k;
int temp1;
int temp2;
/* sort all numerator */
for (j = 0; j <i ; ++j)
{
for (k = j + 1; k < i; ++k)
{
if (arrFraction[j].numerator > arrFraction[k].numerator)
{
temp1 = arrFraction[j].numerator;
temp2 = arrFraction[j].denom;
arrFraction[j].numerator = arrFraction[k].numerator;
arrFraction[j].denom= arrFraction[k].denom;
arrFraction[k].numerator= temp1;
arrFraction[k].denom=temp2;
}
}
}
/* using lcd find original numerator or denom */
for (j = 0; j <i ; ++j)
{
int lcd=gcd(arrFraction[j].numerator, arrFraction[j].denom);
arrFraction[j].numerator=arrFraction[j].numerator/lcd;
arrFraction[j].denom=arrFraction[j].denom/lcd;
}
}
}
system(\"pause\");
return(0);
} // end of main
========================================================================
output is:-
Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
1
Enter your fraction, numerator followed by denominator
8 3
Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
1
Enter your fraction, numerator followed by denominator
3 2
Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
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
3
Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
2
-------------------------
0 5/6
1 1/2
2 2/3
-------------------------
----------------------------------------------------------------------------------------------------------------------------
If you have any query, please feel free to ask
Thanks a lot



