Hi could someone please help me with this problem.
I want to find the greatest and least numbers of all current input numbers. My output is wrong. I can't seem to calculate the max/min of all the current input numbers. My code only find the max/min between two inputs.

Here is my driver's code:

/* File: driver1.c
 *     * Date: February 28, 2010 
 *      */

/* This program gives an output of the greatest or least number given a set of numbers as input */

#include <stdio.h>
#define DEBUG
#include "maxmin.h"

main()
{  /* Variable declarations */
   float x;             /* N1 of input */
   float y;             /* N2 of input */
   float input;         /* Input of N1 and N2 */
   float maximum;
   float minimum;

        /* Obtain two inputs from user */
        printf("Enter a real number: ");
        input = scanf("%f", &x);

                while (input != EOF)
                {

                        /* Invoke function to obtain max of both numbers */
                        maximum = max(x, y);
                        #ifdef DEBUG
                        printf("The maximum of all values is %4.2f\n", maximum);
                        #endif

                        /* Invoke function to obtain min of both numbers */
                        minimum = min(x, y);
                        #ifdef DEBUG
                        printf("The minimum of all values is %4.2f\n", minimum);
                        #endif

                        x = y

                        /* Update loop */
                        printf("Enter a real number: ");
                        input = scanf("%f", &y);

                
                }

} /* End main */

And here is my header:

/* File:        maxmin.h
 * Date:        February 24, 2010
 */

/* Header file declaring maxmin utility functions */

float max(float n1, float n2);
/* Given: Two real numbers
 *  * Return: The maximum of all numbers 
 *  */

float min(float n1, float n2);
/* Given: Two real numbers
 *  * Return: The minimum of all numbers
 *  */

And these are my functions:

/* File: maxmin.c
 * Date: February 28, 2010
 */

/* This file contains driver1's utility functions to determine the maximum and minimum of two given numbers */

#include "maxmin.h"

float max(float n1, float n2)
/* Given: Two real numbers
 *  * Return: The maximum of both numbers 
 *  */

{
  float value;
        if (n1 > n2)
        {
        value = n1;
        return value;
        }
                else
                {
                 value = n2;
                 return value;
                }
}

float min(float n1, float n2)
/* Given: Two real numbers
 *  * Return: The minimum of both numbers
 *  */

{
  float value;
        if (n1 > n2)
        {
         value = n2;
         return value;
        }
                else
                {
                 value = n1;
                 return value;
                }
}

Recommended Answers

All 11 Replies

Read this, it was originally posted on Daniweb by WaltP in this thread.

One method could be to store all the input numbers in an array.
Then find the max and min of this array. If you google you might even get the code for these functions.

Your current approach is quite complicated. You dont need so much complication

Based on what I see:

/* Obtain two inputs from user */
        printf("Enter a real number: ");
        input = scanf("%f", &x);

                while (input != EOF)
                {

                        /* Invoke function to obtain max of both numbers */
                        maximum = max(x, y);

You accept x. Where do you read y? Don't you need two numbers to return the max or min?

Based on what I see:

/* Obtain two inputs from user */
        printf("Enter a real number: ");
        input = scanf("%f", &x);

                while (input != EOF)
                {

                        /* Invoke function to obtain max of both numbers */
                        maximum = max(x, y);

You accept x. Where do you read y? Don't you need two numbers to return the max or min?

yes, I tried initializing y at 0 right before the loop.
However, is it possible for me to calculate the maximum or minimum of all input values?

For example, if I were to have 2 at input:
max = 2
min = 0
My next input would be 3:
max = 3
min = 2
Next input = 1:
max = 3
min = 1

At the end of the loop, I want to be able to find the maximum and minimum of a bunch of numbers given one at a time.

When I initialized y at 0, I was still only able to obtain the max/min of only two of the most current input numbers.

If a number that you pick up is higher than all the other numbers, does it matter what the other numbers are? If a number that you pick up is smaller than all the others that you have does it matter what the higher numbers are (with the exception of the max)? Looping is the right idea but realize what you need to keep track of is important.

If you input more than one number, yes you can keep track. But you need at least two numbers to have a min and max.

If you input more than one number, yes you can keep track. But you need at least two numbers to have a min and max.

I am only allowed to input on number at a time.
For example:
2
3
100
-2
20

Screen Output:
The maximum is 100
The minimum is -2

I know there's nothing wrong with my functions, but something with my loop. How can I arrange my loop in a way that each new input is compared to all previous inputs in order to determine the most current max/min?

All you is input one number.

You do not input one number at a time

I initialized y=0 before my loop
max & min are now determined with the 2 most current inputs
However, I was wondering if it's possible to do update the loop so that the new value for y = all previous values of x.
I don't know how to put it in C language but would it be something like this :

y = x && y; ?

/* File : driver2.c
 * Date : February 28, 2010
 */

/* This program gives an output of the greatest or least number given a set of numbers
 * (entered individually)  as input */

#include <stdio.h>
#define DEBUG
#include "maxmin.h"

main()
{  /* Variable declarations */
   float x;             /* N1 of input */
   float y;             /* N2 of input */
   float input;         /* Input of N1 and N2 */
   float maximum;       /* Result of invoked max function */
   float minimum;       /* Result of invoked min function */

        /* Obtain one input from user */
        printf("Enter a real number: ");
        input = scanf("%f", &x);

                y = 0;
                while (input != EOF)
                {

                        /* Invoke function to obtain max of both numbers */
                        maximum = max(x, y);
                        #ifdef DEBUG
                        printf("The maximum of all values is %4.2f\n", maximum);
                        #endif

                        /* Invoke function to obtain min of both numbers */
                        minimum = min(x, y);
                        #ifdef DEBUG
                        printf("The minimum of all values is %4.2f\n", minimum);
                        #endif

                        y = x;

                        /* Update loop */
                        printf("Enter a real number: ");
                        input = scanf("%f", &x);

                }

        /* Print results */

} /* End main */

Setting y to zero -- doesn't that guarantee the minimum is always 0, even though all the other inputs are > 0?

I'll give you a major clue here:

/* Obtain one input from user */
    printf("Enter a real number: ");
    input = scanf("%f", &x);
    maximum = x;       /* assume x is the biggest number */
    minimum = x;       /* assume x is the smallest number */

    printf("Enter another number: ");
    input = scanf("%f", &x);
    while (input != EOF)
    {
        maximum = max(x, maximum );    // test for a new maximum
    ....

See what you can still use of your loop.

Setting y to zero -- doesn't that guarantee the minimum is always 0, even though all the other inputs are > 0?

I'll give you a major clue here:

/* Obtain one input from user */
    printf("Enter a real number: ");
    input = scanf("%f", &x);
    maximum = x;       /* assume x is the biggest number */
    minimum = x;       /* assume x is the smallest number */

    printf("Enter another number: ");
    input = scanf("%f", &x);
    while (input != EOF)
    {
        maximum = max(x, maximum );    // test for a new maximum
    ....

See what you can still use of your loop.

Thank you so much! I really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.