rustindia / Rust-for-undergrads

C/C++ programming probelms re-written in Rust
https://rustindia.github.io/Rust-for-undergrads/
GNU General Public License v3.0
249 stars 113 forks source link

Pattern #86

Closed Sameer184 closed 5 years ago

Sameer184 commented 5 years ago

use std::io; fn read()->i32 { let mut num=String::new(); io::stdin() .read_line(&mut num) .expect("Error"); num.trim() .parse::() .unwrap() }

fn main() { println!("Enter number of elements of an array"); let n = read() as usize; let mut a = vec!(); println!("Enter elements"); for i in 0..n { let x = read(); a.push(x); } for i in 0..n { if a[0] < a[i] { a[0] = a[i]; } } println!("Largest element = {}",a[0]);

}

Sameer184 commented 5 years ago

Taking an input of number of elements of an array and elements of it then, comparing the each element of an array with the other elements and printing the largest element of an array.