How to Write a C Program to Find Maximum and Minimum Values from 10 Integers Entered via the Keyboard
When working with C programming, one common task is reading a series of integers from the keyboard and storing them in an array. Once this is done, you might want to find the maximum and minimum values in that array. This article will guide you through the process using a practical example and explain the code in detail.
Steps to Implement the C Program
Step 1: Declare an Array to Hold the Integers
First, you need to declare an array large enough to hold the 10 integers you will read from the keyboard. Here's how you do it:
int numbers[10];
Step 2: Use a Loop to Read the Integers from the Keyboard
Next, you will use a loop to read the integers one by one from the user and store them in the array. The loop will run 10 times, allowing you to read 10 integers:
for (int i 0; i 10; i ) { printf("Please enter integer %d: ", i 1); scanf("%d", numbers[i]); }
Step 3: Initialize Variables to Keep Track of the Maximum and Minimum Values
Before you start comparing values to find the maximum and minimum, initialize variables for these values. A good choice is to initialize them with the first element of the array:
int max numbers[0]; int min numbers[0];
Step 4: Loop Through the Array to Find the Maximum and Minimum Values
Using another loop, you will iterate through the array and update the maximum and minimum values as necessary:
for (int i 1; i 10; i ) { if (numbers[i] max) { max numbers[i]; } if (numbers[i] min) { min numbers[i]; } }
Step 5: Print the Results
Finally, you will print the maximum and minimum values to the console:
printf("The maximum value is: %d ", max); printf("The minimum value is: %d ", min);
Complete C Program
Here is the complete C program implementing the above steps:
#include stdio.h int main(void) { int numbers[10]; int max, min; // Read 10 integers from the keyboard printf("Enter 10 integers: "); for (int i 0; i 10; i ) { printf("Integer %d: ", i 1); scanf("%d", numbers[i]); } // Initialize max and min with the first element of the array max numbers[0]; min numbers[0]; // Find the maximum and minimum values for (int i 1; i 10; i ) { if (numbers[i] max) { max numbers[i]; } if (numbers[i] min) { min numbers[i]; } } // Print the results printf("The maximum value is: %d ", max); printf("The minimum value is: %d ", min); return 0; }
Compiling and Running the Program
To compile and run this program, you can use a C compiler like gcc. Here’s how you can do it from the command line:
Save the above code in a file called find_max_min.c. Open a terminal or command prompt. Navigate to the directory where find_max_min.c is located. Compile the program with the following command:gcc -o find_max_min find_max_min.cRun the compiled program with:
./find_max_min
After running, you can input 10 integers, and the program will display the maximum and minimum values among them.
Conclusion
This guide has provided a step-by-step explanation of how to write a C program to read 10 integers from the keyboard, store them in an array, and find the maximum and minimum values. Understanding these basics will help you tackle more complex programming challenges involving arrays and user input.