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
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

finding max and min

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

int main()	
{
    int grade, total, counter, avg;
    total = 0;
    counter = 0;
    printf("Enter grades: (-1 to end list)\n");
     do
	{
	    scanf("%d", &grade);
	     
		if(grade != -1)
			{
				total = total + grade;
				counter = counter + 1;
				avg = (float)total / counter;
			}
	}
	
	while (grade != -1);
	{
    	 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
Solved Threads: 0
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
Solved Threads: 408
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
Solved Threads: 2
Skill Endorsements: 1

Please use code tags in future.

jbennet
Moderator
Team Colleague
18,528 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 609
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
Solved Threads: 33
Skill Endorsements: 4

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0698 seconds using 2.76MB