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
63c6485f5f004bf4898cfbe4
2023-02-25T00:36:29.452Z
Question from Code
v1
whats the code to initialize cur_rule to NULL before the first iteration of the loop.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "pmake.h" /* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { char line[MAXLINE]; Rule *rules = NULL; Rule *cur_rule = NULL; Action *cur_action = NULL; Dependency *cur_dep = NULL; int line_num = 0; while (fgets(line, MAXLINE, fp) != NULL) { line_num++; if (is_comment_or_empty(line)) { continue; } // Remove trailing newline line[strlen(line) - 1] = '\0'; // Check if line is a target if (line[0] == '\t') { // If no rule has been created yet, this is an error if (cur_rule == NULL) { fprintf(stderr, "ERROR: action line before target line at line %d", line_num); exit(1); // If no action has been created yet, create one } else if (cur_action == NULL) { cur_action = malloc(sizeof(Action)); cur_action->args = parse_args(line); cur_action->next_act = NULL; cur_rule->actions = cur_action; // Otherwise, add to the end of the list of actions } else { cur_action->next_act = malloc(sizeof(Action)); cur_action = cur_action->next_act; cur_action->args = parse_args(line); cur_action->next_act = NULL; } // Otherwise, line is a target } else { // If a rule has already been created, add it to the list of rules if (cur_rule != NULL) { cur_rule->next_rule = malloc(sizeof(Rule)); cur_rule = cur_rule->next_rule; // Otherwise, create the first rule } else { cur_rule = malloc(sizeof(Rule)); rules = cur_rule; } // Parse the target cur_rule->target = parse_target(line); cur_rule->dependencies = NULL; cur_rule->actions = NULL; cur_rule->next_rule = NULL; // Parse the dependencies char *dep_line = parse_dependencies(line); if (dep_line != NULL) { cur_dep = malloc(sizeof(Dependency)); cur_dep->rule = find_rule(rules, dep_line); cur_dep->next_dep = NULL; cur_rule->dependencies = cur_dep; while ((dep_line = parse_dependencies(NULL)) != NULL) { cur_dep->next_dep = malloc(sizeof(Dependency)); cur_dep = cur_dep->next_dep; cur_dep->rule = find_rule(rules, dep_line); cur_dep->next_dep = NULL; } // If there are no dependencies, set cur_dep to NULL } else { cur_dep = NULL; } // Set cur_action to NULL cur_action = NULL; } } return rules; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-25T00:40:08.694Z
Question from Code
v1
is there a reason i am getting a seg fault here or no?
/* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { char line[MAXLINE]; Rule *rules = NULL; Rule *cur_rule = NULL; Action *cur_action = NULL; Dependency *cur_dep = NULL; int line_num = 0;
null
null
63c6485f5f004bf4898cfbe4
2023-03-04T00:00:40.094Z
Question from Code
v1
the problem is this is how the Makefile prints: main : linked_list.o main.o : gcc -Wall -g -std=gnu99 -o main linked_list.o main.o as you can see, there is a line below the target line with a colon that is not supposed to be there. What is causing this in my code?
/* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { char line[MAXLINE]; Rule *head = NULL, *current = NULL; while (fgets(line, MAXLINE, fp)) { if (strlen(line) == MAXLINE - 1 && line[MAXLINE - 2] != '\n') { fprintf(stderr, "ERROR: Line too long\n"); exit(1); } // Ignore empty lines and comments if (line[0] == '\n' || line[0] == '#') { continue; } // If the line starts with a tab, it's an action line if (line[0] == '\t') { if (current == NULL) { fprintf(stderr, "ERROR: Action line without target\n"); exit(1); } Action *act = (Action *) malloc(sizeof(Action)); act->args = (char **) malloc(sizeof(char *) * MAXLINE); // Parse the command into an array of strings char *arg = strtok(line, " \t\n"); int i = 0; while (arg != NULL) { act->args[i] = strdup(arg); i++; if (i >= MAXLINE - 1) { fprintf(stderr, "ERROR: Too many arguments in action line\n"); exit(1); } arg = strtok(NULL, " \t\n"); } act->args[i] = NULL; act->next_act = NULL; // Attach the action to the current rule if (current->actions == NULL) { current->actions = act; } else { Action *act_curr = current->actions; while (act_curr->next_act != NULL) { act_curr = act_curr->next_act; } act_curr->next_act = act; } } else { // It's a target line if (current != NULL && current->target == NULL) { fprintf(stderr, "ERROR: Empty target\n"); exit(1); } else { // Parse the target name and create a new Rule node for this target Rule *rule = (Rule *) malloc(sizeof(Rule)); rule->target = strdup(&line[strspn(line, " \t")]); // Initialize the list of dependencies for this rule rule->dependencies = NULL; // Process the dependencies char *dep_str = strtok(NULL, " \t\n"); while (dep_str != NULL) { // Skip the target name if it's the first token returned by strtok if (strcmp(dep_str, rule->target) == 0) { dep_str = strtok(NULL, " \t\n"); continue; } // Create a new dependency node Dependency *dep = (Dependency *) malloc(sizeof(Dependency)); dep->rule = (Rule *) malloc(sizeof(Rule)); dep->rule->target = strdup(dep_str); dep->next_dep = NULL; // Attach the dependency to the current rule if (rule->dependencies == NULL) { rule->dependencies = dep; } else { Dependency *dep_curr = rule->dependencies; while (dep_curr->next_dep != NULL) { dep_curr = dep_curr->next_dep; } dep_curr->next_dep = dep; } // Read the next token from the current line dep_str = strtok(NULL, " \t\n"); } // Attach the rule to the list of rules rule->actions = NULL; rule->next_rule = NULL; if (head == NULL) { head = rule; } else { current->next_rule = rule; } current = rule; } } } return head; }
null
null
63c6485f5f004bf4898cfbe4
2023-03-04T00:02:16.700Z
Help Fix Code
v1
null
/* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { char line[MAXLINE]; Rule *head = NULL, *current = NULL; while (fgets(line, MAXLINE, fp)) { if (strlen(line) == MAXLINE - 1 && line[MAXLINE - 2] != '\n') { fprintf(stderr, "ERROR: Line too long\n"); exit(1); } // Ignore empty lines and comments if (line[0] == '\n' || line[0] == '#') { continue; } // If the line starts with a tab, it's an action line if (line[0] == '\t') { if (current == NULL) { fprintf(stderr, "ERROR: Action line without target\n"); exit(1); } Action *act = (Action *) malloc(sizeof(Action)); act->args = (char **) malloc(sizeof(char *) * MAXLINE); // Parse the command into an array of strings char *arg = strtok(line, " \t\n"); int i = 0; while (arg != NULL) { act->args[i] = strdup(arg); i++; if (i >= MAXLINE - 1) { fprintf(stderr, "ERROR: Too many arguments in action line\n"); exit(1); } arg = strtok(NULL, " \t\n"); } act->args[i] = NULL; act->next_act = NULL; // Attach the action to the current rule if (current->actions == NULL) { current->actions = act; } else { Action *act_curr = current->actions; while (act_curr->next_act != NULL) { act_curr = act_curr->next_act; } act_curr->next_act = act; } } else { // It's a target line if (current != NULL && current->target == NULL) { fprintf(stderr, "ERROR: Empty target\n"); exit(1); } else { // Parse the target name and create a new Rule node for this target Rule *rule = (Rule *) malloc(sizeof(Rule)); rule->target = strdup(&line[strspn(line, " \t")]); // Initialize the list of dependencies for this rule rule->dependencies = NULL; // Process the dependencies char *dep_str = strtok(NULL, " \t\n"); while (dep_str != NULL) { // Skip the target name if it's the first token returned by strtok if (strcmp(dep_str, rule->target) == 0) { dep_str = strtok(NULL, " \t\n"); continue; } // Create a new dependency node Dependency *dep = (Dependency *) malloc(sizeof(Dependency)); dep->rule = (Rule *) malloc(sizeof(Rule)); dep->rule->target = strdup(dep_str); dep->next_dep = NULL; // Attach the dependency to the current rule if (rule->dependencies == NULL) { rule->dependencies = dep; } else { Dependency *dep_curr = rule->dependencies; while (dep_curr->next_dep != NULL) { dep_curr = dep_curr->next_dep; } dep_curr->next_dep = dep; } // Read the next token from the current line dep_str = strtok(NULL, " \t\n"); } // Attach the rule to the list of rules rule->actions = NULL; rule->next_rule = NULL; if (head == NULL) { head = rule; } else { current->next_rule = rule; } current = rule; } } } return head; }
the problem is this is how the Makefile prints: main : linked_list.o main.o : gcc -Wall -g -std=gnu99 -o main linked_list.o main.o as you can see, there is a line below the target line with a colon that is not supposed to be there. What is causing this in my code?
null
63c6485f5f004bf4898cfbe4
2023-03-04T00:03:44.124Z
Question from Code
v1
the problem is this is how the Makefile prints: main : linked_list.o main.o : gcc -Wall -g -std=gnu99 -o main linked_list.o main.o as you can see, there is a line below the target line with a colon that is not supposed to be there. What is causing this in my code?
/* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { char line[MAXLINE]; Rule *head = NULL, *current = NULL; while (fgets(line, MAXLINE, fp)) { if (strlen(line) == MAXLINE - 1 && line[MAXLINE - 2] != '\n') { fprintf(stderr, "ERROR: Line too long\n"); exit(1); } // Ignore empty lines and comments if (line[0] == '\n' || line[0] == '#') { continue; } // If the line starts with a tab, it's an action line if (line[0] == '\t') { if (current == NULL) { fprintf(stderr, "ERROR: Action line without target\n"); exit(1); } Action *act = (Action *) malloc(sizeof(Action)); act->args = (char **) malloc(sizeof(char *) * MAXLINE); // Parse the command into an array of strings char *arg = strtok(line, " \t\n"); int i = 0; while (arg != NULL) { act->args[i] = strdup(arg); i++; if (i >= MAXLINE - 1) { fprintf(stderr, "ERROR: Too many arguments in action line\n"); exit(1); } arg = strtok(NULL, " \t\n"); } act->args[i] = NULL; act->next_act = NULL; // Attach the action to the current rule if (current->actions == NULL) { current->actions = act; } else { Action *act_curr = current->actions; while (act_curr->next_act != NULL) { act_curr = act_curr->next_act; } act_curr->next_act = act; } } else { // It's a target line if (current != NULL && current->target == NULL) { fprintf(stderr, "ERROR: Empty target\n"); exit(1); } else { // Parse the target name and create a new Rule node for this target Rule *rule = (Rule *) malloc(sizeof(Rule)); rule->target = strdup(&line[strspn(line, " \t")]); // Initialize the list of dependencies for this rule rule->dependencies = NULL; // Process the dependencies char *dep_str = strtok(NULL, " \t\n"); while (dep_str != NULL) { // Skip the target name if it's the first token returned by strtok if (strcmp(dep_str, rule->target) == 0) { dep_str = strtok(NULL, " \t\n"); continue; } // Create a new dependency node Dependency *dep = (Dependency *) malloc(sizeof(Dependency)); dep->rule = (Rule *) malloc(sizeof(Rule)); dep->rule->target = strdup(dep_str); dep->next_dep = NULL; // Attach the dependency to the current rule if (rule->dependencies == NULL) { rule->dependencies = dep; } else { Dependency *dep_curr = rule->dependencies; while (dep_curr->next_dep != NULL) { dep_curr = dep_curr->next_dep; } dep_curr->next_dep = dep; } // Read the next token from the current line dep_str = strtok(NULL, " \t\n"); } // Attach the rule to the list of rules rule->actions = NULL; rule->next_rule = NULL; if (head == NULL) { head = rule; } else { current->next_rule = rule; } current = rule; } } } return head; }
null
null
63c6485f5f004bf4898cfbe4
2023-03-12T21:39:07.762Z
Question from Code
v2
for some reason, when I find the matching targets, the action list is null but the other attributes are not.
void execute_actions(Rule *rule); Rule *find_rule_make(char *target, Rule *rules); void run_make(char *target, Rule *rules, int pflag) { Rule *rule = NULL; if (target == NULL) { rule = rules; } else { rule = find_rule_make(target, rules); } printf("%s\n", rule->target); Dependency *dep = rule->dependencies; while (dep != NULL) { if (pflag) { int pid = fork(); if (pid == -1) { perror("fork"); exit(1); } else if (pid == 0) { run_make(dep->rule->target, rules, 0); exit(0); } } else { run_make(dep->rule->target, rules, 0); } dep = dep->next_dep; } while (pflag && wait(NULL) != -1) ; struct stat target_stat; struct stat dep_stat; if (stat(rule->target, &target_stat) == -1) { execute_actions(rule); return; } dep = rule->dependencies; while (dep != NULL) { if (stat(dep->rule->target, &dep_stat) == -1 || target_stat.st_mtim.tv_sec < dep_stat.st_mtim.tv_sec || (target_stat.st_mtim.tv_sec == dep_stat.st_mtim.tv_sec && target_stat.st_mtim.tv_nsec < dep_stat.st_mtim.tv_nsec)) { execute_actions(rule); return; } dep = dep->next_dep; } execute_actions(rule); } Rule *find_rule_make(char *target, Rule *rules) { Rule *cur_rule = rules; while (cur_rule != NULL) { if (strcmp(cur_rule->target, target) == 0) { return cur_rule; } cur_rule = cur_rule->next_rule; } return NULL; }
null
null
63c6485f5f004bf4898cfbe4
2023-03-13T21:48:23.669Z
Question from Code
v2
What does the program print to stderr if the user presses Ctrl+C at the moment when the program is at position B?
int x = 5; void handler(int sig) { x += 3; fprintf(stderr, "inside %d ", x); } int main() { fprintf(stderr, "start "); struct sigaction act; act.sa_handler = handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, NULL); x += 2; fprintf(stderr, "outside %d", x); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-03-13T21:49:49.388Z
Question from Code
v2
What does the program print to stderr if the user presses Ctrl+C at the moment when the program is at position C?
int x = 5; void handler(int sig) { x += 3; fprintf(stderr, "inside %d ", x); } int main() { fprintf(stderr, "start "); struct sigaction act; act.sa_handler = handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, NULL); x += 2; fprintf(stderr, "outside %d", x); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-03-25T14:11:24.326Z
General Question
v2
Variable x has been assigned a digit between 1 and 10. Write a shell command to calculate x minus 1 and display the result to standard output.
null
null
null
63c6485f5f004bf4898cfbe4
2023-04-05T19:49:58.815Z
Help Fix Code
v2
the code is not supposed to block waiting for input from a particular client or the listening socket, but when i run it, it waits for the first client to enter a username before accepting the second client.
int main() { initialize_socket(); printf("Server listening on port %d\n", PORT); User *user_list = NULL; Client *actives = NULL; int max_fd = listen_soc; printf("list soc max_fd %d", max_fd); fd_set all_fds; FD_ZERO(&all_fds); FD_SET(listen_soc, &all_fds); while (1) { fd_set listen_fds = all_fds; int nready = select(max_fd + 1, &listen_fds, NULL, NULL, NULL); if (nready == -1) { perror("server: select"); exit(1); } if (FD_ISSET(listen_soc, &listen_fds)) { struct sockaddr_in client_address; socklen_t client_address_len = sizeof(client_address); int client_fd = accept(listen_soc, (struct sockaddr *)&client_address, &client_address_len); if (client_fd < 0) { perror("accept"); continue; } // Add the new client's socket to the file descriptor set for select FD_SET(client_fd, &all_fds); if (client_fd > max_fd) { max_fd = client_fd; } printf("updated max_fd %d", max_fd); printf("Accepted connection\n"); // Prompt the client for their name char name_buffer[BUFFER_SIZE + 1]; check_user(name_buffer, client_fd, user_list); // Create a new user with the given name create_user(name_buffer, &user_list); User *new_user = find_user(name_buffer, user_list); // create new client and add to list of active clients Client *new_client = malloc(sizeof(Client)); new_client->fd = client_fd; new_client->name = new_user->name; new_client->input_len = 0; new_client->next = actives; actives = new_client; char welcome_message[BUFFER_SIZE]; snprintf(welcome_message, BUFFER_SIZE, "Welcome, %s! Go ahead and enter user commands>\r\n", new_user->name); write(client_fd, welcome_message, strlen(welcome_message)); } for (Client *client = actives; client != NULL; client = client->next) { if (FD_ISSET(client->fd, &listen_fds)) { ... } } } } return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-04-05T19:52:41.469Z
Help Fix Code
v2
the code is not supposed to block waiting for input from a particular client or the listening socket, but when i run it, it waits for the first client to enter a username before accepting the second client.
int main() { initialize_socket(); printf("Server listening on port %d\n", PORT); User *user_list = NULL; Client *actives = NULL; int max_fd = listen_soc; printf("list soc max_fd %d", max_fd); fd_set all_fds; FD_ZERO(&all_fds); FD_SET(listen_soc, &all_fds); while (1) { fd_set listen_fds = all_fds; int nready = select(max_fd + 1, &listen_fds, NULL, NULL, NULL); if (nready == -1) { perror("server: select"); exit(1); } if (FD_ISSET(listen_soc, &listen_fds)) { struct sockaddr_in client_address; socklen_t client_address_len = sizeof(client_address); int client_fd = accept(listen_soc, (struct sockaddr *)&client_address, &client_address_len); if (client_fd < 0) { perror("accept"); continue; } // Add the new client's socket to the file descriptor set for select FD_SET(client_fd, &all_fds); if (client_fd > max_fd) { max_fd = client_fd; } printf("updated max_fd %d", max_fd); printf("Accepted connection\n"); // Prompt the client for their name char name_buffer[BUFFER_SIZE + 1]; check_user(name_buffer, client_fd, user_list); // Create a new user with the given name create_user(name_buffer, &user_list); User *new_user = find_user(name_buffer, user_list); // create new client and add to list of active clients Client *new_client = malloc(sizeof(Client)); new_client->fd = client_fd; new_client->name = new_user->name; new_client->input_len = 0; new_client->next = actives; actives = new_client; char welcome_message[BUFFER_SIZE]; snprintf(welcome_message, BUFFER_SIZE, "Welcome, %s! Go ahead and enter user commands>\r\n", new_user->name); write(client_fd, welcome_message, strlen(welcome_message)); } for (Client *client = actives; client != NULL; client = client->next) { if (FD_ISSET(client->fd, &listen_fds)) { // Read input from client // handle client input } } } return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-04-05T19:58:07.825Z
Help Fix Code
v2
the code is not supposed to block waiting for input from a particular client or the listening socket, but when i run it, it waits for the first client to enter a username before accepting the second client.
int main() { initialize_socket(); printf("Server listening on port %d\n", PORT); User *user_list = NULL; Client *actives = NULL; int max_fd = listen_soc; printf("list soc max_fd %d", max_fd); fd_set all_fds; FD_ZERO(&all_fds); FD_SET(listen_soc, &all_fds); while (1) { fd_set listen_fds = all_fds; int nready = select(max_fd + 1, &listen_fds, NULL, NULL, NULL); if (nready == -1) { perror("server: select"); exit(1); } if (FD_ISSET(listen_soc, &listen_fds)) { struct sockaddr_in client_address; socklen_t client_address_len = sizeof(client_address); int client_fd = accept(listen_soc, (struct sockaddr *)&client_address, &client_address_len); if (client_fd < 0) { perror("accept"); continue; } // Prompt the client for their name char name_buffer[BUFFER_SIZE + 1]; check_user(name_buffer, client_fd, user_list); // Create a new user with the given name create_user(name_buffer, &user_list); User *new_user = find_user(name_buffer, user_list); // create new client and add to list of active clients Client *new_client = malloc(sizeof(Client)); new_client->fd = client_fd; new_client->name = new_user->name; new_client->input_len = 0; new_client->next = actives; actives = new_client; // Add the new client's socket to the file descriptor set for select FD_SET(client_fd, &all_fds); if (client_fd > max_fd) { max_fd = client_fd; } printf("updated max_fd %d", max_fd); printf("Accepted connection\n"); char welcome_message[BUFFER_SIZE]; snprintf(welcome_message, BUFFER_SIZE, "Welcome, %s! Go ahead and enter user commands>\r\n", new_user->name); write(client_fd, welcome_message, strlen(welcome_message)); } for (Client *client = actives; client != NULL; client = client->next) { if (FD_ISSET(client->fd, &listen_fds)) { // Read input from client // handle client input } } } return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-04-05T20:00:23.925Z
Help Fix Code
v2
the code is not supposed to block waiting for input from a particular client or the listening socket, but when i run it, it waits for the first client to enter a username before accepting the second client. The line //handles client input is just a comment that does not aafect this.
int main() { initialize_socket(); printf("Server listening on port %d\n", PORT); User *user_list = NULL; Client *actives = NULL; int max_fd = listen_soc; printf("list soc max_fd %d", max_fd); fd_set all_fds; FD_ZERO(&all_fds); FD_SET(listen_soc, &all_fds); while (1) { fd_set listen_fds = all_fds; int nready = select(max_fd + 1, &listen_fds, NULL, NULL, NULL); if (nready == -1) { perror("server: select"); exit(1); } if (FD_ISSET(listen_soc, &listen_fds)) { struct sockaddr_in client_address; socklen_t client_address_len = sizeof(client_address); int client_fd = accept(listen_soc, (struct sockaddr *)&client_address, &client_address_len); if (client_fd < 0) { perror("accept"); continue; } // Prompt the client for their name char name_buffer[BUFFER_SIZE + 1]; check_user(name_buffer, client_fd, user_list); // Create a new user with the given name create_user(name_buffer, &user_list); User *new_user = find_user(name_buffer, user_list); // create new client and add to list of active clients Client *new_client = malloc(sizeof(Client)); new_client->fd = client_fd; new_client->name = new_user->name; new_client->input_len = 0; new_client->next = actives; actives = new_client; // Add the new client's socket to the file descriptor set for select FD_SET(client_fd, &all_fds); if (client_fd > max_fd) { max_fd = client_fd; } printf("updated max_fd %d", max_fd); printf("Accepted connection\n"); char welcome_message[BUFFER_SIZE]; snprintf(welcome_message, BUFFER_SIZE, "Welcome, %s! Go ahead and enter user commands>\r\n", new_user->name); write(client_fd, welcome_message, strlen(welcome_message)); } for (Client *client = actives; client != NULL; client = client->next) { if (FD_ISSET(client->fd, &listen_fds)) { // Read input from client // handle client input } } } return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-04-05T20:01:48.512Z
Help Fix Code
v2
the code is not supposed to block waiting for input from a particular client or the listening socket, but when i run it, it waits for the first client to enter a username before accepting the second client.
int main() { initialize_socket(); printf("Server listening on port %d\n", PORT); User *user_list = NULL; Client *actives = NULL; int max_fd = listen_soc; printf("list soc max_fd %d", max_fd); fd_set all_fds; FD_ZERO(&all_fds); FD_SET(listen_soc, &all_fds); while (1) { fd_set listen_fds = all_fds; int nready = select(max_fd + 1, &listen_fds, NULL, NULL, NULL); if (nready == -1) { perror("server: select"); exit(1); } if (FD_ISSET(listen_soc, &listen_fds)) { struct sockaddr_in client_address; socklen_t client_address_len = sizeof(client_address); int client_fd = accept(listen_soc, (struct sockaddr *)&client_address, &client_address_len); if (client_fd < 0) { perror("accept"); continue; } // Prompt the client for their name char name_buffer[BUFFER_SIZE + 1]; check_user(name_buffer, client_fd, user_list); // Create a new user with the given name create_user(name_buffer, &user_list); User *new_user = find_user(name_buffer, user_list); // create new client and add to list of active clients Client *new_client = malloc(sizeof(Client)); new_client->fd = client_fd; new_client->name = new_user->name; new_client->input_len = 0; new_client->next = actives; actives = new_client; // Add the new client's socket to the file descriptor set for select FD_SET(client_fd, &all_fds); if (client_fd > max_fd) { max_fd = client_fd; } printf("updated max_fd %d", max_fd); printf("Accepted connection\n"); char welcome_message[BUFFER_SIZE]; snprintf(welcome_message, BUFFER_SIZE, "Welcome, %s! Go ahead and enter user commands>\r\n", new_user->name); write(client_fd, welcome_message, strlen(welcome_message)); } for (Client *client = actives; client != NULL; client = client->next) { if (FD_ISSET(client->fd, &listen_fds)) { // Read input from client // handle client input } } } return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-04-19T22:04:52.383Z
Explain Code
v2
null
int read_a_move(int socket) { int move = -1; while (move < 1 || move > 3) { char line[MAX_BUF]; int num_chars = read(socket, line, MAX_BUF - 1); line[num_chars] = '\0'; while (strstr(line, "\r\n") == NULL) { num_chars += read(socket, &line[num_chars], MAX_BUF - num_chars); line[num_chars] = '\0'; } line[num_chars - 2] = '\0'; move = strtol(line, NULL, 10); if (move < 1 || move > 3) { char *msg = "Your move is not allowed. Please enter a move between 1- 3.\r\n "; write(socket, msg, strlen(msg)); } } return move; }
null
null
63c648605f004bf4898cfbee
2023-02-09T21:39:31.852Z
Question from Code
v1
how dose fseek work here
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: bitmap_printer input_bmp\n"); exit(1); } FILE *image = fopen(argv[1], "rb"); if (image == NULL) { fprintf(stderr, "Cannot open file\n"); exit(1); } // Read in bitmap file metadata int pixel_array_offset, width, height; pixel_array_offset = fseek(image, 11, SEEK_SET); width = fseek(image, 5, SEEK_CUR); height = fseek(image, 1, SEEK_CUR);
null
null
63c648605f004bf4898cfbee
2023-02-09T21:40:40.257Z
Question from Code
v1
what is pixel_array_offset pointing to??
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: bitmap_printer input_bmp\n"); exit(1); } FILE *image = fopen(argv[1], "rb"); if (image == NULL) { fprintf(stderr, "Cannot open file\n"); exit(1); } // Read in bitmap file metadata int pixel_array_offset, width, height; pixel_array_offset = fseek(image, 11, SEEK_SET); width = fseek(image, 5, SEEK_CUR); height = fseek(image, 1, SEEK_CUR);
null
null
63c648605f004bf4898cfbee
2023-02-09T21:41:24.722Z
Question from Code
v1
what is the value of width
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: bitmap_printer input_bmp\n"); exit(1); } FILE *image = fopen(argv[1], "rb"); if (image == NULL) { fprintf(stderr, "Cannot open file\n"); exit(1); } // Read in bitmap file metadata int pixel_array_offset, width, height; pixel_array_offset = fseek(image, 11, SEEK_SET); width = fseek(image, 5, SEEK_CUR); height = fseek(image, 1, SEEK_CUR);
null
null
63c648605f004bf4898cfbee
2023-02-09T21:42:39.096Z
Question from Code
v1
what is height's value
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: bitmap_printer input_bmp\n"); exit(1); } FILE *image = fopen(argv[1], "rb"); if (image == NULL) { fprintf(stderr, "Cannot open file\n"); exit(1); } // Read in bitmap file metadata int pixel_array_offset, width, height; pixel_array_offset = fseek(image, 11, SEEK_CUR); width = fseek(image, 5, SEEK_CUR); height = fseek(image, 1, SEEK_CUR);
null
null
63c648605f004bf4898cfbee
2023-02-09T21:43:04.746Z
Question from Code
v1
what is the value of width
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: bitmap_printer input_bmp\n"); exit(1); } FILE *image = fopen(argv[1], "rb"); if (image == NULL) { fprintf(stderr, "Cannot open file\n"); exit(1); } // Read in bitmap file metadata int pixel_array_offset, width, height; pixel_array_offset = fseek(image, 11, SEEK_CUR); width = fseek(image, 5, SEEK_CUR); height = fseek(image, 1, SEEK_CUR);
null
null
63c648625f004bf4898cfbfd
2023-01-26T19:40:11.489Z
Explain Code
v1
null
char **c; *c = char[2];
null
null
63c648625f004bf4898cfbfd
2023-01-26T23:04:19.750Z
Question from Code
v1
what is &i?
int *i;
null
null
63c648625f004bf4898cfbfd
2023-01-27T23:55:20.088Z
Explain Code
v1
null
char **data; scanf(ls -l, data);
null
null
63c648625f004bf4898cfbfd
2023-01-27T23:58:02.632Z
Help Write Code
v1
null
null
null
run ls -l and pipe its output to your program
63c648635f004bf4898cfc07
2023-02-10T19:39:39.865Z
General Question
v1
how to do assignment 2
null
null
null
63c648635f004bf4898cfc07
2023-02-10T19:42:48.433Z
Question from Code
v1
does it pass all cases?
/* * 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) { // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ if(error < 2){ error = 3; } } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ error = 4; } int smaller = 0; if(sizeof(user1->friends) > sizeof(user2->friends)){ smaller = sizeof(user2->friends); }else{ smaller = sizeof(user1->friends); } // Case 1 if the two users are already friends for(int i = 0;i < smaller; i ++){ if(user1->friends[i]->name == name2){ if(error < 0){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. }else if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 1){ error = 2 ; } }else{ // Case 0 make them friends user1->friends[i] = user2; fprintf(stdout, "%s\n",user1->friends[i]->name); return 0; } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T19:48:29.635Z
Question from Code
v1
does this pass all cases?
/* * 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) { // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ if(error < 2){ error = 3; } } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ error = 4; } int smaller = 0; if(sizeof(user1->friends) > sizeof(user2->friends)){ smaller = sizeof(user2->friends); }else{ smaller = sizeof(user1->friends); } // Case 1 if the two users are already friends for(int i = 0;i < smaller; i ++){ if(user1->friends[i]->name == name2){ if(error < 0){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. }else if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 1){ error = 2 ; } }else{ // Case 0 make them friends user1->friends[i] = user2; fprintf(stdout, "%s\n",user1->friends[i]->name); error = 0; } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T19:49:58.170Z
Question from Code
v1
does this pass case 2
/* * 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) { // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ if(error < 2){ error = 3; } } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ error = 4; } int smaller = 0; if(sizeof(user1->friends) > sizeof(user2->friends)){ smaller = sizeof(user2->friends); }else{ smaller = sizeof(user1->friends); } // Case 1 if the two users are already friends for(int i = 0;i < smaller; i ++){ if(user1->friends[i]->name == name2){ if(error < 0){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. }else if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 1){ error = 2 ; } }else{ // Case 0 make them friends user1->friends[i] = user2; fprintf(stdout, "%s\n",user1->friends[i]->name); return 0; } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T19:51:30.244Z
Help Fix Code
v1
null
/* * 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) { // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ if(error < 2){ error = 3; } } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ error = 4; } int smaller = 0; if(sizeof(user1->friends) > sizeof(user2->friends)){ smaller = sizeof(user2->friends); }else{ smaller = sizeof(user1->friends); } // Case 1 if the two users are already friends for(int i = 0;i < smaller; i ++){ if(user1->friends[i]->name == name2){ if(error < 0){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. }else if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 1){ error = 2 ; } }else{ // Case 0 make them friends user1->friends[i] = user2; fprintf(stdout, "%s\n",user1->friends[i]->name); return 0; } } return error; }
test case 2
null
63c648635f004bf4898cfc07
2023-02-10T19:54:12.109Z
Question from Code
v1
does this pass all cases described in the docstring
/* * 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) { // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ if(error < 2){ error = 3; } } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } int smaller = 0; if(sizeof(user1->friends) > sizeof(user2->friends)){ smaller = sizeof(user2->friends); }else{ smaller = sizeof(user1->friends); } // Case 1 if the two users are already friends for(int i = 0;i < smaller; i ++){ if(user1->friends[i]->name == name2){ if(error < 0){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 1){ error = 2 ; } }else{ // Case 0 make them friends user1->friends[i] = user2; fprintf(stdout, "%s\n",user1->friends[i]->name); return 0; } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T19:58:17.519Z
Question from Code
v1
does this pass all the cases described in the docstring
/* * 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) { // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ if(error < 2){ error = 3; } } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // int smaller = 0; // if(sizeof(user1->friends) > sizeof(user2->friends)){ // smaller = sizeof(user2->friends); // }else{ // smaller = sizeof(user1->friends); // } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(0 > error){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(1 > error){ error = 2 ; } }else{ // Case 0 make them friends user1->friends[i] = user2; fprintf(stdout, "%s\n",user1->friends[i]->name); return 0; } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T19:59:25.201Z
Question from Code
v1
does this pass all cases from the docstring
/* * 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) { // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ if(error < 2){ error = 3; } } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // int smaller = 0; // if(sizeof(user1->friends) > sizeof(user2->friends)){ // smaller = sizeof(user2->friends); // }else{ // smaller = sizeof(user1->friends); // } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(0 > error){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[9] == NULL || user2->friends[9] == NULL){ if(1 > error){ error = 2 ; } }else{ // Case 0 make them friends user1->friends[i] = user2; fprintf(stdout, "%s\n",user1->friends[i]->name); return 0; } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T20:01:38.624Z
Question from Code
v1
does this pass all cases from the docstring
/* * 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) { // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // int smaller = 0; // if(sizeof(user1->friends) > sizeof(user2->friends)){ // smaller = sizeof(user2->friends); // }else{ // smaller = sizeof(user1->friends); // } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(0 > error){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[9] == NULL || user2->friends[9] == NULL){ if(1 > error){ error = 2 ; } }else{ // Case 0 make them friends user1->friends[i] = user2; fprintf(stdout, "%s\n",user1->friends[i]->name); return 0; } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T20:05:17.612Z
Question from Code
v1
does this pass all cases?
/* * 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) { // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // int smaller = 0; // if(sizeof(user1->friends) > sizeof(user2->friends)){ // smaller = sizeof(user2->friends); // }else{ // smaller = sizeof(user1->friends); // } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(0 > error){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. }else if(user1->friends[9] == NULL || user2->friends[9] == NULL){ if(1 > error){ return 2; } }else{ // Case 0 make them friends user1->friends[i] = user2; fprintf(stdout, "%s\n",user1->friends[i]->name); return 0; } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T20:06:21.056Z
Question from Code
v1
does this pass all test
/* * 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) { // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // int smaller = 0; // if(sizeof(user1->friends) > sizeof(user2->friends)){ // smaller = sizeof(user2->friends); // }else{ // smaller = sizeof(user1->friends); // } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(0 > error){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. }else if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(1 > error){ return 2; } }else{ // Case 0 make them friends user1->friends[i] = user2; fprintf(stdout, "%s\n",user1->friends[i]->name); return 0; } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T20:07:27.276Z
Question from Code
v1
list all the cases that this code passes
/* * 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) { // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // int smaller = 0; // if(sizeof(user1->friends) > sizeof(user2->friends)){ // smaller = sizeof(user2->friends); // }else{ // smaller = sizeof(user1->friends); // } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(0 > error){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. }else if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(1 > error){ return 2; } }else{ // Case 0 make them friends user1->friends[i] = user2; fprintf(stdout, "%s\n",user1->friends[i]->name); return 0; } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T20:11:23.602Z
Question from Code
v1
does this pass all cases
* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { if(strlen(filename) > 31){ return 2; } FILE *picture; picture = fopen(filename, "r"); if(picture == NULL){ // fprintf(stdout, "fail"); return 1; }else { strcpy(user-> profile_pic, filename); // fprintf(stdout, "success"); return 0; } }
null
null
63c648635f004bf4898cfc07
2023-02-10T20:12:22.938Z
Question from Code
v1
does this pass all cases
/* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { if(strlen(filename) > 31){ return 2; } FILE *picture; picture = fopen(filename, "r"); if(picture == NULL){ // fprintf(stdout, "fail"); return 1; }else { strcpy(user-> profile_pic, filename); // fprintf(stdout, "success"); return 0; } }
null
null
63c648635f004bf4898cfc07
2023-02-10T20:38:16.965Z
Question from Code
v1
does this have a memory leak or memory issue
/* * 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){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(0 > error){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. } if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(1 > error){ return 2; } }else{ error = 0; } } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; } done1 = 1; } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; } done2 = 1; } fprintf(stdout, "%s\n",user1->friends[i]->name); fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T20:41:17.904Z
Question from Code
v1
how to fix the memory leak
/* * 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){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(0 > error){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. } if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(1 > error){ return 2; } }else{ error = 0; } } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; } done1 = 1; } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; } done2 = 1; } fprintf(stdout, "%s\n",user1->friends[i]->name); fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T20:43:45.936Z
Question from Code
v1
how to fix the memory issue
int make_friends(const char *name1, const char *name2, User *head){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(0 > error){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. } if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(1 > error){ return 2; } }else{ error = 0; } } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; } done1 = 1; } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; } done2 = 1; } // fprintf(stdout, "%s\n",user1->friends[i]->name); // fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T21:00:55.589Z
Question from Code
v1
is this correct passing all cases?
/* * 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){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(0 > error){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. } if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(1 > error){ return 2; } }else{ error = 0; } } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } fprintf(stdout, "%s\n",user1->friends[i]->name); fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T21:08:57.180Z
Question from Code
v1
does this pass all cases?
/* * 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){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(0 > error){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. } if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(1 > error){ return 2; } } } if(error > 0){ return error; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } // fprintf(stdout, "%s\n",user1->friends[i]->name); // fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T21:13:17.405Z
Question from Code
v1
does this pass all cases?
/* * 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){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(0 > error){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. } if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(1 > error){ return 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } fprintf(stdout, "%s\n",user1->friends[i]->name); fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T21:15:29.947Z
Question from Code
v1
does this pass all cases?
/* * 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){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = 0; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(error < 1){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. } if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ return 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } fprintf(stdout, "%s\n",user1->friends[i]->name); fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T21:16:54.011Z
Question from Code
v1
does this pass all cases?
/* * 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){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = 0; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(error < 1){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. } if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } fprintf(stdout, "%s\n",user1->friends[i]->name); fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T21:18:10.659Z
Question from Code
v1
which case does this fail?
/* * 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){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -2; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(error < 1){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. } if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } fprintf(stdout, "%s\n",user1->friends[i]->name); fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T21:20:51.116Z
Question from Code
v1
which line checks if the two users are already friends?
/* * 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){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -2; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(error < 1){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. } if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } fprintf(stdout, "%s\n",user1->friends[i]->name); fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T21:21:53.395Z
Question from Code
v1
does this successfully check if they are already friends?
/* * 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){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -2; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ if(user1->friends[i]->name == name2){ if(error < 1){ error = 1; } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. } if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } fprintf(stdout, "%s\n",user1->friends[i]->name); fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T21:26:12.978Z
Question from Code
v1
does this pass all cases?
/* * 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){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -2; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ // Is this symmetric? if(strcmp(user1->friends[i]->name, name2)==0){ if(error < 1){ error = 1; } } else if(strcmp(user2->friends[i]->name, name1)==0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } fprintf(stdout, "%s\n",user1->friends[i]->name); fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T21:27:41.378Z
Question from Code
v1
does this properly check if the two users are already friends?
/* * 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){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -2; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ // Is this symmetric? if(strcmp(user1->friends[i]->name, name2)==0){ if(error < 1){ error = 1; } } else if(strcmp(user2->friends[i]->name, name1)==0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } fprintf(stdout, "%s\n",user1->friends[i]->name); fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T21:30:00.710Z
Question from Code
v1
does this successfully check if the two users are already friends?
/* * 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){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -2; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ // Is this symmetric? if(strcmp(user1->friends[i]->name, name2)==0 || strcmp(user2->friends[i]->name, name1)==0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } fprintf(stdout, "%s\n",user1->friends[i]->name); fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T21:31:08.202Z
Question from Code
v1
does this successfully check if the two users are already friends?
/* * 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){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -2; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ // Is this symmetric? if((strcmp(user1->friends[i]->name, name2)==0) || (strcmp(user2->friends[i]->name, name1)==0)){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } fprintf(stdout, "%s\n",user1->friends[i]->name); fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T21:36:47.691Z
Question from Code
v1
why does this have segmentation fault?
/* * 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. */ // ./friendme sample.txt int make_friends(const char *name1, const char *name2, User *head){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ // Is this symmetric? if(strcmp(user1->friends[i]->name, name2)== 0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } fprintf(stdout, "%s\n",user1->friends[i]->name); fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T21:38:03.657Z
Question from Code
v1
does this pass all cases?
/* * 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. */ // ./friendme sample.txt int make_friends(const char *name1, const char *name2, User *head){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ // Is this symmetric? if(user1->friends[i] != NULL && strcmp(user1->friends[i]->name, name2)== 0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } fprintf(stdout, "%s\n",user1->friends[i]->name); fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-10T21:40:43.954Z
Question from Code
v1
which cases does this pass
/* * 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. */ // ./friendme sample.txt int make_friends(const char *name1, const char *name2, User *head){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ // Is this symmetric? if(user1->friends[i] != NULL && strcmp(user1->friends[i]->name, name2)== 0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } fprintf(stdout, "%s\n",user1->friends[i]->name); fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-11T19:34:30.747Z
Help Write Code
v1
null
null
null
* * Print a user profile. * For an example of the required output format, see the example output * linked from the handout. * Return: * - 0 on success. * - 1 if the user is NULL. */
63c648635f004bf4898cfc07
2023-02-11T19:35:12.170Z
Help Write Code
v1
null
null
null
print a ASCII file
63c648635f004bf4898cfc07
2023-02-11T19:35:48.583Z
General Question
v1
how to know the length of an ascii file to read
null
null
null
63c648635f004bf4898cfc07
2023-02-11T19:36:23.797Z
General Question
v1
how to read from an ASCII file not knowing the size of the file
null
null
null
63c648635f004bf4898cfc07
2023-02-11T20:37:04.941Z
Help Write Code
v1
null
null
null
/* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */
63c648635f004bf4898cfc07
2023-02-11T22:01:04.716Z
General Question
v1
how to use c and ctime
null
null
null
63c648635f004bf4898cfc07
2023-02-11T22:02:39.998Z
General Question
v1
how to add an element to the front of a non-empty linked struct
null
null
null
63c648635f004bf4898cfc07
2023-02-11T22:09:29.320Z
General Question
v1
give me the code to get the current time in c
null
null
null
63c648635f004bf4898cfc07
2023-02-11T22:11:14.040Z
Help Write Code
v1
null
null
null
how to get the current time in c
63c648635f004bf4898cfc07
2023-02-11T22:13:12.588Z
Help Write Code
v1
null
null
null
how to get the current time using ctime and time
63c648635f004bf4898cfc07
2023-02-11T23:46:19.387Z
Help Write Code
v1
null
null
null
/* * 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. */ int delete_user(const char *name, User **user_ptr_del) { }
63c648635f004bf4898cfc07
2023-02-11T23:55:28.376Z
Question from Code
v1
how to delete the user from the friend's list?
/* * 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
null
63c648635f004bf4898cfc07
2023-02-12T02:08:58.421Z
Question from Code
v1
why is this not freeing everything?
/* * 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. */ int delete_user(const char *name, User **user_ptr_del) { User *curr = *user_ptr_del; int index1 = 0; int i = 0; int j = 0; while(curr != NULL){ //Delete user friend while(i < MAX_FRIENDS - 1 && curr->friends[i] != NULL){ while(j < MAX_FRIENDS - 1 && curr->friends[i]->friends[j] != NULL){ if(strcmp(curr->friends[i]->friends[j]->name, name) == 0){ index1 = j; } j++; } curr->friends[i]->friends[index1] = curr->friends[i]->friends[j - 1]; curr->friends[i]->friends[j - 1] = NULL; i++; } curr = curr->next; } User *curr2 = *user_ptr_del; User *prev = NULL; Post *bye = NULL; while(curr2 != NULL){ if(strcmp(name, curr2->name)==0){ Post *p = curr2->first_post; while(p != NULL){ free(p->date); free(p->contents); bye = p; p = p->next; free(bye); } curr2->first_post = NULL; if(prev ==NULL){ *user_ptr_del = curr2->next; }else{ prev->next = curr2->next; } free(curr2); return 0; } prev = curr2; curr2 = curr2->next; } return 1; }
null
null
63c648635f004bf4898cfc07
2023-02-12T02:10:49.998Z
Question from Code
v1
How come this is not freeing all the malloc
/* * 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. */ int delete_user(const char *name, User **user_ptr_del) { User *curr = *user_ptr_del; int index1 = 0; int i = 0; int j = 0; while(curr != NULL){ //Delete user friend while(i < MAX_FRIENDS - 1 && curr->friends[i] != NULL){ while(j < MAX_FRIENDS - 1 && curr->friends[i]->friends[j] != NULL){ if(strcmp(curr->friends[i]->friends[j]->name, name) == 0){ index1 = j; } j++; } curr->friends[i]->friends[index1] = curr->friends[i]->friends[j - 1]; curr->friends[i]->friends[j - 1] = NULL; i++; } curr = curr->next; } User *curr2 = *user_ptr_del; User *prev = NULL; Post *bye = NULL; while(curr2 != NULL){ if(strcmp(name, curr2->name)==0){ Post *p = curr2->first_post; while(p != NULL){ free(p->date); free(p->contents); bye = p; p = p->next; free(bye); } curr2->first_post = NULL; if(prev ==NULL){ *user_ptr_del = curr2->next; }else{ prev->next = curr2->next; } free(curr2); return 0; } prev = curr2; curr2 = curr2->next; } return 1; }
null
null
63c648635f004bf4898cfc07
2023-02-12T02:12:06.870Z
Question from Code
v1
why is this telling me that i am not freeing all the memory?
/* * 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. */ int delete_user(const char *name, User **user_ptr_del) { User *curr = *user_ptr_del; int index1 = 0; int i = 0; int j = 0; while(curr != NULL){ //Delete user friend while(i < MAX_FRIENDS - 1 && curr->friends[i] != NULL){ while(j < MAX_FRIENDS - 1 && curr->friends[i]->friends[j] != NULL){ if(strcmp(curr->friends[i]->friends[j]->name, name) == 0){ index1 = j; } j++; } curr->friends[i]->friends[index1] = curr->friends[i]->friends[j - 1]; curr->friends[i]->friends[j - 1] = NULL; i++; } curr = curr->next; } User *curr2 = *user_ptr_del; User *prev = NULL; Post *bye = NULL; while(curr2 != NULL){ if(strcmp(name, curr2->name)==0){ Post *p = curr2->first_post; while(p != NULL){ free(p->date); free(p->contents); bye = p; p = p->next; free(bye); } curr2->first_post = NULL; if(prev ==NULL){ *user_ptr_del = curr2->next; }else{ prev->next = curr2->next; } free(curr2); return 0; } prev = curr2; curr2 = curr2->next; } return 1; }
null
null
63c648635f004bf4898cfc07
2023-02-12T02:13:11.018Z
Question from Code
v1
is this over mallocing?
int make_friends(const char *name1, const char *name2, User *head){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ // Is this symmetric? if(user1->friends[i] != NULL && strcmp(user1->friends[i]->name, name2)== 0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } // fprintf(stdout, "%s\n",user1->friends[i]->name); // fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-12T02:13:45.059Z
Question from Code
v1
how is this code over-mallocing?
int make_friends(const char *name1, const char *name2, User *head){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ // Is this symmetric? if(user1->friends[i] != NULL && strcmp(user1->friends[i]->name, name2)== 0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } // fprintf(stdout, "%s\n",user1->friends[i]->name); // fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; }
null
null
63c648635f004bf4898cfc07
2023-02-12T02:16:38.778Z
Question from Code
v1
why is this code not freeing all the malloc
/* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { if(strlen(name) > MAX_NAME - 1){ return 2; } User *curr = *user_ptr_add; User *new_user = malloc(sizeof(User)); if(curr == NULL){ strcpy(new_user-> name, name); new_user->name[MAX_NAME - 1] = '\0'; new_user->first_post = NULL; new_user->next = NULL; *user_ptr_add = new_user; // fprintf(stdout, "%s\n", curr->name); return 0; } else{ while(curr->next != NULL){ if(strcmp(name, curr->name)==0){ return 1; } curr = curr->next; } // Checking the last one if(strcmp(name, curr->name)==0){ return 1; } strcpy(new_user-> name, name); for(int i = 0; i < MAX_FRIENDS - 1; i++){ new_user->friends[i] = NULL; } curr->next = new_user; // fprintf(stdout, "%s\n", curr->name); return 0; } } /* * 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) { User *curr = (User *) head; while(curr != NULL){ if(strcmp(name, curr->name)==0){ return curr; } curr = curr->next; } 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) { User *current = (User*) curr; while(current != NULL){ fprintf(stdout, "%s\n", current->name); current = current->next; } } /* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { if(strlen(filename) > 31){ return 2; } int res = -1; FILE *picture; picture = fopen(filename, "r"); if(picture == NULL){ // fprintf(stdout, "fail"); res = 1; }else { strcpy(user-> profile_pic, filename); // fprintf(stdout, "success"); res = 0; } fclose(picture); return res; } /* * 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. */ // ./friendme sample.txt int make_friends(const char *name1, const char *name2, User *head){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ // Is this symmetric? if(user1->friends[i] != NULL && strcmp(user1->friends[i]->name, name2)== 0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } // fprintf(stdout, "%s\n",user1->friends[i]->name); // fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; } /* * Print a user profile. * For an example of the required output format, see the example output * linked from the handout. * Return: * - 0 on success. * - 1 if the user is NULL. */ int print_user(const User *user) { if(user == NULL){ return 1; } // Print profile picture FILE *picture; picture = fopen(user->profile_pic, "r"); char line[200]; if(picture != NULL){ while(fgets(line, 200, picture)!= NULL){ fprintf(stdout, "%s", line); } fprintf(stdout, "\n"); } // Print Name fprintf(stdout, "Name: %s", user->name); fprintf(stdout, "\n------------------------------------------\n"); // Print Friends fprintf(stdout, "Friends:\n"); for(int i = 0; i < MAX_FRIENDS - 1; i ++){ if(user->friends[i] != NULL){ fprintf(stdout, "%s", user->friends[i]->name); fprintf(stdout, "\n"); } } fprintf(stdout, "------------------------------------------\n"); Post *p = user->first_post; fprintf(stdout, "Posts:\n"); // Print posts while(p != NULL){ // Print the Author fprintf(stdout, "From: %s",p->author); fprintf(stdout, "\n"); fprintf(stdout, "Date: %s",ctime(p->date)); fprintf(stdout, "\n"); // Print the content fprintf(stdout, "%s",p->contents); fprintf(stdout, "\n"); p = p->next; if(p != NULL){ fprintf(stdout, "\n"); fprintf(stdout, "===\n"); fprintf(stdout, "\n"); } } fprintf(stdout, "------------------------------------------\n"); return 0; } /* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { if(author == NULL || target == NULL){ return 2; } // Check if the users are friends for(int i = 0; i < MAX_FRIENDS - 1; i++){ // They have no friends if(author->friends[i] == NULL || target->friends[i] == NULL){ return 1; // If they are friends }else if (strcmp(author->friends[i]->name, target->name) == 0){ Post *p = malloc(sizeof(Post)); strcpy(p-> author, author->name); p->contents = contents; p->date = malloc(sizeof(time_t)); time(p->date); p->next = target->first_post; target->first_post = p; return 0; } } return 1; } /* * 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. */ int delete_user(const char *name, User **user_ptr_del) { User *curr = *user_ptr_del; int index1 = 0; int i = 0; int j = 0; while(curr != NULL){ //Delete user friend while(i < MAX_FRIENDS - 1 && curr->friends[i] != NULL){ while(j < MAX_FRIENDS - 1 && curr->friends[i]->friends[j] != NULL){ if(strcmp(curr->friends[i]->friends[j]->name, name) == 0){ index1 = j; } j++; } curr->friends[i]->friends[index1] = curr->friends[i]->friends[j - 1]; curr->friends[i]->friends[j - 1] = NULL; i++; } curr = curr->next; } User *curr2 = *user_ptr_del; User *prev = NULL; Post *bye = NULL; while(curr2 != NULL){ if(strcmp(name, curr2->name)==0){ Post *p = curr2->first_post; while(p != NULL){ free(p->date); free(p->contents); bye = p; p = p->next; free(bye); } curr2->first_post = NULL; if(prev ==NULL){ *user_ptr_del = curr2->next; }else{ prev->next = curr2->next; } free(curr2); return 0; } prev = curr2; curr2 = curr2->next; } return 1; }
null
null
63c648635f004bf4898cfc07
2023-02-12T02:17:46.190Z
Question from Code
v1
how many is this mallocing
/* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { if(strlen(name) > MAX_NAME - 1){ return 2; } User *curr = *user_ptr_add; User *new_user = malloc(sizeof(User)); if(curr == NULL){ strcpy(new_user-> name, name); new_user->name[MAX_NAME - 1] = '\0'; new_user->first_post = NULL; new_user->next = NULL; *user_ptr_add = new_user; // fprintf(stdout, "%s\n", curr->name); return 0; } else{ while(curr->next != NULL){ if(strcmp(name, curr->name)==0){ return 1; } curr = curr->next; } // Checking the last one if(strcmp(name, curr->name)==0){ return 1; } strcpy(new_user-> name, name); for(int i = 0; i < MAX_FRIENDS - 1; i++){ new_user->friends[i] = NULL; } curr->next = new_user; // fprintf(stdout, "%s\n", curr->name); return 0; } } /* * 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) { User *curr = (User *) head; while(curr != NULL){ if(strcmp(name, curr->name)==0){ return curr; } curr = curr->next; } 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) { User *current = (User*) curr; while(current != NULL){ fprintf(stdout, "%s\n", current->name); current = current->next; } } /* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { if(strlen(filename) > 31){ return 2; } int res = -1; FILE *picture; picture = fopen(filename, "r"); if(picture == NULL){ // fprintf(stdout, "fail"); res = 1; }else { strcpy(user-> profile_pic, filename); // fprintf(stdout, "success"); res = 0; } fclose(picture); return res; } /* * 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. */ // ./friendme sample.txt int make_friends(const char *name1, const char *name2, User *head){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ // Is this symmetric? if(user1->friends[i] != NULL && strcmp(user1->friends[i]->name, name2)== 0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } // fprintf(stdout, "%s\n",user1->friends[i]->name); // fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; } /* * Print a user profile. * For an example of the required output format, see the example output * linked from the handout. * Return: * - 0 on success. * - 1 if the user is NULL. */ int print_user(const User *user) { if(user == NULL){ return 1; } // Print profile picture FILE *picture; picture = fopen(user->profile_pic, "r"); char line[200]; if(picture != NULL){ while(fgets(line, 200, picture)!= NULL){ fprintf(stdout, "%s", line); } fprintf(stdout, "\n"); } // Print Name fprintf(stdout, "Name: %s", user->name); fprintf(stdout, "\n------------------------------------------\n"); // Print Friends fprintf(stdout, "Friends:\n"); for(int i = 0; i < MAX_FRIENDS - 1; i ++){ if(user->friends[i] != NULL){ fprintf(stdout, "%s", user->friends[i]->name); fprintf(stdout, "\n"); } } fprintf(stdout, "------------------------------------------\n"); Post *p = user->first_post; fprintf(stdout, "Posts:\n"); // Print posts while(p != NULL){ // Print the Author fprintf(stdout, "From: %s",p->author); fprintf(stdout, "\n"); fprintf(stdout, "Date: %s",ctime(p->date)); fprintf(stdout, "\n"); // Print the content fprintf(stdout, "%s",p->contents); fprintf(stdout, "\n"); p = p->next; if(p != NULL){ fprintf(stdout, "\n"); fprintf(stdout, "===\n"); fprintf(stdout, "\n"); } } fprintf(stdout, "------------------------------------------\n"); return 0; } /* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { if(author == NULL || target == NULL){ return 2; } // Check if the users are friends for(int i = 0; i < MAX_FRIENDS - 1; i++){ // They have no friends if(author->friends[i] == NULL || target->friends[i] == NULL){ return 1; // If they are friends }else if (strcmp(author->friends[i]->name, target->name) == 0){ Post *p = malloc(sizeof(Post)); strcpy(p-> author, author->name); p->contents = contents; p->date = malloc(sizeof(time_t)); time(p->date); p->next = target->first_post; target->first_post = p; return 0; } } return 1; } /* * 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. */ int delete_user(const char *name, User **user_ptr_del) { User *curr = *user_ptr_del; int index1 = 0; int i = 0; int j = 0; while(curr != NULL){ //Delete user friend while(i < MAX_FRIENDS - 1 && curr->friends[i] != NULL){ while(j < MAX_FRIENDS - 1 && curr->friends[i]->friends[j] != NULL){ if(strcmp(curr->friends[i]->friends[j]->name, name) == 0){ index1 = j; } j++; } curr->friends[i]->friends[index1] = curr->friends[i]->friends[j - 1]; curr->friends[i]->friends[j - 1] = NULL; i++; } curr = curr->next; } User *curr2 = *user_ptr_del; User *prev = NULL; Post *bye = NULL; while(curr2 != NULL){ if(strcmp(name, curr2->name)==0){ Post *p = curr2->first_post; while(p != NULL){ free(p->date); free(p->contents); bye = p; p = p->next; free(bye); } curr2->first_post = NULL; if(prev ==NULL){ *user_ptr_del = curr2->next; }else{ prev->next = curr2->next; } free(curr2); return 0; } prev = curr2; curr2 = curr2->next; } return 1; }
null
null
63c648635f004bf4898cfc07
2023-02-12T02:18:18.630Z
Question from Code
v1
how many malloc calls does this program make in total?
/* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { if(strlen(name) > MAX_NAME - 1){ return 2; } User *curr = *user_ptr_add; User *new_user = malloc(sizeof(User)); if(curr == NULL){ strcpy(new_user-> name, name); new_user->name[MAX_NAME - 1] = '\0'; new_user->first_post = NULL; new_user->next = NULL; *user_ptr_add = new_user; // fprintf(stdout, "%s\n", curr->name); return 0; } else{ while(curr->next != NULL){ if(strcmp(name, curr->name)==0){ return 1; } curr = curr->next; } // Checking the last one if(strcmp(name, curr->name)==0){ return 1; } strcpy(new_user-> name, name); for(int i = 0; i < MAX_FRIENDS - 1; i++){ new_user->friends[i] = NULL; } curr->next = new_user; // fprintf(stdout, "%s\n", curr->name); return 0; } } /* * 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) { User *curr = (User *) head; while(curr != NULL){ if(strcmp(name, curr->name)==0){ return curr; } curr = curr->next; } 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) { User *current = (User*) curr; while(current != NULL){ fprintf(stdout, "%s\n", current->name); current = current->next; } } /* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { if(strlen(filename) > 31){ return 2; } int res = -1; FILE *picture; picture = fopen(filename, "r"); if(picture == NULL){ // fprintf(stdout, "fail"); res = 1; }else { strcpy(user-> profile_pic, filename); // fprintf(stdout, "success"); res = 0; } fclose(picture); return res; } /* * 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. */ // ./friendme sample.txt int make_friends(const char *name1, const char *name2, User *head){ // struct user *friends[MAX_FRIENDS]; an array of users? int error = -1; // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; } // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ return 4; } // Case 1 if the two users are already friends for(int i = 0;i < 9; i ++){ // Is this symmetric? if(user1->friends[i] != NULL && strcmp(user1->friends[i]->name, name2)== 0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[9] != NULL || user2->friends[9] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < 9; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } // fprintf(stdout, "%s\n",user1->friends[i]->name); // fprintf(stdout, "%s\n",user2->friends[i]->name); } } return error; } /* * Print a user profile. * For an example of the required output format, see the example output * linked from the handout. * Return: * - 0 on success. * - 1 if the user is NULL. */ int print_user(const User *user) { if(user == NULL){ return 1; } // Print profile picture FILE *picture; picture = fopen(user->profile_pic, "r"); char line[200]; if(picture != NULL){ while(fgets(line, 200, picture)!= NULL){ fprintf(stdout, "%s", line); } fprintf(stdout, "\n"); } // Print Name fprintf(stdout, "Name: %s", user->name); fprintf(stdout, "\n------------------------------------------\n"); // Print Friends fprintf(stdout, "Friends:\n"); for(int i = 0; i < MAX_FRIENDS - 1; i ++){ if(user->friends[i] != NULL){ fprintf(stdout, "%s", user->friends[i]->name); fprintf(stdout, "\n"); } } fprintf(stdout, "------------------------------------------\n"); Post *p = user->first_post; fprintf(stdout, "Posts:\n"); // Print posts while(p != NULL){ // Print the Author fprintf(stdout, "From: %s",p->author); fprintf(stdout, "\n"); fprintf(stdout, "Date: %s",ctime(p->date)); fprintf(stdout, "\n"); // Print the content fprintf(stdout, "%s",p->contents); fprintf(stdout, "\n"); p = p->next; if(p != NULL){ fprintf(stdout, "\n"); fprintf(stdout, "===\n"); fprintf(stdout, "\n"); } } fprintf(stdout, "------------------------------------------\n"); return 0; } /* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { if(author == NULL || target == NULL){ return 2; } // Check if the users are friends for(int i = 0; i < MAX_FRIENDS - 1; i++){ // They have no friends if(author->friends[i] == NULL || target->friends[i] == NULL){ return 1; // If they are friends }else if (strcmp(author->friends[i]->name, target->name) == 0){ Post *p = malloc(sizeof(Post)); strcpy(p-> author, author->name); p->contents = contents; p->date = malloc(sizeof(time_t)); time(p->date); p->next = target->first_post; target->first_post = p; return 0; } } return 1; } /* * 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. */ int delete_user(const char *name, User **user_ptr_del) { User *curr = *user_ptr_del; int index1 = 0; int i = 0; int j = 0; while(curr != NULL){ //Delete user friend while(i < MAX_FRIENDS - 1 && curr->friends[i] != NULL){ while(j < MAX_FRIENDS - 1 && curr->friends[i]->friends[j] != NULL){ if(strcmp(curr->friends[i]->friends[j]->name, name) == 0){ index1 = j; } j++; } curr->friends[i]->friends[index1] = curr->friends[i]->friends[j - 1]; curr->friends[i]->friends[j - 1] = NULL; i++; } curr = curr->next; } User *curr2 = *user_ptr_del; User *prev = NULL; Post *bye = NULL; while(curr2 != NULL){ if(strcmp(name, curr2->name)==0){ Post *p = curr2->first_post; while(p != NULL){ free(p->date); free(p->contents); bye = p; p = p->next; free(bye); } curr2->first_post = NULL; if(prev ==NULL){ *user_ptr_del = curr2->next; }else{ prev->next = curr2->next; } free(curr2); return 0; } prev = curr2; curr2 = curr2->next; } return 1; }
null
null
63c648635f004bf4898cfc07
2023-02-15T18:04:18.248Z
Question from Code
v1
does this pass all cases?
/* * 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){ int error = -1; // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ error = 4; return error; } // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; return error; } // Case 1 if the two users are already friends for(int i = 0;i < MAX_FRIENDS; i ++){ if(user1->friends[i] != NULL && strcmp(user1->friends[i]->name, name2)== 0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[MAX_FRIENDS - 1] != NULL || user2->friends[MAX_FRIENDS - 1] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < MAX_FRIENDS; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } } } return 0; }
null
null
63c648635f004bf4898cfc07
2023-02-15T18:04:45.315Z
Question from Code
v1
does this pass case 1
/* * 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){ int error = -1; // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ error = 4; return error; } // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; return error; } // Case 1 if the two users are already friends for(int i = 0;i < MAX_FRIENDS; i ++){ if(user1->friends[i] != NULL && strcmp(user1->friends[i]->name, name2)== 0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[MAX_FRIENDS - 1] != NULL || user2->friends[MAX_FRIENDS - 1] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < MAX_FRIENDS; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } } } return 0; }
null
null
63c648635f004bf4898cfc07
2023-02-15T18:05:54.354Z
Question from Code
v1
does case 2 pass
/* * 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){ int error = -1; // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ error = 4; return error; } // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; return error; } // Case 1 if the two users are already friends for(int i = 0;i < MAX_FRIENDS; i ++){ if(user1->friends[i] != NULL && strcmp(user1->friends[i]->name, name2)== 0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[MAX_FRIENDS - 1] != NULL || user2->friends[MAX_FRIENDS - 1] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < MAX_FRIENDS; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } } } return 0; }
null
null
63c648635f004bf4898cfc07
2023-02-15T18:06:43.428Z
Question from Code
v1
does this pass case 3 and 4
/* * 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){ int error = -1; // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ error = 4; return error; } // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; return error; } // Case 1 if the two users are already friends for(int i = 0;i < MAX_FRIENDS; i ++){ if(user1->friends[i] != NULL && strcmp(user1->friends[i]->name, name2)== 0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[MAX_FRIENDS - 1] != NULL || user2->friends[MAX_FRIENDS - 1] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < MAX_FRIENDS; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } } } return 0; }
null
null
63c648635f004bf4898cfc07
2023-02-15T18:07:04.719Z
Question from Code
v1
does this add user correctly as friends
/* * 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){ int error = -1; // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ error = 4; return error; } // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; return error; } // Case 1 if the two users are already friends for(int i = 0;i < MAX_FRIENDS; i ++){ if(user1->friends[i] != NULL && strcmp(user1->friends[i]->name, name2)== 0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[MAX_FRIENDS - 1] != NULL || user2->friends[MAX_FRIENDS - 1] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < MAX_FRIENDS; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } } } return 0; }
null
null
63c648635f004bf4898cfc07
2023-02-15T18:07:48.447Z
Question from Code
v1
why does this code fail the cases?
/* * 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){ int error = -1; // Case 4 if at least one user does not exist. User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL){ error = 4; return error; } // Case 3 if the same user is passed in twice. if(strcmp(name1, name2)==0){ error = 3; return error; } // Case 1 if the two users are already friends for(int i = 0;i < MAX_FRIENDS; i ++){ if(user1->friends[i] != NULL && strcmp(user1->friends[i]->name, name2)== 0){ if(error < 1){ error = 1; } } // Case 2 if the users are not already friends, but at least one already has // MAX_FRIENDS friends. if(user1->friends[MAX_FRIENDS - 1] != NULL || user2->friends[MAX_FRIENDS - 1] != NULL){ if(error < 2){ error = 2; } } } if(error > 0){ return error; } else{ error = 0; } int done1 = 0; int done2 = 0; if(error == 0){ // Case 0 make them friends for(int i = 0; i < MAX_FRIENDS; i++){ if(done1 == 0){ if(user1->friends[i] == NULL){ user1->friends[i] = user2; done1 = 1; } } if(done2 == 0){ if(user2->friends[i] == NULL){ user2->friends[i] = user1; done2 = 1; } } } } return 0; }
null
null
63c648635f004bf4898cfc07
2023-02-24T21:25:16.933Z
General Question
v1
when do you need to pass a double pointer as an parameter to a helper function?
null
null
null
63c648635f004bf4898cfc07
2023-02-24T21:26:23.308Z
General Question
v1
when should i use a single and double pointer as parameter?
null
null
null
63c648635f004bf4898cfc07
2023-02-24T21:28:07.182Z
General Question
v1
why do i need to pass in a double pointer for a helper function that uses heap memory?
null
null
null
63c648635f004bf4898cfc07
2023-02-24T21:28:35.309Z
General Question
v1
does malloc modify the original pointer?
null
null
null
63c648635f004bf4898cfc07
2023-02-24T21:29:24.839Z
Question from Code
v1
can you explain why this code needs a double pointer as parameter?
/* * Read in pixel array by following these instructions: * * 1. First, allocate space for m `struct pixel *` values, where m is the * height of the image. Each pointer will eventually point to one row of * pixel data. * 2. For each pointer you just allocated, initialize it to point to * heap-allocated space for an entire row of pixel data. * 3. Use the given file and pixel_array_offset to initialize the actual * struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is * consistent with the bitmap file format. * NOTE: We've tested this assumption on the Teaching Lab machines, but * if you're trying to work on your own computer, we strongly recommend * checking this assumption! * 4. Return the address of the first `struct pixel *` you initialized. */ struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { // Allocate space struct pixel **allocatedMem = malloc(sizeof(struct pixel *) * height); for (int i = 0; i < height; i++){ allocatedMem[i] = malloc((sizeof(struct pixel)) * width); } fseek(image, pixel_array_offset, SEEK_SET); unsigned char blue; unsigned char green; unsigned char red; for (int i = 0; i < height; i++){ for (int j = 0; j < width; j++){ fread(&blue, 1, 1, image); fread(&green, 1, 1, image); fread(&red, 1, 1, image); struct pixel pixels= {blue, green, red}; allocatedMem[i][j] = pixels; } } return allocatedMem; }
null
null
63c648635f004bf4898cfc07
2023-02-24T21:30:04.855Z
Question from Code
v1
can you tell me why i need to pass a double pointer to fib?
/* * Define a function void fib(...) below. This function takes parameter n * and generates the first n values in the Fibonacci sequence. Recall that this * sequence is defined as: * 0, 1, 1, 2, 3, 5, ... , fib[n] = fib[n-2] + fib[n-1], ... * The values should be stored in a dynamically-allocated array composed of * exactly the correct number of integers. The values should be returned * through a pointer parameter passed in as the first argument. * * See the main function for an example call to fib. * Pay attention to the expected type of fib's parameters. */ /* Write your solution here */ void fib(int **fib_sequence, int count){ // allocate memory *fib_sequence = malloc(count * sizeof(int)); if(count >= 1){ (*fib_sequence)[0] = 0; } if(count >= 2){ (*fib_sequence)[1] = 1; } if(count >= 3){ for(int i = 2; i < count; i++){ (*fib_sequence)[i] = (*fib_sequence)[i - 2] + (*fib_sequence)[i - 1]; } } } int main(int argc, char **argv) { /* do not change this main function */ int count = strtol(argv[1], NULL, 10); int *fib_sequence; fib(&fib_sequence, count); for (int i = 0; i < count; i++) { printf("%d ", fib_sequence[i]); } free(fib_sequence); return 0; }
null
null
63c648635f004bf4898cfc07
2023-02-24T21:30:25.610Z
Question from Code
v1
why do we need a double pointer?
/* * Define a function void fib(...) below. This function takes parameter n * and generates the first n values in the Fibonacci sequence. Recall that this * sequence is defined as: * 0, 1, 1, 2, 3, 5, ... , fib[n] = fib[n-2] + fib[n-1], ... * The values should be stored in a dynamically-allocated array composed of * exactly the correct number of integers. The values should be returned * through a pointer parameter passed in as the first argument. * * See the main function for an example call to fib. * Pay attention to the expected type of fib's parameters. */ /* Write your solution here */ void fib(int **fib_sequence, int count){ // allocate memory *fib_sequence = malloc(count * sizeof(int)); if(count >= 1){ (*fib_sequence)[0] = 0; } if(count >= 2){ (*fib_sequence)[1] = 1; } if(count >= 3){ for(int i = 2; i < count; i++){ (*fib_sequence)[i] = (*fib_sequence)[i - 2] + (*fib_sequence)[i - 1]; } } } int main(int argc, char **argv) { /* do not change this main function */ int count = strtol(argv[1], NULL, 10); int *fib_sequence; fib(&fib_sequence, count); for (int i = 0; i < count; i++) { printf("%d ", fib_sequence[i]); } free(fib_sequence); return 0; }
null
null
63c648635f004bf4898cfc07
2023-02-24T21:30:52.136Z
General Question
v1
i am confused on when i should use a double pointer as parameter
null
null
null
63c648635f004bf4898cfc07
2023-02-24T21:31:27.437Z
General Question
v1
is passing a double pointer as parameter only used for nested structures?
null
null
null
63c648635f004bf4898cfc07
2023-02-24T21:35:56.739Z
General Question
v1
to print an array in the main function, do i need to pass a double pointer as parameter to a helper function?
null
null
null
63c648635f004bf4898cfc07
2023-02-24T21:38:07.303Z
General Question
v1
where should i start for assignment 3
null
null
null
63c648635f004bf4898cfc07
2023-03-10T01:21:34.992Z
General Question
v2
for assignment 3, how do i initialize the structs in parse file?
null
null
null
63c648635f004bf4898cfc07
2023-03-10T01:23:03.738Z
General Question
v2
can you give me the psuedo code for parse file in A3?
null
null
null
63c648635f004bf4898cfc07
2023-03-10T01:23:45.849Z
General Question
v2
how do you use strtok and fgets to parse thorough a makefile?
null
null
null
63c648635f004bf4898cfc07
2023-03-10T17:52:42.159Z
General Question
v2
how to assign a token from strtok() to char *?
null
null
null
63c648635f004bf4898cfc07
2023-03-10T17:56:23.408Z
Question from Code
v2
can you give me psuedo code for this?
/* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) {
null
null
63c648635f004bf4898cfc07
2023-03-10T19:35:30.486Z
General Question
v2
What is \t?
null
null
null
63c648635f004bf4898cfc07
2023-03-10T19:35:57.777Z
General Question
v2
how to use isspace?
null
null
null