By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

Arrays and Pointers

P: 20
I'm currently working on some more stuff for an assignment, this time with pointers. The current task is to bubble sort an array using a extrnal swap function that uses pointers, and to reference the array without using the Arrayname[] operator.

Anyways, here's what I've got:
Expand|Select|Wrap|Line Numbers
  1. int BubbleSort (int numlist[], int size, int order)
  2. {
  3.         int pass, num1, num2;
  4.         int cnt;
  5.                 for (pass = 0; pass < size - 1; pass ++)
  6.                 {
  7.                         for (cnt = 0; cnt < size - 1; cnt ++)
  8.                         {
  9.                                 if (order == 1)
  10.                                 {
  11.                                         if ((numlist + cnt) > (numlist + cnt + 1))
  12.                                         {  
  13.                                                 Swap((numlist + cnt), (numlist + cnt + 1));
  14.                                         }  
  15.                                 }
  16.  
  17.                                 else if (order == -1)
  18.                                 {
  19.                                         if ((numlist + cnt) < (numlist + cnt + 1))
  20.                                         {
  21.                                                 Swap((numlist + cnt), (numlist + cnt + 1));
  22.  
  23.                                         }
  24.                                 }
  25.                         }
  26.                 }
  27.         return 0;
  28. }
Feb 20 '07 #1
Share this Question
Share on Google+
14 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
I'm currently working on some more stuff for an assignment, this time with pointers. The current task is to bubble sort an array using a extrnal swap function that uses pointers, and to reference the array without using the Arrayname[] operator.
Cool. I personally have an issue with using that many embedded loops, but some people have no problem with it.

Did you have a question, or were you just posting code?
Feb 20 '07 #2

P: 20
Yeah, sorry I wasn't more clear about it. The problem is that...well, it doesn't work. I think the problem is with the if ((numlist + cnt) > (numlist + cnt + 1)) and if ((numlist + cnt) < (numlist + cnt + 1)) statements-I think they don't cycle through the array properly.
Feb 20 '07 #3

RedSon
Expert 5K+
P: 5,000
Cool. I personally have an issue with using that many embedded loops, but some people have no problem with it.

Did you have a question, or were you just posting code?
There is only one embedded loop. You think that is too much?
Feb 20 '07 #4

sicarie
Expert Mod 2.5K+
P: 4,677
There is only one embedded loop. You think that is too much?
Only one?
Expand|Select|Wrap|Line Numbers
  1. for (pass = 0; pass < size - 1; pass ++)
  2.                 {
  3.                         for (cnt = 0; cnt < size - 1; cnt ++)
  4.                         {
  5.                                 if (order == 1)
  6.                                 {
  7.                                         if ((numlist + cnt) > (numlist + cnt + 1))
  8.  
Not what I saw...
Feb 20 '07 #5

sicarie
Expert Mod 2.5K+
P: 4,677
Yeah, sorry I wasn't more clear about it. The problem is that...well, it doesn't work. I think the problem is with the if ((numlist + cnt) > (numlist + cnt + 1)) and if ((numlist + cnt) < (numlist + cnt + 1)) statements-I think they don't cycle through the array properly.
So are you getting an error message or an incorrect value?
Feb 20 '07 #6

RedSon
Expert 5K+
P: 5,000
Only one?
Expand|Select|Wrap|Line Numbers
  1. for (pass = 0; pass < size - 1; pass ++) //a loop
  2.                 {
  3.                         for (cnt = 0; cnt < size - 1; cnt ++) //an embedded loop
  4.                         {
  5.  
  6.  

Maybe I'm blind but I only count one embedded loop.
Feb 20 '07 #7

sicarie
Expert Mod 2.5K+
P: 4,677
Expand|Select|Wrap|Line Numbers
  1. for (pass = 0; pass < size - 1; pass ++) //a loop
  2.                 {
  3.                         for (cnt = 0; cnt < size - 1; cnt ++) //an embedded loop
  4.                         {
  5.  
  6.  

Maybe I'm blind but I only count one embedded loop.
And semantics kick my ass again.

You're right RedSon - I saw the conditionals as a buried loop. I guess the '{' indented on the other line really threw me.

Apologies, Acolyte & RedSon, I was incorrect. (though now I am going to try to find a better way - just for the challenge ;))
Feb 20 '07 #8

sicarie
Expert Mod 2.5K+
P: 4,677
Acolyte-

Why are you returning zero in your bubble sort method? Isn't that for main? This means, no matter what, you're always going to return 0 out of the statement - is that waht you want?
Feb 20 '07 #9

RedSon
Expert 5K+
P: 5,000
Anyways, what is this order business all about for your BubbleSort method. What does a call to Bubblesort look like?
Feb 20 '07 #10

P: 20
sicarie-an incorrect value, namely the unsorted array. And the return 0; was a brain fart (It was originally in main before I reorganized the program-I just forgot to delete it!) However, removing it doesn't do anything.

RedSon:
Here's the call in main that calls the sort:
Expand|Select|Wrap|Line Numbers
  1. case 3:
  2.                                 printf("Please enter the size of the array: ");
  3.                                 scanf("%d", &size);
  4.  
  5.                                 printf("Please enter the array data: ");
  6.                                 for (cnt = 0; cnt < size; cnt ++)
  7.                                 {
  8.                                         scanf("%d", &numlist[cnt]);
  9.                                 }
  10.  
  11.                                 printf("Please enter the sort order: ");
  12.                                 scanf("%d", &order);
  13.  
  14.                                 BubbleSort(numlist, size, order);
  15.  
  16.                                 for (cnt = 0; cnt < size; cnt ++)
  17.                                 {
  18.                                         printf("%d ", numlist[cnt]);
  19.                                 }
  20.                                 printf("\n");
  21.                         break;
And, if anybody needs to see it, the Swap function:
Expand|Select|Wrap|Line Numbers
  1. int Swap (int *aPtr, int *bPtr) 
  2. {
  3.         int hold;
  4.         hold = *aPtr;
  5.         *aPtr = *bPtr;
  6.         *bPtr = hold;
  7.         return 0;
  8. }
Feb 20 '07 #11

Ganon11
Expert 2.5K+
P: 3,652
Yeah, sorry I wasn't more clear about it. The problem is that...well, it doesn't work. I think the problem is with the if ((numlist + cnt) > (numlist + cnt + 1)) and if ((numlist + cnt) < (numlist + cnt + 1)) statements-I think they don't cycle through the array properly.
The problem here is that numlist is an array, but you aren't adding an element of that array to cnt and 1. Without specifying an index (such as numlist[4] or something), you are adding cnt and 1 to the address of the array, which is going to give weird results. Reconsider what the BubbleSort algorithm requires and rewrite the if statements.
Feb 20 '07 #12

P: 20
Normally, I would and all would be well, except that the prof has specificed that we can't use Array[] anywhere other then the declaration of the array.
Feb 20 '07 #13

DeMan
100+
P: 1,806
have you considered
Expand|Select|Wrap|Line Numbers
  1. *(numlist + cnt)
  2.  
ie the value at cnt items from numlist
Feb 20 '07 #14

P: 20
Ah, thanks DeMan, that was half the problem, and I realized the other half.
The correct version is:
Expand|Select|Wrap|Line Numbers
  1. int BubbleSort (int numlist[], int size, int order)
  2. {
  3.         int pass, num1, num2;
  4.         int cnt;
  5.                 for (pass = 0; pass < size - 1; pass ++)
  6.                 {
  7.                         for (cnt = 0; cnt < size - 1; cnt ++)
  8.                         {
  9.                                 if (order == 1)
  10.                                 {
  11.                                         if (*(numlist + cnt) > *(numlist + cnt + 1))
  12.                                         {
  13.                                                 Swap(&*(numlist + cnt), &*(numlist + cnt + 1));
  14.                                         }
  15.                                 }
  16.  
  17.                                 else if (order == -1)
  18.                                 {
  19.                                         if (*(numlist + cnt) < *(numlist + cnt + 1))
  20.                                         {
  21.                                                 Swap(&*(numlist + cnt), &*(numlist + cnt + 1));
  22.  
  23.                                         }
  24.                                 }
  25.                         }
  26.                 }
  27. }
Thanks!
Feb 20 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.