{Like user entered 1,2,32,12,55,5,61,23 and from which we have to find out maximum or minimum number without using any sorting technique. }
-
1what you have tried ? – Adil Shaikh Oct 09 '15 at 17:26
-
4I didn't use any sort technique and the answer is `61` – Suresh Atta Oct 09 '15 at 17:27
-
Amit you don't need to sort to find max and min in the array. All you need is simple comparisons. Try creating a program if you get issues in the program then post the question. – vvs Oct 09 '15 at 17:30
-
Java or C, which one? – Bacteria Oct 09 '15 at 17:38
-
@UUIIUI it's just asking for an algorithm, so the language doesn't really matter *and* it doesn't belong here -> shouldn't receive *any* answers IMHO – Oct 09 '15 at 17:43
-
2I'm voting to close this question as off-topic because this is homework without any attempt – Paul Ogilvie Oct 09 '15 at 17:45
-
so I edited the question & modified the tags. – Bacteria Oct 09 '15 at 17:46
-
Using C and simple comparisions will make it lengthy – Amit Kumar Oct 09 '15 at 17:48
-
@AmitKumar have you heard of loops? – Oct 09 '15 at 17:56
5 Answers
Iterate across the numbers set, keeping track of the current highest and current lowest numbers, comparing each number in the set to these, and reassigning the highest or lowest when appropriate.

- 1,968
- 16
- 21
You can read each input and compare that to the current value of some variable "maximum". If the input is higher than the current value make that input the new maximum value. You can do the same for minimum.
If you get the input all in one line, you'll have to parse it.

- 411
- 3
- 14
-
But i dont wanna write a lot of a code its just an example if user enters 10000 numbers can be compare them all – Amit Kumar Oct 09 '15 at 17:47
-
@AmitKumar: You have no other choice. You have to examine __every__ element to find the maximum/minimum. There is no shortcut. – Blastfurnace Oct 09 '15 at 17:50
-
#include
#include – Amit Kumar Oct 09 '15 at 18:02void main(){ int input; int max = 0; while(input != -999){ printf("Enter a Value : "); scanf("%d",&input); if(input > max){ max = input; printf("NEW MAX VALUE ENTERED\n"); } printf("Current Max Value: %d\n\n",max); } } -
@AmitKumar: Is there a reason why you copy/pasted that code into a comment? By the way, `input` is uninitialized the first time it's used in the `while` loop test and your code doesn't work if all the values are negative. Go ahead and turn it in as your homework. – Blastfurnace Oct 09 '15 at 18:10
Really basic example of what you're asking for, this will continue to read in integers until user enters '-999'. Every time the user enters a new max, the program prompts the user.
READ if you would like to use this to find the max in an array, just iterate through the array with the same if statement... The example below does not store the user's values...
#include <stdio.h>
#include <string.h>
void main(){
int input = 0;
int max = 0;
while(input != -999){
printf("Enter a Value : ");
scanf(" %d",&input);
if(input<0)input = 0;
if(input > max){
max = input;
printf("NEW MAX VALUE ENTERED\n");
}
printf("Current Max Value: %d\n\n",max);
}
}

- 165
- 1
- 2
- 13
-
1Not my downvote, but what happens if user enters `-1 -2 -3 -999`? And no minimum attempted. – Weather Vane Oct 09 '15 at 17:53
-
use of uninitialized variable, not caring for newline/whitespace in `scanf()` AND this question really shouldn't have answers anyways – Oct 09 '15 at 17:54
-
-
@FelixPalmen That was my mistake on the errors you brought up, thanks. However, I disagree with you when you said 'AND this question really shouldn't have answers anyways', the whole point of this forum is to help others. We all started somewhere, not everyone understands code the first time they see it, I know this question is 'common sense' to you, but don't put yourself on a pedestal. – LearningCODE Oct 09 '15 at 18:04
-
@LearningCODE this site is about programming problems, not "here is my homework, do it for me". – Oct 09 '15 at 18:05
-
@FelixPalmen Noticed how I didn't 'do his/her homework', I didn't find the max val in an array, and I didn't attempt to find the minimum, I gave him 'a push in the right direction'. – LearningCODE Oct 09 '15 at 18:08
-
@LearningCODE nothing against this answer any more, now that the immediate errors are fixed. But still, this is not the place for this kind of questions, it's just off-topic. (*and* it would help a lot more being forced to research such ridiculously trivial things yourself) – Oct 09 '15 at 18:10
-
@LearningCODE just adding now that there *are* kind of "basic" questions that deserve an answer in my opinion, because it's not that easy to find an answer as a "newbie", not exactly knowing what to look for in the first place. Just in my opinion, asking for some really basic *algorithm* doesn't fit that bill. It also fails to achieve another declared goal of this site: collect answers that are valuable for future readers, too. And, in fact, these can help you sometimes, even as a professional software developer. – Oct 10 '15 at 08:52
This link shows one of the SO answers where the max and min are found simultaneously using only 3n/2 comparisons instead of the traditional 2n comparisons(n for min and n for max).

- 1
- 1

- 698
- 6
- 17