-4

I have to find the minimum and maximum value of elements in a array using divide and conquer. I have written a code but it is not working for more then 6 elements in array. I don't know whats the problem

#include<iostream>
using namespace std;
int minimum=999,maximum,mi,ma;
void result(int mi,int ma)
{
    if(maximum<ma)
    {
        maximum=ma;
    }
    if(minimum>mi)
    {
        minimum=mi;
    }
}
void maxmin(int arr[],int i,int j)
{
    cout<<" i ="<<i<<" j= "<<j<<endl;
    if(i==j)
    {
        mi=ma=arr[i];
        result(mi,ma);
    }
    else if(i==j-1)
    {
        if(arr[i]>arr[j])
        {
            ma=arr[i];
            mi=arr[j];
        }
        else
        {
            mi=arr[i];
            ma=arr[j];
        }
        result(mi,ma);
    }
    else
    {
        int mid=i+j/2;
        maxmin(arr,i,mid);
        maxmin(arr,mid+1,j);
    }
}
int main()
{
    int arr[10],n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    maxmin(arr,0,n-1);
    cout<<" max "<<maximum<<" min "<<minimum<<endl;
    return 0;
}
  • 1
    have you tried using a debugger? – user2717954 Mar 26 '17 at 06:52
  • Do you want to sort the array or just find max and min? – STF Mar 26 '17 at 06:53
  • 2
    at first `int mid=i+j/2;` is incorrect. Probably you want to write `int mid=(i+j)/2;` – JustRufus Mar 26 '17 at 07:02
  • Why do you want to use divide and conquer for finding the max and min? if the array is sorted so you don't need to look for them they are the first and last in the array – STF Mar 26 '17 at 07:02
  • Note: when passing arrays to a function as parameter it treats the array as the original, it does not make a copy of it, thus if you edit the array inside the function it would edit the original one, unless you make the array a const! – Felipe Lopez Mar 26 '17 at 07:05
  • It seems like a lot of overhead for just finding min and max. – Jonas Mar 26 '17 at 07:11
  • 1
    You should generally avoid global variables. And even more important is it to never use an uninitialized variable. Both comments are of course related to `int minimum=999,maximum,mi,ma;`, which are global variables and `maximum` is used uninitialized. – Jonas Mar 26 '17 at 07:16

2 Answers2

1

Your code has a few mistakes

  1. Your code reads n from the user input, but you provided only 10 sized array, and user can try to input 10+ numbers, so we will have an undefined behavior in that case.
  2. You write it very bad and unreadable. If you want somebody else to read your code, check in the your favourite book or in the internet information about how to write beautiful and readable code.
  3. You implemented that algorithm yourself. It is a bad habit, use the standard library algorithms and you will not encounter such mistake.

.

#include <iostream> // std::cin, std::cout
#include <cstddef> // std::size_t
#include <algorithm> // std::min_element, std::max_element

int main ()
{
    std::size_t array_size;
    std::cin >> array_size;

    int *some_array = new int[array_size]; // Allocate memory dynamically
    for(std::size_t i = 0; i < array_size; ++i)
    {
        std::cin >> some_array[i];
    }

    /* Standard library operate on iterators, they are special classes
     * that have interface that is similar in many cases to pointers (so we can use pointers as iterators).
     * std::min/max_element needs one iterator for the sequence beginning  
     * and one iterator after the end. It returns iterator to a found element. 
    */
    int min = *std::min_element(some_array, some_array + array_size);
    int max = *std::max_element(some_array, some_array + array_size);

    delete[] some_array;

    std::cout << "Min = " << min << std::endl << "Max = " << max;
    std::cout << std::endl;
}
Inline
  • 2,566
  • 1
  • 16
  • 32
0

Code isn't well written and first dry run your code, you will find the problem easily.

Change

else
    {
        int mid=i+j/2;
        maxmin(arr,i,mid);
        maxmin(arr,mid+1,j);
    }

To

else
    {
        int mid=(i+j)/2; /*** Adding brackets  ***/
        maxmin(arr,i,mid);
        maxmin(arr,mid+1,j);
    }

And check the logic for calling the result function (because according to your logic the two subsets are individually calculating MIN and MAX in itself not in whole array)

acraig5075
  • 10,588
  • 3
  • 31
  • 50
avish
  • 11
  • 2