user_id
stringlengths 24
24
| time
stringlengths 24
24
| feature_type
stringclasses 5
values | feature_version
stringclasses 2
values | input_question
stringlengths 1
1.24k
⌀ | input_code
stringlengths 2
18.5k
⌀ | input_intention
stringlengths 1
2.48k
⌀ | input_task_description
stringlengths 18
3.32k
⌀ |
---|---|---|---|---|---|---|---|
63c6479f5f004bf4898cf545 | 2023-02-12T22:15:01.596Z | Question from Code | v1 | what do i need to change to get the right time | time_t curr_time;
time(&curr_time);
new_post->date = &curr_time;
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-12T22:41:15.199Z | General Question | v1 | how can i allocate space for a time_t date | null | null | null |
63c6479f5f004bf4898cf545 | 2023-02-12T22:45:36.645Z | Question from Code | v1 | what do i need to change so the current time is stored in new_post->date, a time_t variable? | time_t *curr_time = malloc(sizeof(time_t));
time(curr_time) ;
new_post->date = curr_time;
free(curr_time);
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-12T22:46:31.269Z | Question from Code | v1 | do i need to dereference curr_time when iassign it to new_post->date? how do i do that | time_t *curr_time = malloc(sizeof(time_t));
time(curr_time) ;
new_post->date = curr_time;
free(curr_time);
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-12T22:47:10.083Z | Question from Code | v1 | on which line do i reference curr_time | time_t *curr_time = malloc(sizeof(time_t));
time(curr_time) ;
new_post->date = curr_time;
free(curr_time);
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-12T22:47:40.113Z | Question from Code | v1 | on which line do i need to reference curr_time so this program gives me the correct time | time_t *curr_time = malloc(sizeof(time_t));
time(curr_time) ;
new_post->date = curr_time;
free(curr_time);
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T00:36:41.336Z | Question from Code | v1 | on which line should i free new_user | int create_user(const char *name, User **user_ptr_add) {
// Check if name is too long
if (strlen(name) >= MAX_NAME) {
return 2;
}
// Create a pointer curr to traverse the list
User *curr = *user_ptr_add;
// Create a new user
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("Failed Malloc");
exit(-1);
}
strcpy(new_user->name, name);
new_user->next = NULL;
// If list is empty, make the head pointer equal to our new user
if (*user_ptr_add == NULL) {
*user_ptr_add = new_user;
return 0;
}
// Traverse the list until the end
while (curr->next != NULL) {
// Check if name already exists
if (strcmp(name, curr->name) == 0) {
return 1;
}
curr = curr->next;
}
// Check if the name is the same as the name of the last user
// in the list
if (strcmp(name, curr->name) == 0) {
return 1;
}
// Make the last element equal to our new user
curr->next = new_user;
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T00:38:49.715Z | Question from Code | v1 | do i need to deallocate memory? | int create_user(const char *name, User **user_ptr_add) {
// Check if name is too long
if (strlen(name) >= MAX_NAME) {
return 2;
}
// Create a pointer curr to traverse the list
User *curr = *user_ptr_add;
// Create a new user
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("Failed Malloc");
exit(-1);
}
strcpy(new_user->name, name);
new_user->next = NULL;
// If list is empty, make the head pointer equal to our new user
if (*user_ptr_add == NULL) {
*user_ptr_add = new_user;
return 0;
}
// Traverse the list until the end
while (curr->next != NULL) {
// Check if name already exists
if (strcmp(name, curr->name) == 0) {
return 1;
}
curr = curr->next;
}
// Check if the name is the same as the name of the last user
// in the list
if (strcmp(name, curr->name) == 0) {
return 1;
}
// Make the last element equal to our new user
curr->next = new_user;
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T00:39:14.120Z | Question from Code | v1 | where should i deallocate memory | int create_user(const char *name, User **user_ptr_add) {
// Check if name is too long
if (strlen(name) >= MAX_NAME) {
return 2;
}
// Create a pointer curr to traverse the list
User *curr = *user_ptr_add;
// Create a new user
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("Failed Malloc");
exit(-1);
}
strcpy(new_user->name, name);
new_user->next = NULL;
// If list is empty, make the head pointer equal to our new user
if (*user_ptr_add == NULL) {
*user_ptr_add = new_user;
return 0;
}
// Traverse the list until the end
while (curr->next != NULL) {
// Check if name already exists
if (strcmp(name, curr->name) == 0) {
return 1;
}
curr = curr->next;
}
// Check if the name is the same as the name of the last user
// in the list
if (strcmp(name, curr->name) == 0) {
return 1;
}
// Make the last element equal to our new user
curr->next = new_user;
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T00:39:41.655Z | Question from Code | v1 | if i deallocate memory, will it delete the user | int create_user(const char *name, User **user_ptr_add) {
// Check if name is too long
if (strlen(name) >= MAX_NAME) {
return 2;
}
// Create a pointer curr to traverse the list
User *curr = *user_ptr_add;
// Create a new user
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("Failed Malloc");
exit(-1);
}
strcpy(new_user->name, name);
new_user->next = NULL;
// If list is empty, make the head pointer equal to our new user
if (*user_ptr_add == NULL) {
*user_ptr_add = new_user;
return 0;
}
// Traverse the list until the end
while (curr->next != NULL) {
// Check if name already exists
if (strcmp(name, curr->name) == 0) {
return 1;
}
curr = curr->next;
}
// Check if the name is the same as the name of the last user
// in the list
if (strcmp(name, curr->name) == 0) {
return 1;
}
// Make the last element equal to our new user
curr->next = new_user;
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T00:40:13.679Z | Question from Code | v1 | should i deallocate memory here or in a function that deletes the user | int create_user(const char *name, User **user_ptr_add) {
// Check if name is too long
if (strlen(name) >= MAX_NAME) {
return 2;
}
// Create a pointer curr to traverse the list
User *curr = *user_ptr_add;
// Create a new user
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("Failed Malloc");
exit(-1);
}
strcpy(new_user->name, name);
new_user->next = NULL;
// If list is empty, make the head pointer equal to our new user
if (*user_ptr_add == NULL) {
*user_ptr_add = new_user;
return 0;
}
// Traverse the list until the end
while (curr->next != NULL) {
// Check if name already exists
if (strcmp(name, curr->name) == 0) {
return 1;
}
curr = curr->next;
}
// Check if the name is the same as the name of the last user
// in the list
if (strcmp(name, curr->name) == 0) {
return 1;
}
// Make the last element equal to our new user
curr->next = new_user;
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T00:44:10.161Z | Question from Code | v1 | on which line do i deallocate memory | int make_post(const User *author, User *target, char *contents) {
// Check if users exist
if (author == NULL || target == NULL) {
return 2;
}
// Create a variable to track if users are friends
int friend_mutuality = 0;
// Iterate through the friendlist of author to check if author
// is friends with target
for(int i = 0; i < MAX_FRIENDS; i++) {
if (author->friends[i] == target) {
// Update the tracker variable
friend_mutuality++;
}
}
// Return 1 if author is not friends with target
if (friend_mutuality == 0) {
return 1;
}
// Make a new post
Post *new_post = malloc(sizeof(Post));
if (new_post == NULL) {
perror("Failed malloc");
exit(-1);
}
// Set the author
strcpy(new_post->author, author->name);
// Set the contents
new_post->contents = contents;
// Set the date
time_t *curr_time = malloc(sizeof(time_t));
if (curr_time == NULL) {
perror("Failed malloc");
exit(-1);
}
time(curr_time) ;
new_post->date = curr_time;
// If target has no posts, set the next post to NULL,
// Otherwise, set the next post to the second most recent post
if (target->first_post == NULL) {
new_post->next = NULL;
} else {
new_post->next = target->first_post;
}
// Set the target's first post equal to newest post
target->first_post = new_post;
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T00:44:41.032Z | Question from Code | v1 | should i deallocate memory in this function | int make_post(const User *author, User *target, char *contents) {
// Check if users exist
if (author == NULL || target == NULL) {
return 2;
}
// Create a variable to track if users are friends
int friend_mutuality = 0;
// Iterate through the friendlist of author to check if author
// is friends with target
for(int i = 0; i < MAX_FRIENDS; i++) {
if (author->friends[i] == target) {
// Update the tracker variable
friend_mutuality++;
}
}
// Return 1 if author is not friends with target
if (friend_mutuality == 0) {
return 1;
}
// Make a new post
Post *new_post = malloc(sizeof(Post));
if (new_post == NULL) {
perror("Failed malloc");
exit(-1);
}
// Set the author
strcpy(new_post->author, author->name);
// Set the contents
new_post->contents = contents;
// Set the date
time_t *curr_time = malloc(sizeof(time_t));
if (curr_time == NULL) {
perror("Failed malloc");
exit(-1);
}
time(curr_time) ;
new_post->date = curr_time;
// If target has no posts, set the next post to NULL,
// Otherwise, set the next post to the second most recent post
if (target->first_post == NULL) {
new_post->next = NULL;
} else {
new_post->next = target->first_post;
}
// Set the target's first post equal to newest post
target->first_post = new_post;
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T00:45:19.422Z | Question from Code | v1 | should i deallocate the memory here or in the function that deletes the user | int make_post(const User *author, User *target, char *contents) {
// Check if users exist
if (author == NULL || target == NULL) {
return 2;
}
// Create a variable to track if users are friends
int friend_mutuality = 0;
// Iterate through the friendlist of author to check if author
// is friends with target
for(int i = 0; i < MAX_FRIENDS; i++) {
if (author->friends[i] == target) {
// Update the tracker variable
friend_mutuality++;
}
}
// Return 1 if author is not friends with target
if (friend_mutuality == 0) {
return 1;
}
// Make a new post
Post *new_post = malloc(sizeof(Post));
if (new_post == NULL) {
perror("Failed malloc");
exit(-1);
}
// Set the author
strcpy(new_post->author, author->name);
// Set the contents
new_post->contents = contents;
// Set the date
time_t *curr_time = malloc(sizeof(time_t));
if (curr_time == NULL) {
perror("Failed malloc");
exit(-1);
}
time(curr_time) ;
new_post->date = curr_time;
// If target has no posts, set the next post to NULL,
// Otherwise, set the next post to the second most recent post
if (target->first_post == NULL) {
new_post->next = NULL;
} else {
new_post->next = target->first_post;
}
// Set the target's first post equal to newest post
target->first_post = new_post;
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T00:48:53.214Z | General Question | v1 | can i have more free calls than free calls in a function if i called malloc without calling free in another function? | null | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T01:29:03.176Z | Question from Code | v1 | does this code properly free all memory from the linked list |
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = NULL;
curr_post = curr_post->next;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T01:29:30.309Z | Question from Code | v1 | how can i make the code free all memory from the linked list |
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = NULL;
curr_post = curr_post->next;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T01:30:17.311Z | Question from Code | v1 | do i need to free next |
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = NULL;
curr_post = curr_post->next;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T01:30:46.349Z | Question from Code | v1 | is this code correct |
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = NULL;
curr_post = curr_post->next;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T01:32:03.779Z | Question from Code | v1 | is this code correct |
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T02:48:53.986Z | Question from Code | v1 | does this code properly remove the user from all friendlists | void remove_friends(const char *name, User **ptr_users) {
// Create a pointer to traverse the list of users
User *curr = *ptr_users;
// Traverse through the list of users
while (curr != NULL) {
// f_count stores the number of friends curr has
// u_count stores 1 + the index the deleted user is found in the friendlist
int f_count = 0;
int u_count = 0;
// Iterate through the friendslist of current user to obtain values of
// f_count and u_count
for (int i = 0; i < MAX_FRIENDS; i++) {
if (curr->friends[i] != NULL) {
f_count++;
if (strcmp(curr->friends[i]->name, name) == 0) {
u_count = f_count;
}
}
}
// Remove the user from the friendlists
// Check if user exists in the friendlist
if (u_count != 0) {
// Check if curr only has one friend
if (f_count == 1) {
curr->friends[0] = NULL;
} else {
// Shift every element in the friendlist after the the deleted user
// to the left by 1
for (int x = u_count - 1; x < f_count - 1; x++) {
curr->friends[x] = curr->friends[x + 1];
}
curr->friends[f_count - 1] = NULL;
}
}
curr = curr->next;
}
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T17:23:08.446Z | Question from Code | v1 | will this code properly remove the friends from the removed user? if not, why? | int delete_user(const char *name, User **user_ptr_del) {
// Delete user from list
// Create a variable to traverse the list
User *curr = *user_ptr_del;
// Create a variable to hold the deleted user
User *temp = malloc(sizeof(User));
// Check for empty users list
if (*user_ptr_del == NULL) {
return 1;
}
// If there is 1 element in the list
if (curr->next == NULL) {
if (strcmp(curr->name, name) == 0) {
temp = curr;
*user_ptr_del = NULL;
free(temp);
return 0;
} else {
return 1;
}
}
// If 1st element is to be removed
if (strcmp(curr->name, name) == 0) {
temp = curr;
*user_ptr_del = curr->next;
remove_friends(name, user_ptr_del);
free(temp);
return 0;
}
// Iterate through the users until the end of the list is reached,
// or until the name of curr matches the name of the deleted user
while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) {
curr = curr->next;
}
// If name does not exist
if (curr->next == NULL) {
free(temp);
return 1;
}
// If user to be removed is the last one
if (curr->next->next == NULL) {
temp = curr->next;
curr->next = NULL;
} else {
// If user to be removed is in the middle of the list
temp = curr->next;
curr->next = curr->next->next;
}
// Remove the user from the friendlists of every user
remove_friends(name, user_ptr_del);
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
}
// Free the memory in temp
if (temp != NULL) {
free(temp);
temp = NULL;
}
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T17:24:45.667Z | Help Fix Code | v1 | null | void remove_friends(const char *name, User **ptr_users) {
// Create a pointer to traverse the list of users
User *curr = *ptr_users;
// Traverse through the list of users
while (curr != NULL) {
// f_count stores the number of friends curr has
// u_count stores 1 + the index the deleted user is found in the friendlist
int f_count = 0;
int u_count = 0;
// Iterate through the friendslist of current user to obtain values of
// f_count and u_count
for (int i = 0; i < MAX_FRIENDS; i++) {
if (curr->friends[i] != NULL) {
f_count++;
if (strcmp(curr->friends[i]->name, name) == 0) {
u_count = f_count;
}
}
}
// Remove the user from the friendlists
// Check if user exists in the friendlist
if (u_count != 0) {
// Check if curr only has one friend
if (f_count == 1) {
curr->friends[0] = NULL;
} else {
// Shift every element in the friendlist after the the deleted user
// to the left by 1
for (int x = u_count - 1; x < f_count - 1; x++) {
curr->friends[x] = curr->friends[x + 1];
}
curr->friends[f_count - 1] = NULL;
}
}
curr = curr->next;
}
}
int delete_user(const char *name, User **user_ptr_del) {
// Delete user from list
// Create a variable to traverse the list
User *curr = *user_ptr_del;
// Create a variable to hold the deleted user
User *temp = malloc(sizeof(User));
// Check for empty users list
if (*user_ptr_del == NULL) {
return 1;
}
// If there is 1 element in the list
if (curr->next == NULL) {
if (strcmp(curr->name, name) == 0) {
temp = curr;
*user_ptr_del = NULL;
free(temp);
return 0;
} else {
return 1;
}
}
// If 1st element is to be removed
if (strcmp(curr->name, name) == 0) {
temp = curr;
*user_ptr_del = curr->next;
remove_friends(name, user_ptr_del);
free(temp);
return 0;
}
// Iterate through the users until the end of the list is reached,
// or until the name of curr matches the name of the deleted user
while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) {
curr = curr->next;
}
// If name does not exist
if (curr->next == NULL) {
free(temp);
return 1;
}
// If user to be removed is the last one
if (curr->next->next == NULL) {
temp = curr->next;
curr->next = NULL;
} else {
// If user to be removed is in the middle of the list
temp = curr->next;
curr->next = curr->next->next;
}
// Remove the user from the friendlists of every user
remove_friends(name, user_ptr_del);
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
}
// Free the memory in temp
if (temp != NULL) {
free(temp);
temp = NULL;
}
return 0;
} | /*
* From the list pointed to by *user_ptr_del, delete the user
* with the given name.
* Remove the deleted user from any lists of friends.
*
* Return:
* - 0 on success.
* - 1 if a user with this name does not exist.
*/ | null |
63c6479f5f004bf4898cf545 | 2023-02-13T20:38:49.352Z | Question from Code | v1 | is memory properly freed here |
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T20:39:43.730Z | Question from Code | v1 | am i traversing the list properly and deleting the memory properly |
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T20:43:01.107Z | General Question | v1 | do we always need the same number of malloc calls as free calls? what if there were multiple return statements in if statements and multiple free cals within if statements? | null | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T20:44:18.725Z | Question from Code | v1 | will this code cause memory leaks? if so, where? | int delete_user(const char *name, User **user_ptr_del) {
// Delete user from list
// Create a variable to traverse the list
User *curr = *user_ptr_del;
// Create a variable to hold the deleted user
User *temp = *user_ptr_del;
// Check for empty users list
if (*user_ptr_del == NULL) {
return 1;
}
remove_friends(name, user_ptr_del);
// If there is 1 element in the list
if (curr->next == NULL) {
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = NULL;
free(temp);
return 0;
} else {
return 1;
}
}
// If 1st element is to be removed
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = curr->next;
remove_friends(name, user_ptr_del);
free(temp);
return 0;
}
// Iterate through the users until the end of the list is reached,
// or until the name of curr matches the name of the deleted user
while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) {
curr = curr->next;
}
// If name does not exist
if (curr->next == NULL) {
return 1;
}
// If user to be removed is the last one
if (curr->next->next == NULL) {
temp = curr->next;
curr->next = NULL;
} else {
// If user to be removed is in the middle of the list
temp = curr->next;
curr->next = curr->next->next;
}
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
}
// Free the memory in temp
if (temp != NULL) {
free(temp);
temp = NULL;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T20:44:51.218Z | Question from Code | v1 | how can i fix the memory leak | int delete_user(const char *name, User **user_ptr_del) {
// Delete user from list
// Create a variable to traverse the list
User *curr = *user_ptr_del;
// Create a variable to hold the deleted user
User *temp = *user_ptr_del;
// Check for empty users list
if (*user_ptr_del == NULL) {
return 1;
}
remove_friends(name, user_ptr_del);
// If there is 1 element in the list
if (curr->next == NULL) {
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = NULL;
free(temp);
return 0;
} else {
return 1;
}
}
// If 1st element is to be removed
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = curr->next;
remove_friends(name, user_ptr_del);
free(temp);
return 0;
}
// Iterate through the users until the end of the list is reached,
// or until the name of curr matches the name of the deleted user
while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) {
curr = curr->next;
}
// If name does not exist
if (curr->next == NULL) {
return 1;
}
// If user to be removed is the last one
if (curr->next->next == NULL) {
temp = curr->next;
curr->next = NULL;
} else {
// If user to be removed is in the middle of the list
temp = curr->next;
curr->next = curr->next->next;
}
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
}
// Free the memory in temp
if (temp != NULL) {
free(temp);
temp = NULL;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T20:45:21.610Z | Question from Code | v1 | am i missing any free(temp) statements? | int delete_user(const char *name, User **user_ptr_del) {
// Delete user from list
// Create a variable to traverse the list
User *curr = *user_ptr_del;
// Create a variable to hold the deleted user
User *temp = *user_ptr_del;
// Check for empty users list
if (*user_ptr_del == NULL) {
return 1;
}
remove_friends(name, user_ptr_del);
// If there is 1 element in the list
if (curr->next == NULL) {
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = NULL;
free(temp);
return 0;
} else {
return 1;
}
}
// If 1st element is to be removed
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = curr->next;
remove_friends(name, user_ptr_del);
free(temp);
return 0;
}
// Iterate through the users until the end of the list is reached,
// or until the name of curr matches the name of the deleted user
while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) {
curr = curr->next;
}
// If name does not exist
if (curr->next == NULL) {
return 1;
}
// If user to be removed is the last one
if (curr->next->next == NULL) {
temp = curr->next;
curr->next = NULL;
} else {
// If user to be removed is in the middle of the list
temp = curr->next;
curr->next = curr->next->next;
}
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
}
// Free the memory in temp
if (temp != NULL) {
free(temp);
temp = NULL;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T20:46:27.728Z | General Question | v1 | if i have a struct and i allocated memory for the struct, will i have to free the memory for that struct only, or every attribute of the struct as well? | null | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T20:47:56.774Z | Question from Code | v1 | where are the memory leaks | int delete_user(const char *name, User **user_ptr_del) {
// Delete user from list
// Create a variable to traverse the list
User *curr = *user_ptr_del;
// Create a variable to hold the deleted user
User *temp = *user_ptr_del;
// Check for empty users list
if (*user_ptr_del == NULL) {
return 1;
}
remove_friends(name, user_ptr_del);
// If there is 1 element in the list
if (curr->next == NULL) {
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = NULL;
free(temp);
return 0;
} else {
return 1;
}
}
// If 1st element is to be removed
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = curr->next;
remove_friends(name, user_ptr_del);
free(temp);
return 0;
}
// Iterate through the users until the end of the list is reached,
// or until the name of curr matches the name of the deleted user
while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) {
curr = curr->next;
}
// If name does not exist
if (curr->next == NULL) {
return 1;
}
// If user to be removed is the last one
if (curr->next->next == NULL) {
temp = curr->next;
curr->next = NULL;
} else {
// If user to be removed is in the middle of the list
temp = curr->next;
curr->next = curr->next->next;
}
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
}
// Free the memory in temp
if (temp != NULL) {
free(temp);
temp = NULL;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T20:48:38.047Z | Question from Code | v1 | are there memory leaks in lines 56-62 | int delete_user(const char *name, User **user_ptr_del) {
// Delete user from list
// Create a variable to traverse the list
User *curr = *user_ptr_del;
// Create a variable to hold the deleted user
User *temp = *user_ptr_del;
// Check for empty users list
if (*user_ptr_del == NULL) {
return 1;
}
remove_friends(name, user_ptr_del);
// If there is 1 element in the list
if (curr->next == NULL) {
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = NULL;
free(temp);
return 0;
} else {
return 1;
}
}
// If 1st element is to be removed
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = curr->next;
remove_friends(name, user_ptr_del);
free(temp);
return 0;
}
// Iterate through the users until the end of the list is reached,
// or until the name of curr matches the name of the deleted user
while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) {
curr = curr->next;
}
// If name does not exist
if (curr->next == NULL) {
return 1;
}
// If user to be removed is the last one
if (curr->next->next == NULL) {
temp = curr->next;
curr->next = NULL;
} else {
// If user to be removed is in the middle of the list
temp = curr->next;
curr->next = curr->next->next;
}
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
}
// Free the memory in temp
if (temp != NULL) {
free(temp);
temp = NULL;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T20:49:04.967Z | Question from Code | v1 | what do i need to change to ensure the memory of the posts are being freed | int delete_user(const char *name, User **user_ptr_del) {
// Delete user from list
// Create a variable to traverse the list
User *curr = *user_ptr_del;
// Create a variable to hold the deleted user
User *temp = *user_ptr_del;
// Check for empty users list
if (*user_ptr_del == NULL) {
return 1;
}
remove_friends(name, user_ptr_del);
// If there is 1 element in the list
if (curr->next == NULL) {
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = NULL;
free(temp);
return 0;
} else {
return 1;
}
}
// If 1st element is to be removed
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = curr->next;
remove_friends(name, user_ptr_del);
free(temp);
return 0;
}
// Iterate through the users until the end of the list is reached,
// or until the name of curr matches the name of the deleted user
while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) {
curr = curr->next;
}
// If name does not exist
if (curr->next == NULL) {
return 1;
}
// If user to be removed is the last one
if (curr->next->next == NULL) {
temp = curr->next;
curr->next = NULL;
} else {
// If user to be removed is in the middle of the list
temp = curr->next;
curr->next = curr->next->next;
}
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
}
// Free the memory in temp
if (temp != NULL) {
free(temp);
temp = NULL;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T20:50:09.546Z | Question from Code | v1 | do lines 56-62 properly free the post memory? if not, how can i fix this? what line do i need to add or change and where? | int delete_user(const char *name, User **user_ptr_del) {
// Delete user from list
// Create a variable to traverse the list
User *curr = *user_ptr_del;
// Create a variable to hold the deleted user
User *temp = *user_ptr_del;
// Check for empty users list
if (*user_ptr_del == NULL) {
return 1;
}
remove_friends(name, user_ptr_del);
// If there is 1 element in the list
if (curr->next == NULL) {
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = NULL;
free(temp);
return 0;
} else {
return 1;
}
}
// If 1st element is to be removed
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = curr->next;
remove_friends(name, user_ptr_del);
free(temp);
return 0;
}
// Iterate through the users until the end of the list is reached,
// or until the name of curr matches the name of the deleted user
while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) {
curr = curr->next;
}
// If name does not exist
if (curr->next == NULL) {
return 1;
}
// If user to be removed is the last one
if (curr->next->next == NULL) {
temp = curr->next;
curr->next = NULL;
} else {
// If user to be removed is in the middle of the list
temp = curr->next;
curr->next = curr->next->next;
}
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
}
// Free the memory in temp
if (temp != NULL) {
free(temp);
temp = NULL;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T20:52:02.123Z | Question from Code | v1 | on which lines do the memory leaks occur? | int delete_user(const char *name, User **user_ptr_del) {
// Delete user from list
// Create a variable to traverse the list
User *curr = *user_ptr_del;
// Create a variable to hold the deleted user
User *temp = *user_ptr_del;
// Check for empty users list
if (*user_ptr_del == NULL) {
return 1;
}
remove_friends(name, user_ptr_del);
// If there is 1 element in the list
if (curr->next == NULL) {
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = NULL;
free(temp);
return 0;
} else {
return 1;
}
}
// If 1st element is to be removed
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = curr->next;
remove_friends(name, user_ptr_del);
free(temp);
return 0;
}
// Iterate through the users until the end of the list is reached,
// or until the name of curr matches the name of the deleted user
while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) {
curr = curr->next;
}
// If name does not exist
if (curr->next == NULL) {
return 1;
}
// If user to be removed is the last one
if (curr->next->next == NULL) {
temp = curr->next;
curr->next = NULL;
} else {
// If user to be removed is in the middle of the list
temp = curr->next;
curr->next = curr->next->next;
}
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
}
// Free the memory in temp
if (temp != NULL) {
free(temp);
temp = NULL;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T20:57:29.969Z | General Question | v1 | how would i free all the memory by traversing through a linked list? what code should i write? | null | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T21:12:00.420Z | Question from Code | v1 | why is there a memory leak after freeing the posts? | int delete_user(const char *name, User **user_ptr_del) {
// Delete user from list
// Create a variable to traverse the list
User *curr = *user_ptr_del;
// Create a variable to hold the deleted user
User *temp = *user_ptr_del;
// Check for empty users list
if (*user_ptr_del == NULL) {
return 1;
}
remove_friends(name, user_ptr_del);
// If there is 1 element in the list
if (curr->next == NULL) {
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = NULL;
free(temp);
return 0;
} else {
return 1;
}
}
// If 1st element is to be removed
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = curr->next;
remove_friends(name, user_ptr_del);
free(temp);
return 0;
}
// Iterate through the users until the end of the list is reached,
// or until the name of curr matches the name of the deleted user
while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) {
curr = curr->next;
}
// If name does not exist
if (curr->next == NULL) {
return 1;
}
// If user to be removed is the last one
if (curr->next->next == NULL) {
temp = curr->next;
curr->next = NULL;
} else {
// If user to be removed is in the middle of the list
temp = curr->next;
curr->next = curr->next->next;
}
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
if (curr_post != NULL) {
curr_post = curr_post->next;
}
}
// Free the memory in temp
if (temp != NULL) {
free(temp);
temp = NULL;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T21:13:30.358Z | Question from Code | v1 | how can i fix the memory leak when freeing the posts | int delete_user(const char *name, User **user_ptr_del) {
// Delete user from list
// Create a variable to traverse the list
User *curr = *user_ptr_del;
// Create a variable to hold the deleted user
User *temp = *user_ptr_del;
// Check for empty users list
if (*user_ptr_del == NULL) {
return 1;
}
remove_friends(name, user_ptr_del);
// If there is 1 element in the list
if (curr->next == NULL) {
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = NULL;
free(temp);
return 0;
} else {
return 1;
}
}
// If 1st element is to be removed
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = curr->next;
remove_friends(name, user_ptr_del);
free(temp);
return 0;
}
// Iterate through the users until the end of the list is reached,
// or until the name of curr matches the name of the deleted user
while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) {
curr = curr->next;
}
// If name does not exist
if (curr->next == NULL) {
return 1;
}
// If user to be removed is the last one
if (curr->next->next == NULL) {
temp = curr->next;
curr->next = NULL;
} else {
// If user to be removed is in the middle of the list
temp = curr->next;
curr->next = curr->next->next;
}
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
if (curr_post != NULL) {
curr_post = curr_post->next;
}
}
// Free the memory in temp
if (temp != NULL) {
free(temp);
temp = NULL;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T21:14:44.502Z | Question from Code | v1 | the memory leak is caused by the if (curr_post != NULL) statement. how can i fix this | int delete_user(const char *name, User **user_ptr_del) {
// Delete user from list
// Create a variable to traverse the list
User *curr = *user_ptr_del;
// Create a variable to hold the deleted user
User *temp = *user_ptr_del;
// Check for empty users list
if (*user_ptr_del == NULL) {
return 1;
}
remove_friends(name, user_ptr_del);
// If there is 1 element in the list
if (curr->next == NULL) {
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = NULL;
free(temp);
return 0;
} else {
return 1;
}
}
// If 1st element is to be removed
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = curr->next;
remove_friends(name, user_ptr_del);
free(temp);
return 0;
}
// Iterate through the users until the end of the list is reached,
// or until the name of curr matches the name of the deleted user
while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) {
curr = curr->next;
}
// If name does not exist
if (curr->next == NULL) {
return 1;
}
// If user to be removed is the last one
if (curr->next->next == NULL) {
temp = curr->next;
curr->next = NULL;
} else {
// If user to be removed is in the middle of the list
temp = curr->next;
curr->next = curr->next->next;
}
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
if (curr_post != NULL) {
curr_post = curr_post->next;
}
}
// Free the memory in temp
if (temp != NULL) {
free(temp);
temp = NULL;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T21:15:19.409Z | Question from Code | v1 | how do i remove the last post of the linked list | int delete_user(const char *name, User **user_ptr_del) {
// Delete user from list
// Create a variable to traverse the list
User *curr = *user_ptr_del;
// Create a variable to hold the deleted user
User *temp = *user_ptr_del;
// Check for empty users list
if (*user_ptr_del == NULL) {
return 1;
}
remove_friends(name, user_ptr_del);
// If there is 1 element in the list
if (curr->next == NULL) {
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = NULL;
free(temp);
return 0;
} else {
return 1;
}
}
// If 1st element is to be removed
if (strcmp(curr->name, name) == 0) {
*user_ptr_del = curr->next;
remove_friends(name, user_ptr_del);
free(temp);
return 0;
}
// Iterate through the users until the end of the list is reached,
// or until the name of curr matches the name of the deleted user
while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) {
curr = curr->next;
}
// If name does not exist
if (curr->next == NULL) {
return 1;
}
// If user to be removed is the last one
if (curr->next->next == NULL) {
temp = curr->next;
curr->next = NULL;
} else {
// If user to be removed is in the middle of the list
temp = curr->next;
curr->next = curr->next->next;
}
Post *curr_post = temp->first_post;
while (curr_post != NULL) {
free(curr_post->date);
free(curr_post);
if (curr_post != NULL) {
curr_post = curr_post->next;
}
}
// Free the memory in temp
if (temp != NULL) {
free(temp);
temp = NULL;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T21:22:19.535Z | Help Fix Code | v1 | null | Post *curr_post = temp->first_post;
if (curr_post->next == NULL) {
free(curr_post->date);
free(curr_post);
} else if (curr_post->next !=NULL) {
while (curr_post->next != NULL) {
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
}
free(curr_post->date);
free(curr_post);
}
| delete all posts and free the memory | null |
63c6479f5f004bf4898cf545 | 2023-02-13T21:25:58.846Z | Help Fix Code | v1 | null | // Free post memory
Post *curr_post = temp->first_post;
Post *temp_post = temp->first_post;
if (curr_post->next == NULL) {
free(curr_post->date);
free(curr_post);
} else if (curr_post->next !=NULL) {
while (curr_post->next != NULL) {
temp_post = curr_post
free(curr_post->date);
free(curr_post);
curr_post = temp_post->next;
}
free(curr_post->date);
free(curr_post);
} | delete all posts and free the memory | null |
63c6479f5f004bf4898cf545 | 2023-02-13T21:26:42.535Z | Help Fix Code | v1 | null | // Free post memory
Post *curr_post = temp->first_post;
Post *temp_post = temp->first_post;
if (curr_post->next == NULL) {
free(curr_post->date);
free(curr_post);
} else if (curr_post->next !=NULL) {
while (curr_post->next != NULL) {
temp_post = curr_post;
free(curr_post->date);
free(curr_post);
curr_post = temp_post->next;
}
free(curr_post->date);
free(curr_post);
} | delete all posts and free the memory | null |
63c6479f5f004bf4898cf545 | 2023-02-13T21:27:33.076Z | Help Fix Code | v1 | null | // Free post memory
Post *curr_post = temp->first_post;
Post *temp_post = curr_post;
if (curr_post->next == NULL) {
free(curr_post->date);
free(curr_post);
} else if (curr_post->next !=NULL) {
while (curr_post->next != NULL) {
temp_post = curr_post;
free(curr_post->date);
free(curr_post);
curr_post = temp_post->next;
}
free(curr_post->date);
free(curr_post);
} | delete all posts and free the memory | null |
63c6479f5f004bf4898cf545 | 2023-02-13T21:29:34.317Z | Question from Code | v1 | how can i fix the segmentation fault | // Free post memory
Post *curr_post = temp->first_post;
Post *temp_post = curr_post;
if (curr_post->next == NULL) {
free(curr_post->date);
free(curr_post);
} else if (curr_post->next !=NULL) {
while (curr_post->next != NULL) {
temp_post = curr_post;
free(curr_post->date);
free(curr_post);
curr_post = temp_post->next;
}
free(curr_post->date);
free(curr_post);
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T21:31:35.043Z | Question from Code | v1 | will this give me a segfault? if so, how can i fix it | // Free post memory
Post *curr_post = temp->first_post;
Post *temp_post = curr_post;
if (curr_post->next == NULL) {
free(curr_post->date);
free(curr_post);
} else if (curr_post->next !=NULL) {
while (curr_post->next != NULL) {
temp_post = curr_post;
free(temp->date);
free(temp);
curr_post = curr_post->next;
}
free(curr_post->date);
free(curr_post);
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T21:33:34.283Z | Question from Code | v1 | how do i fix the segfault | // Free post memory
Post *curr_post = temp->first_post;
Post *temp_post = curr_post;
if (curr_post->next == NULL) {
free(curr_post->date);
free(curr_post);
} else if (curr_post->next !=NULL) {
while (curr_post->next != NULL) {
temp_post = curr_post;
free(temp_post->date);
free(temp_post);
curr_post = curr_post->next;
}
free(curr_post->date);
free(curr_post);
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T21:35:38.496Z | Question from Code | v1 | what causes the segfault | // Free post memory
Post *curr_post = temp->first_post;
Post *temp_post = curr_post;
if (curr_post->next == NULL) {
free(curr_post->date);
free(curr_post);
} else if (curr_post->next !=NULL) {
while (curr_post->next != NULL) {
temp_post = curr_post;
curr_post = curr_post->next;
free(temp_post->date);
free(temp_post);
}
free(curr_post->date);
free(curr_post);
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T21:36:15.116Z | Question from Code | v1 | how do i free the last post | // Free post memory
Post *curr_post = temp->first_post;
Post *temp_post = curr_post;
if (curr_post->next == NULL) {
free(curr_post->date);
free(curr_post);
} else if (curr_post->next !=NULL) {
while (curr_post->next != NULL) {
temp_post = curr_post;
curr_post = curr_post->next;
free(temp_post->date);
free(temp_post);
}
free(curr_post->date);
free(curr_post);
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T21:39:09.738Z | Question from Code | v1 | is this code correct | Post *curr_post = temp->first_post;
Post *temp_post = curr_post;
if (curr_post->next == NULL) {
free(curr_post->date);
free(curr_post);
} else if (curr_post->next !=NULL) {
while (curr_post->next != NULL) {
temp_post = curr_post;
curr_post = curr_post->next;
free(temp_post->date);
free(temp_post);
if (curr_post->next == NULL) {
free(curr_post->date);
free(curr_post);
}
}
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T21:44:15.078Z | Question from Code | v1 | will this code properly remove the last post? | Post *curr_post = temp->first_post;
while (curr_post != NULL) {
Post *temp_post = curr_post;
curr_post = curr_post->next;
free(temp_post->date);
free(temp_post);
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T23:04:06.725Z | Question from Code | v1 | why does this cause a memory leak | User *find_user(const char *name, const User *head) {
// Create a pointer to traverse the list
User *curr = (User *)head;
// Traverse the list until the end
while (curr != NULL) {
// If the name matches the name of the current element, return
// the element
if (strcmp(curr->name, name) == 0) {
return curr;
}
curr = curr->next;
}
// If end of the list is reached and user is not found, return NULL
return NULL;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T23:04:31.244Z | Question from Code | v1 | does anything need to be freed | User *find_user(const char *name, const User *head) {
// Create a pointer to traverse the list
User *curr = (User *)head;
// Traverse the list until the end
while (curr != NULL) {
// If the name matches the name of the current element, return
// the element
if (strcmp(curr->name, name) == 0) {
return curr;
}
curr = curr->next;
}
// If end of the list is reached and user is not found, return NULL
return NULL;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T23:04:50.122Z | Question from Code | v1 | does this cause a memory leak | User *find_user(const char *name, const User *head) {
// Create a pointer to traverse the list
User *curr = (User *)head;
// Traverse the list until the end
while (curr != NULL) {
// If the name matches the name of the current element, return
// the element
if (strcmp(curr->name, name) == 0) {
return curr;
}
curr = curr->next;
}
// If end of the list is reached and user is not found, return NULL
return NULL;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T23:05:15.139Z | Question from Code | v1 | is this correct | User *find_user(const char *name, const User *head) {
// Create a pointer to traverse the list
User *curr = (User *)head;
// Traverse the list until the end
while (curr != NULL) {
// If the name matches the name of the current element, return
// the element
if (strcmp(curr->name, name) == 0) {
return curr;
}
curr = curr->next;
}
// If end of the list is reached and user is not found, return NULL
return NULL;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-13T23:06:27.594Z | Question from Code | v1 | is this correct | int create_user(const char *name, User **user_ptr_add) {
// Check if name is too long
if (strlen(name) >= MAX_NAME) {
return 2;
}
// Create a pointer curr to traverse the list
User *curr = *user_ptr_add;
// Create a new user
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("Failed Malloc");
exit(-1);
}
strcpy(new_user->name, name);
new_user->next = NULL;
// If list is empty, make the head pointer equal to our new user
if (*user_ptr_add == NULL) {
*user_ptr_add = new_user;
return 0;
}
// Traverse the list until the end
while (curr->next != NULL) {
// Check if name already exists
if (strcmp(name, curr->name) == 0) {
return 1;
}
curr = curr->next;
}
// Check if the name is the same as the name of the last user
// in the list
if (strcmp(name, curr->name) == 0) {
return 1;
}
// Make the last element equal to our new user
curr->next = new_user;
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-14T20:18:23.962Z | Question from Code | v1 | will this properly check for a full friendlist | // Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-14T20:19:22.681Z | Question from Code | v1 | will this properly check if at least one of the users have a full friendslist | // Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-14T20:26:28.157Z | Question from Code | v1 | will this code work as intended:
Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies. | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while (f1_added == 0 || f2_added == 0) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-14T20:27:32.209Z | Help Fix Code | v1 | null | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while (f1_added == 0 || f2_added == 0) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies. | null |
63c6479f5f004bf4898cf545 | 2023-02-14T20:57:37.859Z | Question from Code | v1 | why does the first element of friendlist get added to the end of the list when i add the MAX_FRIEND th friend to a friendlist | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while (f1_added == 0 || f2_added == 0) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-14T20:58:27.988Z | Question from Code | v1 | how can i check if the friendlist is already full | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while (f1_added == 0 || f2_added == 0) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-14T20:59:23.036Z | Question from Code | v1 | should i check if the MAX_FRIEND th number or the MAX_FRIEND - 1 th number of the friendlist is null | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while (f1_added == 0 || f2_added == 0) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-14T21:06:33.121Z | Question from Code | v1 | an extra element is added everytime i added the MAX_FRIENDS th friend. do you know why | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while (f1_added == 0 || f2_added == 0) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-14T21:07:09.272Z | Question from Code | v1 | why will The while loop always run at least once, even if f1_added and f2_added are both 1? | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while (f1_added == 0 || f2_added == 0) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-14T21:08:50.320Z | Question from Code | v1 | what do i need to change so that an extra element isnt added everytime i added the MAX_FRIENDS th friend | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while (f1_added == 0 || f2_added == 0) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-14T21:14:40.153Z | Question from Code | v1 | what do i need to change so that an extra element isnt added everytime i added the MAX_FRIENDS th friend? is this code correct? | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while (f1_added == 0 || f2_added == 0) {
if (friend1->friends[count] == NULL && f1_added == 0 && friend1->friends[MAX_FRIENDS - 1] != NULL) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0 && friend2->friends[MAX_FRIENDS - 1] != NULL) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-14T21:25:54.681Z | Question from Code | v1 | does my code properly check for if the friendlist is full, or if the friend already exists | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while (f1_added == 0 || f2_added == 0) {
if (friend1->friends[count] == NULL && f1_added == 0 && friend1->friends[MAX_FRIENDS - 1] != NULL) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0 && friend2->friends[MAX_FRIENDS - 1] != NULL) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-14T21:30:27.712Z | Question from Code | v1 | is it possible for a friend to be added to the same list twice in my code | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while (f1_added == 0 || f2_added == 0) {
if (friend1->friends[count] == NULL && f1_added == 0 && friend1->friends[MAX_FRIENDS - 1] != NULL) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0 && friend2->friends[MAX_FRIENDS - 1] != NULL) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T00:42:43.637Z | Question from Code | v1 | is this code correct? if not what do i need to to do to fix it
| int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while (f1_added == 0 || f2_added == 0) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T00:44:00.999Z | Question from Code | v1 | when i run the program, when i add the MAX_FRIEND th name, the first element is added as the MAX_FRIEND th + 1 element. is this function causing that kind of error? | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while (f1_added == 0 || f2_added == 0) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T00:45:18.551Z | Question from Code | v1 | when i run the program, when i add the MAX_FRIEND th name, the first element is added as the MAX_FRIEND th + 1 element. why might the find_user function cause this? | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while (f1_added == 0 || f2_added == 0) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T00:46:37.032Z | Question from Code | v1 | when i run the program, when i add the MAX_FRIEND th name, the first element is added as the MAX_FRIEND th + 1 element. is the find_user function causing this? how can i fix it | User *find_user(const char *name, const User *head) {
// Create a pointer to traverse the list
User *curr = (User *)head;
while (curr != NULL) { // Traverse the list
if (strcmp(curr->name, name) == 0) { // Return the user if found
return curr;
}
curr = curr->next;
}
return NULL; // If end of the list is reached and user is not found, return NULL
}
int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while (f1_added == 0 || f2_added == 0) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T00:50:48.955Z | Question from Code | v1 | when i run the program, when i add the MAX_FRIEND th name, the first element is added as the MAX_FRIEND th + 1 element. is the find_user function causing this? how can i fix it | User *find_user(const char *name, const User *head) {
// Create a pointer to traverse the list
User *curr = (User *)head;
while (curr != NULL) { // Traverse the list
if (strcmp(curr->name, name) == 0) { // Return the user if found
return curr;
}
curr = curr->next;
}
return NULL; // If end of the list is reached and user is not found, return NULL
}
int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T00:52:50.019Z | Question from Code | v1 | when i run the program, when i add the MAX_FRIEND th name, the first element is re-added as the MAX_FRIEND th + 1 element. how can i fix it? Doesnt the f1_added and f2_added check prevent this extra addition? | User *find_user(const char *name, const User *head) {
// Create a pointer to traverse the list
User *curr = (User *)head;
while (curr != NULL) { // Traverse the list
if (strcmp(curr->name, name) == 0) { // Return the user if found
return curr;
}
curr = curr->next;
}
return NULL; // If end of the list is reached and user is not found, return NULL
}
int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T00:54:05.474Z | Question from Code | v1 | why does this program work fine until i add the last element that friendlist is able to contain? | User *find_user(const char *name, const User *head) {
// Create a pointer to traverse the list
User *curr = (User *)head;
while (curr != NULL) { // Traverse the list
if (strcmp(curr->name, name) == 0) { // Return the user if found
return curr;
}
curr = curr->next;
}
return NULL; // If end of the list is reached and user is not found, return NULL
}
int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T00:54:48.915Z | Question from Code | v1 | why does this program work fine until i add the last element that friendlist is able to contain, and then for some reason the first element is added to the end of the list? | User *find_user(const char *name, const User *head) {
// Create a pointer to traverse the list
User *curr = (User *)head;
while (curr != NULL) { // Traverse the list
if (strcmp(curr->name, name) == 0) { // Return the user if found
return curr;
}
curr = curr->next;
}
return NULL; // If end of the list is reached and user is not found, return NULL
}
int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) {
return 1;
}
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
count++;
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T00:58:35.272Z | Question from Code | v1 | is this code correct? |
// Keep looping so long as a new friend is not added to both users
while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T00:59:33.052Z | Question from Code | v1 | could this code be the reason why the first element of the list is added to the end of the list when the MAX_FRIEND th element is added to a list? |
// Keep looping so long as a new friend is not added to both users
while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T01:00:04.560Z | Question from Code | v1 | could this code be the reason why the first element of the list is added to the end of the list when the MAX_FRIEND th element is added to a list?
what do i need to add to fix it |
// Keep looping so long as a new friend is not added to both users
while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) {
if (friend1->friends[count] == NULL && f1_added == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
if (friend2->friends[count] == NULL && f2_added == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T01:19:06.077Z | Question from Code | v1 | i dont understand why when i add the MAX_FRIENDS th friend, the first element is added to the end of the list (so it appears twice) | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
if (check_if_not_friends(friend1, friend2) == 1) {
return 1;
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) {
if (friend1->friends[count] == NULL && f1_added == 0) {
if (check_if_not_friends(friend1, friend2) == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
}
if (friend2->friends[count] == NULL && f2_added == 0) {
if (check_if_not_friends(friend2, friend1)) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
}
count++;
}
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T01:20:11.285Z | Help Fix Code | v1 | null | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
if (check_if_not_friends(friend1, friend2) == 1) {
return 1;
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) {
if (friend1->friends[count] == NULL && f1_added == 0) {
if (check_if_not_friends(friend1, friend2) == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
}
if (friend2->friends[count] == NULL && f2_added == 0) {
if (check_if_not_friends(friend2, friend1)) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
}
count++;
}
return 0;
} | Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies. | null |
63c6479f5f004bf4898cf545 | 2023-02-15T01:21:32.088Z | Question from Code | v1 | will this code always work as inteneded?
Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies. | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
if (check_if_not_friends(friend1, friend2) == 1) {
return 1;
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) {
if (friend1->friends[count] == NULL && f1_added == 0) {
if (check_if_not_friends(friend1, friend2) == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
}
if (friend2->friends[count] == NULL && f2_added == 0) {
if (check_if_not_friends(friend2, friend1)) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
}
count++;
}
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T01:22:22.152Z | Question from Code | v1 | will this code always work as inteneded? Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. |
/*
* Helper function that determines if users are already friends
*/
int check_if_not_friends(User *user1, User *user2) {
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user1->friends[i] == user2) {
return 1; // If user already exists
}
}
return 0; // If user does not exist in friendlist
}
int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
if (check_if_not_friends(friend1, friend2) == 1) {
return 1;
}
// Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) {
if (friend1->friends[count] == NULL && f1_added == 0) {
if (check_if_not_friends(friend1, friend2) == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
}
if (friend2->friends[count] == NULL && f2_added == 0) {
if (check_if_not_friends(friend2, friend1)) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
}
count++;
}
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T01:45:02.870Z | Question from Code | v1 | what is a better more correct way to do this? | // Create variables to track if the friends were added to each other's
// friend lists
int f1_added = 0;
int f2_added = 0;
// Create a counter to track list index
int count = 0;
// Keep looping so long as a new friend is not added to both users
while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) {
if (friend1->friends[count] == NULL && f1_added == 0) {
if (check_if_not_friends(friend1, friend2) == 0) {
friend1->friends[count] = friend2;
f1_added = 1; // Track that friend2 was added to friend1's friendlist
}
}
if (friend2->friends[count] == NULL && f2_added == 0) {
if (check_if_not_friends(friend2, friend1) == 0) {
friend2->friends[count] = friend1;
f2_added = 1; // Track that friend 1 was added to friend2's friendlist
}
}
count++;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T01:55:55.523Z | Question from Code | v1 | is this code correct | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
if (check_if_not_friends(friend1, friend2) == 1) {
return 1;
}
// Set the first NULL element to friend2
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == NULL) {
friend1->friends[i] = friend2;
i = MAX_FRIENDS; // Update i to exit the loop
}
}
// Set the first NULL element to friend1
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend2->friends[i] == NULL) {
friend2->friends[i] = friend1;
i = MAX_FRIENDS; // Update i to exit the loop
}
}
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T01:56:40.076Z | Question from Code | v1 | is this correct | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
if (check_if_not_friends(friend1, friend2) == 1) {
return 1;
}
// Set the first NULL element to friend2
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == NULL) {
friend1->friends[i] = friend2;
i = MAX_FRIENDS; // Update i to exit the loop
}
}
// Set the first NULL element to friend1
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend2->friends[i] == NULL) {
friend2->friends[i] = friend1;
i = MAX_FRIENDS; // Update i to exit the loop
}
}
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T02:00:08.246Z | Question from Code | v1 | is this code correct | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) >= MAX_NAME) { // Check if name is too long
return 2;
}
User *curr = *user_ptr_add; // Create a pointer curr to traverse the list
// Create a new user
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("malloc");
exit(-1);
}
strcpy(new_user->name, name);
new_user->next = NULL;
// If list is empty, make the head pointer equal to our new user
if (*user_ptr_add == NULL) {
*user_ptr_add = new_user;
return 0;
}
while (curr->next != NULL) { // Traverse the list
if (strcmp(name, curr->name) == 0) { // Check if name already exists
return 1;
}
curr = curr->next;
}
if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name
return 1;
}
curr->next = new_user; // Make the last element equal to our new user
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T02:00:45.908Z | Question from Code | v1 | will this code edd a user if it is the only one in the list | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) >= MAX_NAME) { // Check if name is too long
return 2;
}
User *curr = *user_ptr_add; // Create a pointer curr to traverse the list
// Create a new user
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("malloc");
exit(-1);
}
strcpy(new_user->name, name);
new_user->next = NULL;
// If list is empty, make the head pointer equal to our new user
if (*user_ptr_add == NULL) {
*user_ptr_add = new_user;
return 0;
}
while (curr->next != NULL) { // Traverse the list
if (strcmp(name, curr->name) == 0) { // Check if name already exists
return 1;
}
curr = curr->next;
}
if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name
return 1;
}
curr->next = new_user; // Make the last element equal to our new user
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T02:01:24.473Z | Question from Code | v1 | are both these functions correct | /*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
// Create a pointer to traverse the list
User *curr = (User *)head;
while (curr != NULL) { // Traverse the list
if (strcmp(curr->name, name) == 0) { // Return the user if found
return curr;
}
curr = curr->next;
}
return NULL; // If end of the list is reached and user is not found, return NULL
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n"); // Print the header
User *curr_user = (User *)curr; // Create a pointer to traverse the list
while (curr_user != NULL) { // Traverse the list
printf("\t%s\n", curr_user->name); // Print the names
curr_user = curr_user->next;
}
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T02:04:20.287Z | General Question | v1 | I have a program that allows you to create users and make them friends with each other. There is a maximum number of friends allowed for each user. when i make two users friends and one of the friend's friendlist is now full, for some reason, in addition to the added friend being added to the friendlist, the first friend that was added to their friendlist is duplicated and added to the very end of the list. what is causing this? | null | null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T02:11:10.904Z | Question from Code | v1 | I have a program that allows you to create users and make them friends with each other. There is a maximum number of friends allowed for each user. when i make two users friends and one of the friend's friendlist is now full, for some reason, in addition to the added friend being added to the friendlist, the first friend that was added to their friendlist is duplicated and added to the very end of the list. what is causing this? | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
if (check_if_not_friends(friend1, friend2) == 1) {
return 1;
}
// Set the first NULL element to friend2
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == NULL) {
friend1->friends[i] = friend2;
i = MAX_FRIENDS; // Update i to exit the loop
}
}
// Set the first NULL element to friend1
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend2->friends[i] == NULL) {
friend2->friends[i] = friend1;
i = MAX_FRIENDS; // Update i to exit the loop
}
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T02:12:03.235Z | Question from Code | v1 | why is this the case?: If friend1 is added to friend2's friendlist and friend2 is added to friend1's friendlist, then friend1 is added to friend1's friendlist. | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
if (check_if_not_friends(friend1, friend2) == 1) {
return 1;
}
// Set the first NULL element to friend2
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == NULL) {
friend1->friends[i] = friend2;
i = MAX_FRIENDS; // Update i to exit the loop
}
}
// Set the first NULL element to friend1
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend2->friends[i] == NULL) {
friend2->friends[i] = friend1;
i = MAX_FRIENDS; // Update i to exit the loop
}
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T02:13:06.300Z | Question from Code | v1 | in which cases will this code be wrong? | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
if (check_if_not_friends(friend1, friend2) == 1) {
return 1;
}
// Set the first NULL element to friend2
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == NULL) {
friend1->friends[i] = friend2;
i = MAX_FRIENDS; // Update i to exit the loop
}
}
// Set the first NULL element to friend1
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend2->friends[i] == NULL) {
friend2->friends[i] = friend1;
i = MAX_FRIENDS; // Update i to exit the loop
}
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T02:14:11.165Z | Question from Code | v1 | when the MAX_FRIEND th element is added, the first element of the friendlist is added too. why are two friends being added when the last element of friendlist is being set? | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
if (check_if_not_friends(friend1, friend2) == 1) {
return 1;
}
// Set the first NULL element to friend2
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == NULL) {
friend1->friends[i] = friend2;
i = MAX_FRIENDS; // Update i to exit the loop
}
}
// Set the first NULL element to friend1
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend2->friends[i] == NULL) {
friend2->friends[i] = friend1;
i = MAX_FRIENDS; // Update i to exit the loop
}
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T02:22:10.864Z | Question from Code | v1 | is it possible that this function could add 2 elements to the array of friends instead of 1? when does this happen? how can i prevent this | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
if (check_if_not_friends(friend1, friend2) == 1) {
return 1;
}
// Set the first NULL element to friend2
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == NULL) {
friend1->friends[i] = friend2;
i = MAX_FRIENDS; // Update i to exit the loop
}
}
// Set the first NULL element to friend1
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend2->friends[i] == NULL) {
friend2->friends[i] = friend1;
i = MAX_FRIENDS; // Update i to exit the loop
}
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T02:26:03.077Z | Question from Code | v1 | why is it that friend1->friends[i]->name is null when the code runs | int make_friends(const char *name1, const char *name2, User *head) {
// Create users friend1 and friend2
User *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
// Check for user non-existence
if (friend1 == NULL || friend2 == NULL) {
return 4;
}
// Check for same user passed twice
if (friend1 == friend2) {
return 3;
}
// Check for full friends list
if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
return 2;
}
// Check for friend already exists
if (check_if_not_friends(friend1, friend2) == 1) {
return 1;
}
// Set the first NULL element to friend2
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == NULL) {
friend1->friends[i] = friend2;
i = MAX_FRIENDS; // Update i to exit the loop
}
}
// Set the first NULL element to friend1
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend2->friends[i] == NULL) {
friend2->friends[i] = friend1;
i = MAX_FRIENDS; // Update i to exit the loop
}
}
return 0;
}
| null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T02:40:12.830Z | Question from Code | v1 | is a user struct properly being initialized here? | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) >= MAX_NAME) { // Check if name is too long
return 2;
}
User *curr = *user_ptr_add; // Create a pointer curr to traverse the list
// Create a new user
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("malloc");
exit(-1);
}
// Initialize attributes
strcpy(new_user->name, name);
new_user->next = NULL;
// If list is empty, make the head pointer equal to our new user
if (*user_ptr_add == NULL) {
*user_ptr_add = new_user;
return 0;
}
while (curr->next != NULL) { // Traverse the list
if (strcmp(name, curr->name) == 0) { // Check if name already exists
return 1;
}
curr = curr->next;
}
if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name
return 1;
}
curr->next = new_user; // Make the last element equal to our new user
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T02:40:37.992Z | Question from Code | v1 | ais a user struct and its attributes properly being initialized here? | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) >= MAX_NAME) { // Check if name is too long
return 2;
}
User *curr = *user_ptr_add; // Create a pointer curr to traverse the list
// Create a new user
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("malloc");
exit(-1);
}
// Initialize attributes
strcpy(new_user->name, name);
new_user->next = NULL;
// If list is empty, make the head pointer equal to our new user
if (*user_ptr_add == NULL) {
*user_ptr_add = new_user;
return 0;
}
while (curr->next != NULL) { // Traverse the list
if (strcmp(name, curr->name) == 0) { // Check if name already exists
return 1;
}
curr = curr->next;
}
if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name
return 1;
}
curr->next = new_user; // Make the last element equal to our new user
return 0;
} | null | null |
63c6479f5f004bf4898cf545 | 2023-02-15T03:16:54.057Z | Question from Code | v1 | is space properly being allocated | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) >= MAX_NAME) { // Check if name is too long
return 2;
}
User *curr = *user_ptr_add; // Create a pointer curr to traverse the list
// Create a new user
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("malloc");
exit(-1);
}
strcpy(new_user->name, name);
new_user->next = NULL;
// If list is empty, make the head pointer equal to our new user
if (*user_ptr_add == NULL) {
*user_ptr_add = new_user;
return 0;
}
while (curr->next != NULL) { // Traverse the list
if (strcmp(name, curr->name) == 0) { // Check if name already exists
return 1;
}
curr = curr->next;
}
if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name
return 1;
}
curr->next = new_user; // Make the last element equal to our new user
return 0;
} | null | null |