We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,571 Members — Technology Publication meets Social Media

finding max and min

``````#include <stdio.h>
int max(int x, int y);
int min(int x, int y);

int main()
{
total = 0;
counter = 0;
printf("Enter grades: (-1 to end list)\n");
do
{

{
counter = counter + 1;
avg = (float)total / counter;
}
}

{
printf("max = %d\n", max);
printf("min = %d\n", min);
printf("average = %0.2f\n", (float)total/counter);
}
}

int max(int x, int y)
{
int biggest = x;
if (y > biggest)
biggest = y;
return biggest;
}

int min(int x, int y)
{
int smallest = x;
if (y < smallest)
smallest = y;
return smallest;
}``````

i need help finding the maximum and minimum values inputed by the user. i keep getting values such as 67808 and 67880 as my maximum and minimum values when those are not values inserted by the user. somebody help please!

5
Contributors
4
Replies
20 Hours
Discussion Span
1 Year Ago
Last Updated
5
Views
cali_kid
Newbie Poster
5 posts since Mar 2012
Reputation Points: 10
Skill Endorsements: 0

Do you need to do this without calling library functions?

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Skill Endorsements: 7

I'm assuming you need to find the maximum and minimum grade of the whole set. Since your max and min functions only compare two grades (and you never call them -- you'll get an error if you try to compile what you have above), they need to be called for every grade entered. This should get you started.

Dervish1
Light Poster
29 posts since Apr 2009
Reputation Points: 47
Skill Endorsements: 1

Please use code tags in future.

jbennet
Moderator
Team Colleague
18,528 posts since Apr 2005
Reputation Points: 1,826
Skill Endorsements: 29

This is definitely one issue:

``````printf("max = %d\n", max);
printf("min = %d\n", min);``````

`max` and `min` are not integers, they are functions that return integers, so these lines of code don't make any sense. I'm not even sure how you got this to compile, but your program is probably printing out the memory addresses of your `min()` and `max()` functions.

You're not even recording any of your grade's values anyway, so it will be impossible to know what the maximum and minimum grades are the way you wrote your code.
HINT: You need to recalculate the minimum and maximum values inside your loop.

Tumlee
Junior Poster
172 posts since Oct 2011
Reputation Points: 84