Finding a Student
Lucy has come to complain: the teacher added up her first test wrong. She should have 35 points, not 27. The program needs a function that fixes a student's points by name. But first, it has to find the student in the array.
Searching is a pattern you know: a loop goes through the students, and when it finds the right one, it returns a pointer to them straight away (return also ends the loop). If the loop ends without finding anybody, the function returns NULL, the pointer that points nowhere. You compare the names with strcmp from string.h, which returns 0 for the same text:
#include <stdio.h>
#include <string.h>
typedef struct {
char name[20];
int price;
} Product;
Product *find_product(Product products[], int count, const char name[]) {
for (int i = 0; i < count; i++) {
if (strcmp(products[i].name, name) == 0) {
return &products[i];
}
}
return NULL;
}
int main(void) {
Product shop[] = {
{"Bread", 35},
{"Milk", 24},
};
Product *milk = find_product(shop, 2, "Milk");
if (milk != NULL) {
milk->price = 22;
}
printf("Milk: %d\n", shop[1].price);
if (find_product(shop, 2, "Cheese") == NULL) {
printf("No cheese today.\n");
}
return 0;
}Milk: 22
No cheese today.The function returns &products[i], the address of the product in the caller's array, not a copy. That's why changing milk->price changed the price in shop. The parameter is Product products[] without const, because the product is changed through the pointer it returns.
Common mistakes:
students[i].name == namedoesn't compare the texts but their addresses, so it never finds anybody. Usestrcmp.strcmpreturns0when the texts are the same, so the condition isstrcmp(...) == 0. Without== 0, it's true for every other name.- Without
#include <string.h>, the compiler reportserror: call to undeclared library function 'strcmp'.
And what if the teacher types a name that isn't in the array? Max, for example, is in the class next door. find_student() returns NULL, and writing student->points[test] through NULL would stop the program with RuntimeError: unreachable. That's why fix_points() checks the pointer before it uses it.
Instructions
Add #include <string.h> under #include <stdio.h>. Then, under add_bonus(), write a function Student *find_student(Student students[], int count, const char name[]) that returns a pointer to the student called name, or NULL if they aren't in the array.
Under it, write a function void fix_points(Student students[], int count, const char name[], int test, int new_points). It finds the student with find_student(). If it gets NULL, it prints the name followed by is not in the class. For Max, that's Max is not in the class. Otherwise, it sets the student's points in the test with index test to new_points.
In main(), above the call of print_report(), fix Lucy's first test (index 0): fix_points(students, count, "Lucy", 0, 35);. Lucy now has a B:
Lucy 35 40 41 38.7 BIf Lucy has 27 35 41, you used index 1. The first test has index 0.
Try a name that isn't in the class: under Lucy's fix, add fix_points(students, count, "Max", 0, 40);. This appears above the results:
Max is not in the class.Start programming for free
6/7