diff options
author | Adam T. Carpenter <atc@53hor.net> | 2023-07-17 22:44:41 -0400 |
---|---|---|
committer | Adam T. Carpenter <atc@53hor.net> | 2023-07-17 22:44:41 -0400 |
commit | 738d22188695226cf1d9efe47da4114f43da1b93 (patch) | |
tree | 6684af3efd4e4d58a22fcad374759e09143bb4c0 | |
parent | cbfbfcf897552f3f162572d6c3fa50b84e26e3e1 (diff) | |
download | cps-rust-master.tar.xz cps-rust-master.zip |
-rw-r--r-- | 02/circumference.rs | 20 | ||||
-rwxr-xr-x | 02/readme.md | 87 |
2 files changed, 107 insertions, 0 deletions
diff --git a/02/circumference.rs b/02/circumference.rs new file mode 100644 index 0000000..a27da44 --- /dev/null +++ b/02/circumference.rs @@ -0,0 +1,20 @@ +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}."); +} diff --git a/02/readme.md b/02/readme.md new file mode 100755 index 0000000..a1f87b9 --- /dev/null +++ b/02/readme.md @@ -0,0 +1,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}."); +} +``` |