Ok i finished off the program i was making. now i just got a couple problems icant work out....spent hours doing this please help me with my last few probs...
First thing is when i write the records to file, and then when i try to sort it...the display shows all the records twice instead of just the sorted records.
As you can see i managed to make the bubble sort work with the critical count but i got help on that.
I added the code for a quick sort as well,, can some show me how i can implement this into this program.
What i want is to give the user a choice to sort using bubble sort for Artist and Quicksort for Genre.
Please i will really apreciate if anyone can help me solve these two problems...
//(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>
#define MAX 20
//Structure
struct CdRecords
{
char Artist[50];
char Album[50];
char Year[10];
char Label[50];
char Genre[50];
}cdDB[MAX];
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 sort_critical_count(struct CdRecords cdDB[], int choice);
void q_sort (struct CdRecords array [], int count);
void quick_sort (struct CdRecords array [], int left, int right);
// file pointer
FILE * fp;
/******************************************************************************************/
void sort_critical_count(struct CdRecords cdDB[],int choice)
{
system("CLS");
int count = 0;
naive_sort (cdDB, MAX, & count);
if(choice == 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,ch;
//input Data from the keyboard
//go through record
//write to disk using formatted output
for (i=0;i<MAX;i++)
{
printf("Enter the Artist: \n");
scanf("%s",cdDB[i].Artist);
printf("Enter the Album: \n");
scanf("%s",cdDB[i].Album);
printf("Enter Label name: \n");
scanf("%s",cdDB[i].Label);
printf("Enter Year: \n");
scanf("%s",&cdDB[i].Year);
printf("Enter the Genre of music: \n");
scanf("%s",cdDB[i].Genre);
printf("Enter 1 to continue or 0 To Finish: \n");
scanf("%d",&ch);
if (ch==0){
break;
}
}
}
/******************************************************************************************/
/******************************************************************************************/
void write_file(struct CdRecords cdDB[])
{
system("CLS");
int i;
fp=fopen("CD-Records.txt","w");
for (i=0;i<MAX;i++)
{
fprintf(fp, "%s %s %s %s %s\n",cdDB[i].Artist, cdDB[i].Album, cdDB[i].Label,cdDB[i].Year,cdDB[i].Genre);
//Close the file
}
printf("Data has been written to file");
fflush(stdin);
getch();
fclose(fp);
}
/***********************************/
/******************************************************************************************/
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<MAX; j++)
{
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");
}
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()
{
int ch ;
// struct CdRecords cdDB[i];
system("CLS");
do {
printf("MAIN MENU\n");
printf("Press 1 To Enter Records\n");
printf("Press 2 To Sort Records\n");
printf("Press 3 To Save Records To File\n");
printf("Press 4 To Read Records From File\n");
printf("Press 5 To Show Diagnostic Details\n");
printf("Press 6 To Search For A Record\n");
printf("Press 7 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:
write_file(cdDB);
break;
case 4:
read_file(cdDB);
break;
case 5:
sort_critical_count(cdDB,5);
break;
// case 6:
// record_search(cdDB);
// break;
case 7:
break;
default:
break;
}
} while(ch !=7);
}