Write a C program that takes an array of integers and its size as input and returns the maximum element in the array.Requirements:The function should be named findMax.The function should take two parameters: an integer array and its size.The function should return the maximum element in the array.
Question
Write a C program that takes an array of integers and its size as input and returns the maximum element in the array.Requirements:The function should be named findMax.The function should take two parameters: an integer array and its size.The function should return the maximum element in the array.
Solution 1
Sure, here is a simple C program that fulfills your requirements:
#include <stdio.h>
// Function to find maximum in arr[] of size n
int findMax(int arr[], int n) {
int i;
int max = arr[0]; // Assume first number in array is the largest initially
// Loop through the array
for (i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i]; // If any number is greater than the current max, replace it
return max; // Return the maximum element
}
int main() {
int arr[] = {10, 324, 45, 90, 9808};
int n = sizeof(arr) / sizeof(arr[0]);
printf("The maximum element in the array is %d", findMax(arr, n));
return 0;
}
In this program, the findMax function takes an array and its size as input, and returns the maximum element in the array. The main function creates an array, calculates its size, and then calls findMax to find the maximum element. The result is then printed to the console.
Solution 2
Sure, here is a simple C program that meets your requirements:
#include <stdio.h>
// Function to find maximum in arr[] of size n
int findMax(int arr[], int n) {
int i;
// Initialize maximum element
int max = arr[0];
// Traverse array elements from second and compare every element with current max
for (i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
// Driver program to test above function
int main() {
int arr[] = {10, 324, 45, 90, 9808};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Largest in given array is %d", findMax(arr, n));
return 0;
}
In this program, the function findMax takes an array and its size as input, and returns the maximum element in the array. The main function is just a driver to test the findMax function. It initializes an array, calculates its size, and then calls findMax to find the maximum element in the array. The maximum element is then printed to the console.
Similar Questions
Problem StatementYou are given a function,static String FindMax(int[] arr){}The function accepts an integer array 'arr' of length 'size' as its argument. Implement the function to find and return the maximum number that can be formed by any permutation or arrangement of all the digits obtained from all the numbers present in the array. You have to return the number formed as a string.Note: You may need to rearrange the digits of the numbers to form the maximum number.Example:Input:34 79 58 64Output:98765443Explanation:All digits obtained from all the numbers of array are 3, 4, 7, 9, 5, 8, 6, 4. Maximum number obtained after rearranging all these digit gives 98765443. Thus, output is 98765443.The custom input format for the above case:434 79 58 64(The first line represents the 'size', the second line represents the elements of the array 'arr')Sample input21 90 23Sample Output932210The custom input format for the above case:321 90 23(The first line represents the 'size', the second line represents the elements of the array 'arr')Instructions :This is a template based question, DO NOT write the "main" function.Your code is judged by an automated system, do not write any additional welcome/greeting messages."Save and Test" only checks for basic test cases, more rigorous cases will be used to judge your code while scoring.Additional score will be given for writing optimized code both in terms of memory and execution time.Now let's start coding :Language:Read-only code below . . .1class SolutionClass {2 public static void main(String[] args) throws java.lang.Exception {3 //Input read from STDIN4 String result = FindMax(arr);5 //Value in result printed to STDOUT6 }7}8Write your code below . . .9static String FindMax(int[] arr) throws java.lang.Exception10{11 /* Write your code here. */12}13
Problem StatementRaj, a budding programmer, is practicing array manipulation. He is working on a program to find the largest element in a matrix. Can you assist him in creating the code?Write a program that takes row and column size of the matrix as input and finds the largest element in it. Raj wants to know the maximum value in the matrix to better understand array operations.Input format :The first line consists of two space-separated integers r and c, representing the row and column size of the matrix.The next r lines consist of c space-separated integers, which represent the elements of the matrix.Output format :The output displays a single line containing the message "Largest element: X", where X is the largest element in the input matrix.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:2 ≤ r, c ≤ 50 ≤ elements ≤ 100Sample test cases :Input 1 :3 39 4 5 6 7 3 2 0 8Output 1 :Largest element: 9Input 2 :5 358 79 4514 18 6714 25 10014 28 6313 24 71Output 2 :Largest element: 100Input 3 :2 415 18 38 6428 97 48 83Output 3 :Largest element: 97
write a c program Find the largest number among the three numbers.
Problem StatementEmily is a budding programmer who loves solving mathematical problems. One day, she comes across a challenge to find the largest of three numbers using a custom function. Determined to tackle the challenge, she decided to create a program. Can you help her write the code and solve the problem?Function Specifications: double findMax(double a, double b, double c)Input format :The input consists of three space-separated double values a, b, and c.Output format :The output prints a double value which is the largest number of the given three numbers, rounded off to two decimal places.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:-100.00 ≤ a, b, c ≤ 100.00Sample test cases :Input 1 :2.22 3.98 3.99Output 1 :3.99Input 2 :-99.99 -10.78 -78.56Output 2 :-10.78Input 3 :-54.27 2.54 -23.58Output 3 :2.54
Create a class A with data member to store a array of N integers. Define a default constructor to read the array. Define a member function show() that will print the integer array.Create a derived class B with a member function show() that will print the maximum value in the integer array.Make the member finction show() in base class as virtual.Create objects and pointers to access the member functions.Sample Input5 (Read N)1297343Sample Output34
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.