Changing from a Bubble Sort to a Return Void Function. Need help!

My teacher had us create a program using a Bubble Sort. I did that assignment correctly, but now his new assignment is to use that program and make changes. I have no idea how to do this. Here is his new assignment:

********************************************************
Implement the sort procedure from Assignment 18 as a void function. Pass the address of the first array element to the function along with the number of elements in the array. Use pointers in your function to reference the array elements. The sort function should do nothing but sort the elements of the array. DO NOT pass the entire array as an argument to the function.

As in Assignment 18, print out the array before and after sorting. Use the same test data.
**********************************************************

My code for the original program is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <stdio.h>

using namespace std;

int main()

{

    int x[] = { 28, 87, -3, 45, 19 };

    int ArrSize = sizeof( x ) / sizeof( int );

    int Num = 0;


    for( Num = 0 ; Num < ArrSize ; Num++)

        {

        printf("%d ",x[Num]);

        }

    printf("\n");


    for( int i = 0 ; i < ArrSize - 1 ; i++ ) // For I = 0 to (number of array elements - 2), increment by 1

        {

        for ( int j = i + 1 ; j < ArrSize ; j++ ) // For J = (I + 1) to (number of array elements - 1), increment by 1

            {

            if( x[i] > x[j] ) // If X[I] is greater than X[J] then

                {

                // swap X[I] and X[J]

                int Tmp = x[i];

                x[i] = x[j];

                x[j] = Tmp;

                }

            }

        }

    for( Num = 0 ; Num < ArrSize ; Num++)

        {

        printf("%d ",x[Num]);

        }

printf("\n");

}


I'm pretty sure I need to add "void Sort(int *x, int size);" and "Sort(x, sizeof(x)/sizeof(int))" somewhere but I don't know where. Can anyone help?

How comfortable are you with pointers? :)




Well your on the right track with void Sort(int *x, int size);. I would suggest you have a look at this: http://www.cplusplus.com/doc/tutorial/pointers/ to get a good understanding of pointers.

If you look at your existing Bubble Sort it uses array notation to cycle through the array; in your newly created function you would need to use pointer notation to access the different addresses where your 5 items are stored.

So, with that in mind, if I were accessing the 2nd element of my array using normal array notation I would use array[1], if I were to do that with pointers I would need to use *(array+1).

This is one of those posts where you think.. would it be beneficial to the OP if I wrote the solution to the problem, on this occasion I feel it would be beneficial... pointers are a pain in the butt, but once you master them your well on the way to becoming a good programmer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include <iostream>

void Sort(int*, int);

int main()
{

	int x[] = { 28, 87, -3, 45, 19 };
	Sort(x, 5);
	return 0;
}

void Sort(int *data, int size)
{
	int temp;
	for (int i = 0; i < size; i++)
		for (int j = 0; j < size - i - 1; j++)
			if (*(data+j) > *((data+j)+1))
			{
				temp = *(data + j);
				*(data + j) = *((data + j) + 1);
				*((data + j) + 1) = temp;
			}
}


If you have any questions please feel free to ask, pointers do take time to get your head around. :)


@softrix- Thank you! I'm not very comfortable with pointers, but I'm trying to learn:)

Good to see, and your welcome.

Make sure you have a strong understanding of my last post because its guaranteed your next assignment will be harder :)
*(data + j) is the same as data[j] or j[data] So you can write your bubble sort the same exact if you wish to.

You could literally copy/paste your bubble sort into a function then just call it in the main function like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void bubblesort(int x[], int ArrSize) //int array[] gets turned to int *array either way is fine
//since I copy/pasted I used your variable names but you can make them whatever if you wish
{
        for( int i = 0 ; i < ArrSize - 1 ; i++ ) // For I = 0 to (number of array elements - 2), increment by 1

        {

        for ( int j = i + 1 ; j < ArrSize ; j++ ) // For J = (I + 1) to (number of array elements - 1), increment by 1

            {

            if( x[i] > x[j] ) // If X[I] is greater than X[J] then

                {

                // swap X[I] and X[J]

                int Tmp = x[i];

                x[i] = x[j];

                x[j] = Tmp;

                }

            }

        }
}


Then call it like bubblesort(x, ArrSize); http://www.cplusplus.com/doc/tutorial/functions/


Pass the address of the first array element to the function along with the number of elements in the array. Use pointers in your function to reference the array elements.


@giblit, O/P is required to use a pointer with the address of the first element.
I'm fairly positive that an array decays to a pointer when passed to a function.

AFAIK There is no difference between the function I mentioned and this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void bubblesort(int *x, int ArrSize) //int array[] gets turned to int *array either way is fine
//since I copy/pasted I used your variable names but you can make them whatever if you wish
{
        for( int i = 0 ; i < ArrSize - 1 ; i++ ) // For I = 0 to (number of array elements - 2), increment by 1

        {

        for ( int j = i + 1 ; j < ArrSize ; j++ ) // For J = (I + 1) to (number of array elements - 1), increment by 1

            {

            if( x[i] > x[j] ) // If X[I] is greater than X[J] then

                {

                // swap X[I] and X[J]

                int Tmp = x[i];

                x[i] = x[j];

                x[j] = Tmp;

                }

            }

        }
}
since the compiler converts int x[] to int *x

As I was saying earlier:
Softrix wrote:
array[1], if I were to do that with pointers I would need to use *(array+1)
array[1] and *(array+1) are the same.

http://www.cplusplus.com/doc/tutorial/pointers/

In the chapter about arrays, brackets ([]) were explained as specifying the index of an element of the array. Well, in fact these brackets are a dereferencing operator known as offset operator. They dereference the variable they follow just as * does, but they also add the number between brackets to the address being dereferenced. For example:

1
2
a[5] = 0;       // a [offset of 5] = 0
*(a+5) = 0;     // pointed by (a+5) = 0   



These two expressions are equivalent and valid, not only if a is a pointer, but also if a is an array. Remember that if an array, its name can be used just like a pointer to its first element.
Last edited on

Use pointers in your function to reference the array elements.


That kinda swung it a little for me with the decision to use pointer notation rather than the usual array notation as you demonstrated in your post. If I've read into this wrong then I've probably made my example more complicated that it needed to be... who knows lol, perhaps O/P can clear that one up.

Take Care.

:)




Topic archived. No new replies allowed.