1

I am trying to write a simple logical program to find the highest and lowest values, which has already been done. The issue is how to find the index position is when it finds the highest and lowest value. See the attached picture. enter image description here

#include <iostream>

using namespace std;


int main()
{

int number=0, min=0, max=0;
int *rangeOfNumbers = new int[];

cout<<"Enter 5 numbers: ";

for(int i=0; i<5;i++)
{
    cout<<"Enter number "<<i+1<<": ";
    cin>>rangeOfNumbers[i];
}

//max min array positions

min=rangeOfNumbers[0];
max=rangeOfNumbers[0];

//find max and mins
for(int j=0; j<5;j++)
{
if(max<rangeOfNumbers[j])
{
    max=rangeOfNumbers[j];
}
else if(min>rangeOfNumbers[j])
{
    min=rangeOfNumbers[j];
}
}

cout<<"\n\nMin number: "<<min;
cout<<"\nMax number: "<<max;

cin.get();
cin.get();

return 0;
}

5 Answers 5

4

In addition to updating min and max, also keep an index variable and update it.

//max min array positions

min=rangeOfNumbers[0];
max=rangeOfNumbers[0];
int minindex = 0;
int maxindex = 0;

//find max and mins
for(int j=0; j<5;j++)
{
  if(max<rangeOfNumbers[j])
  {
    max=rangeOfNumbers[j];
    maxindex = j;
  }
  if(min>rangeOfNumbers[j])
  {
    min=rangeOfNumbers[j];
    minindex = j;
  }
}

maxindex += 1;
minindex += 1;
Sign up to request clarification or add additional context in comments.

5 Comments

The index never changes and will not display the proper position. It comes up as 1 and 3 each time.
Also, it shows as 1 and 3 because the array indices start at 0 (i.e. first element is 0). I have edited to add 1 to the indices so you get the "correct" answer.
Didn't realize that simple issue... Thank you.
That else is a bug. The min and max can be the same -- when all elements of the list are the same. (Having programmed for 45 years, this is far from the first time I have seen this bug ... there are plenty of instances of it out in the wild.)
Actually, the bug doesn't happen here because you initialize min and max to the first element ... but that is generally a bug, since the list might be empty ... not in this case with a fixed size list. The usual approach is to set min to the max integer and max to the min integer ... but any way of detecting an empty list will do.
1

Change max and min to indexOfMax and indexOfMin, Because by storing index of maximum you can access both maximum index and maximum value.

So you should change the max if to something like this:

if(rangeOfNumbers[indexOfMax] < rangeOfNumbers[j])
{
    indexOfMax = j;
}

Continue this change for other lines yourself.

Comments

1

Not sure how you can even compile the code:

int *rangeOfNumbers = new int[];

You need to specify a size when you new an array of integers.

int *rangeOfNumbers = new int[5];

I compiled under gcc 4.5.3, got the following error:

 error: expected primary-expression before ‘]’ token

You also need to remember the indices for max and min when you scan the array.

For example: before the for loop, initialize:

int maxIndex = -1;

Inside for loop:

if (max < A[i])
{
   maxIndex = i;
   max = A[i];
}

Similar stuff should be done for min.

1 Comment

This was compiled under Visual Studio '12.
1

This might be far from what you are looking for right now. However you should get used to stl containers (vectors, lists and all that stuff) if you are going to do some intensive algorithms like finding max and min.

The benefit is that many algorithms are already available and are optimized for performance.

I think your example is just an exercise. anyways her is how to if it wasn't

For example this problem is ideal for using a vector. Here is an example

#include <iostream>
#include<vector>
#include<algorithm>

using namespace std;


int main()
{

    int number=0, min=0, max=0;
    vector<int> rangeOfNumbers;

    cout<<"Enter 5 numbers: ";

    for(int i=0; i<5;i++)
    {
        cout<<"Enter number "<<i+1<<": ";
        cin>>number;
        rangeOfNumbers.push_back(number);
    }

    vector<int>::iterator maxelem = max_element(rangeOfNumbers.begin(), rangeOfNumbers.end());
    vector<int>::iterator minelem = min_element(rangeOfNumbers.begin(), rangeOfNumbers.end());

    cout << endl << "Max number: " << (*maxelem) << " at " << std::distance(rangeOfNumbers.begin(), maxelem) + 1;
    cout << endl << "Min number: " << (*minelem)<< " at " << std::distance(rangeOfNumbers.begin(), minelem) + 1;

    cin.get();

    return 0;
}

Comments

0

I would suggest a tiny optimization, if you initialize min and max at item 0 why ask again for the same element

for (int I = 0;

in place

for (int I = 1;

The source code:

#include <iostream>

using namespace std;

#define SIZE 5

int main()
{

    int min=0, max=0;
    int *rangeOfNumbers = new int[SIZE];

    cout<<"Enter 5 numbers: ";

    for(int i=0; i < SIZE; i++)
    {
        cout<<"Enter number " << i + 1 <<": ";
        cin>>rangeOfNumbers[i];
    }

    //max min array positions

    min = rangeOfNumbers[0];
    max = rangeOfNumbers[0];

    int minindex = 0;
    int maxindex = 0;

    //find max and mins
    for(int j = 1; j < SIZE; j++)
    {
        if( max < rangeOfNumbers[j])
        {
            max = rangeOfNumbers[j];
            maxindex = j;
        }
        if( min > rangeOfNumbers[j])
        {
            min = rangeOfNumbers[j];
            minindex = j;
        }
    }

    maxindex += 1;
    minindex += 1;

    cout<<"\n\nMin number: "<<min;
    cout<<"\nMax number: "<<max;

    cin.get();
    cin.get();

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.