samedi 29 octobre 2016

If-statement to not print data unless it was sorted

I have this C program almost 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, option 4 finds the min, median and max of my set of fractions. However. If the user selects option 4 before sorting the fractions, then a "Please sort the values first" should pop up. I know to add the if statement but not sure what the statement would be.

This is my code:

//This is my code:
#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("\nPress 1 to enter a fraction\n");
        printf("Press 2 to view stored fractions\n");
        printf("Press 3 to sort fractions\n");
        printf("Press 4 to find min max median fraction\n");


        scanf("%d", &choice);


        if (choice == 1)
        {
            //Prompting user
            printf("\nEnter your fraction, numerator followed by denominator\n");

            //Reading values from user
            scanf("%d %d", &arrFraction[i].numerator, &arrFraction[i].denom);

            //Incrementing counter
            i++;

        }

        if (choice == 2) {
            printf("-------------------------\n");
            for (j = 0; j < i; j++)

            {
                printf("%d  %d/%d \n", arrFraction[j].numerator / 
arrFraction[j].denom, arrFraction[j].numerator%arrFraction[j].denom, 
arrFraction[j].denom);
            }
            printf("\n-------------------------\n\n");
        }



        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)
        {

 if (//the statement ){
      printf("please sort values first"\n);
    }
      else
            {
                printf("Min fraction is: %d %d/%d\n", arrFraction[0].numerator / arrFraction[0].denom, arrFraction[0].numerator%arrFraction[0].denom, arrFraction[0].denom);
                printf("Median fraction is: %d/%d\n", arrFraction[i / 2].numerator, arrFraction[i / 2].denom);
                printf("Max fraction is: %d %d/%d\n", arrFraction[i - 1].numerator / arrFraction[i - 1].denom, arrFraction[i - 1].numerator%arrFraction[i - 1].denom, arrFraction[i - 1].denom);
            }

        }

    }

 system("pause");
 return(0);
 }

Aucun commentaire:

Enregistrer un commentaire