-3

I have a very simple program. I need to write a program that reads two integers (wins, losses). Call the two functions by passing the two numbers. Display the winning and losing percents. I wrote a code, but it didn't calculate and display the percentage. Need some help.

#include<stdio.h>

double Win (double winns);
double Loss (double losses);

int main() {
    int num1, num2, total;
    double winns, losses, sum1, sum2;
    printf("Enter the number of wins: ");
    scanf("%d", &num1);
    printf("Enter the number of losses: ");
    scanf("%d", &num2);

    total = num1 + num2;
    winns = (num1/total)/100;
    losses = (num2/total)/100;
    sum1 = Win(winns);
    sum2 = Loss(losses);

    printf("The team's winning percent for the years was: %.2f\n", sum1);
    printf("The team's losig percent for the years was: %.2f\n", sum2);

    return 0;
}

double Win (double winns){
   return (winns);
}

double Loss (double losses){
   return (losses);
}
user2884834
  • 129
  • 4
  • 9
  • 14
  • What exactly is the point of having a function with one parameter that just returns the argument passed to it? – Shashank Oct 31 '13 at 23:51
  • 1
    You're doing integer arithmetic, which is going to round all fractions down to 0. You need to cast the numbers to float before dividing. Also, you convert a fraction to percent by _multiplying_ by 100, not dividing. – Barmar Oct 31 '13 at 23:51
  • 1
    What did you input? What did it output? – Paul Oct 31 '13 at 23:51
  • possible duplicate of [Convert int to float](http://stackoverflow.com/questions/13530209/convert-int-to-float) – Matt Oct 31 '13 at 23:52
  • Also your formula for percentage is wrong...It should be (var/ total)*100 – Shashank Oct 31 '13 at 23:52
  • 1
    Is there a definitive resource we should link to for problems of `float=int/int`? – Matt Oct 31 '13 at 23:54

2 Answers2

4
  1. When you divide integer by integer, you will get integer. In your case it will be 0, because num1 is less than total. You need to cast one of them to double.
  2. You need to multiply by 100, not divide to get percents.
  3. I don't see the reason of creating Win and Loss here - you've already got your answers in winns and losses.
  4. Why 'winns' with double n???
sashkello
  • 17,306
  • 24
  • 81
  • 109
0

Three problems:

First of all, you should be casting num1, num2 and/or total to double. Otherwise you'll be performing integer division and the result will always be zero.

Second problem: you need to multply by 100 to get a percentage, not divide.

Third problem: the %f format specifier expects a float, but you gave it a double. sum1 and sum2 need to be casted to floats.

NapoleonBlownapart
  • 309
  • 1
  • 2
  • 8