# Divide and conquer

Page 1 of 1

## 7 Replies - 15218 Views - Last Post: 16 September 2007 - 12:20 PMRate 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=30505&amp;s=aba377b8324f7ba761a4a286d7809927&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sari563

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

# Divide and conquer

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

• Barbarbar

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

## Re: Divide and conquer

Posted 17 July 2007 - 12:57 AM

### #3 mohanrohith

Reputation: 0
• 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 display();
void call_fun();
void minmax(int,int,int,int);
};

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

{
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.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...

### #4 ajaymatrix

• D.I.C Regular

Reputation: 1
• 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..

### #5 NickDMax

Reputation: 2255
• 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.

### #6 mohanrohith

Reputation: 0
• 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.....

### #7 mohanrohith

Reputation: 0
• 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 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;
}

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.call_fun();
A.display();
}

```

### #8 NickDMax

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

## Re: Divide and conquer

Posted 16 September 2007 - 12:20 PM

Thank you for posting your solution!!