It messes up in the while loop when I am trying to mergeSort the data. The error message is:
Code:
86 [Warning] passing arg 2 of `findPerson' makes integer from pointer without a cast
Here is the full code:
Code:
#include<stdio.h>
#include<stdlib.h>
FILE* fin;
FILE* fout;
int i;
struct record
{
char first[20];
char last[20];
int age;
};
int findPerson(struct record* r, int num_records, char firstsearch[20], char lastsearch[20], int agesearch);
void mergeSort(struct record* r, int temp[], int array_size);
void m_sort(struct record* r, int temp[], int left, int right);
void merge(int numbers[], int temp[], int left, int mid, int right);
int countPeople; //Count the number of people in an age range
int countAges; //Count the number of people of a distinct age
int frequency; //Display the frequency of an age
int main (void)
{
struct record* r;
char filename[1024];
int num_records;
int choice;
//Ask user for the name of the file to read from
printf("Please enter the name of the file you would like to read from \n");
//Read in the file to read from
scanf("%s", filename);
//Open file for reading
fin = fopen(filename, "r");
//Check to make sure the file exists and opens correctly, if not display
//an error message to the user
if(fin == NULL)
{
printf("Unable to open the file %s\n", filename);
system("PAUSE");
return 0;
}
/*****************READ IN FROM THE FILE************************************/
//The first line of the input file will contain the number of records
fscanf(fin, "%d ", &num_records);
r = malloc(num_records * sizeof(*r));
//Read in all of the records
for (i = 0; i < num_records; i++)
{
int ret = fscanf(fin, "%s %s %d", r[i].first, r[i].last, &r[i].age);
printf("%s %s %d \n", r[i].first, r[i].last, r[i].age);
}
printf("\n");
/**************************MERGE SORT THE DAT..........***************************/
//Interactive portion that allows the user to do different things with the data
//Menu
printf("Choose an option (1-5):\n");
printf("1. Find the index of a person in the sorted ordering.\n");
printf("2. Count the number of people in an age range.\n");
printf("3. Count the number of distinct ages.\n");
printf("4. Display frequency of each distinct age.\n");
printf("5. Exit.\n");
//Read in a choice from the user
scanf("%d", &choice);
while(choice != 5)
{
if (choice == 1)
{
int d = findPerson(r, num_records, r[i].first, r[i].last, r[i].age);
printf("This person is at index %d \n", d);
}
else if (choice == 2)
{
}
else if (choice == 3)
{
}
else if (choice == 4)
{
}
else //If the user enters something other than (1-5)
{
printf("You have selected an invalid option\n");
system("PAUSE");
return 0;
}
}
//Close the input file
fclose(fin);
free(r);
printf("Exiting program...Goodbye!\n");
system("PAUSE");
return 0;
}
/*****************************FUNCTIONS****************************************/
int findPerson(struct record* r, int num_records, char firstsearch[20], char lastsearch[20], int agesearch)
{
printf("Enter a first name: \n");
scanf("%s", firstsearch);
printf("Enter a last name: \n");
scanf("%s", lastsearch);
printf("Enter an age: \n");
scanf("%d", &agesearch);
//Start at the first record and stop once you get to a record that has an
//age larger than the one that we are looking for. Then search by last name.
//Then search by first name to see if we have a match. Return the index
//location of that record.
for (i = 0; i < num_records; i++)
{
while (agesearch < r[i].age) //Don't look past the age the user entered
{
if(agesearch == r[i].age)
{
while (strcmp(lastsearch, r[i].last) < 0)
{
if(strcmp(lastsearch, r[i].last) == 0)
{
while (strcmp(firstsearch, r[i].first) < 0)
{
if(strcmp(firstsearch, r[i].first) == 0)
{
return i;
}
}
}
}
}
else
{
printf("No such person is present"); //This may need to be moved
i++;
}
}
}
return;
}