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 ;
}