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

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

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

Disclosure: My content contains affiliate links.

43,637 questions

56,925 answers

573 users

How to calculate the dot product of two arrays in Rust

1 Answer

0 votes
fn calculate_dot_product(arr1: &[i32], arr2: &[i32]) -> i32 {
    let mut product = 0;
    let size = arr1.len();

    for i in 0..size {
        product += arr1[i] * arr2[i];
    }

    product
}

fn main() {
    let arr1 = [1, 4, 8, 9, 6];
    let arr2 = [0, 7, 1, 3, 40];

    // result = 1*0 + 4*7 + 8*1 + 9*3 + 6*40 = 0 + 28 + 8 + 27 + 240 = 303

    println!("Dot product = {}", calculate_dot_product(&arr1, &arr2));
}



  
/*
run:

Dot product = 303

*/

 



answered Sep 5, 2024 by avibootz
edited Oct 23, 2024 by avibootz
...