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);
} fflush(stdin); Donot use that. (Nor gets.) Use this:
int c;
while((c = getchar()) != '\n' && c != EOF); scanf("%s",cdDB[i].Artist); This is a bad way to read in a string (although, admittedly, better than gets()). You should use fgets() instead.Ok try to compile and run the program.
When i enter a cdrecord instead of asking me to continue or finish, the program automatically asks for next cd record data.
Can u please show what i need to modify in order to make it run as i want it to.
Also take alook at the quicksort function even though i call it in the program it dont work .
Can someone show what i need to modify,,, only thing is i cant make any big changes cuz im running out of time..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 80
struct Music
{
char Artist[20];
char Album[20];
char Year[4];
char Label[20];
char Genre[20];
};
void getline(char *line)
{
int c, i;
for ( i=0;i<MAXLINE-1 && ( c=getchar()) != EOF && c != '\n'; i++)
line[i] = c;
if(c == '\n')
{
line[i] = c;
++i;
}
line[i] = '\0';
}
void bufferKeys()
{
int c;
do{
c= getchar();
}while (c<32);
}
void naive_sort (struct Music array [], int arraySize, int * count);
void swap (struct Music * v1, struct Music * v2);
void enterData(struct Music myDB[]);
void filewriting(struct Music myDB[]);
void readfromFile(struct Music myDB[]);
void sortAndCriticalCount(struct Music myDB[], int choice);
void find(struct Music myDB[]);
void quick_sort (struct Music array[], int left, int right);
void q_sort (struct Music array[], int count);
const int datasize = 10;
// file pointer
FILE * mydata;
int main()
{
int choice = 0;
struct Music myDB[datasize];
do{
system("CLS"); //clear screen
printf("Application\n");
printf("1 Enter Data\n");
printf("2 Bubble Sort\n");
printf("3 Quick Sort\n");
printf("4 Write Data to file\n");
printf("5 Read Data to file\n");
printf("6 Search\n");
printf("7 Diagnostics\n");
printf("-1 Exit\n");
printf("Enter a number : ");
scanf("%d",&choice);
if(choice == 1)
enterData(myDB);
else if(choice == 2)
sortAndCriticalCount(myDB,2);
else if(choice == 3)
q_sort(myDB,1);
else if(choice == 4)
filewriting(myDB);
else if(choice == 5)
readfromFile(myDB);
else if(choice == 6)
find(myDB);
else if (choice == 7)
sortAndCriticalCount(myDB,7);
else if(choice == -1)
exit(0);
else if ((choice > 1) || (choice < 6))
{
printf("\nChoice not available press enter to continue");
fflush(stdin);
getch();
}
}while(choice != -1);
exit(0);
}
void sortAndCriticalCount(struct Music myDB[],int choice)
{
system("CLS");
int count = 0;
naive_sort (myDB, datasize, & count);
if(choice == 5)
printf("\nCritical count is : %d\n",count);
else
printf("Array has been sorted");
printf("Press enter to return to menu");
fflush(stdin);
getch();
}
void enterData(struct Music myDB[])
{
system("CLS");
int i;
//input Data from the keyboard
//go through record
//write to disk using formatted output
i=1;
while(i!=0)
{
printf("Enter the Artist: \n");
getline(myDB[i].Artist);
bufferKeys();
printf("Enter the Album: \n");
getline(myDB[i].Album);
bufferKeys();
printf("Enter Label name: \n");
getline(myDB[i].Label);
bufferKeys();
printf("Enter Year: \n");
getline(myDB[i].Year);
bufferKeys();
printf("Enter the Genre of music: \n");
getline(myDB[i].Genre);
bufferKeys();
printf("\n\n press 1 to continue,0 to stop\n\n");
scanf("%d",&i);
break;
}
}
void find(struct Music myDB[])
{
system("CLS");
int i;
char name[20];
printf("Enter Artist name :");
scanf("%s", name);
for(i = 0;i<datasize;i++)
{
if((strcmp(name,myDB[i].Artist))==0)
{
printf("\n");
printf("%s\n",myDB[i].Artist);
printf("%s\n",myDB[i].Album);
printf("%s\n",myDB[i].Label);
printf("%d\n",myDB[i].Year);
printf("%s\n",myDB[i].Genre);
}
}
printf("Press enter to return to menu");
fflush(stdin);
getch();
}
void filewriting(struct Music myDB[])
{
system("CLS");
int i;
mydata=fopen("mymusic.txt","w");
for (i=0;i<datasize;i++)
{
fprintf(mydata, "%s %s %s %s %s\n",myDB[i].Artist, myDB[i].Album,
myDB[i].Label,myDB[i].Year,myDB[i].Genre);
//Close the file
}
printf("Data has been written to file");
fflush(stdin);
getch();
fclose(mydata);
}
void readfromFile(struct Music myDB[])
{
system("CLS");
int j;
mydata = fopen("mymusic.txt","r");
//Read the data that was written
printf("reading data...\n");
for (j=0; j<datasize; j++)
{
fscanf(mydata,"%s %s %s %s %s",myDB[j].Artist, myDB[j].Album,
myDB[j].Label,
myDB[j].Year, myDB[j].Genre);
printf("Record %d\n",j+1," is...\n");
printf("%s\n",myDB[j].Artist);
printf("%s\n",myDB[j].Album);
printf("%s\n",myDB[j].Label);
printf("%s\n",myDB[j].Year);
printf("%s\n",myDB[j].Genre);
printf("\n");
}
fclose(mydata);
printf("Press enter to return to menu");
fflush(stdin);
getch();
// close the file again
}
void naive_sort (struct Music 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]);
}
}
}
void swap (struct Music * v1, struct Music * v2)
{
struct Music temp;
temp = *v1;
*v1 = *v2;
*v2 = temp;
}
void q_sort (struct Music array[], int count)
{
quick_sort(array,0,count-1);
}
void quick_sort (struct Music array[], int left, int right)
{
int i, j;
char *x;
struct Music 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);
}