How to Bubble Sort Array using Strncmp?

a program that can accept 20 names.Displaying the output with ascending order. Using bubble sort technique combine with strcmp() function to create the output.

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
#include<stdio.h>
#include<string.h>
void main ()
{
	char name[20];
	int i;
	int pass;
	int hold;


	printf("\n");

	for(i=0;i<20;i++)
	{
	 printf("Insert Name,%d:",i+1);
	 gets(name);
	}

for ( pass = 1; pass < 20; pass++ )
			 {


			for ( i = 0; i < 20 - 1; i++ )
				{


				if (strncmp(name[i],name[i+1],20) > 0)
				{
					hold = name[i];
					name[i] = name[i+1];
					name[i+1] = hold;
				}

			}

		}
		printf( "\nData items in ascending order\n" );

		for ( i = 0; i < 20; i++ )
		{
			printf( "%s", name[i] );
		}

		printf( "\n" );




}


this is the coding that I've created
I cant compile because of the errors
I am a beginner whereas i can't find the error
By ascending, I'm assuming you mean alphabetical. The problem is that you have only one such variable to store names. So your program can accept and store only one name, and then re-arrange the letters of that name in order.
You need a 2D array, or an array of arrays. Try having this:

1
2
3
4
5
6
7
char names[20][20];

for (int i=0; i<20; o++)
{
   printf("Enter name: ")
   gets(names[i]);
}


Also, I've been told gets is not good to use. Use fgets instead, it has the same function.

Your main problem is in lines 29, 30, 31. You've declared hold as an int (integer, which will store a whole number), whereas name is a character array. So you're trying to store a char into an int. This is probably where your compiler shows the error...

These are all the mistakes I could find. Try learning to understand the errors your compiler shows.. They are usually self-explanatory, and you'll need to be able to understand them for further use...

Hope this helps... :)
this is what i did , now there's 1 warning and 3 errors

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
#include<stdio.h>
#include<string.h>
void main ()
{
	char name[20][20];
	int i;
	int pass;
	char hold;


	printf("\n");

	for(i=0;i<20;i++)
	{
	 printf("Enter Name,%d:",i+1);
	 gets(name[i]);
	}

for ( pass = 1; pass < 20; pass++ )
			 {


			for ( i = 0; i < 20 - 1; i++ )
				{


				if (strncmp(name[i],name[i+1],20) > 0)
				{
					hold = name[i];
					name[i] = name[i+1];
					name[i+1] = hold;
				}

			}

		}
		printf( "\nData items in ascending order\n" );

		for ( i = 0; i < 20; i++ )
		{
			printf( "%s", name[i] );
		}

		printf( "\n" );




}
Again, your errors are in lines 29, 30 and 31... Ohk, I guess you're not understanding the concept of same data/variable type..

name is an array of array of characters, or an array of strings.
name[i] holds an array of characters (a string)
name[i][j] holds a single character, to be precise, the (j+1)th letter of the (i+1)th name. For example:
name[3][4] would show the 5th letter of the 4th name in the array.

Now hold is a single character. You cannot copy a string (an array of chars) into a single char, can you! You need another string.. So declare hold as a string, as so:
 
char hold[20];


Furthermore, as you're dealing with the string library, you should know about the string copy function, strcpy. It works as so: strcpy(s1, s2) copies string s2 into s1. So lines 29, 30, and 31 can be replaced with:
1
2
3
strcpy(hold, name[i]);
strcpy(name[i], name[i+1]);
strcpy(name[i+1], hold);


Does that make it clearer now?

thanks a lot
its much clearer.
im truly sorry
i really am a beginner
its like an empty cup
thanks for the assistance
Eh, mistakes are how you learn! Glad I could help.. :)
Topic archived. No new replies allowed.