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?
1 2 3 4 5 6
|
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
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
//CMP425
//Theory of Algorithms
//Dr. Alak
//Jason Thomas
//Multiple Sorts
#include <iostream>
#include <algorithm>
#include <iterator>
#include <functional>
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<<a[j];
}
}
int main()
{
char sortSelection;
char enterValues;
int e=0;
//rand (time(0));
cout<<"Please enter size of array: ";
int n;
cin>>n;
cout<<endl;
int *a = new int[n];
cout<<"Enter a random series of integers: "<<endl;
cout<<"Press (M) to enter them manually"<<endl;
cout<<"Press (R) to automatically generate the values"<<endl;
cin>>enterValues;
if(enterValues == 'M'||'m')
{
cout<<"enter by keyboard: "<<endl;
for (int e=0; e<n; e++)
{
cin>>a[e];
}
cout<<endl;
cout<<"Choose which method of sorting you would like to do "<<endl;
cout<<"For Merge Sort select (M or m) For Insertion Sort select (I or i) "<<endl;
cout<<"For Selection Sort select (S or s) For Quick Sort select (Q or q) "<<endl;
cin>>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<<endl;
cout<<"Choose which method of sorting you would like to do "<<endl;
cout<<"For Merge Sort select (M or m) For Insertion Sort select (I or i) "<<endl;
cout<<"For Selection Sort select (S or s) For Quick Sort select (Q or q) "<<endl;
cin>>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