Maximum and Minimum in Arrays

Pages: 123
Can anyone tell me how exactly to find the maximum and minimum value within an array, along with the maximum and minimum positive value and negative value? If there are no negative numbers in the array, I just cout "No negative values" and just the opposite if there are no positive values. Also, how would I prompt the user to hit enter after displaying 6 lines of numbers to continue the rest of the display? Also, how would I keep count of the total number of files processed? Any help is greatly appreciated.
This is what I have so far for the maximum and minimum part.

void maxMin (double x[], int n)
{
int i;

int max = x[0];
for (i=0; i <= n-1; i++)
{
if (x[i] > max)
max = x[i];
}
if (x[i] < 0.0)
cout << endl << " No positive elements." << endl << endl;
else if (x[i] > 0.0)
cout << endl << " No negative elements." << endl << endl;
}
Last edited on
Try to separate your questions so they are more readable. Also, use code tags.

To get maximum and minimum, you want to set both values to the value of the first element, then start comparing.

To get maximum and minimum positive and negative values, you'll do the same, but now you need to do it with positive and negative values.

Prompt the user
http://www.cplusplus.com/forum/beginner/1988/

keep a counter that keeps track of the number of files that are processed.
For your information, C++ already has the std::min_element() and std::max_element() function templates, that can be used to search for minimum and maximum element.

http://www.cplusplus.com/reference/algorithm/min_element/
http://www.cplusplus.com/reference/algorithm/max_element/

If there are no negative numbers in the array, I just cout "No negative values" and just the opposite if there are no positive values.

Use booleans to "remember" if you found negative numbers.

1
2
3
4
5
6
7
8
9
10
bool found_a_negative = false;

for ( /* ... */ )
{
    if (x[i] < 0)
        found_a_negative = true;
}

if (!found_a_negative)
    cout << "No negative elements.\n" << endl;


Also, how would I prompt the user to hit enter after displaying 6 lines of numbers to continue the rest of the display?

My suggestion, assuming you're on Windows, is to use the std::system() function:
http://www.cplusplus.com/reference/cstdlib/system/

1
2
3
4
5
#include <cstdlib>

// ...

std::system("PAUSE");

Duoas wrote:

Don't use system("anything"). It is slow. It is disliked by antivirus software. It is OS-dependent. It tags you as an absolute beginner. And it is a massively huge, gaping, ugly security hole.
It is slow.

So what? This is a homework assignment, is it not?

It is disliked by antivirus software.

So what? This is a homework assignment, is it not?

It is OS-dependent. It tags you as an absolute beginner. And it is a massively huge, gaping, ugly security hole.

So what? This is a homework assignment, is it not?
closed account (D80DSL3A)
Catfish wrote:
Use booleans to "remember" if you found negative numbers.

Or you could let the max and min values reveal it.
1
2
if( min >= 0 ) cout << "No negative values";
if( max < 0 ) cout << "No positive values";
Last edited on
Catfish4 wrote:
So what? This is a homework assignment, is it not?

Indeed. Its purpose is that the user learns something. And what do they learn now? To use system()?
@Catfish4
I agree it's not a problem for homework assignments, but if you write bad, problematic code when you're learning how to write code, then you may carry on those bad habits. If beginners learn to use a better way of doing it, std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); now, then they'll remember it later if they ever need it.
Indeed. Its purpose is that the user learns something. And what do they learn now? To use system()?

By system("PAUSE"); they learn a quick, and easy, and sane way to pause the program, that is, without resorting to cryptic input stream hackery.

And I do believe that by the time they program professionally, they will not only unlearn using system() through means of learning why it's bad, but they will also grow out of wanting to pause the program for the user altogether. Share my optimism!
Okay, I've tried using system() and it works fine. The only problem is I'm not sure how to stop it after the first six lines exactly. The way I have it now stops it after the whole array and I tried putting it in the for loop, but that stops it after the first number. I have a feeling I'm going to need another for loop somewhere. Here's what I have now.


void printArray (const double x[], int n)
{
int i;
int columnCount;
char response;
bool pause;

cout << endl << endl;
cout << setw(48) << right << "Array of Numbers" << endl << endl;
cout << setprecision(2) << fixed << right;
columnCount = 0;
for (i=0; i < n; i++)
{
cout << setw(12) << x[i];
columnCount++;
if (columnCount % 6 == 0) // end the line every 6 numbers
cout << endl << endl;
}
while (pause == true)
std::system("PAUSE");
cout << "Continue display? y/n & <Enter> : " << endl;
cin >> response;
if (response == n || response == n) pause = false;
cout << endl << endl;
cout << setw(40) << right << "Array has " << n << " elements" << endl << endl;

} // end printarray function
Please use code tags. It preserves indentation, and makes the code easier to read:
[code] int i; // example [/code]

One immediate mistake I see in your above code is that you don't initialize pause.
This means that until you assign a value to it, it can randomly have any value.
(Of course in practice it is likely to have the value false, but the idea is you can't rely on this.)

bool pause = false; // initialization

The only problem is I'm not sure how to stop it after the first six lines exactly. The way I have it now stops it after the whole array and I tried putting it in the for loop, but that stops it after the first number.

Lines are horizontal and columns are vertical.
This means that basically i is the columnCount (which renders the latter useless).
So instead of columnCount, you could use a lineCount, which you increase every time you get to a new line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int lineCount = 0;

// ...

for (i=0; i < n; i++)
{
    // ...

    if (i % 6 == 0)
    {
        cout << "\n\n"; // you are overusing std::endl
        ++lineCount;
    }

    if (lineCount % 6 == 0)
        system("PAUSE"); // std:: not needed if you are "using namespace std;"
}
Is there a way for me to pause after every 6 lines of numbers without using system()? We haven't talked about that at all in class. We have just now gotten to arrays.
Look at Catfish4's code. You shouldn't use system("pause"), but as long as you're going to do it anyways, that code will work for what you're doing.
I tried doing it, but it only does 5 numbers for some reason, and it splits the array up. For instance, the first number will be on one line, then the next four will be on the next. Any ideas?
A two dimensional array would work best for this, but since you're using one dimension

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int line = 1;  //1 because it is on the first line already before you increment
for(int x = 0; x < max; x++)
{
    cout << array[x];

    if((x + 1) % 6 == 0)
    {
        cout << "\n\n";
        line++;  //++line also works here, but I don't like ++ before a variable
    }

    if(line %6 == 0)
    {
        //you can replace the following with system("pause"), but I wouldn't
        cout << "Press ENTER to continue . . .";
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}
I tried doing it, but it only does 5 numbers for some reason, and it splits the array up.

Yep, my mistake, it's because we start the count from 0 and not 1 (GRex's code corrects this).

1
2
3
        //you can replace the following with system("pause"), but I wouldn't
        cout << "Press ENTER to continue . . .";
        cin.ignore(numeric_limits<streamsize>::max(), '\n');


I would. This is a misuse of ignore() which has the purpose of skipping input data, not of pausing the program (which is only a side-effect).
Is there a certain library required to do the "numeric_limits<streamsize>::max()" because I can't get it to compile. It says "numeric_limits was not declared in this scope" along with many other things.
I don't mean to argue, so please don't regard this as an argument.

It's true that ignore is meant to clear the input stream rather than pause the program, but that line stops the program from continuing until the user uses the ENTER key (effectively pausing the program).

I agree that if the OP ever does anything with programming, they'll probably never need to pause the program, so either way will probably be a waste of time anyways.

I agree that if you're developing for yourself or just doing homework, it doesn't matter what method you use to pause (nobody really cares).

I just feel that if the need to do this again ever arises, then the OP will be better prepared if they follow the safer/better coding guidelines than if they use system and something happens they weren't expecting because of the way system works.
It's true that ignore is meant to clear the input stream rather than pause the program, but that line stops the program from continuing until the user uses the ENTER key (effectively pausing the program).

... except when it doesn't...

1
2
3
4
5
6
7
8
    cin.clear();
    cin.sync();

    if (cin.rdbuf()->in_avail())
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

    cout << "Press ENTER to continue . . .";
    cin.ignore(numeric_limits<streamsize>::max(), '\n');


Ah, much better now!
Pages: 123