Posted on by Kalkicode
Code Array

# Find average of array

The problem at hand is to find the average of elements in an array. In this context, an array is a data structure that can hold a fixed number of elements of the same data type. The average is calculated by summing up all the elements in the array and then dividing that sum by the total number of elements. This simple task has implications in various fields like statistics, computer science, and everyday programming tasks.

## Problem Statement

Given an array of integers, we need to calculate and output the average of its elements.

## Example

Let's consider two arrays as shown in the code: `arr1` and `arr2`.

`arr1 = [1, 2, 3, 4, 5, 6, 7, 8]` `arr2 = [-1, 1, 6, 8, 7, 4, 6, 5, 9]`

For `arr1`, the sum of all elements is `1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36`, and there are 8 elements in the array. So, the average is `36 / 8 = 4.5`.

For `arr2`, the sum of all elements is `-1 + 1 + 6 + 8 + 7 + 4 + 6 + 5 + 9 = 45`, and there are 9 elements in the array. Therefore, the average is `45 / 9 = 5.0`.

## Idea to Solve

1. Define an array of integers.
2. Calculate the sum of all elements in the array.
3. Divide the sum by the total number of elements to find the average.

## Pseudocode

``````function print_array(arr, size):
for i from 0 to size - 1:
print arr[i]

function find_average(arr, size):
print_array(arr, size)
sum = 0
for i from 0 to size - 1:
sum = sum + arr[i]
average = sum / size
print "Average:", average

arr1 = [1, 2, 3, 4, 5, 6, 7, 8]
size1 = length of arr1
find_average(arr1, size1)

arr2 = [-1, 1, 6, 8, 7, 4, 6, 5, 9]
size2 = length of arr2
find_average(arr2, size2)``````

## Algorithm Explanation

1. The `print_array` function prints the elements of the array.
2. The `find_average` function calculates the average of the given array:
• It calls `print_array` to display the elements.
• Initializes a variable `sum` to store the sum of the array elements.
• It then iterates through each element of the array, adding its value to the `sum`.
• After the loop, it calculates the average by dividing `sum` by the size of the array.
• The average is printed.

## Code Solution

``````//C Program
//Find average of array
#include <stdio.h>

//Print array element
void print_array(int arr[], int size)
{
printf("\n");
for (int i = 0; i < size; i++)
{
printf(" %d ", arr[i]);
}
}
//Calculate average of given array
void find_average(int arr[], int size)
{
print_array(arr, size);
int sum = 0;
for (int i = 0; i < size; i++)
{
//sum the array elements
sum += arr[i];
}
// Get average
double average = (double) sum / size;
printf("\nAverage : %lf\n", average);
}
int main()
{
// Define the array elements
int arr1[] = {
1,
2,
3,
4,
5,
6,
7,
8
};
// Find the size
int size = sizeof(arr1) / sizeof(arr1[0]);
find_average(arr1, size);
// Define the array elements
int arr2[] = {
-1,
1,
6,
8,
7,
4,
6,
5,
9
};
// Find the size
size = sizeof(arr2) / sizeof(arr2[0]);
find_average(arr2, size);
return 0;
}``````

#### Output

`````` 1  2  3  4  5  6  7  8
Average : 4.500000

-1  1  6  8  7  4  6  5  9
Average : 5.000000``````
``````/*
Java Program
Find the average of array
*/
class MyArray
{
//Print array element
public void print_array(int[] arr, int size)
{
System.out.print("\n");
for (int i = 0; i < size; i++)
{
System.out.print(" " + arr[i] + " ");
}
}
//Calculate average of given array
public void find_average(int[] arr, int size)
{
print_array(arr, size);
int sum = 0;
for (int i = 0; i < size; i++)
{
//sum the array elements
sum += arr[i];
}
// Get average
double average = (double) sum / size;
System.out.print("\nAverage : " + average + "\n");
}
public static void main(String[] args)
{
MyArray obj = new MyArray();
// Define the array elements
int[] arr1 = {
1,
2,
3,
4,
5,
6,
7,
8
};
// Find the size
int size = arr1.length;
obj.find_average(arr1, size);
// Define the array elements
int[] arr2 = {
-1,
1,
6,
8,
7,
4,
6,
5,
9
};
// Find the size
size = arr2.length;
obj.find_average(arr2, size);
}
}``````

#### Output

`````` 1  2  3  4  5  6  7  8
Average : 4.5

-1  1  6  8  7  4  6  5  9
Average : 5.0``````
``````/*
C++ Program
Find the average of array
*/
#include<iostream>

using namespace std;
class MyArray
{
public:
//Print array element
void print_array(int arr[], int size)
{
cout << "\n";
for (int i = 0; i < size; i++)
{
cout << " " << arr[i] << " ";
}
}
//Calculate average of given array
void find_average(int arr[], int size)
{
this->print_array(arr, size);
int sum = 0;
for (int i = 0; i < size; i++)
{
//sum the array elements
sum += arr[i];
}
// Get average
double average = (double) sum / size;
cout << "\nAverage : " << average << "\n";
}
};
int main()
{
MyArray obj ;
int arr1[] = {
1,
2,
3,
4,
5,
6,
7,
8
};
// Find the size
int size = sizeof(arr1) / sizeof(arr1[0]);
obj.find_average(arr1, size);
int arr2[] = {
-1,
1,
6,
8,
7,
4,
6,
5,
9
};
// Find the size
size = sizeof(arr2) / sizeof(arr2[0]);
obj.find_average(arr2, size);
return 0;
}``````

#### Output

`````` 1  2  3  4  5  6  7  8
Average : 4.5

-1  1  6  8  7  4  6  5  9
Average : 5``````
``````/*
C# Program
Find the average of array
*/
using System;
class MyArray
{
//Print array element
public void print_array(int[] arr, int size)
{
Console.Write("\n");
for (int i = 0; i < size; i++)
{
Console.Write(" " + arr[i] + " ");
}
}
//Calculate average of given array
public void find_average(int[] arr, int size)
{
print_array(arr, size);
int sum = 0;
for (int i = 0; i < size; i++)
{
//sum the array elements
sum += arr[i];
}
// Get average
double average = (double) sum / size;
Console.Write("\nAverage : " + average + "\n");
}
public static void Main(String[] args)
{
MyArray obj = new MyArray();
int[] arr1 = {
1,
2,
3,
4,
5,
6,
7,
8
};
// Find the size
int size = arr1.Length;
obj.find_average(arr1, size);
int[] arr2 = {
-1,
1,
6,
8,
7,
4,
6,
5,
9
};
// Find the size
size = arr2.Length;
obj.find_average(arr2, size);
}
}``````

#### Output

`````` 1  2  3  4  5  6  7  8
Average : 4.5

-1  1  6  8  7  4  6  5  9
Average : 5``````
``````<?php
/*
Php Program
Find the average of array
*/
class MyArray
{
//Print array element
function print_array( & \$arr, \$size)
{
echo "\n";
for (\$i = 0; \$i < \$size; \$i++)
{
echo " ". \$arr[\$i] ." ";
}
}
//Calculate average of given array
function find_average( & \$arr, \$size)
{
\$this->print_array(\$arr, \$size);
\$sum = 0;
for (\$i = 0; \$i < \$size; \$i++)
{
//sum the array elements
\$sum += \$arr[\$i];
}
// Get average
\$average = (\$sum) / \$size;
echo "\nAverage : ". \$average ."\n";
}
}

function main()
{
\$obj = new MyArray();
// Define the array elements
\$arr1 = array(1, 2, 3, 4, 5, 6, 7, 8);
// Find the size
\$size = count(\$arr1);
\$obj->find_average(\$arr1, \$size);
// Define the array elements
\$arr2 = array(-1, 1, 6, 8, 7, 4, 6, 5, 9);
// Find the size
\$size = count(\$arr2);
\$obj->find_average(\$arr2, \$size);
}
main();``````

#### Output

`````` 1  2  3  4  5  6  7  8
Average : 4.5

-1  1  6  8  7  4  6  5  9
Average : 5``````
``````/*
Node Js Program
Find the average of array
*/
class MyArray
{
//Print array element
print_array(arr, size)
{
process.stdout.write("\n");
for (var i = 0; i < size; i++)
{
process.stdout.write(" " + arr[i] + " ");
}
}
//Calculate average of given array
find_average(arr, size)
{
this.print_array(arr, size);
var sum = 0;
for (var i = 0; i < size; i++)
{
//sum the array elements
sum += arr[i];
}
// Get average
var average = (sum) / size;
process.stdout.write("\nAverage : " + average + "\n");
}
}

function main()
{
var obj = new MyArray();
// Define the array elements
var arr1 = [1, 2, 3, 4, 5, 6, 7, 8];
// Find the size
var size = arr1.length;
obj.find_average(arr1, size);
// Define the array elements
var arr2 = [-1, 1, 6, 8, 7, 4, 6, 5, 9];
// Find the size
size = arr2.length;
obj.find_average(arr2, size);
}
main();``````

#### Output

`````` 1  2  3  4  5  6  7  8
Average : 4.5

-1  1  6  8  7  4  6  5  9
Average : 5``````
``````#   Python 3 Program
#   Find the average of array

class MyArray :
# Print array element
def print_array(self, arr, size) :
print("\n", end = "")
i = 0
while (i < size) :
print(" ", arr[i] ," ", end = "")
i += 1

# Calculate average of given array
def find_average(self, arr, size) :
self.print_array(arr, size)
sum = 0
i = 0
while (i < size) :
# sum the array elements
sum += arr[i]
i += 1

#  Get average
average = (sum) / size
print("\nAverage : ", average ,"\n", end = "")

def main() :
obj = MyArray()
#  Define the array elements
arr1 = [1, 2, 3, 4, 5, 6, 7, 8]
#  Find the size
size = len(arr1)
obj.find_average(arr1, size)
#  Define the array elements
arr2 = [-1, 1, 6, 8, 7, 4, 6, 5, 9]
#  Find the size
size = len(arr2)
obj.find_average(arr2, size)

if __name__ == "__main__": main()``````

#### Output

``````  1    2    3    4    5    6    7    8
Average :  4.5

-1    1    6    8    7    4    6    5    9
Average :  5.0``````
``````#   Ruby Program
#   Find the average of array

class MyArray

# Print array element
def print_array(arr, size)

print("\n")
i = 0
while (i < size)

print(" ", arr[i] ," ")
i += 1
end
end
# Calculate average of given array
def find_average(arr, size)

self.print_array(arr, size)
sum = 0
i = 0
while (i < size)

# sum the array elements
sum += arr[i]
i += 1
end
#  Get average
average = (sum).to_f / size
print("\nAverage : ", average ,"\n")
end
end
def main()

obj = MyArray.new()
#  Define the array elements
arr1 = [1, 2, 3, 4, 5, 6, 7, 8]
#  Find the size
size = arr1.length
obj.find_average(arr1, size)
#  Define the array elements
arr2 = [-1, 1, 6, 8, 7, 4, 6, 5, 9]
#  Find the size
size = arr2.length
obj.find_average(arr2, size)
end
main()``````

#### Output

`````` 1  2  3  4  5  6  7  8
Average : 4.5

-1  1  6  8  7  4  6  5  9
Average : 5.0
``````
``````/*
Scala Program
Find the average of array
*/
class MyArray
{
//Print array element
def print_array(arr: Array[Int], size: Int): Unit = {
print("\n");
var i: Int = 0;
while (i < size)
{
print(" " + arr(i) + " ");
i += 1;
}
}
//Calculate average of given array
def find_average(arr: Array[Int], size: Int): Unit = {
print_array(arr, size);
var sum: Int = 0;
var i: Int = 0;
while (i < size)
{
//sum the array elements
sum += arr(i);
i += 1;
}
// Get average
var average: Double = sum.toFloat / size;
print("\nAverage : " + average + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyArray = new MyArray();
// Define the array elements
var arr1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8);
// Find the size
var size: Int = arr1.length;
obj.find_average(arr1, size);
// Define the array elements
var arr2: Array[Int] = Array(-1, 1, 6, 8, 7, 4, 6, 5, 9);
// Find the size
size = arr2.length;
obj.find_average(arr2, size);
}
}``````

#### Output

`````` 1  2  3  4  5  6  7  8
Average : 4.5

-1  1  6  8  7  4  6  5  9
Average : 5.0``````
``````/*
Swift Program
Find the average of array
*/
class MyArray
{
//Print array element
func print_array(_ arr: [Int], _ size: Int)
{
print("\n", terminator: "");
var i: Int = 0;
while (i < size)
{
print(" ", arr[i] ," ", terminator: "");
i += 1;
}
}
//Calculate average of given array
func find_average(_ arr: [Int], _ size: Int)
{
self.print_array(arr, size);
var sum: Int = 0;
var i: Int = 0;
while (i < size)
{
//sum the array elements
sum += arr[i];
i += 1;
}
// Get average
let average: Double = Double(sum) / Double(size);
print("\nAverage : ", average ,"\n", terminator: "");
}
}
func main()
{
let obj: MyArray = MyArray();
// Define the array elements
let arr1: [Int] = [1, 2, 3, 4, 5, 6, 7, 8];
// Find the size
var size: Int = arr1.count;
obj.find_average(arr1, size);
// Define the array elements
let arr2: [Int] = [-1, 1, 6, 8, 7, 4, 6, 5, 9];
// Find the size
size = arr2.count;
obj.find_average(arr2, size);
}
main();``````

#### Output

``````  1    2    3    4    5    6    7    8
Average :  4.5

-1    1    6    8    7    4    6    5    9
Average :  5.0``````

## Time Complexity

The time complexity of this algorithm is O(n), where n is the number of elements in the array. This is because both the sum calculation loop and the array printing loop iterate through the array once, and their combined time complexity is linear in terms of the array size.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post