Re: [Dev-C++] My first request for help, problems with Min/Max
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2005-06-23 19:22:38
|
First of all this is the C way of coding. In c++, there is the std::vector
class to handle arrays of entries.
Anyway, you have forgotten to initialize the min and max variables. The
easiest way is to do:
min = max = numberGroup[0];
for (i = 1; i < 5; i++) {
if (min > numberGroup[i]) min = numberGroup[i];
if (max < numberGroup[i]) max = numberGroup[i];
}
/Per W
On Thu, 23 Jun 2005, TornadoChaser wrote:
> Hello everyone,
>
> I'm trying to teach myself more C++ than I learned in college through
> creating some fun programs. Today I got the idea to write a program
> that would take a file in a common format (.txt or .html), find all the
> doubles or ints, save them to an array or vector, then sort through them
> to find the highest and lowest values. I'm not expecting to get there
> in one day, but I'm hoping that I'm on the right track.
>
> My first step was to see if I could determine the min and max of a list
> of values, to do this I constructed an array with 5 values, then listed
> 5 values for each of the subscripts of the array. Next we search the
> array for the biggest value, then display the results. I believe that I
> wrote the code correctly, but I got some funny results. I'm hoping that
> some of the wiser members of the list could help my figure out what my
> problem is and perhaps set me on the road to fix it. The code follows
> below.
>
> Thanks,
> Kevin.
>
> First, the output:
>
> Maximum: 4268036
> Minimum: 1
> Press any key to continue . . .
>
> Now, the code:
>
> #include <iostream.h>
> #include <stdlib.h>
> using namespace std;
>
> int main()
> {
> int numberGroup[5];
> int max, min, j = 0;
>
> numberGroup[0] = 1;
> numberGroup[1] = 2;
> numberGroup[2] = 3;
> numberGroup[3] = 4;
> numberGroup[4] = 5;
>
> while (j <= 4)
> {
> if (max <= numberGroup[j]){
> max = numberGroup[j];
> }
> else if (min >= numberGroup[j]){
> min = numberGroup[j];
> }
>
> j++;
> }
> cout << "Maximum: " << max << endl;
> cout << "Minimum: " << min << endl;
>
> system("PAUSE");
> return 0;
> }
>
>
> -------------------------------------------------------
> SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
> from IBM. Find simple to follow Roadmaps, straightforward articles,
> informative Webcasts and more! Get everything you need to get up to
> speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|