help needed in a c program :)

I have thought hard to find a proper logic to find largest and least of a given set of numbers in C language.I know it is easy to find when only 2 or even 3 are given, but using if...else or else if ladder becomes confusing when more than 3 numbers are given.Is there a better way to do it? Guys please dont mock at me for asking such a noobish question.I'm just starting to learn C and im new to thinking about problems in this manner.Any help here would be great. TIA :)

EDIT : I want it without using arrays
 
Do you know data structure?? Just put the numbers in an array, now simply sort them(using any techniques), and simply the array will be sorted in ascending or descending order, now it became that simple to say which one is the greatest. If ascending, then the last cell of the array and if descending then the first cell of the array :)

But learn data structure first(if you don't know it already)

Happy Coding. :)

EDIT: Ahh you should have mentioned that first, without using an array, it would be complex, as you need to check each number in nested if-else

Though it makes no sense to not to use an array, no one is bothered about how are you storing it. You are free to use any approach which seems simpler for you. So why wouldn't you like to use the great feature??
 
If you don't want to use an array, how exactly do you plan to store the numbers? Is it going to be something like command line input where the user keeps entering a set of numbers and then you give the min and max of that set? Either case, the logic is that you have two place holders - one each for the min and max - and compare each number against the min and the max. If the number is greater than the current max, it is the new max. Likewise for the min. When you encounter the first number, the min and max are both set to that number, since your set at this point contains only one integer.
 
Ya, From his point of describing the problem I think he's planning to store the numbers in different variables like he do in case of 2 or 3 number. That's why he asked if would be a lot of complexity to solve the problem as he would need to lots of nested if...else.
 
Ih you are not using arrays, it depends what sort of output you need.

1. THE HIGHEST/LOWEST NUMBER IS : X (i.e., remembering the set is not important.)

2. THE HIGHEST/LOWEST NUMBER IN THE SET OF A,B,C,X,Y,Z IS : X (i.e., remembering the set is important)

1. Create 2 variables. 1st(say X) one will store the max value, in the next(say Y) the value entered by user. When the user enters the first variable, make X=Y. after that every time the user enters a value store it in Y and compare to X. If highest/lowest make X=Y. You can create a loop using do while and keep either a numeric/alphabetical value to end the loop and display result.

2. You will need to use linked lists or Binary Search Tree for this. In the former you will have to compare all values using loops. In the latter, once you write the code, it will sort the values for you.
 
if you have an array of 'n' numbers like

int total_numbers = 100;

int numbers[total_numbers] ...

then:

Code:
int min_number = numbers[0];

int max_number = numbers[0];

for (int number = 1; number < total_numbers; number++)

{

  min_number =  min_number < numbers[number] ? min_number : numbers[number];

  max_number = max_number > numbers[number] ? max_number : numbers[number];

}

At the end you have the results.
 
oops, should be easy enough to change to a do/while loop which reads input until user enters zero or empty...?

(replace the numbers[number] with the input value)
 
thats ok, since you only need to go over each number/input just once, there is no need to store anywhere (array or whatever)... just read input and check it right there then move on to the next in the loop...
 
int max=-32000,min=32000,t,n;

cin>>n//no. of numbers

for(int i=0;i<n;i++)

{

cin>>t;

if(t>max)

max=t;

else if(t<min)

min=t;

}

cout<<max<<min;

//there is a bug in this program, see if you can find it.. :)

--- Updated Post - Automerged ---

krishnandu said:
Do you know data structure?? Just put the numbers in an array, now simply sort them(using any techniques), and simply the array will be sorted in ascending or descending order, now it became that simple to say which one is the greatest. If ascending, then the last cell of the array and if descending then the first cell of the array :)

But learn data structure first(if you don't know it already)

Happy Coding. :)

EDIT: Ahh you should have mentioned that first, without using an array, it would be complex, as you need to check each number in nested if-else

Though it makes no sense to not to use an array, no one is bothered about how are you storing it. You are free to use any approach which seems simpler for you. So why wouldn't you like to use the great feature??

Sorting is way less efficient, it has a best case order of n logn

Check the easiest method which gives the minimum and maximum in linear time
crvinay_4u said:
I have thought hard to find a proper logic to find largest and least of a given set of numbers in C language.I know it is easy to find when only 2 or even 3 are given, but using if...else or else if ladder becomes confusing when more than 3 numbers are given.Is there a better way to do it? Guys please dont mock at me for asking such a noobish question.I'm just starting to learn C and im new to thinking about problems in this manner.Any help here would be great. TIA :)

EDIT : I want it without using arrays

I assume that you are in 12th class or 1st year of college..

If so, and your stream is CS then its best that you spend a while trying out these problems practically..

else u will get screwed in later semesters
 
^^ thanks for your advice dude.Im in my first year of my btech in cse.ya,i am practising these types quite thoroughly without leaving even minor details :)
 
@crvinay_4u: Since you're just starting off, you have no idea of efficiency but just remember, sorting will always be less efficient than a linear search. Simple logic is that linear search goes through your "list" once while sorting always takes more than one iteration (as mehrotra.akash correctly said n*log(n) at best).

As I see it, you have a couple of options (wrt using/not using arrays)

1) if you are using arrays. Simply iterate through them and check for largest/smallest (code has already been provided by vishalrao :))

2) if you are not using arrays and you have a definite set of numbers where the count of numbers is low (say 3-4), a simple ladder would do.

3) if you are not using arrays and you have an indefinite set of numbers, your first concern is storing them. You could store the numbers by doing memory management and then iterate through them and apply the same logic as for arrays. Since you're just starting off, you are not going to be doing malloc/calloc so I can safely rule this out (just wanted to make a note of this in case you come back months later after reading memory management to read this post :))

4) if you are not using arrays and you have an indefinite set of numbers which you are not storing, you have to evaluate them on the fly and maintain just 2 variables (for max/min). Again, the code for this (I believe, since I've not read it completely) has been provided by mehrotra.akash

Am I missing anything? :)

mehrotra.akash said:
If so, and your stream is CS then its best that you spend a while trying out these problems practically..

else u will get screwed in later semesters
Later semesters? If you want to pursue a career in this line, make that getting screwed even when you start working :)
 
John Anderton said:
Later semesters? If you want to pursue a career in this line, make that getting screwed even when you start working :)
Currently in my 3rd year CSE, so see many people around me getting screwed badly, I guess it should have been "Later semesters and beyond.."
 
I passed out of college a bit over a year back and started working in a big IT firm soon after. A year down, I still see people being screwed :)

If you get your basics right, it will always help. :)
 
Back
Top