Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,970 questions

51,912 answers

573 users

How to find the maximum value in a multidimensional vector with Rust

2 Answers

0 votes
fn find_max_in_multidimensional_vector(multivec: &Vec<Vec<f64>>) -> f64 {
    let mut max_value = f64::MIN; // Initialize with the smallest possible value for f64
 
    for row in multivec {
        for &value in row {
            if value > max_value {
                max_value = value; // Update max_value if a larger value is found
            }
        }
    }
 
    max_value
}
 
fn main() {
    // Define a multidimensional array
    let multivec = vec![
        vec![1.0, 2.0,  3.14],
        vec![1.0, 1.0, 16.80],
        vec![3.0, 5.0, 17.50],
        vec![2.0, 4.0, 11.03],
    ];
 
    let max_value = find_max_in_multidimensional_vector(&multivec);
 
    println!("The maximum value in the array is: {:.2}", max_value);
}
 
 
       
/*
run:
 
The maximum value in the array is: 17.50
      
*/

 



answered Apr 5, 2025 by avibootz
edited Apr 6, 2025 by avibootz
0 votes
fn find_max_in_multidimensional_vector(multivec: &Vec<Vec<f64>>) -> Option<f64> {
    if multivec.is_empty() {
        return None;
    }

    let mut max_value = multivec[0][0]; // Initialize with the first element

    for row in multivec {
        for &value in row {
            if value > max_value {
                max_value = value;
            }
        }
    }

    Some(max_value)
}

fn main() {
    // Define a multidimensional array
    let multivec = vec![
        vec![1.0, 2.0,  3.14],
        vec![1.0, 1.0, 16.80],
        vec![3.0, 5.0, 17.50],
        vec![2.0, 4.0, 11.03],
    ];
    
    if let Some(max_val) = find_max_in_multidimensional_vector(&multivec) {
        println!("Maximum value: {}", max_val); 
    } else {
        println!("multivec is empty.");
    }

    let empty_multivec: Vec<Vec<f64>> = vec![];
    if let Some(max_val) = find_max_in_multidimensional_vector(&empty_multivec) {
        println!("Maximum value: {}", max_val);
    } else {
        println!("multivec is empty."); 
    }
}

 
       
/*
run:
 
Maximum value: 17.5
multivec is empty.
      
*/

 



answered Apr 6, 2025 by avibootz
...