help with functions 
Author Message
 help with functions

I am having a problem with this program that has left me a bit confused.
The idea is supposed to be to use three functions to 1, print a header
(void return, void in); 2, cube a floating point number and return the
answer to main (double returned, float in), and last, to take the values
from the cubing function and print them out in the third function(void
returned, float in ). The program must not have any global variables
declared. Here is what I have so far :
--
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

/* prototype print_header function; nothing in or out */
void print_header(void);
/*  prototype cube function; gets a float, returns a double */
double cube(float);
/* proto print_result function, gets a float(original number); returns a
double(num cubed) */
void print_result(float, double);

int main(void)

{

 /* declare vars local to main() */

 float pi, number_nine, number_thir{*filter*};
 float cubed_pi, cubed_nine, cubed_thir{*filter*};

 /* interactive section, gets input from user */
 printf("First, enter the value of PI to 5 digits ( 3.14159)\n");
 scanf("%f", &pi);
 printf("Now, enter the next number ( 9.00000 )\n");
 scanf( "%f", &number_nine);
 printf("Finally, enter the last number ( 13.54286 )\n");
 scanf("%f", &number_thir{*filter*});

 /* call print_header function */
 print_header();

 /* pass pi to cube(), store result in  lvalue cubed_pi*/
 cubed_pi = (cube(pi));
 cubed_nine = (cube(number_nine)); /* pass number_nine, get cube */
 cubed_thir{*filter*} = (cube(number_thir{*filter*})); /* pass and return cube*/

 /* pass results to print_result()  function, nothing back*/
 print_result(pi, cubed_pi);
 print_result(number_nine, cubed_nine);
 print_result(number_thir{*filter*}, cubed_thir{*filter*});
 printf("Program has terminated");
 return 0;

Quote:
}

void print_header(void)
{
 printf("\n");
 printf("C Programming 1\n");
 printf("Assignment 3 For Terry Ferrarese\n");
 printf("--------------------------------\n");
 printf("\n");
 printf("Value          Value Cubed   \n"); /* 10 spaces between */
 printf("\n");
 printf("-----          -----------         \n");
 printf("\n");

Quote:
}

double cube(float num)      /* takes in a float, returns a double */
{
 double cubed = 0;
 cubed = num * num  * num ;
 return   cubed;

Quote:
}

void print_result(float num, double cubed_num)
{
 printf("%-6.5f %15.5f\n", &num, &cubed_num);

Quote:
}

-----------------end
According to the watch window, cube seems to be getting and returning
the proper numbers, function print_result prints only garbage. What am I
doing wrong here?
BTW, as this is for school, please don't do my work for me,  I just need
an idea of what it is I'm doing wrong.
Thanks to anyone who cares to help
--



Fri, 29 Mar 2002 03:00:00 GMT  
 help with functions


Quote:
> I am having a problem with this program that has left me a bit confused.
> The idea is supposed to be to use three functions to 1, print a header
> (void return, void in); 2, cube a floating point number and return the
> answer to main (double returned, float in), and last, to take the values
> from the cubing function and print them out in the third function(void
> returned, float in ). The program must not have any global variables
> declared. Here is what I have so far :

---snip---

-------- end snip------

Quote:

> void print_result(float num, double cubed_num)
> {
>  printf("%-6.5f %15.5f\n", &num, &cubed_num);

> }
> -----------------end
> According to the watch window, cube seems to be getting and returning
> the proper numbers, function print_result prints only garbage. What am I
> doing wrong here?
> BTW, as this is for school, please don't do my work for me,  I just need
> an idea of what it is I'm doing wrong.
> Thanks to anyone who cares to help
> --


It has been said that one persons weed is another persons wildflower.
printf( ) isn't printing garbage.  If you want to know where num and
cubed_num are stored in memory, use &num etc as above but
use the %p (for pointer) print conversion specifier.
But, you wanted the value of num, a float.  I'm trying not to just tell you.
Don't read this unless you can't figure it out.  Just num will pass
the value of num to printf.

--



Sat, 30 Mar 2002 03:00:00 GMT  
 help with functions
On Mon, 11 Oct 1999 19:51:19 GMT, in comp.lang.c.moderated terry

[...]

Quote:
>void print_result(float num, double cubed_num)
>{
> printf("%-6.5f %15.5f\n", &num, &cubed_num);

>}
>-----------------end
>According to the watch window, cube seems to be getting and returning
>the proper numbers, function print_result prints only garbage. What am I
>doing wrong here?

You're trying to print the addresses of num and cubed_num as doubles.
As you said, "garbage."  ;-)   I suspect this is because you are
confusing the uses of scanf and printf.

HTH.  Regards,

                          -=Dave
Just my (10-010) cents
I can barely speak for myself, so I certainly can't speak for B-Tree.
Change is inevitable.  Progress is not.
--



Sat, 30 Mar 2002 03:00:00 GMT  
 help with functions

writes

Quote:
>void print_result(float num, double cubed_num)
>{
> printf("%-6.5f %15.5f\n", &num, &cubed_num);

you are confusing the way scanf uses its parameters with the way printf
does.  scanf needs to know where to store the information input and so
needs the addresses of the variables; printf just needs the values to be
output.  You get that by just using the variables (without the &,
address-of operator)

Quote:

>}
>-----------------end
>According to the watch window, cube seems to be getting and returning
>the proper numbers, function print_result prints only garbage. What am I
>doing wrong here?
>BTW, as this is for school, please don't do my work for me,  I just need
>an idea of what it is I'm doing wrong.
>Thanks to anyone who cares to help

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Sat, 30 Mar 2002 03:00:00 GMT  
 help with functions

Quote:

>I am having a problem with this program that has left me a bit confused.
>The idea is supposed to be to use three functions to 1, print a header
>(void return, void in); 2, cube a floating point number and return the
>answer to main (double returned, float in), and last, to take the values
>from the cubing function and print them out in the third function(void
>returned, float in ). The program must not have any global variables
>declared. Here is what I have so far :
>--
>#include <stdio.h>
>#include <stdlib.h>
>#include <conio.h>

>/* prototype print_header function; nothing in or out */
>void print_header(void);
>/*  prototype cube function; gets a float, returns a double */
>double cube(float);

Here you say cube returns a double.

- Show quoted text -

Quote:
>/* proto print_result function, gets a float(original number); returns a
>double(num cubed) */
>void print_result(float, double);

>int main(void)

>{

> /* declare vars local to main() */

> float pi, number_nine, number_thir{*filter*};
> float cubed_pi, cubed_nine, cubed_thir{*filter*};

> /* interactive section, gets input from user */
> printf("First, enter the value of PI to 5 digits ( 3.14159)\n");
> scanf("%f", &pi);
> printf("Now, enter the next number ( 9.00000 )\n");
> scanf( "%f", &number_nine);
> printf("Finally, enter the last number ( 13.54286 )\n");
> scanf("%f", &number_thir{*filter*});

> /* call print_header function */
> print_header();

> /* pass pi to cube(), store result in  lvalue cubed_pi*/
> cubed_pi = (cube(pi));

And you store the value in a float.

- Show quoted text -

Quote:
> cubed_nine = (cube(number_nine)); /* pass number_nine, get cube */
> cubed_thir{*filter*} = (cube(number_thir{*filter*})); /* pass and return cube*/

> /* pass results to print_result()  function, nothing back*/
> print_result(pi, cubed_pi);
> print_result(number_nine, cubed_nine);
> print_result(number_thir{*filter*}, cubed_thir{*filter*});
> printf("Program has terminated");
> return 0;
>}

>void print_header(void)
>{
> printf("\n");
> printf("C Programming 1\n");
> printf("Assignment 3 For Terry Ferrarese\n");
> printf("--------------------------------\n");
> printf("\n");
> printf("Value          Value Cubed   \n"); /* 10 spaces between */
> printf("\n");
> printf("-----          -----------         \n");
> printf("\n");

>}

>double cube(float num)      /* takes in a float, returns a double */
>{
> double cubed = 0;
> cubed = num * num  * num ;
> return   cubed;
>}

>void print_result(float num, double cubed_num)
>{
> printf("%-6.5f %15.5f\n", &num, &cubed_num);

You pass a parameter as '&parameter' if you want the function to
change 'parameter'.
printf will never change its parameters, so the & is not needed.
Also, the format string ("%-6.5f %15.5f\n") tells printf what types of
variables to expect next. %f signifies a variable of type float, %lf
says it will be a double.

Quote:

>}
>-----------------end
>According to the watch window, cube seems to be getting and returning
>the proper numbers, function print_result prints only garbage. What am I
>doing wrong here?
>BTW, as this is for school, please don't do my work for me,  I just need
>an idea of what it is I'm doing wrong.
>Thanks to anyone who cares to help
>--


Bart v Ingen Schenau
--



Sat, 30 Mar 2002 03:00:00 GMT  
 help with functions

Quote:

> void print_result(float num, double cubed_num)
> {
>  printf("%-6.5f %15.5f\n", &num, &cubed_num);

> }

printf needs the values themselves, not their addresses.
(Except when printing strings.)

GCC warns about such errors if invoked with the -Wformat option.
Your C compiler may be able to do this too; check its documents.
--



Sat, 30 Mar 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. HELP:Passing functions to functions(!)

2. need your help: strange function declarations

3. ***Newbie Needs Help With Functions Program ***

4. Please help with function calling and string problems

5. help:- similar functions work, why not these?

6. Need help calling functions

7. I need help with function to.......

8. Help C functions needed!

9. ***Newbie Needs Help With Functions Program ***

10. help:- similar functions work, why not these?

11. beed help with function prototpye

12. Help accessing functions from a dll...

 

 
Powered by phpBB® Forum Software