Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,228 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,902 people online right now. Registration is fast and FREE... Join Now!




Getting maximum and minimum values in an array

 
Reply to this topicStart new topic

Getting maximum and minimum values in an array

snjugunachege
13 Mar, 2007 - 11:13 AM
Post #1

New D.I.C Head
*

Joined: 5 Mar, 2007
Posts: 6


My Contributions
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:
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;
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Getting Maximum And Minimum Values In An Array
13 Mar, 2007 - 11:29 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
CODE

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.

User is offlineProfile CardPM
+Quote Post

snjugunachege
RE: Getting Maximum And Minimum Values In An Array
14 Mar, 2007 - 11:45 PM
Post #3

New D.I.C Head
*

Joined: 5 Mar, 2007
Posts: 6


My Contributions
QUOTE(Amadeus @ 13 Mar, 2007 - 12:29 PM) *

CODE

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
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 02:57PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month