2017-06-11 4 views
-6

Ich habe dieses C-Programm, das den Bereich eines Kreises berechnen muss, den ein Benutzer eingibt. Ich habe diese zu verwenden, bevor die Hauptfunktion:Wie würde ich Funktion einrichten, um die Fläche eines Kreises zu berechnen - C

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

Ich habe Probleme mit dem Programm bekommen zu arbeiten. Ich benutze die Hauptfunktion und eine andere namens area_circum();

Das ist, was ich jetzt haben:

#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; 
} 

Wenn ich es bauen und führen Sie es, es funktioniert, habe ich eine Nummer eingegeben, aber dann gibt es 0,00

+2

Bitte setzen Sie sich einen beliebigen Text Buch die Unterschrift für 'main'. Alsio beim Lesen von Kapitel eins Start auf Kapitel zwei –

+0

reparieren wie [dies] (http://ideone.com/wfNClC) – BLUEPIXY

+0

Vielen Dank @BLUEPIXY. Ich sehe, dass ich nichts in die Hauptfunktionsparameter eingegeben haben sollte. – JDog

Antwort

-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); 

} 
+0

ya danke Ich habe nicht bemerkt, dass Texteditor nicht enthalten # – Aashi

+0

Das ist eine Möglichkeit, es zu tun. Danke – JDog

+0

@melpomene 'scanf ("% f ", & radius);' geändert zu 'scanf ("% lf ", & radius);' – 4386427

0

Sie sind fast Dort. Alles, was Sie tun müssen, ist, den Wert berechnet zurückgeben:

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

und rufen wie:

area_circum(radius, &area); 
1

Aus dem Namen und Unterschrift dieser Funktion liegt auf der Hand, dass es sowohl die Fläche und Umfang berechnen soll . es ist auch offensichtlich, dass Sie es die Adressen von zwei int-Variablen (oder zwei Zeiger auf int), die dann gegeben werden, um die Ergebnisse der Berechnungen passieren sollte:

bei auf C und schauen
#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; 
} 
Verwandte Themen