maximum and minimum value of array

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 5170 Views - Last Post: 02 April 2011 - 01:42 PM Rate Topic: -----

#1 serj779  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: maximum and minimum value of array

Posted 02 April 2011 - 05:28 AM

Please show snippet of your code so we can help out.

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

Was This Post Helpful? 0
  • +
  • -

#3 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • 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 :)
Was This Post Helpful? 1
  • +
  • -

#4 serj779  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :smile2:
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,491
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 serj779  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,491
  • 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;
  }
}

Was This Post Helpful? 1
  • +
  • -

#8 serj779  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,491
  • 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.
Was This Post Helpful? 1
  • +
  • -

#10 serj779  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,491
  • 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.
Was This Post Helpful? 0
  • +
  • -

#12 serj779  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;

  }

}


Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,491
  • Joined: 08-August 08

Re: maximum and minimum value of array

Posted 02 April 2011 - 07:57 AM

First, sorry about the typo.

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.
Was This Post Helpful? 0
  • +
  • -

#14 serj779  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
{



Was This Post Helpful? 0
  • +
  • -

#15 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2