-6

I have this C program that needs to calculate the area of a circle that a user inputs. I have to use this before the main function:

void area_circum(double radius, double *area, double *circum);

I'm having trouble getting the program to work. I'm using the main function and another one called area_circum();

This is what I have right now:

#include <stdio.h>


void area_circum(double radius, double *area, double *circum);

int main(void) {

    double radius, area, circum;

    printf("Please enter the radius of the circle: ");
    scanf("%f", &radius);

    area_circum(radius, area, circum);

    printf("Area of circle : %0.4f\n", area);

    return 0;
}


void area_circum(double radius, double *area, double *circum) {
    double PIE = 3.141;

    double areaC = 0;

    areaC = PIE * radius * radius;
}

When I build it and run it, it works, I input a number, but then it returns 0.00

JDog
  • 5
  • 1
  • 6
  • 2
    please get any text book on C and look at the signature for `main`. Alsio when reading chapter one start on chapter two – Ed Heal Jun 11 '17 at 15:48
  • fix like [this](http://ideone.com/wfNClC) – BLUEPIXY Jun 11 '17 at 15:52
  • Thank you @BLUEPIXY. I see that I should have not passed in anything into the main function parameters. – JDog Jun 11 '17 at 15:54
  • The parameter list to [`main()`](http://stackoverflow.com/questions/204476/) is … well, it's not just unorthodox, it's erroneous. Those parameters should be local variables in `main()` and you should be using `int main(void)`. Also, you want `area` and `circum` to be plain `double`, not `double *`; you pass `&area` and `&circum` to the function — those are pointers. (You['ve revised the signature of `main()` — since there are no answers, that's OK. You didn't heed your compiler warnings. At this stage, if the compiler complains, you've got a bug in your code. Fix it before running.) – Jonathan Leffler Jun 11 '17 at 15:54
  • 2
    `scanf("%f", &radius)` is a type error (what does `scanf` `%f` take? what is the type of `&radius`?). `area_circum` is a no-op: It sets a local variable that is never read, and it ignores two of its arguments. – melpomene Jun 11 '17 at 15:55
  • From the name and signature of that function, it seems that it's intended to calculate both the area and circumference. – interjay Jun 11 '17 at 16:16

3 Answers3

1

It is obvious from the name and signature of that function, that it should calculate both the area and circumference. Also it is obvious that you should pass to it the addresses of two int variables (or two pointers to int) that will then be given the results of the calculations:

#include <stdio.h>

void area_circum(double radius, double *area, double *circum);

int main(void)
{
    double radius, area, circum;

    printf("Please enter the radius of the circle: ");
    scanf("%lf", &radius); // Note: You should add a check to see if
                           //       scanf() failed.
    area_circum(radius, &area, &circum);  // &area is the address of area
                                          // &circum is the address of circum
    printf("Area of circle : %0.4f\n", area);
    printf("Circumference of circle : %0.4f\n", circum);
    return 0;
}

void area_circum(double radius, double *area, double *circum)
{
    double PIE = 3.1416;
    *area = PIE * radius * radius;
    *circum = PIE * radius * 2;
}
SiggiSv
  • 1,180
  • 1
  • 9
  • 18
0

You are nearly there. All you need to do is return the value calculated:

void area_circum(double radius, double *area) {
    double PIE = 3.141;
    *area = PIE * radius * radius;
}

and call like:

    area_circum(radius, &area);
Paul Ogilvie
  • 24,146
  • 4
  • 18
  • 39
-4
#include <stdio.h>

void area_circum(double radius);

int main() {

   double radius;

   printf("Please enter the radius of the circle: ");

   scanf("%lf", &radius);

   area_circum(radius);

   return 0;

}


void area_circum(double radius) {

   double PIE = 3.141;

   double areaC = 0;

   areaC = PIE * radius * radius;

   printf("Area of circle : %0.4f\n", areaC);

}
Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
Aashi
  • 66
  • 1
  • 4
  • ya thanx I didn't noticed that text editor didn't included # – Aashi Jun 11 '17 at 16:07
  • That's one way of doing it. Thanks – JDog Jun 11 '17 at 16:19
  • @melpomene `scanf("%f", &radius);` changed to `scanf("%lf", &radius);` – 4386427 Jun 11 '17 at 16:19
  • 2
    @JDog No, it's not. It changes the signature of `area_circum` that, according to your question, you *have* to use. – melpomene Jun 11 '17 at 16:20
  • 4
    You print the area but not the circumference. Your function does not return either the area or the circumference (let alone both), so you're answering a different problem from the one in the question. There are better approximations to π, too, starting with `3.142` (since the expansion normally goes 3.1415926535897…). – Jonathan Leffler Jun 11 '17 at 16:21