Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Min, Max, Sum, and Average | Searching and Filtering Arrays
Quizzes & Challenges
Quizzes
Challenges
/
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?

Select the correct answer

question mark

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

Select the correct answer

question mark

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

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 5

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookMin, Max, Sum, and Average

Swipe um das Menü anzuzeigen

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?

Select the correct answer

question mark

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

Select the correct answer

question mark

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

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 5
some-alt