Divide and conquer

Maximum and Minimum

Page 1 of 1

7 Replies - 15218 Views - Last Post: 16 September 2007 - 12:20 PM Rate Topic: -----

#1 sari563  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 09-April 07

Divide and conquer

Post icon  Posted 17 July 2007 - 12:03 AM

Please send me the code for finding the Maximum and Minimum By using divide and Conquer Method in C++....
Is This A Good Question/Topic? 0
  • +

Replies To: Divide and conquer

#2 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Divide and conquer

Posted 17 July 2007 - 12:57 AM

Please show us some work so that we can help you out with your problem....
Was This Post Helpful? 0
  • +
  • -

#3 mohanrohith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-July 07

Re: Divide and conquer

Posted 21 July 2007 - 03:54 AM

i hav the same problem...
and my code is this..

#include<iostream>

using namespace std;

class array
{
	int a[50],min,max,n;
	
	public:
		array(int);
		void read();
		void display();
		void call_fun();
		void minmax(int,int,int,int);
};


array::array(int i)
{
	n=i;
	min=a[0];
	max=a[0];
}


void array::read()
{
	int i;
	
	for(i=0;i<n;i++)
		cin>>a[i];
}


void array::display()
{
	cout<<"Maximum: "<<max<<endl;
	cout<<"Minimum: "<<min<<endl;
}


void array::call_fun()
{
	minmax(0,n-1,min,max);
}


void array::minmax(int start,int end,int mn,int mx)
{
	int mn1,mn2,mx1,mx2;
	if(start==end)
	{
		mx=a[start];
		mn=a[start];
		return;
	}
	
	if(end-start==1)
	{
		if(a[start]>a[end])
		{
			mn=a[end];
			mx=a[start];
			return;
		}
		else
		{
			mx=a[start];
			mn=a[end];
			return;
		}
	}
	else
	{
		int m=(start+end)/2;
		minmax(start,m,mn1,mx1);
		minmax(m+1,end,mn1,mx2);
	}
	
	(mn1>mn2)?min=mn2:min=mn1;
	(mx1>mx2)?max=mx1:max=mx2;

}



main()
{
	int n;
	cout<<"Enter the no of elements"<<endl;
	cin>>n;
	
	array A(n);
	
	A.read();
	A.call_fun();
	A.display();
}




please some one help with this code..
there are no synatx error but can't find wat logical error has occured...
waiting for the reply......
Was This Post Helpful? 0
  • +
  • -

#4 ajaymatrix  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 1
  • View blog
  • Posts: 410
  • Joined: 15-May 07

Re: Divide and conquer

Posted 21 July 2007 - 10:13 AM

I haven't read the code completely..
but from primary inspection I guess when you use the ternary operator all the terms should be in brackets...

write like this:
(mn1>mn2)?(min=mn2):(min=mn1);

see if tht works..
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Divide and conquer

Posted 21 July 2007 - 07:43 PM

(mn1>mn2)?min=mn2:min=mn1;
actually this will work and is perfectly correct. The use of parenthesis might make the line a little clearer. If it were me I would have written this as:
min = (mn1>mn2)? mn2 : mn1;

However, since this line works either way, this will get mohanrohith no closer to his solution.

I can't say that I understand what your minmax() function is supposed to accomplish. Try commenting it and thinking about the logic of what you are trying to do. Post the commented code.
Was This Post Helpful? 0
  • +
  • -

#6 mohanrohith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-July 07

Re: Divide and conquer

Posted 24 July 2007 - 12:00 AM

my problem is to find the minmum value and maximum value in array by using divide and conqueror rule.....

i have also find some srror that i hav initialized the min and max to a garbage value.....
and also corrected the ternary operator problem.....
Was This Post Helpful? 0
  • +
  • -

#7 mohanrohith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-July 07

Re: Divide and conquer

Posted 14 September 2007 - 11:53 PM

guys i hav solved the problem using pointers..
i'm posting the code so some others can also refer it
thnx....... :) :) :)
#include<iostream>

using namespace std;

class arr {
	private:
		int *array,n,*min,*max;
	public:
		arr(int);
		~arr();
		void read();
		void call_fun();
		void minmax(int,int);
		void display();
};

arr::arr(int i){
	n=i;
	array=new int[n];
	min=new int;
	max=new int;
}

arr::~arr(){
	delete []array;
	delete min;
	delete max;
}

void arr::read(){
	cout<<"Enter the elements into array"<<endl;
	for( int i=0;i<n;i++ ){
		cin>>array[i];
	}
}

void arr::call_fun(){
	*min=array[0];
	*max=array[0];
	minmax(0,n-1);
}

void arr::minmax(int start,int end){
	if(start==end){
		if(*min>array[start])
			*min=array[start];
		if(*max<array[start])
			*max=array[start];
		return;
	}
	else{
		int mid=(start+end)/2;
		minmax(start,mid);
		minmax(mid+1,end);
	}
	
}

void arr::display(){
	cout<<"=================================="<<endl;
	cout<<"minimum :"<<*min<<endl;
	cout<<"maximum :"<<*max<<endl;
	cout<<"=================================="<<endl;
}


main(){
	cout<<"Enter how many elements :";
	int n;cin>>n;
	arr A(n);
	A.read();
	A.call_fun();
	A.display();	
}


Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Divide and conquer

Posted 16 September 2007 - 12:20 PM

Thank you for posting your solution!! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1