This repository was archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpizza-2.c
More file actions
43 lines (35 loc) · 1.23 KB
/
pizza-2.c
File metadata and controls
43 lines (35 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//source video: https://www.youtube.com/watch?v=F5feTW3CAZs&index=4&list=PLhQjrBD2T381wyZt81eGNZuZ4rzOos-AF
#include <stdio.h>
#include <cs50.h>
// If we initialize pi out here, at the global scope, it will be accessible everywhere.
float pi = 3.141592654;
// BUT! this is generally a bad idea. What if some piece of code somewhere changes the value pi?
float area(float radius);
float circumference(float diameter);
int main (void)
{
// Get the size of the pizza from the user
printf("What's the diameter of the pizza? ");
float pizza_diameter = GetFloat();
// Calculate circumference and area of the pizza
float pizza_circumference = circumference(pizza_diameter);
float pizza_area = area( pizza_diameter / 2.0 );
// Tell the user all about their pizza
printf("The pizza is %f inches around.\n", pizza_circumference);
printf("The pizza has %f square inches.\n", pizza_area);
}
/*
* returns the area of a circle with a given radius
*/
float area(float radius)
{
// now we can just use pi here, because we declared it globally (see top of file)
return pi * (radius * radius); // pi r squared
}
/*
* returns the circumference of a circle with a given diameter
*/
float circumference(float diameter)
{
return pi * diameter;
}