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

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




Sort with Pointers

 
Reply to this topicStart new topic

Sort with Pointers

dewjunkie
post 22 Mar, 2008 - 04:14 PM
Post #1


New D.I.C Head

*
Joined: 6 Mar, 2008
Posts: 49

Hey all!

I was hoping to get some help with a problem.

My assignment requires that I write code that will sort an array with pointers. It requires that I take the original array (for.example: iint NumList[] = {5, 34, 32, 25, 75, 42, 22, 2}; and output the array in ascending order, its original order, and its descending order.

My problem isn't sorting the array into the different orders, it is doing it with pointers.

Right now I am sorting it correctly, just not the way I am supposed to sort it.



Hopefully, somebody could help me out some!

Thanks in advance to whomever is awesome enough to help me!!

This is what I have right now:

CODE

#include <iostream>
#include <iomanip>
using namespace std;


int main()

{

int NumList[8] = {5, 34, 32, 25, 75, 42, 22, 2};

int *ascend = NumList;
int *descend = NumList;


int Swap;


//cout << "Ascending" << setw(16) << "Original " << setw(16) << "Descending" <<"\n";

//cout << "=========" <<setw(15)<< "========" << setw(16) << "==========" <<"\n";

cout << "Original\n";
for(int ctr = 0; ctr < 8; ctr++)

{
cout<< setw(6) << NumList[ctr]<<"\n";
}

cout<<"\n";

//cout << "\n\n";


for(int i = 0; i < 7; i++)
for(int ii = 0; ii < 7; ii++)

if (NumList[ii] > NumList[ii + 1])

{

Swap = NumList[ii];

NumList[ii] = NumList[ii + 1];

NumList[ii + 1] = Swap;

}

cout << "Ascending\n";

for (int iii=0; iii<8; iii++)
cout<< setw(6) << NumList[iii]<<"\n";

//for (int jj = 0; jj < 8; jj++)
//cout << NumList[jj] << " ";
//int aArray[8] = NumList[iii]
//cout << "\n\n";

//cout << "Original\n";

//for (int i = 0; i < 8; i++)
//cout << setw(6) << NumList[i]<<"\n";

cout << "\nDescending\n";

for (int jiii = 7; jiii >= 0; jiii--)
cout << setw(6) << NumList[jiii] <<"\n";

cout << "\n\n";


//system("pause");
}
User is offlineProfile CardPM

Go to the top of the page

Mila
post 22 Mar, 2008 - 04:43 PM
Post #2


D.I.C Head

Group Icon
Joined: 28 Oct, 2006
Posts: 88



Dream Kudos: 25
My Contributions


Well, one thing to remember is that int NumList[8] is the same thing as int *NumList = new int[8];.

If you replace int NumList[8] = {...} in your code with int *NumLst = new int[8]; and then some sort of instantiation for NumList, your code will still work as-is.


... not sure how helpful that just was. Just pointing out that an array name is technically a pointer to the first element in the array.

Cheers!
-- Mila
User is offlineProfile CardPM

Go to the top of the page

dewjunkie
post 22 Mar, 2008 - 05:39 PM
Post #3


New D.I.C Head

*
Joined: 6 Mar, 2008
Posts: 49

Right. I am just trying to get it to work for one case before I make it more generic.

User is offlineProfile CardPM

Go to the top of the page

Stepler
post 23 Mar, 2008 - 01:52 AM
Post #4


New D.I.C Head

*
Joined: 4 Mar, 2008
Posts: 27



Thanked 2 times
My Contributions


Here look an example.
CODE

for(int i = 0; i < 7; i++)
for(int ii = 0; ii < 7; ii++)
{
if(*(ascend+ii)>*(ascend+ii+1))
{
  *(ascend+ii)+=*(ascend+ii+1);
  *(ascend+ii+1)=*(ascend+ii)-*(ascend+ii+1);
  *(ascend+ii)-=*(ascend+ii+1);
}
}
User is offlineProfile CardPM

Go to the top of the page

dewjunkie
post 24 Mar, 2008 - 10:02 AM
Post #5


New D.I.C Head

*
Joined: 6 Mar, 2008
Posts: 49

Hey all! Thanks for the help so far.

Right now my code is still how it is when I posted it before. Just need some help still.

I am doing a bubble sort to take the values in an array and output them in ascending order, then the original order, and finally descending order. I have to do this using pointers. I can't make a copy of the original array, but I have to manipulate the order.

So if I have an array array[] = {4,2,1,5,3}; and then I do a bubble sort so it becomes
array[]={1,2,3,4,5}; How do I then backtrack to my original array? Because when I doing my bubble sort I am overwriting the values in the 2 new pointer arrays that I create. And since I change those values and they point to my original array, it overwrites that array. How might I do the bubble sort without overwriting the values?

I am pretty sure that the problem is in my pointers

Here is a picture of what exactly I am attempting to do.


Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM

Go to the top of the page

logon
post 17 May, 2008 - 02:20 PM
Post #6


New D.I.C Head

*
Joined: 17 May, 2008
Posts: 1

i have similar problem as you have,,i am trying the change pointers without modifying array,but i could not succeed
if you solved problem please send me the solution
User is offlineProfile CardPM

Go to the top of the page

KYA
post 17 May, 2008 - 05:14 PM
Post #7


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,187



Thanked 48 times

Dream Kudos: 1150
My Contributions


Save a copy of the original array somewhere before sorting
User is online!Profile CardPM

Go to the top of the page

skaoth
post 17 May, 2008 - 08:21 PM
Post #8


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 337



Thanked 9 times

Dream Kudos: 100
My Contributions


It almost seems you want an array of values and an array of int pointers that you can manipulate that points to the array of values

something like this maybe?
CODE

void main(int argc, char* argv[])
{

    int NumList[8] = {5, 34, 32, 25, 75, 42, 22, 2};
    int *aryPtrs[8] = {0};

    // Use an extra array of int pointers
    for(int i = 0; i < sizeof(NumList) / sizeof(NumList[0]); i++)
    {
        // Assigned the addrres to the array of int pointers
        aryPtrs[i] = &NumList[i];
    }

    // bubbleSort descending
    for(int m = 0; m < sizeof(NumList) / sizeof(NumList[0]); m++)
        for(int n = m; n < sizeof(NumList) / sizeof(NumList[0]); n++)
        {
            if(*aryPtrs[n] > *aryPtrs[m])
            {
                // swap
                int *tmp = aryPtrs[m];
                aryPtrs[m] = aryPtrs[n];
                aryPtrs[n] = tmp;
            }
        }

    std::cout << "----------------------\n";
    for(int i = 0; i < sizeof(NumList) / sizeof(NumList[0]); i++)
    {
        std::cout << "value: " << *aryPtrs[i] << std::endl;
    }
    system ("pause");
    return;

}
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 08:18PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month