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: - int BubbleSort (int numlist[], int size, int order)
-
{
-
int pass, num1, num2;
-
int cnt;
-
for (pass = 0; pass < size - 1; pass ++)
-
{
-
for (cnt = 0; cnt < size - 1; cnt ++)
-
{
-
if (order == 1)
-
{
-
if ((numlist + cnt) > (numlist + cnt + 1))
-
{
-
Swap((numlist + cnt), (numlist + cnt + 1));
-
}
-
}
-
-
else if (order == -1)
-
{
-
if ((numlist + cnt) < (numlist + cnt + 1))
-
{
-
Swap((numlist + cnt), (numlist + cnt + 1));
-
-
}
-
}
-
}
-
}
-
return 0;
-
}
14 1492
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?
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.
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?
There is only one embedded loop. You think that is too much?
Only one? -
for (pass = 0; pass < size - 1; pass ++)
-
{
-
for (cnt = 0; cnt < size - 1; cnt ++)
-
{
-
if (order == 1)
-
{
-
if ((numlist + cnt) > (numlist + cnt + 1))
-
Not what I saw...
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?
Only one?
-
for (pass = 0; pass < size - 1; pass ++) //a loop
-
{
-
for (cnt = 0; cnt < size - 1; cnt ++) //an embedded loop
-
{
-
-
Maybe I'm blind but I only count one embedded loop.
-
for (pass = 0; pass < size - 1; pass ++) //a loop
-
{
-
for (cnt = 0; cnt < size - 1; cnt ++) //an embedded loop
-
{
-
-
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 ;))
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?
Anyways, what is this order business all about for your BubbleSort method. What does a call to Bubblesort look like?
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: - case 3:
-
printf("Please enter the size of the array: ");
-
scanf("%d", &size);
-
-
printf("Please enter the array data: ");
-
for (cnt = 0; cnt < size; cnt ++)
-
{
-
scanf("%d", &numlist[cnt]);
-
}
-
-
printf("Please enter the sort order: ");
-
scanf("%d", &order);
-
-
BubbleSort(numlist, size, order);
-
-
for (cnt = 0; cnt < size; cnt ++)
-
{
-
printf("%d ", numlist[cnt]);
-
}
-
printf("\n");
-
break;
And, if anybody needs to see it, the Swap function: - int Swap (int *aPtr, int *bPtr)
-
{
-
int hold;
-
hold = *aPtr;
-
*aPtr = *bPtr;
-
*bPtr = hold;
-
return 0;
-
}
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.
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.
have you considered
ie the value at cnt items from numlist
Ah, thanks DeMan, that was half the problem, and I realized the other half.
The correct version is: - int BubbleSort (int numlist[], int size, int order)
-
{
-
int pass, num1, num2;
-
int cnt;
-
for (pass = 0; pass < size - 1; pass ++)
-
{
-
for (cnt = 0; cnt < size - 1; cnt ++)
-
{
-
if (order == 1)
-
{
-
if (*(numlist + cnt) > *(numlist + cnt + 1))
-
{
-
Swap(&*(numlist + cnt), &*(numlist + cnt + 1));
-
}
-
}
-
-
else if (order == -1)
-
{
-
if (*(numlist + cnt) < *(numlist + cnt + 1))
-
{
-
Swap(&*(numlist + cnt), &*(numlist + cnt + 1));
-
-
}
-
}
-
}
-
}
-
}
Thanks!
Sign in to post your reply or Sign up for a free account.
Similar topics
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
>...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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);
|
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...
|
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:
...
|
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
|
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,...
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |