Function Template - Insertion sort

Hi, I am new here. I got this homework due tonight. I have been working on this code for a while. This works fine and compiles but it crashes at run time. I am thinking there might be some problems with my logic here. Please help me. Here is 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Function Template Program
*
* Description: arranges different user input variable types using insertion sort
*
*
*/

#include <iostream>

using namespace std;

template <class T>
void printArray(T arrray[], int n);
template <class T>
void insertionSort(T arrray[], int n);


//function template for printing array elements
template <class S>
void printArray(S a[], int n)
{
    for(int i = 1; i <= n; i++)
        cout << a[i] << " ";
    cout << endl;
}

//function template for sorting array elements
 template <class S>
 void insertionSort(S a[], int n)
{
    S temp = a[0];
    for(int i = 1; i <= n; i++)
    {
        temp = a[i];
        int j = 0;
        for(j = i; j > 0; j--)
            if(temp < a[j - 1])
               a[j] = a[j - 1];
            else break;
        a[j] =  temp;
    }
}


int main(void){

int n;

cout << "This program sorts different variable types" << endl;
cout << endl;

int selection;
cout << "What variable type do you want to sort?" << endl;
cout << "[1] Integer" << endl;
cout << "[2] Float" << endl;
cout << "[3] Character" << endl;
cout << "[4] Double" << endl;
cout << "[5] Exit" << endl;
cin >> selection;


    switch(selection){

        case 1:
            cout << "Please enter array size: ";
            cin >> n;

            cout << "Please enter array elements:" << endl;
            cout << endl;

            int intArray[n];

                for (int i=1; i <= n; i++){

                    cout << "* ";
                    cin >> intArray[i];
                    cout << endl;

                }
                insertionSort(intArray, n);
                cout << endl;
                printArray(intArray, n); break;

            case 2:
                cout << "Please enter array size: ";
                cin >> n;

                  cout << "Please enter array elements." << endl;
                  cout << endl;

                float floatArray[n];

                for (int i=1; i <= n; i++){

                    cout << "* ";
                    cin >> floatArray[i];
                    cout << endl;

                }
                insertionSort(floatArray, n);
                cout << endl;
                printArray(floatArray, n);
                    break;

            case 3:
                cout << "Please enter array size: ";
                cin >> n;


                cout << "Please enter array elements." << endl;
                cout << endl;

                char charArray[n];

                for (int i=1; i <= n; i++){

                    cout << "* ";
                    cin >> charArray[i];
                    cout << endl;

                }
                insertionSort(charArray, n);
                cout << endl;
                printArray(charArray, n);
                    break;

            case 4:
                  cout << "Please enter array size: ";
                  cin >> n;

                  cout << "Please enter array elements." << endl;
                  cout << endl;

                double doubleArray[n];

                for (int i=1; i <= n; i++){

                    cout << "* ";
                    cout << endl;
                    cin >> doubleArray[i];

                }
                insertionSort(doubleArray, n);
                cout << endl;
                printArray(doubleArray, n);
                    break;

        case 5:
                    break;

        default:
                    cout << "Please enter valid selection!" << endl;
                break;

        }
}

Last edited on
You say this compiles? It doesn't in VS2005 as is. I can make a few changes to make it compile, however. You have a few problems here:

1. "int n" at the top of main() should be a constant expression. This means in this case that it should have a const qualifier in front of it and should reference a concrete number (1000 or 100 for example...)

2. Since you are intending for the value of n to be user entered, this makes 1 somewhat moot, but also means that you cannot use automatic storage for your arrays. You should use pointers instead, i.e. "char *charArray; charArray = new char[ n ];" Then before the break you would do a "delete[] charArray". If you choose to do this then you will need to make each case statement a compound statement block. (i.e. put '{' immediately after the case X: and put a '}' immediately before the "break;".

3. You are looping from 1 to n inclusive... C / C++ arrays go from 0 to n exclusive ( n-1 ). This means you need to change your loop to read "for ( int i = 0; i < n; i++ )" This may not cause a crash (it may as well) but it is certainly incorrect. This goes for ALL of your loops. In addition, if you are counting down in a loop you should do the opposite logically: "for( j = i; j >= 0; j-- )"

Hope this helps...
Last edited on
Topic archived. No new replies allowed.