# maximum and minimum value of array

• (2 Pages)
• 1
• 2

## 19 Replies - 5089 Views - Last Post: 02 April 2011 - 01:42 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=225663&amp;s=d08ac3cdb5c0af28b35f40907f2b150a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 serj779

Reputation: 0
• Posts: 17
• Joined: 31-March 11

# maximum and minimum value of array

Posted 02 April 2011 - 05:23 AM

hi everyone,
im having a problem with finding the minimum and maximum values in a structure array.
i have a structure array numarray[sum].heights which has already been filled with values from the program. i seem to understand how to do this with normal arrays but structure arrays leave me a little confused. and i cant use vectors. i know it has something to do with looping the structures and finding out if each element is bigger than the maximum or smaller than the minimum but im not clear on how to do this. My maximum value is 200, minimum is 0.
Thank you!

Is This A Good Question/Topic? 0

## Replies To: maximum and minimum value of array

### #2 TMKCodes

• D.I.C Regular

Reputation: 48
• Posts: 440
• Joined: 21-March 09

## Re: maximum and minimum value of array

Posted 02 April 2011 - 05:28 AM

```Initialize max number variable as zero or smaller if required.
Loop while index is 200 and raise index by one every time the loop runs.
Check if the array variable at the index is larger than the max number.
If it was then store the array variable at the index to the max number variable.
Else
do nothing, continue the work.
End loop.

```

This post has been edited by TMKCodes: 02 April 2011 - 05:32 AM

### #3 sk1v3r

Reputation: 231
• Posts: 668
• Joined: 06-December 10

## Re: maximum and minimum value of array

Posted 02 April 2011 - 05:29 AM

you say that you know how to do it with normal arrays, yet you can't do it with this?
it is the exact same thing.
Give us an example of how you are trying to do it, and what is going wrong, or just any specific bit that you don't understand, and we will try and help you

And your idea of how to do it is correct, you just have to loop, and if the value in the array is larger than the current biggest, or smaller than the current smallest, then change the current biggest or smallest to the one you just found

### #4 serj779

Reputation: 0
• Posts: 17
• Joined: 31-March 11

## Re: maximum and minimum value of array

Posted 02 April 2011 - 06:03 AM

this is my rough idea on how it should be

```numarray[sum].height = fstr3;    //fstr3 contains 20 float values from a file

void maxminHeight(float numarray[sum].height, int n) // error saying sum should be a constant value
// and the . after the [sum]says expected ')'

float max,min;
max= min = numarray[0].height;
int i;

for(i = 0; i<= n; i++)
{
if (numarray[i] >= max)
max = numarray[i].height;
}
for(i= 0; i<= n; i++)
{
if(numarray<= min)
min = numarray[i].height;
}

```

thank you

### #5 CTphpnwb

• D.I.C Lover

Reputation: 3695
• Posts: 13,355
• Joined: 08-August 08

## Re: maximum and minimum value of array

Posted 02 April 2011 - 06:17 AM

Without seeing your struct/class, it appears that numarray[] is an array of structs, numarray[sum] is a specific struct, and numarray[sum].height is a specific element of that struct and not an array.

This post has been edited by CTphpnwb: 02 April 2011 - 06:19 AM

### #6 serj779

Reputation: 0
• Posts: 17
• Joined: 31-March 11

## Re: maximum and minimum value of array

Posted 02 April 2011 - 06:40 AM

yes, ur right.
it looks like

```struct DATA
{
int id;
string name;
float height;
}numarray[MAX];    // where MAX has been defined as 20
```

and then i had store these with values read from a file which explains the numarray[sum].height. i have already found the average height, trying to find the minimum and maximum heights.

### #7 CTphpnwb

• D.I.C Lover

Reputation: 3695
• Posts: 13,355
• Joined: 08-August 08

## Re: maximum and minimum value of array

Posted 02 April 2011 - 07:04 AM

I'd do something like this.
```float minimum, maximum;
minimum = numarray[0].height;
maximum = numarray[0].height;
for(int i = 0; i < MAX; i++) {
if(numarray[i].height < minimum) {
minimum = numarray[i].height;
} else if(numarray[sum].height > maximum) {
maximum = numarray[i].height;
}
}
```

### #8 serj779

Reputation: 0
• Posts: 17
• Joined: 31-March 11

## Re: maximum and minimum value of array

Posted 02 April 2011 - 07:27 AM

thank you, but why am I getting a garbage value. Am i supposed to initialize something else?

### #9 CTphpnwb

• D.I.C Lover

Reputation: 3695
• Posts: 13,355
• Joined: 08-August 08

## Re: maximum and minimum value of array

Posted 02 April 2011 - 07:36 AM

Yes. Depending on your compiler, float values can be undefined or set to zero if you don't initialize them. XCode will set them to zero, but I believe that MS VS will leave them undefined.

### #10 serj779

Reputation: 0
• Posts: 17
• Joined: 31-March 11

## Re: maximum and minimum value of array

Posted 02 April 2011 - 07:38 AM

okay. this is weird. im getting a maximum value, but my minimum is still garbage.

### #11 CTphpnwb

• D.I.C Lover

Reputation: 3695
• Posts: 13,355
• Joined: 08-August 08

## Re: maximum and minimum value of array

Posted 02 April 2011 - 07:39 AM

No one here is clairvoyant. If you don't show your code we can't divine it or the problem.

### #12 serj779

Reputation: 0
• Posts: 17
• Joined: 31-March 11

## Re: maximum and minimum value of array

Posted 02 April 2011 - 07:46 AM

ok, im sorry.
```float minimum, maximum;
minimum = numarray[0].height;
maximum = numarray[0].height;
for(int i = 0; i < MAX; i++) {
if(numarray[i].height < minimum) {
minimum = numarray[i].height;
} else if(numarray[i].height > maximum) {// i changed the num from ur code to i and this gives me the maximum value
maximum = numarray[i].height;

}

}

```

### #13 CTphpnwb

• D.I.C Lover

Reputation: 3695
• Posts: 13,355
• Joined: 08-August 08

## Re: maximum and minimum value of array

Posted 02 April 2011 - 07:57 AM

Second, and far more important: You need to start thinking logically about this. Nobody needs to see my code again. We need to see your code, specifically where your code defines your struct and initializes an array of that type.

### #14 serj779

Reputation: 0
• Posts: 17
• Joined: 31-March 11

## Re: maximum and minimum value of array

Posted 02 April 2011 - 08:15 AM

Its ok. Alright.

```#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<sstream>
using namespace std;
#define MAX 20
struct DATA
{
int id;
string name;
float height;

}numarray[MAX];
int main()
{
int num = 0;
numarray[num].height = fstr3;// contains float values from a file
float minimum, maximum;
minimum = numarray[0].height;
maximum = numarray[0].height;
for(int i = 0; i < MAX; i++)
{
{
if(numarray[i].height < minimum)
minimum = numarray[i].height;
}

else if(numarray[i].height > maximum)
{
maximum = numarray[i].height;
}

}
cout<< minimum<< "    " << maximum<< endl;
return 0;
{

```

### #15 Oler1s

• D.I.C Lover

Reputation: 1397
• Posts: 3,884
• Joined: 04-June 09

## Re: maximum and minimum value of array

Posted 02 April 2011 - 10:06 AM

> im getting a maximum value, but my minimum is still garbage.

Your above code wouldn't even compile. That much is obvious, even with the lack of indent style.