Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,582 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,942 people online right now. Registration is fast and FREE... Join Now!




Merge Sort

 
Reply to this topicStart new topic

Merge Sort

fountainoftruth
26 Sep, 2008 - 01:49 AM
Post #1

D.I.C Head
Group Icon

Joined: 4 Dec, 2007
Posts: 73


My Contributions
CODE

#include <iostream>
#include <fstream>

using namespace std;

void merge(double numbers[], double temp[], int left, int middle, int right) {
    int i, leftSide, numElements, tempPosition;

    leftSide = (middle-1);
    tempPosition = left;
    numElements = (right - left + 1);

    while((left <= leftSide) && (middle <= right)) {
        if(numbers[left] <= numbers[middle]) {
            temp[tempPosition] = numbers[left];
            tempPosition++;
            left++;
        }
        else {
            temp[tempPosition] = numbers[middle];
            tempPosition++;
            middle++;
        }
    }
    
    while(left <= leftSide) {
        temp[tempPosition] = numbers[left];
        left++;
        tempPosition++;
    }

    while(middle <= right) {
        temp[tempPosition] = numbers[middle];
        middle++;
        tempPosition++;
    }

    for(i=0; i<=numElements; i++) {
        numbers[right] = temp[right];
        right--;
    }
    
}

void mergeSort(double numbers[], double temp[], int left, int right) {
    int middle;
    if(right > left) {
        middle = (right + left) / 2;
        mergeSort(numbers, temp, left, middle);
        mergeSort(numbers, temp, (middle+1), right);

        merge(numbers, temp, left, (middle+1), right);
    }
}

int main() {
    fstream testFile;
    double numbers[200];
    int aSpot = 0;

    testFile.open("test.txt", fstream::in);
    if(!testFile) {
        cout << "For whatever reason, the file cannot be opened." << endl;
        return -1;
    }
    while(!testFile.eof()) {
        testFile.operator >>(numbers[aSpot++]);
    }

    double temp[200];

    mergeSort(numbers, temp, 0, aSpot);
    for(int a = 0; a < aSpot; a++)
        cout << numbers[a] << " ";
    return 0;
}

It's code modified from the snippet found here.
PROBLEM DESCRIPTION
Your program should read from a file and write to a file. ALL programs must run on CSP machines Take the size of your array to be 200. Your program must sort the numbers in
each line in the input file.
Example input:
2.5 600 -2 100
200 400 20.57 36
Example Output:
-2, 2.5, 100, 600
20.57, 36, 200, 400

PROBLEMS
None when I run it in Visual Studio. It compiles and gives me a correct answer. When I run it on the csp machines using g++, it doesn't print anything. I figure it's a simple error between compilers, but I can't figure out what. I'm also trying to figure out how best to sort line by line as opposed to what I have now which is just sorting the whole file at once.

Thanks if anyone helps. I have this due this afternoon and really need to get cracking on studying for my German test in the morning.


Fixed it. I'll post the new code tomorrow showing what I did to fix what was wrong.

This post has been edited by fountainoftruth: 26 Sep, 2008 - 08:20 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:32AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month