Thread: Bubble sort problem

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    15

    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;
    	
     /*------------------------------------------------------------------------------*/
     	
    /*Heading*/
    /*-------------------------------------------------------------------------------*
    
    
    //*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. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by wwwildbill View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My bubble sort only sorts once
    By Muller in forum C Programming
    Replies: 8
    Last Post: 03-27-2009, 04:36 PM
  2. help with debug (bubble sort algorithm)
    By lbraglia in forum C Programming
    Replies: 5
    Last Post: 10-19-2007, 05:24 PM
  3. Problem with Bubble Sort code
    By lisa1234 in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2004, 03:40 PM
  4. Sorting a string using bubble sort?!
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 05:44 PM
  5. optimizing bubble sort
    By Sargnagel in forum C Programming
    Replies: 14
    Last Post: 01-23-2003, 06:27 AM