need some help with rounding and calculations

This is a discussion on need some help with rounding and calculations within the C Programming forums, part of the General Programming Boards category; hey guys so me and a friend were making a program to compute the total amount what recycl centers will ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    41

    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);
    }
    Last edited by ecsx00; 09-29-2011 at 09:58 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    Quote Originally Posted by iMalc View Post
    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
    Last edited by ecsx00; 09-29-2011 at 01:47 AM.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by ecsx00 View Post
    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. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    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?
    Last edited by ecsx00; 09-29-2011 at 09:58 PM.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by ecsx00 View Post
    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. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ecsx00 View Post
    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.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by quzah View Post
    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. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with calculations
    By Taka in forum C Programming
    Replies: 14
    Last Post: 09-01-2007, 07:00 PM
  2. pi calculations
    By nbice in forum C++ Programming
    Replies: 7
    Last Post: 09-30-2002, 01:48 PM
  3. calculations
    By wayko in forum Windows Programming
    Replies: 8
    Last Post: 10-11-2001, 06:19 AM
  4. calculations
    By wayko in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2001, 10:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21