# Finding max and min using arrays and pointers

• (2 Pages)
• 1
• 2

## 19 Replies - 71672 Views - Last Post: 17 July 2011 - 05:03 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=239740&amp;s=74469d2e8602c8aad0286ad3c06460ed&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jrs7892

Reputation: 0
• Posts: 30
• Joined: 23-October 10

# Finding max and min using arrays and pointers

Posted 16 July 2011 - 07:53 AM

Hi there. I've got a homework assignment that seems very simple and straightforward, but I'm having major issues wrapping my head around pointers. I'm also confused as to how to implement the maximum and minimum functions based on the prototypes provided for me. I've searched the forums and read a bunch of tutorials and book chapters, but I just keep going around in circles. I'll post the assignment details and the code I have below. (I know the code I have is very wrong, but I was trying to work through the bits and pieces I understood). Any help would be greatly appreciated!!

Assignment:
Write a program that prompts the user to enter a series of integers with -1 being the sentinel integer. Store the integers in an array. You can assume a max of 10 integers. Then, print the integers entered, the minimum of those integers, and the maximum of those integers. Place your program in a single file named maxmin.cpp.

int minimum(const int* values, size_t numValues);
int maximum(const int* values, size_t numValues);

Next, define the following constant in your main() function:

const size_t maxValues = 10;

Your first task will be to implement the minimum() and maximum() functions. You see that both functions take in a pointer to a integer and the number of values. Effectively, you are passing in your array and the size of your array (which will be 10 or less). For the minimum() function, use array subscript notation. That is, use brackets, but not the *. For the maximum() function, use pointer/offset notation. That is, do not use any brackets.

Your next task is to ask the user to enter up to 10 integers. You can assume that at least one valid integer will be entered. Use -1 as the sentinel value (i.e. -1 stops the entering of numbers and is not included in the array). Print the values that were entered and the min and max of those values. Note that if the user enters over 10 integers, only the first 10 will be counted.

```#include <iostream>
using namespace std;

//function prototypes
int minimum(const int* values, size_t numValues);
int maximum(const int* values, size_t numValues);

int main()
{
const size_t maxValues = 10;
const int SENTINEL = -1;
int i;
int array[i];
int array[maxValues];
int min = array[0];
int max = array[0];
int numValues = 0;

while (array[i] != SENTINEL) //need this to stop if -1 is entered
{
cout <<"Enter up to 10 integers (-1 to stop):" <<endl;
for (int i = 0; i < maxValues; i++)  //loops through array
cin >> array[i];
numValues++;
}

minimum (min, numValues);  //not sure how to implement function
maximum (max, numValues);	//not sure how to implement function

cout << "Values entered:" << array[i]<<endl; //need to print full array
cout << "Minimum value:" << min <<endl; //need to print minimum
cout << "Maximum value:" << max <<endl; //need to print maximum
}

//to find max/min
for (i = 0; i < numValues; i++)
{
if (array[i] < min )
{
min = array[i];
}

else if (array[i] > max )
{
max = array[i];
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Finding max and min using arrays and pointers

### #2 PlasticineGuy

• mov dword[esp+eax],0

Reputation: 281
• Posts: 1,436
• Joined: 03-January 10

## Re: Finding max and min using arrays and pointers

Posted 16 July 2011 - 08:04 AM

Pointers are a rather difficult concept aren't they .
Here's a quick primer.
a[x] is the same as *(a + x). So what the [] subscript operator is doing "behind the scenes" is adding x to a, which in this case is a pointer to the first element in an array.
Knowing this we can implement the same function 2 different ways. Here's an example. Can you find the other way?
```int function (int* array, int pos)
{
return array[pos];
}
```

The next step is finding the minimum of a set of integers. You can do this easily by comparing each element with the lowest number found so far, and if it's lower, setting the lowest found to the element. In rough pseudocode it might look something like this:
```set a variable to some high number
loop through array
if variable is lower than current element
set variable to element
```
Finding the maximum is very similar.

### #3 jimblumberg

Reputation: 5451
• Posts: 16,977
• Joined: 25-December 09

## Re: Finding max and min using arrays and pointers

Posted 16 July 2011 - 08:05 AM

You have prototyped your functions properly, but where have you implemented these functions? Remember that the function prototype, function implementation and function call must all agree on the number and type of parameters.

Also your input loops do not seem to be correct. I would suggest that you break your problem into small steps. First populate the array. Next insure the array contains the value the correct values. Then start writing the functions to return the minimum and maximum.

Jim

### #4 jrs7892

Reputation: 0
• Posts: 30
• Joined: 23-October 10

## Re: Finding max and min using arrays and pointers

Posted 16 July 2011 - 08:49 AM

They don't seem difficult when I read about them, but trying to put them to use sure makes me feel dense!

PlasticineGuy, on 16 July 2011 - 08:04 AM, said:

Pointers are a rather difficult concept aren't they .
Here's a quick primer.
a[x] is the same as *(a + x). So what the [] subscript operator is doing "behind the scenes" is adding x to a, which in this case is a pointer to the first element in an array.
Knowing this we can implement the same function 2 different ways. Here's an example. Can you find the other way?
```int function (int* array, int pos)
{
return array[pos];
}
```

If I'm understanding the first part correctly, the other way is :

```int function (int* array, int pos)
{
return *(array + pos);
}
```

What I don't understand is how/where to put this in my specific program. Does this go into the max/min function that you describe in the rest of your post?

### #5 jimblumberg

Reputation: 5451
• Posts: 16,977
• Joined: 25-December 09

## Re: Finding max and min using arrays and pointers

Posted 16 July 2011 - 09:07 AM

Actually if you look a your prototype:
int maximum(const int* values, size_t numValues);
your second parameter is a size_t not an int. So you will pass into the function the array and the size of the array. You will return the element that contains the largest value.

You may want to start by finding the maximum inside of main. Then once you figure out how to actually find the maximum value move the logic into the function.

Jim

### #6 jrs7892

Reputation: 0
• Posts: 30
• Joined: 23-October 10

## Re: Finding max and min using arrays and pointers

Posted 16 July 2011 - 11:23 AM

Jim,

Thanks for the links.. I've read this before, but I read it again. It's clear to me how functions work when they're explained to me, I'm not sure why I have such a problem making my own function work though.

I've reworked my code again and again. I finally got it to compile at least, but then I had to redo the part with the sentinel value a few times before something worked. All I've got left now is the min/max part. I'll post my new code.. not sure where to go from here. I'm still playing around with finding the max and min within main, but I can't get it to work yet. Thanks in advance for your help!

```#include <iostream>
using namespace std;

//function prototypes
int minimum(const int* values, size_t numValues);
int maximum(const int* values, size_t numValues);

int main()
{
const size_t maxValues = 10;
const int SENTINEL = -1;
int i = 0;
int array[maxValues];
size_t numValues = 0;

{
cout <<"Enter up to 10 integers (-1 to stop): " ;
for (int i = 0; i < maxValues; i++)  //loops through array
{
cin >> array[i];
if (array[i] == -1)
break;
else
numValues++;
}

cout <<"\nValues entered: ";
for (int i = 0; i < maxValues; i++)
{
cout << array[i];
if (array[i] == -1)
break;
}
}

cout <<"\n\nMinimum value: " << " "  << endl;
cout <<"\nMaximum value: \n" << " " << endl;
return 0;
}

int minimum (int* array, size_t numValues)
{
int min = array[0];
size_t i;

for (i = 0; i < numValues; i++)
{
if (array[i] < min)
{
min = array[i];
}

}return min;
}

int maximum (int *array, size_t numValues)
{
int max = array[0];
size_t i ;

for (i = 0; i < numValues; i++)
{
if (array[i] > max)
{
max = array[i];
}

}return max;

}

```

### #7 jimblumberg

Reputation: 5451
• Posts: 16,977
• Joined: 25-December 09

## Re: Finding max and min using arrays and pointers

Posted 16 July 2011 - 11:36 AM

First remember that your function prototype and function implementation must agree on the number an type of parameters. The following prototype and implementation do not agree on the type of the parameters. In your prototype you have your first parameter as const int* in your implementation you have int*. A const int* is not the same as an int*.

```int minimum(const int* values, size_t numValues);

int minimum (int* array, size_t numValues)
{
```

Jim

### #8 jrs7892

Reputation: 0
• Posts: 30
• Joined: 23-October 10

## Re: Finding max and min using arrays and pointers

Posted 16 July 2011 - 11:49 AM

I changed the implementation so that the parameters match. Is there something else obvious that I'm missing with these functions?

```int minimum (const int* array, size_t numValues)
{
int min = array[0];
size_t i;

for (i = 0; i < numValues; i++)
{
if (array[i] < min)
{
min = array[i];
}

}
return min;
}

int maximum (const int* array, size_t numValues)
{
int max = array[0];
size_t i;

for (i = 0; i < numValues; i++)
{
if (array[i] > max)
{
max = array[i];
}

}
return max;

}

```

### #9 jimblumberg

Reputation: 5451
• Posts: 16,977
• Joined: 25-December 09

## Re: Finding max and min using arrays and pointers

Posted 16 July 2011 - 12:00 PM

Your logic inside the functions seems to be good. You still seem to be having problems filling the array. Sometimes when I am developing a program I will use constants in order to insure that my logic is correct. Example:
```#include <iostream>
using namespace std;

//function prototypes
int minimum(const int* values, size_t numValues);
int maximum(const int* values, size_t numValues);

int main()
{
const size_t maxValues = 10;
const int SENTINEL = -1;
int i = 0;
int array[maxValues];
size_t numValues = 0;

array[0] = 3;
array[1] = 59;
array[2] = 10;
array[3] = 45;
array[4] = 17;
numValues = 5;

// After the function calls print out the value returned.

return 0;

}

```

Then once you are sure the functions are operating properly. Change to getting the user input. Since you already know your functions work with a fixed data set any errors will probably be with your data entry routines.

Jim

### #10 jrs7892

Reputation: 0
• Posts: 30
• Joined: 23-October 10

## Re: Finding max and min using arrays and pointers

Posted 16 July 2011 - 12:36 PM

Jim,

I did as suggested and used constants instead of filling the array, but I'm getting memory locations as my max and min. Is this what my cout should look like? I keep going back and forth between maximum or just max, but either way I'm not getting a correct answer.

```cout <<"\n Minimum value: " << minimum  << endl;
cout <<"\n Maximum value: \n" << maximum  << endl;

```

### #11 jimblumberg

Reputation: 5451
• Posts: 16,977
• Joined: 25-December 09

## Re: Finding max and min using arrays and pointers

Posted 16 July 2011 - 12:44 PM

Jim

### #12 jrs7892

Reputation: 0
• Posts: 30
• Joined: 23-October 10

## Re: Finding max and min using arrays and pointers

Posted 16 July 2011 - 12:47 PM

Sorry.. here it is.

```#include <iostream>
using namespace std;

//function prototypes
int minimum(const int* values, size_t numValues);
int maximum(const int* values, size_t numValues);

int main()
{
const size_t maxValues = 10;
const int SENTINEL = -1;
int i = 0;
int array[maxValues];
size_t numValues = 0;

{
array[0] = 3;
array[1] = 59;
array[2] = 10;
array[3] = 45;
array[4] = 17;
numValues = 5;
}

cout <<"\n\nMinimum value: " << minimum  << endl;
cout <<"\nMaximum value: \n" << maximum  << endl;
return 0;
}

int minimum (const int* array, size_t numValues)
{
int min = array[0];
size_t i;

for (i = 0; i < numValues; i++)
{
if (array[i] < min)
{
min = array[i];
}
}
return min;
}

int maximum (const int* array, size_t numValues)
{
int max = array[0];
size_t i;

for (i = 0; i < numValues; i++)
{
if (array[i] > max)
{
max = array[i];
}
}
return max;
}

```

### #13 jimblumberg

Reputation: 5451
• Posts: 16,977
• Joined: 25-December 09

## Re: Finding max and min using arrays and pointers

Posted 16 July 2011 - 01:15 PM

Where are you calling the functions?
Look at this simple example:

```// function example
#include <iostream>
using namespace std;

// Function Prototype.
int addition (int a, int B)/>;

int main ()
{
int z;
// Function call.
cout << "The result is " << z;
return 0;
}

// Function implementation.
int addition (int a, int B)/>
{
int r;
r=a+b;
return (r);
}

```

Notice how the function is called in main.

Jim

### #14 jrs7892

Reputation: 0
• Posts: 30
• Joined: 23-October 10

## Re: Finding max and min using arrays and pointers

Posted 16 July 2011 - 01:47 PM

I had a function call in my program from the beginning, but it was giving me errors. I tried to fix it, but nothing worked so I just removed it. I understand how they've come up with the call in the example you posted, but I can't come up with one that works for these two functions.

```#include <iostream>
using namespace std;

//function prototypes
int minimum(const int* values, size_t numValues);
int maximum(const int* values, size_t numValues);

int main()
{
const size_t maxValues = 10;
const int SENTINEL = -1;
int i = 0;
int array[maxValues];
size_t numValues = 0;

{
array[0] = 3;
array[1] = 59;
array[2] = 10;
array[3] = 45;
array[4] = 17;
numValues = 5;
}

minimum (min, numValues);
maximum (max, numValues);

cout <<"\n\nMinimum value: " << minimum  << endl;
cout <<"\nMaximum value: \n" << maximum  << endl;
return 0;
}

int minimum (const int* array, size_t numValues)
{
int min = array[0];
size_t i;

for (i = 0; i < numValues; i++)
{
if (array[i] < min)
{
min = array[i];
}
}
return min;
}

int maximum (const int* array, size_t numValues)
{
int max = array[0];
size_t i;

for (i = 0; i < numValues; i++)
{
if (array[i] > max)
{
max = array[i];
}
}
return max;
}

```

### #15 jimblumberg

Reputation: 5451
• Posts: 16,977
• Joined: 25-December 09

## Re: Finding max and min using arrays and pointers

Posted 16 July 2011 - 03:32 PM

Let's look at your function prototype: int maximum(const int* values, size_t numValues); This is the same as int maximum(const int values[], size_t numValues);. As you can see your first parameter is expecting an int pointer. Since when you pass arrays to functions the array name is actually a pointer to the first element to use this function you would call it like:
int min = minimum(array, numValues);. Then you would print out the variable min in your cout statement.

Jim

This post has been edited by jimblumberg: 16 July 2011 - 03:33 PM