116,898 views
18 votes
18 votes
Write and application that reads five integers from the user and determines and prints the largest and the smallest integers. Use only the programming techniques your learned in this chapter (use only if statements) 1.2. Modify your first program to add 2 more functions, the first function should determines the largest number and the second smallest. Both new functions must get invoked from the main function.

User Ruturaj
by
2.8k points

1 Answer

14 votes
14 votes

Answer:

#include <stdio.h>

#include <limits.h>

/* Define the number of times you are going

to ask the user for input. This allows you

to customize the program later and avoids the

hard coding of values in your code */

#define NUMVALS 5

int main(void)

{

int i = 0;

int curval = 0;

/* Set your initial max as low as possible */

int maxval = INT_MIN;

/* Set your initial min as high as possible */

int minval = INT_MAX;

/* Loop through and ask the user for the defined

number of values */

for (i = 0; i < NUMVALS; i++)

{

/* Ask the user for the next value */

printf("Enter the next value: ");

/* Get the next value from the user */

scanf("%d", &curval);

/* Check to see if this is our biggest or

smallest value yet */

if (curval > maxval)maxval = curval;

if (curval < minval)minval = curval;

}

/* Output the results */

printf("The smallest value entered was: %d \\", minval);

printf("The largest value entered was: %d \\", maxval);

/* End the program */

return 0;

}

Step-by-step explanation:

User Reuel Ribeiro
by
3.5k points