# Thread: Bubble sort problem

1. ## Bubble sort problem

I'm a newbie to C and I'm trying to sort this program by account_num but the bubble sort just wont seem to work. Its say "int to int lacks a cast" What does this mean and what can i do to fix it.

Code:
```#include <stdio.h>

main()
{
/*Variables*/
/*-------------------------------------------------------------------------------*/
struct info{char name[30]; int account_num[30]; float balance[30];
};
struct info people[25];
int x,a,temp;

/*------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------*

//*Loop to collect info*/

for(x = 1; x <= 2; ++x)
{	 printf("Enter Name: ");
scanf("%s",people[x].name);

printf("Enter Account Number: ");
scanf("%i",people[x].account_num);

printf("Enter Balance: ");
scanf("%f",&people[x].balance);
}

for (a=1; a<x-1;++a)
{		for (x=1; x<10;++x)
{
if (people[x].account_num>people[x+1].account_num)
{
temp = people[x+1].account_num;
people[x+1].account_num = people[x].account_num;
people[x].account_num = temp;
}/*end if*/
}/*end nested for*/
}/*end for loop*/

}```

Thanks!

2. Code:
```struct info temp;

for(i = 0; i < 25 - 1; i++)  {
for(j = i + 1; j < 25; j++)  {
if(people[i].account_number > people[j].account_number)  {
//copy people[i] struct members to temp
strcpy(temp.name, people[i].name);
//I can't believe an account would have 30 account numbers,
// so I'm transferring      one number.
temp.account_number = people[i].account_number;
temp.balance = people[i].balance;

//copy people[j] to people[i]
strcpy(people[i].name, people[j].name);
people[i].account_number = people[j].account_number;
people[i].balance = people[j].balance;

//copy temp members into people[j] members
strcpy(people[j]name, temp.name);
people(j].account_number = temp.account_number;
people[j].balance = temp.balance;
}
}
}```
And this can all be done easier with structs, using pointers to structs, but I didn't want to confuse you.

This code is not tested.

3. Originally Posted by wwwildbill
I'm a newbie to C and I'm trying to sort this program by account_num but the bubble sort just wont seem to work. Its say "int to int lacks a cast" What does this mean and what can i do to fix it.
And which line is it referring to?

Note that you don't seem to be aware that arrays start at zero, NOT one.

You've also stuffed yourself up by using 'x' in both the outer loop and the inner loop. The first time through the 'a' loop, the value of 'x' will be 3, and every subsequent time it will be 10.
Please fix your indentation. There are guides for that on this site.

Popular pages Recent additions