| | |
Why is qsort not initialised here!!??
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2005
Posts: 73
Reputation:
Solved Threads: 0
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...??
C Syntax (Toggle Plain Text)
//(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); }
•
•
•
•
Originally Posted by nabil1983
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...??
•
•
Join Date: Mar 2005
Posts: 73
Reputation:
Solved Threads: 0
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!!
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!!
Last edited by nabil1983; Dec 9th, 2005 at 3:06 pm. Reason: Forgot To Add something
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
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.
C Syntax (Toggle Plain Text)
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.
•
•
Join Date: Mar 2005
Posts: 73
Reputation:
Solved Threads: 0
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...
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.
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.
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);![]() |
Other Threads in the C Forum
- Previous Thread: need help with graph class
- Next Thread: pseodo code challenge <event driven programming>
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures suggestions system systemcall test testautomation unix user voidmain() wab win32api windows.h






