Question

In: Computer Science

Please complete the following code in challenge.c. The code for main.c and challenge.h is below that....

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

Solutions

Expert Solution

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;
}



Related Solutions

Please complete the following code in C using the comments as instructions. Further instructions are below...
Please complete the following code in C using the comments as instructions. Further instructions are below the code. challenge.c // goal: print the environment variables to the file "env.txt", one per line // (If envp is NULL, the file should be empty, opening in write mode will do that.) // example: // inputs: // envp/environ = {"E1=2","E2=7",NULL} // outputs: // env.txt as a string would be "E1=2\nE2=7\n" // example: // inputs: // envp/environ = {NULL} or NULL // outputs: //...
C++ Please complete based on the code below. Declare another stack object to the code in...
C++ Please complete based on the code below. Declare another stack object to the code in main(). Add a stack operator called CopyStack to the Stack class which, when executed, copies the contents of the first stack into the second stack. Modify your menu so that this option is available. The menu should also allow the second stack to be printed, pushed, popped, and so forth, just like with the first stack. #include using namespace std; #define MAXSize 10 class...
PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite...
PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...
Please complete in MASM (x86 assembly language). Use the code below to get started. Use a...
Please complete in MASM (x86 assembly language). Use the code below to get started. Use a loop with indirect or indexed addressing to reverse the elements of an integer array in place. Do not copy the elements to any other array. Use the SIZEOF, TYPE, and LENGTHOF operators to make the program as flexible as possible if the array size and type should be changed in the future. .386 .model flat,stdcall .stack 4096 ExitProcess PROTO,dwExitCode:DWORD .data    ; define your...
Please complete following c++ code asap using following prototypes complete each missing part / Linked list...
Please complete following c++ code asap using following prototypes complete each missing part / Linked list operations int getLength() const {return length;} void insertNode(College); bool deleteNode(string); void displayList() const; bool searchList(string, College &) const; }; main.cpp /*   Build and procees a sorted linked list of College objects. The list is sorted in ascending order by the college code. Assume that the college code is unique. */ #include <iostream> #include <fstream> #include <string> #include "LinkedList.h" using namespace std; void buildList(const string...
Please follow the instructions carefully and complete the code given below. Language: Java Instructions from your...
Please follow the instructions carefully and complete the code given below. Language: Java Instructions from your teacher: (This is a version of an interview question I once saw.) In this problem, we will write a program that, given an integer k, an integer n, and a list of integers, outputs the number of pairs in in the list that add to k. To receive full credit for design, your algorithm must have a runtime in O(n) , where n is...
Please carefully review the code to fill in after the given instructions and complete the code...
Please carefully review the code to fill in after the given instructions and complete the code for me. Thank you in advance. In this problem, we will implement a simple dictionary of common words in the English language, represented as an array of words paired with their lengths. You will need to implement each of the below methods in the Dictionary class. In this problem, the first line of input represents the method to call. It will be one of...
Complete ArrayCollection.java with following methods (Please complete code in java language): public ArrayCollection(); public ArrayCollection(int size);...
Complete ArrayCollection.java with following methods (Please complete code in java language): public ArrayCollection(); public ArrayCollection(int size); public boolean isEmpty(); public boolean isFull(); public int size(); // Return number of elements in the Collection. public String toString(); public boolean add(T element); // Add an element into the Collection, return true if successful public boolean remove(T target); // Remove the target from Collection, return true if successful public boolean removeAll(T target); // Remove all occurrences of Target, return if successful public void...
[50%] Code Snippet Given snippet code below that you are required to complete. You are not...
[50%] Code Snippet Given snippet code below that you are required to complete. You are not allowed to make a new function or change any given code. Please complete each section that are marked with the notation “INSERT YOUR CODE HERE”. Once you complete the snippet below, your output should have the same result with the given output below. Descriptions: [15%] isValid() This function is for checking that there is no duplicated employee data in the linked list. This function...
Using the provided Java program below, complete the code to do the following. You may need...
Using the provided Java program below, complete the code to do the following. You may need to create other data items than what is listed for this assignment. The changes to the methods are listed in the comments above the method names. TODO comments exist. Apply any TODO tasks and remove the TODO comments when complete. Modify the method findMyCurrency() to do the following:    a. Use a do-while loop b. Prompt for a currency to find. c. Inside this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT