Answer:
The method definition to this question can be defined as follows:
int getPop(char myCode[3])//defining a method getPop that takes char array in its parameter
{
FILE* f = NULL;//defining a pointer variable
int pop;//defining integer variable
char s_code[2];//defining char array state_code
char s_name[100];//defining char array stateName
f = fopen("state", "r");//using pointer variable that use fopen method for open file
if(f == NULL)//defining if block that check file is empty
{
return -1;//return value -1
}
while(!feof(f))//defining while loop for input value in file
{
fscanf(f, "%s %s %d",s_code, s_name, &pop);//input value
if(strncmp(myCode, s_code, 2) == 0)//use if block to compare string value
{
printf("Population for %s: %d", s_name, pop);//print value
return 0;//return 0
}
}
fclose(f);//close file
return 0;
}
Step-by-step explanation:
In the above code, a method "getPop" is defined that accepts a character array "myCode" in its parameter, and inside the method, one pointer variable "f", one integer variable "pop", and two char array "s_code and s_name" is declared.
In the method firstly fopen method is used that holds a file and use if block to check it is not empty, if the condition is true it will give a value that is "-1".
In the next step, a while loop is declared, that input value and use if block to compare string value, and return 0, and at the last, it closed the file.