paint-brush
Calculating the Mean of an Array in Rust by@rockyessel
2,137 reads
2,137 reads

Calculating the Mean of an Array in Rust

by Rocky EsselJune 19th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

To calculate the mean of an array or any data, you first have to know how the mean is calculated. So to simply obtain the value of a mean, you have to add all the values in the data set. Rust has a built-in function `len()` which returns an integer value of the length of the array. To code the above in Rust, we first need to put our scores data set inside an array.
featured image - Calculating the Mean of an Array in Rust
Rocky Essel HackerNoon profile picture


Basic Knowledge

To calculate the mean of an array or any data, you first have to know how the mean is 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 divided by the number of items in the data set, so let n be the nth term (which is the number of items in the array) and x (be the summation of data set values). So let's see the example below for more understanding.

Scores

45

12

76

89

54

6

Students

12

66

98

3

54

12


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 in our case 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 Student’s data set.


Coding it in Rust

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.


To do this in Rust, we first need to put our scores data set inside an array 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 into 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.


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.

fn main() {
  // Array declared
    let arr: [u32; 6] = [45, 12, 76, 89, 54, 6];
      
  // Storing the length of the arr
    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;

  // We add each element to the all_sum_values 
    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
  
}


Explanation:

What we are doing, is creating a variable called arr_len to store the length of the array.


Now note this, you don’t have to be necessary store the length of the array because there another way to do this, one is to declare a variable called count, then after each loop, we increase the count by one to the time when the total array values have been added, we then divide the total values by the total total count number, then we get the same answer. I will give an example lastly.


Then created a mutable variable called all_sum_values to store the total value of the arr after we loop through each value within the range of 0 to the arr_len, then add each value by accessing their position, and adding them to all_sum_values until the condition i in 0..arr_len is no longer true. Then store the value(answer) to mean and print it out.

And we are done!


Here is the example code I promised😊.

fn main() {
    // Array declared
    let arr: [u32; 6] = [45, 12, 76, 89, 54, 6];

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

    // Created mutable variable to store the count of data items in the array
    let mut count = 0;

    // We add each element to the all_sum_values and increment the count
    for &num in &arr {
        all_sum_values += num;
        count += 1;
    }

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

    // Then we print out the output to the terminal.
    println!("{}", mean);
    // Output: 47
}


Conclusion

If you’re learning Rust, don’t give up, I won’t say it is easy even though I do have 1.5 years of programming experience with JavaScript. In case you’re wondering, I’m still on the path of becoming a Software Engineer, and if you want to say hi or ask me about something feel free to reach me on Twitter, Linkedin, Facebook, and Instagram as rockyessel. And also hope you understood it. Great happy coding and learning.


Also published here.