sorting (insertion/selection)

There's some error appeared when running the 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
  #include<stdio.h>
#include<conio.h>
#include<string.h>

void selectionSort();
void insertionSort();
void inseprint();

void main()
{	char test[80];
	char choice;
	int n;

	printf("String to sort>> ");
	gets(test);
	n=strlen(test);
	printf("Elements to be entered>> %d",n);

	puts("\nSort by... ");
	puts("\t[S]election");
	puts("\t[I]nsertion");

	printf("Choice>> ");
	choice=getchar();
	printf("%s(input string)\n", test);

	switch(choice)
	{	case 'S':selectionSort();
					puts(test);
					inseprint();
					break;
		case 'I':inseprint();
					insertionSort();
					puts(test);
					inseprint();
					break;
	}
}

void inseprint(int test[],int n)
{	int j;
	for(j=0; j<=n-1; j++)
	{	if(j%20==0)
		{	printf("\n");
			printf("%2d\t", test[j]);
		}
	}
}

void insertionSort(int test[],n)
{  int c,d,t;
	for (c=1; c<n-1; c++)
	{	d=c;
		while(d>0&& test[d]<test[d-1])
		{	t=array[d];
			test[d];
			test[d-1]=t;

			d--;
		}
	}
}

void selectionSort(test,n)
{	int c,d,t;
	for(c=n+1; c<n; c++)
	{	for(d=c+1;d<n;d++)
		{	if(test[c]>test[d])
			{	t=test[c];
				test[c]=test[d];
				test[d]=t;
			}
		}
	}
}
Error?

Aceix.
For starters, inseprint, insertionSort and selectionSort methods.. your declarations at the top don't match your method implementations at the bottom.

e.g. this:
1
2
3
4
5
6
void selectionSort(test,n)
{

....

}

is illegal, you need to tell the compiler what test and n actually are (and make the corresponding function declaration match as well.

Your compiler should tell you stuff like this though.
Last edited on
There's some error appeared when running the code

Please be specific and include actual errors messages.
The code you posted won't run because it does not compile. Part of programming is understanding compiler error messages and fixing the problem.

Lines 5-7: You function prototypes do not match your function definitions.

Line 50: No type for n.

Line 55: Where is array defined? Do you mean test?

Line 56: This line does nothing.

Line 64: test is not declared correctly. No type specified for n.

Last edited on
Topic archived. No new replies allowed.