Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Friday, September 30, 2022

C program to find largest element in array using function


C program to find largest element in array using function





Introduction:


If you are interested in programming, then you must have come across arrays at some point. Arrays are used to store a collection of data in a single variable, making it easier to manage and manipulate large sets of data. In this article, we will discuss how to find the largest elements in an array by using function in C programming language.
We will start by discussing what an array is and then move on to the steps involved in finding the largest elements in an array by using function. We will also provide you with a step-by-step guide to help you understand the process.

What is an Array?

An array is a collection of similar data types stored in contiguous memory locations. Each element in an array is identified by an index or a subscript, which starts from 0 and goes up to the size of the array minus 1.

Writing program:

#include<stdio.h>
#define MAX 100
int find(int []);
int n;
int main()
{
int arr1[MAX],mxe,i;
printf(" Enter how many numbers :");
scanf("%d",&n);
printf(" Enter numbers :\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr1[i]);
}
mxe=find(arr1);
printf(" The largest element in the array is : %d\n\n",mxe);
return 0;
}
int find(int arr1[])
{
int i=1,mxe;
mxe=arr1[0];
while(i < n)
{
if(mxe<arr1[i])
mxe=arr1[i];
i++;
}
return mxe;
}




Explanation of the code:


The above code is a C program to find the largest element in an array of integers. Here's a step-by-step explanation of the code:

  1. The program starts by including the standard input/output library "stdio.h" and defining a constant MAX with a value of 100.
  2. It declares a function called "find" that takes an integer array as its parameter and returns an integer. This function is defined after the main function.
  3. It declares two global variables, an integer array arr1 of size MAX and an integer n, which will hold the number of elements in the array.
  4. Inside the main function, it prompts the user to enter the number of elements in the array and then reads the input from the user.
  5. Then it prompts the user to enter the elements of the array and reads the input from the user.
  6. It calls the "find" function and passes the array as an argument.
  7. It stores the value returned by the "find" function in a variable called "mxe".
  8. Finally, it prints the largest element found in the array using the "printf" function.
  9. The "find" function starts by initializing two variables, "i" and "mxe", where "i" is set to 1, and "mxe" is set to the first element of the array.
  10. It uses a while loop to iterate through the array and compare each element with the current maximum value stored in the variable "mxe".
  11. If the current element is greater than the current maximum value, it updates the value of "mxe".
  12. The loop continues until all the elements in the array have been compared.
  13. The function then returns the maximum value found in the array.


Output:






FAQs:


Q: What is an array in C programming?
A: An array is a collection of similar data types that are stored in contiguous memory locations. It is a data structure that stores a fixed-size sequential collection of elements of the same type.

Q: What is a function in C programming?
A: A function is a self-contained block of code that performs a specific task. It is a reusable block of code that can be called from other parts of a program.

Q: How do we find the largest element in an array in C programming?
A: We can find the largest element in an array by iterating through all the elements of the array and comparing each element with the current maximum element. We can use a loop to iterate through the array and keep track of the current maximum element.

Q: What is the purpose of the "find" function in the program?
A: The "find" function is used to find the largest element in an array. It takes an integer array as its parameter and returns the largest element found in the array.

Q: What is the purpose of the constant MAX in the program?
A: The constant MAX is used to define the maximum size of the array. It is used to declare the array size and ensures that the array size does not exceed the maximum allowed value.

Conclusion:


In C programming, finding the largest element in an array is a common task. It can be done using a simple algorithm that involves iterating through all the elements of the array and comparing each element with the current maximum element. By using a function to perform this task, we can make the code more modular and reusable. The C program to find the largest element in an array using a function demonstrates the use of functions in C programming and provides a useful example for beginners.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages