In: Computer Science
Please complete the following code in challenge.c. The code for main.c and challenge.h is below that. (Don't edit main.c or challenge.h, only edit challenge.c) The instructions are in the comments. Hint: the_person is declared in main, so you need to define it in challenge.c using extern
challenge.c
#include "challenge.h"
//return: struct
//param: (struct person p1, struct person p2)
//TODO: create a function that returns the person who has a higher GPA.
// 1. if GPAs are equal, then return the first person (the first parameter).
// Hint: this function can only be called in the file where it is declared, so You might need to put a key word before the function declaration.
struct person compareTo(struct person p1, struct person p2) {
return null;
}
//return: void
//param: (struct person persons[], int len)
//TODO: create a function that assign the person who has the highest GPA to the variable the_person defined in main.c.
// 1. use compareTo() function to find the person who has the highest GPA.
// 2. update the value of the_person in main function to the person.
// Hint: this function can only be called in the file where it is declared, so You might need to put a key word before the function declaration.
void find_the_person(struct person persons[], int len) {
}
main.c
#include "challenge.h"
struct person the_person;
// return the person who is older.
struct person compareTo(struct person p1, struct person p2){
if (p1.age >= p2.age){
return p1;
}
return p2;
}
// return the oldest person.
// update the value of the_person to the oldest person.
void find_the_person(struct person persons[], int len){
if (len <= 0) return;
the_person = persons[0];
for(int i = 1; i < len; i++){
the_person = compareTo(the_person, persons[i]);
}
}
// main function for non-test code
// This exists solely for the benefit of the students to test their own code
int main()
{
printf("Hello, World\n");
}
challenge.h
#include <stdio.h>
#ifndef CH_HEAD
#define CH_HEAD
struct person {
char name[20];
int age;
double GPA;
};
#endif
challenge.c
======================
#include "challenge.h"
// function that returns the person who has a higher GPA
// return: struct
// param: (struct person p1, struct person p2)
static struct person compareTo(struct person p1, struct person p2)
{
if (p1.GPA >= p2.GPA) {
return p1;
}
return p2;
}
// function that assign the person who has the highest GPA to
the variable the_person defined in main.c
// return: void
// param: (struct person persons[], int len)
static void find_the_person(struct person persons[], int len)
{
if (len <= 0) return;
extern struct person the_person; // declaration of
external variable
// extern variable can be accesed from any where in
program
the_person = persons[0];
for (int i = 1; i < len; i++) {
the_person = compareTo(the_person,
persons[i]);
}
}
================================================================
Above code is code you asked to modify. It will work as you expected. I have provided modified code from main.c and also added a function in challenge.c to show how static function can be used to modify external variable that is declared outside of file. Below is code that is for demonstration only, You may not add it to your assignment. Let me know if you still have any problem or doubt. Thank you.
=================================================================
challenge.h
=======================
#include <stdio.h>
#ifndef CH_HEAD
#define CH_HEAD
struct person {
char name[20];
int age;
double GPA;
};
#endif
=======================
challenge.c
=======================
#include "challenge.h"
// function that returns the person who has a higher GPA
// return: struct
// param: (struct person p1, struct person p2)
static struct person compareTo(struct person p1, struct person p2)
{
if (p1.GPA >= p2.GPA) {
return p1;
}
return p2;
}
// function that assign the person who has the highest GPA to
the variable the_person defined in main.c
// return: void
// param: (struct person persons[], int len)
static void find_the_person(struct person persons[], int len)
{
if (len <= 0) return;
extern struct person the_person; // declaration of
external variable
// extern variable can be accesed from any where in
program
the_person = persons[0];
for (int i = 1; i < len; i++) {
the_person = compareTo(the_person,
persons[i]);
}
}
/////////////////////////////////////////////////////////
// Below code is for test only
// use wrapper function to call static function outside of the
file
void wraper(struct person persons[], int len) {
find_the_person(persons, len);
}
=======================
main.c
=======================
#include "challenge.h"
#include <string.h>
#pragma warning(disable : 4996) // for visual studio only
struct person the_person;
// return the person who is older.
struct person compareTo(struct person p1, struct person p2) {
if (p1.age >= p2.age) {
return p1;
}
return p2;
}
// return the oldest person.
// update the value of the_person to the oldest person.
void find_the_person(struct person persons[], int len) {
if (len <= 0) return;
the_person = persons[0];
for (int i = 1; i < len; i++) {
the_person = compareTo(the_person,
persons[i]);
}
}
// main function for non-test code
// This exists solely for the benefit of the students to test their
own code
int main()
{
// create an array of person with size 5
struct person* persons = (struct person*)
malloc(sizeof(struct person) * 5);
// initialize array
strcpy(persons[0].name, "First Person");
persons[0].age = 12;
persons[0].GPA = 3.6;
strcpy(persons[1].name, "Seconod Person");
persons[1].age = 17;
persons[1].GPA = 3.4;
strcpy(persons[2].name, "Third Person");
persons[2].age = 15;
persons[2].GPA = 3.9;
strcpy(persons[3].name, "Fourth Person");
persons[3].age = 14;
persons[3].GPA = 2.8;
strcpy(persons[4].name, "Fifth Person");
persons[4].age = 19;
persons[4].GPA = 3.1;
// call function defined in main.c file
find_the_person(persons, 5);
// print the person
printf("Use of global function:\n");
printf("Name: %s\n", the_person.name);
printf("Age: %d\n", the_person.age);
printf("GPA: %.2f\n", the_person.GPA);
// static function can be used only in the file
where they are declared
// use a wraper to call static function from
challenge.c
extern void wraper(struct person persons[], int len);
// declare external function to be used
wraper(persons, 5);
// print the person
printf("\nUse of static function:\n");
printf("Name: %s\n", the_person.name);
printf("Age: %d\n", the_person.age);
printf("GPA: %.2f\n", the_person.GPA);
free(persons);
return 0;
}