Зміст курсу
Основи C#
Основи C#
Практика індексування
Знайдіть середнє між найбільшим і найменшим елементом масиву numbers
.
Середнє, також зване середнім арифметичним, представляє центральне число між двома або більше числами. Наприклад, середнє значення між 1
і 5
дорівнює 3
.
Формула для знаходження середнього між двома числами: сума двох чисел, поділена на 2: (num1 + num2) / 2
Використовуйте індексацію для доступу до найменших і найбільших елементів масиву.
Use indexing to access the smallest and the largest elements of the array.
main
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[] numbers = { 5, 9, 27, 17, 19, 21, 0, -7, 10 }; int sum = ___; int mean = ___; Console.WriteLine(mean); } } }
The
sum
variable should contain the sum of the two values.Figure out the index of the smallest and the largest elements of the
numbers
array and access those elements via indexing (numbers[index]
), then store their sum in thesum
variable.The
mean
will be the sum divided by 2.
main
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[] numbers = { 5, 9, 27, 17, 19, 21, 0, -7, 10 }; int sum = numbers[2] + numbers[7]; int mean = sum / 2; Console.WriteLine(mean); } } }
Дякуємо за ваш відгук!