### insertion sort printing out strange values

I created an insertion sort and it runs alright but instead of printing out the numbers I entered it prints out a strange string of numbers that make no sense.
my program will work like this when I'm done
a)you enter the size of data/array first
ex. 10 integers

b)then you enter a random sequence of numbers either manually or have the program generate the numbers.

this is my code for that and it really doesn't work like i want it yet
any suggestions on how I can do this better?
 ``123456`` ``````srand(time(0)); cout << "\nGenerating " << n << " random integers..." << endl; for (int e = 0; e < n; e++) { a[e] = rand() % 100; //range: [0, 99] ``````

c)after the numbers are entered then you are prompt to select a method of sorting
ex. insertion, quick, merge, etc.

d) then the program will return the sorted data
my program is returning numbers like
-336860191-33686019-33686019
i entered three simple numbers and got three long strings of gibberish like illustrated above. why am I getting this as a result?

here's my code
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113`` ``````//CMP425 //Theory of Algorithms //Dr. Alak //Jason Thomas //Multiple Sorts #include #include #include #include using namespace std; void insertionSort(int a[], int n, int e){ int i, j; for (i=1; i<=sizeof(a[e]); ++i){ int key = a[i]; for(j=i-1; j>=0 && a[j]> key; --j) a[j+1]=a[j]; a[j+1]=key; cout<>n; cout<>enterValues; if(enterValues == 'M'||'m') { cout<<"enter by keyboard: "<>a[e]; } cout<>sortSelection; if (sortSelection == 'I'||'i') { insertionSort(a, n, e); } if (sortSelection == 'S'||'s') { // selection_sort (a, n, e); } if (sortSelection == 'M'||'m') { // mergesort(); } } else if (enterValues == 'R'||'r') { cout << "\nGenerating " << n << " random integers..." << endl; for (int e = 0; e < n; e++) { a[e] = rand() % 100; //range: [0, 99] } cout<>sortSelection; if (sortSelection == 'I'||'i') { insertionSort(a, n, e); } if (sortSelection == 'S'||'s') { // selection_sort (a, n, e); } if (sortSelection == 'M'||'m') { // mergesort(); } } system ("PAUSE"); return 0; }``````

I'd appreciate any and all sorts of advice here
what ever I can get
Thank you
Last edited on
That sizeof(a[e]) thing does not get the size of the array. It gets the size in bytes of an element of the array, which is not what you want as the loop limit in your for loop.

Also, in your if statements, conditions like `sortSelection == 'M'||'m'` checks, as the second part of the condition, whether `'m'` is true. Since `'m'` is non-zero, it is always true and that code will always execute, which I assume is not what you want.
thanks Zhuge
is that the reason I'm getting the strange numbers?
it's driving me crazy that I cant figure this out.
I know my Algorithms are good its just that stuff in the main I gotta figure out.
and should I change` sortSelection == 'M'||'m'` to
`sortSelection == 'M'&&'m'` ?
I've added to my code with some more sorts and I changed that` sizeof` thing also. here's what I've added.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166`` ``````//CMP425 //Theory of Algorithms //Dr. Alak //Jason Thomas //Multiple Sorts #include #include #include #include using namespace std; typedef int data; const int eMax = 100; void insertionSort(int a[], int e) { for (int i=1; i<= e; i++) { int key; key = a[i]; for(int j=i-1; j>=0 && a[j]> key; j--) { a[j+1]=a[j]; a[j+1]=key; cout< pivot) { right--; continue; } int temp = *left; *left++ = *right; *right-- = temp; } quickSort(a, right - a+1); quickSort(left, a + e - left); } int main() { char sortSelection; char enterValues; int e=0; //rand (time(0)); cout<<"Please enter size of array: "; int n; cin>>n; cout<>enterValues; if(enterValues == 'M'||'m') { cout<<"enter by keyboard: "<>a[e]; } cout<>sortSelection; if (sortSelection == 'I'||'i') { a[e]; int e = sizeof a/sizeof a[0]; insertionSort(a, e); } if (sortSelection == 'S'||'s') { a[e]; int e = sizeof a/sizeof a[0]; selectionSort (a, e); } if (sortSelection == 'M'||'m') { // mergeSort(); } if (sortSelection == 'Q'||'q') { a[e]; int e = sizeof a/sizeof a[0]; quickSort(a, e); } } else if (enterValues == 'R'||'r') { cout << "\nGenerating " << n << " random integers..." << endl; for (int e = 0; e < n; e++) { a[e] = rand() % 100; //range: [0, 99] } cout<>sortSelection; if (sortSelection == 'I'||'i') { a[e]; insertionSort(a, e); } if (sortSelection == 'S'||'s') { a[e]; selectionSort (a, e); } if (sortSelection == 'M'||'m') { a[e]; // mergesort(a, e); } if (sortSelection == 'Q'||'q') { a[e]; quickSort(a, e); } } system ("PAUSE"); return 0; }``````

I still need to fix that random part
Last edited on
Topic archived. No new replies allowed.