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

• 10-22-2006
noodles355
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:
Quote:

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.
Quote:

# 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:
Quote:

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.
• 10-22-2006
manutd
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...
• 10-22-2006
CornedBee
It would help a lot (especially me, trying to understand your post) if you gave the parameters to book() meaningful names.
• 10-22-2006
yxunomei
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?
• 10-22-2006
manutd
• 10-22-2006
anon
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);```
• 10-22-2006
noodles355
Sorry, I called them all a b c x y z etc as I wasn't sure if having full words would effect it...

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.

Quote:

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
• 10-22-2006
anon
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 ; }```
• 10-22-2006
manutd
A one line comment can also be in the form //This is a comment.
• 10-22-2006
quzah
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.
• 10-23-2006
noodles355
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:
Quote:

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...
• 10-23-2006
SKeane
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; }```
• 10-23-2006
noodles355
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,