# Thread: New to programming - Lack math skills

1. ## New to programming - Lack math skills

Hello,
I just finished my first programming class and find myself lacking any programming skill. I obviously need to take a math class, but for now would like to learn as much as possible till next semester.

I have started to create a program that will calculate the users income in comparison to the world.
I want it to return a percent.
In this case the user income is above 33700 so they are in the top 5 percent. However, I would like to factor in the remaining 6300.
Any and all help would be greatly appreciated.
For now I just am doing the program for one income , but once I understand the math part I will create the rest of the program.
Here is what I have done

Code:
```#include <stdio.h>
#define TOP_INCOME 100
#define TOP_TIER 80
#define TOP_TEN 25400
#define TOP_FIVE 33700
#define TOP_ONE 47500

int main ()
{
//Would scan right here
//lets say scan returned 40,000
int userIncome=40000;
int overBottomOfTier=0;
if (userIncome>=TOP_FIVE && userIncome<TOP_ONE)
{
overBottomOfTier=userIncome-TOP_FIVE;
printf("You are %d over the bottom of top five percent\n\n", overBottomOfTier );

}

return 0;

}```
So in this case I would need 95% + 6300\$ = a percentage
Thanks again for the help.

2. Here is the chart I am going by.

Global Rich List

There are 8 tiers and I would like to factor what is over each tier into the total percentage

3. Originally Posted by EdMarx
Hello,
I just finished my first programming class and find myself lacking any programming skill. I obviously need to take a math class, but for now would like to learn as much as possible till next semester.
int main instead of void main....check
return 0; at the end of main....check
descriptive variable names....check
defined constants instead of magic numbers....check
working on small parts at a time instead of writing 5000 lines before compiling and testing....check
showing effort instead of asking us to do it all for you....check
attempting to state your problem clearly....check

I wouldn't say you're lacking any skill. You're miles ahead of most of the n00bs we get on here.

But it would be a bit more helpful if you could give a concrete example of what you're trying to do. What do you mean by "factor in the remaining 6300". I understand where the 6300 comes from, but I don't know what calculation you want to do with it, or what output you expect based on it.

Based on the chart you posted, \$40000 is in the top 5 percent, \$6300 over the bottom limit of the top 5% group. The top 1% income level is \$47500, which means that the top 5% and top 1% have \$47500 - \$33700 = \$13800 between the two levels. So are you looking for what percent of the way is \$40000 between the 5% group and the 1% group? If so, you need to divide the surplus (6300) by the difference between the level it's in and the level above (13800). 6300 / 13800 * 100 = 45.65% (approximately). That's 45.65% of the way between the 95% mark and the 99% mark.

If that's not what you're after, then you need to explain a little more clearly, perhaps with a more complete example.

4. I think I might be onto something.
Or I could be completly off
Code:
```difference=TOP_ONE-TOP_FIVE;
//which is 13800
//6300 is the income above 95%
//do...
j=13800/6300
//j = .27```
So if I find the common denominator cant I just factor it into the 95%??

5. Your division is upside-down, and I have no idea where that j = .27 bit came from. Reread my second to last paragraph from my previous post. Hopefully it will be clear enough on second inspection.

EDIT:
I don't know what you intend to use common denominator for or what you mean by "factoring it into the 95%". I don't see a use for common denominator here.

Again, you need to provide me a clear example of what you are trying to do. Exactly what input to you give the program. Exactly what output do you get for that input? Maybe 2-3 examples if it's not too much trouble to work up.

6. A guess on what you want is Linear interpolation.
Linear interpolation - Wikipedia, the free encyclopedia

So in this case I would need 95% + 6300\$ = a percentage
What do you think the answer should be to the about problem?
Do you have a range the answer should be inside of?

Tim S.

7. ## ITT

Originally Posted by anduril462
I wouldn't say you're lacking any skill. You're miles ahead of most of the n00bs we get on here.
Wow! You have no idea what a compliment that is.
I try but I just feel like there is soooo much I dont know.
For instance I dont even know why I use int main as opposed to void main
or
(other than argv) what parameters to put in the parenthesis of main.
But I am learning, and am very driven to be as good as you guys.

Thanks for the reply. I am at work but will work more on this issue on my next break.

8. Originally Posted by EdMarx
Wow! You have no idea what a compliment that is.
I try but I just feel like there is soooo much I dont know.
We were all there at one point in our lives.
For instance I dont even know why I use int main as opposed to void main
or
(other than argv) what parameters to put in the parenthesis of main.
The short answer is because that's what the C standard says you must do (and argc is the only other parameter besides argv you can use*). More info is here if you want it: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com.

But I am learning, and am very driven to be as good as you guys.
Because of this, you will succeed.

Nice picture, BTW...I know your frustration. I used to work tutoring math to all ages (literally kindergarten through college), and word problems were the biggest trouble spot for the vast majority. It is one of the most difficult parts of programming and many other fields, translating human ideas to logical systems that can be implemented. The good news is your difficulties can be overcome.

* For hosted environments, like a regular PC, the parameters can be named anything you want, but the types must be correct (an int and a char ** or equivalent), and in the right order, and you must either use both or neither. There are extensions that some systems provide (Linux/GCC allows you to pass an "environment" as a third parameter). Also, non-hosted environments (embedded systems and the like, without a "typical" user interface) can make main look however they want, but those are special cases and probably not anything you'll have to deal with for a while.

9. ## still n00b

Originally Posted by anduril462

you need to divide the surplus (6300) by the difference between the level it's in and the level above (13800). 6300 / 13800 * 100 = 45.65% (approximately). That's 45.65% of the way between the 95% mark and the 99% mark.
.
Yes.
So far I know.

a. The users income is in the top 95% of the world (5% tier)
b. The tier ranges from 47500 to 33700 (13800 difference)
c. The user is 6300 over the bottom of the tier he is in

So I know his percentage is going to be over 95%.
But less than 99%(percentage range of the tier he is in)
I dont know how to factor in the 6300(45.65%) to the 95%
to make it an appropriate number.

On a different note, I tried to return the percentage the user is above the bottom of his tier. (45.65%) but it is returning 0.000.
Anyone know what I am doing wrong?

Code:
```#include <stdio.h>
#define TOP_INCOME 100
#define TOP_TIER 80
#define TOP_TEN 25400
#define TOP_FIVE 33700
#define TOP_ONE 47500

int main ()
{
//Would scan right here
//lets say scan returned 40,000
int userIncome=40000;
int overBottomOfTier=0;
int tierRange=0;
float percentAboveTierBottom;
if (userIncome>=TOP_FIVE && userIncome<TOP_ONE)
{
overBottomOfTier=userIncome-TOP_FIVE;
tierRange=TOP_ONE - TOP_FIVE;

printf("You are %d over the bottom of top five percent\n\n", overBottomOfTier );

printf("There is a %d difference between\n the bottom and top of the tier you are in\n\n", tierRange);

percentAboveTierBottom=(overBottomOfTier/tierRange)* 100;
printf("you are %f above the bottom of your tier", percentAboveTierBottom);
}

return 0;

}```

10. #define TOP_FIVE 33700
#define TOP_ONE 47500

The math is like this.

percent = 95 + ((40,000-TOP_FIVE)/(TOP_ONE-TOP_FIVE) * (5-1))
Note: I did not verify the math or use the website I posted to do it.

It is an example of Linear interpolation.
Linear interpolation - Wikipedia, the free encyclopedia

Tim S.

11. Try changing

Code:
```#define TOP_TEN 25400
#define TOP_FIVE 33700
#define TOP_ONE 47500```
To
Code:
```#define TOP_TEN 25400.0
#define TOP_FIVE 33700.0
#define TOP_ONE 47500.0```
You are having the integer division issue the newbie have.

in C 2/3 equals 0
but, 2.0/3 equals 0.66666666666666

Note: You can also cast the values to float or double.

As in (double)2/3 equals 0.66666666666

Tim S.

12. Originally Posted by stahta01
What do you think the answer should be to the about problem?
Do you have a range the answer should be inside of?

Tim S.

Originally Posted by stahta01
A guess on what you want is Linear interpolation.
Do you have a range the answer should be inside of?
Yes.
I know the range is between 95% and 99%
because the users income is > 33700 (bottom of the tier he is in)
and < 47499(top of tier he is in.

I also know the user is 45.65% from 33700 to 47499.

the user is above the bottom
1.82% =percentage range(4) * percentage above bottom of range(45.65) = .4565 * 4

1.82 + 95

which is 96.82% ???

.45

13. Originally Posted by EdMarx
Yes.
I know the range is between 95% and 99%
because the users income is > 33700 (bottom of the tier he is in)
and < 47499(top of tier he is in.

I also know the user is 45.65% from 33700 to 47499.

the user is above the bottom
1.82% =percentage range(4) * percentage above bottom of range(45.65) = .4565 * 4

1.82 + 95

which is 96.82% ???
Well, your formula is correct, and the numbers you plugged in are correct, and unless you had a typo on the calculator, your answer is correct. My intuition agrees, 45% is just a little less than half way (which would be 50%), and 96.82 is just a little less than half way between 95 and 99 (which would be 97), so I think you got it.

14. ## n00b++

Ok guys (and gals)

This is what I have so far.

Code:
```#include <stdio.h>
#define TOP_INCOME 100
#define TOP_TIER 80
#define BOTTOM_TEN 400.0
#define BOTTOM_TWENTY 500.0
#define BOTTOM_FIFTY 850.0
#define BOTTOM_SEVENTYFIVE 1487.0
#define BOTTOM_EIGHTYFIVE 2182.0
#define TOP_TEN 25400.0
#define TOP_FIVE 33700.0
#define TOP_ONE 47500.0

int main ()
{
//Would scan right here
//lets say scan returned 40,000
int userIncome=0;
int overBottomOfTier=0;
int tierRange=0;
float percent=0;
float percentAboveTierBottom;
//for some reason I think it looks cleaner to do 2 new lines
scanf("%d", &userIncome);
if (userIncome<BOTTOM_TEN)
printf("Your in the bottom 10 percent\n");

else if (userIncome>=BOTTOM_TEN && userIncome<BOTTOM_TWENTY)
{
percent = 10.0 + ((userIncome-BOTTOM_TEN)/(BOTTOM_TWENTY-BOTTOM_TEN) * (10.0-1.0));
printf("You are %f percent richer\nthan the rest of the world\n\n", percent);
}
else if (userIncome>=BOTTOM_TWENTY && userIncome<BOTTOM_FIFTY)
{
percent = 20.0 + ((userIncome-BOTTOM_TWENTY)/(BOTTOM_FIFTY-BOTTOM_TWENTY) * (40.0-1.0));
printf("You are %f percent richer\nthan the rest of the world\n\n", percent);
}

else if (userIncome>=BOTTOM_FIFTY && userIncome<BOTTOM_SEVENTYFIVE)
{
percent = 50.0 + ((userIncome-BOTTOM_FIFTY)/(BOTTOM_SEVENTYFIVE-BOTTOM_FIFTY) * (25.0-1.0));
printf("You are %f percent richer\nthan the rest of the world\n\n", percent);
}

else if (userIncome>=BOTTOM_SEVENTYFIVE&& userIncome<BOTTOM_EIGHTYFIVE)
{
percent = 75.0 + ((userIncome-BOTTOM_SEVENTYFIVE)/(BOTTOM_EIGHTYFIVE-BOTTOM_SEVENTYFIVE) * (10.0-1.0));
printf("You are %f percent richer\nthan the rest of the world\n\n", percent);
}

else if (userIncome>=BOTTOM_EIGHTYFIVE && userIncome<TOP_TEN)
{
percent = 85.0 + ((userIncome-BOTTOM_EIGHTYFIVE)/(TOP_TEN-BOTTOM_EIGHTYFIVE) * (5.0-1.0));
printf("You are %f percent richer\nthan the rest of the world\n\n", percent);
}

else if (userIncome>=TOP_TEN && userIncome<TOP_FIVE)
{
percent = 90.0 + ((userIncome-TOP_TEN)/(TOP_FIVE-TOP_TEN) * (5.0-1.0));
printf("You are %f percent richer\nthan the rest of the world\n\n", percent);
}

else if (userIncome>=TOP_FIVE && userIncome<TOP_ONE)
{
overBottomOfTier=userIncome-TOP_FIVE;
tierRange=TOP_ONE - TOP_FIVE;

printf("You are %d over the bottom of top five percent\n\n", overBottomOfTier );

printf("There is a %d difference between\n the bottom and top of the tier you are in\n\n", tierRange);

percentAboveTierBottom=(overBottomOfTier/tierRange)* 100.0;
printf("you are %f above the bottom of your tier\n\n", percentAboveTierBottom);
percent = 95.0 + ((userIncome-TOP_FIVE)/(TOP_ONE-TOP_FIVE) * (5.0-1.0));
printf("OK! (fingers crossed)You are %f\nricher than the rest of the world\n\n", percent);

}
else if (userIncome>TOP_ONE)
{
printf("You are top 1 percent");
}
else{
printf("does not compute\n");
printf("Can I Borrow 5000 Bucks");
}

if(percent>=50)
{
printf("\n\n\nCan I Borrow 5 Bucks");
}

return 0;

}```

15. Nice, looks pretty good. One correction to your formulas (I didn't read Tim's post #10 carefully):

At the end of your precent = ... lines, you always do (something - 1.0). You don't always want 1.0, you want the difference between that percentage and the one above it. Really, for the difference between the top 5% and top 1%, you are looking at the difference between 95% and 99%, i.e. 99 - 95 = 4. So for example, when calculating how far between the top 20% and top 50%, you want to multiply by (50.0 - 20.0). I'm not really sure where your 40.0 came from, but whatever:
Code:
`percent = 20.0 + ((userIncome-BOTTOM_TWENTY)/(BOTTOM_FIFTY-BOTTOM_TWENTY) * (50.0-20.0));`
And similar for the other ranges.