beginner coder - having trouble calling previous functions, probably something simple

This is a discussion on beginner coder - having trouble calling previous functions, probably something simple within the C Programming forums, part of the General Programming Boards category; Hi, I'm a complete beginner to coding, I'm studying Comp. Sci. at Bristol Uni in the Uk and am into ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    5

    Question beginner coder - having trouble calling previous functions, probably something simple

    Hi, I'm a complete beginner to coding, I'm studying Comp. Sci. at Bristol Uni in the Uk and am into my second week. The first thing we did was easy, the ol' printf "Hello World!" code, but we're onto our second coursework and I think I must have just missed something basic... I'm not looking for someone to write the code I need or anything like that, I'm not looking for coursework answers... It's simply that there must be some simple code I've just over looked... (ps the program is supposed to not utilise any "variable" at all, only functions)

    Ok, the question boils down to giving you a set of data for the cost of printing different kinds of books. The data, costs of pages, etc is on the page which I'll post at the end. Anyway the work boils down to 4 parts:
    Write a function that calculates the price of a job given the number of pages, price per page, price per plate, and number of copies.
    Ok this was ok (I think...). Considering 4 possible variables: Cost of Plates, Cost of Paper, Number of Pages and Number of books, as well as binding the books and VAT, etc I've managed to get:
    Code:
    double book( double a, double b, double c, double d ) {
      return (((b * c) + ((b/2) * d * a) + (200 * d)) * (117.5/100))/100 ;
    }
    And that seems to work fine. I plugged in the example data and tested it against the result they showed and it works. This is all fine and dandy.
    # Write a function that calculates the price of a job given the number of pages and the number of copies for black and white printing. This function should call the function that you wrote for part 1.
    # Write a function that calculates the price of a job given the number of pages and the number of copies for colour printing. This function should call the function that you wrote for part 1.
    Ok, part 2... now this is where it starts to get slightly confusing... the only other code I know really is the printf one, so I wrote this:
    Code:
    int main ( void ) {
      printf(" Black and White copy= %f\n", book(1,b,700,d) ) ;
      printf(" Colour copy= %f\n", book(4,b,2800,d) ) ;
      return 0 ;
    }
    And that prints fine. But I can't help feel this isn't really what I'm after... especially considering the next (and final) part of the excercise:
    Write a main program that calculates the total price of printing 1000 colour books with 32 pages, 2000 black/white books with 40 pages and 400 black/white books with 160 pages. The main function should call the functions that you wrote for parts 2 and 3.
    Now... this I got stuck on when I started it on mondy and lead to me putting this off all week untill today (surprise surprise deadline time >.>;; ), although I have also had a silly italian teacher going confusing me... but I digress. My first on this question was "how the heck do I do that?!".

    I looked back through all the lecture slides... the first set were on the course outline, the double function I posted above and the main printf one. Nothing else. The second set are all about inputs and variables which this excercise doesn't use... So I had a search for some C guides online (sadly the course book we use is out of print and my copy is on order from the US and wont be here for a good few days -.-*) eventually I found something that I thought would help, another University course guide... all about "int"s and such... so I had a read through and thought maybe I could use the %f thing to refer to the double function above to calculate the three prices ofr the books... and basically... this is where I am now... I don't know how to "call" that first function without printing any text (the method I tried (and that is posted below) doesn't work). That's what I need to do, after that I need to print the total of the three results in one line. This is where I'm at now: (this code doesn't actually work sadly ;-; )
    Code:
    double book( double a, double b, double c, double d ) {
      return (((b * c) + ((b/2) * d * a) + (200 * d)) * (117.5/100))/100 ;
    }
    
    int x ;
    x=%f, book(4,32,2800,1000) ;
    
    int y ;
    y=%f, book(1,40,700,2000) ;
    
    int z ;
    z=%f, book(1,160,700,400) ;
    
    double total( double p, double q, double r ) {
      return p + q + r ;
    }
    
    int main ( void ) {
      printf(" The total cost of this printing job is %f pounds./n", total(x,y,z) ) ;
      return 0 ;
    }
    The actual page with all the data and instructions on it is here for refference:
    http://www.cs.bris.ac.uk/Teaching/Re...sework/02.html

    And I would like to stress again I am NOT looking for someone to do my coursework, I do not want this to be taken as lazy student trying to cheat through his course. I really just think I have missed something basic... That's what I want to find out, how can I calculate three different book prices, calling to the first formula, and then later print the total of the three prices togeather without using any inputs or variables.

    If you can offer any help, thank you very much, if not, I'll be crying in a corner...
    -Rich.

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I may be wrong on this, and please correct me if I am, but can you use %f in assigning a value to the variable? Try taking this out...
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It would help a lot (especially me, trying to understand your post) if you gave the parameters to book() meaningful names.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    try this
    Code:
    #include <stdio.h>
    
    int functionA(int x) {
       return x * 2;
    }
    
    int functionB(int y) {
       return functionA(y) * 2;
    }
    
    int functionC(int z) {
       return functionB(z) * 2;
    }
    
    int main() {
       printf("%d\n", functionC(10));
       return 0;
    }
    so the basic idea is to pass a parameter to a function, then that function passes the parameter to another function and so on rite?

  5. #5
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Also, as a bit of future advice, please include comments in your code in case others, like us, have to read it.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could just add up the results of three calls to function book(), not forgetting that it returns double.

    Code:
    double foo(int n);
    //....
    double x;
    x = foo(1) + foo(2) + foo(3);

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    Sorry, I called them all a b c x y z etc as I wasn't sure if having full words would effect it...
    I'll add comments next time aswell, that's as in /* Comment */ right?

    Ok, yes the basic idea is to have a function (which has 4 variables). From that make three functions that use the first one to create 3 totals. From there I need to then print the total of those three totals, if that makes sense.

    so like:
    Function 1: (A + B)/C +D.

    Book1: A=1, B=1, C=2, D=1
    (Thus book1 total is 2)

    Book2: A=2, B=2, C=1, D=2
    (Thus book2 total is 6)

    Book3: A=1, B=3, C=2, D=2
    (Thus book3 total is 4)

    Function 2: Book1 + Book2 + Book3
    (So total is 12)

    Then Print: "Total is 12".

    I think that's essentially what i'm after.
    I'll repost my last attempt with some comments aswell:
    Code:
    double book( double a, double b, double c, double d ) {
      return (((b * c) + ((b/2) * d * a) + (200 * d)) * (117.5/100))/100 ;
    }
    This is the original formula to work the total cost of the book depending on the 4 numbers: a, b, c and d.
    Code:
    int x ;
    x=%f, book(4,32,2800,1000) ;
    
    int y ;
    y=%f, book(1,40,700,2000) ;
    
    int z ;
    z=%f, book(1,160,700,400) ;
    These were an attempt to have x, y and z in all these cases equal what you would get if you input the 4 numbers in brackets into the above formula.
    Code:
    double total( double p, double q, double r ) {
      return p + q + r ;
    }
    This was to be the second function that added the totals of the above three sums togeather.
    Code:
    int main ( void ) {
      printf(" The total cost of this printing job is %f pounds./n", total(x,y,z) ) ;
      return 0 ;
    }
    And this was meant to print the total from that second function.


    Code:
    int functionA(int x) {
       return x * 2;
    }
    
    int functionB(int y) {
       return functionA(y) * 2;
    }
    
    int functionC(int z) {
       return functionB(z) * 2;
    }
    What will this do? is this like the correct way of doing what I tried to do in my second step?
    Again, with the other suggestion... I'm not sure entirely what you mean >.< (I really am new to this and am having trouble finding resources that don't jump to inputs and vairables right after the Hello World script...

    Thanks for the help again,
    -Rich

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Ok, I've pointed out some problems.
    Assignment is done so:
    x = a + b
    y = foo(a, b)
    etc.

    I don't see a reason to write a special function to add up 3 numbers.

    Code:
    double book( double a, double b, double c, double d ) {
      return (((b * c) + ((b/2) * d * a) + (200 * d)) * (117.5/100))/100 ;
    }
    
    
    //following should be inside main
    int x ; //book returns double
    x=%f, book(4,32,2800,1000) ;  //assignment: x = book(4,32,2800,1000) ;
    
    int y ;
    y=%f, book(1,40,700,2000) ;
    
    int z ;
    z=%f, book(1,160,700,400) ;
    
    double total( double p, double q, double r ) {
      return p + q + r ;
    } //it's easier just to do p + q + r without a special function
    
    int main ( void ) {
      printf(" The total cost of this printing job is %f pounds./n", total(x,y,z) ) ; //pro: x + y + z
      return 0 ;
    }

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    A one line comment can also be in the form //This is a comment.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by manutd
    A one line comment can also be in the form //This is a comment.
    Only on a C99 compiler.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    ah I see, I wasn't aware I could just do ", x + y + z", that makes life a lot easier. Ok, I've now got:

    Code:
    double book( double a, double b, double c, double d ) {
      return (((b * c) + ((b/2) * d * a) + (200 * d)) * (117.5/100))/100 ;
    }
    
    int main ( void ) {
    int x ;
    x = book(4,32,2800,1000) ;
    int y ;
    y = book(1,40,700,2000) ;
    int z ;
    z = book(1,160,700,400) ;
      printf(" The total cost for this printing job is %f pounds./n", x + y + z ) ;
      return 0 ;
    }
    That's the right way huh? x = name(1,2,3,4), Ok I'll remember that, I was only trying x=%f, name(1,2,3,4) as that's how it was done in the printf statement (like I said I'm having real problems without the text book...)

    But now I'm getting the error when I try to compile:
    line 7: cannot convert from float/double
    '= book(4,32,2800,1000)'
    I'm sorry this is so retarded, I really am a complete beginner... I can understand why what the lecturer says would work, I can understand *why* for it all, it's just a matter of *how* to do it, you know, what to write... I think I must have missed a lecture where he went through this...

  12. #12
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    book() is returning a double, you are trying to store that double in an int (called x or y).

    Code:
    double book( double a, double b, double c, double d ) 
    {
        return (((b * c) + ((b/2) * d * a) + (200 * d)) * (117.5/100))/100 ;
    }
    
    int main ( void ) 
    {
        double  x, y, z;
        x = book(4,32,2800,1000) ;
        y = book(1,40,700,2000) ;
        z = book(1,160,700,400) ;
        printf(" The total cost for this printing job is %f pounds.\n", x + y + z ) ;
    
       return 0;
    }

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    Oh, that makes sense now... Like I said, befor the only time I had seen the double in action was similar to:
    Code:
    double book( double a, double b, double c, double d ) {
      return (((b * c) + ((b/2) * d * a) + (200 * d)) * (117.5/100))/100 ;
    }
    
    int main ( void ) {
      printf(" Black and White copy= %f\n", book(1,b,700,d) ) ;
      printf(" Colour copy= %f\n", book(4,b,2800,d) ) ;
      return 0 ;
    }
    Now I see how it works... heh kinda simple when you think about it. Brilliant, I'll edit that accordingly. Thank you so much for the help, hopefully with any future problems I have I'll try to make it a little more clear, though the next one is a lot of fun about loops, if-then-else statements, and recursion, so that should be alrigth (as we've had the lecture and I have the slides on that topic). Thanks again,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-03-2007, 02:02 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. trouble defining member functions
    By dP munky in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2003, 08:52 PM
  4. Calling functions written in assembly?
    By The V. in forum C Programming
    Replies: 5
    Last Post: 10-24-2001, 08:11 PM
  5. Major Beginner looking for simple pointers
    By dlkchic in forum C++ Programming
    Replies: 4
    Last Post: 09-05-2001, 12:27 PM

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