Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Min, Max, Sum, and Average | Section
C# Arrays

bookMin, Max, Sum, and Average

メニューを表示するにはスワイプしてください

When working with arrays, you often need to perform basic calculations such as finding the smallest (minimum) or largest (maximum) value, as well as computing the sum and average of all elements. These operations are common in many real-world scenarios: you might want to find the lowest score in a set of test results, determine the highest temperature recorded during the week, or compute the average sales for a store. Understanding how to efficiently perform these calculations on arrays is an essential programming skill.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = { 7, 3, 15, 2, 8, 10 }; int min = numbers[0]; int max = numbers[0]; for (int i = 1; i < numbers.Length; i++) { if (numbers[i] < min) { min = numbers[i]; } if (numbers[i] > max) { max = numbers[i]; } } Console.WriteLine("Minimum value: " + min); Console.WriteLine("Maximum value: " + max); } } }

To find the minimum and maximum values in an integer array, you start by assuming that the first element is both the smallest and largest. You then loop through the rest of the array, comparing each element to your current min and max. If you find a value smaller than your current min, you update min; if you find a value larger than your current max, you update max. This approach ensures you check every element and always end up with the true minimum and maximum, as shown in the code above.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { double[] prices = { 9.99, 14.50, 7.25, 12.00, 8.75 }; double sum = 0; for (int i = 0; i < prices.Length; i++) { sum += prices[i]; } double average = 0; if (prices.Length > 0) { average = sum / prices.Length; } Console.WriteLine("Sum: " + sum); Console.WriteLine("Average: " + average); } } }
Note
Definition

Average is the sum of all elements divided by the number of elements.

1. What is the initial value you should use when searching for a minimum in an array?

2. How do you calculate the average of an array of numbers?

3. Why is it important to check for empty arrays before performing calculations?

question mark

What is the initial value you should use when searching for a minimum in an array?

正しい答えを選んでください

question mark

How do you calculate the average of an array of numbers?

正しい答えを選んでください

question mark

Why is it important to check for empty arrays before performing calculations?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  12

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  12
some-alt