percentage

B

Bill Cunningham

I need help on this utillity it is supposed to take as its first
argument a number and its second argument a subtraction of that number say
for example 20 and 2. The program should calculate the percentage lost or
gained by the first number using the second number. This program is
reporting to me the opposite of what it should and I have tried everything.
It says this.

20 2

or 2 from 20 leaving 18 is 90% loss.
2 from 20 is a 10% loss. What's wrong with the formula?

#include <stdio.h>
#include <stdlib.h>
#define ex exit(EXIT_FAILURE)

int main (int argc, char *argv[]) {
if (argc!=3) {
puts("beta usage error");
ex;
}
double x,y;
x=strtod(argv[1],NULL);
y=strtod(argv[2],NULL);

double percent(double a,double b) {
double per;
per=(a-b)*100/a;
return per;
}
printf("%.2f",percent(x,y));
return 0;
}
 
D

desas2

        I need help on this utillity it is supposed to take as its first
argument a number and its second argument a subtraction of that number say
for example 20 and 2. The program should calculate the percentage lost or
gained by the first number using the second number. This program is
reporting to me the opposite of what it should and I have tried everything.
It says this.

20 2

or 2 from 20 leaving 18 is 90% loss.
2 from 20 is a 10% loss. What's wrong with the formula?

#include <stdio.h>
#include <stdlib.h>
#define ex exit(EXIT_FAILURE)

int main (int argc, char *argv[]) {
    if (argc!=3) {
       puts("beta usage error");
       ex;
       }
    double x,y;
    x=strtod(argv[1],NULL);
    y=strtod(argv[2],NULL);

double percent(double a,double b) {
             double per;
             per=(a-b)*100/a;
      return per;
             }
printf("%.2f",percent(x,y));
return 0;

}

Following function gives what you are expecting.
double percent(double a,double b) {
double per;
if ( a < b) {
per = (b -a ) * 100 /b;
per = 100 - per;
} else {
per=(a-b)*100/a;
}
printf("%f\n", per);

return per;
}
 
R

Robert Gamble

        I need help on this utillity it is supposed to take as its first
argument a number and its second argument a subtraction of that number say
for example 20 and 2. The program should calculate the percentage lost or
gained by the first number using the second number. This program is
reporting to me the opposite of what it should and I have tried everything.
It says this.

20 2

or 2 from 20 leaving 18 is 90% loss.
2 from 20 is a 10% loss. What's wrong with the formula?

#include <stdio.h>
#include <stdlib.h>
#define ex exit(EXIT_FAILURE)

int main (int argc, char *argv[]) {
    if (argc!=3) {
       puts("beta usage error");
       ex;
       }
    double x,y;
    x=strtod(argv[1],NULL);
    y=strtod(argv[2],NULL);

double percent(double a,double b) {
             double per;
             per=(a-b)*100/a;
      return per;
             }
printf("%.2f",percent(x,y));
return 0;

}

C doesn't support nested functions, your printf should have a newline,
and your indentation is hard to follow. The core of your question
seems to be one of basic arithmetic. Your explanation is confusing
but you seem to be looking for the percentage difference between x and
x-y which is:

100 * (x - (x-y))/x

which is equivalent to:

100 * (y/x)

The following works for me:

#include <stdio.h>
#include <stdlib.h>
#define ex exit(EXIT_FAILURE)

int main (int argc, char *argv[]) {
double x,y;

if (argc!=3) {
puts("beta usage error");
ex;
}

x=strtod(argv[1],NULL);
y=strtod(argv[2],NULL);

printf("%.2f\n", 100*y/x);
return 0;
}
 
R

Robert Gamble

        I need help on this utillity it is supposed to take as its first
argument a number and its second argument a subtraction of that number say
for example 20 and 2. The program should calculate the percentage lost or
gained by the first number using the second number. This program is
reporting to me the opposite of what it should and I have tried everything.
It says this.
or 2 from 20 leaving 18 is 90% loss.
2 from 20 is a 10% loss. What's wrong with the formula?
#include <stdio.h>
#include <stdlib.h>
#define ex exit(EXIT_FAILURE)
int main (int argc, char *argv[]) {
    if (argc!=3) {
       puts("beta usage error");
       ex;
       }
    double x,y;
    x=strtod(argv[1],NULL);
    y=strtod(argv[2],NULL);
double percent(double a,double b) {
             double per;
             per=(a-b)*100/a;
      return per;
             }
printf("%.2f",percent(x,y));
return 0;

Following function gives what you are expecting.
 double percent(double a,double b) {
             double per;
             if ( a < b) {
               per = (b -a ) * 100 /b;
               per = 100 - per;
             } else {
               per=(a-b)*100/a;
             }
             printf("%f\n", per);

      return per;
             }

Is this a joke?
 
B

Bill Cunningham

[snip]

#include <stdio.h>
#include <stdlib.h>
#define ex exit(EXIT_FAILURE)

int main (int argc, char *argv[]) {
double x,y;

if (argc!=3) {
puts("beta usage error");
ex;
}

x=strtod(argv[1],NULL);
y=strtod(argv[2],NULL);

printf("%.2f\n", 100*y/x);
return 0;
}

Yes it works great but what about in the opposite direction? This
program shows percentage lost from a number but how could you show
percentage gained?


Bill
 
R

Robert Gamble

[snip]

#include <stdio.h>
#include <stdlib.h>
#define ex exit(EXIT_FAILURE)

int main (int argc, char *argv[]) {
  double x,y;

  if (argc!=3) {
     puts("beta usage error");
     ex;
   }

  x=strtod(argv[1],NULL);
  y=strtod(argv[2],NULL);

  printf("%.2f\n", 100*y/x);
  return 0;

}

    Yes it works great but what about in the opposite direction? This
program shows percentage lost from a number but how could you show
percentage gained?

Bill

If './percent_lost 20 2' means "20 losing 2":

./percent_lost 20 2 # => 10.00 representing a 10 percent loss
./percent_lost 20 20 # => 100.00 representing a 100 percent loss
./percent_lost 20 -5 # => -25.00 representing a -25 percent loss

The last example, "20 losing -5" or "20 gaining 5" results in -25.00
which represents a negative loss, i.e. a positive gain.
 
M

Mark L Pappin

Bill Cunningham said:
I need help on this utillity

You need a lot of other help first.

Get on with the tutorial and stop faffing around with other things.
If you won't stick to it, I won't help you any more.

mlp
 
B

Bill Cunningham

Mark L Pappin said:
You need a lot of other help first.

Get on with the tutorial and stop faffing around with other things.
If you won't stick to it, I won't help you any more.

mlp

Ok Mark. I just wanted to write this for awhile.

Bill
 
M

Mark L Pappin

Ok Mark. I just wanted to write this for awhile.

You don't have the skills you need to "write this".

I'm working to get you up to the level where you _do_ have the skills
to solve this problem but I need your help to do that. You can help
by not confusing yourself, but instead sticking to the graduated
exercises I have set.

Each exercise will reinforce the ideas presented in previous
exercises, and introduce at most a couple of new ideas. If you look
back at what I've set so far, you'll see

- drive a compiler
- print a string
- print a different string (which happens to be a sequence of numbers)
- print a simple sequence of numbers
- lay out a program so its structure is visible
- print a sequence of calculated values
- look at the range of values a variable can take

which you might see are working toward one of the projects you've
previously posted your failed attempts at here: printing the
cumulative and/or rolling sum and/or average of a sequence of numbers
input by the user. Since you seem interested in calculation of other
characteristics of sets of values, I'll add this as a future exercise,
but it's a non-trivial (to you) step beyond anything you've
demonstrated competence at so far.

Spend your time on the exercises I have set. Pretend it's a job, and
that I'll fire your donkey if you don't.

When you spend time on side projects like this, your attention is
diverted from the learning task at hand, and any gains we may have
made are eroded. Once you are competent, then doing exercises like
this will be useful for you as a break from whatever more-involved
task you're working on, to shake the cobwebs out (like, say, doing
some pushups for variety in the middle of a jog) but for now you're
stuck tying your laces (or perhaps finding two shoes from the same
pair).

mlp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

big mess 12
sh?tpile of errors 82
pow type problem 6
SENTINEL CONTROL LOOP WHEN DEALING WITH TWO ARRAYS 1
code question 74
Function is not worked in C 2
completely stuck 14
moving average 19

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top