#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
//function prototypes
void printRecords(char **, char **, float *, int);
int addNewRecord(char **, char **, float *, int);
int deleteRecords(char **, char **, float *, int);
void searchRecordByLastName(char **, char **, float *, int);
void sortByScores(char **, char **, float *, int);
void sortByLastName(char **, char **, float *, int);
void medianScoredStudents(char **, char **, float *, int);
//main function
int main()
{
//declare the 2D pointer variables
char **firstName, **lastName;
//single dimenstional variables
float *scores;
//integer variables
int count = 5, choice;
int i = 0, j = 0;
//allocate dynamic memory
firstName = (char **)malloc(20 * sizeof(char **));
lastName = (char **)malloc(20 * sizeof(char **));
scores = (float *)malloc(20 * sizeof(float));
for (i = 0; i < 5; i++)
{
firstName[i] = (char *)malloc(15 * sizeof(char));
lastName[i] = (char *)malloc(15 * sizeof(char));
}
//prompt the user to enter the values
printf("Working with student records: \n");
for (i = 0; i < 5; i++)
{
printf("Enter first name: ");
scanf("%s", firstName[i]);
printf("Enter second name: ");
scanf("%s", lastName[i]);
printf("Enter score: ");
scanf("%f", &scores[i]);
}
//print the details
printf("The details of student records are: \n");
printRecords(firstName, lastName, scores, count);
printf("\n");
//print the menu driven functionality
do
{
printf("\n");
printf("Menu for student records \n");
printf("1. Print records\n");
printf("2. Add a new record\n");
printf("3. Delete record(s)\n");
printf("4. Search by last name\n");
printf("5. Sort by score\n");
printf("6. Sort by last name\n");
printf("7. Median of the records\n");
printf("8. Exit Application \n");
printf("\n");
//prompt the user to enter the choice
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
//call the printRecords function
printRecords(firstName, lastName, scores, count);
break;
case 2:
//call the addNewRecord function
count = addNewRecord(firstName, lastName, scores, count);
break;
case 3:
//call the deleteRecords function
count = deleteRecords(firstName, lastName, scores, count);
break;
case 4:
//call the searchRecordByLastName function
searchRecordByLastName(firstName, lastName, scores, count);
break;
case 5:
//call the sortByScores function to sort the records
sortByScores(firstName, lastName, scores, count);
//call the printRecords function to print the records
printRecords(firstName, lastName, scores, count);;
break;
case 6:
//call the sortByLastName function to sort the records
sortByLastName(firstName, lastName, scores, count);
//call the printRecords function to print the records
printRecords(firstName, lastName, scores, count);
break;
case 7:
//call the sortByScores function to sort the records
sortByScores(firstName, lastName, scores, count);
//call the medianScoredStudents function to find median value
//print the records above the median
medianScoredStudents(firstName, lastName, scores, count);
break;
case 8:
//exit the program
printf("Exiting the program\n");
return 0;
break;
default:
printf("Choice not in Menu ,Exit the program\n");
break;
}
} while (1);
return 0;
}
//Method definition of printRecords: prints records of all students
void printRecords(char **firstName, char **lastName, float *scores, int count)
{
int i = 0;
printf("------------------------------------------------------- \n");
for (i = 0; i < count; i++)
{
printf("First Name: %10s, Second Name: %10s, Score: %5.2f\n", firstName[i], lastName[i], scores[i]);
}
printf("------------------------------------------------------- \n");
}
//addNewRecord – add a record of a student to the existing list
int addNewRecord(char **firstName, char **lastName, float *scores, int count)
{
printf("\nTo add the record, enter the prompted details.\n");
firstName[count] = (char *)malloc(15 * sizeof(char));
lastName[count] = (char *)malloc(15 * sizeof(char));
scores[count] = (float)malloc(sizeof(float));
printf("Enter first name: ");
scanf("%s", firstName[count]);
printf("Enter second name: ");
scanf("%s", lastName[count]);
printf("Enter score: ");
scanf("%f", &scores[count]);
count = count + 1;
return count;
}
//deleteRecords – deletes a record of a student to the existing list
int deleteRecords(char **firstName, char **lastName, float *scores, int count)
{
int i = 0;
char *name;
printf("\nTo delete a record from the list, enter the last name.\n");
name = (char *)malloc(15 * sizeof(char));
printf("Enter last name to delete: ");
scanf("%s", name);
int counter = count;
for (i = 0; i < count - 1; i++)
{
if (strcmp(lastName[i], name) == 0)
{
for(j = i; j<count; j++)
{
lastName[j] = lastName[j + 1];
firstName[j] = firstName[j + 1];
scores[j] = scores[j + 1];
}
counter--;
}
}
count = counter;
return count;
}
//searchRecordByLastName – searches a record of a student in the existing list
//by last name and prints the respective details
void searchRecordByLastName(char **firstName, char **lastName, float *scores, int count)
{
int i = 0;
char *name;
name = (char *)malloc(15 * sizeof(char));
printf("\nTo search a record from the list, enter the last name.\n");
printf("Enter last name to search: ");
scanf("%s", name);
int bool = 0;
for (i = 0; i < count; i++)
{
if (strcmp(lastName[i], name) == 0)
{
printf("First Name: %10s, Second Name: %10s, Score: %5.2f\n", firstName[i], lastName[i], scores[i]);
bool = 1;
}
}
if (bool == 0)
printf("Sorry! Unable to find the record.\n");
}
//sortByScores – sorts a student records by the scores
void sortByScores(char **firstName, char **lastName, float *scores, int count)
{
char *Ftemp = NULL;
char *Ltemp = NULL;
float temp = 0;
int i, j;
printf("\nSort the records by their scores.\n");
for (i = 0; i < count; i++)
{
for (j = i + 1; j < count; j++)
{
if (scores[j] < (scores[i]))
{
Ftemp = firstName[j];
firstName[j] = firstName[i];
firstName[i] = Ftemp;
Ltemp = lastName[j];
lastName[j] = lastName[i];
lastName[i] = Ltemp;
temp = scores[j];
scores[j] = scores[i];
scores[i] = temp;
}
}
}
}
//sortByLastName – sorts a student records by the last names
void sortByLastName(char **firstName, char **lastName, float *scores, int count)
{
char *Ftemp = NULL;
char *Ltemp = NULL;
float temp = 0;
int i, j;
printf("\nSort the records by their last names.\n");
for (i = 0; i < count; i++)
{
for (j = i + 1; j < count; j++)
{
if (strcmp(lastName[j], (lastName[i])) < 0)
{
Ftemp = firstName[j];
firstName[j] = firstName[i];
firstName[i] = Ftemp;
Ltemp = lastName[j];
lastName[j] = lastName[i];
lastName[i] = Ltemp;
temp = scores[j];
scores[j] = scores[i];
scores[i] = temp;
}
}
}
}
//medianScoredStudents – finds the median value and print the recodrs at the median
//and displays the records those are above median value
void medianScoredStudents(char **firstName, char **lastName, float *scores, int count)
{
int median = 0, i;
printf("\nFind the median of the records and printing the records above median.\n");
if (count % 2 != 0)
{
median = count / 2;
printf("The median score is: %0.2f\n", scores[median]);
}
else
{
median = count / 2;
printf("The first median score is: %0.2f\n", scores[median]);
printf("The second median score is: %0.2f\n", scores[median + 1]);
}
for (i = 0; i < median; i++)
{
printf("First Name: %10s, Second Name: %10s, Score: %5.2f\n", firstName[i], lastName[i], scores[i]);
}
}
У меня продолжают появляться ошибки, когда я использую mallo c в функции add new record, в которой говорится: «несовместимое неявное объявление встроенной функции« malloc »» и «значение указателя, используемое там, где значение с плавающей запятой было ожидаемый », и я не знаю, почему у кого-то есть какие-либо предложения, вот что я имею до сих пор, и это в C.