LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-06-2006, 10:53 AM   #1
Machiaveli
Member
 
Registered: Jan 2004
Posts: 47

Rep: Reputation: 15
Testing values in C


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.

I would really appreciate some help here..
 
Old 09-06-2006, 11:08 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.

Last edited by Nylex; 09-06-2006 at 11:12 AM.
 
Old 09-06-2006, 11:33 AM   #3
kstan
Member
 
Registered: Sep 2004
Location: Malaysia, Johor
Distribution: Dual boot MacOS X/Ubuntu 9.10
Posts: 851

Rep: Reputation: 31
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.
 
Old 09-06-2006, 11:49 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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). 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

'Hope that helps .. PSM

Last edited by paulsm4; 09-06-2006 at 11:53 AM.
 
Old 09-06-2006, 12:32 PM   #5
Machiaveli
Member
 
Registered: Jan 2004
Posts: 47

Original Poster
Rep: Reputation: 15
Oh, guys.. This is great!

This is just what I need.

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

'Hope that helps .. PSM
 
Old 09-06-2006, 01:26 PM   #6
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by Machiaveli
The secret is in limits.h with the INT_MAX and INT_MIN, or am I mistaken?
It's not necessary. An alternative is to set your initial min/max values to the first element of the array.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Threshold Values talat Linux - Software 1 08-26-2006 07:45 PM
Sarge-testing To Etch-testing? SMurf7 Debian 3 02-21-2006 10:59 PM
Setup as getting debian testing files from ftp - will it stay with testing BrianHenderson Debian 2 09-02-2004 06:06 PM
Sync Values waslit Linux - Software 0 06-05-2004 07:34 AM
rx/tx values Nauseous *BSD 1 05-26-2004 02:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration