Two of your problems are:
for (i =0, i < SIZE_OF_ARRAY; i++;){
array[i] = (int)rand();
for (i =0, i < SIZE_OF_ARRAY; i++;){
In both the loops, the comma should be a semicolon and the final semicolon should be missing. As it is, you have an assignment (i = 0
followed by an unused comparison i < SIZE_OF_ARRAY
) and the loop condition is i++ != 0
(which takes a long time to become false; you've probably crashed long before it does become false), and there is no reinitialize step. You also don't really want two nested loops both indexing on i
— use i
and j
, instead. Thus, if you need nested loops, you write:
for (i = 0; i < SIZE_OF_ARRAY; i++)
{
array[i] = (int)rand();
for (i = 0; j < SIZE_OF_ARRAY; j++)
{
However, it looks to me like there's no need for nested loops at all:
for (i = 0; i < SIZE_OF_ARRAY; i++)
{
array[i] = rand();
pointer[i] = &array[i]; // More on this in a moment
}
The compiler is also diagnosing a problem with the assignment:
pointer[i] = &array[i];
You're assigning a pointer (&array[i]
) to an int
(because int *pointer
means that pointer
is a pointer to an int
so pointer[i]
, equivalent to *(pointer + i)
, is an int
). Judging from the code, it looks like in main()
you should be declaring:
int *pointer[SIZE_OF_ARRAY];
and the function interface should be:
void ArrayInitialize(int *array, int **pointer)
but all sorts of other parts of the code are inconsistent with that. I'm sure it can be made consistent; the question is which way to make it consistent. There isn't a reason to use pointer
at all (that I can see) unless it is int **pointer
in the function.
You have another variant of the loop syntax problems at:
for (i =0, int < SIZE_OF_ARRAY; i++;){
That too should be:
for (i = 0; i < SIZE_OF_ARRAY; i++){
In the other functions, you are using the names ArrayInt
and ArrayPtr
as if they are typedef
names, but they're actually (unused) global variables. If you make them into typedef
s after all, use them consistently. If you aren't going to make them into typedef
s, then get rid of them and fix the function definitions.
You have unmatched parentheses in the sort functions where you swap:
temp = *array+(j-1));
*array+(j-1)) = array+(j));
*array+(j) = temp;
The notation you're using is weird; why not just use array indexes?
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
Working code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SIZE_OF_ARRAY 5
typedef int *IntegerPtr;
typedef int ArrayInt[SIZE_OF_ARRAY];
typedef int *ArrayPtr[SIZE_OF_ARRAY];
void PointerBubbleSort(ArrayPtr pointer);
void ArrayBubbleSort(ArrayInt array);
void ArrayPointerPrint(ArrayPtr pointer);
void ArrayPrint(ArrayInt array);
void ArrayInitialize(ArrayInt array, ArrayPtr pointer);
void ArrayInitialize(ArrayInt array, ArrayPtr pointer)
{
int i;
srand(getpid());
for (i = 0; i < SIZE_OF_ARRAY; i++)
{
array[i] = (int)rand();
pointer[i] = &array[i];
}
}
void ArrayPrint(ArrayInt array)
{
int i;
for (i = 0; i < SIZE_OF_ARRAY; i++)
{
printf("%d : %10d \n", i, array[i]);
}
printf("\n");
}
void ArrayPointerPrint(ArrayPtr pointer)
{
int i;
for (i = 0; i < SIZE_OF_ARRAY; i++)
{
printf("%d : %10d \n", i, *pointer[i]);
}
printf("\n");
}
void ArrayBubbleSort(ArrayInt array)
{
int i;
int j;
int temp;
for (i = (SIZE_OF_ARRAY - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (array[j-1] > array[j])
{
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
}
}
}
}
void PointerBubbleSort(ArrayPtr pointer)
{
int i;
int j;
int *temp;
for (i = (SIZE_OF_ARRAY - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (*pointer[j-1] > *pointer[j])
{
temp = pointer[j-1];
pointer[j-1] = pointer[j];
pointer[j] = temp;
}
}
}
}
int main(void)
{
ArrayInt array;
ArrayPtr pointer;
ArrayInitialize(array, pointer);
ArrayPrint(array);
PointerBubbleSort(pointer);
ArrayPointerPrint(pointer);
ArrayBubbleSort(array);
ArrayPrint(array);
return(EXIT_SUCCESS);
}
Sample output
0 : 881325466
1 : 1242393703
2 : 927466540
3 : 1493827854
4 : 533425101
0 : 533425101
1 : 881325466
2 : 927466540
3 : 1242393703
4 : 1493827854
0 : 533425101
1 : 881325466
2 : 927466540
3 : 1242393703
4 : 1493827854
Revised code
Minor tweaks to the code to adhere to the letter of the assignment. In particular, it uses the otherwise unused type definition for 'pointer to integer' to build the 'array of five pointers to integer'. It tries to get the output format to match the question's output precisely. Try to avoid trailing spaces on output lines (and source code lines).
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
enum { SIZE_OF_ARRAY = 5 };
typedef int *IntPtr;
typedef int ArrayInt[SIZE_OF_ARRAY];
typedef IntPtr ArrayPtr[SIZE_OF_ARRAY];
void PointerBubbleSort(ArrayPtr pointer);
void ArrayBubbleSort(ArrayInt array);
void ArrayPointerPrint(ArrayPtr pointer);
void ArrayPrint(ArrayInt array);
void ArrayInitialize(ArrayInt array, ArrayPtr pointer);
void ArrayInitialize(ArrayInt array, ArrayPtr pointer)
{
srand(getpid());
for (int i = 0; i < SIZE_OF_ARRAY; i++)
{
array[i] = rand();
pointer[i] = &array[i];
}
}
void ArrayPrint(ArrayInt array)
{
for (int i = 0; i < SIZE_OF_ARRAY; i++)
printf("%2d : %10d\n", i, array[i]);
}
void ArrayPointerPrint(ArrayPtr pointer)
{
for (int i = 0; i < SIZE_OF_ARRAY; i++)
printf("%2d : %10d\n", i, *pointer[i]);
}
void ArrayBubbleSort(ArrayInt array)
{
for (int i = (SIZE_OF_ARRAY - 1); i >= 0; i--)
{
for (int j = 1; j <= i; j++)
{
if (*(array + j-1) > *(array + j))
{
int temp = *(array + j-1);
*(array + j-1) = *(array + j);
*(array + j) = temp;
}
}
}
}
void PointerBubbleSort(ArrayPtr pointer)
{
for (int i = (SIZE_OF_ARRAY - 1); i >= 0; i--)
{
for (int j = 1; j <= i; j++)
{
if (*pointer[j-1] > *pointer[j])
{
int *temp = pointer[j-1];
pointer[j-1] = pointer[j];
pointer[j] = temp;
}
}
}
}
int main(void)
{
ArrayInt array;
ArrayPtr pointer;
ArrayInitialize(array, pointer);
puts("---- Initialized array of integers ----");
ArrayPrint(array);
PointerBubbleSort(pointer);
puts("---- Sorted array of pointers ----");
ArrayPointerPrint(pointer);
ArrayBubbleSort(array);
puts("---- Sorted array of integers ----");
ArrayPrint(array);
puts("---- Array of pointers ----");
ArrayPointerPrint(pointer);
return(EXIT_SUCCESS);
}
Revised output
---- Initialized array of integers ----
0 : 974520281
1 : 2052070745
2 : 565640395
3 : 1955497143
4 : 950748713
---- Sorted array of pointers ----
0 : 565640395
1 : 950748713
2 : 974520281
3 : 1955497143
4 : 2052070745
---- Sorted array of integers ----
0 : 565640395
1 : 950748713
2 : 974520281
3 : 1955497143
4 : 2052070745
---- Array of pointers ----
0 : 974520281
1 : 2052070745
2 : 565640395
3 : 1955497143
4 : 950748713