Thread: bubble sort not working... wats d prob?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    39

    bubble sort not working... wats d prob?

    here's my code...
    the colored portion is the bubble sort...
    i just commented the bubble sort...

    if i remove the bubble sort... it works fine, except for its order...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct student_profile{
    	long long int stud_num;
    	char name[50];
    	char bday[50];
    	char course[50];
    	} STUDENT;
    
    void login(char username[], char password[]);
    char admin(void);
    	void add_student(STUDENT array[20]);
    	void display_students(STUDENT array[20]);
    	void modify_student(STUDENT array[20]);
    	void search_student(STUDENT array[20]);
    	void delete_student(STUDENT array[20]);
    	void delete_all(STUDENT array[20]);
    
    //*******************************************************************
    
    int main(void)
    {
    	STUDENT array[20];
    	char username[50];
    	char password[50];
    	char option;
    	int i;
    
    	for(i = 0; i < 20; i++)
    	{
    		array[i].stud_num = 0;
    	}
    
    	clrscr();
    
    	while(strcmp(username,"quit") != 0 || strcmp(password,"quit") != 0)
    	{
    
    	login(username, password);
    
    
    	if(strcmp(username, "ADMIN") == 0 && strcmp(password, "admin") == 0)
    	{
    		option = '0';
    
    		while(option != 'x')
    		{
    			option = admin();
    			switch(option)
    			{
    				case '1' :
    				{
    					gotoxy(14,48); printf("Redirecting to [Add Student] Function --->...");
    					getche();
    					add_student(array);
    					break;
    				}				//end of case '1'
    
    				case '2' :
    				{
    					gotoxy(14,48); printf("Redirecting to [Modify Student Account] Function --->...");
    					getche();
    					modify_student(array);
    					break;
    				}				//end of case '2'
    
    				case '3' :
    				{
    					gotoxy(14,48); printf("Redirecting to [Delete Student Account] Function --->...");
    					getche();
    					delete_student(array);
    					break;
    				}				//end of case '3'
    
    				case '4' :
    				{
    					gotoxy(14,48); printf("Redirecting to [Search Student Account] Function --->...");
    					getche();
    					search_student(array);
    					break;
    				}				//end of case '4'
    
    				case '5' :
    				{
    					gotoxy(14,48); printf("Redirecting to [Display all Accounts] Function --->...");
    					getche();
    					display_students(array);
    					break;
    				}				//end of case '5'
    
    				case '6' :
    				{
    					gotoxy(14,48); printf("Redirecting to [Delete all Accounts] Function --->...");
    					getche();
    					delete_all(array);
    					break;
    				}				//end of case '6'
    
    
    				case '7' :
    				{
    					gotoxy(14,50); printf("Logging out...");
    					getche();
    					option = 'x';
    					break;
    				}				//end of case '7'
    
    				case 'x' :
    				{	gotoxy(14,48); printf("Terminating Program...");
    					getche();
    					return 0;
    				}				//end of case '8'
    
    				default :
    				{	printf("\n\nInvalid option");
    				}  				//end of default
    			}					//end of switch
    		}						//end of while(option != '6')
    	}							//end of while(strcmp...)
    
    	/*else if(ang account kay student)
    	{
    	}*/
    
    	else
    	{
    		if(strcmp(username,"quit") != 0 && strcmp(password,"quit") != 0)
    		{
    			gotoxy(19,30); printf("Invalid username or password.");
    		}
    
    	}
    	}							//end of super big loop! XD haha
    
    	gotoxy(19,31); printf("Terminating Program...");
    	getche();
    	return 0;
    }
    
    //*******************************************************************
    
    void login(char username[], char password[])
    {
    	clrscr();
    
    	gotoxy(19,5);  printf("********* ********    ***     *** *********");
    	gotoxy(19,6);  printf("**    ***  **    ***   ***   ***  **    ***");
    	gotoxy(19,7);  printf("**     **  **     ***  ***   ***  **     **");
    	gotoxy(19,8);  printf("***        **    ***   **** ****  ***      ");
    	gotoxy(19,9);  printf("********   *******     ** *** **  ******** ");
    	gotoxy(19,10); printf(" ********  **    ***   ** *** **   ********");
    	gotoxy(19,11); printf("      ***  **     **   **  *  **        ***");
    	gotoxy(19,12); printf("**     **  **     **   **     **  **     **");
    	gotoxy(19,13); printf("***    **  **     **   **     **  ***    **");
    	gotoxy(19,14); printf("********* ***     *** ****   **** *********");
    	gotoxy(45,40); printf("Just type 'quit' as username");
    	gotoxy(45,41); printf("and 'quit' also as password");
    	gotoxy(45,42); printf("to terminate program.");
    	gotoxy(45,45); printf("Student Record Management System");
    	gotoxy(45,47); printf("By: Reynaldo P. Libutan Jr.");
    	gotoxy(19,22); printf("Enter username:");
    	gotoxy(35,22); gets(username);
    	gotoxy(19,24); printf("Enter password:");
    	gotoxy(35,24); gets(password);
    }
    
    //*******************************************************************
    
    char admin(void)
    {
    	char option;
    
    	clrscr();
    
    	gotoxy(14,2); printf("===================================================");
    	gotoxy(14,4); printf("               Administrator's Menu");
    	gotoxy(14,6); printf("===================================================");
    
    	gotoxy(14,10); printf("[1]     ---     Add a Student");
    	gotoxy(14,12); printf("[2]     ---     Modify a Student Account.");
    	gotoxy(14,14); printf("[3]     ---     Delete a Student Account.");
    	gotoxy(14,16); printf("[4]     ---     Search a Student Account.");
    	gotoxy(14,18); printf("[5]     ---     Display all Student Account Entries.");
    	gotoxy(14,20); printf("[6]     ---     Delete all Student Account Entries.");
    	gotoxy(14,22); printf("[7]     ---     Logout");
    	gotoxy(14,24); printf("[x]     ---     Terminate Program.");
    
    	gotoxy(14,28); printf("===================================================");
    
    	gotoxy(14,32); printf("Please choose the task you want to perform.");
    	gotoxy(14,34); scanf("%c", &option);
    
    	return option;
    }
    
    //*******************************************************************
    
    void add_student(STUDENT array[20])
    {
    	int i = 0;
    	int empty;
    	char opt = 'y';
    
    	while(opt == 'y' && i < 19){
    	for(i = 0; i < 20; i++)
    	{
    		if(array[i].stud_num == 0)
    		{
    			empty = i;
    			break;
    		}
    	}
    
    	clrscr();
    
    	if(i >= 19)
    	{
    		gotoxy(14,2);  printf("===================================================");
    		gotoxy(14,4);  printf("            Enter account information");
    		gotoxy(14,6);  printf("===================================================");
    		gotoxy(14,22); printf("===================================================");
    		gotoxy(14,32); printf("There are currently no free slots.");
    		gotoxy(14,26); printf("===================================================");
    
    	}
    
    	else
    	{
    		gotoxy(14,2);  printf("===================================================");
    		gotoxy(14,4);  printf("            Enter account information");
    		gotoxy(14,6);  printf("===================================================");
    		gotoxy(14,22); printf("===================================================");
    		gotoxy(14,26); printf("===================================================");
    
    		gotoxy(16,9);  printf("Student Number  [Username]:");
    		gotoxy(16,13); printf("Name:");
    		gotoxy(16,15); printf("Birthdate:");
    		gotoxy(16,17); printf("Course:");
    
    		gotoxy(32,11); scanf("%lld", &array[empty].stud_num);
    		gotoxy(32,13); gets(array[empty].name);					//try unya ang fgets(); if sakto na...
    		gotoxy(32,13); gets(array[empty].name);
    		gotoxy(32,15); gets(array[empty].bday);
    		gotoxy(32,17); gets(array[empty].course);
    
    		gotoxy(14,24); printf("       --- Account successfully created ---");
    
    		if(i >= 19)
    		{
    			clrscr();
    			gotoxy(14,2);  printf("===================================================");
    			gotoxy(14,4);  printf("            Enter account information");
    			gotoxy(14,6);  printf("===================================================");
    			gotoxy(14,22); printf("===================================================");
    			gotoxy(14,24); printf("There are currently no free slots.");
    			gotoxy(14,26); printf("===================================================");
    
    		}
    
    		else
    		{
    			gotoxy(14,32); printf("Do you want to enter another account?");
    			gotoxy(14,33); printf("[y for yes, else no]");
    			gotoxy(14,35); opt = getche();
    		}
    	}
    	} // end of while loop
    
    	gotoxy(14,48); printf("<--- Redirecting to the Administrator's page...");
    	getche();
    }
    
    //*******************************************************************
    
    void display_students(STUDENT array[20])
    {
    	int i, j;
    	long long int temp;
    	/*char temp1[50];
    	char temp2[50];
    	char temp3[50];*/
    
    	clrscr();
    
    	gotoxy(6,2);  printf("=====================================================================");
    	gotoxy(6,4);  printf("STUDENT NUMBER");
    	gotoxy(25,4); printf("NAME");
    	gotoxy(45,4); printf("BIRTHDATE");
    	gotoxy(65,4); printf("COURSE");
    	gotoxy(6,6);  printf("=====================================================================");
    
    	/*for(j = 1; j <= 19; j++)
    	{
        	for(i = 0; i <= 18; i++)
        	{
            	if(array[i].stud_num > array[i+1].stud_num)
            	{
    				temp = array[i].stud_num;
    				strcpy(temp1, array[i].name);
    				strcpy(temp2, array[i].bday);
    				strcpy(temp3, array[i].course);
    
    				array[i].stud_num = array[i+1].stud_num;
    				strcpy(array[i].name, array[i+1].name);
    				strcpy(array[i].bday, array[i+1].bday);
    				strcpy(array[i].course, array[i+1].course);
    
    				array[i+1].stud_num = temp;
    				strcpy(array[i+1].name, temp1);
    				strcpy(array[i+1].bday, temp2);
    				strcpy(array[i+1].course, temp3);
    
                }
    		}
    	}*/
    
    	for(j = 10, i = 0; array[i].stud_num != 0; j++, i++)
    	{
    		if(array[i].stud_num > 0)
    		{
    			gotoxy(6,j);  printf("%lld", array[i].stud_num);
    			gotoxy(25,j); puts(array[i].name);
    			gotoxy(45,j); puts(array[i].bday);
    			gotoxy(65,j); puts(array[i].course);
    		}
    	}
    
    	gotoxy(6,j+2); printf("\=====================================================================");
    	gotoxy(6,48);  printf("<----- Redirecting to the Administrator's Page...");
    	getche();
    }
    
    //*******************************************************************
    
    void modify_student(STUDENT array[20])
    {
    	int i;
    	long long int stud_num;
    
    	clrscr();
    	printf("\n\nPlease enter the student number of the account to be modified:\n\n");
    	scanf("%lld", &stud_num);
    	printf("\n\nSearching Student Account with Student Number %lld...", stud_num);
    
    	for(i = 0; i < 20; i++)
    	{
    		if(stud_num == array[i].stud_num)
    			break;
    	}
    
    	if(i >= 19)
    	{
    		printf("\n\nThe given student number did not match any of the existing accounts.\n");
    		printf("\n\nRedirecting to Administrator's Page...");
    		getche();
    	}
    
    	else
    	{
    		gotoxy(1,10);  printf("=====================================================");
    		gotoxy(1,12);  printf("Modifying the account of %s", array[i].name);
    		gotoxy(1,14);  printf("=====================================================");
    		gotoxy(1,16);  printf("Current Student Number: %lld", array[i].stud_num);
    		gotoxy(40,16); printf("New Student Number:");
    		scanf("%lld", &array[i].stud_num);
    		gotoxy(1,18);  printf("Current Name: %s", array[i].name);
    		gotoxy(40,18); printf("New Name: ");
    		gets(array[i].name);
    		gets(array[i].name);
    		gotoxy(1,20);  printf("Current Birthdate: %s", array[i].bday);
    		gotoxy(40,20); printf("New birthdate: ");
    		gets(array[i].bday);
    		gotoxy(1,22);  printf("Current Course: %s", array[i].course);
    		gotoxy(40,22); printf("New Course: ");
    		gets(array[i].course);
    
    		printf("\n\n=====================================================");
    		printf("\n\nStudent account modified...");
    		printf("\n\nRedirecting to Administrator's Page...");
    		printf("\n\n=====================================================");
    		getche();
    	}
    }
    
    //*******************************************************************
    
    void delete_student(STUDENT array[20])
    {
    	long long int stud_num;
    	int i;
    	int size;
    	char option;
    
    	clrscr();
    	gotoxy(14,2);  printf("===================================================");
    	gotoxy(14,4);  printf("          Delete  Student Account Function         ");
    	gotoxy(14,6);  printf("===================================================");
    	gotoxy(14,10); printf("Enter the Student Number of Account to be deleted: ");
    	gotoxy(20,12); scanf("%lld", &stud_num);
    	gotoxy(14,14); printf("Searching Account with Student Number %lld..", stud_num);
    
    	for(i = 0; i < 20; i++)
    	{
    		if(stud_num == array[i].stud_num)
    			break;
    	}
    
    	if(i >= 19)
    	{
    		gotoxy(14,18); printf("The given student number did not match");
    		gotoxy(14,20); printf("any of the existing accounts.");
    		getche();
    	}
    
    	else
    	{
    		gotoxy(14,18); printf("Student Number:");
    		gotoxy(35,18); printf("%lld", array[i].stud_num);
    		gotoxy(14,20); printf("Name");
    		gotoxy(35,20); printf("%s", array[i].name);
    		gotoxy(14,22); printf("Birthdate:");
    		gotoxy(35,22); printf("%s", array[i].bday);
    		gotoxy(14,24); printf("Course:");
    		gotoxy(35,24); printf("%s", array[i].course);
    
    		gotoxy(14,28); printf("Are you sure in deleting this account?");
    		gotoxy(14,30); printf("press 'y' for yes, else a no...");
    		gotoxy(14,32); option = getche();
    
    		if(option == 'y')
    		{
    			for(size = 0; array[size].stud_num > 0 && i < 20; size++)
    			{
    			}
    
    			for(i = i; i < size; i++)
    			{
    				array[i].stud_num = array[i+1].stud_num;
    				strcpy(array[i].name, array[i+1].name);
    				strcpy(array[i].bday, array[i+1].bday);
    				strcpy(array[i].course, array[i+1].course);
    			}
    		}
    
    		else
    		{
    			gotoxy(14,36); printf("Aborted");
    		}
    	}
    
    
    
    	gotoxy(14,48); printf("<-- Redirecting to Administrator's Page...");
    	getche();
    }
    
    //*******************************************************************
    
    void delete_all(STUDENT array[20])
    {
    	int i;
    	char option;
    
    	clrscr();
    	gotoxy(14,2);  printf("===================================================");
    	gotoxy(14,4);  printf("       Delete All Student Accounts Function        ");
    	gotoxy(14,6);  printf("===================================================");
    	gotoxy(14,24); printf("===================================================");
    	gotoxy(14,28); printf("===================================================");
    	gotoxy(14,12); printf("Are you sure you want to delete all student accounts?");
    	gotoxy(14,14); printf("press 'y' for yes, else a no...");
    	gotoxy(20,18); option = getche();
    
    	if(option == 'y')
    	{
    		for(i = 0; i < 20; i++)
    		{
    			array[i].stud_num = 0;
    			strcpy(array[i].name, "");
    			strcpy(array[i].bday, "");
    			strcpy(array[i].course, "");
    		}
    
    		gotoxy(14,26); printf("        Deletion of All Accounts Successful        ");
    	}
    
    	else
    	{
    		gotoxy(14,26); printf("                     Aborted                       ");
    	}
    
    
    	gotoxy(14,48); printf("<-- Redirecting to Administrator's Page...");
    	getche();
    }
    
    //*******************************************************************
    
    void search_student(STUDENT array[20])
    {
    	long long int stud_num;
    	int i;
    
    	clrscr();
    	gotoxy(14,2);  printf("===================================================");
    	gotoxy(14,4);  printf("             Search Student Function               ");
    	gotoxy(14,6);  printf("===================================================");
    	gotoxy(14,10); printf("Enter Student Number:");
    	gotoxy(40,10); scanf("%lld", &stud_num);
    	gotoxy(14,14); printf("Searching Account with Student Number %lld..", stud_num);
    
    	for(i = 0; i < 20; i++)
    	{
    		if(stud_num == array[i].stud_num)
    			break;
    	}
    
    	if(i >= 19)
    	{
    		gotoxy(14,18); printf("The given student number did not match");
    		gotoxy(14,20); printf("any of the existing accounts.");
    		getche();
    	}
    
    	else
    	{
    		gotoxy(14,18); printf("Student Number:");
    		gotoxy(35,18); printf("%lld", array[i].stud_num);
    		gotoxy(14,20); printf("Name");
    		gotoxy(35,20); printf("%s", array[i].name);
    		gotoxy(14,22); printf("Birthdate:");
    		gotoxy(35,22); printf("%s", array[i].bday);
    		gotoxy(14,24); printf("Course:");
    		gotoxy(35,24); printf("%s", array[i].course);
    	}
    
    	gotoxy(14,48); printf("<-- Redirecting to Administrator's Page...");
    	getche();
    }
    
    //*******************************************************************

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This part is the trouble. You want to swap array[j] with array[i] (or vice-versa depending on which way you're sorting), but i + 1 is incorrect.



    Code:
    				array[i].stud_num = array[i+1].stud_num;
    				strcpy(array[i].name, array[i+1].name);
    				strcpy(array[i].bday, array[i+1].bday);
    				strcpy(array[i].course, array[i+1].course);
    I know it's small potatoes, but it's easier if you stick with a certain convention with your iterators. When you put j on the outside loop, and i on the inside, it just looks "odd".

    You'll need two sets of loops for this, of course, not just one: You rem'd out the j loop!! You'll need it!
    Code:
    for(i = 0; i < MaxArraySize - 1; i++)  {
       for(j = i + 1; j < MaxArraySize; j++)  {
          if(A[i] > A[j] {
             temp = Array[i];
             Array[i] = Array[j];
             Array[j] = temp;
    
          }
       }
    }
    That's the version of bubblesort I use, and recommend. Add your struct members, and you've got it.




    BTW, you can swap out entire structs using a struct of this type, with a lot less work (although this is a good thing to learn to do the pedantic way, as well).

    Also, you can use a 3 pointers to a struct of that type to swap out all the struct members, easily.
    Last edited by Adak; 03-31-2009 at 11:58 AM.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    i tried ur code sir... but it wont display...
    i know the values are there because i used my search function...
    i think its the display section of the code which is under the bubble sort loops...

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I think you posted too much code again. Most of it isn't relevant.
    Are you able to simply use qsort instead?
    Otherwise, Adak shows a correct sorting algorithm. (It's not bubblesort though, bubblesort only swaps adjacent items)
    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"

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    1
    you should add struct members first before you immediately compile-run your program.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Huskar View Post
    i tried ur code sir... but it wont display...
    i know the values are there because i used my search function...
    i think its the display section of the code which is under the bubble sort loops...
    Ok, this is the display code under the sort:

    Code:
    	for(j = 10, i = 0; array[i].stud_num != 0; j++, i++)
    	{
    		if(array[i].stud_num > 0)
    		{
    			gotoxy(6,j);  printf("%lld", array[i].stud_num);
    			gotoxy(25,j); puts(array[i].name);
    			gotoxy(45,j); puts(array[i].bday);
    			gotoxy(65,j); puts(array[i].course);
    		}
    	}
    And the part that's bad is the testing part of the for loop, in red.

    What is going to happen when the first empty slot in the database comes along? Not only will it not be printed, but it will cause the program control to quit the for loop, entirely.

    So let's do a good for loop:

    Code:
    	for(i = 0, j = 10; i < NumberOfStudents; i++)
    	{
    		if(array[i].stud_num > 0)
    		{
    			gotoxy(6, j);  printf("%lld", array[i].stud_num);  
    			gotoxy(25, j); puts(array[i].name);
    			gotoxy(45, j); puts(array[i].bday);
    			gotoxy(65, j); puts(array[i].course);
                            j++;
                            if(j > MaxRowsOnDisplay)
                                j = 10;
    		}
    	}
    Sooner or later, the rows (j value), will not match up with your display, as the students continue to be printed out on screen, so it needs the if() statement in there, to reset j to 10.

    At that time, the display will start looking like junk, but try it for yourself. Only the longer rows of data will look OK. The answer is to print out empty spaces from the end of your data, to the end of the row, for each row you print out. You may want to use printf(), rather than puts(), for better control of this problem.
    Last edited by Adak; 03-31-2009 at 03:56 PM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, compress the table before sorting (that is, move the empty slots to the back) , and do not sort the empty slots.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by matsp View Post
    So, compress the table before sorting (that is, move the empty slots to the back) , and do not sort the empty slots.

    --
    Mats
    What I meant was you have a fixed row display:

    line 10: student_num student.name, etc.

    Now you print out your rows until you need to come back to line 10 again (no scrolling).

    When you print out on line 10 again, if the line is shorter than the last line 10, your display will be stuffed.

    The answer is to print out spaces after the row of data has been printed, until the end of the row has been reached, or (and perhaps a better idea, is to print out a row of blank char's (ascii 32) in the row that is about to be printed on. Then you know exactly how many char's to print (80 for a text screen), and it's easy to code.

    Moving empty "slots" in the database around, is just a poor idea, defeating the whole purpose of having them integrated into the database records.
    Last edited by Adak; 03-31-2009 at 05:51 PM.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Adak View Post
    Moving empty "slots" in the database around, is just a poor idea, defeating the whole purpose of having them integrated into the database records.
    It's no worse than moving the items around. Not necessarily saying one should do either though.
    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. Help With Bubble Sort
    By Explicit in forum C Programming
    Replies: 7
    Last Post: 05-28-2004, 08:46 AM
  2. Problem with Bubble Sort code
    By lisa1234 in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2004, 03:40 PM
  3. optimizing bubble sort
    By Sargnagel in forum C Programming
    Replies: 14
    Last Post: 01-23-2003, 06:27 AM
  4. bubble sort and structures?
    By bluebob in forum C Programming
    Replies: 7
    Last Post: 03-15-2002, 11:16 PM
  5. Urgent - Bubble Sort problem
    By Bada Bing in forum C Programming
    Replies: 1
    Last Post: 11-15-2001, 05:56 AM