MergeSort function not working with randomn number

This is a discussion on MergeSort function not working with randomn number within the C++ Programming forums, part of the General Programming Boards category; Hi, I am still a programmer in the process of learning, I am trying to implement this program with generates ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    21

    MergeSort function not working with randomn number

    Hi,
    I am still a programmer in the process of learning, I am trying to implement this program with generates 20 randomn numbers and uses the merge sort to organize them. My Function call doesn't seem to be working properly as I am able generate the numbers but not sort them properly. Thanks. The code is attached below.


    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    void Merge( float A[], int F, int Mid, int L);
    void Mergesort(float A[], int F, int L);
    
    int main(int argc, char *argv[])
    {
      float x[20000];
      int first, middle, last;
      x[0] = 1;
      for (int i = 0; i < 10; i++)
      {
      
      {
      x[i+1]=0.5*(x[i]-(4/(x[i])));
      if (x[i] <= 0)
      {
      cout << x[i] * -1 <<endl;
      }
      else {
      
      cout << x[i] << endl;
       }
       }
      }
      Merge (x, first, middle, last);
      Mergesort(x, first, last);
      for (int i = 0; i <10; i++)
      cout << x <<endl;	
      system("PAUSE");
      return 0;
      
    }
    
    void Merge( float A[], int F, int Mid, int L)
    {
    float x[20000];
    int First1 = F;
    int Last1 = Mid;
    int First2 = Mid + 1;
    int Last2 = L;
    
    int Index = First1;
    
    for (; (First1 <= Last1) && (First2 <= Last2); ++Index)
    {
    if (A[First1] < A[First2])
    {
    x[Index] = A[First1];
    ++First1;
    }
    else 
    {
    x[Index] = A[First2];
    ++First2;
    }
    }
    
    for (;First1 <=Last1; ++First1, ++Index)
    x[Index] = A[First1];
    
    for (;First2 <= Last2; ++First2, ++Index)
    x[Index] = A[First2];
    
    for (Index = F; Index <= L; ++Index)
    A[Index] = x[Index];
    system("PAUSE");
    return;
    }
    
    void Mergesort(float A[], int F, int L)
    {
    int Mid = (F + L)/2;
    Mergesort(A,F,Mid);
    Mergesort(A,Mid+1,L);
    
    Merge(A,F,Mid,L);
    system("PAUSE");
    return ;
    }

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    You never assign values to first, last, or middle. That might cause some problems.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    21

    Hi thanks

    I am still a bit confused, how do I assign values to first,last, and middle? Since I first generated some randomn numbers. Thank you sir.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Take a look between here:
    Code:
    int main(int argc, char *argv[])
    {
      float x[20000];
      int first, middle, last;
      //snip
      Merge (x, first, middle, last);
    And here. You never assign a value to first, middle, or last. You need to assign 0 to first, the last valid index to last, and some number in the middle to middle. Maybe:
    Code:
    first = 0;
    middle = 4;
    last = 10;
    Or something like that.

    By the way, I also get a Stack Overflow when trying to run. I'm almost positive that this is wrong:
    Code:
    void Mergesort(float A[], int F, int L)
    {
    int Mid = (F + L)/2;
    Mergesort(A,F,Mid);
    Mergesort(A,Mid+1,L);
    
    Merge(A,F,Mid,L);
    system("PAUSE");
    return ;
    }
    Consider the call to Mergesort with some array of floats, F = 0, L = 4. The next call is with F = 0, L = 2. The next call is with F = 0, L = 1. Every call after that will be with F = 0, L = 0, and it will do this until you run out of stack space. I'm pretty sure there's supposed to be some sort of condition in that Mergesort function.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Why don't you look here .

    The explaination is decent and you should see what you are doing wrong.

    To pianorain, yes there needs to be a check in there. All recursive functions need to have a default check to break out of the recursion cycle otherwise they will all cause a stack overflow.

    *edit*
    Look at what I just found at cprogramming.com
    Last edited by andyhunter; 02-11-2005 at 02:45 PM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Your problems would be a lot easier to find if your code wasn't so badly indented.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

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