ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How would you go about testing say 10 (or more) values for the highest and lowest in C?
I just can't figure it out. Surely you can do if, else if, until your fingers bleed but there has to be an easier way, or is there? What if I had 1000 values I wanted to test for the highest and/or lowest?
Is there a function that would be able to extract them from an array, for example? Is there a function to sort values in an array in C?
And yes - I'm a complete noob so please do explain this to me as you would to a five year old.
Assuming you're using an array, then what I would do is have a variable to store the highest number (say we call it "highest") and initially give it the value of the first element in the array. Then you could use a for loop to loop through each element, comparing the current element with highest to check which is higher. If the current element is higher than highest, then reassign highest to hold the value of the current element (and continue looping, of course!). If not, do nothing and continue looping through. After the loop has ended, highest should contain the highest number.
I'm not sure if there are functions in C for sorting arrays, but obviously if there are, you'd want to use these instead.
if you lazy to think about the source code, try this:-
-export all the values in plain text file (using system),
-sort them with linux command 'sort -g',
-finally use head and tail to get the highest and lowest result.
all above command can use system(command) to operate.
Actually, this is a surprisingly interesting and subtle problem:
1. Compare items in a loop
In C/C++, the simplest, most straightforward solution, as Nylex suggested, is to simply examine each item in turn and "remember" the highest (or lowest). For example:
Code:
/*
* SAMPLE OUTPUT:
* imin= 1, imax= 25
*/
#include <stdio.h>
#include <limits.h>
#define NELMS(A) (sizeof(A) / sizeof(A[0]))
int myarray[] =
{
3,9,3,6,1,3,11,25
};
int main ()
{
int i, imin = INT_MAX, imax = INT_MIN;
for (i=0; i < NELMS(myarray); i++)
{
if(imin > myarray[i])
imin = myarray[i];
if(imax < myarray[i])
imax = myarray[i];
}
printf ("imin= %d, imax= %d\n",
imin, imax);
return 0;
}
2. Use the standard shell tools
As kstan said, the problem can easily be solved with the shell:
Quote:
less tmp =>
3
9
3
6
1
3
11
25
sort -n |head -1 =>
1
sort -n |tail -1 =>
25
There are a million other alternatives (including, of course, awk or perl, if you so chose).
3. Use an appropriate algorithm/data structure
Finally, an "industrial strength" solution might be to read your items into a heap: http://en.wikipedia.org/wiki/Binary_heap
I'm actually taking a college class in C programming and although I have already handed over my assignments, I was very curious about the more difficult ones. The secret is in limits.h with the INT_MAX and INT_MIN, or am I mistaken? (Yes, it's my first week, with no programming experience what so ever, except for some in Bash) Anyway, In the project you had to manually assign values into an array, remove the highest and lowest and run the remaining values through a formula and output the result. I thought it was all too hard, at least for a first assignment, what do you guys think?
I really appreciate this.. I thank you all!
Quote:
Originally Posted by paulsm4
Hi -
Actually, this is a surprisingly interesting and subtle problem:
1. Compare items in a loop
In C/C++, the simplest, most straightforward solution, as Nylex suggested, is to simply examine each item in turn and "remember" the highest (or lowest). 3. Use an appropriate algorithm/data structure
Finally, an "industrial strength" solution might be to read your items into a heap: http://en.wikipedia.org/wiki/Binary_heap
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.