473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Arrays and Pointers

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
14 1492
sicarie
4,677 Expert Mod 4TB
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
Acolyte
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
5,000 Expert 4TB
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
4,677 Expert Mod 4TB
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
4,677 Expert Mod 4TB
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
5,000 Expert 4TB
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
4,677 Expert Mod 4TB
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
4,677 Expert Mod 4TB
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
5,000 Expert 4TB
Anyways, what is this order business all about for your BubbleSort method. What does a call to Bubblesort look like?
Feb 20 '07 #10
Acolyte
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
3,652 Expert 2GB
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
Acolyte
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
1,806 1GB
have you considered
Expand|Select|Wrap|Line Numbers
  1. *(numlist + cnt)
  2.  
ie the value at cnt items from numlist
Feb 20 '07 #14
Acolyte
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

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

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
11
by: Linny | last post by:
Hi, I need some help in declaring an array of pointers to array of a certain fixed size. I want the pointers to point to arrays of fixed size only (should not work for variable sized arrays of the...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
8
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along...
36
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
1
by: Nathan Gilbert | last post by:
I have a function that is returning a 2D array (declared using double pointers) and I want to be able to append a row of data to the beginning and end of this array without having to create a new...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.