Find average of array

Here given code implementation process.

//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


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.

New Comment







© 2021, kalkicode.com, All rights reserved