6 Replies - 6135 Views - Last Post: 03 May 2012 - 01:39 PM Rate Topic: -----

#1 loveandwar  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 14
  • Joined: 08-April 12

Max and min value of array

Posted 02 May 2012 - 09:22 AM

hi ,
how can i find maximum and minimum value of this array
include<iostream>

using namespace std;
int division(int,int);
main()
{
      int size=5;              //Array size
      int array[size];          //Declaring array
      int sum=0;                
      for(int i=0;i<size;i++)   //Loop which inputs arrays data and
                                //Calculates its sum
      {
              cout<<"Enter element number "<<i+1<<endl;
              cin>>array[i];
              sum=sum+array[i];
      }
      //Now calling division function to find the average...
      cout<<"Average of array elements is "<<division(sum,size);
      
}
int division(int sum,int size)
{
    return sum/size;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Max and min value of array

#2 loveandwar  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 14
  • Joined: 08-April 12

Re: Max and min value of array

Posted 02 May 2012 - 09:36 AM

hi ,
how can i find the maximum and minimum value of this array

include<iostream>

using namespace std;
int division(int,int);
main()
{
      int size=5;              //Array size
      int array[size];          //Declaring array
      int sum=0;                
      for(int i=0;i<size;i++)   //Loop which inputs arrays data and
                                //Calculates its sum
      {
              cout<<"Enter element number "<<i+1<<endl;
              cin>>array[i];
              sum=sum+array[i];
      }
      //Now calling division function to find the average...
      cout<<"Average of array elements is "<<division(sum,size);
      
}
int division(int sum,int size)
{
    return sum/size;
}

Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,237
  • Joined: 21-June 11

Re: Max and min value of array

Posted 02 May 2012 - 09:40 AM

int division(int sum,int size)
{
    return sum/size;
}



If I may ask, what exactly was your motivation for writing that function?
Was This Post Helpful? 0
  • +
  • -

#4 loveandwar  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 14
  • Joined: 08-April 12

Re: Max and min value of array

Posted 02 May 2012 - 10:01 AM

View Postsepp2k, on 02 May 2012 - 09:40 AM, said:

int division(int sum,int size)
{
    return sum/size;
}



If I may ask, what exactly was your motivation for writing that function?

Quote

all i concern my code is working or not , as it seems to me it is working but i don't know how can i find the maximum and minimum value from that array, i will appreciate if you will help rather than pointing my code .thanks

Was This Post Helpful? 0
  • +
  • -

#5 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: Max and min value of array

Posted 02 May 2012 - 10:08 AM

Let the first element be min. Then iterate through the array, changing min if you find a smaller element.
Was This Post Helpful? 0
  • +
  • -

#6 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Max and min value of array

Posted 02 May 2012 - 11:15 AM

To find the maximum and minimum value,Initialize both max and min to the very first element of your array.Then compare both of the variable with rest of your array,if min < arr[i] then assign arr[i] to min,similarly you test the max just with reverse condition.
Was This Post Helpful? 0
  • +
  • -

#7 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Max and min value of array

Posted 03 May 2012 - 01:39 PM

Quote

how can i find maximum and minimum value of this array

You know ... 'sepp2k' raised a valid question ... Coding can be very concise ... i.e. many precise meanings are implied by 'saying' just a few 'words' ... and also many insights may be gleaned ... by the use/misuse of code :)
include <iostream>

using namespace std;

double average( int[], int );

int main() // main returns an int ...
{
      const int size = 5; // Array size; Note const int
      int array[size]; // Get memory for 5 int's                
      for( int i = 0; i < size; ++i ) // Data input loop
      {
              cout << "Enter element number " << i+1 << ": " << flush;
              cin >> array[i];
      }

      //Now calling function to find the average...
      cout << "Average of array elements is: "<< average( array, size );
}

double average( int ary[], int size )
{
     double sum = 0.0;
     for( int i = 0; i <= size; ++i ) sum += ary[i];
     return sum/size;
}


Hint: your functions to return max and min values will have similar prototypes ...
int max( int ary[], int size );
int min( int ary[], int size );

This post has been edited by David W: 03 May 2012 - 02:41 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1