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: //...
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 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...
[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...
Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
Please code by C++ The required week2.cpp file code is below Please ask if you have...
Please code by C++ The required week2.cpp file code is below Please ask if you have any questions instruction: 1. Implement the assignment operator: operator=(Vehicle &) This should make the numWheels and numDoors equal to those of the object that are being passed in. It is similar to a copy constructor, only now you are not initializing memory. Don’t forget that the return type of the function should be Vehicle& so that you can write something like: veh1 = veh2...
Please walk me through SPSS setup for this and complete the following: Consider the data below...
Please walk me through SPSS setup for this and complete the following: Consider the data below of inches of rainfall per month for three different regions in the Northwestern United States: Please use SPSS and complete the following: Plains Mountains. Forest April 25.2. 13.0 9.7 May 17.1 18.1 16.5 June 18.9. 15.7. 18.1 July 17.3 11.3 13.0 August 16.5 14.0 15.4 Using SPSS, perform an ANOVA test for the hypothesis that there is not the same amount of rainfall in...
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT