summaryrefslogtreecommitdiff
path: root/02/readme.md
blob: a1f87b9f5f0b890951f5bc3232f307b63e76702c (plain) (blame)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
---
author: Adam T. Carpenter, Carpenter Tutoring
---

# Computational problem solving with _Rust_

## Starting to program

```
~~~fsays
Starting to practice!
~~~
```

[practice of computing using python](https://www.pearson.com/en-us/subject-catalog/p/practice-of-computing-using-python-the/p200000003329/9780137524839)

[rust book](https://doc.rust-lang.org/book/)

---

# Getting started

We need to dive into language issues in order to work through problem-solving
issues. Recall:

## 1. Think before you program

## 2. A program is a human-readable essay on problem solving (that also runs on a computer)

---

# Practice!

Learning to experiment is key to learning to program. If you have a question,
try it out!

Time for a new rule:

## 3. The best way to improve your programming an problem skills is to practice!

---

# Calculate the circumference and area of a circle given its radius

Formulas:

```
circumference = 2 * π * radius
area = π * radius ^ 2
```

Requirements:

1. We need to prompt the user for a radius

TODO:

---

# Calculate the circumference and area of a circle given its radius

```bash
tmux display-popup 'rustc ./circumference.rs && ./circumference'
```

```rust
use std::f64::consts::PI;
use std::io::stdin;

// Calculate the area and circumference of a circle from its radius.
// 1. Prompt for a radius input.
// 2. Parse the input into a numeric radius.
// 3. Apply the area and circumference formulas.
// 4. Print out the results.
fn main() {
    let mut input = String::new();
    println!("Enter the radius of your circle: ");
    stdin().read_line(&mut input).expect("No radius given.");

    let radius: f64 = input.trim().parse().expect("Invalid radius given.");

    let circumference = 2.0 * PI * radius;
    let area = PI * radius.powi(2);

    println!("The circumference is: {circumference}, and the area is: {area}.");
}
```