how to print the min value from an array ?
i just understand the logic of printing max value, i tried so much to print the min but didn't find any clue...
here is the program for max value:

int main(void)
{
	int num[5];
	int i,max=0;
	for (i=0;i<5;i++)
		{
		printf ("Enter value for %d:",i);
		scanf ("%d",&num[i]);
		if (num[i]>max)
		max=num[i];
		}
	printf ("Maximum value is %d ",max);
	getche ();
}

It's exactly the same logic as finding the max. You basically just start with a "high" value (rather than a "low" value) and reverse your comparison.

Store the first element of the array directly to the "min" variable, don't bother with min=0 because that can skew your results. Once you start into your loop, it's exactly the same as finding the max, you just base your comparison on less-than instead of greater-than.

what do you mean ?
should i've to do this ? if (num[i]<max) i've already tried this but it won't works and printing '0' as the minimum value because min is assigned for '0'

for one thing, you're finding a minimum value so call the variable "min" or something. not "max"

remember how when you found max, you started at the lowest possible value and worked up. you set max = 0 and changed it any time the current value was found to be greater than max.

to find the min is just the opposite in every way, you'll start at the highest possible value and work down. you'll set min = INFINITY (well, not literally) and change it anytime the current value is found to be less than min.

.

commented: I didn't give it to him. What i've put there is not compilable in his program. He has to re-write it. +0

It keeps printing '0' because you are initializing "max" to zero (0). Unless you enter a negative number, (0) is less than anything you enter, so the value never gets updated.

Try something like this inside your for loop instead:

{
  printf ("Enter value for %d:",i);
  scanf ("%d",&num[i]);
  if ( 0 == i )
    minVal = num[i];
  else if (num[i] < minVal)
    minVal = num[i];
}

[edit]
oops...overlapped w/ jeph

commented: he's already had MAX given to him in the previous thread. now you're giving him MIN too. they've got to do something on their own, or they'll never learn. Don't worry, i'll find pos rep to give you soon. i just wanted to bring your attention. -1

hey it works !!
can you please explain the logic of these two lines of the code ?

if ( 0 == i )
minVal = num[i];

hey it works !!
can you please explain the logic of these two lines of the code ?

if ( 0 == i )
minVal = num[i];

The index ("i") of the first iteration of the loop is (0). They say "If this is the first iteration of the loop (the index is (0)), store the input as the starting minimum value."

Or

Store the first element of the array directly to the "min" variable...

what d0 you mean ? can y0u please explain it in a bettEr way ?

Loops repeat sections of code. Each time a loop runs, it is called an "iteration".

Most for loops use an index variable to control the loop. In your program, this index variable is called "i", its value starts at (0) and increments by 1 at the end of each iteration. The loop ends after the iteration in which the number stored in the index is (in this case) 4. This is a total of 5 "iterations".

The lines you asked about look at the index variable to see what its value is. If the value in the index is (0), the computer knows that this is the first iteration of the loop. If it's the first iteration of the loop, the computer assumes that the number the user enters is the lowest value in the series. On the subsequent iterations, the value stored in the index is > (0), so the computer knows it's not the first iteration of the loop. Since it's not the first iteration, it compares the new input to the previous minimum and reacts accordingly.

its just assigning the first "num" value (the index i == 0 ) to the control variable, "minVal"... you got to start somewhere, right?

but I've got to take exception to if (0 == i) It is logically correct, but considered poor style.

there's no reason why you should obfuscate your code and decrease it's readability by reversing the standard left->right read direction of the conditional.

the only effect is to startle half the people who read your code, and thoroughly confuse a smaller but significant fraction.


.

I saw the tip posted by a couple of the senior members here. I have started doing equality comparisons this way as a way of reducing program errors.

Basically, it makes the compiler check that you aren't attempting assignment instead of an equality comparison. If you accidentally use assignment, the compiler will flag it as a syntax error because you are attempting to modify a constant.

I had the assignment vs. equality thing bite me just the other day because I didn't do this.

commented: okay, i'll buy that. +7

I saw the tip posted by a couple of the senior members here. I have started doing equality comparisons this way as a way of reducing program errors.

Basically, it makes the compiler check that you aren't attempting assignment instead of an equality comparison. If you accidentally use assignment, the compiler will flag it as a syntax error because you are attempting to modify a constant.

I had the assignment vs. equality thing bite me just the other day because I didn't do this.

hmm. i'd never heard that before, but i guess it makes some sense... at least theres a legitimate reason. i thought people were doing it just to be clever.

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.