my code

#include <cstdlib> #include <iostream> #include <iomanip> /* Program sorts an array of integers using a selection sort. The general algorithm repeatedly finds the smallest number in the array and places it at the front of the list. */ using namespace std; const int size = 20; int find_small_ptr (int start_ptr, int numbers []); void swap_values (int ptr1, int ptr2, int numbers []); void print (int numbers []); int main(int argc, char *argv[]) { // array of numbers int numbers [size] = {7, 9, 21, 16, 65, 8, 32, 1, 17, 41, 54, 128, 62, 44, 12, 1023, 89, 905, 32, -12}; int *start_ptr; // current starting spot for search int *small_ptr; // index of the smallest number in the array start_ptr = 0; // continue finding the smallest value and placing it // at the front of the list while (*start_ptr < size - 1) { *small_ptr = find_small_ptr (*start_ptr, numbers); swap_values (*small_ptr, *start_ptr, numbers); start_ptr++; } cout << "\n\nThe sorted array is:\n"; print (numbers); cout << "\n\n"; system("PAUSE"); return EXIT_SUCCESS; } // finds and returns the index of the smallest number remaining in // the array int find_small_ptr (int start_ptr, int numbers []) { int *small_ptr, // smallest index to be returned *ptr, // current index being viewed *siz; *siz = size; *small_ptr = start_ptr; // look at each element for (ptr = small_ptr + 1; ptr < siz; ptr++) // remember index of smaller value if (&numbers [*ptr] < &numbers [*small_ptr]) small_ptr = ptr; return *small_ptr; } // swap the values in the array at indexes index1 and index2 void swap_values (int ptr1, int ptr2, int numbers []) { int *swapper; *swapper = numbers [ptr1]; numbers [ptr1] = numbers [ptr2]; numbers [ptr2] = *swapper; } // prints the array in nice format, 10 numbers per line void print (int numbers []) { int *on_line, // number of values printed on the line *ptr; // index of current number being printed on_line = 0; // print each element in the array for (ptr = 0; ptr < numbers; ptr++) { cout << setw (5) << &numbers [*ptr]; on_line++; // if 10 numbers have been printed on the line // go to next line if (*on_line == 10) { cout << "\n"; *on_line = 0; } } }

castigero, on 09 November 2012 - 10:20 AM, said:

I'm making an array sort using pointers and don't understand why it's in an infinite loop, I'm sure I'm missing something simple. Any help would be greatly appreciated.

my code

my code

#include <cstdlib> #include <iostream> #include <iomanip> /* Program sorts an array of integers using a selection sort. The general algorithm repeatedly finds the smallest number in the array and places it at the front of the list. */ using namespace std; const int size = 20; int find_small_ptr (int start_ptr, int numbers []); void swap_values (int ptr1, int ptr2, int numbers []); void print (int numbers []); int main(int argc, char *argv[]) { // array of numbers int numbers [size] = {7, 9, 21, 16, 65, 8, 32, 1, 17, 41, 54, 128, 62, 44, 12, 1023, 89, 905, 32, -12}; int *start_ptr; // current starting spot for search int *small_ptr; // index of the smallest number in the array start_ptr = 0; // continue finding the smallest value and placing it // at the front of the list while (*start_ptr < size - 1) { *small_ptr = find_small_ptr (*start_ptr, numbers); swap_values (*small_ptr, *start_ptr, numbers); start_ptr++; } cout << "\n\nThe sorted array is:\n"; print (numbers); cout << "\n\n"; system("PAUSE"); return EXIT_SUCCESS; } // finds and returns the index of the smallest number remaining in // the array int find_small_ptr (int start_ptr, int numbers []) { int *small_ptr, // smallest index to be returned *ptr, // current index being viewed *siz; *siz = size; *small_ptr = start_ptr; // look at each element for (ptr = small_ptr + 1; ptr < siz; ptr++) // remember index of smaller value if (&numbers [*ptr] < &numbers [*small_ptr]) small_ptr = ptr; return *small_ptr; } // swap the values in the array at indexes index1 and index2 void swap_values (int ptr1, int ptr2, int numbers []) { int *swapper; *swapper = numbers [ptr1]; numbers [ptr1] = numbers [ptr2]; numbers [ptr2] = *swapper; } // prints the array in nice format, 10 numbers per line void print (int numbers []) { int *on_line, // number of values printed on the line *ptr; // index of current number being printed on_line = 0; // print each element in the array for (ptr = 0; ptr < numbers; ptr++) { cout << setw (5) << &numbers [*ptr]; on_line++; // if 10 numbers have been printed on the line // go to next line if (*on_line == 10) { cout << "\n"; *on_line = 0; } } }

ignore the "siz" variable haha