Activity 9 - Loops, Functions, and Variables Review

Group members:

Part 1: Fill-in-the-blanks

Problem 1

fn find_max(numbers: [i32]) -> _______ {
    let mut max = numbers[0];
    for _______ in _______ {
        if _______ > max {
            max = _______;
        }
    }
    _______
}

fn main() {
    let scores = [85, 92, 78, 96, 88];
    let highest = find_max(_______);
    println!("Highest score: {}", highest);
}

Problem 2

fn count_even_numbers(limit: u32) -> u32 {
    let mut count = 0;
    for i in _______ {
        if i % 2 _______ {
            count _______;
        }
    }
    count
}

fn main() {
    let result = count_even_numbers(10);
    println!("Even numbers from 1 to 10: {}", _______);
}

Problem 3

#![allow(unused)]
fn main() {
fn find_pair_sum(target: i32) -> _______ {
    let numbers = [1, 3, 5, 7, 9, 2, 4, 6];

    for (i, &first) in numbers.iter()._______ {
        for j in _______..numbers.len() {
            if first + _______ == _______ {
                return (first, numbers[j]);
            }
        }
    }
    (0, 0)  // Not found
}
}

Problem 4

#![allow(unused)]
fn main() {
fn try_to_set_a_high_score() -> u32 {
    let mut personal_best = 100;
    let mut lives_left = 3;

    _______ personal_best <= 210 _______ lives_left > 0 {
        personal_best _______ 25;  // You get a little better every time!
        lives_left _______;
        println!("Score: {}, Lives left: {}", _______, _______);
    }

    _______ personal_best _______ {
        println!("High score achieved!");
    } _______ {
        println!("Try again later");
    }

    _______
}
}

Part 2: What Does This Print?

Problem 1

fn main() {
    'outer: for x in 1..=4 {
        'inner: for y in 1..=3 {
            if x * y == 6 {
                break 'outer;
            }
            if x + y == 5 {
                continue 'outer;
            }
        }
        println!("Finished inner loop for x = {}", x);
    }
}

Problem 2

#![allow(unused)]
fn main() {
let mut result = [0; 3];
let data = [10, 20, 30, 40, 50];

for (i, &value) in data[1..4].iter().enumerate() {
    result[i] = value / 10;
}
println!("{:?}", result);
}

Problem 3

#![allow(unused)]
fn main() {
let mut x = 0;
for i in 1..=3 {
    x += i;
}
println!("{}", x);
}

Problem 4

#![allow(unused)]
fn main() {
for i in (0..5).step_by(2) {
    if i == 2 {
        continue;
    }
    println!("{}", i);
}
}

Part 3: Quick Quiz

  1. Which loop type should you use when you don't know how many iterations you need? (Could be more than one)





  1. Which is a correct function signature for a function is_positive that takes an integer and returns whether it's positive?





  1. What command creates a rust project, including Cargo.lock and src/main.rs?





  1. What shell command lists all files in the current directory, including hidden files?





  1. What git command would you use to add all modified files to the staging area?





  1. What shell command would you use to move to your home directory?





Part 4: Debug the Code

Problem 1 (3 bugs):

#![allow(unused)]
fn main() {
fn calculate_average(numbers: [f64]) -> f64 {
    let mut sum = 0;
    for num in numbers {
        sum += num;
    }
    sum / numbers.len()
}
}

Bug 1: ________________________________________________

Fix 1: ________________________________________________

Bug 2: ________________________________________________

Fix 2: ________________________________________________

Bug 3: ________________________________________________

Fix 3: ________________________________________________

Problem 2:

#![allow(unused)]
fn main() {
let arr = [1, 2, 3];
for i in 0..arr.len() {
    arr[i] = arr[i] * 2;
}
}

Bug: ________________________________________________

Fix: ________________________________________________

Problem 3 (2 bugs):

#![allow(unused)]
fn main() {
fn find_first_even(numbers: [u32; 5]) -> u32 {
    for num in numbers {
        if num % 2 = 0 {
            return num;
        }
    }
    return -1; // indicating no even numbers found
}
}

Bug 1: ________________________________________________

Fix 1: ________________________________________________

Bug 2: ________________________________________________

Fix 2: ________________________________________________