# Getting maximum and minimum values in an array

Page 1 of 1

## 2 Replies - 31346 Views - Last Post: 15 March 2007 - 12:45 AMRate 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=25032&amp;s=fbb68516b1afc63004d7a7ffa679f067&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 snjugunachege

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 05-March 07

# Getting maximum and minimum values in an array

Posted 13 March 2007 - 12:13 PM

I was trying to create a program to calculate the sum, average, maximum and minimum numbers in an array. My program worked for the sum and the average but failed in getting the maximum and minimum values in the array. Can somebody help me solve this please....

Here is my code:
```#include<iostream.h>

float computeAvg(float b[], int n)
{

float sum1=0;
int c;
for( c=0; c<n;c++)
{
sum1=sum1+b[c];
}
return (sum1/n);
}

//compute maximum value in the array

float computemax(float b[],int n)
{
float max;
max=b[n];
for(int c=0; c<n; c++)
{

if(max>b[c])
max=b[c];
}

return max;
}//end of function

//the main function

int main()
{
float mysum=0;
int a;
int d=1;
float myFunc;

cout<<"Enter number of students: ";
cin>>a;
float *mark;
mark=new float[a];

for (int m=0;m<a;m++ )
{

cout<<"Enter number "<<d++<<":";
cin>>mark[m];
mysum=mysum+mark[m];
}

float myMax=computemax(mark,a);
myFunc=computeAvg(mark,a);
cout<<"The sum is: "<<mysum<<"\n";
cout<<"The average is: "<<myFunc<<"\n";
cout<<"The highest mark is: "<<myMax;
delete []mark;
return 0;
}
```

Is This A Good Question/Topic? 0

## Replies To: Getting maximum and minimum values in an array

### #2 Amadeus

• g+ + -o drink whiskey.cpp

Reputation: 248
• Posts: 13,507
• Joined: 12-July 02

## Re: Getting maximum and minimum values in an array

Posted 13 March 2007 - 12:29 PM

```float computemax(float b[],int n)
{
float max = 0.0;
for(int c=0; c<n; c++)
{

if(b[c]>max)
max=b[c];
}

return max;
}//end of function

```

Same idea for min, but reverse the comparison.
Was This Post Helpful? 0

### #3 snjugunachege

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 05-March 07

## Re: Getting maximum and minimum values in an array

Posted 15 March 2007 - 12:45 AM

Amadeus, on 13 Mar, 2007 - 12:29 PM, said:

```float computemax(float b[],int n)
{
float max = 0.0;
for(int c=0; c<n; c++)
{

if(b[c]>max)
max=b[c];
}

return max;
}//end of function

```

Same idea for min, but reverse the comparison.

Thanks for your help. It works for getting the maximum value but not for getting minimum value- i still have a problem getting minimum value- please help
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }