#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    I did every thing by the book


    Contributions wont calculate :confused: :confused: :confused:
    Code:
    #include <stdio.h> 
    #include <conio.h> 
    int main() 
    { float tax;
     float salary;
     float taxable;
     float taxmo;
     float contributions;
     float pension;
     float ni;
     float pensionmo;
     float nimo;
     printf("Please Enter Gross Salary\n");
     scanf("%f", &salary);
     contributions = 6/100*salary + 2/100*salary;
     taxable = salary - contributions;
     if (taxable <= 4000) { tax = 0 ; } else { tax = 0;
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,155
    Rep Power
    2222
    Whatever do you mean by "Contributions wont calculate"? That statement absolutely meaningless, because I cannot read your mind despite your delusional assumption that we all can read your mind. Why do you believe so strongly in such a nonsensical idea?

    Rather, you need to tell us what you're talking about! That is, after all, what language is for and what communication is supposed to enable. You wouldn't go to see your physician about a medical problem and insist that he guess what all your symptoms are, would you? So why insist on playing that stupid game with us?

    When your program does something that you don't think is what it should do, then there are a few things you need to tell us:
    1. What did your program do? IOW, what are the symptoms? Be specific enough, please; "it don't work, waaah!" simply will not work.

    2. If the problem is that it produces the wrong output, then you need to tell us what output you were expecting to get. You also need to tell us what the input was that produced this problem; without knowing what input you had fed the program, it can be impossible for us to figure out how the program had tried to process that input.

    3. Provide us with a program that will produce the problem and that we can compile. Being able to run your erroneous code and to test its processing of different input values can help us immensely to help you. Remember: you need to help us to help you.

    The code you posted will not compile, so we cannot do anything with it. It doesn't even try to output the value of contributions, so we have no idea where you ever got the idea that there's something wrong with how it's being calculated.

    Also, while it's nice that you have finally begun to use code tags, that effort is useless if you neglect to indent your code! The whole idea of code tags is to make code readable by preserving its indentation which in turn preserves its formatting. Refusing to indent code inside of code tags can very easily be seen as a direct insult akin to making an obscene gesture at us for trying to help you.


    Considering contributions = 6/100*salary + 2/100*salary;, I would suspect that your immediate problem is that you are performing integer division instead of floating-point division. You perform integer division when both the dividend and the divisor are integers; if either one is a floating-point datatype, then floating-point division is performed. The fact that you then convert the result to float in order to multiply it to salary does not keep the division from being integer -- I would suspect, since I have no practical experience to base that in because I would never do anything like that.

    In case you had slept through your elementary-school arithmetic class, in integer division you get a quotient and a remainder, therefore 6/100 would give you a quotient of 0 and a remainder of 6 -- to get the remainder, you would need to use the modulo operator, % . Floating-point division is what you get when you use an electronic calculator, such that 6.0/100.0 = 0.06.

    As an experiment, besides using float literals there instead of integer literals, try changing the order within the terms (evaluation is left-to-right while observing the order of precedence of the operators -- * and / are of the same order of precedence); eg:
    contributions = salary*6/100 + salary*2/100;
    That way, since salary is float, salary*6 will also be float, which will cause a floating-point division to occur. But it's still better to use a float or double literal so that you make it very clear what you are doing.

    PS
    If the book told you to use integer division there, then you need to find yourself a different book.
    Last edited by dwise1_aol; August 6th, 2012 at 09:05 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by dwise1_aol
    Whatever do you mean by "Contributions wont calculate"? That statement absolutely meaningless, because I cannot read your mind despite your delusional assumption that we all can read your mind. Why do you believe so strongly in such a nonsensical idea?

    Rather, you need to tell us what you're talking about! That is, after all, what language is for and what communication is supposed to enable. You wouldn't go to see your physician about a medical problem and insist that he guess what all your symptoms are, would you? So why insist on playing that stupid game with us?

    When your program does something that you don't think is what it should do, then there are a few things you need to tell us:
    1. What did your program do? IOW, what are the symptoms? Be specific enough, please; "it don't work, waaah!" simply will not work.

    2. If the problem is that it produces the wrong output, then you need to tell us what output you were expecting to get. You also need to tell us what the input was that produced this problem; without knowing what input you had fed the program, it can be impossible for us to figure out how the program had tried to process that input.

    3. Provide us with a program that will produce the problem and that we can compile. Being able to run your erroneous code and to test its processing of different input values can help us immensely to help you. Remember: you need to help us to help you.

    The code you posted will not compile, so we cannot do anything with it. It doesn't even try to output the value of contributions, so we have no idea where you ever got the idea that there's something wrong with how it's being calculated.

    Also, while it's nice that you have finally begun to use code tags, that effort is useless if you neglect to indent your code! The whole idea of code tags is to make code readable by preserving its indentation which in turn preserves its formatting. Refusing to indent code inside of code tags can very easily be seen as a direct insult akin to making an obscene gesture at us for trying to help you.


    Considering contributions = 6/100*salary + 2/100*salary;, I would suspect that your immediate problem is that you are performing integer division instead of floating-point division. You perform integer division when both the dividend and the divisor are integers; if either one is a floating-point datatype, then floating-point division is performed. The fact that you then convert the result to float in order to multiply it to salary does not keep the division from being integer -- I would suspect, since I have no practical experience to base that in because I would never do anything like that.

    In case you had slept through your elementary-school arithmetic class, in integer division you get a quotient and a remainder, therefore 6/100 would give you a quotient of 0 and a remainder of 6 -- to get the remainder, you would need to use the modulo operator, % . Floating-point division is what you get when you use an electronic calculator, such that 6.0/100.0 = 0.06.

    As an experiment, besides using float literals there instead of integer literals, try changing the order within the terms (evaluation is left-to-right while observing the order of precedence of the operators -- * and / are of the same order of precedence); eg:
    contributions = salary*6/100 + salary*2/100;
    That way, since salary is float, salary*6 will also be float, which will cause a floating-point division to occur. But it's still better to use a float or double literal so that you make it very clear what you are doing.

    PS
    If the book told you to use integer division there, then you need to find yourself a different book.
    Am sorry about the way i created that thread, won't happen again. Am also sorry about the way i posted my codes and all, my tutor a uni did not really teach us anything, we had to learn everything on our own. i think i should post the full code so that you would understand what am trying to do.

    Code:
    int main() 
    { 
     float tax; 
     float salary; 
     float taxable;
     float taxmo;
     float contributions;
     float pension = 6/100;
     float ni = 2/100;
     float pensionmo;
     float nimo;
    
    
     printf("Please Enter Gross Salary\n");
     scanf("%d",&salary);
    
     contributions = pension*salary + ni*salary;
     taxable = salary - contributions; 
    
     if (taxable <= 4000) 
    { tax = 0 ; } else { tax = 0; taxable = taxable - 4000 ;
     }
     if (taxable <= 15000) 
    { tax = taxable*17/100;
     } else 
    { tax = tax + (11000*17/100);
     taxable = taxable - 10999;
     } if (taxable <=30000) 
    { tax = tax + taxable*25/100;
     } else
     { tax = tax + (14999*25/100);
     taxable = taxable - 14999 ;
     } if (taxable >= 30001 )
     { tax = tax + taxable*40/100 ;
     } taxmo = tax/12;
     pension = 6/100* salary;
     ni = 2/100* salary;
     pensionmo = pension/12;
     nimo = ni/12; 
    
     printf("\nTax due\t\t%7.2f\t\t%7.2f \n",taxmo,tax);
     printf("\nPension\t\t%7.2f\t\t%7.2f \n",pensionmo,pension);
    
     getch(); 
    
    }

    i think due to the error i made, that is my i can get results for (pensionmo and nimo) which are the contributions i was talking about. the code is to compute yearly tax to be paid by an individual when pension and NI contributions are deducted.

    i think the question would help to :
    We want to compute the yearly and monthly rate of taxes to be paid by a person earning a given gross yearly salary, knowing the rates for NI contributions, pension contributions, and income tax. We shall use imaginary figures, not real rates. The salary is input from the keyboard. The NI contribution is calculated as 6% of the gross salary. The pension contribution is calculated as 2% of the gross salary. The income tax is computed progressively after detracting NI and pension contributions. The taxable amount (after detractions) is divided into bands, each taxed at a different rate as follows: Tax bands: Tax rate: 0 - 4000 0% on first 4000 4001 15000 17% on next 11000 15001 30000 25% on next 15000 30001 and above 40% on any remainder
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0
    Here's a solution to this problem if it helps:-

    PHP Code:
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
        
    float salary,pension,ni,contributions,taxable,band1,band2,band3,band4,tax;

        
    band1 band2 band3 band4 0;

        
    printf("Enter the Gross Salary\t");
        
    scanf("%f",&salary);

        
    pension salary 100;        // OR pension = 2.0 / 100 * salary;
        
    ni salary 100;            // OR pension = 6.0 / 100 * salary;
        
    contributions pension ni;
        
    taxable salary contributions;

        if (
    taxable 30000)
        {
            
    band1 4000;
            
    band2 11000;
            
    band3 15000;
            
    band4 taxable 30000;
        }
        else if (
    taxable 15000 && taxable <=30000)
        {
            
    band1 4000;
            
    band2 11000;
            
    band3 taxable 15000;
        }
        else if (
    taxable 4000 && taxable <= 15000)
        {
            
    band1 4000;
            
    band2 taxable 4000;
        }
        else
            
    band1 taxable;
        
        
    tax = (band1 100) + (band2 17 100) + (band3 25 100) + (band4 40 100);
        
    printf("Tax due = %.2f",tax);

        
    getch();
        return 
    0;

    Comments on this post

    • ptr2void disagrees : Don
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,155
    Rep Power
    2222
    OK, think about what I told you to do:
    Originally Posted by dwise1_aol
    2. If the problem is that it produces the wrong output, then you need to tell us what output you were expecting to get. You also need to tell us what the input was that produced this problem; without knowing what input you had fed the program, it can be impossible for us to figure out how the program had tried to process that input.
    Do you understand that? What value did you input? What values did you get out?

    Remember: we need to know the symptoms! And we get very grumpy when we're force to play guessing games.


    And did you not understand the issue with using integer division? Did you not understand it when I told you " it's still better to use a float or double literal "? Because by using integer literals in the following code, you are still using integer division:
    Code:
        float pension = 6/100;
        float ni = 2/100;
    First your code performs the integer division, then it converts the quotient (which is zero) to float and assigns that to the variable.

    Do you really want to initialize pension and ni to zero? If not, then why do you insist on using integer division? Shouldn't you be using floating-point division instead? You'll need to use float or double literals for that, not integer literals.

    In case you don't understand what a literal is, click on that link; from that article:
    In computer science, a literal is a notation for representing a fixed value in source code. Almost all programming languages have notations for atomic values such as integers, floating-point numbers, strings, and booleans; ...
    Here are some examples in C:
    integer literal -- 6
    double literal -- 6.0
    float literal -- 6.0f
    char literal -- 'a'
    string literal -- "abcd"

    Capisci?


    Here's a debugging suggestion. In order to double-check what a variable is set to, use a printf to output its value. For example, make the very first line after the declarations this:
    printf("pension = %f, in = %f \n", pension, in);
    You expect them to be 0.06 and 0.02, respectively, but I think that you're actually setting them both to zero. Add that printf to your code and you will see for sure.

    That is a common debugging practice while developing your code. Learn to use it. Just make sure to remove them when you're done.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    I take it you ignored the experiment I asked you to perform in your other thread.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo