Last active
January 3, 2016 00:19
-
-
Save sunny1304/8381978 to your computer and use it in GitHub Desktop.
get avarage of the number argumants
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{os}; | |
fn args_avg(vec: &[~str]) -> int { | |
let mut sum = 0; | |
let mut count = 0; | |
for i in vec.iter(){ | |
let x:Option<int> = from_str(*i); | |
if x != None{ | |
sum += x.unwrap(); | |
count += 1; | |
} | |
} | |
if count > 0 { | |
return (sum/count) ; | |
} | |
return 0; | |
} | |
fn catch_bad_args(vec:&[~str]) -> (){ | |
print!("Bad args: ") | |
for i in vec.iter(){ | |
let x:Option<int> = from_str(*i); | |
if x == None { | |
print!("{}", *i); | |
} | |
} | |
println!(""); | |
} | |
fn main(){ | |
let user_args = os::args(); | |
let args_vec = user_args.slice(1, user_args.len()); | |
for user_arg in args_vec.iter(){ | |
print!("{} ", *user_arg); | |
} | |
println(""); | |
catch_bad_args(args_vec); | |
println!("Avarage: {:?}",args_avg(args_vec)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment