Write a function int max_of_four(int a, int b, int c, int d) which reads four arguments and returns the greatest of them.
Question
Write a function int max_of_four(int a, int b, int c, int d) which reads four arguments and returns the greatest of them.
Solution 1
Sure, here is a simple function in C that takes four integers as arguments and returns the maximum among them.
#include <stdio.h>
int max_of_four(int a, int b, int c, int d) {
int max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
if (d > max) {
max = d;
}
return max;
}
int main() {
int a = 1, b = 2, c = 3, d = 4;
printf("The maximum number is %d\n", max_of_four(a, b, c, d));
return 0;
}
In this function, we first assume that a is the maximum. Then we compare max with b, c, and d respectively. If any of these is greater than max, we update max. Finally, we return max, which will be the greatest of the four numbers.
Solution 2
Sure, here is a simple function in C that takes four integers as arguments and returns the greatest among them.
#include <stdio.h>
int max_of_four(int a, int b, int c, int d) {
int max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
if (d > max) {
max = d;
}
return max;
}
int main() {
int a = 1, b = 2, c = 3, d = 4;
printf("The maximum of four numbers is: %d", max_of_four(a, b, c, d));
return 0;
}
In this function, we first assume that 'a' is the maximum. Then we compare 'max' with 'b', 'c', and 'd' respectively. If any of these is greater than 'max', we update 'max'. Finally, we return 'max' which holds the maximum of the four numbers.
Similar Questions
Write a function which takes three integers as arguments, and returns the maximum of the three. The main() function then tests this function appropriately.
Write a C++ code to find largest among 4 numbers using the concept call by reference.
Write a program to display a menu driven facility to find the greatest of two numbers,three numbers and four numbers
Order the following numbers from smallest to greatest using the numbers 1, 2, 3, and 4.Use the number 1 for the smallest value. Use the number 4 for the largest value.
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
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.