reciprocal calculator in c

Discussion in 'Programmer's Corner' started by jonathanf, Oct 31, 2010.

  1. jonathanf

    Thread Starter New Member

    Oct 31, 2010
    1
    0
    Hi all,
    Im wondering if I can get some pointers.
    I have an assignment for basic "c" programming , a reciprocal calculator with conditions.The conditions being : if entry =0 then print "reciprocal undefined" (this bit I can get!). else (should be) print "the reciprocal = 1/(number entered) or (decimal equivelent). (this bit is frying my head).
    please help. this is what I have so far

    // reciprocal calculator
    // by jf
    // date last modified 31/10/10

    // include standard input/output from header file h
    #include <stdio.h>

    // include standard library from header file h
    #include <stdlib.h>

    // main function heading
    main ()

    // begining of instructions
    {

    // declare variables and set initial value to zero
    float r=0,a=0,b=0;

    // print on screen enter number
    printf("Enter number:",&a); (this bit works ok)

    // scan for input
    scanf("%f",&b); (as does this)

    // manipulate variables
    r=(1/b); (not sure if im doing this bit right)

    // if conditions
    if (b==0) printf("Reciprocal undefined\n"); (this bit also works)

    // otherwise do this
    else printf("the reciprocal of,b,is = 1/,b,or %f\n",&r); :)():)mad:)!!!

    // pause to view answer
    system("PAUSE");
    }
     
    Last edited: Oct 31, 2010
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Using the &, you are sending the memory address of the variable r in the printf statement.

    printf

    Pointers are typically used for strings.

    Try this:
    Code ( (Unknown Language)):
    1.  
    2. #include <stdio.h>
    3.  
    4. // include standard library from header file h
    5. #include <stdlib.h>
    6.  
    7. // main function heading
    8. main ()
    9.  
    10.         // begining of instructions
    11. {
    12.  
    13.         // declare variables and set initial value to zero
    14.         float r=0,b=0;
    15.  
    16.         // print on screen enter number
    17.         printf("Enter number:");
    18.  
    19.         // scan for input
    20.         scanf("%f",&b);
    21.  
    22.         // manipulate variables
    23.         r=(1/b);
    24.  
    25.         // if conditions
    26.         if (b==0) printf("Reciprocal undefined\n");
    27.  
    28.         // otherwise do this
    29.         else printf("the reciprocal of %f, 1/%f is %f\n",b,b,r);
    30. }
    31.  
    Code above compiled and ran on a linux system with no errors.

    The float a is not needed in the first printf, and is not used elsewhere in the program.


    Output:

    Code ( (Unknown Language)):
    1.  
    2. Enter number:34
    3. the reciprocal of 34.000000, 1/34.000000 is 0.029412
    4. Enter number:0
    5. Reciprocal undefined
    6. Enter number:0.3333333333333333333333333333
    7. the reciprocal of 0.333333, 1/0.333333 is 3.000000
    8. Enter number:-1293
    9. the reciprocal of -1293.000000, 1/-1293.000000 is -0.000773
    10.  
     
    Last edited: Oct 31, 2010
    jonathanf likes this.
Loading...