Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- //Declaring the functions.
- void printrecords(char **firstname, char **lastname, float *score, int *numofrecords);
- void addnewrecord(char **firstname, char **lastname, float *score, int *numofrecords);
- void deleterecords(char **firstname, char **lastname, float *score, int *numofrecords);
- void searchbylastname(char **firstname, char **lastname, float *score, int *numofrecords);
- void sortbyscore(char **firstname, char **lastname, float *score, int *numofrecords);
- void sortbylastname(char **firstname, char **lastname, float *score, int *numofrecords);
- void findmedianscore(char **firstname, char **lastname, float *score, int *numofrecords);
- int main(){
- //Defining empty arrays for records to be stored.
- char **firstname;
- char **lastname;
- float *score;
- //Taking input of the number of records.
- int numofrecords = 0;
- while(numofrecords<5){
- printf("Please indicate number of records you want to enter (min 5): ");
- scanf("%d", &numofrecords);
- }
- //Dynamic allocation ( making pointers ) for score, firstname, lastname
- score=(float*)malloc(numofrecords*sizeof(float*));
- firstname=(char**)malloc(numofrecords*sizeof(char**));
- lastname=(char**)malloc(numofrecords*sizeof(char**));
- for(int x=0; x<numofrecords; x++)
- firstname[x]=(char*)malloc(21*sizeof(char*));
- for(int x=0; x<numofrecords; x++)
- lastname[x]=(char*)malloc(21*sizeof(char*));
- //Print statements for input of records.
- printf("Please input records of students \n(enter a new line after each record), with following format;\n");
- printf("first_name last_name score \n");
- //Taking input of the of records.
- for(int x=0; x<numofrecords; x++){
- scanf("%s %s %f", firstname[x], lastname[x], &score[x]);
- }
- //Providing the options and taking input of the users option.
- int opt;
- int x=0;
- while (x==0){
- printf("\n");
- printf("Print records (press 1)\n");
- printf("Add a new record (press 2)\n");
- printf("Delete record(s) (press 3)\n");
- printf("Search by last name (press 4)\n");
- printf("Sort by score (press 5)\n");
- printf("Sort by last name (press 6)\n");
- printf("Find median score (press 7)\n");
- printf("Exit the program (press 0)\n");
- scanf("%d", &opt);
- printf("\n");
- //Calling the respective function that the user chooses.
- if(opt==0){
- x++;
- }
- if(opt==1){
- printrecords(firstname, lastname, score, &numofrecords);
- }
- if(opt==2){
- addnewrecord(firstname, lastname, score, &numofrecords);
- }
- if(opt==3){
- deleterecords(firstname, lastname, score, &numofrecords);
- }
- if(opt==4){
- searchbylastname(firstname, lastname, score, &numofrecords);
- }
- if(opt==5){
- sortbyscore(firstname, lastname, score, &numofrecords);
- }
- if(opt==6){
- sortbylastname(firstname, lastname, score, &numofrecords);
- }
- if(opt==7){
- findmedianscore(firstname, lastname, score, &numofrecords);
- }
- }
- return 0;
- }
- //Defining the functions
- void printrecords(char **firstname, char **lastname, float *score, int *numofrecords){
- for(int i=0; i<*numofrecords; i++){
- printf("First Name: %s, Last Name: %s, Score: %.2f \n", firstname[i], lastname[i], score[i]);
- }
- }
- void addnewrecord(char **firstname, char **lastname, float *score, int *numofrecords){
- *numofrecords+=1;
- firstname=(char**)realloc(firstname,(*numofrecords)*sizeof(char**));
- firstname[*numofrecords-1]=(char*)calloc(20,sizeof(char*));
- lastname=(char**)realloc(lastname,(*numofrecords)*sizeof(char**));
- lastname[*numofrecords-1]=(char*)calloc(20,sizeof(char*));
- score=(float*)realloc(score,(*numofrecords)*sizeof(float*));
- scanf("%s %s %f", firstname[*numofrecords-1], lastname[*numofrecords-1], &score[*numofrecords-1]);
- for(int i=0; i<*numofrecords; i++){
- printf("First Name: %s, Last Name: %s, Score: %.2f \n", firstname[i], lastname[i], score[i]);
- }
- }
- void deleterecords(char **firstname, char **lastname, float *score, int *numofrecords){
- }
- void searchbylastname(char **firstname, char **lastname, float *score, int *numofrecords){
- char searchlastname[21];
- //Taking input of the last name the user wants to search.
- printf("Please enter the Last Name: ");
- scanf("%s", searchlastname);
- for(int i=0; i<*numofrecords; i++){
- //Comparing the input first name with the last names in the records.
- if (strcmp(lastname[i], searchlastname) == 0){
- printf("First Name: %s, Last Name: %s, Score: %.2f \n", firstname[i], lastname[i], score[i]);
- }
- }
- }
- void sortbyscore(char **firstname, char **lastname, float *score, int *numofrecords){
- char tempfirstname[21];
- char templastname[21];
- float tempscore;
- for (int i = 0; i < *numofrecords - 1; ++i){
- for (int j = i + 1; j < *numofrecords; ++j){
- //Comparing two consequtive scores and sorting in ascending order.
- if (score[i] > score[j]){
- //Using strcpy to sort the First Name array.
- strcpy(tempfirstname, firstname[i]);
- strcpy(firstname[i], firstname[j]);
- strcpy(firstname[j], tempfirstname);
- //Using strcpy to sort the Last Name array.
- strcpy(templastname, lastname[i]);
- strcpy(lastname[i], lastname[j]);
- strcpy(lastname[j], templastname);
- //Using strcpy to sort the Score array.
- tempscore = score[i];
- score[i] = score[j];
- score[j] = tempscore;
- }
- }
- }
- //Calling the print function to print records.
- printrecords(firstname, lastname, score, numofrecords);
- }
- void sortbylastname(char **firstname, char **lastname, float *score, int *numofrecords){
- char tempfirstname[21];
- char templastname[21];
- float tempscore;
- for (int i = 0; i < *numofrecords - 1 ; ++i){
- for (int j = i + 1; j < *numofrecords; ++j){
- //Comparing two consequtive lastnames and sorting in ascending order.
- if (strcmp(lastname[i], lastname[j])>0){
- //Using strcpy to sort the First Name array.
- strcpy(tempfirstname, firstname[i]);
- strcpy(firstname[i], firstname[j]);
- strcpy(firstname[j], tempfirstname);
- //Using strcpy to sort the Last Name array.
- strcpy(templastname, lastname[i]);
- strcpy(lastname[i], lastname[j]);
- strcpy(lastname[j], templastname);
- //Using strcpy to sort the Score array.
- tempscore = score[i];
- score[i] = score[j];
- score[j] = tempscore;
- }
- }
- }
- //Calling the print function to print records.
- printrecords(firstname, lastname, score, numofrecords);
- }
- void findmedianscore(char **firstname, char **lastname, float *score, int *numofrecords){
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement