After i key in 20 different value of float, what command should i use so that i can determine the largest and smallest value from these numbers ?

Maybe use a array to store the variables , then sort the array in numerical order then access the first and last element of the array.

The best way would be to calculate it during the process of input.

float s=0,l=0,x;
for(int i=0;i<20;++i){
cin>>x;
if(x>l) l=x;
if(x<s) s=x;
}
/* Use the values */

Or after taking the input in an array, go through it and get the smallest and largest values.

float s=0,l=0,a[20];
/* input the array */
for (int i=0;i<19;++i) {
if(x>l) l=x;
if(x<s) s=x;
}
/* Use the values */

Edited 6 Years Ago by techie1991: correction in code

Maybe use a array to store the variables , then sort the array in numerical order then access the first and last element of the array.

There are only 20 numbers. It's faster to linear search while saving the biggest and smallest as you go:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int nums[20];
    int hi, lo;
    srand(unsigned(time(0)));
    for (int x = 0; x < 20; ++x)
    {
        nums[x] = rand();
        cout << nums[x] << endl;
    }
    hi = lo = nums[0];
    for (int x = 0; x < 20; ++x)
    {
        if (nums[x] < lo)
            lo = nums[x];
        if (nums[x] > hi)
            hi = nums[x];
    }
    cout << "Smallest: " << lo << endl;
    cout << "Biggest:  " << hi << endl;
}

@rpdm read my post again, it's the implementation of the same algo.
And remember:
1. chalson wanted to get the numbers as input, so you won't require the time() and srand() and the inclusion of header files <cstdlib> and <ctime>
2. The values required are float, not integers.
3. your main() should return a value.

Comments
All bad points. Thanks for assuming I'm an idiot.

@rpdm read my post again, it's the implementation of the same algo.

Did it occur to you that I was writing my post when you submitted yours and I didn't see it?

1. chalson wanted to get the numbers as input, so you won't require the time() and srand() and the inclusion of header files <cstdlib> and <ctime>

Read the rules. I did just enough of the assignment to help chalson without giving him something he can turn in for homework credit.

2. The values required are float, not integers.

The algorithm doesn't change for floats.

3. your main() should return a value.

I guess you didn't know that falling off main returns 0 automatically.

Read the rules. I did just enough of the assignment to help chalson without giving him something he can turn in for homework credit.

If your was motive as this, then try not to feed in code here, give out just the pseudo code,.. if you really want it so.

The algorithm doesn't change for floats.

Who said to you that the algo was wrong..!?

I guess you didn't know that falling off main returns 0 automatically.

Sorry, but i know that. But you must also know that, writing "return 0" at the end of the program is useful when making large programs.

If your was motive as this, then try not to feed in code here

What a hypocrite. You gave C++ code too. The only difference is mine can be pasted into a compiler and run. Both of our examples can't be plagiarized as is and turned in as homework.

Who said to you that the algo was wrong..!?

Then what was your point about ints vs floats?

But you must also know that, writing "return 0" at the end of the program is useful when making large programs.

  1. I don't agree with that opinion
  2. The program I posted is anything but "large" :icon_rolleyes:
Comments
c'mon, you are not here to fight.! are you? see my previous posts and you will come to know how idotic it is to get offensive here.!

@rpdm Don't get offensive/angry!! I was just pointing to some loop holes you were having in your program.. I believe that is what we are here for, helping each other..?
@mods maybe this thread is going off the topic, I would request you for it's closure..

You should return EXIT_SUCCESS or EXIT_FAILURE which are in the stdlib.h file.

C99 defines that if left without a return statement it will return 0, but you should not get in the habit of doing so. What happens if one day, all hell breaks loose and EXIT_SUCCESS changes to 1 and the OS now expects this. Or, the in the next specification it no longer assumes 0 and assumes 1? (Which makes sense, because your method has a return value, but you didn't hit a return statement, so surely the app failed? Think about it...)

All your missing return statements will need to be implemented anyway.

As good practise you should return EXIT_SUCCESS, EXIT_FAILURE or some implementation specific value.

I was just pointing to some loop holes you were having in your program..

I am having no loop holes in my program. If you want to help, find a *real* problem with the code.

What happens if one day, all hell breaks loose and EXIT_SUCCESS changes to 1 and the OS now expects this.

What if the sky turns pink polka dot and starts raining reese's peanut butter cups? That's about as likely as the language specification changing for no good reason except to break 40 years worth of code.

Comments
Rude and obnoxious

What if the sky turns pink polka dot and starts raining reese's peanut butter cups? That's about as likely as the language specification changing for no good reason except to break 40 years worth of code.

Actually in specification C89 the main method did not return any value by default and would throw a compiler error. This is probably how returning void arose as a way to get around that and now why int main returns 0 by default(However, I'm not stating this as fact).

Regardless of simple quirk of the specification to allow laziness, my ascertation that it is best practise to use one of the exit identifiers, or an implementation specific value, still stands. The fact is, it *has* changed before so by all means it could change again. I have never found an argument other than the implied "I'm lazy" for not having the main method return at least *something*.

I'm not attempting to insult you, but surely it would be better not to teach new coders bad habits.

Actually in specification C89 the main method did not return any value by default and would throw a compiler error. This is probably how returning void arose as a way to get around that and now why int main returns 0 by default(However, I'm not stating this as fact).

Regardless of simple quirk of the specification to allow laziness, my ascertation that it is best practise to use one of the exit identifiers, or an implementation specific value, still stands. The fact is, it *has* changed before so by all means it could change again. I have never found an argument other than the implied "I'm lazy" for not having the main method return at least *something*.

I'm not attempting to insult you, but surely it would be better not to teach new coders bad habits.

I think you're confused. This is the C++ forum.

I used C89 and C99 to highlight the changes in specifications. A lot of C99 was used to create the C++ specification, including the part I was talking about.

Also, a few features that were added to the C99 specification are likely, but as of yet not officially confirmed, to be added to the new C++ specification (C++0x, will update/replace the current C++03 specification)

If you are interested in a read, here is a link to the draft specification that was created this August.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3126.pdf

I appreciate your point that this is a C++ forum and I quoted part of the C spec, but my point is still valid as it is also part of the C++ spec.

There are only 20 numbers. It's faster to linear search while saving the biggest and smallest as you go:

]

There was nothing in the specification given to have the fastest algorithm its certainly not the most efficient it has an O(n) compared to my method which has O(n log n). I was only suggesting a way for the @op to go they might decided that either one might not be best its not a competition

Comments
Why defend a slower *and* more complex solution?

@rpdm - wow that was childish with the voting down reputation just because some one did agree with you !
also why are you arguing with people in this thread they are usually more than one solution to the problem ! be a bit more open minded please

techie1991's code is fundamentally broken anyway. s should be initialized to FLT_MAX and l to FLT_MIN.

This article has been dead for over six months. Start a new discussion instead.