Question

Write 2 C functions. One returns the maximum of 5 numbers. The other function, returns the...

Write 2 C functions.

One returns the maximum of 5 numbers. The other function, returns the minimum of 5 numbers.

In your main function, prompt the user to input 5 numbers and call the 2 functions above.

Homework Answers

Answer #1

I have used variable names which are self-explanatory.

Happy Coding!!!!

#include <stdio.h>

int max_finder(int arr[5])
{
    int ans=-1000000007; //Almost like -infinity
    //Looping over arr to find Maximum
    for(int i=0;i<5;i++)
    {
        //If arr[i] is greater than current ans the update ans
        if(arr[i]>ans)
        {
            ans=arr[i];
        }
    }
    return ans; //Return ans as its max in given array
}
int min_finder(int arr[5])
{
    int ans=1000000007;  //Almost like -infinity
    //Looping over arr to find Maximum
    for(int i=0;i<5;i++)
    {
        //If arr[i] is smaller than current ans the update ans
        if(arr[i]<ans)
        {
            ans=arr[i];
        }
    }
    return ans; //Return ans as its min in given array
}
int main()
{
    int values[5];
    printf("Enter 5 number: ");
    for(int i=0;i<5;i++)
    {
        //Taking Input from the user
        scanf("%d", &values[i]);
    }
    int max=max_finder(values); //Calling max_finder() to get Maximum
    int min=min_finder(values); //Calling min_finder() to get Minimum
    
    printf("Maximum of 5 numbers is %d",max);
    printf("\n");
    printf("Minimum of 5 numbers is %d",min);
    
    return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them.         [0.5 mark] (BY USING C PROGRAM)
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
Question 1 Write functions that do the following: i) A function that takes 2 arguments and...
Question 1 Write functions that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both. Question 2 Write functions: i) One that prompts a user for 2 numbers. ii) Adds the two numbers if they are...
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
1. Write a function called compute_discount which takes a float as the cost and a Boolean...
1. Write a function called compute_discount which takes a float as the cost and a Boolean value to indicate membership. If the customer is a member, give him/her a 10% discount. If the customer is not a member, she/he will not receive a discount. Give all customers a 5% discount, since it is Cyber Tuesday. Return the discounted cost. Do not prompt the user for input or print within the compute_discount function. Call the function from within main() and pass...
Write a C++ program to compute the value of x * (x + 1) + y...
Write a C++ program to compute the value of x * (x + 1) + y * y + z * (z - 1) where x, y, and z are 3 floating point numbers entered by the user. Requirements: • You should have a function get3numbers() that asks the user to enter 3 numbers. • You should have a function computeExp() that computes the value of the expression. • Your main function should call the above 2 functions and get...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
Write the following function and provide a program to test it (main and function in one...
Write the following function and provide a program to test it (main and function in one .py) 5 pts def readFloat(prompt) that displays the prompt string, followed by a space, reads a floating-point number in, and returns it. Below is how you’re going to call the function: salary = readFloat(“Please enter your salary:”) percentageRaise = readFloat(“What percentage raise would you like?”) print("The salary is", salary) print("The raise percentage is", percentageRaise) keep it simple, python
Write a C++ program which consists of several functions besides the main() function. 1)   The main()...
Write a C++ program which consists of several functions besides the main() function. 1)   The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. 2)   A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. 3)   A value-returning function called Power(int a,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT