where is error in my codee?? please help

#include <iostream>
using namespace std;

void enternumber (int *array, int N);
void bubblesort (int *array, int N, int Order);
void selectionsort (int *array, int N, int Order);
void insertionsort (int *array, int N, int Order);
void quicksort (int *array, int left, int right, int N, int Order);
int split (int lower, int upper, int *array, int Order);
void displayarray (int *array, int N);//fungsi prototype

void main ()
{
int n;
int * array;
int order, sorting=0;
cout<<"select (1) for ascending, (2) for descending order";
cin>>order;
while (order !=1&&order !=2)
{
cout<<"input error"<<endl;
cout<<"selct (1) for ascending, (2) for descending order";
cin>>order;
}
cout<<"Enter list of size";
cin>>n;
array=new int[n];
enternumber (array,n);
cout<<"select sorting tekhnik"<<endl;
cout<<" 1) Bubble sort"<<endl;
cout<<"2) selection sort"<<endl;
cout<<"3) Insertion sort"<<endl;
cout<<"4) Quick sort"<<endl;
cin>>sorting;
while ((sorting!=1)&&(sorting!=2)&&(sorting!=3)&&(sorting!=4))
{
cout<<"Input error"<<endl;
cout<<"select sorting tekhnik:"<<endl;
cout<<" 1) Bubble sort"<<endl;
cout<<"2) selection sort"<<endl;
cout<<"3) Insertion sort"<<endl;
cout<<"4) Quick sort"<<endl;
cin>>sorting;
}
cout<<"your numbers are :";
displayarray (array,n);
cout<<"your choice of sorting is :"<<sorting<<endl;
if (sorting==1)
bubblesort(array,n,order);
else if (sorting==2)
selectionsort(array,n,order);
else if (sorting==3)
insertionsort(array,n,order);
else
{
quicksort(array,0,n-1,n,order);
cout<<"sorted list for quick sort :";
displayarray(array,n);
}
void enternumber (int *array, int N)
{
int i;
for (i=0;i<N;i++)
{
cout<<"enter number : ";
cin>>array[i];
}
}
void bubblesort(int *array,int N, int Order)
{
int i,j,temp=0;
for(i-0;i<N-1;i++)
for (j=1;j<N;j++)
{
if ((array[j]<array[j-1]&&(Order==1))
{
temp=array[j];
array [j]=array[j-1];
array [j-1]=temp;
}
else if((array[j]array[j-1]&&(Order==2)
{
temp=array[j];
array[j]=array[j-1];
array[j-1]=temp;
}
}
cout<<"sorted list for bubble sort:";
displayarray(array,N);
}
void selectionsort(int *array, int N, int Order)
{
int i,j,min,temp=0;
for(i-0;i<N-1;i++)
{
min=i;
for(j=i+1;j<N;j++)
{
if((array[j]<array[j-1]&&(Order==1))
min j;
else if((array[j]>array[j-1]&& (Order==2))
min=j;
}
temp=array[min];
array[min]=array[i];
array[i]=temp;
}
cout<<"sorted for selection sort : ";
displayarray (array,N);
}
void insertionsort(int *array,int N, int Order)
{
int i,j,temp=0;
for(i=1;i<N;i++)
{
temp=array[i];
j=i-1;
if(Order==1)
{
while (array[j]>temp)
{
array[j+1]=array[i];
j--;
}
}
else if (Order==2)
{
while((array[j]<temp)&&(j>=0))
{
array [j+1}=array[j];
j--;
}
}
array[j+1]=temp;
}
cout<<"sorted list for insertion sort:";
displayarray(array,N);
}
void quicksort(int *array, int left, int right, int N, int Order)
{
if (right>left)
{
int pivot=split(left, right,array,Order);
quicksort(array,left,pivot-1,N,Order);
quicksort(array,pivot+1;right,N,Order);
}
}
int split(int left, int right,int *array, int Order)
{
int pivot=array[left];
if(Order==1)
{
while(true)
{
while (array[left]<pivot)
left++;
while(array[right]>pivot)
right--;
if(left<right)
swap(array[left],array[right]};
else
return right;
}

}
else if (Order==2)
{
while(true)
{
while (array[left]<pivot)
left++;
while(array[right]>pivot)
right--;
if(left<right)
swap(array[left],array[right]};
else
return right;
}
}
}
void displayarray(int *array, int N)
{
int i;
for (i-0;i<N;i++)
cout<<array[i]<<" ";
cout<<"\n";
}
return 0;
}














































Firstly please post your code using code tags - select the code then press the <> button on the right.

If you have errors - then post them. Either the compiler output (not your description of it), or whatever messages that are issued for a runtime error.


The first problem you have is that the size of arrays must be known at compile time, not run time. Either use an array with a fixed size, or use a vector.

I haven't looked any further.

HTH
closed account (18hRX9L8)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//Edited by Usandfriends//

#include <iostream>
using namespace std;

void enternumber (int *array, int N);
void bubblesort (int *array, int N, int Order);
void selectionsort (int *array, int N, int Order);
void insertionsort (int *array, int N, int Order);
void quicksort (int *array, int left, int right, int N, int Order);
int split (int lower, int upper, int *array, int Order);
void displayarray (int *array, int N);//fungsi prototype

void displayarray(int *array, int N)
{
     int i;
     for (i-0;i<N;i++)
     {
          cout<<array[i]<<" ";
          cout<<"\n";
      }
}

void quicksort(int *array, int left, int right, int N, int Order)
{
     if (right>left)
     {
          int pivot=split(left, right,array,Order);
          quicksort(array,left,pivot-1,N,Order);
          quicksort(array,pivot+1,right,N,Order);
     }
}

int split(int left, int right,int *array, int Order)
{
      int pivot=array[left];
      if(Order==1)
     {
     while(true)
     {
          while (array[left]<pivot)
          left++;
               while(array[right]>pivot)
              {
                    right--;
                    if(left<right)
                    {
                         swap(array[left],array[right]);
                    }
                   else
                   {
                         return right;
                    }
               }
     else if (Order==2)
     {
          while(true)
     {
     while (array[left]<pivot)
     {
          left++;
     }
     while(array[right]>pivot)
     {
           right--;
           if(left<right)
           {
               swap(array[left],array[right]);
           }
           else
           {
                return right;
           }
     }
}

void insertionsort(int *array,int N, int Order)
{
     int i,j,temp=0;
     for(i=1;i<N;i++)
     {
          temp=array[i];
          j=i-1;
          if(Order==1)
          {
               while (array[j]>temp)
               {
                    array[j+1]=array[i];
                    j--;
               }
          }
          else if (Order==2)
          {
               while((array[j]<temp)&&(j>=0))
               {
                    array [j+1]=array[j];
                    j--;
               }
          }
          array[j+1]=temp;
     }
     cout<<"sorted list for insertion sort:";
     displayarray(array,N);
}

void selectionsort(int *array, int N, int Order)
{
     int i,j,min,temp=0;
     for(i-0;i<N-1;i++)
     {
          min=i;
          for(j=i+1;j<N;j++)
          {
               if(array[j]<array[j-1]&&(Order==1))
               {
                    min=j;
               }
               else if(array[j]>array[j-1]&& (Order==2))
               {
                    min=j;
               }
          }
          temp=array[min];
          array[min]=array[i];
          array[i]=temp;
     }
     cout<<"sorted for selection sort : ";
     displayarray (array,N);
}

void enternumber (int *array, int N)
{
     int i;
     for (i=0;i<N;i++)
     {
          cout<<"enter number : ";
          cin>>array[i];
     }
}

void bubblesort(int *array,int N, int Order)
{
     int i,j,temp=0;
     for(i-0;i<N-1;i++)
     {
          for (j=1;j<N;j++)
          {
               if (array[j]<array[j-1]&&(Order==1))
               {
                    temp=array[j];
                    array [j]=array[j-1];
                    array [j-1]=temp;
               }
               else if(array[j]<array[j-1]&&(Order==2))
               {
                    temp=array[j];
                    array[j]=array[j-1];
                    array[j-1]=temp;
               }
          }
          cout<<"sorted list for bubble sort:";
          displayarray(array,N);
     }
}

main()
{
     int n;
     int * array;
     int order, sorting=0;
     cout<<"select (1) for ascending, (2) for descending order";
     cin>>order;
     while (order !=1&&order !=2)
     {
          cout<<"input error"<<endl;
          cout<<"selct (1) for ascending, (2) for descending order";
          cin>>order;
     }
     cout<<"Enter list of size";
     cin>>n;
     array=new int[n];
     enternumber (array,n);
     cout<<"select sorting tekhnik"<<endl;
     cout<<" 1) Bubble sort"<<endl;
     cout<<"2) selection sort"<<endl;
     cout<<"3) Insertion sort"<<endl;
     cout<<"4) Quick sort"<<endl;
     cin>>sorting;
     while ((sorting!=1)&&(sorting!=2)&&(sorting!=3)&&(sorting!=4))
     {
          cout<<"Input error"<<endl;
          cout<<"select sorting tekhnik:"<<endl;
          cout<<" 1) Bubble sort"<<endl;
          cout<<"2) selection sort"<<endl;
          cout<<"3) Insertion sort"<<endl;
          cout<<"4) Quick sort"<<endl;
          cin>>sorting;
     }
     cout<<"your numbers are :";
     displayarray (array,n);
     cout<<"your choice of sorting is :"<<sorting<<endl;
     if (sorting==1)
     {
          bubblesort(array,n,order);
     }
     else if (sorting==2)
     {
          selectionsort(array,n,order);
     }
     else if (sorting==3)
     {
          insertionsort(array,n,order);
     }
     else
     {
          quicksort(array,0,n-1,n,order);
          cout<<"sorted list for quick sort :";
          displayarray(array,n);
     }
     for(int $=0;$<16;$++)
     {
          cin.ignore();
     }
}


I fixed it so it can compile, but the sort doesn't work. Please indent and make it user friendly. Good luck.

EDIT: Please do not post two of the same threads.
http://www.cplusplus.com/forum/beginner/88745/
Last edited on
Topic archived. No new replies allowed.