Need help with this C program I have it almsot all completed
Need help with this C program. I have it almsot all completed. Option 1 asks the user to enter a fraction and then option 2 displays that fraction. option 3 sorts the fractions in ascending order, i figured out all the code for this however for option 4 i need to Calculate the Max, Min and Median fractions. I can\'t figure out what the code for it is. If the user selects option 4 before sorting the fractions, then a \"Please sort the values first\" should pop up.
#include <stdio.h>
#include <stdlib.h>
//Struct to hold fraction data
typedef struct fraction
{
int numerator, denom;
}fraction;
double Calc_Frac(fraction b)
{
return((double)b.numerator / b.denom);
}
int main()
{ 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\ \");
printf(\"Press 4 to find min max median fraction\ \");
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) {
int min;
fraction tmp;
for (int k = 0; k < i; k++)
{
min = k;
for (j = k + 1; j < i; j++)
{
if (Calc_Frac(arrFraction[j]) < Calc_Frac(arrFraction[min]))
{
min = j;
}
}
tmp = arrFraction[k];
arrFraction[k] = arrFraction[min];
arrFraction[min] = tmp;
}
}
if (choice == 4)
{
}
}
system(\"pause\");
return(0);
}
Solution
if (choice == 4)
{
if(chosen3==1)
{
printf(\"Min fraction: %d/%d\",arrFraction[0].numerator,arrFraction[0].denom);
printf(\"Max fraction: %d/%d\",arrFraction[i-1].numerator,arrFraction[i-1].denom);
printf(\"Median fraction: %d/%d\",arrFraction[i/2].numerator,arrFraction[i/2].denom);
}
else
{
printf(\"Please sort the values first\");
}
If array is already sorted,then min will be at index 0
max would be at last index
median will be at middle

