C Program

Program to find the largest of n numbers and its location in an array

program to find the largest of n numbers and its location in an array
#include <stdio.h>
#include <conio.h> // Required for clrscr() and getch()

int main() {
int array[100], maximum, size, c, location = 1;
clrscr(); // Clear the screen (only works in some compilers)

printf("Enter the number of elements in the array:\n");
scanf("%d", &size); // Read the size of the array

printf("Enter %d integers:\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]); // Read the elements into the array

maximum = array[0]; // Initialize maximum to the first element

// Loop to find the maximum value and its location
for (c = 1; c < size; c++) {
if (array[c] > maximum) {
maximum = array[c]; // Update maximum
location = c + 1; // Update location (1-based index)
}
}

// Print the results
printf("Maximum element is present at location %d and its value is %d.\n", location, maximum);

getch(); // Wait for a key press (only works in some compilers)
return 0; // Return success status
}

1. Include Directives

#include <stdio.h>
#include <conio.h>
  • #include <stdio.h>: This includes the Standard Input Output library, necessary for functions like printf and scanf.
  • #include <conio.h>: This includes the console input/output library, which provides functions like clrscr() (to clear the screen) and getch() (to wait for a key press). Note that conio.h is not part of the standard C library and may not be available in all compilers.

2. Main Function

int main() {
  • Defines the main function where the program execution starts. (It’s better to use int main() instead of void main() for standard compliance.)

3. Variable Declarations

int array[100], maximum, size, c, location = 1;
  • int array[100]: An array to store up to 100 integers.
  • maximum: Variable to store the largest number found.
  • size: Variable to store the number of elements the user will enter.
  • c: Loop index variable.
  • location: Variable to store the position of the maximum element (initialized to 1 for a 1-based index).

4. Clear Screen (Optional)

clrscr(); // Clear the screen
  • This function clears the console screen (specific to certain compilers).

5. Input Size of Array

printf("Enter the number of elements in the array:\n");
scanf("%d", &size); // Read the size of the array
  • Prompts the user to enter the number of elements they want to input into the array.

6. Input Array Elements

printf("Enter %d integers:\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]); // Read the elements into the array
  • Prompts the user to enter the integers for the array and stores them in array.

7. Initialize Maximum

maximum = array[0]; // Initialize maximum to the first element
  • Initializes maximum with the first element of the array.

8. Finding Maximum and Its Location

for (c = 1; c < size; c++) 
{ if (array[c] > maximum)
{ maximum = array[c]; // Update maximum location = c + 1; // Update location (1-based index) } }
  • This loop starts from the second element (index 1) and checks each element:
    • If an element is greater than the current maximum, it updates maximum and its location.

9. Output the Result

printf("Maximum element is present at location %d and its value is %d.\n", location, maximum);
  • Prints the maximum element and its position in the array.

10. Wait for Key Press and Return

getch(); // Wait for a key press
return 0; // Return success status
  • getch() waits for the user to press a key before closing the console window (specific to some compilers).
  • return 0; indicates successful completion of the program.
Output:
Enter the number of elements in array
5
Enter 5 integers
2
4
7
9
1 
Maximum element is present at location 4 and it's value is 9 

Team Educate

About Author

Leave a comment

Your email address will not be published. Required fields are marked *

You may also like

C program to test whether a given identifier is valid or not
C Program

C Program to Test Whether a Given Identifier is Valid or Not

Rules for Valid Identifiers in C: The identifier must begin with a letter or an underscore (_). It cannot start
C program to test whether a given identifier is valid or not
C Program Compiler Design

C program to implement simple code generator

ALGORITHM:1. Start2. Get address code sequence.3. Determine current location of 3 using address (for 1st operand).4. If current location not