Thread: league table

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    99

    league table

    hey everyone. theres a few things i need to find out if you consider helping me.
    1. at the end of my program i want to print the points of the teams in descending order i.e if chelsea have 7 points and eveton have 9 points then i want everton to be top then chelsea underneath etc... so how do i specify that?
    2. is there an easier way of writing that program does anyone know?
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int main()
    {
        int points[4] = {0, 3, 1, 0};
        char a[20];
        char b[20];
        char c[20];
        char d[20];
        int x = 1;
        int i;
        int aa;
        int bb;
        int cc;
        int dd;
        int ptsa=0;
        int ptsb=0;
        int ptsc=0;
        int ptsd=0;
    
    
        printf("enter the teams:\n");
        fgets(a, 20, stdin);
        fgets(b, 20, stdin);
        fgets(c, 20, stdin);
        fgets(d, 20, stdin);
    printf("%s will play %s\n",a,b);
    printf("enter %s score\n",a);
    scanf("%d",&aa);
    printf("enter %s score\n",b);
    scanf("%d",&bb);
    
    
            if(aa>bb)
                {
                    ptsa = ptsa + points[1];
                }
    
    
                        else if(aa<bb)
                            {
                                ptsb = ptsb + points[1];
                            }
                                else if(aa==bb)
                                {
                                    ptsa = ptsa + points[2];
                                    ptsb = ptsb + points[2];
                                }
            printf("%s will play %s\n",a,c);
            printf("enter %s score\n",a);
            scanf("%d",&aa);
            printf("enter %s score\n",c);
            scanf("%d",&cc);
            if(aa>cc)
                {
                    ptsa = ptsa + points[1];
                }
    
    
                        else if(aa<cc)
                            {
                                ptsc = ptsc + points[1];
                            }
                                else if(aa==cc)
                                {
                                    ptsa = ptsa + points[2];
                                    ptsc = ptsc + points[2];
                                }
    
    
                printf("%s will play %s\n",a,d);
                printf("enter %s score\n",a);
                scanf("%d",&aa);
                printf("enter %s score\n",d);
                scanf("%d",&dd);
                 if(aa>dd)
                {
                    ptsa = ptsa + points[1];
                }
    
    
                        else if(aa<dd)
                            {
                                ptsd = ptsd + points[1];
                            }
                                else if(aa==dd)
                                {
                                    ptsa = ptsa + points[2];
                                    ptsd = ptsd + points[2];
                                }
                printf("%s will play %s\n",b,c);
                printf("enter %s score\n",b);
                scanf("%d",&bb);
                printf("enter %s score\n",c);
                scanf("%d",&cc);
                 if(bb>cc)
                {
                    ptsb = ptsb + points[1];
                }
    
    
                        else if(bb<cc)
                            {
                                ptsc = ptsc + points[1];
                            }
                                else if(bb==cc)
                                {
                                    ptsb = ptsb + points[2];
                                    ptsc = ptsc + points[2];
                                }
    
    
                printf("%s will play %s\n",b,d);
                printf("enter %s score\n",b);
                scanf("%d",&bb);
                printf("enter %s score\n",d);
                scanf("%d",&dd);
                 if(bb>dd)
                {
                    ptsb = ptsb + points[1];
                }
    
    
                        else if(bb<dd)
                            {
                                ptsd = ptsd + points[1];
                            }
                                else if(bb==dd)
                                {
                                    ptsb = ptsb + points[2];
                                    ptsd = ptsd + points[2];
                                }
    
    
                printf("%s will play %s\n",c,d);
                printf("enter %s score\n",c);
                scanf("%d",&cc);
                printf("enter %s score\n",d);
                scanf("%d",&dd);
                 if(cc>dd)
                {
                    ptsc = ptsc + points[1];
                }
    
    
                        else if(cc<dd)
                            {
                                ptsc = ptsc + points[1];
                            }
                                else if(cc==dd)
                                {
                                    ptsc = ptsc + points[2];
                                    ptsd = ptsd + points[2];
                                }
    printf("%s is on %d points\n",a,ptsa);
    printf("%s is on %d points\n",b,ptsb);
    printf("%s is on %d points\n",c,ptsc);
    printf("%s is on %d points\n",d,ptsd);
    
    
    
    
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You should use functions.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ingeniousreader View Post
    if chelsea have 7 points and eveton have 9 points then i want everton to be top then chelsea underneath etc... so how do i specify that?
    Relational aka comparison operators: >=, <=, <, >, ==

    C Relational Operators
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    99

    reply to functions answer

    ye but how could i go about using functions?

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    Quote Originally Posted by MK27 View Post
    Relational aka comparison operators: >=, <=, <, >, ==

    C Relational Operators
    so for relational i could go if(a>b && a>c && a>c && a>d ) printf.......?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ingeniousreader View Post
    so for relational i could go if(a>b && a>c && a>c && a>d ) printf.......?
    You can do that, yes.

    but how could i go about using functions?
    Specifically, I can't say because your code lacks comments or meaningful identifiers. However, one of the general ideas behind the use of functions is to reduce code duplication. Eg, it looks (at a glance) like the sections from line 52-71, 74-93, 94-114, 117-136, and 139-158 do something very similar just with different variables. So you should write one function with appropriate parameters, and call it 5 times with the different variables.

    If they don't do exactly the same thing, you can definitely extract some smaller sections that are the same and make those functions. You can use both the return value and pointers in the parameter list to set values back in main, eg:

    Code:
    int somefunc (int a, int b, int *x);  // prototype
    int data1, data2, result1, result2;
    // assign to data1 and data2
    result1 = somefunc(data1, data2, &result2);
    Here the "data" variable pass values into the function, and the "result" variables receive values from it. Since a function can only have one return type, param x is a pointer you can deference and assign to inside somefunc:

    Code:
    *x = 666;
    Last edited by MK27; 02-29-2012 at 01:48 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    Quote Originally Posted by MK27 View Post
    You can do that, yes.



    Specifically, I can't say because your code lacks comments or meaningful identifiers. However, one of the general ideas behind the use of functions is to reduce code duplication. Eg, it looks (at a glance) like the sections from line 52-71, 74-93, 94-114, 117-136, and 139-158 do something very similar just with different variables. So you should write one function with appropriate parameters, and call it 5 times with the different variables.

    If they don't do exactly the same thing, you can definitely extract some smaller sections that are the same and make those functions. You can use both the return value and pointers in the parameter list to set values back in main, eg:

    Code:
    int somefunc (int a, int b, int *x);  // prototype
    int data1, data2, result1, result2;
    // assign to data1 and data2
    result1 = somefunc(data1, data2, &result2);
    Here the "data" variable pass values into the function, and the "result" variables receive values from it. Since a function can only have one return type, param x is a pointer you can deference and assign to inside somefunc:

    Code:
    *x = 666;
    thank you very much for your input mate

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. league table
    By ingeniousreader in forum C Programming
    Replies: 5
    Last Post: 02-04-2012, 07:25 AM
  2. Gaming League Hiring
    By Absolute in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 11-13-2011, 06:11 PM
  3. problem with league table
    By vw1970 in forum C Programming
    Replies: 6
    Last Post: 01-04-2009, 09:16 AM
  4. Making A League Table
    By apple_ranger in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:22 PM