# Doubt about max, minimum and average value with if function

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-18-2009
georgio777
Doubt about max, minimum and average value with if function
Hi,
I started this semester with C programming and one of the assignments asks to find the max value, minimum value and average of 10 integers with range of 0 and 100.

So here is what I did:
Code:

{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}

Imagine that the integers have already a value by doing a scanf.

My problem starts when two or more integers have the same value, the minimum and/or maximum value will not appear.

How can I solve this problem?

Thanks for your help and happy Programming!
• 09-18-2009
tabstop
I would start by solving the problem, as opposed to cutting and pasting the same line of code ten times.

If someone showed you ten numbers, one at a time, and then asked you which is largest, how would you answer the question?
• 09-18-2009
georgio777
Well I was thinking in nested statements, but in this case I have no idea how to start.
• 09-18-2009
tabstop
• 09-18-2009
georgio777
Ok, if someone showed you ten numbers, one at a time, and then asked you which is largest, how would you answer the question?

As common sense I would compare all the numbers and simply tell the one that is the biggest.

Am I right?
• 09-18-2009
tabstop
Quote:

Originally Posted by georgio777
Ok, if someone showed you ten numbers, one at a time, and then asked you which is largest, how would you answer the question?

As common sense I would compare all the numbers and simply tell the one that is the biggest.

Am I right?

And how would you compare all the numbers? You can't see them all at one time....
• 09-18-2009
Quote:

Originally Posted by tabstop
If someone showed you ten numbers, one at a time, and then asked you which is largest, how would you answer the question?

as tabstop suggests, the real difficulty is coming up with a solution to the problem (i.e. nothing to do with programming!). pretend you are the computer, get a piece of a paper and a pencil (and an eraser!) and try to solve the problem. once you have that, it is quite easy to translate it into code. this is what "programming" really is, and dont get discouraged by learning this new language and its syntax.

what is the largest of the following three numbers: 1, 4, 3? how did you come up with your answer? well you simply went through the list one by one and compared them. lets start with an initial "max" value of whatever the first number is, 1. how do we determine that 1 < 4 and 4 > 3, thus the max is 4?

say your requirement was to find the max number, but instead of a list of 10, the list is now 1000, or simply 50. would you continue using your current design?
• 09-18-2009
georgio777
Quote:

Originally Posted by tabstop
And how would you compare all the numbers? You can't see them all at one time....

Well, If I am smart enough I will pay more attention to the biggest number as the person is telling me the numbers.

For example is he starts telling 5, 10, 6, 8, 2; I would care about the second one since is the biggest by that moment, if he continues telling number like 5, 15, 11; I would change from 10 to 15 since is now the biggest.
• 09-18-2009
laserlight
Yes, so you have an idea right there: you need to keep track of the currently known largest number.
• 09-18-2009
georgio777
Quote:

as tabstop suggests, the real difficulty is coming up with a solution to the problem (i.e. nothing to do with programming!). pretend you are the computer, get a piece of a paper and a pencil (and an eraser!) and try to solve the problem. once you have that, it is quite easy to translate it into code. this is what "programming" really is, and dont get discouraged by learning this new language and its syntax.

what is the largest of the following three numbers: 1, 4, 3? how did you come up with your answer? well you simply went through the list one by one and compared them. lets start with an initial "max" value of whatever the first number is, 1. how do we determine that 1 < 4 and 4 > 3, thus the max is 4?

say your requirement was to find the max number, but instead of a list of 10, the list is now 1000, or simply 50. would you continue using your current design?

Well with ten numbers is already very complex, so I would find another design.
• 09-18-2009
Quote:

Originally Posted by georgio777
Well, If I am smart enough I will pay more attention to the biggest number as the person is telling me the numbers.

For example is he starts telling 5, 10, 6, 8, 2; I would care about the second one since is the biggest by that moment, if he continues telling number like 5, 15, 11; I would change from 10 to 15 since is now the biggest.

exactly. for the problem of finding a max (or min) of an unsorted list, which is what your problem is, there really isnt anything special going on. can you translate what you propose the algorithm is into "pseudocode"? that is, simply english statements which mimic the way an algorithm is written? maybe start with:
Code:

- create a list (array) with the following numbers: 5, 10, 6, 8, 2
- declare a variable "max" and assign it the first value in the list
- loop over all remaining numbers in the list:
- if current number ....
- (do something?)

hope it helps
• 09-18-2009
Quote:

Originally Posted by georgio777
Well with ten numbers is already very complex, so I would find another design.

• 09-18-2009
georgio777
Quote:

exactly. for the problem of finding a max (or min) of an unsorted list, which is what your problem is, there really isnt anything special going on. can you translate what you propose the algorithm is into "pseudocode"? that is, simply english statements which mimic the way an algorithm is written? maybe start with:
Code:

- create a list (array) with the following numbers: 5, 10, 6, 8, 2
- declare a variable "max" and assign it the first value in the list
- loop over all remaining numbers in the list:
- if current number ....
- (do something?)

hope it helps

Ok, well here is the problem, we haven't seen arrays nether loops still. That's the reason I am concerned and a little bit confused.

Is there is a way to do it without loops nor arrays?
• 09-18-2009
thats quite alright. as i mentioned above, the real difficulty is finding a solution to the problem, and not the part of inputting that into the computer in some programming language. if you were writing a book, what would be more difficult (mentally, not wrist strain!): coming up with the storyline/plot and its characters, etc, or writing those thoughts you have come up with on a piece of paper?

just think of a list/array as a bunch of "things". in this case, those "things" are numbers, more precisely they are integers. think of a loop as something that goes through a bunch of things (i.e. a list/array) and does something. in your case, you want to go through the list and compare the numbers in the list. using "for" loops you can easily go through a list, and have a variable that points to which thing in the list you're currently working on (i.e., an "index" in that list). heres an example of printing 10 numbers:
Code:

int[] myList = {5, 8, 10, 11, 4};
int i; // this is our "counter", which is used to keep track of where we are while looping through the above list of 5 things/numbers/integers
for ( i = 0; i < 5; i++ )
{
printf ("number %d is: %d\n" , i, list[i] );
}

the output would be:
Code:

number 0 is: 5
number 1 is: 8
number 2 is: 10
number 3 is: 11
number 4 is: 4

but anyways, im sure you must have some notes on looping to refer to.

the point is to come up with something on paper, disregarding the syntax of the "C" language, then translate that to C code. so try and work on the first part.
• 09-18-2009
Quote:

Originally Posted by georgio777
Is there is a way to do it without loops nor arrays?

yes, this is what you were trying to do in your initial code. however, you can see that if you changed the problem to finding max of 1000, instead of 10, numbers, your code grows exponentially or unreasonably large and not maintainable/readable. in order to make it generic (i.e. your algorithm works with a list of 1 number, 5 numbers, and 1,000,000 numbers), you use an array and a loop (otherwise you are hard coding, as you have already tried).

input: array of N integers
logic: loop over all N numbers and store the largest into "max" (setting an initial "max", you have to start somewhere!)
output: "max"

just go through the primitive steps you do yourself when finding the max of this list: 1, 4, 3, 5. a computer is actually very "dumb", and you must tell it exactly what to do. pretend you are writing instructions on how to teach a young child how to find a maximum number from the above list. ("is 1 greater than 4?", etc).