Help me with this please!!!

This is a discussion on Help me with this please!!! within the C Programming forums, part of the General Programming Boards category; I have this code that will compute a student's average QPI (Qoutient Percentile Intelligence). You see, the code is too ...

  1. #1
    ComSci newbie...ICS kid
    Join Date
    Jul 2006
    Location
    PHILIPPINES!!!
    Posts
    38

    Red face Help me with this please!!!

    I have this code that will compute a student's average QPI (Qoutient Percentile Intelligence). You see, the code is too messy. Can anyone help me to make it clean? I mean, help me to make a function that will multiply the grade and the units and divide the product to the total number of units... something like that. Thanks!!! I will gladly appreciate your help and if you have any links that you want me to go, please do tell.

    I really need a proffesional's opinion. Hope you'll listen to me.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    #define MAXNAME 100
    int main(void){
        char c, name[MAXNAME];
        int i, sub;
        float unit, grde, sum, prod, qou, tot;
        while((c=getchar()) != 0){
                 grde=sum=prod=qou=tot=0;
                 printf("\n\nACADEMIC HONORS COMPUTATION\n");
                 printf("Complete name of student: \n");
                 printf("\t");
                 for(i=0; (c = getchar())!='\n'; ++i){
                          name[i]=c;
                          }
                          name[i]='\0';
                 printf("Number of subjects: \n");
                 printf("\t");
                 scanf("%d", &sub);
                 for(i=0; i<sub; ++i){
                          printf("Grade of Subject %d: ", i+1);
                          scanf("%f", &grde);
                          printf("Number of units of subject %d: ", i+1);
                          scanf("%f", &unit);
                          prod=grde*unit;
                          sum+=prod;
                          }
                 printf("%s has %d subjects.\n", name, sub);
                 printf("Sum of all is %.2f", sum);
                 printf("\nEnter total number of units: ");
                 scanf("%f", &tot);
                 qou=sum/tot;
                 printf("\nYour grade is %.2f", qou);
                 if(qou>=3.70 && qou<=4)
                        printf("\n%s is qualified for 1st honors\n", name);
                 else if(qou>=3.25 && qou<=3.69)
                        printf("\n%s is qualified for 2nd honors\n", name);
                 else
                        printf("\n%s is not qualified to become an honor\n", name);
                 }
                 getch();
                 return 0;
    }
    Last edited by nesir; 09-28-2006 at 06:00 AM.

    TO ALL THOSE WITH FRIENDSTER, ADD ME!

    i'm ms_anime61@yahoo.com
    a newbie in the field of Computer Science!! Pray for all the student's success!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. I'll teach you how to make a function though. Do you know how?

    Functions have these parts:

    1 - A return value, void if they don't return anything.
    2 - A name.
    3 - Function arguments, void if none.
    4 - A block of code, or body, surrounded by { }.

    Just like the main function.
    Code:
    int main( void )
    {
    
        return 0;
    }
    The main function returns an int, it's name is main, it takes, this time anyway, no arguments, so we use void, and the body of the function is wrapped in { }

    Now you try:

    1 - Make a function to display a message. Something simple like "Hello World!"
    2 - Make a function display something you pass it. Whatever you like.
    3 - Make a function return a value, assign that to some variable.
    4 - Make a function take two arguments, add them, and return the sum.

    Can you do any of those? If not, try the first one, and post your attempt. If you can't do all of them, post the code for the one you get stuck on.

    We're not here to do your work for you. We're here so you can learn. And because we're bored.


    Quzah.
    Last edited by quzah; 09-28-2006 at 06:07 AM.
    Hope is the first step on the road to disappointment.

  3. #3
    ComSci newbie...ICS kid
    Join Date
    Jul 2006
    Location
    PHILIPPINES!!!
    Posts
    38
    Yah... I know how to make a function.. but I'm not quite good. I get confused sometimes.


    So for example, I will make a function that will display a message... Tell me if I'm wrong or if I did something wrong...

    Code:
    void display()
                {
                    printf("Hello World");
                    return();
                  }
    function of two arguments

    Code:
    void mult(int a, int b)
                {
                   int prod;
                   prod = a*b;
                   return();
                }
    And I'm at a complete lost! How am I going to return a value again?
    Last edited by nesir; 09-28-2006 at 07:26 AM.

    TO ALL THOSE WITH FRIENDSTER, ADD ME!

    i'm ms_anime61@yahoo.com
    a newbie in the field of Computer Science!! Pray for all the student's success!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It will multiply the variables, but only inside the function. You have to return whatever it is you want to end up back in the function that called it from. Like so:
    Code:
    int mult( int x, int y )
    {
        int prod;
        prod = x * y;
        return prod;
    }
    See, you've stored the results in 'prod', but you didn't actually return it. Thus, when the function call ends, 'prod' is discarded. So, we return that value. If you need more than one item "returned", you have to fake it with pointers. However, you'll want to get the hang of creating simple functions before taking on pointers. You can simplify the above, but there's no need to work out "tricks" or optimization until you've got the basics covered.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    ComSci newbie...ICS kid
    Join Date
    Jul 2006
    Location
    PHILIPPINES!!!
    Posts
    38
    Now i finally understand!

    Thanks but one thing is still bothering me though...

    When will I call my function? I mean, where in the code that I made will I place that function? Is it inside the 2nd for loop?

    So in other words, it will look like this:

    So after the scanf("");

    Code:
    ...
               scanf("%d", &unit);
               mult(grde, unit);
               ...
    ?

    TO ALL THOSE WITH FRIENDSTER, ADD ME!

    i'm ms_anime61@yahoo.com
    a newbie in the field of Computer Science!! Pray for all the student's success!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Something close to that. Now since you're returning something, you'll probably want to store that some place.
    Code:
    int store_here;
    ...
    store_here = mult( grde, unit );
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    ComSci newbie...ICS kid
    Join Date
    Jul 2006
    Location
    PHILIPPINES!!!
    Posts
    38
    Quote Originally Posted by quzah
    Something close to that. Now since you're returning something, you'll probably want to store that some place.
    Code:
    int store_here;
    ...
    store_here = mult( grde, unit );
    Quzah.
    Huh? Sorry, but I'm completely at a lost... Please elaborate a little.. BTW, thanks for coping with me...

    TO ALL THOSE WITH FRIENDSTER, ADD ME!

    i'm ms_anime61@yahoo.com
    a newbie in the field of Computer Science!! Pray for all the student's success!

  8. #8
    ComSci newbie...ICS kid
    Join Date
    Jul 2006
    Location
    PHILIPPINES!!!
    Posts
    38
    Oh! I also have one question:

    How can I output the qoutient together with its remainder?

    We know that if we place 9/2 the answer is 4 and if we place 9%2 the answer is 1.

    How can I get 4.5 as an answer to 9 divided by 2 in C code?

    TO ALL THOSE WITH FRIENDSTER, ADD ME!

    i'm ms_anime61@yahoo.com
    a newbie in the field of Computer Science!! Pray for all the student's success!

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nesir
    Huh? Sorry, but I'm completely at a lost... Please elaborate a little.. BTW, thanks for coping with me...
    You are returning something. You have to take what you return and put it some place, otherwise it's (simplified) as if you didn't return anything.

    It's like playing fetch with your dog. Throw him a stick, and tell him to go get it. If he brings it back, but you don't take it, you don't end up with the stick, so you can't throw it a gain. He may as well have not bothered fetching it, because you've not bothered to do anything with it.

    Put it in your hand. There. Now the stick is stored some place, so you can throw it again if you want. Same concept.
    Code:
    int r, a, b;
    
    a = 5;
    b = 6;
    
    r = multi( a, b );
    
    printf( "r is %d\n", r );
    We can now print what we've returned, because we restored that value in r. Just like putting 5 into a, we can put what is returned into a variable of the appropriate type.

    As to your second question, you can't return two things, so you would need to make two function calls (assuming you actually wanted to use functions). Or, don't bother with function calls, and just do the math.

    To get the remainder, you use the % operator.
    Code:
    int rem, div, a, b;
    
    a = 9;
    b = 4;
    
    div = a / b;
    rem = a % b;
    
    printf( "a / b is %d, with % remainder\n", div, rem );

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    ComSci newbie...ICS kid
    Join Date
    Jul 2006
    Location
    PHILIPPINES!!!
    Posts
    38
    Now I get it!!! Thanks a LOT!!!! If you put it that way, it's actually very simple. (Why didn't I thought about that?) Thanks! You don't know how much you've helped me.



    Quote Originally Posted by quzah
    To get the remainder, you use the % operator.
    Code:
    int rem, div, a, b;
    
    a = 9;
    b = 4;
    
    div = a / b;
    rem = a % b;
    
    printf( "a / b is %d, with % remainder\n", div, rem );

    Quzah.
    So I can't actually place 2.5, right? Thanks.

    TO ALL THOSE WITH FRIENDSTER, ADD ME!

    i'm ms_anime61@yahoo.com
    a newbie in the field of Computer Science!! Pray for all the student's success!

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nesir
    So I can't actually place 2.5, right? Thanks.
    Not with integers. You'll need float or double for that.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21