Thread: Homework help

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    18

    Writing and sorting fractions homework help

    Press 6 to enter a Fraction //Prompt the user for numerator and denominator.
    Press 7 to Print fraction values //Print all stored fractions. Fractions should be printed in mixed number format.
    Press 8 to Sort fractions //Sort fractions in ascending order.
    Press 9 to analyze fractions

    Code:
    typedefstruct fraction
    {
    int num, denom;
    
    
    } fraction;
    
    fraction arrfraction[100];
    
    
            arrfraction[0].num = 0;
            arrfraction[0].denom = 0;
    
    if (a == 6){
    
    printf("enter a numerator and a denominator value\n");
    scanf("%d %d", arrfraction[x].num, arrfraction[x].denom);
                x++;}
    
    I have a program written out that was assigned previously and now i am given the tasks as outlined in bold. This is what i started with but im having trouble understanding what to write. If someone could help me and lead me in the right direction that would be awesome.
    Last edited by timka138; 10-28-2016 at 06:42 PM.

  2. #2
    Registered User
    Join Date
    Oct 2016
    Posts
    18
    Code:
    	if (a == 6)
    		{
    
    
    
    
    			printf("enter a numerator and a denominator value\n");
    			scanf("%d %d", &arrFraction[i].num, &arrFraction[i].denom);
    			i++;
    		}
    
    
    		if (a == 7)
    		{
    			printf("-------------------------\n");
    
    
    			for (j = 0; j < i; j++)
    			{
    				printf("%d  %d/%d \n", arrFraction[j].num/arrFraction[j].denom, arrFraction[j].num%arrFraction[j].denom, arrFraction[j].denom);
    
    
    			}
    			printf("\n-------------------------\n\n");
    		}

    I have this now, could someone help me with sorting the fractions that the user inputs into ascending order

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    18
    This is the full code that i actually have so far( I know I should have probably posted this in the first place, sorry about the confusion)

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #define MAXSIZE 100
    
    
    typedef struct fraction
    {
        int num, denom;
    
    
    } fraction;
    
    
    double Calc_Frac(fraction b)
    {
        return((double)b.num / b.denom);
    }
    
    
    int main()
    {
        fraction arrFraction[100];
    
    
        int i = 0;
        int a = 0;
        int j;
        
    
    
        while (1) {
    
    
    
    
            char name[500][100];
    
    
    
    
            printf("Press 1 to view author info\nPress 2 to enter a name\nPress 3 to view names\nPress 4 to alphabetize names\nPress 5 to clear names\nPress 6 to enter a fraction\nPress 7 to print fraction values\nPress 8 to sort fractions\nPress 9 to analyze fractoins\n");
    
    
            scanf("%d", &a);
            char arr[100];
    
    
    
            if (a == 1)
            {
                printf("----------------\n");
                printf("Tim Kononets\n2651763\n");
                printf("----------------\n");
            }
    
    
            if (a == 2)
            {
    
    
                printf("----------------\n");
                printf("enter a name(or word without any spaces)\n");
                scanf("%s", arr);
                strcpy(name[i], arr);
                i++;
                printf("----------------\n");
    
    
            }
    
    
            if (a == 3)
            {
                printf("\n\n-----------------------------\n");
    
    
    
    
                for (int j = 0; j < i; j++)
                {
                    printf("\n%s\n", name[j]);
                }
                printf("\n\n-----------------------------\n");
    
    
            }
            if (a == 4)
            {
                int j, k;
    
    
                for (j = 0; j < i; j++)
                {
                    for (k = 0; k < i - j - 1; k++)
                    {
                        if (strcmp(name[k], name[k + 1]) > 0)
                        {
                            strcpy(arr, name[k]);
                            strcpy(name[k], name[k + 1]);
                            strcpy(name[k + 1], arr);
                        }
                    }
                }
            }
    
    
    
    
            if (a == 5)
            {
                i = 0;
                a = 0;
    
    
            }
    
    
            if (a == 6)
            {
    
    
    
    
                printf("enter a numerator and a denominator value\n");
                scanf("%d %d", &arrFraction[i].num, &arrFraction[i].denom);
                i++;
            }
    
    
            if (a == 7)
            {
                printf("-------------------------\n");
    
    
                for (j = 0; j < i; j++)
                {
                    printf("%d  %d/%d \n", arrFraction[j].num/arrFraction[j].denom, arrFraction[j].num%arrFraction[j].denom, arrFraction[j].denom);
    
    
                }
                printf("\n-------------------------\n\n");
            }
    
    
            if (a == 8)
            {
                int min;
                fraction tmp;
                
    
    
                for (i = 0; i < j; i++)
                {
                    min = i;
                    for (j = i + 1; j < i ; j++)
                    {
                        if (Calc_Frac(arrFraction[j]) < Calc_Frac(arrFraction[min]))
                        {
                            min = j;
                        }
                    }
                    tmp = arrFraction[i];
                    arrFraction[i] = arrFraction[min];
                    arrFraction[min] = tmp;
                }
                i++;
            }
    
    
        }
    
    
    
    
        system("pause");
        return 0;
    }
    So my problem is with the sorting (a==8) right now, I know that the way the loop is set up, it can only sort up to 2 fractions, so if you could help me with what to put in in order for it to sort every number that is stored into the program that would be great.

  4. #4
    Registered User
    Join Date
    Oct 2016
    Posts
    10

    Cool

    You misused i in your loop, which should not used there because it is the number of fractions you have. So number 8 should start like
    Code:
    if (a == 8)
            {
                int min, k;
                fraction tmp;
    
                for (j = 0; j < i; j++)
                {
                    min = j;
                    for (k = j + 1; k < i; k++)
    further you already worked it out!
    Last edited by GiForce; 10-29-2016 at 07:24 AM. Reason: linguistic

  5. #5
    Registered User
    Join Date
    Oct 2016
    Posts
    18
    @GiForce
    Awesome, thanks for the help it worked.
    Last edited by timka138; 10-29-2016 at 10:27 AM.

  6. #6
    Registered User
    Join Date
    Oct 2016
    Posts
    18
    Im having a problem with finding the maximum and minimun fraction, mean of the fractions. i started it but since i have the array as a fraction it keeps on telling me i cant compare it with an integer
    Code:
    if (a == 9)
            { 
                fraction maximum = arrFraction[0]; 
                fraction min = arrFraction[0]; 
                for (int c = 1; c < i; c++) 
                { 
                    if (b(arrFraction[c]) > i); 
                    { 
                        maximum = arrFraction[c]; 
                    } 
                    
                } 
                printf("Max = %d", maximum);

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    What does the b() function do?

  8. #8
    Registered User
    Join Date
    Oct 2016
    Posts
    18
    @whiteflags
    It defines the fraction. if you look in the top of the program in the previous post it shows it. I saw this from a previous code in my class and used it. I think its like a shortening for the fraction.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I'm still not really seeing it. Did you mean Calc_Frac()? It has a parameter named b, but that is not the same as calling a function named b(). In short, that particular line has multiple problems, so it doesn't surprise me that the error is unhelpful. If you didn't know what you were doing, it would take a while to get at what the error means.

    Anyway, it makes sense for these things to be compared with the previous minimum and maximum. Maybe something like this.

    Code:
    double minimum, maximum, this_f;
    minimum = maximum = Calc_Frac(arrFraction[0]);
    // i is the number of elements of arrFraction; consider renaming i
    for (int c = 1; c < i; ++c) {
       this_f = Calc_Frac(arrFraction[c]);
       if (this_f < minimum)
          minimum = this_f;
       
       if (this_f > maximum)
          maximum = this_f;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 09:44 PM
  2. This is my Homework
    By abh!shek in forum C++ Programming
    Replies: 20
    Last Post: 04-06-2008, 02:23 PM
  3. Need Help with Homework.
    By skaldicpoet9 in forum C Programming
    Replies: 8
    Last Post: 02-08-2008, 02:28 AM
  4. Homework Help
    By alecmcraig in forum C Programming
    Replies: 4
    Last Post: 11-17-2007, 01:03 AM
  5. Homework =)
    By Raeliean in forum C++ Programming
    Replies: 9
    Last Post: 07-16-2005, 10:27 PM

Tags for this Thread