473,382 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

percentage

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;
}
Jul 29 '08 #1
7 2526
On Jul 29, 5:44*pm, "Bill Cunningham" <nos...@nspam.comwrote:
* * * * 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;
}
--
Dinakar
Jul 29 '08 #2
On Jul 29, 6:44*pm, "Bill Cunningham" <nos...@nspam.comwrote:
* * * * 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;
}

--
Robert Gamble
Jul 29 '08 #3
On Jul 29, 7:34*pm, "des...@gmail.com" <des...@gmail.comwrote:
On Jul 29, 5:44*pm, "Bill Cunningham" <nos...@nspam.comwrote:
* * * * 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;
* * * * * * *}
--
Dinakar
Is this a joke?

--
Robert Gamble
Jul 30 '08 #4

"Robert Gamble" <rg*******@gmail.comwrote in message
news:c8**********************************@2g2000hs n.googlegroups.com...

[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
Jul 30 '08 #5
On Jul 29, 9:01*pm, "Bill Cunningham" <nos...@nspam.comwrote:
"Robert Gamble" <rgambl...@gmail.comwrote in message

news:c8**********************************@2g2000hs n.googlegroups.com...

[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.

--
Robert Gamble
Jul 30 '08 #6

"Mark L Pappin" <ml*@acm.orgwrote in message
news:87************@Don-John.Messina...
"Bill Cunningham" <no****@nspam.comwrites:
> 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
Ok Mark. I just wanted to write this for awhile.

Bill
Jul 30 '08 #7
"Bill Cunningham" <no****@nspam.comwrites:
"Mark L Pappin" <ml*@acm.orgwrote
>Get on with the tutorial and stop faffing around with other things.
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
Jul 30 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Conax | last post by:
Hello, This is actually a general programming question but I don't know which newsgroup best to put it so please pardon me. I've always been looking for the best way to get an accurate...
7
by: Rob Meade | last post by:
Hi all, I have a recordset iterating through and dumping out to the screen a series of percentages, using the precision 5 and numericscale 2 etc. When I dump them to the page some of the...
2
by: thomasamillergoogle | last post by:
I am going to be distributing a shareware application soon and would like to know what percentage of MS OS machines have the framework installed. I have looked in multiple places (google,...
22
by: Les Juby | last post by:
I am trying to adjust the window/table size of a website (www.worklaw.co.za) which has made use of DIV tags with its settings embedded in an CSS file. The client wants its width and height to...
0
by: fake ID | last post by:
Since you can't search for these symbols used in asp.net "<%#" or '<%=' I thought i'd post this to make things a little easier to find. Potential search word combinations: -lessthan Percentage...
5
by: James Conrad StJohn Foreman | last post by:
Have found http://www-128.ibm.com/developerworks/db2/library/techarticle/lyle/0110lyle.html which is quite helpful, but doesn't quite tell me what I want. I have a table, advertising_spend with...
6
by: Hacking Bear | last post by:
Hi, I still don't quite fully understand how to handle mixing border/margin pixel width with percentage width. In the example below, I want to place side-by-side two DIV boxes inside a box....
4
by: Micheal | last post by:
Greetings Access Group, Being relatively new to Access, I try to work through problems on my own and have been very successful, although I have a conundrum that I have been working on for two days...
8
Nathan H
by: Nathan H | last post by:
I am trying to write a query that will result in showing records that have a large percentage difference among fields in a table. For instance: Table field1.Item field2.account1 (percentage)...
5
by: Aswanth | last post by:
I'm Using Asp.Net with C# & Working with SSRS 2005 for Generating Reports.. The Following Expression I'm using in Reports to Show the Percentage of Particular Items in REPORT.. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.