Code that generates 25 random numbers.
An [iarray] is set to each number.
Another array, [parray] is set to the address of the [iarray]
My only issue is when it prints out the new sorted digits, instead of printing out the actual number, it prints out the address.
I know I'm missing a few "*", just cant find where.
Any help would be appreciated.
EDIT: New code, I'm pretty sure its correct now, just feels weird.
Code:#include <stdlib.h> #include <stdio.h> #include <time.h> #define ARRAY_SIZE 25 void print_array(int *array) { int x; for(x = 0; x < ARRAY_SIZE; x++) { if(x != ARRAY_SIZE-1) fprintf(stdout, "%d, ", array[x]); else fprintf(stdout, "%d\n", array[x]); } } int main() { int iarray[ARRAY_SIZE]; int *parray[ARRAY_SIZE]; int x, y, p, holder; // Creating an array to point to the addresses. for(p=0; p < ARRAY_SIZE; p++) parray[p] = &iarray[p]; // Seed rand() srand((unsigned int)time(NULL)); for(x = 0; x < ARRAY_SIZE; x++) iarray[x] = (int)(rand() % 100); fprintf(stdout, "Before Sort\n---------------\n"); print_array(iarray); // Bubble sort method. for(x = 0; x < ARRAY_SIZE; x++) for(y = 0; y < ARRAY_SIZE-1; y++) if(*parray[y] > *parray[y+1]) { holder = *parray[y+1]; *parray[y+1] = *parray[y]; *parray[y] = holder; } fprintf(stdout, "\nAfter Sort\n---------------\n"); print_array(*parray); getchar();getchar(); }



LinkBack URL
About LinkBacks



