Search
 
SCRIPT & CODE EXAMPLE
 

RUST

Transpose matrix, pass-by-reference to function

// Transpose matrix, pass-by-reference to function

fn transpose_matrix(matrix: &mut Vec<Vec<i32>>) {
    let last = matrix.len();
    for y in 0..last {
        for x in y..last {
            let tmp = matrix[y][x];
            matrix[y][x] = matrix[x][y];
            matrix[x][y] = tmp; 
        }
    }
} 

fn print_vector(vector: &Vec<Vec<i32>>) {
    println!("v {:?}", vector)
}

fn main() {
    // Two dimensional vector
    let mut v5 = vec![
        vec![11, 12, 13, 14, 15],
        vec![21, 22, 23, 24, 25],
        vec![31, 32, 33, 34, 35],
        vec![41, 42, 43, 44, 45],
        vec![51, 52, 53, 54, 55],
    ];
    println!("5 x 5 vector");
    // &v5 is how the vector is passed by reference
    print_vector(&v5);
    // Passed by reference with intention to make changes
    transpose_matrix(&mut v5);
    println!("Transposed");
    print_vector(&v5);
}
Comment

PREVIOUS NEXT
Code Example
Rust :: rust•armanriazi•trait•PartialEq 
Rust :: armanriazi•rust•thread•channel 
Rust :: rust calculate every root 
Rust :: primitive data types in rust 
Rust :: rust•armanriazi•concept•nan 
Rust :: rustlang get substring 
Lua :: lua string.split 
Lua :: lua how to get random object from a table 
Lua :: how to comment multiple lines in lua 
Lua :: lua calculate average number 
Lua :: open popup windows lua 
Lua :: luau loop players 
Lua :: roblox studio lua for loop 
Lua :: roblox key pressed script 
Lua :: string to int lua 
Lua :: lua json 
Lua :: what is lua used for 
Lua :: how to access an index of a table lua 
Lua :: lua print table 
Lua :: pcall lua 
Lua :: how to make auto scroll roblox 
Lua :: Lua dynamic variable name 
Lua :: how to make kill block in roblox lua 
Matlab :: matlab symbolic set value 
Matlab :: Load .mat data in Matlab 
Basic :: pmatplotlib draw a square with a magenta dotted line and pentagon markersython matplotlib overlaped 
Basic :: visual basic non modal message box 
Elixir :: elixir get_in access all 
Scala :: scala enum 
Scala :: get last index of list scala 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =