Input Numbers and Find the Minimum Value

C# Arrays: Input Numbers and Find the Minimum Value


Introduction of the Lesson

In C# programming, arrays are essential for handling multiple values efficiently. Instead of working with a single variable, arrays allow you to store and process a collection of related data such as numbers, names, or scores. One of the most common operations when working with numeric data is determining the minimum value, which represents the smallest number in a dataset.

This lesson focuses on how to accept multiple numbers from the user, store them in an array, and identify the smallest value. This process involves key programming concepts such as loops, comparisons, and variable tracking. Understanding how to find the minimum value is important in real-world applications like analyzing grades, tracking expenses, or identifying lowest measurements in datasets.


Learning Objectives

To ensure effective learning, this lesson follows Outcome-Based Education (OBE) principles. The objectives are designed to guide you from basic understanding to practical application.

By the end of this lesson, you should be able to:

  • Understand how arrays store multiple numeric values in C#
  • Learn how to accept and store user input into an array
  • Practice using loops and conditional statements to compare values
  • Apply array processing techniques to determine the minimum value

These objectives provide a structured pathway for learning. You begin by understanding the concept, then learning the implementation, practicing through coding, and finally applying the knowledge to solve real-world problems.


Beginner-Friendly Source Code (with User Input)

Instructions:

  1. Open Visual Studio or any C# IDE
  2. Create a new Console Application
  3. Copy and paste the code below
  4. Run the program and follow the prompts
using System;

namespace ArrayMinimumApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("=== Find Minimum Value in an Array ===");

            Console.Write("How many numbers do you want to input? ");
            int size = Convert.ToInt32(Console.ReadLine());

            int[] numbers = new int[size];

            // Input values
            for (int i = 0; i < size; i++)
            {
                Console.Write($"Enter number {i + 1}: ");
                numbers[i] = Convert.ToInt32(Console.ReadLine());
            }

            // Assume first element is the minimum
            int min = numbers[0];

            // Find minimum value
            for (int i = 1; i < size; i++)
            {
                if (numbers[i] < min)
                {
                    min = numbers[i];
                }
            }

            Console.WriteLine("The minimum value is: " + min);

            Console.ReadLine();
        }
    }
}

Explanation of the Code

  • The program starts by asking the user how many numbers they want to input.
  • An array is created with the specified size to store the values.
  • A for loop is used to collect user input and store each number in the array.
  • The variable min is initially set to the first element of the array.
  • Another loop compares each element with min. If a smaller value is found, min is updated.
  • Finally, the program displays the smallest number entered by the user.

This approach is efficient and commonly used in real-world applications where datasets need to be analyzed.


Example Output

=== Find Minimum Value in an Array ===
How many numbers do you want to input? 5
Enter number 1: 45
Enter number 2: 12
Enter number 3: 78
Enter number 4: 9
Enter number 5: 30
The minimum value is: 9

Lesson Summary

In this lesson, you learned how to use arrays in C# to store multiple user inputs and determine the minimum value among them. By using loops and comparison logic, the program efficiently identifies the smallest number in a dataset. This fundamental technique is widely used in data analysis, system optimization, and real-world applications where identifying minimum values is essential. Mastering this concept strengthens your understanding of arrays and prepares you for more advanced programming challenges.


Multiple Choice Questions

  1. What is the purpose of an array?
    a. Store a single value
    b. Store multiple values in one variable
    c. Execute loops
    d. Handle errors
  2. Why is the first element assigned to min initially?
    a. It is always the smallest
    b. It provides a starting comparison value
    c. It avoids loops
    d. It sorts the array
  3. Which condition checks for a smaller value?
    a. numbers[i] > min
    b. numbers[i] == min
    c. numbers[i] < min
    d. numbers[i] != min
  4. What happens when a smaller number is found?
    a. The loop stops
    b. The number is removed
    c. The value of min is updated
    d. The array is sorted
  5. Which improvement enhances reliability?
    a. Removing loops
    b. Adding input validation
    c. Avoiding arrays
    d. Using only one number

Exercises, Assessment, and Lab Exam

To fully develop your understanding of arrays and comparison logic, you must extend this basic program into more advanced scenarios. The following activities will help reinforce your learning and improve your coding skills.

Exercises

  • Modify the program to also find the maximum value
  • Display all numbers entered by the user
  • Count how many numbers are less than the minimum + 10

Assessment

  • Create a program that:
    • Finds both minimum and maximum values
    • Calculates the range (max – min)
  • Explain how the algorithm works step-by-step

Lab Exam

  • Develop a Number Analysis System:
    • Accept at least 10 numbers
    • Find minimum, maximum, and average
    • Sort the numbers
    • Include input validation and error handling
    • Display results in a clean format

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

, , , , , , ,

Post navigation