my recursive logic

This is a discussion on my recursive logic within the C++ Programming forums, part of the General Programming Boards category; I was wondering if my logic was correct here, or if there are some things to change, with the given ...

  1. #1
    *this
    Join Date
    Mar 2005
    Posts
    498

    my recursive logic

    I was wondering if my logic was correct here, or if there are some things to change, with the given numbers the program works fine. but then i tried to add it to another program that chooses which type of sort algorithm to use and it wouldnt work for beans.

    Code:
    #include <iostream>
    using namespace std;
    
    const int MAX = 10+1;
    
    void mergeSort (int [MAX], int , int);
    void merge (int [MAX], int, int, int);
    void print (const int [MAX]);
    
    main()
    {
       int array[MAX];
       array[0] = 2; array[1] = 3; array[2] = 1; array[3] = 2;
       array[4] = 134; array[5] = 2; array[6] = 13; array[7] = 343;
       array[8] = 1111; array[9] = 1; array[10] = 0;
       mergeSort (array, 1, MAX - 1);
       print (array);
       system("pause");
    }
    
    void mergeSort (int array[MAX], int first, int last)
    {
       int mid;
       
       if (first < last)
       {
          mid = (first + last) / 2;
          mergeSort (array, first, mid);
          mergeSort (array, mid+1, last);
          merge (array, first, mid, last);
       }
    }
    
    void merge (int array[MAX], int first, int mid, int last)
    {
       const int SIZE = (last - first) + 1;
       int temp[SIZE];
       int tIndex = 0, left = first, right = mid+1;
       
       while (left <= mid && right <= last)
       {
          if (array[left] <= array[right])
             temp[tIndex++] = array[left++];
          else 
             temp[tIndex++] = array[right++];
       }
       
       while (left <= mid) temp[tIndex++] = array[left++];
       while (right <= last) temp[tIndex++] = array[right++];
       
       for (int count = 0; count < SIZE; count++)
          array[first+count] = temp[count];
    }
    
    void print (const int array[MAX])
    {
       for (int count = 1; count < MAX; count++)
          cout << array[count] << "  ";
    }

  2. #2
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Just a quick note, instead of:
    Code:
    int array[MAX];
    array[0] = 2; array[1] = 3; array[2] = 1; array[3] = 2;
    array[4] = 134; array[5] = 2; array[6] = 13; array[7] = 343;
    array[8] = 1111; array[9] = 1; array[10] = 0;
    You can use:
    Code:
    int array[MAX] = {2, 3, 1, 2, 134, 2, 13, 343, 1111, 1, 0};
    That wouldn't cause an error though. As far as I can tell, it seems okay. Maybe somebody else will be able to point out where it's going wrong, but I can't...When added it to the other program, was it just a matter of it not sorting properly? Or did something else not work?...
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    ya it was a pain, apvector was used so i had to change some things, and adapt it to the programs environment. so it didnt sort correctly thats probably where the problem is. thanks.
    Last edited by JoshR; 04-14-2005 at 10:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 04:42 AM
  2. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 09:34 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21