# Thread: need some help with rounding and calculations

1. ## need some help with rounding and calculations

hey guys so me and a friend were making a program to compute the total amount what recycl centers will give. we have center a which rounds up the pounds before calculating total and center b where you calculate the total and then round to the nearest 0.005 cents. the program runs but the calculations are not working and center b isnt computing anything back. we still havent figured out what we are doing wrong. could someone point us to the right direction? will appreciate the help we can get

edit: posted new code
Code:
```#include <stdio.h>
#include <stdlib.h>

float roundb(float v);
int rounda(float aluminum, float glass, float newspaper);
int main()
{
int valuea, valueb, al, gl, ne;
float aluminum, glass, newspaper, v;

//input
printf("Number of pounds of \n");
printf("\t Aluminum: ");
scanf("%f", &aluminum);
printf("\t Glass: ");
scanf("%f", &glass);
printf("\t Newspaper: ");
scanf("%f", &newspaper);

//output the input
printf(" %0.2f lbs Aluminum\n", aluminum);
printf(" %0.2f lbs Glass\n", glass);
printf(" %0.2f lbs Newspaper\n", newspaper);

//equation for a / b
rounda(aluminum, glass, newspaper);
roundb(v);
valuea = (al * 0.25) + (gl * 0.02) + (ne * 0.02);
valueb = roundb(v);

//results
printf("You get: \n");
printf("%d from Center A\n", valuea);
printf("%d from Center X\n", valueb);
}

float roundb(float v)
{
v = v+0.005;
int vInt = (int)(100.0*v);
v = (float)(vInt/100.0);
return v;
}

int rounda(float aluminum, float glass, float newspaper)
{
int al = (int)(aluminum+0.5);
int gl = (int)(glass+0.5);
int ne = (int)(newspaper+0.5);
}```

2. You haven't declared valuex, and you haven't assigned anything to valueb. Are those two meant to be related? Not declating valuex means that at the moment it cannot compile.

You can't return more than one value from a function. Your round2 function would give warnings about unreachable code. If not, turn your compiler warning levels up and pay attention to them. I can't even work out what you want the round2 function to do.

3. Originally Posted by iMalc
You haven't declared valuex, and you haven't assigned anything to valueb. Are those two meant to be related? Not declating valuex means that at the moment it cannot compile.

You can't return more than one value from a function. Your round2 function would give warnings about unreachable code. If not, turn your compiler warning levels up and pay attention to them. I can't even work out what you want the round2 function to do.
sorry mybad i was modding the code to make it easy for my friend to read and forgot to change valuex to valueb, i have corrected the code to help

4. Originally Posted by ecsx00
Code:
```valuea = (aluminuma * 0.25) + (glassa * 0.02) + (newspapera * 0.02);

// centerb equation
valueb = (aluminum * 0.21) + (glass * 0.025) + (newspaper * 0.03);

valuea = round2(aluminum, glass, newspaper);
valueb = round1(valueb);

int round1(int valueb)
{
//return int
return (int)(valueb + 0.005);
}

int round2(float aluminuma, float glassa, float newspapera)
{
return (int)(aluminuma + 0.5);
return (int)(glassa + 0.5);
return (int)(newspapera + 0.5);
}```
Since valuea is an int, the value you calculate with "(aluminuma * 0.25) .." will be truncated, i.e. if the result is 3.7 valuea will contain 3. I don't really see the point of this statement at all, since you later in the code assign a new value to valuea without using the first one. Also in round2(...) you cannot return multiple values in that way. Either you have to call the function with the arguments being pointers to the variables you want to change, or return a structure containing your variables.

5. Those are absolutely terrible variable names.
Code:
```int round1(int valueb)
{
//return int
return (int)(valueb + 0.005);
}```
This effectively does nothing, because ints can't do partial numbers, so you may as well save yourself the trouble, and just do this:
Code:
`int round1( int valueb ) { return valueb; }`
Or better yet, throw the whole function away.

Quzah.

6. i made some adjustments to the code and i am getting some feedback but not really ideal feedback i wanted. what is the next step?

7. Originally Posted by ecsx00
i made some adjustments to the code and i am getting some feedback but not really ideal feedback i wanted. what is the next step?
Code:
`rounda(aluminum, glass, newspaper);`
This function call does nothing, as you aren't passing pointers to the function, neither using it's return value (the function isn't even returning a value)

Code:
```roundb(v);
valuea = (al * 0.25) + (gl * 0.02) + (ne * 0.02);
valueb = roundb(v);```
first you call roundb without using the return value, which effectively does nothing.

valuea is still an int, meaning that the expression to the right will be truncated (maybe this is your intent).

In the second call to roundb you use the return value, but the function returns a float value and valueb is an int, truncation will occur (and your compiler should give you warnings).
Code:
```float roundb(float v)
{
v = v+0.005;
int vInt = (int)(100.0*v);
v = (float)(vInt/100.0);
return v;
}```
Again you are converting back and forth between floats and ints. Remember that when a float value is assigned to an int, any floating point precision will be lost.

Code:
```int rounda(float aluminum, float glass, float newspaper)
{
int al = (int)(aluminum+0.5);
int gl = (int)(glass+0.5);
int ne = (int)(newspaper+0.5);
}```
This function does not return value when it should. al, gl ne are local variables and are at this point useless. Isn't your compiler giving you any warnings??

I think you have do learn the basics about different data types and functions in C, and then rewrite this program from the beginning.

8. Originally Posted by ecsx00
i made some adjustments to the code and i am getting some feedback but not really ideal feedback i wanted. what is the next step?
We kinda really hate it when you go back and edit your first post. Just make a new post and put your new code, that way people can see what you've changed along the way.

Quzah.

9. Originally Posted by quzah
We kinda really hate it when you go back and edit your first post. Just make a new post and put your new code, that way people can see what you've changed along the way.

Quzah.
A version controlled post system with the possibility of producing diff's would be nice :-)

10. sorry my bad i will post a new code tomorrow when me and my friend work out new changes and mods on it thanks for the explanations!