2 Replies - 28446 Views - Last Post: 15 March 2007 - 12:45 AM Rate Topic: -----

#1 snjugunachege  Icon User is offline

  • New D.I.C Head

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

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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  Icon User is offline

  • New D.I.C Head

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

Re: Getting maximum and minimum values in an array

Posted 15 March 2007 - 12:45 AM

View PostAmadeus, 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