218k views
0 votes
Write a function that will read up an array of 5 numbers and write the numbers back to the screen in the reverse order. For example, if the input numbers are (enter one-by-one):

9
6
-2
0
10
Then the output should be
10 0-2 6 9
The 5 numbers are collected from the keyboard using a function.

User MadSkunk
by
8.3k points

1 Answer

6 votes

In Rust:

use std::io;

fn main() -> Result<(), io::Error> {

let mut array_nums: Vec<i32> = Vec::new();

while array_nums.len() < 5 {

let mut user_input = String::new();

println!("Enter a number (press x to end): ");

io::stdin().read_line(&mut user_input)?;

if user_input == String::from("x") {

break;

}

array_nums.push(user_input.trim().parse::<i32>().unwrap());

}

array_nums.reverse();

println!("{:?}", array_nums);

Ok(())

}

In Python:

array_nums = []

while len(array_nums) < 5:

user_input = input("Enter a number (press x to end): ")

if user_input == "x":

break

array_nums.append(int(user_input))

print(array_nums[::-1])

User Mojtaba Hosseini
by
8.3k points

No related questions found