I got everything working, a couple of probls with file write. But ill work it out. I just need help on getting the quick sort working here...i got it written and even in my switch statement but for some reason it dont work....ne one know y...??

//(Database Management System(DBMS), user interface and report generator for results. 
//Also a diagnostic tool that allows access to the database metrics. 
//This is to show the efficiency of the sort algorithms used.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <conio.h>
/******************************************************************************************/

//Structure

struct CdRecords
	{	
 		char Artist[20];
		char Album[20];
		char Year[8];
		char Label[20];
		char Genre[20];
	};
/******************************************************************************************/

void naive_sort (struct CdRecords array [], int arraySize, int * count);
void swap (struct CdRecords * v1, struct CdRecords * v2);

void write_records(struct CdRecords cdDB[]);
void write_file(struct CdRecords cdDB[]);	
void read_file(struct CdRecords cdDB[]);
void delAlbum(struct CdRecords cdDB[]);

void sort_critical_count(struct CdRecords cdDB[], int choice);
void record_search(struct CdRecords cdDB[]);

void quick_sort (struct CdRecords array [], int left, int right);
void q_sort (struct CdRecords array [], int count);

void init_list(struct CdRecords cdDB[]);
int find_free(struct CdRecords cdDB[]);



const int datasize = 20;
// file pointer
FILE * fp;


/******************************************************************************************/
void init_list(struct CdRecords cdDB [])
{
	int i;					//TO CONTROL LOOP FOR CLEARING STRUCTURE
	for (i=0;i<datasize; i++)
		cdDB[i].Artist[0] = '\0';	//SET THE TITLE TO NULL
}
/******************************************************************************************/
//FIND FREE SLOT TO ADD NEW ENTRY

int find_free(struct CdRecords cdDB[])
{
	int i;
	char test;				//FIELD TO CHECK FOR A VALUE IN TITLE
	for(i=0; i<datasize; i++){
		test = cdDB[i].Album[0];	//STORE TITLE IN TEST FIELD

		if (test=='\0'){		//IF TEST FIELD IS NULL THERE IS AN EMPTY SLOT		
			return i;		//RETURN THE SLOT NUMBER
	}

	}

	return -1;				//IF NO FREE SLOTS FOUND RETURN -1
}
/******************************************************************************************/

void sort_critical_count(struct CdRecords cdDB[],int ch)
{
     system("CLS");
	 int count = 0;
	 
     naive_sort (cdDB, datasize, & count);
	 
	 					//if(ch==3){
	 					//q_sort (cdDB, count);
						// }
     if(ch == 5)
        printf("\nCritical count is : %d\n",count);
     else
        printf("Array has been sorted");
     printf("Press Enter To Continue");
     fflush(stdin);
     getch();
}	

/******************************************************************************************/

void write_records(struct CdRecords cdDB[])
{	
	system("CLS");     	
    int i;    
	char ch;
	ch=1; 
	i=0; 
	int slot;				// INTEGER TO LOCATE FREE SPACE IN STRUCTURE
		char s[80];				// CALL THE FIND_FREE FUNCTION AND RETURN VALUE IN FIELD - SLOT
		slot = find_free(cdDB);
		
		if (slot==-1){				//IF SLOT = -1 THEN THE STRUCTURE IS FULL
	  		printf("Database Full");
		return;
	}
    //input Data from the keyboard
    //go through record 
    //write to disk using formatted output		
   // for (i=0;i<datasize;i++)
			
   // {	
			

       printf("Enter the Artist: \n");
       //scanf("%s",cdDB[i].Artist);
	   gets(cdDB[slot].Artist);
	   gets(cdDB[slot].Artist);
	   printf("Enter the Album: \n");
	   //scanf("%s",cdDB[i].Album);
	   gets(cdDB[slot].Album);
	   printf("Enter Label name: \n");
	   //scanf("%s",cdDB[i].Label);
	   gets(cdDB[slot].Label);
	   printf("Enter Year: \n");
	   //scanf("%d",&cdDB[i].Year);
	   gets(cdDB[slot].Year);
	   printf("Enter the Genre of music: \n");
	   //scanf("%s",cdDB[i].Genre);
	   gets(cdDB[slot].Genre);
	  // printf("Enter 1 to continue or 0 To Finish: \n");
	  // scanf("%d",&ch);  
	//if(ch==0)
		//{
		//break;
		//}          
   // }
}

/******************************************************************************************/
void delAlbum(struct CdRecords cdDB[])
{
	int slot;				//INTEGER TO HOLD SLOT NUMBER
	char s[80];				//FIELD TO HOLD INPUTTED RECORD NUMBER TO DELETE
	printf("Enter Album: ");		//PROMPT FOR Album TO DELETE
	gets(s);
	slot = atoi(s);				//CONVERT INPUT VALUE TO A SLOT NUMBER

	if (slot>=0 && slot < datasize)		
		cdDB[slot].Artist[0] = '\0';	//CLEAR SELECTED STRUCTURE ENTRY
}
/******************************************************************************************/
void record_search(struct CdRecords cdDB[])
{
   system("CLS");
   int i;
   char name[20];
     
   printf("Enter Artist name :");
   scanf("%s", name);
   for(i = 0;i<datasize;i++)
   {
     if((strcmp(name,cdDB[i].Artist))==0)
     {
	  printf("\n");
        printf("%s\n",cdDB[i].Artist);
	  printf("%s\n",cdDB[i].Album);
	  printf("%s\n",cdDB[i].Label);
	  printf("%s\n",cdDB[i].Year);
	  printf("%s\n",cdDB[i].Genre);
     
	 }
   }
   printf("Press Enter To Continue");
   fflush(stdin);
   getch();

}

/******************************************************************************************/

void write_file(struct CdRecords cdDB[])
{
    system("CLS");
	int i;
    fp=fopen("CD-Records.txt","w");
	
    for (i=0;i<datasize;i++)		
    {
         fprintf(fp, "%s		%s		%s		%s		%s\n",cdDB[i].Artist,cdDB[i].Album,cdDB[i].Label,cdDB[i].Year,cdDB[i].Genre); 
    }
    
		printf("Data has been written to file");
    		fflush(stdin);
    		getch();            
    		fclose(fp);//Close the file

		}

/******************************************************************************************/

void read_file(struct CdRecords cdDB[])
{
	system("CLS");	
	int j;
		fp = fopen("CD-Records.txt","r");
	//Read the data that was written	
		printf("reading data...\n");

      for (j=0; j<datasize; j++) 
      {
		if (cdDB[j].Artist[0]){	
		fscanf(fp,"%s %s %s %s %s",cdDB[j].Artist, cdDB[j].Album, cdDB[j].Label, &cdDB[j].Year, cdDB[j].Genre);
		printf("Record %d\n",j+1," is...\n");
		printf("%s\n",cdDB[j].Artist);
		printf("%s\n",cdDB[j].Album);
		printf("%s\n",cdDB[j].Label);
		printf("%s\n",cdDB[j].Year);
		printf("%s\n",cdDB[j].Genre);
		//printf("\n\n");
		}
		printf("\n\n");
	}
	
	  fclose(fp);
      printf("Press Enter To Continue");
      fflush(stdin);
      getch();     
	// close file 
}

/******************************************************************************************/

	// Sort an array of integers with inefficient version of bubblesort

void naive_sort (struct CdRecords array [], int arraySize, int * count)
 {
   for (int pass = 0; pass <= arraySize - 2; pass++)
    { for (int counter = 0; counter <= arraySize - 2-pass; counter++)
       {
        *count = *count + 1; // count critical operations
        if (strcmp(array[counter].Artist,array[counter+1].Artist)>0)
	   swap (&array[counter], &array[counter+1]);
       }
    }
 }

	// Exchange a given pair of values in an array
void swap (struct CdRecords * v1, struct CdRecords * v2)
  {
    struct CdRecords temp;
    temp = *v1;
    *v1 = *v2;
    *v2 = temp;
  }

/******************************************************************************************/
void q_sort (struct CdRecords array [], int count)
{
quick_sort(array,0,count-1);
}
void quick_sort (struct CdRecords array [], int left, int right)
{
int i, j;
char *x;
struct CdRecords temp;

i = left; 
j = right;

x = array[(left+right)/2].Genre;

do {
		while(strcmp(array[i].Genre,x)<0 && i<right) i++;
		while(strcmp(array[i].Genre,x)>0 && j>left) j--;
		if(i<=j) {
			temp = array[i];
			array[i] = array[j];
			array[j] = temp;
			i++; j--;
			
			}
			
			}while(i<=j);
			
		if(left<j) quick_sort(array, left, j);
	  	if(i<right)quick_sort(array, i, right);
	}
/******************************************************************************************/

int main() 
{
     system("CLS");
     int ch = 0;
       
     struct CdRecords cdDB[datasize];
     init_list(cdDB);	//CLEAR THE STRUCTURE
      do{
        system("CLS");
        printf("MAIN MENU\n");	
        printf("Press 1 To Enter Records\n");	
        printf("Press 2 To Bubble Sort Records\n");	
		printf("Press 3 To Quick Sort Records\n");	
        printf("Press 4 To Save Records To File\n");	
        printf("Press 5 To Read Records From File\n");	  
        printf("Press 6 To Show Diagnostic Details\n");
        printf("Press 7 To Search For A Record\n");
		printf("Press 8 To Delete A Record\n");
		printf("Press 9 To Quit\n");         
        printf("Enter Your Choice : ");
        scanf("%d",&ch);
    
  switch (ch)
  		{
  
          case 1:
           	write_records(cdDB);
          	 	break;
		  case 2:
			sort_critical_count(cdDB,2);
          		break;
          case 3:
            quick_sort(cdDB,1,2);
          		break;
          case 4:
             write_file(cdDB);
         	  	break;
          case 5:
   	        read_file(cdDB);
         		break;
          case 6:
            sort_critical_count(cdDB,5);       
  				break;
          case 7:
           record_search(cdDB);
  				break;
		  case 8:
           delAlbum(cdDB);
  				break;	
          case 9:
  				break;
  		  default:			
  				break;	
      		 } 
  					
  		} while(ch !=8);
	}

I got everything working, a couple of probls with file write. But ill work it out. I just need help on getting the quick sort working here...i got it written and even in my switch statement but for some reason it dont work....ne one know y...??

what "doesn't work" -- compiler errors? if yes, what are they. runtime errors? describe them.

Comments
Thanks For All YOur Help

For the switch, print something out in the default (for example, print out the value of ch to see what value was passed...)

that the thing im not getting no compiler errors. When in the program i press the option to do a quick sort, nothing happens, its just prompts for another option.
?? dont know y

i've done wat u said and it still did not sort the data but the problem now also is that when i goto
display the data on screen i get all jibrish data entries???
and the search aint working either,, im sure i've done the coding right for it though!!

1. function init_list is not clearing all the fields in the structure. There is a very easy and quick way to do that with only one programming line and no loops

void init_list(struct CdRecords cdDB [])
{
	memset(cdDB,0,datasize*sizeof(CdRecords));
}

2. The reason you see gibberish after sorting the array is because the sort alborithms attempt to sort all rows of the CdRecords, even though the array may contain just a few valid rows, however many you type in. function find_free() gets the row number of the next unused row, so all you have to do is pass that to the sort algorithm and have it sort only that many rows.

when i use the one code line , its giving acompile error "Undefined Identifier CdRecords"

Also might sound dumb and crazy but how do i pass the find_free() to the sort algorithm and use it to only sort the rows....???

you probably think its crazy but im still at a learning stage...

Apreciate all the help u've already given me...

1. check the spelling -- just copy that line that I posted and past it into your program.

2. Just do it like this. you don't need to change the sort function at all.

void sort_critical_count(struct CdRecords cdDB[],int ch)
{
     system("CLS");
	 int count = 0;
 int nRows = find_free(cdDB);
     naive_sort (cdDB,  nRows, & count);

At the end of main() change the do-while loop to exit when ch == 9 instead of 8, so that you can exit the menu.

} while(ch !=9);

I have checked it with the structurename above and all is the same

void init_list(struct CdRecords cdDB [])
{
	int i;					//TO CONTROL LOOP FOR CLEARING STRUCTURE
	for (i=0;i<datasize; i++)
		cdDB[i].Artist[0] = '\0';	//SET THE TITLE TO NULL	
	memset(cdDB,0,datasize*sizeof(CdRecords));	//TO CLEARING STRUCTURE
		//SET THE TITLE TO NULL

Oh I think I know what is wrong. you probably have a *.c file -- mine is *.cpp. Change as shown below in red. And you don't need any of those other extraneous lines in that function.

void init_list(struct CdRecords cdDB [])
{
	memset(cdDB,0,datasize*sizeof(struct CdRecords));
}

I've had trouble with memset with structures before. So I used calloc instead.

cdDB = (CdRecords*) calloc(datasize, sizeof(CdRecords))

This should set everything to 0.

Ancient Dragon might be able to shed some light on this.

Hey it was because of the file save *c / *cpp

it works fine now. thank you very much for your help mate.

I have another of getting the quicksort to work!!
But imma work on it, and see if i can do it on my own.

Thanks again.

In the OPs case he doesn't want to use calloc() because the array is not being dynamically allocated. You may have had problems with memset when the second parameter used sizeof(some pointer) -- which always return 4 on most 32-bit compilers.

Quick Q.

In the first post where i got all my coding,, is the way i call the quick-sort in the switch correct,, and are the arguments supposed to be like that?

I don't think the arguments are right. see function q_sort() for correct arguments, where count should be the number of valid rows in the array to be sorted.

Could you explain alil more because the quick sort is actually confusing me,, the layout in the code is what i got from a book and i simply changed it according to my structure.
When i compile it gives me no errors.
But still wont quick sort when used in option 3.

This article has been dead for over six months. Start a new discussion instead.