C++ sorting problem

Discussion in 'OT Technology' started by Caliban42, Feb 19, 2004.

  1. Caliban42

    Caliban42 For every sprinkle I find, I shall kill you.

    Joined:
    Dec 31, 2001
    Messages:
    104
    Likes Received:
    0
    I've got two pointer arrays. pOriDonation is supposed to hold the original order the donations are entered in while pDonation is supposed to hold the donations after being sorted in ascending order. The problem is that after I sort, both arrays end up being sorted. I'm sure I'm missing something really simple, so if someone could help out I'd appreciate it.

    Here's the program:

    #include <iostream>
    #include <fstream>
    #include <cstdlib>

    using namespace std;

    int GetSize (int);
    double* AllocateOriDonation (double*, int);
    double** AllocateDonation (double**, int);
    char** GetNames (char**, int);
    void GetDonation (double*, int, char**);
    void SortDonation (double*, double**, int);
    void PrintOriDonation (double*, int);
    void PrintDonation (double* , double**, int);

    int main()
    {
    double* pOriDonation = NULL;
    double** pDonation = NULL;
    char** names = NULL;
    int size = 0;

    size = GetSize(size);


    pOriDonation = AllocateOriDonation(pOriDonation, size);
    pDonation = AllocateDonation(pDonation, size);
    GetDonation(pOriDonation, size, names);
    PrintOriDonation (pOriDonation, size);
    PrintDonation (pOriDonation, pDonation, size);

    delete []pOriDonation;
    delete []pDonation;
    delete []names;

    return 0;
    }

    //****************************************************
    //Gets the number of donations

    int GetSize (int size)
    {
    cout << "How many donations are to be entered? ";
    cin >> size;

    return size;
    }

    //****************************************************
    //Allocates memory for pOriDonation

    double* AllocateOriDonation (double* pOriDonation, int size)
    {
    pOriDonation = new double[size];

    if (pOriDonation == NULL)
    {
    cout << "Memory Allocation Failed\n";
    exit(0);
    }

    return pOriDonation;
    }

    //****************************************************
    //Allocates memory for pDonation

    double** AllocateDonation (double** pDonation, int size)
    {
    pDonation = new double*[size];

    if (pDonation == NULL)
    {
    cout << "Memory Allocation Failed\n";
    exit(0);
    }

    return pDonation;
    }

    //****************************************************
    //Gets the names of the donors

    char** GetNames (char** names, int size)
    {
    names = new char*[size];

    for (int count = 0; count < size; count++)
    {
    names[count] = new char [30];
    }

    fstream inFile;
    inFile.open("names.txt");

    if (!inFile)
    {
    cout << "Can't open file\n";
    exit(0);
    }

    for (count = 0; count < size; count++)
    {
    inFile.getline (names [count],30);
    }

    inFile.close();

    return names;
    }

    //****************************************************
    //Gets the values of the donations

    void GetDonation (double* pOriDonation, int size, char** names)
    {
    names = GetNames(names, size);

    for (int count = 0; count < size; count++)
    {
    cout << "Enter donation by " << names[count] << ": ";
    cin >> pOriDonation[count];
    }
    }

    //****************************************************
    //Gets the values of the donations

    void SortDonation (double* pOriDonation, double** pDonation, int size)
    {
    bool swap;
    double temp = 0;
    int count;
    int grr = 3;

    for (count = 0; count < size; count++)
    {
    pDonation[count] = &pOriDonation[count];
    }

    do
    {
    swap = false;
    for (count = 0; count < (size - 1); count++)
    {

    if (*pDonation[count] > *pDonation[count +1])
    {
    temp = *pDonation[count];
    *pDonation[count] = *pDonation[count + 1];
    *pDonation[count+1] = temp;
    swap = true;
    }
    }
    }while (swap);

    }

    //****************************************************
    //Prints the values of the donations

    void PrintOriDonation (double* pOriDonation, int size)
    {
    cout << "The donations in their original order are :\n";

    for (int count = 0; count < size; count++)
    {
    cout << pOriDonation[count] << " ";
    }

    cout << endl;
    }

    //****************************************************
    //Prints the values of the sorted donations

    void PrintDonation (double* pOriDonation, double** pDonation, int size)
    {
    SortDonation (pOriDonation, pDonation, size);

    cout << "The donations sorted in descending order are :\n";

    for (int count = 0; count < size; count++)
    {
    cout << *pDonation[count] << " ";
    }

    cout << endl;
    }
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    perhaps your input is already sorted?
     
  3. Caliban42

    Caliban42 For every sprinkle I find, I shall kill you.

    Joined:
    Dec 31, 2001
    Messages:
    104
    Likes Received:
    0
    No, that's not it. When I input something like 25, 5, 20 if I print pOriDonation before sorting, it comes out 25, 5, 20. If I sort then print pOriDonation it is 5, 20, 25. It should stay 25, 5, 20 and only pDonation should actually end up sorted.
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Code:
    if (*pDonation[count] > *pDonation[count +1])
    {
    temp = *pDonation[count];
    *pDonation[count] = *pDonation[count + 1];
    *pDonation[count+1] = temp;
    swap = true;
    }
    
    It's your sort function. Since pDonation is an array of pointers to values in the pOriDonation array, you don't want to dereference the pointers when your swapping values during the sort. You want to swap pointers, not the values pointed to by the pointers.
     
  5. DFWSR4EVER

    DFWSR4EVER There's Stlil Time to Change the Road You're On

    Joined:
    Jul 29, 2001
    Messages:
    51,573
    Likes Received:
    0
    Location:
    Wisco

Share This Page