3
140
6:22 PM

How To Calculate The Mean Of An Array In Rust

Jan 31st 233 Minutes

How To Calculate The Mean Of An Array In Rust

Basic Knowledge

To be able to calculate the mean of an array or any data, you first have to know how the mean is been calculated. So to simply obtain the value of a mean, you have to add all the values in the data set, then divide the summation of the data set by the number of the data items, inside the data set.

The formula goes like this:

total summation of items in the data set/ number of items in the data set, so let `n` be the nth term, and `x` be the summation of data set values. So let's see the example below for more understanding.

Scores45127689546
Students12669835412

So let's say you want to find out the mean of the data set (Scores), we do this by first, finding the `x` which by adding `45 + 12 + 76 + 89 + 54 + 6`, the total value is represented by `x`, then we count the number of items in the data set, which for our case is `scores` is `6` and it represents `n`. Then we divide it like this (`mean` = `x/n`) to get our mean value for `scores`. And that's it. And the same goes for the `Students` data set.

Coding it in Rust

In order to code the above in Rust, you first need to have a basic understanding of how the mean works and is calculated. So make sure to understand it very well.

Tips: Whenever you want to create something or add functionality to something, it is very much important to know how it works first before you jump to coding it. And remember Google is your friend.

In order to do this in Rust, we first need to put our scores data set inside an array in Rust (check the resources tab to learn more about arrays in Rust).

So let's declare the scores data set in an array in Rust

let arr: [u32; 6] = [45, 12, 76, 89, 54, 6]

This is how arrays look in Rust language, first you bind `arr` to an array of `unsigned integers` with a fixed length of `6`. The array has been explicitly given a type, so in a case where the length of the array is more than `6,` an error gets thrown to the terminal.

Now we have to add all the values in the `arr` data set and then divide by the length of the data set.

In order to get the length of the `arr`, Rust has a built-in function `len()`, which returns an integer value of the length of `arr`.

Now, the way one could add an integer value in an array is by using the `for loop` which is what we will be using. So write the code below and explain.

fn main() {
  // Array declared
    let arr: [u32; 6] = [45, 12, 76, 89, 54, 6];
      
  // Storing the integer value to arr_len
    let arr_len = arr.len() as usize;

  // Created mutable variable to store the total values of the data set
    let mut all_sum_values = 0;

  // After every loop, we add.
    for i in 0..arr_len {
        all_sum_values += arr[i];
    }

  // Creates a variable to store the mean value(integer)
    let mean = all_sum_values as usize / arr_len;

  // Then we print out the output to the terminal.
    println!("{}", mean);
  // Output : 47
  
}
TTdsdsdsd
3 comment
T
March 2, 2023 1:36 PM

B

T
March 2, 2023 1:36 PM

B

dsdsdsd
February 1, 2023 10:13 PM

keep up the good work guy