diff --git "a/students-coding-questions-from-ai-assistant.csv" "b/students-coding-questions-from-ai-assistant.csv" new file mode 100644--- /dev/null +++ "b/students-coding-questions-from-ai-assistant.csv" @@ -0,0 +1,142395 @@ +user_id,time,feature_type,feature_version,input_question,input_code,input_intention,input_task_description +63c647635f004bf4898cf351,2023-01-18T04:30:17.303Z,Question from Code,v1,assign the amount in rain_today to the space rainfall points to,"int main(int argc, char **argv) { + float *rainfall; + float rain_today; +}",, +63c647635f004bf4898cf351,2023-02-07T21:51:18.185Z,Question from Code,v1,Why am I getting segmentation fault,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { + return 2; + } + + User *curr = *user_ptr_add; + while (curr->next != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + + User *new_user = malloc(sizeof(User)); + strncpy(new_user->name, name, sizeof(MAX_NAME - 1)); + (new_user->name)[MAX_NAME - 1] = '\0'; + curr->next = new_user; + new_user->next = NULL; + + return 0; +}",, +63c647635f004bf4898cf351,2023-02-07T22:17:08.664Z,Question from Code,v1,strcmp is not working,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { + return 2; + } + + // make new user + User *new_user = malloc(sizeof(User)); + strncpy(new_user->name, name, sizeof(MAX_NAME - 1)); + (new_user->name)[MAX_NAME - 1] = '\0'; + + User *curr = *user_ptr_add; + if (curr == NULL) { + curr = new_user; + new_user->next = NULL; + return 0; + } + + while (curr->next != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + + curr->next = new_user; + new_user = NULL; + return 0; +}",, +63c647635f004bf4898cf351,2023-02-07T22:17:57.317Z,Question from Code,v1,is curr->name a string,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { + return 2; + } + + // make new user + User *new_user = malloc(sizeof(User)); + strncpy(new_user->name, name, sizeof(MAX_NAME - 1)); + (new_user->name)[MAX_NAME - 1] = '\0'; + + User *curr = *user_ptr_add; + if (curr == NULL) { + curr = new_user; + new_user->next = NULL; + return 0; + } + + while (curr->next != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + + curr->next = new_user; + new_user = NULL; + return 0; +}",, +63c647635f004bf4898cf351,2023-02-07T22:18:59.859Z,Question from Code,v1,how do I know whether name and curr->name are the same,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { + return 2; + } + + // make new user + User *new_user = malloc(sizeof(User)); + strncpy(new_user->name, name, sizeof(MAX_NAME - 1)); + (new_user->name)[MAX_NAME - 1] = '\0'; + + User *curr = *user_ptr_add; + if (curr == NULL) { + curr = new_user; + new_user->next = NULL; + return 0; + } + + while (curr->next != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + + curr->next = new_user; + new_user = NULL; + return 0; +}",, +63c647635f004bf4898cf351,2023-02-07T22:22:40.387Z,Question from Code,v1,"what does strcmp(name, curr->name) do","int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { + return 2; + } + + // make new user + User *new_user = malloc(sizeof(User)); + strncpy(new_user->name, name, sizeof(MAX_NAME - 1)); + (new_user->name)[MAX_NAME - 1] = '\0'; + + User *curr = *user_ptr_add; + if (curr == NULL) { + curr = new_user; + new_user->next = NULL; + return 0; + } + + while (curr->next != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + + curr->next = new_user; + new_user = NULL; + return 0; +}",, +63c647635f004bf4898cf351,2023-02-08T04:53:26.759Z,Question from Code,v1,why do I get error with new_post.author = author->name,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + if (check_friends((User *)author, target) == 1) { + return 1; + } + + struct post new_post; + new_post.author = author->name; + new_post.contents = contents; + + time_t curtime; + new_post.date = &curtime; + new_post.next = target->first_post->next; + target->first_post = &new_post; + + return 0; +}",, +63c647635f004bf4898cf351,2023-02-08T04:57:44.821Z,Question from Code,v1,"how to fix error for new_post->author = author->name +","int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + if (check_friends((User *)author, target) == 1) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + new_post->author = author->name; + new_post->contents = contents; + + time_t curtime; + new_post->date = &curtime; + new_post->next = target->first_post->next; + target->first_post = new_post; + + return 0; +}",, +63c647635f004bf4898cf351,2023-02-08T05:05:09.640Z,Question from Code,v1,why am i getting segmentation fault,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + if (check_friends((User *)author, target) == 1) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + + time_t curtime; + new_post->date = &curtime; + new_post->next = target->first_post->next; + target->first_post = new_post; + + return 0; +} + +int check_friends(User *author, User *target) { + int friends = 1; + + int i = 0; + while (i < MAX_FRIENDS) { + if (author->friends[i] == NULL) { + break; + } + + if (strcmp(author->friends[i]->name, target->name) == 0) { + friends = 0; + } + i++; + } + + return friends; +}",, +63c647635f004bf4898cf351,2023-02-08T05:07:14.885Z,Question from Code,v1,why am I getting segmentation fault,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + if (check_friends((User *)author, target) == 1) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + strcpy(new_post->contents, contents); + + time_t curtime; + new_post->date = &curtime; + new_post->next = target->first_post->next; + target->first_post = new_post; + + return 0; +} + +int check_friends(User *author, User *target) { + int friends = 1; + + int i = 0; + while (i < MAX_FRIENDS) { + if (author->friends[i] == NULL) { + break; + } + + if (strcmp(author->friends[i]->name, target->name) == 0) { + friends = 0; + } + i++; + } + + return friends; +}",, +63c647635f004bf4898cf351,2023-02-08T05:11:57.321Z,Question from Code,v1,how to fix error in ew_post->author = malloc(sizeof(char) * MAX_NAME);,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + if (check_friends((User *)author, target) == 1) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + new_post->author = malloc(sizeof(char) * MAX_NAME); + strcpy(new_post->author, author->name); + strcpy(new_post->contents, contents); + + time_t curtime; + new_post->date = &curtime; + new_post->next = target->first_post->next; + target->first_post = new_post; + + return 0; +} + +int check_friends(User *author, User *target) { + int friends = 1; + + int i = 0; + while (i < MAX_FRIENDS) { + if (author->friends[i] == NULL) { + break; + } + + if (strcmp(author->friends[i]->name, target->name) == 0) { + friends = 0; + } + i++; + } + + return friends; +}",, +63c647635f004bf4898cf351,2023-02-08T05:13:45.509Z,Question from Code,v1,error in new_post->author = malloc(MAX_NAME * sizeof(char));,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + if (check_friends((User *)author, target) == 1) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + new_post->author = malloc(MAX_NAME * sizeof(char)); + strcpy(new_post->author, author->name); + strcpy(new_post->contents, contents); + + time_t curtime; + new_post->date = &curtime; + new_post->next = target->first_post->next; + target->first_post = new_post; + + return 0; +} + +int check_friends(User *author, User *target) { + int friends = 1; + + int i = 0; + while (i < MAX_FRIENDS) { + if (author->friends[i] == NULL) { + break; + } + + if (strcmp(author->friends[i]->name, target->name) == 0) { + friends = 0; + } + i++; + } + + return friends; +}",, +63c647635f004bf4898cf351,2023-02-12T00:50:28.733Z,Question from Code,v1,does this work,"int check_friends(User *user1, User *user2) { + int friends = 1; + + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp((user1->friends[i])->name, user2->name) == 0) { + friends = 0; + break; + } + } + + return friends; +}",, +63c647635f004bf4898cf351,2023-02-12T00:51:08.355Z,Question from Code,v1,why am i getting segmentation fault,"int check_friends(User *user1, User *user2) { + int friends = 1; + + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp((user1->friends[i])->name, user2->name) == 0) { + friends = 0; + break; + } + } + + return friends; +}",, +63c647635f004bf4898cf351,2023-02-13T01:44:42.104Z,Question from Code,v1,why do I have unitialised values,"void delete_from_friends(User *user) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user->friends[i] == NULL) {} + else { + for (int j = 0; j < MAX_FRIENDS; j++) { + User *friends_friend = user->friends[i]->friends[j]; + if (friends_friend == NULL) {} + else if (strcmp(friends_friend->name, user->name) == 0) { + user->friends[i]->friends[j] = NULL; + break; + } + } + } + } +}",, +63c647635f004bf4898cf351,2023-02-13T01:47:04.905Z,Question from Code,v1,why do i have an error with if (friends_friend == NULL) {},"void delete_from_friends(User *user) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user->friends[i] == NULL) {} + else { + for (int j = 0; j < MAX_FRIENDS; j++) { + User *friends_friend = user->friends[i]->friends[j]; + if (friends_friend == NULL) {} + else if (strcmp(friends_friend->name, user->name) == 0) { + user->friends[i]->friends[j] = NULL; + break; + } + } + } + } +}",, +63c647635f004bf4898cf351,2023-02-22T00:43:09.546Z,Question from Code,v1,Is there a segmentation fault in this,"Rule *parse_file(FILE *fp) { + // Implement this function and remove the stubbed return statement below. + + Rule *first_rule_ptr = NULL; + Rule *last_rule_ptr = NULL; + char str[MAXLINE]; + + while (fgets(str, MAXLINE, fp) != NULL) { + if (str[0] == ' ' || str[0] == '\t' || str[0] == '\n') {} + else if (is_comment_or_empty(str)) {} + else if (is_target_line(str)) { + Rule *new_rule = init_rule(str); + last_rule_ptr = new_rule; + if (first_rule_ptr == NULL) {first_rule_ptr = new_rule;} + else {insert_rule(&first_rule_ptr, &new_rule);} + } else { // is action line + add_actions(&last_rule_ptr, str); + } + } + + fseek(fp, 0, SEEK_SET); + while (fgets(str, MAXLINE, fp) != NULL) { + if (is_target_line(str)) { + add_dependencies(&first_rule_ptr, str); + } + } + + return first_rule_ptr; +}",, +63c647635f004bf4898cf351,2023-02-22T00:44:31.528Z,Question from Code,v1,Is there a segmentation fault in this,"Rule *init_rule(char *line) { + + Rule *new_rule = malloc(sizeof(Rule)); + new_rule->dependencies = NULL; + new_rule->actions = NULL; + new_rule->next_rule = NULL; + + char *ptr = strtok(line, "" :""); + new_rule->target = malloc((sizeof(char) * strlen(ptr)) + 1); + strcpy(new_rule->target, ptr); + new_rule->target[strlen(ptr)] = '\0'; + + return new_rule; +}",, +63c647635f004bf4898cf351,2023-02-22T00:45:42.588Z,Question from Code,v1,Is there a segmentation fault in this,"void add_dependencies(Rule **rules_head, char *line) { + + char *ptr = strtok((char *)line, "" :""); // target name + + Rule *curr = *rules_head; + while (curr->next_rule != NULL) { + if (strcmp(curr->target, ptr) == 0) {break;} + else {curr = curr->next_rule;} + } + + ptr = strtok(NULL, "" ""); + while (ptr != NULL) { + Dependency *new_dep = malloc(sizeof(Dependency)); + new_dep->rule = find_rule(&curr, ptr); + + if (curr->dependencies == NULL) { + curr->dependencies = new_dep; + new_dep->next_dep = NULL; + } else { + new_dep->next_dep = curr->dependencies; + curr->dependencies = new_dep; + } + ptr = strtok(NULL, "" ""); + } +}",, +63c647635f004bf4898cf351,2023-02-22T00:49:50.319Z,Question from Code,v1,Is there a segmentation fault in this,"Rule *find_rule(Rule **rules_ptr, char *name) { + Rule *curr = *rules_ptr; + while(curr->next_rule != NULL) { + if (strcmp(curr->target, name) == 0) { + break; + } + curr = curr->next_rule; + } + + if (strcmp(curr->target, name) != 0) { // rule doesn't exist + curr->next_rule = init_rule(name); + curr = curr->next_rule; + } + + return curr; +}",, +63c647635f004bf4898cf351,2023-02-22T00:57:51.378Z,Question from Code,v1,Where are the segmentation faults,"int is_target_line(const char *line); +Rule *init_rule(char *line); +void insert_rule(Rule **rules_head, Rule **new_rule); +void add_actions(Rule **last_rule_ptr, char *line); +void add_dependencies(Rule **rules_head, char *line); +Rule *find_rule(Rule **rules_ptr, char *name); + +/* 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) { + // Implement this function and remove the stubbed return statement below. + + Rule *first_rule_ptr = NULL; + Rule *last_rule_ptr = NULL; + char str[MAXLINE]; + + while (fgets(str, MAXLINE, fp) != NULL) { + if (str[0] == ' ' || str[0] == '\t' || str[0] == '\n') {} + else if (is_comment_or_empty(str)) {} + else if (is_target_line(str)) { + Rule *new_rule = init_rule(str); + last_rule_ptr = new_rule; + if (first_rule_ptr == NULL) {first_rule_ptr = new_rule;} + else {insert_rule(&first_rule_ptr, &new_rule);} + } else { // is action line + add_actions(&last_rule_ptr, str); + } + } + + fseek(fp, 0, SEEK_SET); + while (fgets(str, MAXLINE, fp) != NULL) { + if (is_target_line(str)) { + add_dependencies(&first_rule_ptr, str); + } + } + + return first_rule_ptr; +} +/* Return 1 if the line is a target line, as defined on the assignment handout. + * Return 0 otherwise. + */ +int is_target_line(const char *line) { + if (strstr(line, "" : "") != NULL) { + return 1; + } + return 0; +} + +/* Initialize a new rule node with the specified target name + * from line (in target line format). Initialize everything else to NULL. + * Return a pointer to this new node. + */ +Rule *init_rule(char *line) { + + Rule *new_rule = malloc(sizeof(Rule)); + new_rule->dependencies = NULL; + new_rule->actions = NULL; + new_rule->next_rule = NULL; + new_rule->target = NULL; + + char *ptr = strtok(line, "" :""); + new_rule->target = malloc((sizeof(char) * strlen(ptr)) + 1); + strcpy(new_rule->target, ptr); + new_rule->target[strlen(ptr)] = '\0'; + + return new_rule; +} + +/* Add dependencies to target from line (in target line format). + * Point each dependency to its corresponding rule node in the + * rules list pointed to by rules_head. + */ +void add_dependencies(Rule **rules_head, char *line) { + + char *ptr = strtok((char *)line, "" :""); // target name + + Rule *curr = *rules_head; + while (curr->next_rule != NULL) { + if (strcmp(curr->target, ptr) == 0) {break;} + else {curr = curr->next_rule;} + } + + ptr = strtok(NULL, "" ""); + while (ptr != NULL) { + Dependency *new_dep = malloc(sizeof(Dependency)); + new_dep->rule = NULL; + new_dep->next_dep = NULL; + new_dep->rule = find_rule(&curr, ptr); + + if (curr->dependencies == NULL) { + curr->dependencies = new_dep; + new_dep->next_dep = NULL; + } else { + new_dep->next_dep = curr->dependencies; + curr->dependencies = new_dep; + } + ptr = strtok(NULL, "" ""); + } +} + +/* Return the rule node with the given target name + * in the rules list pointed to by rules_ptr. + * If no such rule exists, create it and return it. + */ +Rule *find_rule(Rule **rules_ptr, char *name) { + Rule *curr = *rules_ptr; + while(curr->next_rule != NULL) { + if (strcmp(curr->target, name) == 0) { + break; + } + curr = curr->next_rule; + } + + if (strcmp(curr->target, name) != 0) { // rule doesn't exist + curr->next_rule = init_rule(name); + curr = curr->next_rule; + } + + return curr; +} + +/* Insert the new rule to the tail of the rules list pointed to + * by rules_head. + * + * Precondition: rules list pointed to by rules_head is not empty + */ +void insert_rule(Rule **rules_head, Rule **new_rule) { + Rule *curr = *rules_head; + while (curr->next_rule != NULL) { + curr = curr->next_rule; + } + curr->next_rule = *new_rule; +} + +/* Add actions in line (in action line format) to + * the rule node pointed to by last_rule_ptr + */ +void add_actions(Rule **last_rule_ptr, char *line) {}",, +63c647645f004bf4898cf35b,2023-01-25T21:27:31.768Z,Explain Code,v1,,"void fib(int **fib_sequence, int count){ + *fib_sequence = malloc(sizeof(int)*count); + if (count == 1) { + *fib_sequence[0] = 0; + } else { + *fib_sequence[0] = 0; + *fib_sequence[1] = 1; + for (int i = 2; i < count; i++) { + *fib_sequence[i] = *fib_sequence[i-2] + *fib_sequence[i-1]; + } + } + }",, +63c647645f004bf4898cf35b,2023-01-25T21:28:35.214Z,General Question,v1,why do I get a segmentation fault,,, +63c647645f004bf4898cf35b,2023-01-25T21:30:17.991Z,Help Fix Code,v1,,"void fib(int **fib_sequence, int count){ + *fib_sequence = malloc(sizeof(int)*count); + if (count == 1) { + *fib_sequence[0] = 0; + } else { + *fib_sequence[0] = 0; + *fib_sequence[1] = 1; + for (int i = 2; i < count; i++) { + *fib_sequence[i] = *fib_sequence[i-2] + *fib_sequence[i-1]; + } + } + }","generate the first n values in the Fibonacci sequence +", +63c647645f004bf4898cf35b,2023-01-25T21:36:05.238Z,Help Fix Code,v1,,"void fib(int **fib_sequence, int count){ + int *cur = *fib_sequence + cur = malloc(sizeof(int)*count); + if (count == 1) { + cur[0] = 0; + } else { + cur[0] = 0; + cur[1] = 1; + for (int i = 2; i < count; i++) { + cur[i] = cur[i-2] + cur[i-1]; + } + } + }",generate the first n values in the Fibonacci sequence, +63c647645f004bf4898cf35b,2023-01-25T21:37:03.455Z,Help Fix Code,v1,,"void fib(int **fib_sequence, int count){ + int *cur = *fib_sequence + cur = malloc(sizeof(int)*count); + if (count == 1) { + *cur[0] = 0; + } else { + *cur[0] = 0; + *cur[1] = 1; + for (int i = 2; i < count; i++) { + *cur[i] = *cur[i-2] + *cur[i-1]; + } + } + }",generate the first n values in the Fibonacci sequence, +63c647645f004bf4898cf35b,2023-01-25T21:38:00.702Z,Help Fix Code,v1,,"void fib(int **fib_sequence, int count){ + int *cur = *fib_sequence + cur = malloc(sizeof(int)*count); + if (count == 1) { + cur[0] = 0; + } else { + cur[0] = 0; + cur[1] = 1; + for (int i = 2; i < count; i++) { + cur[i] = *cur[i-2] + *cur[i-1]; + } + } + }",generate the first n values in the Fibonacci sequence, +63c647645f004bf4898cf35b,2023-01-25T21:39:23.197Z,Help Fix Code,v1,,"void fib(int **fib_sequence, int count){ + int *cur = *fib_sequence + *cur = malloc(sizeof(int)*count); + if (count == 1) { + cur[0] = 0; + } else { + cur[0] = 0; + cur[1] = 1; + for (int i = 2; i < count; i++) { + cur[i] = cur[i-2] + cur[i-1]; + } + } + }",generate the first n values in the Fibonacci sequence, +63c647645f004bf4898cf35b,2023-01-25T21:40:29.438Z,Help Fix Code,v1,,"void fib(int **fib_sequence, int count){ + int *cur = **fib_sequence + cur = malloc(sizeof(int)*count); + if (count == 1) { + cur[0] = 0; + } else { + cur[0] = 0; + cur[1] = 1; + for (int i = 2; i < count; i++) { + cur[i] = cur[i-2] + cur[i-1]; + } + } + }",generate the first n values in the Fibonacci sequence, +63c647645f004bf4898cf35b,2023-01-30T21:59:11.935Z,Help Fix Code,v1,,"int populate_array(int sin, int *sin_array) { + for (int i = 8; i >= 0; i--){ + sin_array[i] = sin % 10; + sin = sin / 10; + } + if (sin != 0) { + return 1; + } + else { + return 0; + } +}","It takes an integer and an integer array as its arguments, and returns an integer. This function's job is to populate the given integer array so that it contains the 9 digits of the given integer, in the same order as in the integer. ", +63c647645f004bf4898cf35b,2023-01-30T22:00:26.564Z,Help Fix Code,v1,,"int populate_array(int sin, int *sin_array) { + for (int i = 8; i >= 0; i--){ + sin_array[i] = sin % 10; + sin = sin / 10; + } + if (sin == 0) { + return 0; + } + else { + return 1; + } +}","It takes an integer and an integer array as its arguments, and returns an integer. This function's job is to populate the given integer array so that it contains the 9 digits of the given integer, in the same order as in the integer.", +63c647645f004bf4898cf35b,2023-01-30T22:19:32.496Z,Help Fix Code,v1,,"int check_sin(int *sin_array) { + int sin_total = 0; + for (int i = 0; i < 9; i++){ + if (i % 2 == 0){ + int result = sin_array[i] * 2; //multiplies every second digit by 2 + if (result >= 10) { + sin_total += 1 + (result % 10); + } + else { + sin_total += result; + } + } + else { + sin_total += sin_array[i]; + } + } + if (sin_total % 10 == 0){ + return 0; + } else { + return 1; + } +}","it takes an 9 element integer array representing a candidate SIN number. It returns 0 if the number given is a valid Canadian SIN number, and 1 otherwise.", +63c647645f004bf4898cf35b,2023-02-10T21:56:14.388Z,Explain Code,v1,,"User *find_user(const char *name, const User *head) { + User *curr_user = (User *) head; + while (curr_user != NULL) { + if (strcmp(curr_user->name, name) == 0) { + return curr_user; + } + } + return NULL; +}",, +63c647645f004bf4898cf35b,2023-02-10T22:09:34.049Z,Explain Code,v1,,"void list_users(const User *curr) { + User *curr_user = (User *) curr; + while (curr_user != NULL) { + printf(""%s\n"", curr_user->name); + curr_user = curr_user->next; + } +}",, +63c647645f004bf4898cf35b,2023-02-14T19:06:00.310Z,General Question,v1,how to determine the number of elements in an array,,, +63c647645f004bf4898cf35b,2023-02-15T04:49:52.485Z,General Question,v1,Do we have to assign values to all attributes of a struct when creating a new instance of it,,, +63c647645f004bf4898cf35b,2023-02-15T21:10:37.196Z,General Question,v1,when allocating space for a next pointer in a linked list struct what is the size of the allocation,,, +63c647645f004bf4898cf35b,2023-02-17T04:06:47.317Z,Explain Code,v1,,"char *copy(char *dest, const char *src, int capacity) { + int null_count = 0; + for (int i = 0; i < capacity; i++) { + if (src[i] == '\0') { // When src is less than or equal to capacity to be copied + null_count += (capacity - i); + break; + } + } + + if (null_count != 0) { + for (int j = 0; j < capacity - null_count; j++) { + dest[j] = src[j]; + } + for (int k = capacity - null_count; k < capacity; k++) { + dest[k] = '\0'; + } + } + + else { + for (int j = 0; j < capacity - 1; j++) { + dest[j] = src[j]; + } + dest[capacity - 1] = '\0'; + } + for (int i = 0; i < capacity && src[i] != '\0'; i++) { + dest[i] = src[i]; + for ( ; i < capacity; i++) { + dest[i] = '\0'; + } + } + return dest; +} +",, +63c647645f004bf4898cf35b,2023-02-17T04:09:24.340Z,Explain Code,v1,,"char *copy(char *dest, const char *src, int capacity) { +for (int i = 0; i < capacity && src[i] != '\0'; i++) { + dest[i] = src[i]; + for ( ; i < capacity; i++) { + dest[i] = '\0'; + } + } + return dest; +}",, +63c647645f004bf4898cf35b,2023-02-17T04:10:50.648Z,Explain Code,v1,,"char *copy(char *dest, const char *src, int capacity) { + int null_count = 0; + for (int i = 0; i < capacity; i++) { + if (src[i] == '\0') { // When src is less than or equal to capacity to be copied + null_count += (capacity - i); + break; + } + } + + if (null_count != 0) { + for (int j = 0; j < capacity - null_count; j++) { + dest[j] = src[j]; + } + for (int k = capacity - null_count; k < capacity; k++) { + dest[k] = '\0'; + } + } + + else { + for (int j = 0; j < capacity - 1; j++) { + dest[j] = src[j]; + } + dest[capacity - 1] = '\0'; + } +}",, +63c647645f004bf4898cf35b,2023-02-28T04:01:01.804Z,General Question,v1,"is the sizeof(int**) equal to sizeof(int*) +",,, +63c647645f004bf4898cf35b,2023-03-08T18:27:19.096Z,Help Write Code,v1,,,,Get parent process to wait for all child process to complete +63c647645f004bf4898cf35b,2023-04-02T19:38:32.471Z,General Question,v2,when a message with newlines is sent to a client from a server should the message end with one network newline or does the network newline replace all newline characters in the message,,, +63c647645f004bf4898cf35b,2023-04-06T17:22:07.297Z,General Question,v2,When to use memset vs when to malloc,,, +63c647645f004bf4898cf35b,2023-04-06T20:39:25.321Z,General Question,v2,how to copy a char array to a char pointer,,, +63c647645f004bf4898cf35b,2023-04-06T23:45:58.815Z,General Question,v2,how to use select ,,, +63c647645f004bf4898cf35b,2023-04-24T02:38:51.049Z,Help Fix Code,v2,print list,"int *make_list() { +int list[3] = {1, 2, 3}; +return list; +} +int main() { +int *l = make_list(); +int i; +for (i = 0; i < 3; i++) { +printf(""%d\n"", l[i]); +} +return 0; +}",, +63c647645f004bf4898cf35b,2023-04-24T02:48:05.134Z,General Question,v2,does initializing a char array include the null terminator,,, +63c647645f004bf4898cf35b,2023-04-24T02:51:51.169Z,Help Fix Code,v2,store literal dynamically,"char *word = malloc(6 * sizeof(char)); +*word = ""hello"";",, +63c647645f004bf4898cf35b,2023-04-24T17:54:28.518Z,General Question,v2,can strcpy alter string literal,,, +63c647645f004bf4898cf35b,2023-04-24T20:11:20.699Z,General Question,v2,how to guarantee order of output for child processes when forking,,, +63c647645f004bf4898cf35b,2023-04-25T03:31:11.151Z,Explain Code,v2,,"#include +#include +#include +#include +#include +int main(int argc, char **argv) { + int ret; + int sum = 0; + int status; + for (int i = 1; i < argc; i++) { + ret = fork(); + if (ret < 0) { + perror(""fork""); + exit(1); + } else if (ret == 0) { + int len = strlen(argv[i]); + printf(""I am child %d and I'm returning %d\n"", i, len); + exit(len); + } + } + for (int i = 1; i < argc; i++) { + if ((ret = wait(&status)) == -1) { + perror(""wait""); + exit(1); + } + if (WIFEXITED(status)) { + sum += WEXITSTATUS(status); + } + } + printf(""The length of all the args is %d\n"", sum); + return 0; +}",, +63c647645f004bf4898cf35b,2023-04-25T03:35:19.295Z,General Question,v2,what is dup2 used for,,, +63c647645f004bf4898cf35b,2023-04-25T21:39:20.796Z,Help Fix Code,v2,print hello world,"char *s = ""hello world""; +char *t = malloc(sizeof(s)); +strcpy(t, s); +printf(""%s"", t);",, +63c647645f004bf4898cf35b,2023-04-25T21:42:09.850Z,Question from Code,v2,"what happens if we use sizeof(s) in malloc instead of strlen(s) + 1 +","char *s = ""hello world""; +char *t = malloc(sizeof(s)); +strcpy(t, s); +printf(""%s"", t);",, +63c647665f004bf4898cf36f,2023-03-02T03:02:51.468Z,Question from Code,v1,modify the program so that the new children do not create additional processes. Only the original parent calls fork. Keep the printf call for all processes. ,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +",, +63c647665f004bf4898cf36f,2023-03-02T03:04:17.403Z,Help Fix Code,v1,,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +",modify the program so that the new children do not create additional processes. Only the original parent calls fork. Keep the printf call for all processes. , +63c647665f004bf4898cf36f,2023-03-02T03:06:20.878Z,Question from Code,v1,modify the program so that the new children do not create additional processes. Only the original parent calls fork ITERATIONS times. Keep the printf call for all processes. ,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +",, +63c647665f004bf4898cf36f,2023-03-14T01:27:57.390Z,General Question,v2,"how to fork children processes in parallel and make the parent wait for all the childern +",,, +63c647665f004bf4898cf36f,2023-03-14T01:30:03.763Z,Help Write Code,v2,,,,fork 5 child proccess in parallel and make the parent process wait until all child processes have terminated +63c647665f004bf4898cf36f,2023-03-14T02:21:51.085Z,General Question,v2,how to fork childern sequentially and not in parallel,,, +63c647665f004bf4898cf36f,2023-03-14T04:24:42.482Z,General Question,v2,execvp: No such file or directory why am i getting this error ,,, +63c647665f004bf4898cf374,2023-01-22T06:21:22.141Z,Question from Code,v1,Why does this compiling and executing this result in a segmentation fault?,"int main(int argc, char **argv) { + // TODO: Verify that command line arguments are valid. + if (argc != 2) { + return 2; //returning with code 2 + } + // TODO: Parse arguments and then call the two helpers in sin_helpers.c + // to verify the SIN given as a command line argument. + + int sin_number = strtol(argv[1], NULL, 10); + int sin_array[9]; + + if (populate_array(sin_number, sin_array) == 0) { //the parsed argument was a 9 digit integer + if (check_sin(sin_array) == 0) { //in addition, if valid sin number then + printf(""Valid SIN\n""); + return 0; + } + else { + printf(""Invalid SIN\n""); + return 1; + } + } + return 0; +}",, +63c647665f004bf4898cf374,2023-01-22T06:23:39.600Z,Question from Code,v1,Does calling this function with appropriate parameters result in a segmentation fault?,"int populate_array(int sin, int *sin_array) { + //setting up dummy variables to test whether sin is a 9 digit in + int dummy = sin; + int count = 0; + while (dummy != 0) { + dummy = dummy / 10; + count = count + 1; + } + if (count != 9) { //10 didn't divide into sin exactly 9 times, thus the number is not a 9 digit int + return 1; + } + else{ + for (int i = 8; i >= 0; i--) { //sin is a 9 digit int, so populate array + sin_array[i] = sin % 10; + sin = sin / 10; + } + return 0; + } +}",, +63c647665f004bf4898cf374,2023-01-22T06:36:16.925Z,Question from Code,v1,Will executing main result in a segmentation fault?,"#include +int check_sin(int *sin_array) { + if (sin_array[0] == 0) { //assignment simplification: if it starts with 0 its invalid + return 0; + } + else { + int result[9]; + int sum = 0; + + for (int i = 0; i < 9; i++) { + if (i % 2 == 0) { + //even indices get multiplied by 1 + result[i] = sin_array[i]; //the result at i is just going to be the digit since i * 1 = i + } + else { + //odd indices get multiplied by 2, and if its a 2 digit number than add the 2 digits and output it to result[i] + result[i] = ((sin_array[i] * 2) / 10) + ((sin_array[i] * 2) % 10); + //if the product is < 10, the division is 0 and the modulo will be the digit, so the sum is just the digit + //if the product is >= 10, the division is the first digit of the product, the modulo is the second, so the sum + //are the added digits + } + } + for (int i = 0; i < 9; i++) { + sum = sum + result[i]; + } + if (sum % 10 == 0) { + return 0; + } + return 1; + } +} + +int main() { + int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6}; + printf(""%d/n"", check_sin(sin_array)); +} +",, +63c647665f004bf4898cf374,2023-01-22T06:39:17.378Z,Question from Code,v1,Does executing main result in a segmentation fault?,"#include +int populate_array(int sin, int *sin_array) { + //setting up dummy variables to test whether sin is a 9 digit in + int dummy = sin; + int count = 0; + while (dummy != 0) { + dummy = dummy / 10; + count = count + 1; + } + if (count != 9) { //10 didn't divide into sin exactly 9 times, thus the number is not a 9 digit int + return 1; + } + else{ + for (int i = 8; i >= 0; i--) { //sin is a 9 digit int, so populate array + sin_array[i] = sin % 10; + sin = sin / 10; + } + return 0; + } +} + +int main() { + int sin_array[9]; + printf(""%d/n"", check_sin(sin_array)); +} +",, +63c647665f004bf4898cf374,2023-01-22T06:40:04.712Z,Question from Code,v1,Does executing this code result in a segmentation fault?,"#include +int populate_array(int sin, int *sin_array) { + //setting up dummy variables to test whether sin is a 9 digit in + int dummy = sin; + int count = 0; + while (dummy != 0) { + dummy = dummy / 10; + count = count + 1; + } + if (count != 9) { //10 didn't divide into sin exactly 9 times, thus the number is not a 9 digit int + return 1; + } + else{ + for (int i = 8; i >= 0; i--) { //sin is a 9 digit int, so populate array + sin_array[i] = sin % 10; + sin = sin / 10; + } + return 0; + } +} + +int main() { + int sin_array[9]; + printf(""%d/n"", populate_array(sin_array)); +} +",, +63c647665f004bf4898cf374,2023-01-22T06:41:09.042Z,Question from Code,v1,Does executing this code result in a segmentation fault?,"#include +int populate_array(int sin, int *sin_array) { + //setting up dummy variables to test whether sin is a 9 digit in + int dummy = sin; + int count = 0; + while (dummy != 0) { + dummy = dummy / 10; + count = count + 1; + } + if (count != 9) { //10 didn't divide into sin exactly 9 times, thus the number is not a 9 digit int + return 1; + } + else{ + for (int i = 8; i >= 0; i--) { //sin is a 9 digit int, so populate array + sin_array[i] = sin % 10; + sin = sin / 10; + } + return 0; + } +} + +int main() { + int sin_array[9]; + printf(""%d/n"", populate_array(810620716, sin_array)); +} +",, +63c647665f004bf4898cf374,2023-01-22T06:51:03.766Z,Question from Code,v1,Why does executing this produce a segmentation fault?,"#include +int populate_array(int sin, int *sin_array) { + //setting up dummy variables to test whether sin is a 9 digit in + int dummy = sin; + int count = 0; + while (dummy != 0) { + dummy = dummy / 10; + count = count + 1; + } + if (count != 9) { //10 didn't divide into sin exactly 9 times, thus the number is not a 9 digit int + return 1; + } + else{ + for (int i = 8; i >= 0; i--) { //sin is a 9 digit int, so populate array + sin_array[i] = sin % 10; + sin = sin / 10; + } + return 0; + } +} + +int main() { + int sin_array[9]; + printf(""%d/n"", check_sin(sin_array)); +} + +Yes, because the populate_array function is not called. +Will executing main result in a segmentation fault? +#include +int check_sin(int *sin_array) { + if (sin_array[0] == 0) { //assignment simplification: if it starts with 0 its invalid + return 0; + } + else { + int result[9]; + int sum = 0; + + for (int i = 0; i < 9; i++) { + if (i % 2 == 0) { + //even indices get multiplied by 1 + result[i] = sin_array[i]; //the result at i is just going to be the digit since i * 1 = i + } + else { + //odd indices get multiplied by 2, and if its a 2 digit number than add the 2 digits and output it to result[i] + result[i] = ((sin_array[i] * 2) / 10) + ((sin_array[i] * 2) % 10); + //if the product is < 10, the division is 0 and the modulo will be the digit, so the sum is just the digit + //if the product is >= 10, the division is the first digit of the product, the modulo is the second, so the sum + //are the added digits + } + } + for (int i = 0; i < 9; i++) { + sum = sum + result[i]; + } + if (sum % 10 == 0) { + return 0; + } + return 1; + } +} + +int main() { + int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6}; + printf(""%d/n"", check_sin(sin_array)); +} +",, +63c647665f004bf4898cf374,2023-01-22T06:52:22.825Z,Question from Code,v1,Why does running this produce a segmentation fault?,"#include +int check_sin(int *sin_array) { + if (sin_array[0] == 0) { //assignment simplification: if it starts with 0 its invalid + return 0; + } + else { + int result[9]; + int sum = 0; + + for (int i = 0; i < 9; i++) { + if (i % 2 == 0) { + //even indices get multiplied by 1 + result[i] = sin_array[i]; //the result at i is just going to be the digit since i * 1 = i + } + else { + //odd indices get multiplied by 2, and if its a 2 digit number than add the 2 digits and output it to result[i] + result[i] = ((sin_array[i] * 2) / 10) + ((sin_array[i] * 2) % 10); + //if the product is < 10, the division is 0 and the modulo will be the digit, so the sum is just the digit + //if the product is >= 10, the division is the first digit of the product, the modulo is the second, so the sum + //are the added digits + } + } + for (int i = 0; i < 9; i++) { + sum = sum + result[i]; + } + if (sum % 10 == 0) { + return 0; + } + return 1; + } +} + +int main() { + int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6}; + printf(""%d/n"", check_sin(sin_array)); +} +",, +63c647665f004bf4898cf374,2023-01-22T06:54:07.434Z,Help Fix Code,v1,,"#include +int check_sin(int *sin_array) { + if (sin_array[0] == 0) { //assignment simplification: if it starts with 0 its invalid + return 0; + } + else { + int result[9]; + int sum = 0; + + for (int i = 0; i < 9; i++) { + if (i % 2 == 0) { + //even indices get multiplied by 1 + result[i] = sin_array[i]; //the result at i is just going to be the digit since i * 1 = i + } + else { + //odd indices get multiplied by 2, and if its a 2 digit number than add the 2 digits and output it to result[i] + result[i] = ((sin_array[i] * 2) / 10) + ((sin_array[i] * 2) % 10); + //if the product is < 10, the division is 0 and the modulo will be the digit, so the sum is just the digit + //if the product is >= 10, the division is the first digit of the product, the modulo is the second, so the sum + //are the added digits + } + } + for (int i = 0; i < 9; i++) { + sum = sum + result[i]; + } + if (sum % 10 == 0) { + return 0; + } + return 1; + } +} + +int main() { + int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6}; + printf(""%d/n"", check_sin(sin_array)); +} +",Outputs 1, +63c647665f004bf4898cf374,2023-01-22T06:56:43.358Z,Question from Code,v1,Why does running this result in a segmentation fault?,"#include +int check_sin(int *sin_array) { + if (sin_array[0] == 0) { //assignment simplification: if it starts with 0 its invalid + return 1; + } + else { + int result[9]; + int sum = 0; + + for (int i = 0; i < 9; i++) { + if (i % 2 == 0) { + //even indices get multiplied by 1 + result[i] = sin_array[i]; //the result at i is just going to be the digit since i * 1 = i + } + else { + //odd indices get multiplied by 2, and if its a 2 digit number than add the 2 digits and output it to result[i] + result[i] = ((sin_array[i] * 2) / 10) + ((sin_array[i] * 2) % 10); + //if the product is < 10, the division is 0 and the modulo will be the digit, so the sum is just the digit + //if the product is >= 10, the division is the first digit of the product, the modulo is the second, so the sum + //are the added digits + } + } + for (int i = 0; i < 9; i++) { + sum = sum + result[i]; + } + if (sum % 10 == 0) { + return 0; + } + return 1; + } +} + +int main() { + int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6}; + printf(""%d/n"", check_sin(sin_array)); +} +",, +63c647665f004bf4898cf374,2023-04-23T00:17:23.387Z,Question from Code,v2,Would the lines of code compile and execute without error always?,"int x; +int *p = &x; +printf(""%d\n"", *p);",, +63c647665f004bf4898cf374,2023-04-23T00:19:52.704Z,Question from Code,v2,Would the code compile and execute without error all the time?,"char arr[11] = ""Basket""; +strcat(arr, ""ball""); +printf(""%s\n"", arr);",, +63c647665f004bf4898cf374,2023-04-23T00:28:26.919Z,Question from Code,v2,Would this code always compile and execute without error?,"char str[] = ""CSC209""; +char *s = ""CSC209S""; +s[6] = '\0'; +printf(""%d\n"", strcmp(str, s));",, +63c647665f004bf4898cf374,2023-04-23T00:50:03.928Z,Question from Code,v2,Does this code always compile and run without error,"char *get_extension(char •str) { + char ext[4]; + char *p; + if ((p = strchr(str, '.')) != NULL) { + strncpy(ext, p, 4); + ext[3] = '\0'; + return ext; + } else { + return NULL; + } +} +printf(""%s\n"", get_extension(""run.c""));",, +63c647665f004bf4898cf374,2023-04-23T00:51:58.020Z,Question from Code,v2,Does running this code cause any errors such as segmentation fault,"char *get_extension(char *str) { + char ext[4]; + char *p; + if ((p = strchr(str, '.')) != NULL) { + strncpy(ext, p, 4); + ext[3] = '\0'; + return ext; + } else { + return NULL; + } +} +printf(""%s\n"", get_extension(""run.c""));",, +63c647675f004bf4898cf37e,2023-02-14T19:02:19.184Z,Help Fix Code,v1,," +int create_user(const char *name, User **user_ptr_add) { + // Checks if the name fits into the array. + if (strlen(name) >= MAX_NAME) { + return 2; + } else ( + User *curr = *user_ptr_add; + User *next = curr -> next; + + // While loop to check if the name is already in the list. + while (next != NULL) { + if (strcmp(name, curr->name) == 0) { + curr = curr -> next; + next = curr -> next; + } else { + return 1; + } + } + + // Creates the new user and adds it to the list. + User *new_user = malloc(sizeof(User)); + strcpy(new_user -> name, name); + curr->next = new_user; + new_user->next = NULL; + ) + return -1; +}","/* + * 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) + */", +63c647675f004bf4898cf37e,2023-02-14T19:04:12.190Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + const User *curr = head; + + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + curr = curr -> next; + } else { + return &curr; + } + } + + return NULL; +} +","/* + * 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. + */", +63c647675f004bf4898cf37e,2023-02-14T19:08:05.009Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + const User *curr = head; + + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + curr = curr -> next; + } else { + return curr; + } + } + + return NULL; +}","/* + * 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. + */", +63c647675f004bf4898cf37e,2023-02-14T23:06:09.413Z,Question from Code,v1,Why is my code giving a segmentation fault?,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +/* + * 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) { + // Checks if the name fits into the array. + if (strlen(name) >= MAX_NAME) { + return 2; + } else { + User *curr = *user_ptr_add; + User *next = curr -> next; + + // While loop to check if the name is already in the list. + while (next != NULL) { + if (strcmp(name, next->name) != 0) { + curr = curr -> next; + next = curr -> next; + } else { + return 1; + } + } + + // Creates the new user and adds it to the list. + User *new_user = malloc(sizeof(User)); + strcpy(new_user -> name, name); + strcpy(new_user->profile_pic, """"); + curr->next = new_user; + new_user->next = NULL; + + return 0; + } +} +",, +63c647675f004bf4898cf37e,2023-02-14T23:07:27.395Z,Question from Code,v1,why is this giving me a segmentation fault,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +/* + * 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) { + // Checks if the name fits into the array. + if (strlen(name) >= MAX_NAME) { + return 2; + } else { + User *curr = *user_ptr_add; + User *next = curr -> next; + + // While loop to check if the name is already in the list. + while (next != NULL) { + if (strcmp(name, next->name) != 0) { + curr = curr -> next; + next = curr -> next; + } else { + return 1; + } + } + + // Creates the new user and adds it to the list. + User *new_user = malloc(sizeof(User)); + strcpy(new_user -> name, name); + curr->next = new_user; + new_user->next = NULL; + + return 0; + } +} +",, +63c647675f004bf4898cf37e,2023-02-14T23:14:30.147Z,Explain Code,v1,,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + + +/* + * 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) { + // Checks if the name fits into the array. + if (strlen(name) >= MAX_NAME) { + return 2; + } else { + User *curr = *user_ptr_add; + User *next = curr -> next; + + // Creates the new user and adds it to the list. + User *new_user = malloc(sizeof(User)); + strcpy(new_user -> name, name); + + if (curr == NULL) { + curr = new_user; + new_user->next = NULL; + } else if (strcmp(name, curr->name) == 0) { + return 1; + } + + // While loop to check if the name is already in the list. + while (next != NULL) { + if (strcmp(name, next->name) != 0) { + curr = curr -> next; + next = curr -> next; + } else { + return 1; + } + } + + curr->next = new_user; + new_user->next = NULL; + + return 0; + } +}",, +63c647675f004bf4898cf37e,2023-02-14T23:15:04.222Z,Question from Code,v1,why is this giving me a segmentation fault?,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + + +/* + * 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) { + // Checks if the name fits into the array. + if (strlen(name) >= MAX_NAME) { + return 2; + } else { + User *curr = *user_ptr_add; + User *next = curr -> next; + + // Creates the new user and adds it to the list. + User *new_user = malloc(sizeof(User)); + strcpy(new_user -> name, name); + + if (curr == NULL) { + curr = new_user; + new_user->next = NULL; + } else if (strcmp(name, curr->name) == 0) { + return 1; + } + + // While loop to check if the name is already in the list. + while (next != NULL) { + if (strcmp(name, next->name) != 0) { + curr = curr -> next; + next = curr -> next; + } else { + return 1; + } + } + + curr->next = new_user; + new_user->next = NULL; + + return 0; + } +}",, +63c647675f004bf4898cf37e,2023-02-15T09:08:50.730Z,Question from Code,v1,"WHY AM I GETTING A SEGMENTATION FAULT +","#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int print_user(const User *user) { + if (user == NULL) { + return 1; + } else { + + // Print the profile_pic + FILE *profile_pic; + profile_pic = fopen(user->profile_pic, ""r""); + char line[100]; + + if (profile_pic != NULL) { + while (fgets(line, 100, profile_pic) != NULL) { + printf(""%s"", line); + } + } + fclose(profile_pic); + + // Prints the name + printf(""\nName: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // Prints the friends + printf(""Friends:\n""); + for (int i=0; i < MAX_FRIENDS; i++) { + if (user->friends[i] != NULL) { + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + + return 0; + } +}",, +63c647675f004bf4898cf37e,2023-02-15T09:29:12.204Z,Question from Code,v1,why am i getting a segmentation fault (core dumped),"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + User *new_user = malloc(sizeof(User)); + + // Check if memory allocation was successful; exits if not. + if (new_user == NULL) { + exit(1); + } + + if (strlen(name) >= MAX_NAME) { + free(new_user); + return 2; + } else if (curr == NULL) { + + *user_ptr_add = new_user; + new_user->next = *user_ptr_add; + + } else { + User *next = curr -> next; + + // While loop to check if the name is already in the list. + while (next != NULL) { + if (strcmp(name, next->name) != 0) { + curr = next; + next = curr -> next; + } else { + free(new_user); + return 1; + } + + } + + curr->next = new_user; + new_user->next = NULL; + + } + + new_user->profile_pic[0] = '\0'; + strcpy(new_user->name, name); + for (int i=0; i < MAX_FRIENDS; i++) { + new_user->friends[i] = NULL; + } + new_user->first_post = NULL; + + return 0; +} + +int print_user(const User *user) { + if (user == NULL) { + return 1; + } else { + + // Print the profile_pic + FILE *profile_pic; + profile_pic = fopen(user->profile_pic, ""r""); + char line[100]; + + if (profile_pic != NULL) { + while (fgets(line, 100, profile_pic) != NULL) { + printf(""%s"", line); + } + } + fclose(profile_pic); + + // Prints the name + printf(""\nName: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // Prints the friends + printf(""Friends:\n""); + for (int i=0; i < MAX_FRIENDS; i++) { + if (user->friends[i] != NULL) { + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + + return 0; + } +}",, +63c647675f004bf4898cf37e,2023-02-15T09:30:05.608Z,Question from Code,v1,why am i getting a segmentation fault (core dumped),"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int print_user(const User *user) { + if (user == NULL) { + return 1; + } else { + + // Print the profile_pic + FILE *profile_pic; + profile_pic = fopen(user->profile_pic, ""r""); + char line[100]; + + if (profile_pic != NULL) { + while (fgets(line, 100, profile_pic) != NULL) { + printf(""%s"", line); + } + } + fclose(profile_pic); + + // Prints the name + printf(""\nName: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // Prints the friends + printf(""Friends:\n""); + for (int i=0; i < MAX_FRIENDS; i++) { + if (user->friends[i] != NULL) { + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + + return 0; + } +}",, +63c647675f004bf4898cf37e,2023-02-15T09:40:08.793Z,Question from Code,v1,why does my while loop run forever,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +/* + * 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) { + User *curr = *user_ptr_add; + User *new_user = malloc(sizeof(User)); + + // Check if memory allocation was successful; exits if not. + if (new_user == NULL) { + exit(1); + } + + if (strlen(name) >= MAX_NAME) { + free(new_user); + return 2; + } else if (curr == NULL) { + + *user_ptr_add = new_user; + new_user->next = *user_ptr_add; + + } else { + User *next = curr -> next; + + // While loop to check if the name is already in the list. + while (next != NULL) { + if (strcmp(name, next->name) != 0) { + curr = curr -> next; + next = curr -> next; + } else { + free(new_user); + return 1; + } + + } + + curr->next = new_user; + new_user->next = NULL; + + } + + new_user->profile_pic[0] = '\0'; + strcpy(new_user->name, name); + for (int i=0; i < MAX_FRIENDS; i++) { + new_user->friends[i] = NULL; + } + new_user->first_post = NULL; + + return 0; +}",, +63c647675f004bf4898cf37e,2023-02-15T09:43:16.813Z,Question from Code,v1,why is my while loop running forever,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +/* + * 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) { + User *curr = *user_ptr_add; + User *new_user = malloc(sizeof(User)); + + // Check if memory allocation was successful; exits if not. + if (new_user == NULL) { + exit(1); + } + + if (strlen(name) >= MAX_NAME) { + free(new_user); + return 2; + } else if (curr == NULL) { + + *user_ptr_add = new_user; + new_user->next = *user_ptr_add; + + } else { + User *next = curr -> next; + + // While loop to check if the name is already in the list. + while (next != NULL) { + if (strcmp(name, next->name) == 0) { + free(new_user); + return 1; + } + curr = curr -> next; + next = next -> next; + } + + curr->next = new_user; + new_user->next = NULL; + + } + + new_user->profile_pic[0] = '\0'; + strcpy(new_user->name, name); + for (int i=0; i < MAX_FRIENDS; i++) { + new_user->friends[i] = NULL; + } + new_user->first_post = NULL; + + return 0; +}",, +63c647675f004bf4898cf37e,2023-02-15T10:02:56.564Z,Help Fix Code,v1,,"#include + +#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } else { + + // Create and allocate a new post. + Post* new_post = malloc(sizeof(Post)); + + // Check if memory allocation was successful; exits if not. + if (new_post == NULL) { + exit(1); + } + + // Check if users are friends + if (check_friends((User*) author, target) == 0) { + + strcpy(new_post->author, author->name); + strcpy(new_post->contents, contents); + + // Grab the date. + time_t date = time(NULL); + time(&date); + new_post->date = &date; + + // Change the posts in author's struct. + Post* old_post = target->first_post; + if (old_post == NULL) { + target->first_post = new_post; + target->next = NULL; + } else { + target->first_post = new_post; + new_post->next = old_post; + } + + return 0; + + } else { + + free(new_post); + return 1; + + } + + } +}","/* + * 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 + */ + +I am currently getting a segmentation fault (core dumped).", +63c647675f004bf4898cf37e,2023-02-15T11:39:02.020Z,Help Fix Code,v1,,"#include + +#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } else { + + // Create and allocate a new post. + Post* new_post = malloc(sizeof(Post)); + + // Check if memory allocation was successful; exits if not. + if (new_post == NULL) { + exit(1); + } + + // Check if users are friends + if (check_friends((User*) author, target) == 0) { + + strcpy(new_post->author, author->name); + new_post->contents = contents; + + // Grab the current time (using time(NULL)) and assign to new_post. + time_t curr_time = time(NULL); + time_t *date = malloc(sizeof(time_t)); + + // If memory could not be allocated to date, exit and free the new_post memory. + if (date == NULL) { + free(new_post); + exit(1); + } + *date = curr_time; + new_post->date = date; + + // Change the posts in author's struct. + Post* old_post = target->first_post; + if (old_post == NULL) { + target->first_post = new_post; + target->next = NULL; + } else { + target->first_post = new_post; + new_post->next = old_post; + } + + return 0; + + } else { + + free(new_post); + 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* user = find_user(name, *user_ptr_del); + if (user == NULL) { + return 1; + } + + User *curr_user = *user_ptr_del; + while (curr_user != NULL) { + // Check if the current user in the list is friends with the deleted user. + if (check_friends(user, curr_user) == 0) { + for (int i=0; i < MAX_FRIENDS; i++) { + if (curr_user->friends[i] == user) { + curr_user->friends[i] = NULL; + } + user->friends[i] = NULL; + } + } + } + + // Remove all posts from the user's profile. + Post *curr_post = user->first_post; + Post *next_post = curr_post->next; + if (curr_post != NULL) { + while (next_post != NULL) { + // Free all allocated memory from the current post. + free(curr_post->contents); + free(curr_post->date); + free(curr_post->next); + free(curr_post); + + curr_post = next_post; + next_post = curr_post->next; + } + } + + // Check if user is first in the linked list. + if (user == *user_ptr_del) { + // Check if the deleted user is the only user. + if (user->next == NULL) { + *user_ptr_del = NULL; + } else { + *user_ptr_del = user->next; + user->next = NULL; + } + } else { + + User* prev_user = *user_ptr_del; + User* curr_user = prev_user->next; + User* next_user = curr_user->next; + + // Iterate until curr_user is the deleted user. + while (curr_user != user) { + prev_user = prev_user->next; + curr_user = curr_user->next; + next_user = next_user->next; + } + + prev_user->next = next_user; + curr_user->next = NULL; + + } + + // Free all alocated memory from the user. + free(user->next); + free(user->first_post); + free(user); + + 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 + */ + +My while loop is running forever.", +63c647675f004bf4898cf37e,2023-02-15T19:19:56.804Z,Question from Code,v1,"Why am I having a memory leakage from this? +","typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int print_user(const User *user) { + if (user == NULL) { + return 1; + } else { + + // Prints the profile_pic + FILE *pic; + + if (user->profile_pic[0] != '\0') { + pic = fopen(user->profile_pic, ""r""); + char line[100]; + + if (pic != NULL) { + while (fgets(line, 100, pic) != NULL) { + printf(""%s"", line); + } + printf(""\n""); + } + } + fclose(pic); + }",, +63c647675f004bf4898cf37e,2023-02-15T19:20:56.252Z,Question from Code,v1,Why am I having a memory leakage? It seems like the pic is being closed by fclose(pic);,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int print_user(const User *user) { + if (user == NULL) { + return 1; + } else { + + // Prints the profile_pic + FILE *pic; + + if (user->profile_pic[0] != '\0') { + pic = fopen(user->profile_pic, ""r""); + char line[100]; + + if (pic != NULL) { + while (fgets(line, 100, pic) != NULL) { + printf(""%s"", line); + } + printf(""\n""); + } + } + fclose(pic); + }",, +63c647675f004bf4898cf37e,2023-02-15T19:36:02.092Z,Question from Code,v1,"Why am I getting a memory leakage? In function update_pic, the file is being closed using fclose(image);","typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +/* + * 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) >= MAX_NAME) { + return 2; + } else { + FILE* image; + + image = fopen(filename, ""r""); + + if (image == NULL) { + return 1; + } else { + strcpy(user->profile_pic, filename); + return 0; + } + + fclose(image); + + } +} + +int print_user(const User *user) { + if (user == NULL) { + return 1; + } else { + + // Prints the profile_pic + FILE *pic; + + if (user->profile_pic[0] != '\0') { + pic = fopen(user->profile_pic, ""r""); + char line[100]; + + if (pic != NULL) { + while (fgets(line, 100, pic) != NULL) { + printf(""%s"", line); + } + printf(""\n""); + } + } + fclose(pic); + }",, +63c647675f004bf4898cf37e,2023-02-17T20:23:48.522Z,Help Write Code,v1,,,,"/* Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset. + */ +" +63c647675f004bf4898cf37e,2023-02-17T22:39:10.196Z,Explain Code,v1,,"#include + +#define SIZE 4 +#define OVERFLOW 4 + +int main() { + int index = 0; + int i; + int before[SIZE] = {10, 10, 10, 10}; + int a[SIZE] = {0, 0, 0, 0}; + int after[SIZE] = {10, 10, 10, 10}; + + printf(""Address of the variables:\n""); + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &after[%d]\n"", (unsigned long) &after[index], index); + } + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &a[%d]\n"", (unsigned long) &a[index], index); + } + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &before[%d]\n"", (unsigned long) &before[index], index); + } + printf(""%lx -> &i\n"", (unsigned long)&i); + printf(""%lx -> &index\n"", (unsigned long)&index); + printf(""\n""); + + + printf(""Initial values:\n""); + printf(""i = %d\n"", i); + printf(""before = {%d, %d, %d, %d}\n"", before[0], before[1], before[2], before[3]); + printf(""a = {%d, %d, %d, %d}\n"", a[0], a[1], a[2], a[3]); + printf(""after = {%d, %d, %d, %d}\n"", after[0], after[1], after[2], after[3]); + printf(""\n""); + + + for (i = 0; i < OVERFLOW; i++) { + a[i] = i * 10; + printf(""i = %d\n"", i); + printf(""before = {%d, %d, %d, %d}\n"", before[0], before[1], before[2], before[3]); + printf(""a = {%d, %d, %d, %d}\n"", a[0], a[1], a[2], a[3]); + printf(""after = {%d, %d, %d, %d}\n"", after[0], after[1], after[2], after[3]); + } + + return 0; +} +",, +63c647695f004bf4898cf38d,2023-01-27T21:06:10.224Z,Help Fix Code,v1,,"void fib(int **fib_sequence, int count){ + *fib_sequence = malloc(sizeof(int) * count); + 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; +}",Building a dynamic array containing the first n elements of the Fibonacci sequence, +63c647695f004bf4898cf38d,2023-01-27T21:13:00.344Z,Help Fix Code,v1,,"void fib(int **fib_sequence, int count){ + *fib_sequence = malloc(sizeof(int) * count); + 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; +}","/* + * 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. + */", +63c647695f004bf4898cf38d,2023-01-29T23:38:28.773Z,Help Fix Code,v1,," +int populate_array(int sin, int *sin_array) { + int temp = sin; + int size = 0; + while (temp != 0){ + temp /= 10; + size ++; + } + if (size != 9) { + return 1; + } + else { + for (int i = size - 1; i >= 0; i--){ + sin_array[i] = sin % 10; + sin /= 10; + } + return 0; + } +}","/* + * Convert a 9 digit int to a 9 element int array. + */ +", +63c647695f004bf4898cf38d,2023-01-29T23:39:54.539Z,Explain Code,v1,," +int populate_array(int sin, int *sin_array) { + int temp = sin; + int size = 0; + while (temp != 0){ + temp /= 10; + size ++; + } + if (size != 9) { + return 1; + } + else { + for (int i = size - 1; i >= 0; i--){ + sin_array[i] = sin % 10; + sin /= 10; + } + return 0; + } +}",, +63c647695f004bf4898cf38d,2023-02-13T01:25:44.446Z,Help Fix Code,v1,,"/* + * 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; + } + if (!are_friends(author, target)) { + return 1; + } + + Post *new_post = (Post *)malloc(sizeof(Post)); + new_post->contents = contents; + new_post->next = target->first_post; + + Post *p = author->first_post; + while (p->next != NULL){ + p = p->next; + } + p->next = new_post; + + 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 + */", +63c647695f004bf4898cf38d,2023-02-13T01:59:51.672Z,Explain Code,v1,,"/* + * 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; + } + if (!are_friends(author, target)) { + return 1; + } + + Post *new_post = (Post *)malloc(sizeof(Post)); + new_post->contents = contents; + new_post->next = target->first_post; + target->first_post = new_post; + + Post *p = author->first_post; + + while (p->next != NULL){ + p = p->next; + } + p->next = new_post; + + return 0; +}",, +63c647695f004bf4898cf38d,2023-02-13T02:07:26.663Z,Help Fix Code,v1,," * 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; + } + if (!are_friends(author, target)) { + return 1; + } + + Post *new_post = (Post *)malloc(sizeof(Post)); + new_post->author = author->name; + new_post->contents = contents; + new_post->next = target->first_post; + target->first_post = new_post; + + 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 + */", +63c647695f004bf4898cf38d,2023-02-13T20:26:37.587Z,Help Fix Code,v1,,"FILE *file; + char line[4096]; + file = fopen(user->profile_pic, ""r""); + while (fgets(line, 4096, file) != NULL) { + printf(""%s"", line); + } + fclose(file);",Open a file and print each line in the file to standard output, +63c647695f004bf4898cf38d,2023-02-13T20:54:04.454Z,Help Fix Code,v1,,"FILE *file; + char line[4096]; + char string[32]; + strncpy(string, user->profile_pic, 32); + file = fopen(string, ""r""); + while (fgets(line, 4096, file) != NULL) { + printf(""%s"", line); + } + fclose(file);","STORE the name of the file. Then you read the file and immediately print it back out. You can do this in pieces and never store the entire thing. If you think about it for a while, you will realize that you don't need to assume a maximum line length for using fgets.", +63c647695f004bf4898cf38d,2023-02-15T05:44:24.782Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + if (!are_friends(author, target)) { + return 1; + } + + Post *new_post = (Post *)malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + time_t *ltime; + ltime =(time_t*)malloc(sizeof(*ltime)); + time(ltime); + new_post->date = ltime; + new_post->contents = contents; + new_post->next = target->first_post; + target->first_post = new_post; + + free(new_post->contents); + free(new_post->date); + free(new_post); + + + 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 + */ +Free memory after use", +63c647695f004bf4898cf38d,2023-03-14T01:46:47.061Z,Help Fix Code,v2,"You will need to use fork and execvp to the run the command specified by an action. Fortunately in Part 1, you already prepared the array of arguments to pass into execvp. The parent process should wait for each child to complete. If the child terminates with a non-zero exit status, the parent should also terminate with a non-zero exit status. This means that pmake will stop when it encounters an error.","#include +#include +#include +#include +#include +#include +#include + +#include ""pmake.h"" + +void execute_action(Action *action) { + printf(""%s\n"", *action->args); + pid_t pid = fork(); + if (pid == 0) { + execvp(action->args[0], action->args); + fprintf(stderr, ""Error: execvp failed\n""); + exit(1); + } else if (pid < 0) { + fprintf(stderr, ""Error: Fork failed\n""); + exit(1); + } else { + int status; + waitpid(pid, &status, 0); + if (WIFSIGNALED(status) || WEXITSTATUS(status) != 0) { + fprintf(stderr, ""Error: Child process exited with non-zero status\n""); + exit(1); + } + } +}",, +63c6476a5f004bf4898cf397,2023-02-15T01:57:37.027Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name)>= MAX_NAME){ + return 2; + } + + //Initializing a new user with name ""name"" and .next set to NULL to be added + + User *new_user; + new_user = malloc(sizeof(struct user)); + strncpy(new_user->name, name, MAX_NAME); + new_user->first_post = NULL; + // new_user->profile_pic = NULL; + + new_user->friends = (User*)calloc(MAX_FRIENDS, sizeof(User)); + // User *friends[MAX_FRIENDS]; + // new_user->friends = friends; + + // if (friends == NULL) { + // return 2; + // } + // = friends; + new_user->next = NULL; + + + if (user_ptr_add != NULL){ + User *curr_user = *user_ptr_add; + while (curr_user->next!= NULL){ + if (strcmp(curr_user->name, name)==0){ + return 1; + } + curr_user = curr_user->next; + } + if (strcmp(curr_user->name, name)==0){ + return 1;} + curr_user->next = new_user; + return 0; + + }else{ + // struct user *new_user; + // printf(""New user initialized\n""); + // new_user = malloc(sizeof(struct user)); + // printf(""malloc\n""); + // strncpy(new_user->name, name, MAX_NAME); + // printf(""name set\n""); + // new_user->first_post = NULL; + // printf(""1st post done\n""); + // // new_user->profile_pic = NULL; + // // new_user->friends = NULL; + // new_user->next = NULL; + // printf(""set next to nill\n""); + *user_ptr_add = new_user; + return 0; + } + +}","* 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)", +63c6476b5f004bf4898cf39c,2023-01-24T01:28:52.498Z,General Question,v1,"syntax for strtol +",,, +63c6476b5f004bf4898cf39c,2023-01-24T01:29:28.471Z,General Question,v1,how does scanf work?,,, +63c6476b5f004bf4898cf39c,2023-02-01T05:51:57.934Z,General Question,v1,"how does malloc work? + +",,, +63c6476b5f004bf4898cf39c,2023-02-14T08:59:26.715Z,General Question,v1,"what does strncpy do? +",,, +63c6476b5f004bf4898cf39c,2023-02-14T09:00:30.196Z,Help Write Code,v1,,,,"How does strncpy work? +" +63c6476b5f004bf4898cf39c,2023-03-17T21:49:23.736Z,General Question,v2,"How does wait work? +",,, +63c6476b5f004bf4898cf39c,2023-03-17T21:50:12.266Z,General Question,v2,what are the attributes of the struct time_t,,, +63c6476b5f004bf4898cf39c,2023-03-23T05:00:40.129Z,General Question,v2,"how does bind work +",,, +63c6476b5f004bf4898cf39c,2023-03-23T05:01:25.269Z,Help Write Code,v2,,,,how to find a character in a character array without a null terminator +63c6476e5f004bf4898cf3b5,2023-03-23T17:49:49.910Z,Help Fix Code,v2,print hello world,"print(""Helloworld"");",, +63c6476e5f004bf4898cf3b5,2023-03-23T17:50:48.733Z,Help Fix Code,v2,print Hello world,"printf(""Hello world"");",, +63c6476e5f004bf4898cf3b5,2023-03-23T17:51:13.198Z,Help Fix Code,v2,print Hello world,"printf(""Hello world\n"");",, +63c647705f004bf4898cf3c4,2023-02-14T16:40:01.041Z,General Question,v1,"what is valgrind? +",,, +63c647705f004bf4898cf3c4,2023-02-14T16:45:08.369Z,General Question,v1,what is the best way to debug c code?,,, +63c647705f004bf4898cf3c4,2023-02-14T17:10:00.285Z,General Question,v1,"is a null pointer different from null? +",,, +63c647705f004bf4898cf3c4,2023-03-08T05:04:51.815Z,General Question,v1,"how to count number of spaces in a string +",,, +63c647705f004bf4898cf3c4,2023-03-09T17:59:17.293Z,General Question,v2,what does getopt do?,,, +63c647705f004bf4898cf3c4,2023-03-10T04:39:55.998Z,General Question,v2,what does the exec function do,,, +63c647705f004bf4898cf3c4,2023-03-10T18:23:27.712Z,General Question,v2,"how to use dup2 +",,, +63c647705f004bf4898cf3c4,2023-03-14T21:29:46.419Z,General Question,v2,"how to use stat() +",,, +63c647705f004bf4898cf3c4,2023-04-02T22:23:25.093Z,General Question,v2,what is asprintf and how do i use it,,, +63c647705f004bf4898cf3c4,2023-04-23T19:10:20.144Z,Question from Code,v2,"what is line 3 doing +","FLAGS = -Wall - g - std = gnu99 all : classifier test_load_data classifier : dectree.o classifier.o gcc ${FLAGS} - o classifier dectree.o classifier.o - + lm test_load_data : dectree.o test_load_data.o gcc ${FLAGS} - o test_load_data dectree.o test_load_data.o - + lm classifier.o : classifier.c dectree.h gcc ${FLAGS} - c classifier.c test_load_data.o : test_load_data.c dectree.h gcc ${FLAGS} - + c test_load_data.c dectree.o : dectree.c dectree.h gcc ${FLAGS} - + c dectree.c datasets : datasets.tgz tar xzf datasets.tgz.PHONY : clean all clean : rm classifier test_load_data *.o",, +63c647705f004bf4898cf3c4,2023-04-24T02:47:39.158Z,General Question,v2,"in a makefile, are actions called if a target has no dependencies?",,, +63c647705f004bf4898cf3c4,2023-04-24T16:16:33.334Z,General Question,v2,"what does wait() do? +",,, +63c647705f004bf4898cf3c4,2023-04-24T16:33:06.066Z,General Question,v2,"what does dup2() do? +",,, +63c647705f004bf4898cf3c4,2023-04-24T16:45:57.001Z,General Question,v2,how to use pipe(),,, +63c647735f004bf4898cf3e2,2023-01-18T21:53:27.099Z,General Question,v1,"What value is returned by scanf when the input stream is closed? +",,, +63c647735f004bf4898cf3e2,2023-01-18T21:53:58.520Z,General Question,v1,What is the EOF symbol in C?,,, +63c647745f004bf4898cf3e7,2023-01-27T20:03:54.977Z,Question from Code,v1,What is wrong with this code?,"#include +#include + + +void fib(int **arr, int n) { + *arr = malloc(sizeof(int) * n); + *arr[0] = 0; + int index = 1; + + if (n>=2) { + *arr[1] = 1; + index = 2; + } + + while (index < n) { + *arr[index] = *arr[index - 1] + *arr[index -2]; + index ++; + } +} + +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; +}",, +63c647745f004bf4898cf3e7,2023-01-27T20:05:51.724Z,Help Fix Code,v1,,"#include +#include + + +void fib(int **arr, int n) { + *arr = malloc(sizeof(int) * n); + *arr[0] = 0; + int index = 1; + + if (n>=2) { + *arr[1] = 1; + index = 2; + } + + while (index < n) { + *arr[index] = *arr[index - 1] + *arr[index -2]; + index ++; + } +} + +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; +}","build a dynamic array containing the first n fibonacci numbers, assuming that n is greater than or equal to 1.", +63c647745f004bf4898cf3e7,2023-01-29T02:51:54.328Z,General Question,v1,how do i get the output of a shell command in my c program?,,, +63c647745f004bf4898cf3e7,2023-01-29T04:29:29.621Z,General Question,v1,"in the c programming language, how do i skip the rest of a line using scanf once I've gotten the data I wanted to use?",,, +63c647745f004bf4898cf3e7,2023-01-29T04:31:44.137Z,General Question,v1,how do i skip the rest of the line using scanf if there are spaces remaining?,,, +63c647745f004bf4898cf3e7,2023-01-29T04:33:18.003Z,General Question,v1,"If I pipe the output of ""ls -l"" into a c program, how can I use scanf to get the size of each file?",,, +63c647745f004bf4898cf3e7,2023-01-29T04:35:59.302Z,Help Fix Code,v1,,"#include +#include + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + int size = strtol(argv[1], NULL, 10); + int total = 0; + int cur; + char permissions[11]; + scanf(""%*s %*d""); + while (scanf(""%s %*d %*s %*s %d %*s"", permissions, &cur) != EOF) { + printf(""%s %d\n"", permissions, cur); + } + + return 0; +} +","I am piping the following output into this code: +-rwx------ 1 reid staff 1734 Jun 22 14:52 prog +-rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt +-rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz + +it should output: +-rwx------1734 +-rw------- 21510 +-rwxr-xr-x 8968", +63c647745f004bf4898cf3e7,2023-01-29T04:37:28.277Z,Question from Code,v1,"I am piping the following output into this code: +-rwx------ 1 reid staff 1734 Jun 22 14:52 prog +-rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt +-rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz + +it should output: +-rwx------1734 +-rw------- 21510 +-rwxr-xr-x 8968","#include +#include + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + int size = strtol(argv[1], NULL, 10); + //int total = 0; + int cur; + char permissions[11]; + while (scanf(""%s %*d %*s %*s %d %*s"", permissions, &cur) != EOF) { + printf(""%s %d\n"", permissions, cur); + } + + return 0; +} +",, +63c647745f004bf4898cf3e7,2023-01-29T04:39:46.758Z,Help Write Code,v1,,,,"I am piping the following output into this code: +-rwx------ 1 reid staff 1734 Jun 22 14:52 prog +-rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt +-rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz + +it should use scanf to output: +-rwx------1734 +-rw------- 21510 +-rwxr-xr-x 8968" +63c647745f004bf4898cf3e7,2023-01-29T12:24:26.420Z,Question from Code,v1,"after passing sin_array into the populate_array function, how do i access the modified array?","#include +#include + +int populate_array(int, int *); +int check_sin(int *); + + +int main(int argc, char **argv) { + // TODO: Verify that command line arguments are valid. + if (argc != 2) { + return 2; + } + // TODO: Parse arguments and then call the two helpers in sin_helpers.c + // to verify the SIN given as a command line argument. + int sin_int = strtol(argv[1], NULL, 10); + int *sin_array = malloc(sizeof(int) * 9); + + if (populate_array(sin_int, sin_array) == 1) { + for (int i = 0; i < 9; i ++) { + printf(""%d\n"", sin_array[i]); + } + return 1; + } else if (check_sin(sin_array)) { + return 1; + } + + return 0; +} +",, +63c647745f004bf4898cf3e7,2023-02-15T09:45:42.310Z,Question from Code,v1,Why do I get a segmentation fault from this code?,"int create_user(const char *name, User **user_ptr_add) { + // Check length + if (strlen(name) > MAX_NAME) { + return 2; + } + + // Search for given User + if (find_user(name, *user_ptr_add) != NULL) { + return 1; + } + + // Create User + User *new_user = malloc(sizeof(User)); + strncpy(new_user->name, name, MAX_NAME); + User *curr = *user_ptr_add; + while (curr->next != NULL) { + curr = curr->next; + } + curr->next = new_user; + return 0; +}",, +63c647745f004bf4898cf3e7,2023-02-15T10:24:30.234Z,General Question,v1,"If a struct contains an attribute which is an array of pointers to another struct, do I need to malloc any new elements to the array when adding them in a helper function?",,, +63c647745f004bf4898cf3e7,2023-02-15T12:08:49.120Z,General Question,v1,"How can I use time_t and ctime to get the current date/time as a string in the following format: ""Friday Jan 27 23:01:00 2023""",,, +63c647745f004bf4898cf3e7,2023-02-15T14:33:07.070Z,Question from Code,v1,Why does this code not correctly set the friend_of_deleted->friends[j] element to NULL? I want to delete the element user_to_be_deleted from the array called friend_of_deleted->friends,"int delete_user(const char *name, User **user_ptr_del) { + + User *user_to_be_deleted = find_user(name, *user_ptr_del); + + // Check existence + if (user_to_be_deleted == NULL) { + return 1; + } + + + // Delete from main user list if first + if (*user_ptr_del == user_to_be_deleted) { + *user_ptr_del = (*user_ptr_del)->next; + return 0; + } + + + // Delete from main user list if not first + User *curr = *user_ptr_del; + while (curr->next != user_to_be_deleted) { + curr = curr->next; + } + curr->next = curr->next->next; + return 0; + + + // Delete friends + User *friend_of_deleted; + for (int i = 0; i < MAX_FRIENDS; i++) { + friend_of_deleted = find_user(user_to_be_deleted->friends[i]->name, *user_ptr_del); + for (int j = 0; j < MAX_FRIENDS - 1; j++) { + if (friend_of_deleted->friends[j] == user_to_be_deleted) { + friend_of_deleted->friends[j] = NULL; + } + } + } + + +}",, +63c647745f004bf4898cf3e7,2023-02-15T14:36:10.614Z,Question from Code,v1,Why does this code not correctly set the friend_of_deleted->friends[j] element to NULL? I want to delete the element user_to_be_deleted from the array called friend_of_deleted->friends,"int delete_user(const char *name, User **user_ptr_del) { + + User *user_to_be_deleted = find_user(name, *user_ptr_del); + + // Check existence + if (user_to_be_deleted == NULL) { + return 1; + } + + + // Delete from main user list if first + if (*user_ptr_del == user_to_be_deleted) { + *user_ptr_del = (*user_ptr_del)->next; + return 0; + } + + + // Delete from main user list if not first + User *curr = *user_ptr_del; + while (curr->next != user_to_be_deleted) { + curr = curr->next; + } + curr->next = curr->next->next; + return 0; + + + // Delete friends + User *friend_of_deleted; + for (int i = 0; i < MAX_FRIENDS; i++) { + friend_of_deleted = find_user(user_to_be_deleted->friends[i]->name, *user_ptr_del); + for (int j = 0; j < MAX_FRIENDS - 1; j++) { + if (strcmp(friend_of_deleted->friends[j]->name, user_to_be_deleted->name) == 0) { + friend_of_deleted->friends[j] = NULL; + } + } + } + + +}",, +63c647745f004bf4898cf3e7,2023-02-15T14:43:57.679Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + + User *user_to_be_deleted = find_user(name, *user_ptr_del); + + // Check existence + if (user_to_be_deleted == NULL) { + return 1; + } + + + // Delete from main user list if first + if (*user_ptr_del == user_to_be_deleted) { + *user_ptr_del = (*user_ptr_del)->next; + return 0; + } + + + // Delete from main user list if not first + User *curr = *user_ptr_del; + while (curr->next != user_to_be_deleted) { + curr = curr->next; + } + curr->next = curr->next->next; + return 0; + + + // Delete friends + User *friend_of_deleted; + for (int i = 0; i < MAX_FRIENDS; i++) { + friend_of_deleted = find_user(user_to_be_deleted->friends[i]->name, *user_ptr_del); + + int j = 0; + while (strcmp(friend_of_deleted->friends[j]->name, user_to_be_deleted->name) != 0) { + j++; + } + while (j < MAX_FRIENDS - 1) { + friend_of_deleted->friends[j] = friend_of_deleted->friends[j + 1]; + j++; + } + friend_of_deleted->friends[j] = NULL; + } + return 0; + +}",friend_of_deleted->friends contains the element user_to_be_deleted. This element should be deleted., +63c647745f004bf4898cf3e7,2023-02-15T15:56:40.862Z,Question from Code,v1,Are there any memory leaks in this code?,"#include ""friends.h"" +#include +#include +#include + +int check_friends_length(User *checked_user); +void add_friends(User *user1, User *user2); +int check_friendship(User *user1, User *user2); +int remove_from_friends(User *unfriendee, User *unfriender); + +/* + * 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) { + // Check length + if (strlen(name) > MAX_NAME) { + return 2; + } + + // Search for given User + User *user_to_check = find_user(name, *user_ptr_add); + if (user_to_check != NULL) { + return 1; + } + + // Create User + User *new_user = malloc(sizeof(User)); + strncpy(new_user->name, name, MAX_NAME); + + // If list is empty, initialize it + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + // If list is not empty, add a User to the end of it + User *curr = *user_ptr_add; + while (curr->next != NULL) { + curr = curr->next; + } + curr->next = new_user; + 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(curr->name, name) == 0) { + return curr; + } + curr = curr->next; + } + return curr; +} + + +/* + * 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) { + while (curr != NULL) { + printf(""%s\n"", curr->name); + curr = curr->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) { + // Check length + if (strlen(filename) > MAX_NAME) { + return 2; + } + + FILE *given_file = fopen(filename, ""r""); + if (given_file == NULL) { + return 1; + } + fclose(given_file); + + strncpy(user->profile_pic, filename, MAX_NAME); + + return 0; +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int make_friends(const char *name1, const char *name2, User *head) { + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + + // Check existence + if (user1 == NULL || user2 == NULL) { + return 4; + } + + // Check equivalence + if (strcmp(name1, name2) == 0) { + return 3; + } + + // Check friend list lengths + if (check_friends_length(user1) == MAX_FRIENDS || check_friends_length(user2) == MAX_FRIENDS) { + return 2; + } + + // Check existing friendship + if (check_friendship(user1, user2) == 0) { + return 1; + } + + // Add friends + add_friends(user1, user2); + return 0; + +} + +int check_friends_length(User *checked_user) { + //HELPER + // return number of friends + + int counter = 0; + + for (int i = 0; i < MAX_FRIENDS; i++) { + if (checked_user->friends[i] != NULL) { + counter++; + } + } + return counter; +} + +void add_friends(User *user1, User *user2) { + // HELPER + // PRECONDITION: Users have room in their friend's list + + int i = 0; + while (user1->friends[i] != NULL) { + i++; + } + user1->friends[i] = user2; + + i = 0; + while (user2->friends[i] != NULL) { + i++; + } + user2->friends[i] = user1; +} + +int check_friendship(User *user1, User *user2) { + // HELPER + // Return 0 if friends, return 1 if not friends + + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == user2) { + return 0; + } + } + return 1; +} + + +/* + * 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) { + // Check if User exists + if (user == NULL) { + return 1; + } + + + // Print photo if photo exists + FILE *profile_photo = fopen(user->profile_pic, ""r""); + char c; + if (profile_photo) { + while ((c = getc(profile_photo)) != EOF) + printf(""%c"", c); + printf(""\n""); + } + fclose(profile_photo); + + // Print details + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + for (int i = 0; i < 10; i++) { + if (user->friends[i] != NULL){ + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts:\n""); + Post *curr = user->first_post; + while(curr != NULL) { + printf(""From: %s\n"", curr->author); + printf(""Date: %s\n"", ctime(curr->date)); + printf(""%s\n"", curr->contents); + curr = curr->next; + if (curr != NULL) { + printf(""\n===\n\n""); + } + } + printf(""------------------------------------------\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) { + + // Check if Users exist + if (author == NULL || target == NULL) { + return 2; + } + + + // Check if Users are friends + if (check_friendship((User *) author, target) == 1) { + return 1; + } + + + // Create Post + Post *new_post = malloc(sizeof(Post)); + strncpy(new_post->author, author->name, MAX_NAME); + new_post->contents = contents; + + + // Initialize time created + time_t *time_ptr = malloc(sizeof(time_t)); + *time_ptr = time(NULL); + new_post->date = time_ptr; + + + + // // Check if a first post exists + if (target->first_post == NULL) { + target->first_post = new_post; + return 0; + } + + + // // Add post to the front if others already exist + Post *old_first = target->first_post; + new_post->next = old_first; + target->first_post = new_post; + return 0; +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ +int delete_user(const char *name, User **user_ptr_del) { + + User *user_to_be_deleted = find_user(name, *user_ptr_del); + + // Check existence + if (user_to_be_deleted == NULL) { + return 1; + } + + // Delete friends + User *friend_of_deleted; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user_to_be_deleted->friends[i] != NULL) { + friend_of_deleted = find_user(user_to_be_deleted->friends[i]->name, *user_ptr_del); + remove_from_friends(user_to_be_deleted, friend_of_deleted); + } + } + + + // Delete from main user list if first + if (*user_ptr_del == user_to_be_deleted) { + *user_ptr_del = (*user_ptr_del)->next; + return 0; + } + + + // Delete from main user list if not first + User *curr = *user_ptr_del; + while (curr->next != user_to_be_deleted) { + curr = curr->next; + } + curr->next = curr->next->next; + + + // Free memory from posts sent to this user + if (user_to_be_deleted->first_post != NULL) { + Post *curr_post = user_to_be_deleted->first_post; + Post *next_post = curr_post->next; + while (next_post != NULL){ + free(curr_post->date); + free(curr_post); + curr_post = next_post; + next_post = next_post->next; + } + free(curr_post->date); + free(curr_post); + } + free(user_to_be_deleted); + + return 0; +} + +int remove_from_friends(User *unfriendee, User *unfriender) { + // HELPER + // Returns 0 when successfully unfriended and returns 1 when not found in friends list + for (int i = 0; i < MAX_FRIENDS; i++) { + if (unfriender->friends[i] == unfriendee) { + unfriender->friends[i] = NULL; + return 0; + } + } + return 1; +} + + +",, +63c647745f004bf4898cf3e7,2023-02-15T16:05:10.781Z,Question from Code,v1,Are there any memory leaks or segmentation faults in this code? ,"#include ""friends.h"" +#include +#include +#include + +int check_friends_length(User *checked_user); +void add_friends(User *user1, User *user2); +int check_friendship(User *user1, User *user2); +int remove_from_friends(User *unfriendee, User *unfriender); + +/* + * 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) { + // Check length + if (strlen(name) > MAX_NAME) { + return 2; + } + + // Search for given User + User *user_to_check = find_user(name, *user_ptr_add); + if (user_to_check != NULL) { + return 1; + } + + // Create User + User *new_user = malloc(sizeof(User)); + strncpy(new_user->name, name, MAX_NAME); + + // If list is empty, initialize it + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + // If list is not empty, add a User to the end of it + User *curr = *user_ptr_add; + while (curr->next != NULL) { + curr = curr->next; + } + curr->next = new_user; + 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(curr->name, name) == 0) { + return curr; + } + curr = curr->next; + } + return curr; +} + + +/* + * 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) { + while (curr != NULL) { + printf(""%s\n"", curr->name); + curr = curr->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) { + // Check length + if (strlen(filename) > MAX_NAME) { + return 2; + } + + FILE *given_file = fopen(filename, ""r""); + if (given_file == NULL) { + return 1; + } + fclose(given_file); + + strncpy(user->profile_pic, filename, MAX_NAME); + + return 0; +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int make_friends(const char *name1, const char *name2, User *head) { + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + + // Check existence + if (user1 == NULL || user2 == NULL) { + return 4; + } + + // Check equivalence + if (strcmp(name1, name2) == 0) { + return 3; + } + + // Check friend list lengths + if (check_friends_length(user1) == MAX_FRIENDS || check_friends_length(user2) == MAX_FRIENDS) { + return 2; + } + + // Check existing friendship + if (check_friendship(user1, user2) == 0) { + return 1; + } + + // Add friends + add_friends(user1, user2); + return 0; + +} + +int check_friends_length(User *checked_user) { + //HELPER + // return number of friends + + int counter = 0; + + for (int i = 0; i < MAX_FRIENDS; i++) { + if (checked_user->friends[i] != NULL) { + counter++; + } + } + return counter; +} + +void add_friends(User *user1, User *user2) { + // HELPER + // PRECONDITION: Users have room in their friend's list + + int i = 0; + while (user1->friends[i] != NULL) { + i++; + } + user1->friends[i] = user2; + + i = 0; + while (user2->friends[i] != NULL) { + i++; + } + user2->friends[i] = user1; +} + +int check_friendship(User *user1, User *user2) { + // HELPER + // Return 0 if friends, return 1 if not friends + + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == user2) { + return 0; + } + } + return 1; +} + + +/* + * 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) { + // Check if User exists + if (user == NULL) { + return 1; + } + + + // Print photo if photo exists + FILE *profile_photo = fopen(user->profile_pic, ""r""); + char c; + if (profile_photo) { + while ((c = getc(profile_photo)) != EOF) + printf(""%c"", c); + printf(""\n""); + } + fclose(profile_photo); + + // Print details + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + for (int i = 0; i < 10; i++) { + if (user->friends[i] != NULL){ + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts:\n""); + Post *curr = user->first_post; + while(curr != NULL) { + printf(""From: %s\n"", curr->author); + printf(""Date: %s\n"", ctime(curr->date)); + printf(""%s\n"", curr->contents); + curr = curr->next; + if (curr != NULL) { + printf(""\n===\n\n""); + } + } + printf(""------------------------------------------\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) { + + // Check if Users exist + if (author == NULL || target == NULL) { + return 2; + } + + + // Check if Users are friends + if (check_friendship((User *) author, target) == 1) { + return 1; + } + + + // Create Post + Post *new_post = malloc(sizeof(Post)); + strncpy(new_post->author, author->name, MAX_NAME); + new_post->contents = contents; + + + // Initialize time created + time_t *time_ptr = malloc(sizeof(time_t)); + *time_ptr = time(NULL); + new_post->date = time_ptr; + + + + // // Check if a first post exists + if (target->first_post == NULL) { + target->first_post = new_post; + return 0; + } + + + // // Add post to the front if others already exist + Post *old_first = target->first_post; + new_post->next = old_first; + target->first_post = new_post; + return 0; +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ +int delete_user(const char *name, User **user_ptr_del) { + + User *user_to_be_deleted = find_user(name, *user_ptr_del); + + // Check existence + if (user_to_be_deleted == NULL) { + return 1; + } + + // Delete friends + User *friend_of_deleted; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user_to_be_deleted->friends[i] != NULL) { + friend_of_deleted = find_user(user_to_be_deleted->friends[i]->name, *user_ptr_del); + remove_from_friends(user_to_be_deleted, friend_of_deleted); + } + } + + + // Delete from main user list if first + if (*user_ptr_del == user_to_be_deleted) { + *user_ptr_del = (*user_ptr_del)->next; + } else { + // Delete from main user list if not first + User *curr = *user_ptr_del; + while (curr->next != user_to_be_deleted) { + curr = curr->next; + } + curr->next = curr->next->next; + } + + + // Free memory from posts sent to this user + if (user_to_be_deleted->first_post != NULL) { + Post *curr_post = user_to_be_deleted->first_post; + Post *next_post = curr_post->next; + while (next_post != NULL){ + free(curr_post->date); + free(curr_post->contents); + free(curr_post); + curr_post = next_post; + next_post = next_post->next; + } + free(curr_post->date); + free(curr_post->contents); + free(curr_post); + } + free(user_to_be_deleted); + + return 0; +} + +int remove_from_friends(User *unfriendee, User *unfriender) { + // HELPER + // Returns 0 when successfully unfriended and returns 1 when not found in friends list + for (int i = 0; i < MAX_FRIENDS; i++) { + if (unfriender->friends[i] == unfriendee) { + unfriender->friends[i] = NULL; + return 0; + } + } + return 1; +} + + +",, +63c647745f004bf4898cf3e7,2023-02-15T16:10:48.577Z,Question from Code,v1,Are there any segmentation faults? WHich line is it on?,"#include ""friends.h"" +#include +#include +#include + +int check_friends_length(User *checked_user); +void add_friends(User *user1, User *user2); +int check_friendship(User *user1, User *user2); +int remove_from_friends(User *unfriendee, User *unfriender); + +/* + * 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) { + // Check length + if (strlen(name) > MAX_NAME) { + return 2; + } + + // Search for given User + User *user_to_check = find_user(name, *user_ptr_add); + if (user_to_check != NULL) { + return 1; + } + + // Create User + User *new_user = malloc(sizeof(User)); + strncpy(new_user->name, name, MAX_NAME); + + // If list is empty, initialize it + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + // If list is not empty, add a User to the end of it + User *curr = *user_ptr_add; + while (curr->next != NULL) { + curr = curr->next; + } + curr->next = new_user; + 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(curr->name, name) == 0) { + return curr; + } + curr = curr->next; + } + return curr; +} + + +/* + * 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) { + while (curr != NULL) { + printf(""%s\n"", curr->name); + curr = curr->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) { + // Check length + if (strlen(filename) > MAX_NAME) { + return 2; + } + + FILE *given_file = fopen(filename, ""r""); + if (given_file == NULL) { + return 1; + } + fclose(given_file); + + strncpy(user->profile_pic, filename, MAX_NAME); + + return 0; +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int make_friends(const char *name1, const char *name2, User *head) { + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + + // Check existence + if (user1 == NULL || user2 == NULL) { + return 4; + } + + // Check equivalence + if (strcmp(name1, name2) == 0) { + return 3; + } + + // Check friend list lengths + if (check_friends_length(user1) == MAX_FRIENDS || check_friends_length(user2) == MAX_FRIENDS) { + return 2; + } + + // Check existing friendship + if (check_friendship(user1, user2) == 0) { + return 1; + } + + // Add friends + add_friends(user1, user2); + return 0; + +} + +int check_friends_length(User *checked_user) { + //HELPER + // return number of friends + + int counter = 0; + + for (int i = 0; i < MAX_FRIENDS; i++) { + if (checked_user->friends[i] != NULL) { + counter++; + } + } + return counter; +} + +void add_friends(User *user1, User *user2) { + // HELPER + // PRECONDITION: Users have room in their friend's list + + int i = 0; + while (user1->friends[i] != NULL) { + i++; + } + user1->friends[i] = user2; + + i = 0; + while (user2->friends[i] != NULL) { + i++; + } + user2->friends[i] = user1; +} + +int check_friendship(User *user1, User *user2) { + // HELPER + // Return 0 if friends, return 1 if not friends + + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == user2) { + return 0; + } + } + return 1; +} + + +/* + * 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) { + // Check if User exists + if (user == NULL) { + return 1; + } + + + // Print photo if photo exists + FILE *profile_photo = fopen(user->profile_pic, ""r""); + char c; + if (profile_photo) { + while ((c = getc(profile_photo)) != EOF) + printf(""%c"", c); + printf(""\n""); + } + fclose(profile_photo); + + // Print details + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + for (int i = 0; i < 10; i++) { + if (user->friends[i] != NULL){ + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts:\n""); + Post *curr = user->first_post; + while(curr != NULL) { + printf(""From: %s\n"", curr->author); + printf(""Date: %s\n"", ctime(curr->date)); + printf(""%s\n"", curr->contents); + curr = curr->next; + if (curr != NULL) { + printf(""\n===\n\n""); + } + } + printf(""------------------------------------------\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) { + + // Check if Users exist + if (author == NULL || target == NULL) { + return 2; + } + + + // Check if Users are friends + if (check_friendship((User *) author, target) == 1) { + return 1; + } + + + // Create Post + Post *new_post = malloc(sizeof(Post)); + strncpy(new_post->author, author->name, MAX_NAME); + new_post->contents = contents; + + + // Initialize time created + time_t *time_ptr = malloc(sizeof(time_t)); + *time_ptr = time(NULL); + new_post->date = time_ptr; + + + + // // Check if a first post exists + if (target->first_post == NULL) { + target->first_post = new_post; + return 0; + } + + + // // Add post to the front if others already exist + Post *old_first = target->first_post; + new_post->next = old_first; + target->first_post = new_post; + return 0; +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ +int delete_user(const char *name, User **user_ptr_del) { + + User *user_to_be_deleted = find_user(name, *user_ptr_del); + + // Check existence + if (user_to_be_deleted == NULL) { + return 1; + } + + // Delete friends + User *friend_of_deleted; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user_to_be_deleted->friends[i] != NULL) { + friend_of_deleted = find_user(user_to_be_deleted->friends[i]->name, *user_ptr_del); + remove_from_friends(user_to_be_deleted, friend_of_deleted); + } + } + + + // Delete from main user list if first + if (*user_ptr_del == user_to_be_deleted) { + *user_ptr_del = (*user_ptr_del)->next; + } else { + // Delete from main user list if not first + User *curr = *user_ptr_del; + while (curr->next != user_to_be_deleted) { + curr = curr->next; + } + curr->next = curr->next->next; + } + + + // Free memory from posts sent to this user + if (user_to_be_deleted->first_post != NULL) { + Post *curr_post = user_to_be_deleted->first_post; + Post *next_post = curr_post->next; + while (next_post != NULL){ + free(curr_post->date); + free(curr_post->contents); + free(curr_post); + curr_post = next_post; + next_post = next_post->next; + } + free(curr_post->date); + free(curr_post->contents); + free(curr_post); + } + free(user_to_be_deleted); + + return 0; +} + +void remove_from_friends(User *unfriendee, User *unfriender) { + // HELPER + // Returns 0 when successfully unfriended and returns 1 when not found in friends list + for (int i = 0; i < MAX_FRIENDS; i++) { + if (unfriender->friends[i] == unfriendee) { + unfriender->friends[i] = NULL; + } + } +} + + +",, +63c647745f004bf4898cf3e7,2023-02-15T16:12:53.120Z,Help Fix Code,v1,,"#include ""friends.h"" +#include +#include +#include + +int check_friends_length(User *checked_user); +void add_friends(User *user1, User *user2); +int check_friendship(User *user1, User *user2); +int remove_from_friends(User *unfriendee, User *unfriender); + +/* + * 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) { + // Check length + if (strlen(name) > MAX_NAME) { + return 2; + } + + // Search for given User + User *user_to_check = find_user(name, *user_ptr_add); + if (user_to_check != NULL) { + return 1; + } + + // Create User + User *new_user = malloc(sizeof(User)); + strncpy(new_user->name, name, MAX_NAME); + + // If list is empty, initialize it + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + // If list is not empty, add a User to the end of it + User *curr = *user_ptr_add; + while (curr->next != NULL) { + curr = curr->next; + } + curr->next = new_user; + 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(curr->name, name) == 0) { + return curr; + } + curr = curr->next; + } + return curr; +} + + +/* + * 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) { + while (curr != NULL) { + printf(""%s\n"", curr->name); + curr = curr->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) { + // Check length + if (strlen(filename) > MAX_NAME) { + return 2; + } + + FILE *given_file = fopen(filename, ""r""); + if (given_file == NULL) { + return 1; + } + fclose(given_file); + + strncpy(user->profile_pic, filename, MAX_NAME); + + return 0; +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int make_friends(const char *name1, const char *name2, User *head) { + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + + // Check existence + if (user1 == NULL || user2 == NULL) { + return 4; + } + + // Check equivalence + if (strcmp(name1, name2) == 0) { + return 3; + } + + // Check friend list lengths + if (check_friends_length(user1) == MAX_FRIENDS || check_friends_length(user2) == MAX_FRIENDS) { + return 2; + } + + // Check existing friendship + if (check_friendship(user1, user2) == 0) { + return 1; + } + + // Add friends + add_friends(user1, user2); + return 0; + +} + +int check_friends_length(User *checked_user) { + //HELPER + // return number of friends + + int counter = 0; + + for (int i = 0; i < MAX_FRIENDS; i++) { + if (checked_user->friends[i] != NULL) { + counter++; + } + } + return counter; +} + +void add_friends(User *user1, User *user2) { + // HELPER + // PRECONDITION: Users have room in their friend's list + + int i = 0; + while (user1->friends[i] != NULL) { + i++; + } + user1->friends[i] = user2; + + i = 0; + while (user2->friends[i] != NULL) { + i++; + } + user2->friends[i] = user1; +} + +int check_friendship(User *user1, User *user2) { + // HELPER + // Return 0 if friends, return 1 if not friends + + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == user2) { + return 0; + } + } + return 1; +} + + +/* + * 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) { + // Check if User exists + if (user == NULL) { + return 1; + } + + + // Print photo if photo exists + FILE *profile_photo = fopen(user->profile_pic, ""r""); + char c; + if (profile_photo) { + while ((c = getc(profile_photo)) != EOF) + printf(""%c"", c); + printf(""\n""); + } + fclose(profile_photo); + + // Print details + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + for (int i = 0; i < 10; i++) { + if (user->friends[i] != NULL){ + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts:\n""); + Post *curr = user->first_post; + while(curr != NULL) { + printf(""From: %s\n"", curr->author); + printf(""Date: %s\n"", ctime(curr->date)); + printf(""%s\n"", curr->contents); + curr = curr->next; + if (curr != NULL) { + printf(""\n===\n\n""); + } + } + printf(""------------------------------------------\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) { + + // Check if Users exist + if (author == NULL || target == NULL) { + return 2; + } + + + // Check if Users are friends + if (check_friendship((User *) author, target) == 1) { + return 1; + } + + + // Create Post + Post *new_post = malloc(sizeof(Post)); + strncpy(new_post->author, author->name, MAX_NAME); + new_post->contents = contents; + + + // Initialize time created + time_t *time_ptr = malloc(sizeof(time_t)); + *time_ptr = time(NULL); + new_post->date = time_ptr; + + + + // // Check if a first post exists + if (target->first_post == NULL) { + target->first_post = new_post; + return 0; + } + + + // // Add post to the front if others already exist + Post *old_first = target->first_post; + new_post->next = old_first; + target->first_post = new_post; + return 0; +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ +int delete_user(const char *name, User **user_ptr_del) { + + User *user_to_be_deleted = find_user(name, *user_ptr_del); + + // Check existence + if (user_to_be_deleted == NULL) { + return 1; + } + + // Delete friends + User *friend_of_deleted; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user_to_be_deleted->friends[i] != NULL) { + friend_of_deleted = find_user(user_to_be_deleted->friends[i]->name, *user_ptr_del); + remove_from_friends(user_to_be_deleted, friend_of_deleted); + } + } + + + // Delete from main user list if first + if (*user_ptr_del == user_to_be_deleted) { + *user_ptr_del = (*user_ptr_del)->next; + } else { + // Delete from main user list if not first + User *curr = *user_ptr_del; + while (curr->next != user_to_be_deleted) { + curr = curr->next; + } + curr->next = curr->next->next; + } + + + // Free memory from posts sent to this user + if (user_to_be_deleted->first_post != NULL) { + Post *curr_post = user_to_be_deleted->first_post; + Post *next_post = curr_post->next; + while (next_post != NULL){ + free(curr_post->date); + free(curr_post->contents); + free(curr_post); + curr_post = next_post; + next_post = next_post->next; + } + free(curr_post->date); + free(curr_post->contents); + free(curr_post); + } + free(user_to_be_deleted); + + return 0; +} + +void remove_from_friends(User *unfriendee, User *unfriender) { + // HELPER + // Returns 0 when successfully unfriended and returns 1 when not found in friends list + for (int i = 0; i < MAX_FRIENDS; i++) { + if (unfriender->friends[i] == unfriendee) { + unfriender->friends[i] = NULL; + } + } +} + + +",Can you fix the segmentation fault?, +63c647745f004bf4898cf3e7,2023-02-15T16:15:52.321Z,Question from Code,v1,Where is the segmentation fault exactly? I cannot find it nor how to fix it.,"#include ""friends.h"" +#include +#include +#include + +int check_friends_length(User *checked_user); +void add_friends(User *user1, User *user2); +int check_friendship(User *user1, User *user2); +int remove_from_friends(User *unfriendee, User *unfriender); + +/* + * 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) { + // Check length + if (strlen(name) > MAX_NAME) { + return 2; + } + + // Search for given User + User *user_to_check = find_user(name, *user_ptr_add); + if (user_to_check != NULL) { + return 1; + } + + // Create User + User *new_user = malloc(sizeof(User)); + strncpy(new_user->name, name, MAX_NAME); + + // If list is empty, initialize it + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + // If list is not empty, add a User to the end of it + User *curr = *user_ptr_add; + while (curr->next != NULL) { + curr = curr->next; + } + curr->next = new_user; + 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(curr->name, name) == 0) { + return curr; + } + curr = curr->next; + } + return curr; +} + + +/* + * 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) { + while (curr != NULL) { + printf(""%s\n"", curr->name); + curr = curr->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) { + // Check length + if (strlen(filename) > MAX_NAME) { + return 2; + } + + FILE *given_file = fopen(filename, ""r""); + if (given_file == NULL) { + return 1; + } + fclose(given_file); + + strncpy(user->profile_pic, filename, MAX_NAME); + + return 0; +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int make_friends(const char *name1, const char *name2, User *head) { + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + + // Check existence + if (user1 == NULL || user2 == NULL) { + return 4; + } + + // Check equivalence + if (strcmp(name1, name2) == 0) { + return 3; + } + + // Check friend list lengths + if (check_friends_length(user1) == MAX_FRIENDS || check_friends_length(user2) == MAX_FRIENDS) { + return 2; + } + + // Check existing friendship + if (check_friendship(user1, user2) == 0) { + return 1; + } + + // Add friends + add_friends(user1, user2); + return 0; + +} + +int check_friends_length(User *checked_user) { + //HELPER + // return number of friends + + int counter = 0; + + for (int i = 0; i < MAX_FRIENDS; i++) { + if (checked_user->friends[i] != NULL) { + counter++; + } + } + return counter; +} + +void add_friends(User *user1, User *user2) { + // HELPER + // PRECONDITION: Users have room in their friend's list + + int i = 0; + while (user1->friends[i] != NULL) { + i++; + } + user1->friends[i] = user2; + + i = 0; + while (user2->friends[i] != NULL) { + i++; + } + user2->friends[i] = user1; +} + +int check_friendship(User *user1, User *user2) { + // HELPER + // Return 0 if friends, return 1 if not friends + + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == user2) { + return 0; + } + } + return 1; +} + + +/* + * 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) { + // Check if User exists + if (user == NULL) { + return 1; + } + + + // Print photo if photo exists + FILE *profile_photo = fopen(user->profile_pic, ""r""); + char c; + if (profile_photo) { + while ((c = getc(profile_photo)) != EOF) + printf(""%c"", c); + printf(""\n""); + } + fclose(profile_photo); + + // Print details + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + for (int i = 0; i < 10; i++) { + if (user->friends[i] != NULL){ + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts:\n""); + Post *curr = user->first_post; + while(curr != NULL) { + printf(""From: %s\n"", curr->author); + printf(""Date: %s\n"", ctime(curr->date)); + printf(""%s\n"", curr->contents); + curr = curr->next; + if (curr != NULL) { + printf(""\n===\n\n""); + } + } + printf(""------------------------------------------\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) { + + // Check if Users exist + if (author == NULL || target == NULL) { + return 2; + } + + + // Check if Users are friends + if (check_friendship((User *) author, target) == 1) { + return 1; + } + + + // Create Post + Post *new_post = malloc(sizeof(Post)); + strncpy(new_post->author, author->name, MAX_NAME); + new_post->contents = contents; + + + // Initialize time created + time_t *time_ptr = malloc(sizeof(time_t)); + *time_ptr = time(NULL); + new_post->date = time_ptr; + + + + // // Check if a first post exists + if (target->first_post == NULL) { + target->first_post = new_post; + return 0; + } + + + // // Add post to the front if others already exist + Post *old_first = target->first_post; + new_post->next = old_first; + target->first_post = new_post; + return 0; +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ +int delete_user(const char *name, User **user_ptr_del) { + + User *user_to_be_deleted = find_user(name, *user_ptr_del); + + // Check existence + if (user_to_be_deleted == NULL) { + return 1; + } + + // Delete friends + User *friend_of_deleted; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user_to_be_deleted->friends[i] != NULL) { + friend_of_deleted = find_user(user_to_be_deleted->friends[i]->name, *user_ptr_del); + remove_from_friends(user_to_be_deleted, friend_of_deleted); + } + } + + + // Delete from main user list if first + if (*user_ptr_del == user_to_be_deleted) { + *user_ptr_del = (*user_ptr_del)->next; + } else { + // Delete from main user list if not first + User *curr = *user_ptr_del; + while (curr->next != user_to_be_deleted) { + curr = curr->next; + } + curr->next = curr->next->next; + } + + + // Free memory from posts sent to this user + if (user_to_be_deleted->first_post != NULL) { + Post *curr_post = user_to_be_deleted->first_post; + Post *next_post = curr_post->next; + while (next_post != NULL){ + free(curr_post->date); + free(curr_post->contents); + free(curr_post); + curr_post = next_post; + next_post = next_post->next; + } + free(curr_post->date); + free(curr_post->contents); + free(curr_post); + } + free(user_to_be_deleted); + + return 0; +} + +void remove_from_friends(User *unfriendee, User *unfriender) { + // HELPER + // Returns 0 when successfully unfriended and returns 1 when not found in friends list + for (int i = 0; i < MAX_FRIENDS; i++) { + if (unfriender->friends[i] == unfriendee) { + unfriender->friends[i] = NULL; + } + } +} + + +",, +63c647745f004bf4898cf3e7,2023-03-15T18:55:00.721Z,Help Write Code,v2,,,,"allocate memory to a node and then set it's value to ""hello""" +63c647745f004bf4898cf3e7,2023-03-15T19:24:26.940Z,Help Fix Code,v2,"parse_file should create rule_node and/or action_node from the given file fp. fp will have the following format: +main : linked_list.o main.o + gcc -Wall -g -std=gnu99 -o main linked_list.o main.o + +main.o : main.c linked_list.h + gcc -Wall -g -std=gnu99 -c main.c + +linked_list.o : linked_list.c linked_list.h + gcc -Wall -g -std=gnu99 -c linked_list.c","#define MAXLINE 256 + +typedef struct action_node { + char **args; // An array of strings suitable to be passed to execvp + struct action_node *next_act; +} Action; + +typedef struct dep_node { + struct rule_node *rule; + struct dep_node *next_dep; +} Dependency; + +typedef struct rule_node { + char *target; + Dependency *dependencies; + Action *actions; + struct rule_node *next_rule; +} Rule; + +int is_comment_or_empty(const char *line) { + for (int i = 0; i < strlen(line); i++){ + if (line[i] == '#') { + return 1; + } + if (line[i] != '\t' && line[i] != ' ') { + return 0; + } + } + return 1; +} + +/* 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) { + // Implement this function and remove the stubbed return statement below. + + // Create the rules linked_list + Rule rules = malloc(sizeof(Rule)); + Rule *cur_rule = &rules; + + + // Read each line in the file + char line[MAXLINE + 1]; + while (fgets(line, MAXLINE + 1, fp) != NULL) { + if (is_comment_or_empty(line) == 1) { + continue; + } + + if( myString[0] == ' ' || myString[0] == '\t' ){ + // Create action_node + + // Insert node to the end of rules + } else { + // create rule_node + + // Insert node to the end of rules + } + + } +}",, +63c647745f004bf4898cf3e7,2023-03-15T19:59:40.354Z,Help Fix Code,v2,count the amount of words in string,"#include +#include +#include +#include +#include + + +int count_words(char *sentence) { + int count = 0; + + char* token = strtok(sentence, "" ""); + while (token) { + token++; + token = strtok(NULL, "" ""); + } + + return count; +} + +int main() { + int n; + n = count_words(""gcc -Wall -g -std=gnu99 -o main linked_list.o main.o""); + printf(""%d\n"", n); + + return 0; +} + +",, +63c647745f004bf4898cf3e7,2023-03-15T20:00:42.237Z,Help Fix Code,v2, count the amount of words in string,"#include +#include +#include +#include +#include + + +int count_words(char *sentence) { + int count = 0; + + char* token = strtok(sentence, "" ""); + while (token) { + count++; + token = strtok(NULL, "" ""); + } + + return count; +} + +int main() { + int n; + n = count_words(""gcc -Wall -g -std=gnu99 -o main linked_list.o main.o""); + printf(""%d\n"", n); + + return 0; +} + +",, +63c647745f004bf4898cf3e7,2023-03-15T20:03:33.345Z,Help Write Code,v2,,,,function to count the number of words in a string +63c647745f004bf4898cf3e7,2023-03-15T20:18:56.578Z,Question from Code,v2,why does this return a bus error,"#include +#include +#include +#include +#include + +int count_words(char *sentence) { + int count = 0; + char *token = strtok(sentence, "" ""); + while (token) { + count++; + printf(""token: %s\n"", token); + token = strtok(NULL, "" ""); + } + + return count; +} + +int main() { + char *sentence = ""main.o : main.c linked_list.h ""; + + int n = count_words(sentence); + + printf(""%d\n"", n); + + return 0; +}",, +63c647785f004bf4898cf400,2023-02-10T22:53:28.203Z,Help Fix Code,v1,,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + + struct pixel** pixel_array = malloc(height * sizeof(struct pixel*)); + + struct pixel* first_pixel_row = malloc(width * sizeof(struct pixel)); + for (int k = 0; k < width; k+= 3) { + struct pixel* pixie = malloc(3); + fseek(image, pixel_array_offset + k, SEEK_SET); + fread(&pixie->blue, 1, 1, image); + fseek(image, pixel_array_offset + k + 1, SEEK_SET); + fread(&pixie->green, 1, 1, image); + fseek(image, pixel_array_offset + k + 2, SEEK_SET); + fread(&pixie->red, 1, 1, image); + first_pixel_row[k] = *pixie; + } + pixel_array[0] = first_pixel_row; + + for (int i = 1; i < height; i++) { + struct pixel* pixel_row = malloc(width * sizeof(struct pixel)); + for (int j = 0; j < width; i+= 3) { + struct pixel* pixie = malloc(3); + fseek(image, i * pixel_array_offset + j, SEEK_SET); + fread(&pixie->blue, 1, 1, image); + fseek(image, i * pixel_array_offset + j + 1, SEEK_SET); + fread(&pixie->green, 1, 1, image); + fseek(image, i * pixel_array_offset + j + 2, SEEK_SET); + fread(&pixie->red, 1, 1, image); + pixel_row[j] = *pixie; + } + pixel_array[i] = pixel_row; + } + return pixel_array; +}","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. + * 4. Return the address of the first struct pixel * you initialized.", +63c647785f004bf4898cf400,2023-02-10T23:18:17.472Z,Help Fix Code,v1,,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + + struct pixel** pixel_array = malloc(height * sizeof(struct pixel*)); + + for (int i = 0; i < height; i++) { + struct pixel* pixie_row = malloc(width * sizeof(struct pixel)); + for (int j = 0; j + 3 < width; j+= 3) { + struct pixel* pixie = malloc(3); + fseek(image, pixel_array_offset + j + (width * i), SEEK_SET); + fread(&pixie->blue, 1, 1, image); + fseek(image, pixel_array_offset + j + (width * i) + 1, SEEK_SET); + fread(&pixie->green, 1, 1, image); + fseek(image, pixel_array_offset + j + (width * i) + 2, SEEK_SET); + fread(&pixie->red, 1, 1, image); + pixie_row[j] = *pixie; + // printf(""row %d, column %d\n"", i, j); + } + pixel_array[i] = pixie_row; + } + + return pixel_array; +}","Each group of 3 bytes represents a single pixel, where the bytes store the blue, green, and red colour values of the pixel, in that order. +Pixels are grouped by row. +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. +4. Return the address of the first struct pixel * you initialized.", +63c647785f004bf4898cf400,2023-02-10T23:20:18.682Z,Help Fix Code,v1,," +struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + + struct pixel** pixel_array = malloc(height * sizeof(struct pixel*)); + + for (int i = 0; i < height; i++) { + struct pixel* pixie_row = malloc(width * sizeof(struct pixel)); + for (int j = 0; j + 4 < width; j+= 4) { + struct pixel* pixie = malloc(3); + fseek(image, pixel_array_offset + j + (width * i), SEEK_SET); + fread(&pixie->blue, 1, 1, image); + fseek(image, pixel_array_offset + j + (width * i) + 1, SEEK_SET); + fread(&pixie->green, 1, 1, image); + fseek(image, pixel_array_offset + j + (width * i) + 2, SEEK_SET); + fread(&pixie->red, 1, 1, image); + pixie_row[j] = *pixie; + // printf(""row %d, column %d\n"", i, j); + } + pixel_array[i] = pixie_row; + } + + return &pixel_array[0]; +}","Each group of 3 bytes represents a single pixel, where the bytes store the blue, green, and red colour values of the pixel, in that order. Pixels are grouped by row. 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. 4. Return the address of the first struct pixel * you initialized.", +63c647785f004bf4898cf400,2023-03-10T23:22:37.737Z,Explain Code,v2,,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + if (fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if (fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + if (strlen(password) > 9) { + perror(""password too long""); + exit(1); + } + + if (strlen(user_id) > 9) { + perror(""user id too long""); + exit(1); + } + + int fd[2]; + + if (pipe(fd) == -1) { + perror(""pipe""); + exit(1); + } + + int r = fork(); + + if (r == 0) { + close(fd[1]); + if (dup2(fd[0], fileno(stdin)) == -1) { + perror(""dup2""); + exit(1); + } + execl(""./validate"", ""validate"", NULL); + } else if (r > 0) { + close(fd[0]); + write(fd[1], user_id, strlen(user_id)); + write(fd[1], password, strlen(password)); + + int status; + while (wait(&status) > 0) + ; + + if (WIFEXITED(status)) { + if (WEXITSTATUS(status) == 1) { + perror(""validate""); + exit(1); + } else if (WEXITSTATUS(status) == 0) { + printf(""%s\n"", SUCCESS); + } else if (WEXITSTATUS(status) == 2) { + printf(""%s\n"", INVALID); + } else if (WEXITSTATUS(status) == 3) { + printf(""%s\n"", NO_USER); + } + } else { + perror(""validate""); + exit(1); + } + } else { + perror(""fork""); + exit(1); + } + return 0; +}",, +63c647785f004bf4898cf400,2023-03-10T23:23:50.108Z,Help Fix Code,v2,return exit status of child,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + if (strlen(password) > 9) { + perror(""password too long""); + exit(1); + } + + if (strlen(user_id) > 9) { + perror(""user id too long""); + exit(1); + } + + int fd[2]; + + if (pipe(fd) == -1) { + perror(""pipe""); + exit(1); + } + + int r = fork(); + + if (r == 0) { + close(fd[1]); + if (dup2(fd[0], fileno(stdin)) == -1) { + perror(""dup2""); + exit(1); + } + execl(""./validate"", ""validate"", NULL); + } + else if (r > 0) { + close(fd[0]); + write(fd[1], user_id, strlen(user_id)); + write(fd[1], password, strlen(password)); + + int status; + while (wait(&status) > 0); + + if (WIFEXITED(status)) { + if (WEXITSTATUS(status) == 1) { + perror(""validate""); + exit(1); + } + else if (WEXITSTATUS(status) == 0) { + printf(""%s\n"", SUCCESS); + } + else if (WEXITSTATUS(status) == 2) { + printf(""%s\n"", INVALID); + } + else if (WEXITSTATUS(status) == 3) { + printf(""%s\n"", NO_USER); + } + } + else { + perror(""validate""); + exit(1); + } + } + else { + perror(""fork""); + exit(1); + } + return 0; +}",, +63c647795f004bf4898cf40a,2023-02-07T19:35:03.403Z,General Question,v1,how are pointers used with structs?,,, +63c647795f004bf4898cf40a,2023-02-07T19:35:52.449Z,General Question,v1,could you explain what printf does?,,, +63c647795f004bf4898cf40a,2023-02-07T19:36:45.488Z,Explain Code,v1,,"for (int i = 0; i < 5; i++){ + printf(i); +}",, +63c6477a5f004bf4898cf414,2023-01-30T15:59:08.737Z,General Question,v1,"can you declare two variables of the same type on one line? +",,, +63c6477a5f004bf4898cf414,2023-02-06T16:04:19.712Z,General Question,v1,"how do you find current time using time and ctime functions? +",,, +63c6477a5f004bf4898cf414,2023-02-07T14:19:25.003Z,General Question,v1,what does fseek return?,,, +63c6477a5f004bf4898cf414,2023-02-12T14:25:22.391Z,General Question,v1,how would you free nodes in a linked list?,,, +63c6477a5f004bf4898cf414,2023-02-20T15:47:53.422Z,General Question,v1,How do you split a string on spaces?,,, +63c6477a5f004bf4898cf414,2023-02-20T19:42:42.309Z,General Question,v1,Can you write me a function that checks whether a string has any non-space characters?,,, +63c6477a5f004bf4898cf414,2023-03-06T15:50:30.862Z,General Question,v1,how do I calculate how many space separated words make up a line of a file?,,, +63c6477a5f004bf4898cf414,2023-03-10T01:33:37.751Z,General Question,v2,what is the return type of stat? and how do i call it to determine the last time a file was modified?,,, +63c6477a5f004bf4898cf414,2023-03-14T13:28:50.295Z,General Question,v2,how to generate a random number from 0-99 inclusive using random function?,,, +63c6477a5f004bf4898cf414,2023-03-14T13:32:41.204Z,Explain Code,v2,,"for (;;) { + ; +}",, +63c6477a5f004bf4898cf414,2023-03-14T14:09:12.613Z,General Question,v2,"how to set an alarm using setitimer and ITIMER_PROF? +",,, +63c6477a5f004bf4898cf414,2023-03-14T14:20:56.315Z,Explain Code,v2,,"struct itimerval new_time, old_time; +new_time.it_interval.tv_usec = 0; +new_time.it_interval.tv_sec = 5; +new_time.it_value.tv_usec = 0; +new_time.it_value.tv_sec = seconds; +setitimer(ITIMER_PROF, &new_time, &old_time);",, +63c6477a5f004bf4898cf414,2023-03-14T14:22:55.749Z,Explain Code,v2,,"struct itimerval new_time, old_time; +new_time.it_interval.tv_usec = 0; +new_time.it_interval.tv_sec = 0; +new_time.it_value.tv_usec = 0; +new_time.it_value.tv_sec = seconds; +setitimer(ITIMER_PROF, &new_time, &old_time);",, +63c6477a5f004bf4898cf414,2023-03-14T14:25:49.634Z,General Question,v2,"how do you make an alarm that will send the SIGPROF signal to a program while it's executing +",,, +63c6477a5f004bf4898cf414,2023-03-14T14:28:09.580Z,Help Fix Code,v2,"I need this code to exit(0) when the timer sends a SIGPROF to the process +","/* The purpose of this program is to practice writing signal handling + * functions and observing the behaviour of signals. + */ + +#include +#include +#include +#include +#include + +/* Message to print in the signal handling function. */ +#define MESSAGE ""%ld reads were done in %ld seconds.\n"" + +/* Global variables to store number of read operations and seconds elapsed. + */ +long num_reads, seconds; + +// Signal handler function. +void handler(int code) { + fprintf(stdout, MESSAGE, num_reads, seconds); + exit(0); +} + + +/* The first command-line argument is the number of seconds to set a timer to run. + * The second argument is the name of a binary file containing 100 ints. + * Assume both of these arguments are correct. + */ +int main(int argc, char **argv) { + if (argc != 3) { + fprintf(stderr, ""Usage: time_reads s filename\n""); + exit(1); + } + seconds = strtol(argv[1], NULL, 10); + + FILE *fp; + if ((fp = fopen(argv[2], ""r"")) == NULL) { + perror(""fopen""); + exit(1); + } + + struct sigaction newact; + newact.sa_handler = handler; + newact.sa_flags = 0; + sigemptyset(&newact.sa_mask); + sigaction(SIGPROF, &newact, NULL); + + struct itimerval new_time, old_time; + new_time.it_interval.tv_usec = 0; + new_time.it_interval.tv_sec = 0; + new_time.it_value.tv_usec = 0; + new_time.it_value.tv_sec = seconds; + setitimer(ITIMER_PROF, &new_time, &old_time); + + /* In an infinite loop, read an int from a random location in the file, + * and print it to stderr. + */ + int num; + for (;;) { + fseek(fp, random() % 100, SEEK_SET); + fread(&num, sizeof(int), 1, fp); + num_reads++; //Increment num_reads + fprintf(stderr, ""%d"", num); + } + return 1; // something is wrong if we ever get here! +} +",, +63c6477c5f004bf4898cf428,2023-01-24T20:41:32.819Z,General Question,v1,"what is char** +",,, +63c6477c5f004bf4898cf428,2023-01-24T20:42:08.099Z,General Question,v1,how to dereference char**?,,, +63c6477e5f004bf4898cf432,2023-01-17T18:48:35.924Z,Explain Code,v1,,"print(""Hello World"")",, +63c6477e5f004bf4898cf432,2023-01-17T20:34:36.144Z,Explain Code,v1,,"int main() { + int i = 2; + int j = 30; + int a[4]; + int *p; + int *q; + p = &i; + j = *p; + *p = 1; + a[0] = 10; + a[3] = 12; + a[i] = 11; + return 0; +}",, +63c6477f5f004bf4898cf441,2023-01-17T20:14:43.308Z,Question from Code,v1,what will be the value of pA after execution?,"int A[4] = {5, 10, 15, 20}; +int *pA = A[0]; +pA += 1;",, +63c6477f5f004bf4898cf441,2023-01-17T20:16:01.166Z,Question from Code,v1,what will be the value of pA after execution? why?,"int A[4] = {5, 10, 15, 20}; +int *pA = A[0]; +pA += 1;",, +63c6477f5f004bf4898cf441,2023-01-17T20:19:18.954Z,General Question,v1,"given an array of four random integers called arr, and a pointer to an integer called point, assign point to be the address of the last element in arr",,, +63c6477f5f004bf4898cf441,2023-01-17T20:21:20.406Z,General Question,v1,what are strings in C?,,, +63c6477f5f004bf4898cf441,2023-01-17T20:22:23.255Z,General Question,v1,give an example of a linked list in C using structs ,,, +63c6477f5f004bf4898cf441,2023-01-17T20:25:41.876Z,Help Write Code,v1,,,,make a linked list using structs +63c6477f5f004bf4898cf441,2023-01-21T05:23:59.489Z,General Question,v1,"in C, does scanf read until the newline?",,, +63c6477f5f004bf4898cf441,2023-01-21T06:13:37.805Z,General Question,v1,read past the first newline of scanf,,, +63c6477f5f004bf4898cf441,2023-01-21T06:14:29.068Z,General Question,v1,"generate a detailed documentation of `scanf(""%*c%d"", &number)` with usage examples and explanations",,, +63c6477f5f004bf4898cf441,2023-01-21T06:42:07.726Z,General Question,v1,skip the first line of scanf,,, +63c6477f5f004bf4898cf441,2023-01-21T07:02:47.356Z,Explain Code,v1,,"scanf(""%c%s %*s %*s %*s %s"", &type, perms, file_mem)",, +63c6477f5f004bf4898cf441,2023-01-21T07:11:13.887Z,General Question,v1,generate a detailed documentation of `%*[^\n]` with usage examples and explanations,,, +63c6477f5f004bf4898cf441,2023-01-21T07:19:33.836Z,General Question,v1,"what is the format specifier for a char +",,, +63c6477f5f004bf4898cf441,2023-01-21T07:21:37.645Z,General Question,v1,first character is empty when using the format specifier %*[^\n],,, +63c6477f5f004bf4898cf441,2023-01-21T07:22:56.178Z,General Question,v1,discard newline in scanf,,, +63c6477f5f004bf4898cf441,2023-01-21T08:12:24.774Z,General Question,v1,how to call a function located in file A from file B in the C programming language,,, +63c6477f5f004bf4898cf441,2023-01-21T22:03:07.292Z,Question from Code,v1,will this code return 1?,"if (-1) { + return 1; +} +return 0;",, +63c6477f5f004bf4898cf441,2023-01-21T22:12:19.966Z,General Question,v1,%3s in scanf will only check for 3 characters?,,, +63c6477f5f004bf4898cf441,2023-01-23T05:34:12.373Z,General Question,v1,"expected ‘char **’ but argument is of type ‘char (*)[50]’ + +how would i fix this issue?",,, +63c6477f5f004bf4898cf441,2023-01-23T05:35:40.927Z,Question from Code,v1,why would this code have issues?,"void ask(char *arg[20]); + +int main() { + char potato[20]; + ask(&potato); + return 0; +}",, +63c6477f5f004bf4898cf441,2023-01-23T05:43:27.836Z,General Question,v1,is the null pointer always added to a string?,,, +63c6477f5f004bf4898cf441,2023-01-23T05:53:01.631Z,General Question,v1,what would tar -czvf do?,,, +63c6477f5f004bf4898cf441,2023-01-24T04:59:58.297Z,General Question,v1,what is $@ and $^ in bash scripting?,,, +63c6477f5f004bf4898cf441,2023-01-24T05:18:49.519Z,General Question,v1,"what is $@, $^, and $< in bash scripting?",,, +63c6477f5f004bf4898cf441,2023-01-24T05:20:55.883Z,General Question,v1,what is % in bash scripting?,,, +63c6477f5f004bf4898cf441,2023-01-24T05:21:26.008Z,General Question,v1,what is % in bash scripting for a makefile?,,, +63c6477f5f004bf4898cf441,2023-01-24T06:09:13.800Z,General Question,v1,pointer arithmetic with an array looks like?,,, +63c6477f5f004bf4898cf441,2023-01-24T06:10:25.052Z,General Question,v1,does sizeof function of an array return the length?,,, +63c6477f5f004bf4898cf441,2023-01-24T06:33:10.433Z,General Question,v1,"let there be char **argv. when we do argv + 1, we are starting from the second element in the argv array of char pointers?",,, +63c6477f5f004bf4898cf441,2023-01-24T06:36:25.124Z,Question from Code,v1,does line 2 give the number of elements in argv?,"char **argv; +sizeof(argv) / sizeof(char**)",, +63c6477f5f004bf4898cf441,2023-01-27T22:46:03.530Z,General Question,v1,Explain the strtok function in C. What is a token and a delimiter? How are they used?,,, +63c6477f5f004bf4898cf441,2023-01-27T22:50:01.680Z,General Question,v1,"In C, if i call the error function with a string, it would print an error with that string? For example, if I had error(""too many characters""); then the error would be ""too many characters""?",,, +63c6477f5f004bf4898cf441,2023-02-01T07:28:11.617Z,Question from Code,v1,"On line 2, why does this not work? Why do we need to use strcpy?","char s[] = ""Hello""; +s = ""Bye"";",, +63c6477f5f004bf4898cf441,2023-02-01T07:31:18.955Z,Question from Code,v1,Does variable p get assigned to Smith after the program ends?,"char s[] = ""Hello""; +s = ""Bye""; + +char *p = ""Joe""; +p = ""Smith"";",, +63c6477f5f004bf4898cf441,2023-02-01T07:32:08.620Z,Question from Code,v1,"Why does p get assigned to ""Smith"" but s does not get assigned to ""Bye""?","char s[] = ""Hello""; +s = ""Bye""; + +char *p = ""Joe""; +p = ""Smith"";",, +63c6477f5f004bf4898cf441,2023-02-01T07:34:09.466Z,General Question,v1,Why can you not reassign arrays in C? Is there a historical explanation for this?,,, +63c6477f5f004bf4898cf441,2023-02-01T07:34:46.347Z,General Question,v1,Why can you not re-assign arrays?,,, +63c6477f5f004bf4898cf441,2023-02-01T07:36:22.135Z,Question from Code,v1,Will this work properly in C?,"char s[] = ""Hello""; +char s[] = ""Goodbye"";",, +63c6477f5f004bf4898cf441,2023-02-07T20:50:27.481Z,General Question,v1,what is memcpy? how does it work? why?,,, +63c6477f5f004bf4898cf441,2023-02-14T03:00:31.934Z,Question from Code,v1,What will be the behaviour of this program? Will the strncpy keep on copying all 10 bytes or will it know that s contains space for only 5 bytes and stop?,"char s[5]; +strncpy(s, ""hello world!!!!!!"", 10);",, +63c647805f004bf4898cf446,2023-01-17T22:07:28.482Z,General Question,v1,how to code in C,,, +63c647805f004bf4898cf446,2023-01-18T21:14:14.771Z,General Question,v1,"how to input a string +",,, +63c647805f004bf4898cf446,2023-01-18T21:17:38.341Z,Explain Code,v1,,"char phone[11]; +int keypad; + +scanf(""%s %d"", &phone, &keypad);",, +63c647805f004bf4898cf44b,2023-02-02T01:38:59.247Z,General Question,v1,"How to compare two strings +",,, +63c647805f004bf4898cf44b,2023-02-02T02:06:03.311Z,Help Fix Code,v1,,"int **result = malloc(sizeof(int*) * 2); +result[0] = malloc(sizeof(int) * ((length + 1)/ 2)); +result[1] = malloc(sizeof(int) * length / 2); ","what malloc is incorrectly allocated +", +63c647805f004bf4898cf44b,2023-02-02T02:12:25.517Z,General Question,v1,how to change a string array,,, +63c647805f004bf4898cf44b,2023-02-02T02:13:06.232Z,General Question,v1,how to mutate a char array.,,, +63c647805f004bf4898cf44b,2023-02-02T02:22:29.265Z,Help Fix Code,v1,,"char tempString[strlen(s)] = s; +strncpy(s, tempString, n); +strlen(tempString);","creating a copy of s char +", +63c647805f004bf4898cf44b,2023-02-12T18:57:05.809Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *newUser = malloc(sizeof(User)); + strcpy(newUser->name, name); + User *curr = user_ptr_add; + while (curr != NULL) + { + curr = curr->next; + } + curr = newUser; +}",is assignment and traversal correct, +63c647805f004bf4898cf44b,2023-02-12T18:58:43.637Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *newUser = malloc(sizeof(User)); + strcpy(newUser->name, name); + User *curr = *user_ptr_add; + while (curr != NULL) + { + curr = curr->next; + } + curr = newUser; +}",is traversal of linked list correct, +63c647805f004bf4898cf44b,2023-02-12T19:01:43.401Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *newUser = malloc(sizeof(User)); + strcpy(newUser->name, name); + User *curr = *user_ptr_add; + while (curr != NULL) + { + curr = curr->next; + } + curr = newUser; +}",add user at the end of the list, +63c647805f004bf4898cf44b,2023-02-12T19:02:57.581Z,General Question,v1,how to add a item at the end of linked list,,, +63c647805f004bf4898cf44b,2023-02-12T19:06:50.007Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *newUser = malloc(sizeof(User)); + strcpy(newUser->name, name); + User *curr = *user_ptr_add; + while (curr != NULL) + { + curr = curr->next; + } + curr = newUser; + newUser-> = NULL; +}",newUser should be at the end of the linked list, +63c647805f004bf4898cf44b,2023-02-12T19:07:45.488Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *newUser = malloc(sizeof(User)); + strcpy(newUser->name, name); + User *curr = *user_ptr_add; + while (curr != NULL) + { + curr = curr->next; + } + curr->next = newUser; + newUser-> = NULL; +}",adding user at the end of the linked list, +63c647805f004bf4898cf44b,2023-02-12T19:08:43.422Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *newUser = malloc(sizeof(User)); + strcpy(newUser->name, name); + User *curr = *user_ptr_add; + while (curr != NULL) + { + curr = curr->next; + } + curr = newUser; + newUser->next = NULL; +}",how do you add new user to the end of linked list, +63c647805f004bf4898cf44b,2023-02-12T19:14:41.645Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *newUser = malloc(sizeof(User)); + strcpy(newUser->name, name); + User *curr = *user_ptr_add; + if (curr == NULL) { + curr = newUser; + curr->next = NULL; + return 0; + } +}",w, +63c647805f004bf4898cf44b,2023-02-12T20:18:38.249Z,Help Fix Code,v1,,"int update_pic(User *user, const char *filename) { + if (sizeof(filename) > 31) { + return 2; + } else if (fopen(filename, ""r"") == NULL) + { + return 1; + } else { + strncpy(user->profile_pic, filename, 32); + }",open the file and assign it to user profile pic, +63c647805f004bf4898cf44b,2023-02-12T20:47:10.873Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + User *curr = (User *) head; + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + return curr; + } + head = head->next; + return 0; + } + return NULL; +}",return pointer to the user, +63c647805f004bf4898cf44b,2023-02-12T21:02:58.711Z,General Question,v1,how to compare two strings,,, +63c647805f004bf4898cf44b,2023-02-12T23:07:33.997Z,General Question,v1,check if string array is empty,,, +63c647805f004bf4898cf44b,2023-02-13T00:53:30.863Z,Help Fix Code,v1,, (User *)author->first_post = &newPost;,author a struct should point to new post which is another struct, +63c647805f004bf4898cf44b,2023-02-13T00:54:18.376Z,Help Fix Code,v1,,author->first_post = &newPost;,author struct should point to newpost which is another struct, +63c647805f004bf4898cf44b,2023-02-13T00:55:12.736Z,Help Fix Code,v1,,author->first_post = newPost;,author struct with property first_post should point to another struct newpost, +63c647805f004bf4898cf44b,2023-02-14T03:39:10.894Z,Help Fix Code,v1,,"Post *newPost = malloc(sizeof(Post)); +author->first_post = newPost;",author first post should point to new post, +63c647805f004bf4898cf44b,2023-02-14T03:40:18.878Z,Help Fix Code,v1,," Post *newPost = malloc(sizeof(Post)); + author->first_post = newPost; + strncpy(newPost->author, author->name, MAX_NAME); + newPost->author[MAX_NAME-1] ='\0'; + newPost->contents = contents; + newPost->next = NULL;",author first post should newPost, +63c647805f004bf4898cf44b,2023-02-14T05:46:03.550Z,Help Fix Code,v1,,"Post *prevPost = target->first_post; +newPost->next = prevPost; +target->first_post = newPost;",insert before the first_post, +63c647805f004bf4898cf44b,2023-02-14T05:56:33.297Z,Help Fix Code,v1,,"Post *newPost = malloc(sizeof(Post)); + strncpy(newPost->author, author->name, MAX_NAME); + newPost->author[MAX_NAME-1] ='\0'; + newPost->contents = contents; + time_t current_time = time(NULL); + newPost->date = ¤t_time; + if (target->first_post == NULL) { + newPost->next = NULL; + target->first_post = newPost; + } else { + newPost->next = target->first_post; + target->first_post = newPost; + } + }",new post should be inserted in front of the linked list, +63c647805f004bf4898cf44b,2023-02-14T05:57:22.859Z,Help Fix Code,v1,,"Post *newPost = malloc(sizeof(Post)); + strncpy(newPost->author, author->name, MAX_NAME); + newPost->author[MAX_NAME-1] ='\0'; + newPost->contents = contents; + if (target->first_post == NULL) { + newPost->next = NULL; + target->first_post = newPost; + } else { + newPost->next = target->first_post; + target->first_post = newPost; + } + }",new post should be inserted infront of the list, +63c647805f004bf4898cf44b,2023-02-15T01:27:49.237Z,Help Fix Code,v1,,"time_t current_time = time(NULL); +newPost->date = ¤t_time;",storing current time as a date in newPost, +63c647805f004bf4898cf44b,2023-02-15T02:58:54.971Z,Help Fix Code,v1,,"for (int i = 0; i < MAX_FRIENDS; i++) { + User *userFriend = find_user(curr->friends[i]->name, *user_ptr_del); + if (userFriend != NULL) { + for (int j = 0; j < MAX_FRIENDS; j++) { + if (userFriend->friends[j] != NULL && strcmp(userFriend->friends[j]->name, curr->name) == 0) { + userFriend->friends[j] = NULL; + break; + } + } + } + }",remove curr from curr friend's list., +63c647805f004bf4898cf44b,2023-02-16T02:22:47.497Z,Help Fix Code,v1,,"char *copy(char *dest, const char *src, int capacity) { + //dest = destination string + // copy from source and dest + int counter = 0; + while (src[counter] != '\0' || counter < capacity) { + dest[counter] = src[counter]; + } + dest[counter - 1] = '\0'; + return dest; +}",copy char from src to dest, +63c647805f004bf4898cf44b,2023-03-09T03:53:45.740Z,Help Fix Code,v1,,"if (strcmp(token, "":"") == 0) { + bool = 1; +}",checking if current token is a colon, +63c647815f004bf4898cf450,2023-02-08T05:29:13.121Z,General Question,v1,I am getting a segmentation fault when I run this on a bitmap display,,, +63c647815f004bf4898cf450,2023-02-08T05:30:06.212Z,General Question,v1,I am getting a segmentation fault on this code,,, +63c647815f004bf4898cf450,2023-02-08T05:30:39.505Z,Question from Code,v1,I am getting a seg fault,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel** pixel_data_start = malloc(sizeof(struct pixel*)*height); + fseek(image, pixel_array_offset, SEEK_SET); + for (int i = 0; i < height; i++){ + pixel_data_start[i] = malloc(sizeof(struct pixel)*width); + + for (int j = 0; j < width; j++){ + unsigned char blue; + fread(&blue, sizeof(unsigned char), 1, image); + unsigned char green; + fread(&green, sizeof(unsigned char), 1, image); + unsigned char red; + fread(&red, sizeof(unsigned char), 1, image); + struct pixel pix = {blue, green, red}; + pixel_data_start[i][j] = pix; + } + } + return pixel_data_start; +}",, +63c647835f004bf4898cf45f,2023-03-18T00:58:48.841Z,General Question,v2,"what does the mask do for sigaction? +",,, +63c647835f004bf4898cf45f,2023-03-18T01:00:56.472Z,General Question,v2,what types of signals cannot be blocked?,,, +63c647835f004bf4898cf45f,2023-03-18T01:02:51.379Z,Explain Code,v2,,"struct itimerval timer_info; +// Makes alarm happen only once +timer_info.it_interval.tv_sec = 0; +timer_info.it_interval.tv_usec = 0; +// Sent it after s seconds +timer_info.it_value.tv_sec = 1; +timer_info.it_value.tv_usec = 0; + +if (setitimer(ITIMER_PROF, &timer_info, NULL) == -1) { + perror(""setitimer""); + exit(1); +}",, +63c647845f004bf4898cf46e,2023-02-11T21:00:09.195Z,Help Fix Code,v1,," +int make_friends(const char *name1, const char *name2, User *head) { + User *user1 = NULL; + User *user2 = NULL; + int friend_count1 = 0; + int friend_count2 = 0; + + // Find the two users with the given names + User *current = head; + while (current != NULL) { + if (strcmp(current->name, name1) == 0) { + user1 = current; + } if (strcmp(current->name, name2) == 0) { + user2 = current; + } + if (user1 != NULL && user2 != NULL) { + break; + } + } + + // Check if at least one user does not exist and return 4 if so is true + if (user1 == NULL || user2 == NULL) { + return 4; + } + + // Check if the same user is passed in twice and return 3 if so is true + if (user1 == user2) { + return 3; + } + + // Create marker to be used to check if already friends + int marker = 0; + // Check if the two users are already friends and return 1 if so is true + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == user2) { + marker = 1; + } + if (user2->friends[i] == user1) { + marker = 1; + } + if (user1->friends[i] == NULL) { + friend_count1++; + } + if (user2->friends[i] == NULL) { + friend_count2++; + } + } + + // Check if at least one user has MAX_FRIENDS friends + if (friend_count1 == 0 || friend_count2 == 0) { + return 2; + } + + // Return 1 if users are already friends and none of the other error return statements apply + if (marker == 1) return 1; + + // Make the two users friends with each other + user1->friends[MAX_FRIENDS - friend_count1] = user2; + user2->friends[MAX_FRIENDS - friend_count2] = user1; + return 0; +}","/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */", +63c647845f004bf4898cf46e,2023-02-11T21:03:37.846Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + bool is_friends = false; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + is_friends = true; + break; + } + } + + if (!is_friends) { + return 1; + } + + Post *new_post = (Post *)malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + + new_post->date = (time_t *)ctime(NULL); + + new_post->next = target->first_post; + target->first_post = new_post; + + 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 + */", +63c647845f004bf4898cf46e,2023-02-11T21:11:02.531Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + bool is_friends = false; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + is_friends = true; + break; + } + } + + if (!is_friends) { + return 1; + } + + Post *new_post = (Post *)malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + + new_post->date = (time_t *)ctime(NULL); + + new_post->next = target->first_post; + target->first_post = new_post; + + 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 + */ +How would you go about inserting the new post at the front of the list?", +63c647845f004bf4898cf46e,2023-02-11T21:11:34.688Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + bool is_friends = false; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + is_friends = true; + break; + } + } + + if (!is_friends) { + return 1; + } + + Post *new_post = (Post *)malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + + new_post->date = (time_t *)ctime(NULL); + + new_post->next = target->first_post; + target->first_post = new_post; + + 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 + */", +63c647845f004bf4898cf46e,2023-02-12T00:18:14.686Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + + if (author == NULL || target == NULL) { + return 2; + } + bool is_friends = false; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + is_friends = true; + break; + } + } + + if (!is_friends) { + return 1; + } + + Post *new_post = (Post *)malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + new_post->date = (time_t *)malloc(sizeof(time_t)); + time(new_post->date); + + new_post->next = target->first_post; + target->first_post = new_post; + + 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 + */", +63c647845f004bf4898cf46e,2023-02-12T00:45:41.726Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + User *user1 = NULL; + User *user2 = NULL; + int friend_count1 = 0; + int friend_count2 = 0; + + // Find the two users with the given names + User *current = head; + while (current != NULL) { + if (strcmp(current->name, name1) == 0) { + user1 = current; + } if (strcmp(current->name, name2) == 0) { + user2 = current; + } + if (user1 != NULL && user2 != NULL) { + break; + } + current = current->next; + } + + // Check if at least one user does not exist and return 4 if so is true + if (user1 == NULL || user2 == NULL) { + return 4; + } + + // Check if the same user is passed in twice and return 3 if so is true + if (user1 == user2) { + return 3; + } + + // Create marker to be used to check if already friends + int marker = 0; + // Check if the two users are already friends and return 1 if so is true + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == user2) { + marker = 1; + } + if (user2->friends[i] == user1) { + marker = 1; + } + if (user1->friends[i] == NULL) { + friend_count1++; + } + if (user2->friends[i] == NULL) { + friend_count2++; + } + } + + // Check if at least one user has MAX_FRIENDS friends + if (friend_count1 == 0 || friend_count2 == 0) { + return 2; + } + + // Return 1 if users are already friends and none of the other error return statements apply + if (marker == 1) return 1; + + // Make the two users friends with each other + user1->friends[MAX_FRIENDS - friend_count1] = user2; + user2->friends[MAX_FRIENDS - friend_count2] = user1; + return 0; +}","* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */", +63c647845f004bf4898cf46e,2023-02-12T01:20:32.849Z,Help Fix Code,v1,," +int delete_user(const char *name, User **user_ptr_del) { + User *prev = NULL; + User *curr = *user_ptr_del; + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + // remove user from friends lists + int i, j; + for (i = 0; i < MAX_FRIENDS; i++) { + User *friend = curr->friends[i]; + if (!friend) continue; + for (j = 0; j < MAX_FRIENDS; j++) { + if (friend->friends[j] == curr) { + friend->friends[j] = NULL; + break; + } + } + } + + // remove user from linked list + if (prev == NULL) { + *user_ptr_del = curr->next; + } else { + prev->next = curr->next; + } + + // free memory + Post *post = curr->first_post; + while (post != NULL) { + Post *temp = post->next; + free(post->contents); + free(post); + post = temp; + } + free(curr); + + return 0; + } + prev = curr; + curr = curr->next; +} + +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. + */", +63c647845f004bf4898cf46e,2023-02-12T02:03:35.602Z,Help Fix Code,v1,," int delete_user(const char *name, User **user_ptr_del) { + User *prev = NULL; + User *curr = *user_ptr_del; + while (curr != NULL) { + // remove user from friend's list + if (strcmp(curr->name, name) == 0) { + // remove current user from all friends + int i, j; + for (i = 0; i < MAX_FRIENDS; i++) { + User *friend = curr->friends[i]; + if (!friend) continue; + for (j = 0; j < MAX_FRIENDS; j++) { + if (friend->friends[j] == curr) { + // move all friends after current to the left + int k; + for (k = j; k < MAX_FRIENDS - 1; k++) { + friend->friends[k] = friend->friends[k + 1]; + } + // clear the last friend + friend->friends[MAX_FRIENDS - 1] = NULL; + break; + } + } + } + + // remove current user from linked list + if (prev == NULL) { + *user_ptr_del = curr->next; + } else { + prev->next = curr->next; + } + + // free memory of post + Post *post = curr->first_post; + while (post != NULL) { + Post *temp = post->next; + free(post->contents); + free(post); + post = temp; + } + free(curr); + + return 0; + } + prev = curr; + curr = curr->next; + } + + 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. + */", +63c647845f004bf4898cf46e,2023-02-12T02:05:05.398Z,Help Fix Code,v1,," int delete_user(const char *name, User **user_ptr_del) { + User *prev = NULL; + User *curr = *user_ptr_del; + while (curr != NULL) { + // remove user from friend's list + if (strcmp(curr->name, name) == 0) { + // remove current user from all friends + int i, j; + for (i = 0; i < MAX_FRIENDS; i++) { + User *friend = curr->friends[i]; + if (!friend) continue; + for (j = 0; j < MAX_FRIENDS; j++) { + if (friend->friends[j] == curr) { + // move all friends after current to the left + int k; + for (k = j; k < MAX_FRIENDS - 1; k++) { + friend->friends[k] = friend->friends[k + 1]; + } + // clear the last friend + friend->friends[MAX_FRIENDS - 1] = NULL; + break; + } + } + } + + // remove current user from linked list + if (prev == NULL) { + *user_ptr_del = curr->next; + } else { + prev->next = curr->next; + } + + // free memory of post + Post *post = curr->first_post; + while (post != NULL) { + Post *temp = post->next; + free(post->contents); + free(post); + post = temp; + } + free(curr); + + return 0; + } + prev = curr; + curr = curr->next; + } + + return 1; +}","It must remove the user from the list of users +It must remove the user from all user's friends lists. +It must remove the user's posts from all user's posts lists.", +63c647845f004bf4898cf46e,2023-02-12T02:05:57.533Z,Explain Code,v1,," int delete_user(const char *name, User **user_ptr_del) { + User *prev = NULL; + User *curr = *user_ptr_del; + while (curr != NULL) { + // remove user from friend's list + if (strcmp(curr->name, name) == 0) { + // remove current user from all friends + int i, j; + for (i = 0; i < MAX_FRIENDS; i++) { + User *friend = curr->friends[i]; + if (!friend) continue; + for (j = 0; j < MAX_FRIENDS; j++) { + if (friend->friends[j] == curr) { + // move all friends after current to the left + int k; + for (k = j; k < MAX_FRIENDS - 1; k++) { + friend->friends[k] = friend->friends[k + 1]; + } + // clear the last friend + friend->friends[MAX_FRIENDS - 1] = NULL; + break; + } + } + } + + // remove current user from linked list + if (prev == NULL) { + *user_ptr_del = curr->next; + } else { + prev->next = curr->next; + } + + // free memory of post + Post *post = curr->first_post; + while (post != NULL) { + Post *temp = post->next; + free(post->contents); + free(post); + post = temp; + } + free(curr); + + return 0; + } + prev = curr; + curr = curr->next; + } + + return 1; +}",, +63c647845f004bf4898cf46e,2023-02-12T02:34:32.075Z,Help Fix Code,v1,,"#include ""friends.h"" +#include +#include +#include +#include + +/* + * 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) { + User *current = *user_ptr_add; + while (current != NULL) { + if (strcmp(current->name, name) == 0) { + return 1; + } + current = current->next; + } + + if (strlen(name) >= MAX_NAME) { + return 2; + } + // must cast to be of type User* + User *new_user = (User*) malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + } else { + current = *user_ptr_add; + while (current->next != NULL) { + current = current->next; + } + current->next = new_user; + } + + 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 *current = (User *) head; + + // Iterate through the list of users starting at head until username is name + while (current != NULL){ + if (strcmp(current->name, name)== 0){ + return (User *) current; + } + current = current -> 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) { + printf(""User List: \n""); + while (curr != NULL){ + printf(""\t%s\n"", curr->name); + curr = curr->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) { + FILE *file = fopen(filename, ""r""); + if (file == NULL) { + return 1; + } + if (strlen(filename) >= MAX_NAME) { + return 2; + } + strcpy(user->profile_pic, filename); + fclose(file); + return 0; + +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int make_friends(const char *name1, const char *name2, User *head) { + User *user1 = NULL; + User *user2 = NULL; + + // Find the two users with the given names + User *current = head; + while (current != NULL) { + if (strcmp(current->name, name1) == 0) { + user1 = current; + } if (strcmp(current->name, name2) == 0) { + user2 = current; + } + if (user1 != NULL && user2 != NULL) { + break; + } + current = current->next; + } + + // Check if at least one user does not exist and return 4 if so is true + if (user1 == NULL || user2 == NULL) { + return 4; + } + + // Check if the same user is passed in twice and return 3 if so is true + if (user1 == user2) { + return 3; + } + + // Create marker to be used to check if already friends + int marker = 0; + int friend_count1 = MAX_FRIENDS; + int friend_count2 = MAX_FRIENDS; + // Check if the two users are already friends and return 1 if so is true + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == user2) { + marker = 1; + } + if (user2->friends[i] == user1) { + marker = 1; + } + if (user1->friends[i] != NULL) { + friend_count1--; + } + if (user2->friends[i] != NULL) { + friend_count2--; + } + } + + // Check if at least one user has MAX_FRIENDS friends + if (friend_count1 == 0 || friend_count2 == 0) { + return 2; + } + + + // Return 1 if users are already friends and none of the other error return statements apply + if (marker == 1) return 1; + + // Make the two users friends with each other + user1->friends[MAX_FRIENDS - friend_count1] = user2; + user2->friends[MAX_FRIENDS - friend_count2] = user1; + return 0; +} + + + +/* + * 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; + } + if (user->profile_pic != NULL){ + FILE *fp; + char line[100000]; + + fp = fopen(user->profile_pic, ""r""); + if (fp == NULL) { + printf(""Error opening file\n""); + return 1; + } + + while (fgets(line, 10000, fp) != NULL) { + printf(""%s"", line); + } + + fclose(fp); + } + + printf(""\nName: %s\n"", user->name); + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user->friends[i] == NULL) { + break; + } + printf(""%s\n"", user->friends[i]->name); + } + printf(""------------------------------------------\n""); + printf(""Posts:\n""); + + Post *curr = user->first_post; + int counter = 1; + while (curr != NULL){ + if (counter > 1){ + printf(""\n\n===\n\n""); + } + printf(""From: %s\nDate: %s\n%s\n"", curr->author, ctime(curr->date), curr->contents); + curr = curr -> next; + counter++; + } + printf(""------------------------------------------\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; + } + bool is_friends = false; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + is_friends = true; + break; + } + } + + if (!is_friends) { + return 1; + } + + Post *new_post = (Post *)malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + new_post->date = (time_t *)malloc(sizeof(time_t)); + time(new_post->date); + + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; +} + + +",When creating a user profile the profile should be printed correctly after a user was added, +63c647845f004bf4898cf46e,2023-02-12T02:56:31.325Z,Help Fix Code,v1,,"int print_user(const User *user) { + if (user == NULL) { + return 1; + } + if (user->profile_pic[0] == '\0'){ + FILE *fp; + char line[100000]; + + fp = fopen(user->profile_pic, ""r""); + if (fp == NULL) { + printf(""Error opening file\n""); + return 1; + } + + while (fgets(line, 10000, fp) != NULL) { + printf(""%s"", line); + } + + fclose(fp); + } + + printf(""\nName: %s\n"", user->name); + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user->friends[i] == NULL) { + break; + } + printf(""%s\n"", user->friends[i]->name); + } + printf(""------------------------------------------\n""); + printf(""Posts:\n""); + + Post *curr = user->first_post; + int counter = 1; + while (curr != NULL){ + if (counter > 1){ + printf(""\n\n===\n\n""); + } + printf(""From: %s\nDate: %s\n%s\n"", curr->author, ctime(curr->date), curr->contents); + curr = curr -> next; + counter++; + } + printf(""------------------------------------------\n""); + return 0; +}",To print out the profile of the user , +63c647845f004bf4898cf46e,2023-02-12T18:13:42.671Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + + if (author == NULL || target == NULL) { + return 2; + } + bool is_friends = false; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + is_friends = true; + break; + } + } + + if (!is_friends) { + return 1; + } + + Post *new_post = (Post *)malloc(sizeof(Post)); + strncpy(new_post->author, author->name, MAX_NAME - 1); + new_post->author[MAX_NAME - 1] = '\0'; + new_post->contents = contents; + new_post->date = (time_t *)malloc(sizeof(time_t)); + time(new_post->date); + + new_post->next = target->first_post; + target->first_post = new_post; + + free(new_post->date); + free(new_post); + + return 0; +}",Free the memory correctly, +63c647845f004bf4898cf46e,2023-03-11T02:08:32.491Z,Help Fix Code,v2,Part of a function called run_make which should check if the target in the make file exists nd is up to date.,"// check if the target exists and is up to date + struct stat target_stat; + struct stat dep_stat; + if (stat(rule->target, &target_stat) == 0) { + // target file exists + int up_to_date = 1; + for (Dependency *dep = rule->dependencies; dep != NULL; dep = dep->next_dep) { + if (stat(dep->rule->target, &dep_stat) != 0) { + // dependency file doesn't exist, so target is not up to date + up_to_date = 0; + break; + } + if (dep_stat.st_mtim.tv_sec > target_stat.st_mtim.tv_sec || + (dep_stat.st_mtim.tv_sec == target_stat.st_mtim.tv_sec && + dep_stat.st_mtim.tv_nsec > target_stat.st_mtim.tv_nsec)) { + // dependency file is more recent than target file + up_to_date = 0; + break; + } + } + if (up_to_date) { + // target is up to date, so no need to run actions + return; + } + }",, +63c647845f004bf4898cf46e,2023-03-11T02:11:20.225Z,Help Write Code,v2,,,,"/* Evaluate the rule in rules corresponding to the given target. + If target is NULL, evaluate the first rule instead. + If pflag is 0, evaluate each dependency in sequence. + If pflag is 1, then evaluate each dependency in parallel (by creating one + new process per dependency). In this case, the parent process will wait until + all child processes have terminated before checking dependency modified times + to decide whether to execute the actions. + */" +63c647845f004bf4898cf46e,2023-03-11T02:47:48.372Z,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; +} +",, +63c647845f004bf4898cf46e,2023-03-11T02:49:22.917Z,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; +} +",, +63c647845f004bf4898cf46e,2023-03-11T02:55:06.354Z,Explain Code,v2,,"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; +} +",, +63c647845f004bf4898cf46e,2023-03-11T02:58:57.194Z,Question from Code,v2,If you type Ctl+C while this function is running then what happens?,"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; +} +",, +63c647845f004bf4898cf46e,2023-03-11T20:12:33.279Z,Help Fix Code,v2,"/* 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) { + // Implement this function and remove the stubbed return statement below. + Rule *rules = NULL; + Rule *cur_rule = NULL; + char line[MAXLINE]; + + while(fgets(line, MAXLINE, fp) != NULL) { + // Skip comments and empty lines + if(is_comment_or_empty(line)) { + continue; + } + // Target line + if(line[0] != '\t') { + // Create a new rule + Rule *rule = malloc(sizeof(Rule)); + rule->target = malloc(strlen(strtok(line, "": "")) + 1); + strcpy(rule->target, strtok(line, "": "")); + rule->dependencies = NULL; + rule->actions = NULL; + rule->next_rule = NULL; + + // Add the new rule to the list of rules + if(rules == NULL) { + rules = rule; + } else { + cur_rule->next_rule = rule; + } + cur_rule = rule; + + // Action line + } else { + // Create a new action + Action *act = malloc(sizeof(Action)); + act->args = NULL; + act->next_act = NULL; + + // Add arguments + int arg_count = 0; + char *arg_str = strtok(line, "" \t\n""); + while(arg_str != NULL) { + act->args = realloc(act->args, (arg_count + 2) * sizeof(char *)); + act->args[arg_count] = malloc(strlen(arg_str) + 1); // Allocate memory + strcpy(act->args[arg_count], arg_str); // Copy string + arg_count++; + + arg_str = strtok(NULL, "" \t\n""); + } + act->args[arg_count] = NULL; + + // Add the new action to the current rule + if(cur_rule->actions == NULL) { + cur_rule->actions = act; + } else { + Action *cur_act = cur_rule->actions; + while(cur_act->next_act != NULL) { + cur_act = cur_act->next_act; + } + cur_act->next_act = act; + } + } + } + return rules; +}",, +63c647845f004bf4898cf46e,2023-03-22T18:35:58.556Z,Help Fix Code,v2,"/* + * Print the usernames of all users in the list starting at curr. + * Names should be printed to standard output, one per line. + * Return a dynamically allocated string containing the list of user names. + */","char* list_users(const User *curr) { + char* result = NULL; + int len = 0; + while (curr != NULL) { + len += strlen(curr->name) + 1; // Add 1 for newline character + curr = curr->next; + } + + result = (char*) malloc(len + 1); // Add 1 for null terminator + if (result == NULL) { + perror(""malloc""); + exit(1); + } + + char* ptr = result; + curr = curr->next; + while (curr != NULL) { + int n = snprintf(ptr, len, ""%s\n"", curr->name); + if (n < 0) { + perror(""snprintf""); + free(result); + exit(1); + } + ptr += n; + len -= n; + curr = curr->next; + } + + return result; +}",, +63c647845f004bf4898cf46e,2023-03-22T18:36:56.986Z,Help Fix Code,v2,"/* + * Print the usernames of all users in the list starting at curr. + * Names should be printed to standard output, one per line. + * Return a dynamically allocated string containing the list of user names. + */","char* list_users(const User *curr) { + char* result = NULL; + int len = 0; + while (curr != NULL) { + len += strlen(curr->name) + 1; // Add 1 for newline character + curr = curr->next; + } + + result = (char*) malloc(len + 1); // Add 1 for null terminator + if (result == NULL) { + perror(""malloc""); + exit(1); + } + + char* ptr = result; + curr = curr->next; + while (curr != NULL) { + int n = snprintf(ptr, len, ""%s\n"", curr->name); + if (n < 0) { + perror(""snprintf""); + free(result); + exit(1); + } + ptr += n; + len -= n; + curr = curr->next; + } + + return result; +} +",, +63c647845f004bf4898cf46e,2023-03-22T18:37:35.954Z,General Question,v2,Will this give a segmentation fault?,,, +63c647845f004bf4898cf46e,2023-03-22T19:44:09.174Z,Explain Code,v2,,"char* print_user(const User* user) { + if (user == NULL) { + return NULL; + } + + int length = 0; + + length += snprintf(NULL, 0, ""Name: %s\r\n\r\n"", user->name); + if (length < 0) { + perror(""snprintf""); + exit(1); + } + char* output = malloc(length + 1); + if (output == NULL) { + perror(""malloc""); + exit(1); + } + + length = strlen(output); + + length += snprintf(output + length, strlen(""Friends:\r\n"") + 1, ""Friends:\r\n""); + for (int i = 0; i < MAX_FRIENDS && user->friends[i] != NULL; i++) { + length += snprintf(output + length, strlen(user->friends[i]->name) + strlen(""\r\n"") + 1, ""%s\r\n"", user->friends[i]->name); + } + length += snprintf(output + length, strlen(""------------------------------------------\r\n"") + 1, ""------------------------------------------\r\n""); + + length += snprintf(output + length, strlen(""Posts:\r\n"") + 1, ""Posts:\r\n""); + const Post* curr = user->first_post; + while (curr != NULL) { + char* post_output = print_post(curr); + if (post_output != NULL) { + length += snprintf(output + length, strlen(post_output) + 1, ""%s"", post_output); + free(post_output); + } + curr = curr->next; + if (curr != NULL) { + length += snprintf(output + length, strlen(""\r\n===\r\n\r\n"") + 1, ""\r\n===\r\n\r\n""); + } + } + length += snprintf(output + length, strlen(""------------------------------------------\r\n"") + 1, ""------------------------------------------\r\n""); + + return output; +}",, +63c647855f004bf4898cf478,2023-01-17T20:06:50.584Z,General Question,v1,"fix my code +",,, +63c647875f004bf4898cf487,2023-01-30T06:04:39.369Z,Help Fix Code,v1,,"int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + + if (!check_permissions(argv[1], argv[2])) { + printf(""working\n""); + + } + + char *first; + char *second; + + scanf(""%s %s"", first, second); + printf(""%s %s\n"", first, second); + + scanf(""%s %s"", first, second); + printf(""%s %s\n"", first, second); + + scanf(""%s %s"", first, second); + printf(""%s %s\n"", first, second); + + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + + return 0; +}","no segmentation fault +", +63c647875f004bf4898cf487,2023-01-30T06:05:19.983Z,General Question,v1,"do you have to initialize strings +",,, +63c647875f004bf4898cf487,2023-01-30T06:05:53.424Z,General Question,v1,why can't i use scanf to write to a string if i did not initialize the string,,, +63c647895f004bf4898cf496,2023-01-27T01:22:51.532Z,Help Fix Code,v1,,"void fib(int** sequence, int n){ + *sequence = malloc(sizeof(int) * n); + *sequence[0] = 0; +}","this is supposed to be a pointer to a pointer to a list of integers. but i keep getting a segmentation fault. +", +63c647895f004bf4898cf496,2023-01-27T01:42:16.047Z,Help Fix Code,v1,,"void fib(int** sequence, int n){ + sequence = (int) malloc(sizeof(int) * n); + (*sequence)[0] = 0; + (*sequence)[1] = 1; + for(int i = 2; i amt){ + return 0; + } + char result[45] = ""abc"" ; + strncpy(result, target, amt); + printf(""This is the value of the truncated string: %s\n"", result); + return leng - amt ; + + +}",supposed to take target and store the first amt characters in result., +63c647895f004bf4898cf496,2023-02-13T01:46:40.306Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + if(author == NULL || target == NULL){ + printf(""Returning 2\n""); + return 2; + } + + //We only check in one list because we assume that make_friends works properly + int flag = 0; + for(int i=0; ifriends[i] != NULL) && (author->friends[i]->name == target->name)){ + flag = 1; + } + } + if(flag != 1){ //if flag == 1, then they are friends. So we return if != 1. + return 1; + } + + Post *newpost = malloc(sizeof(Post)); + strcpy(newpost->author, author->name); + newpost->contents = contents; + + time_t creation_time = time(NULL); + *(newpost->date ) = creation_time; + + newpost->next = target->first_post; + target->first_post = newpost; + + + 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", +63c647895f004bf4898cf496,2023-02-13T01:51:31.338Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int make_post(const User *author, User *target, char *contents) { + if(author == NULL || target == NULL){ + printf(""Returning 2\n""); + return 2; + } + + //We only check in one list because we assume that make_friends works properly + int flag = 0; + for(int i=0; ifriends[i] != NULL) && (author->friends[i]->name == target->name)){ + flag = 1; + } + } + if(flag != 1){ //if flag == 1, then they are friends. So we return if != 1. + return 1; + } + + Post *newpost = malloc(sizeof(Post)); + strcpy(newpost->author, author->name); + newpost->contents = contents; + + time_t creation_time = time(NULL); + *(newpost->date ) = creation_time; + + newpost->next = target->first_post; + target->first_post = newpost; + + + return 0; + +}",problem with newpost->date;, +63c647895f004bf4898cf496,2023-02-13T02:19:56.199Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int make_post(const User *author, User *target, char *contents) { + if(author == NULL || target == NULL){ + printf(""Returning 2\n""); + return 2; + } + + //We only check in one list because we assume that make_friends works properly + int flag = 0; + for(int i=0; ifriends[i] != NULL) && (author->friends[i]->name == target->name)){ + flag = 1; + } + } + if(flag != 1){ //if flag == 1, then they are friends. So we return if != 1. + return 1; + } + + Post *newpost = malloc(sizeof(Post)); + strcpy(newpost->author, author->name); + newpost->contents = contents; + + time_t creation_time = time(NULL); + *(newpost->date ) = creation_time; + + newpost->next = target->first_post; + target->first_post = newpost; + + + return 0; + +}",I want to allocate memory on the heap for the current time. Then I want to set newpost->date to point to this time., +63c647895f004bf4898cf496,2023-02-13T21:29:42.186Z,Help Fix Code,v1,,"for(int i=0; ifriends[i]->name, name) == 0){ //deleted user is in the list. + curr->friends[MAX_FRIENDS - 1] = NULL; //Last element must be NULL after deletion. + int j = i; + for(int j; j < MAX_FRIENDS-1; j++){ + //from i(index with deleted user), shift over every element to the left + curr->friends[j] = curr->friends[j + 1]; + } + } + }","I want to iterate through elements up until a point. Then if I reach that point, I want to shift each element from that point onwards to the left.", +63c647895f004bf4898cf496,2023-02-13T21:30:44.138Z,Explain Code,v1,,"for(int i=0; ifriends[i]->name, name) == 0){ //deleted user is in the list. + curr->friends[MAX_FRIENDS - 1] = NULL; //Last element must be NULL after deletion. + int j = i; + for(int j; j < MAX_FRIENDS-1; j++){ + //from i(index with deleted user), shift over every element to the left + curr->friends[j] = curr->friends[j + 1]; + } + } + }",, +63c647895f004bf4898cf496,2023-02-13T23:05:13.616Z,Help Fix Code,v1,,"User *current = *user_ptr_del; + + while(current != NULL){ + printf(""Now in %s\n"", current->name); + int i=0; + for(i; ifriends[i] != NULL) && (strcmp(current->friends[i]->name, name) == 0)){ + for(int i; i < MAX_FRIENDS-1; i++){ + //from i(index with deleted user), shift over every element to the left + printf(""%s Old value: %s\n"", current->name, current->friends[i]->name); + current->friends[i] = current->friends[i + 1]; + printf(""%s New value: %s\n"", current->name, current->friends[i]->name); + } + current->friends[MAX_FRIENDS - 1] = NULL; //Last element must be NULL after deletion. + } + } + current = current->next; + }","iterate through all of the User objects. For each user, it checks through the User's list of friends.", +63c647895f004bf4898cf496,2023-02-15T20:22:09.563Z,Question from Code,v1,how would i free the space that is allocated to post->contents,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +",, +63c6478a5f004bf4898cf4a0,2023-01-30T18:40:51.261Z,General Question,v1,"how can I scan multiple lines with scanf +",,, +63c6478a5f004bf4898cf4a0,2023-01-30T21:56:15.423Z,General Question,v1,/bin/bash: line 1: [: too many arguments,,, +63c6478a5f004bf4898cf4a0,2023-01-30T22:23:11.436Z,General Question,v1,"how to read a multiline string using scan f until the end of the string? +",,, +63c6478b5f004bf4898cf4a5,2023-01-17T18:40:22.766Z,General Question,v1,what is a pointer,,, +63c6478b5f004bf4898cf4aa,2023-01-18T05:30:49.651Z,General Question,v1,"what is wrong with this test error? + +phone_loop_checker.py::TestPhoneLoop::test_valid +fail +0 / 1 +c_helper.py:125: in _t + stdout, stderr, returncode = self._run_exec(args=args, input_=input_, timeout=timeout) +c_helper.py:85: in _run_exec + return _exec([os.path.join(""."", self.executable_name)] + (args or []), **kwargs) +c_helper.py:475: in _exec + raise e from e +c_helper.py:471: in _exec + stdout, stderr = proc.communicate(timeout=timeout, input=input_) +/local/packages/python-3.10/lib/python3.10/subprocess.py:1152: in communicate + stdout, stderr = self._communicate(input, endtime, timeout) +/local/packages/python-3.10/lib/python3.10/subprocess.py:2004: in _communicate + self._check_timeout(endtime, orig_timeout, stdout, stderr) +/local/packages/python-3.10/lib/python3.10/subprocess.py:1196: in _check_timeout + raise TimeoutExpired( +E subprocess.TimeoutExpired: Command '['./phone_loop']' timed out after 2 seconds",,, +63c6478b5f004bf4898cf4aa,2023-01-18T06:06:43.169Z,Help Fix Code,v1,,"int main() { + char phone[11]; + int i; + + scanf(""%s"", phone); + + do { + scanf(""%d"", &i); + + if (i == -1) { + printf(""%s\n"", phone); + } + else if (0 <= i && i <= 9) { + printf(""%c\n"", phone[i]); + } + else { + printf(""ERROR""); + return 1; + } + } + while (i != 1); + + return 0; +}","Your task is to write a C program called phone_loop.c. This program will again read from standard input using scanf and take no command-line arguments. Similar to phone.c, this program reads a 10-character string as the first input value but then it repeatedly reads integers until standard input is closed. (Hint: Use a while loop that checks the return value of scanf to accomplish this. Check the man page for scanf to determine the return value when the input stream is closed.) + +After each integer the output produced is as before: + +if the integer is -1, the full string is printed +if the integer is between 0 and 9, the individual character at that position is printed +if the integer is less than -1 or greater than 9, the message ""ERROR"" is printed (to stdout) +In each case the printing is followed by a newline character. + +When the program finishes running, main returns with a 0 return code if there were no errors and with a 1 return code otherwise. + +Note: the program must not print anything other than then the values specified.", +63c6478b5f004bf4898cf4aa,2023-01-18T06:17:09.656Z,Help Fix Code,v1,,"int main() { + char phone[11]; + int i; + int result_to_check; + + scanf(""%s"", phone); + + do { + result_to_check = scanf(""%d"", &i); + + if (i == -1) { + printf(""%s\n"", phone); + } + else if (0 <= i && i <= 9) { + printf(""%c\n"", phone[i]); + } + else { + printf(""ERROR""); + return 1; + } + } + while (result_to_check != EOF); + + return 0; +}","Your task is to write a C program called phone_loop.c. This program will again read from standard input using scanf and take no command-line arguments. Similar to phone.c, this program reads a 10-character string as the first input value but then it repeatedly reads integers until standard input is closed. (Hint: Use a while loop that checks the return value of scanf to accomplish this. Check the man page for scanf to determine the return value when the input stream is closed.) + +After each integer the output produced is as before: + +if the integer is -1, the full string is printed +if the integer is between 0 and 9, the individual character at that position is printed +if the integer is less than -1 or greater than 9, the message ""ERROR"" is printed (to stdout) +In each case the printing is followed by a newline character. + +When the program finishes running, main returns with a 0 return code if there were no errors and with a 1 return code otherwise. + +Note: the program must not print anything other than then the values specified.", +63c6478c5f004bf4898cf4af,2023-01-21T05:17:53.406Z,General Question,v1,"what does ls -s do +",,, +63c6478c5f004bf4898cf4af,2023-01-21T05:44:44.838Z,General Question,v1,"what does the number next to total in the output of ls -l mean +",,, +63c6478c5f004bf4898cf4af,2023-01-21T06:21:26.514Z,General Question,v1,"what is strtod in c +",,, +63c6478c5f004bf4898cf4af,2023-01-21T06:39:37.920Z,General Question,v1,why is the first argument of argv indexed at 1,,, +63c6478c5f004bf4898cf4af,2023-01-21T06:41:55.803Z,General Question,v1,"when creating strings in C, do we need one extra element to hold any special characters?",,, +63c6478c5f004bf4898cf4af,2023-01-21T06:49:16.413Z,General Question,v1,how do I take in a string as an argument,,, +63c6478c5f004bf4898cf4af,2023-01-21T06:54:55.484Z,General Question,v1,generate a detailed documentation of `fgets` with usage examples and explanations,,, +63c6478c5f004bf4898cf4af,2023-01-21T07:38:32.368Z,General Question,v1,how do I store a 5 length string from the command line argument,,, +63c6478c5f004bf4898cf4af,2023-01-21T07:41:14.230Z,General Question,v1,does c break conditionals early with &&,,, +63c6478c5f004bf4898cf4af,2023-01-21T07:51:36.866Z,General Question,v1,how do I skip a line from the input with scanf,,, +63c6478c5f004bf4898cf4af,2023-01-21T08:08:44.352Z,General Question,v1,how do I split a string,,, +63c6478c5f004bf4898cf4af,2023-01-21T08:31:11.107Z,General Question,v1,string formatting doucmentation,,, +63c6478c5f004bf4898cf4af,2023-01-21T08:41:47.326Z,General Question,v1,"is this valid +permission[10] = ""---------\0"";",,, +63c6478c5f004bf4898cf4af,2023-01-21T09:03:38.625Z,General Question,v1,print without special characters,,, +63c6478c5f004bf4898cf4af,2023-01-21T09:16:28.857Z,General Question,v1,how do you properly compare strings in c,,, +63c6478c5f004bf4898cf4af,2023-01-21T09:23:05.125Z,Question from Code,v1,how would I properly point to the parameters ,"int check_permissions(char *current_perms, char *required_perms){ + int i; + for (i = 0; i < 9; i++) { + if (strcmp(required_perms[i], ""-"") == 0 && strcmp(current_perms[i], ""-"") == 0) { + return 1; + } + } + return 0; +}",, +63c6478c5f004bf4898cf4af,2023-01-21T09:27:28.336Z,General Question,v1,how do I compare a string literal with a element of a char array,,, +63c6478c5f004bf4898cf4af,2023-01-21T09:41:02.596Z,Question from Code,v1,what does this format mean,%*[^\n],, +63c6478c5f004bf4898cf4af,2023-01-21T09:41:46.825Z,General Question,v1,can you explain %*[^\n] in detail,,, +63c6478c5f004bf4898cf4af,2023-01-23T02:01:23.510Z,General Question,v1,"typecast bool to int in c +",,, +63c6478c5f004bf4898cf4af,2023-01-23T02:08:47.091Z,General Question,v1,how do I call helper functions from another file,,, +63c6478c5f004bf4898cf4af,2023-01-23T02:23:15.638Z,General Question,v1,does c automatically convert integers to bools in conditionals,,, +63c6478c5f004bf4898cf4af,2023-01-24T23:40:56.421Z,Question from Code,v1,is this valid,"int *build_array(char **strs, int size) { + int **array_pointer; + array_pointer = malloc(sizeof(int) * size); + int i = 0; + for (i = 0, i < size;, i++) { + strtol((*array_pointer)[i], NULL, 10); + } + return array_pointer; +}",, +63c6478c5f004bf4898cf4af,2023-02-01T00:57:35.283Z,General Question,v1,"provide documentation for strncmp +",,, +63c6478c5f004bf4898cf4af,2023-02-01T01:06:10.206Z,General Question,v1,does argc include the name of the file,,, +63c6478c5f004bf4898cf4af,2023-02-01T01:28:52.955Z,Help Fix Code,v1,,"if (strlen(argv[1]) == strlen(argv[2]) && strncmp(argv[1], argv[2], strlen(argv[1]) == 0)) { + printf(""Same\n""); + }",it should compare if the 2 strings are equal, +63c6478c5f004bf4898cf4af,2023-02-01T02:10:47.376Z,General Question,v1,difference between string literal and constant,,, +63c6478c5f004bf4898cf4af,2023-02-01T02:13:42.922Z,General Question,v1,does adding a null terminator in the middle of a stack allocated string change the pointer of the stack,,, +63c6478c5f004bf4898cf4af,2023-02-01T02:25:23.315Z,General Question,v1,how do you check if 2 strings are equal,,, +63c6478c5f004bf4898cf4af,2023-02-01T02:27:04.131Z,Explain Code,v1,,"char *a = argv[1]; +char *b = argv[2]; +if (*a == *b) { + printf(""Same\n""); +}",, +63c6478c5f004bf4898cf4af,2023-02-10T19:57:29.226Z,General Question,v1,"how do I check for memory leaks +",,, +63c6478c5f004bf4898cf4af,2023-02-15T00:32:11.598Z,General Question,v1,provide code for basic linked list traversal pattern,,, +63c6478c5f004bf4898cf4af,2023-02-15T00:42:49.535Z,General Question,v1,c do while,,, +63c6478c5f004bf4898cf4af,2023-02-15T01:02:08.432Z,General Question,v1,how do you vie stderror,,, +63c6478c5f004bf4898cf4af,2023-02-15T01:32:38.840Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + // Check username length + if (strlen(name) > MAX_NAME - 1) { + return 2; + } + User * curr = *user_ptr_add; + // Check empty list + if (curr == NULL) { + User *new_user = malloc(sizeof(User)); + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME-1] = '\0'; + * user_ptr_add = new_user; + return 0; + } + do { // Guarantee check first element + if (strcmp(curr->name, name) == 0) { + return 1; + } + curr = curr->next; + } while (curr != NULL); + // Add new User + User *new_user = malloc(sizeof(User)); + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME-1] = '\0'; + curr->next = new_user; + return 0; +} +",accepts a string up to length MAX_NAME, +63c6478c5f004bf4898cf4af,2023-02-15T02:56:28.050Z,General Question,v1,waht does const do in parameter type ,,, +63c6478c5f004bf4898cf4af,2023-02-15T05:27:33.566Z,General Question,v1,how to use time(),,, +63c6478c5f004bf4898cf4af,2023-02-15T19:28:47.771Z,General Question,v1,how do you free a struct,,, +63c6478c5f004bf4898cf4af,2023-02-15T19:32:27.011Z,General Question,v1,can I free a pointer but not the object it refers too if I have a copy of the pointer,,, +63c6478c5f004bf4898cf4af,2023-02-15T19:35:51.618Z,General Question,v1,does freeing a char array free the whole array,,, +63c6478c5f004bf4898cf4af,2023-02-21T03:35:27.079Z,General Question,v1,why does fork() not loop infinitely in a program,,, +63c6478c5f004bf4898cf4af,2023-03-15T07:35:49.049Z,Help Write Code,v2,,,,how do you allocate tokens from strtok into an array +63c6478c5f004bf4898cf4af,2023-03-15T16:17:28.903Z,General Question,v2,what is the syntax for a struct pointer,,, +63c6478f5f004bf4898cf4c3,2023-01-18T00:53:11.435Z,Help Fix Code,v1,,"/****************************************************************************** + + Online C Compiler. + Code, Compile, Run and Debug C program online. +Write your code in this editor and press ""Run"" button to compile and execute it. + +*******************************************************************************/ + +#include +int main(){ + int a[10]; + int size = 10; + change(a, size); +} + +static double change(int *a, int size){ + int sum; + double average; + for(int i = 0; i < size; i++){ + a[i] = a[i]+10; + sum += a[i]; + } + average = sum / size; + return average; +}",, +63c6478f5f004bf4898cf4c3,2023-01-18T00:54:22.190Z,Help Fix Code,v1,,"/****************************************************************************** + + Online C Compiler. + Code, Compile, Run and Debug C program online. +Write your code in this editor and press ""Run"" button to compile and execute it. + +*******************************************************************************/ + +#include +int main(){ + int a[10]; + int size = 10; + change(a, size); +} + +static double change(int *a, int size){ + int sum; + double average; + for(int i = 0; i < size; i++){ + a[i] = a[i]+10; + sum += a[i]; + } + average = sum / size; + return average; +} +","In the space below, write a small program that allocates an array of integers in the main function and passes +that array to a function called change. (You’ll also need to pass in the length of the array – why?) The function +should do two things: +• Add 10 to each element of the array. +• Return the average of the new contents of the array. +", +63c6478f5f004bf4898cf4c3,2023-01-18T01:28:32.815Z,General Question,v1,How do I use fgets on an int,,, +63c6478f5f004bf4898cf4c3,2023-01-18T01:38:04.293Z,General Question,v1,"Your task is to write a C program called phone_loop.c. This program will again read from standard input using scanf and take no command-line arguments. Similar to phone.c, this program reads a 10-character string as the first input value but then it repeatedly reads integers until standard input is closed. (Hint: Use a while loop that checks the return value of scanf to accomplish this. Check the man page for scanf to determine the return value when the input stream is closed.) + +After each integer the output produced is as before: + +if the integer is -1, the full string is printed +if the integer is between 0 and 9, the individual character at that position is printed +if the integer is less than -1 or greater than 9, the message ""ERROR"" is printed (to stdout) +In each case the printing is followed by a newline character. + +When the program finishes running, main returns with a 0 return code if there were no errors and with a 1 return code otherwise. + +Note: the program must not print anything other than then the values specified.",,, +63c6478f5f004bf4898cf4c3,2023-01-18T01:39:23.705Z,Help Write Code,v1,,,,"Your task is to write a C program called phone_loop.c. This program will again read from standard input using scanf and take no command-line arguments. Similar to phone.c, this program reads a 10-character string as the first input value but then it repeatedly reads integers until standard input is closed. (Hint: Use a while loop that checks the return value of scanf to accomplish this. Check the man page for scanf to determine the return value when the input stream is closed.) + +After each integer the output produced is as before: + +if the integer is -1, the full string is printed +if the integer is between 0 and 9, the individual character at that position is printed +if the integer is less than -1 or greater than 9, the message ""ERROR"" is printed (to stdout) +In each case the printing is followed by a newline character. + +When the program finishes running, main returns with a 0 return code if there were no errors and with a 1 return code otherwise. + +Note: the program must not print anything other than then the values specified." +63c6478f5f004bf4898cf4c3,2023-01-18T02:00:04.785Z,Help Fix Code,v1,,"// Your task is to write a small C program called phone.c that uses scanf to read two values from standard input. +#include + +int main() { + char phone[11]; + printf(""Type in a phone number:\n""); + fgets(phone, 11, stdin); + + int number; + printf(""Type in a number:\n""); + scanf(""%d"", &number); + + if (number == -1){ + printf(""%s\n"", phone); + } + else if ((0 <= number)&&(number <= 9)){ + printf(""%d\n"", phone[number]); + } + else { + printf(""ERROR""); + return 1; + } + + return 0; +}","Your task is to write a small C program called phone.c that uses scanf to read two values from standard input. The first is a 10 character string and the second is an integer. The program takes no command-line arguments. (You will write this program from scratch, so remember to add, commit, and push.) + +If the integer is -1, the program prints the full string to stdout. If the integer i is between 0 and 9, the program prints only the corresponding character (i.e., at index i) from the string to stdout. In both of these cases the program returns 0. + +If the integer is less than -1 or greater than 9, the program prints the message ""ERROR"" to stdout and returns 1.", +63c6478f5f004bf4898cf4c3,2023-01-18T02:02:04.096Z,Help Fix Code,v1,,"// Your task is to write a small C program called phone.c that uses scanf to read two values from standard input. +#include + +int main() { + char phone[11]; + printf(""Type in a phone number:\n""); + fgets(phone, 11, stdin); + + int number; + printf(""Type in a number:\n""); + scanf(""%d"", &number); + + if (number == -1){ + printf(""%s\n"", phone); + } + else if ((0 <= number)&&(number <= 9)){ + printf(""%d\n"", phone[number]); + } + else { + printf(""ERROR""); + return 1; + } + + return 0; +} ",It is supposed to print the digit of the phone number corresponding to the variable number, +63c6478f5f004bf4898cf4c3,2023-01-26T08:20:21.698Z,Help Fix Code,v1,,"int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + + char* perms = argv[0]; + int sizeLimit = stol(argv[1]) + + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + char permissions[10]; + int links; + char owner[30]; + char group[30]; + int size; + char dateMonth[10]; + int dateDay; + char dateTime[20]; + char file_name[10]; + scanf(); + int biggerFiles = 0; + while (scanf(""%s %d %s %s %d %s %d %s %s"", permissions, links, owner, group, size, dateMonth, dateDay, dateTime, file_name) == 9) { + if (size > sizeLimit) { + if (check_permissions(perms, permissions)) { + biggerFiles++; + } + } + } + printf(""%d"", biggerFiles); + + return 0; +}",work, +63c6478f5f004bf4898cf4c3,2023-01-26T08:22:06.102Z,Help Fix Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +int check_permissions(char *, char *){ + +}; + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + + char* perms = argv[0]; + int sizeLimit = stol(argv[1]) + + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + char permissions[10]; + int links; + char owner[30]; + char group[30]; + int size; + char dateMonth[10]; + int dateDay; + char dateTime[20]; + char file_name[10]; + scanf(); + int biggerFiles = 0; + while (scanf(""%s %d %s %s %d %s %d %s %s"", permissions, links, owner, group, size, dateMonth, dateDay, dateTime, file_name) == 9) { + if (size > sizeLimit) { + if (check_permissions(perms, permissions)) { + biggerFiles++; + } + } + } + printf(""%d"", biggerFiles); + + return 0; +}",work, +63c6478f5f004bf4898cf4c3,2023-01-26T08:32:15.548Z,Help Fix Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +int check_permissions(char *perm1, char *perm2){ + for(int i = 0; i < 10; i++){ + if (perm1[i] == '-'){ + continue; + } + if (perm1[i] != perm2[i]){ + return 0; + } + } + return 1; +}; + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + + char* perms = argv[0]; + int sizeLimit = stol(argv[1]) + + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + char permissions[10]; + int links; + char owner[30]; + char group[30]; + int size; + char dateMonth[10]; + int dateDay; + char dateTime[20]; + char file_name[10]; + scanf(); + int biggerFiles = 0; + while (scanf(""%s %d %s %s %d %s %d %s %s"", &permissions, &links, &owner, &group, &size, &dateMonth, &dateDay, &dateTime, &file_name) == 9) { + if (size > sizeLimit) { + if (check_permissions(perms, permissions)) { + biggerFiles++; + } + } + } + printf(""%d"", biggerFiles); + + return 0; +} +",work, +63c6478f5f004bf4898cf4c3,2023-01-26T08:37:57.638Z,Help Fix Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +int check_permissions(char *perm1, char *perm2){ + for(int i = 0; i < 10; i++){ + if (perm1[i] == '-'){ + continue; + } + if (perm1[i] != perm2[i]){ + return 0; + } + } + return 1; +}; + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + + char *perms = argv[0]; + int sizeLimit = atol(argv[1]); + + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + char *permissions[10]; + int links; + char owner[30]; + char group[30]; + int size; + char dateMonth[10]; + int dateDay; + char dateTime[20]; + char file_name[10]; + scanf(); + int biggerFiles = 0; + while (scanf(""%s %d %s %s %d %s %d %s %s"", *permissions, &links, &owner, &group, &size, &dateMonth, &dateDay, &dateTime, &file_name) == 9) { + if (size > sizeLimit) { + if (check_permissions(perms, permissions)) { + biggerFiles++; + } + } + } + printf(""%d"", biggerFiles); + + return 0; +} +",work, +63c6478f5f004bf4898cf4c3,2023-01-26T08:44:04.822Z,Help Fix Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +int check_permissions(char *perm1, char *perm2){ + for(int i = 0; i < 10; i++){ + if (perm1[i] == '-'){ + continue; + } + if (perm1[i] != perm2[i]){ + return 0; + } + } + return 1; +}; + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + + char *perms = argv[0]; + int sizeLimit = atol(argv[1]); + + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + char permissions[10]; + int links; + char owner[30]; + char group[30]; + int size; + char dateMonth[10]; + int dateDay; + char dateTime[20]; + char file_name[10]; + scanf(); + int biggerFiles = 0; + while (scanf(""%s %d %s %s %d %s %d %s %s"", permissions, links, owner, group, size, dateMonth, dateDay, dateTime, file_name) == 9) { + if (size > sizeLimit) { + if (check_permissions(perms, permissions)) { + biggerFiles++; + } + } + } + printf(""%d"", biggerFiles); + + return 0; +} +",work, +63c6478f5f004bf4898cf4c3,2023-01-26T09:00:10.833Z,Help Fix Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +int check_permissions(char *perm1, char *perm2){ + for(int i = 0; i < 10; i++){ + if (perm1[i] == '-'){ + continue; + } + if (perm1[i] != perm2[i]){ + return 0; + } + } + return 1; +}; + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + + char perms[10]; + int sizeLimit = atol(argv[1]); + + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + if (argc == 2) { + *perms = ""---------""; + } else { + for (int i = 0; i < 9; i++) { + perms[i] = argv[2][i]; + } + } + char permissions[10]; + int links; + char owner[30]; + char group[30]; + int size; + char dateMonth[10]; + int dateDay; + char dateTime[20]; + char file_name[10]; + scanf(""%s %d""); + int biggerFiles = 0; + while (scanf(""%s %*d %s %s %*d %s %*d %s %s"", permissions, links, owner, group, size, dateMonth, dateDay, dateTime, file_name) == 9) { + if (size > sizeLimit) { + if (check_permissions(perms, permissions)) { + biggerFiles++; + } + } + } + printf(""%d"", biggerFiles); + + return 0; +} +",work, +63c6478f5f004bf4898cf4c3,2023-01-26T09:24:56.902Z,Help Fix Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +int check_permissions(char *perm1, char *perm2){ + for(int i = 0; i < 10; i++){ + if (perm2[i] == '-'){ + continue; + } + if (perm1[i] != perm2[i]){ + return 0; + } + } + return 1; +}; + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + + char *perms = ""----------""; + int sizeLimit = atol(argv[1]); + + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + if (argc == 3) { + for (int i = 0; i < 10; i++) { + perms[i] = argv[2][i]; + } + } + char *permissions = malloc(10 * sizeof(char)); + int size; + scanf(""%*s %*d""); + int biggerFiles = 0; + while (scanf(""%s %*d %*s %*s %d %*s %*d %*s %*s"", permissions, &size) == 2) { + if (size > sizeLimit) { + if (check_permissions(permissions, perms)) { + biggerFiles++; + } + } + } + printf(""%d \n"", biggerFiles); + + return 0; +} +",work, +63c6478f5f004bf4898cf4c3,2023-01-26T17:46:54.384Z,General Question,v1,How do I use functions defined in another file?,,, +63c6478f5f004bf4898cf4c3,2023-02-01T00:24:21.321Z,General Question,v1,"When do I use -> vs . +",,, +63c6478f5f004bf4898cf4c3,2023-02-01T00:25:35.178Z,General Question,v1,When do I use -> vs . ? Provide examples,,, +63c6478f5f004bf4898cf4c3,2023-02-08T01:27:13.157Z,Help Write Code,v1,,,,"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." +63c6478f5f004bf4898cf4c3,2023-02-08T01:29:21.598Z,Help Write Code,v1,,,,"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. +4. Return the address of the first `struct pixel *` you initialized." +63c6478f5f004bf4898cf4c3,2023-02-25T22:04:09.792Z,General Question,v1,"What are default values for variables in C +",,, +63c6478f5f004bf4898cf4c3,2023-02-25T22:08:02.997Z,General Question,v1,Does a program terminate when main returns? Justify your answer.,,, +63c6478f5f004bf4898cf4c3,2023-02-27T22:50:22.460Z,Question from Code,v1,Does this work,"int** code = {{0, 1}, {1, 2}}; +int num = code[0][0]; +int*** codeptr = &#",, +63c6478f5f004bf4898cf4c3,2023-02-27T22:50:50.246Z,Question from Code,v1,Why doesn't this work,"int** code = {{0, 1}, {1, 2}}; +int num = code[0][0]; +int*** codeptr = &#",, +63c6478f5f004bf4898cf4c3,2023-02-27T22:51:38.260Z,Question from Code,v1,Why is line 3 invalid? Explain using 3-5 sentences.,"int** code[2][2] = {{0, 1}, {1, 2}}; +int num = code[0][0]; +int*** codeptr = &#",, +63c6478f5f004bf4898cf4c3,2023-02-27T22:52:11.838Z,Question from Code,v1,Does this work? Explain in 3-5 sentences.,"int** code[2][2] = {{0, 1}, {1, 2}}; +int num = code[0][0]; +int** codeptr = &#",, +63c6478f5f004bf4898cf4c3,2023-02-27T22:52:49.150Z,Question from Code,v1,Does this work?,"int code[2][2] = {{0, 1}, {1, 2}}; +int num = code[0][0]; +int** codeptr = &#",, +63c6478f5f004bf4898cf4c3,2023-02-27T22:53:36.183Z,Question from Code,v1,Explain why this code does not work in 3-5 sentences.,"int code[2][2] = {{0, 1}, {1, 2}}; +int num = code[0][0]; +int** codeptr = &#",, +63c6478f5f004bf4898cf4c3,2023-02-27T22:54:04.553Z,Question from Code,v1,Does this work?,"int code[2][2] = {{0, 1}, {1, 2}}; +int num = code[0][0]; +int** codeptr = &&#",, +63c6478f5f004bf4898cf4c3,2023-02-27T22:54:29.353Z,General Question,v1,Can I ever have 2 or more & in a row?,,, +63c6478f5f004bf4898cf4c3,2023-03-08T02:14:16.379Z,Help Fix Code,v1,,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" +/* +Your task is to complete checkpasswd.c, which reads a user id and password from stdin, +creates a new process to run the validate program, sends it the user id and password, +and prints a message to stdout reporting whether the validation is successful. + +Your program should use the exit status of the validate program to determine which of +the three following messages to print: + +""Password verified"" if the user id and password match. +""Invalid password"" if the user id exists, but the password does not match. +""No such user"" if the user id is not recognized +The exact messages are given in the starter code as defined constants. + +The only case that should be handled directly in checkpasswd is the case where either the +userid or password are too long. This is to prevent sending the wrong number of bytes to validate. + +Note that in the given password file pass.txt, the ""killerwhales:swim"" has a user id that +is too large, and ""monkeys:eatcoconuts"" has a password that is too long. The examples are expected +to fail, but the other cases should work correctly. + +You will find the following system calls useful: fork, exec, pipe, dup2, write, wait (along with +WIFEXITED, WEXITSTATUS). You may not use popen or pclose in your solution. + +Important: execl arguments +Week 7 Video 6 ""Running Different Programs"" demonstrates a version of execl that takes only two +arguments. The signature for execl is: + +int execl(const char *path, const char *arg0, ... *, (char *)NULL ); +In the video, the execl call only passed two arguments (execl(""./hello"", NULL)), but that shortcut +doesn't work on teach.cs. Instead, you need to pass the middle argument (respresenting argv[0]) +explicitly: execl("".hello"", ""hello"", NULL). + +Let's consider two more examples. If you want to call the executable ./giant with the arguments fee fi fo, +you would do it like this: execl("".giant"", ""giant"", ""fee"", ""fi"", ""fo"", NULL); If you want to call ./giant +with no arguments you would call it like this: execl("".giant"", ""giant"", NULL); +*/ + +int main(void) +{ + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if (fgets(user_id, MAXLINE, stdin) == NULL) + { + perror(""fgets""); + exit(1); + } + if (fgets(password, MAXLINE, stdin) == NULL) + { + perror(""fgets""); + exit(1); + } + + if (strlen(user_id) > MAXLINE || strlen(password) > MAXLINE) + { + exit(1); + } + + int fd[2]; + pipe(fd); + + pid_t pid = fork(); + if (pid < 0) + { + perror(""fork""); + exit(1); + } + else if (pid == 0) + { + dup2(fd[1], STDOUT_FILENO); + + close(fd[0]); + close(fd[1]); + + execl(""./validate"", ""validate"", user_id, password, NULL); + perror(""execl""); + exit(1); + } + else + { + int status; + wait(&status); + + int exit_status = WEXITSTATUS(status); + + switch (exit_status) + { + case 0: + printf(SUCCESS); + break; + case 1: + printf(INVALID); + break; + case 2: + printf(INVALID); + break; + case 3: + printf(NO_USER); + break; + default: + fprintf(stderr, ""Unknown exit status: %d\n"", exit_status); + exit(1); + } + } + + return 0; +}","Your task is to complete checkpasswd.c, which reads a user id and password from stdin, +creates a new process to run the validate program, sends it the user id and password, +and prints a message to stdout reporting whether the validation is successful. + +Your program should use the exit status of the validate program to determine which of +the three following messages to print: + +""Password verified"" if the user id and password match. +""Invalid password"" if the user id exists, but the password does not match. +""No such user"" if the user id is not recognized +The exact messages are given in the starter code as defined constants. + +The only case that should be handled directly in checkpasswd is the case where either the +userid or password are too long. This is to prevent sending the wrong number of bytes to validate. + +Note that in the given password file pass.txt, the ""killerwhales:swim"" has a user id that +is too large, and ""monkeys:eatcoconuts"" has a password that is too long. The examples are expected +to fail, but the other cases should work correctly. + +You will find the following system calls useful: fork, exec, pipe, dup2, write, wait (along with +WIFEXITED, WEXITSTATUS). You may not use popen or pclose in your solution. + +Important: execl arguments +Week 7 Video 6 ""Running Different Programs"" demonstrates a version of execl that takes only two +arguments. The signature for execl is: + +int execl(const char *path, const char *arg0, ... *, (char *)NULL ); +In the video, the execl call only passed two arguments (execl(""./hello"", NULL)), but that shortcut +doesn't work on teach.cs. Instead, you need to pass the middle argument (respresenting argv[0]) +explicitly: execl("".hello"", ""hello"", NULL). + +Let's consider two more examples. If you want to call the executable ./giant with the arguments fee fi fo, +you would do it like this: execl("".giant"", ""giant"", ""fee"", ""fi"", ""fo"", NULL); If you want to call ./giant +with no arguments you would call it like this: execl("".giant"", ""giant"", NULL); +*/ + +int main(void) +{ + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + ", +63c6478f5f004bf4898cf4c3,2023-03-08T02:15:09.790Z,Question from Code,v1,why doesn't this work?,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" +/* +Your task is to complete checkpasswd.c, which reads a user id and password from stdin, +creates a new process to run the validate program, sends it the user id and password, +and prints a message to stdout reporting whether the validation is successful. + +Your program should use the exit status of the validate program to determine which of +the three following messages to print: + +""Password verified"" if the user id and password match. +""Invalid password"" if the user id exists, but the password does not match. +""No such user"" if the user id is not recognized +The exact messages are given in the starter code as defined constants. + +The only case that should be handled directly in checkpasswd is the case where either the +userid or password are too long. This is to prevent sending the wrong number of bytes to validate. + +Note that in the given password file pass.txt, the ""killerwhales:swim"" has a user id that +is too large, and ""monkeys:eatcoconuts"" has a password that is too long. The examples are expected +to fail, but the other cases should work correctly. + +You will find the following system calls useful: fork, exec, pipe, dup2, write, wait (along with +WIFEXITED, WEXITSTATUS). You may not use popen or pclose in your solution. + +Important: execl arguments +Week 7 Video 6 ""Running Different Programs"" demonstrates a version of execl that takes only two +arguments. The signature for execl is: + +int execl(const char *path, const char *arg0, ... *, (char *)NULL ); +In the video, the execl call only passed two arguments (execl(""./hello"", NULL)), but that shortcut +doesn't work on teach.cs. Instead, you need to pass the middle argument (respresenting argv[0]) +explicitly: execl("".hello"", ""hello"", NULL). + +Let's consider two more examples. If you want to call the executable ./giant with the arguments fee fi fo, +you would do it like this: execl("".giant"", ""giant"", ""fee"", ""fi"", ""fo"", NULL); If you want to call ./giant +with no arguments you would call it like this: execl("".giant"", ""giant"", NULL); +*/ + +int main(void) +{ + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if (fgets(user_id, MAXLINE, stdin) == NULL) + { + perror(""fgets""); + exit(1); + } + if (fgets(password, MAXLINE, stdin) == NULL) + { + perror(""fgets""); + exit(1); + } + + if (strlen(user_id) > MAXLINE || strlen(password) > MAXLINE) + { + exit(1); + } + + int fd[2]; + pipe(fd); + + pid_t pid = fork(); + if (pid < 0) + { + perror(""fork""); + exit(1); + } + else if (pid == 0) + { + dup2(fd[1], STDOUT_FILENO); + + close(fd[0]); + close(fd[1]); + + execl(""./validate"", ""validate"", user_id, password, NULL); + perror(""execl""); + exit(1); + } + else + { + int status; + wait(&status); + + int exit_status = WEXITSTATUS(status); + + switch (exit_status) + { + case 0: + printf(SUCCESS); + break; + case 1: + printf(INVALID); + break; + case 2: + printf(INVALID); + break; + case 3: + printf(NO_USER); + break; + default: + fprintf(stderr, ""Unknown exit status: %d\n"", exit_status); + exit(1); + } + } + + return 0; +}",, +63c6478f5f004bf4898cf4c3,2023-03-15T04:53:28.948Z,Help Fix Code,v2,work,"#include#include#include#include""pmake.h""Rule*parse_file(FILE*fp){Rule*rules=makeRule();Rule*cur=rules;Action*cur_act=makeAction();Dependency*cur_dep=makeDependency();char*line=malloc(sizeof(char*)*MAXLINE);while(fgets(line,sizeof(MAXLINE),fp)){if(is_comment_or_empty(line)){continue;}char**args=parseLine(line);if(line[0]=='\t'){Action*cur_act=cur->actions;while(cur_act->next_act!=NULL){cur_act=cur_act->next_act;}cur_act->next_act=malloc(sizeof(Action));cur_act->next_act=makeAction();cur_act->next_act->args=args;}else{Rule*cur=rules;while(cur->next_rule!=NULL){cur=cur->next_rule;}for(inti=0;idependencies;while(searchRule->next_rule!=NULL){if(strcmp(searchRule->target,args[i])==0){if(i==0){cur=searchRule;break;}elseif(i>=2){while(cur_dep->next_dep!=NULL){cur_dep=cur_dep->next_dep;}cur_dep->rule=searchRule;cur_dep->next_dep=makeDependency();cur_dep=cur_dep->next_dep;break;}}searchRule=searchRule->next_rule;}if(searchRule->next_rule==NULL){if(i==0){Rule*oldcur=cur;cur->target=args[0];cur->dependencies=makeDependency();cur_dep=cur->dependencies;cur->actions=makeAction();for(intj=2;jrule=makeRule();cur_dep->rule->target=args[j];cur_dep->next_dep=makeDependency();cur_dep=cur_dep->next_dep;cur->next_rule=cur_dep->rule;cur=cur->next_rule;}cur->next_rule=makeRule();cur=oldcur;break;}elseif(i>=2){Rule*newcur=rules;while(newcur->next_rule!=NULL){newcur=newcur->next_rule;}Rule*new_rule=makeRule();new_rule->target=args[i];newcur->next_rule=new_rule;cur_dep->rule=new_rule;cur_dep->next_dep=makeDependency();cur_dep=cur_dep->next_dep;break;}}}}}returnrules;}",, +63c6478f5f004bf4898cf4c3,2023-03-15T04:56:43.280Z,Help Fix Code,v2,work,"Rule *parse_file(FILE *fp) { +Rule *rules = makeRule(); +Rule *cur = rules; +Action *cur_act = makeAction(); +Dependency *cur_dep = makeDependency(); +char *line = malloc(sizeof(char*) * MAXLINE); +while (fgets(line, sizeof(MAXLINE), fp)) { +if (is_comment_or_empty(line)) { +continue; +} +char** args = parseLine(line); +if (line[0] == '\t') { +Action *cur_act = cur->actions; +while (cur_act->next_act != NULL) { +cur_act = cur_act->next_act; +} +cur_act->next_act = malloc(sizeof(Action)); +cur_act->next_act = makeAction(); +cur_act->next_act->args = args; +} +else { +Rule *cur = rules; +while (cur->next_rule != NULL) { +cur = cur->next_rule; +} +for (int i = 0; i < sizeof(args); i++) { +Rule *searchRule = rules; +cur_dep = cur->dependencies; +while (searchRule->next_rule != NULL) { +if (strcmp(searchRule->target, args[i]) == 0) { +if (i == 0){ +cur = searchRule; +break; +} +else if (i >= 2) { +while (cur_dep->next_dep != NULL) { +cur_dep = cur_dep->next_dep; +} +cur_dep->rule = searchRule; +cur_dep->next_dep = makeDependency(); +cur_dep = cur_dep->next_dep; +break; +} +} +searchRule = searchRule->next_rule; +} +if (searchRule->next_rule == NULL) { +if (i == 0){ +Rule *oldcur = cur; +cur->target = args[0]; +cur->dependencies = makeDependency(); +cur_dep = cur->dependencies; +cur->actions = makeAction(); +for (int j = 2; j < sizeof(args); j++) { +cur_dep->rule = makeRule(); +cur_dep->rule->target = args[j]; +cur_dep->next_dep = makeDependency(); +cur_dep = cur_dep->next_dep; +cur->next_rule = cur_dep->rule; +cur = cur->next_rule; +} +cur->next_rule = makeRule(); +cur = oldcur; +break; +} +else if (i >= 2) { +Rule *newcur = rules; +while (newcur->next_rule != NULL) { +newcur = newcur->next_rule; +} +Rule *new_rule = makeRule(); +new_rule->target = args[i]; +newcur->next_rule = new_rule; +cur_dep->rule = new_rule; +cur_dep->next_dep = makeDependency(); +cur_dep = cur_dep->next_dep; +break; +} +} + +} +} +} +return rules; +}",, +63c6478f5f004bf4898cf4c3,2023-03-15T04:58:16.755Z,Explain Code,v2,,"Rule *parse_file(FILE *fp) { + Rule *rules = makeRule(); + Rule *cur = rules; + Action *cur_act = makeAction(); + Dependency *cur_dep = makeDependency(); + char *line = malloc(sizeof(char *) * MAXLINE); + while (fgets(line, sizeof(MAXLINE), fp)) { + if (is_comment_or_empty(line)) { + continue; + } + char **args = parseLine(line); + if (line[0] == '\t') { + Action *cur_act = cur->actions; + while (cur_act->next_act != NULL) { + cur_act = cur_act->next_act; + } + cur_act->next_act = malloc(sizeof(Action)); + cur_act->next_act = makeAction(); + cur_act->next_act->args = args; + } else { + Rule *cur = rules; + while (cur->next_rule != NULL) { + cur = cur->next_rule; + } + for (int i = 0; i < sizeof(args); i++) { + Rule *searchRule = rules; + cur_dep = cur->dependencies; + while (searchRule->next_rule != NULL) { + if (strcmp(searchRule->target, args[i]) == 0) { + if (i == 0) { + cur = searchRule; + break; + } else if (i >= 2) { + while (cur_dep->next_dep != NULL) { + cur_dep = cur_dep->next_dep; + } + cur_dep->rule = searchRule; + cur_dep->next_dep = makeDependency(); + cur_dep = cur_dep->next_dep; + break; + } + } + searchRule = searchRule->next_rule; + } + if (searchRule->next_rule == NULL) { + if (i == 0) { + Rule *oldcur = cur; + cur->target = args[0]; + cur->dependencies = makeDependency(); + cur_dep = cur->dependencies; + cur->actions = makeAction(); + for (int j = 2; j < sizeof(args); j++) { + cur_dep->rule = makeRule(); + cur_dep->rule->target = args[j]; + cur_dep->next_dep = makeDependency(); + cur_dep = cur_dep->next_dep; + cur->next_rule = cur_dep->rule; + cur = cur->next_rule; + } + cur->next_rule = makeRule(); + cur = oldcur; + break; + } else if (i >= 2) { + Rule *newcur = rules; + while (newcur->next_rule != NULL) { + newcur = newcur->next_rule; + } + Rule *new_rule = makeRule(); + new_rule->target = args[i]; + newcur->next_rule = new_rule; + cur_dep->rule = new_rule; + cur_dep->next_dep = makeDependency(); + cur_dep = cur_dep->next_dep; + break; + } + } + } + } + } + return rules; +}",, +63c6478f5f004bf4898cf4c3,2023-03-15T05:04:03.404Z,Help Fix Code,v2, ,"char** parseLine(char* line) { + // Parse the line + char** args = malloc(sizeof(char**)); + char* arg = strtok(line, "" ""); + // Parse the arguments and add them to args + while (arg != NULL) { + args = realloc(args, sizeof(char**) * (sizeof(args) + 1)); + args[sizeof(args)] = arg; + arg = strtok(NULL, "" ""); + } + return args; +}",, +63c6478f5f004bf4898cf4c3,2023-03-15T05:05:22.246Z,Help Fix Code,v2,"This should read the characters from line and generate an array of strings, one per argument","char** parseLine(char* line) { + // Parse the line + char** args = malloc(sizeof(char**)); + char* arg = strtok(line, "" ""); + // Parse the arguments and add them to args + while (arg != NULL) { + args = realloc(args, sizeof(char**) * (sizeof(args) + 1)); + args[sizeof(args)] = arg; + arg = strtok(NULL, "" ""); + } + return args; +}",, +63c6478f5f004bf4898cf4c3,2023-03-15T05:10:07.536Z,Help Fix Code,v2,Split the strings in line into an array using spaces and return a list of strings,"char** parseLine(char* line) { + // Parse the line + char** args = malloc(sizeof(char*)); + char* arg = strtok(line, "" ""); + // Parse the arguments and add them to args + int i = 0; + while (arg != NULL) { + args[i] = malloc(strlen(arg) + 1); + strcpy(args[i], arg); + i++; + args = realloc(args, sizeof(char*) * (i + 1)); + arg = strtok(NULL, "" ""); + } + args[i] = NULL; + return args; +}",, +63c6478f5f004bf4898cf4c3,2023-03-15T06:25:48.812Z,Help Fix Code,v2,Does this work?,"cur_dep->rule = makeRule(); +cur->next_rule = cur_dep->rule; +cur = cur->next_rule;",, +63c6478f5f004bf4898cf4c3,2023-04-05T05:44:17.297Z,Help Fix Code,v2,compile,"char* list_users(const User *curr) { + const User *og_user = curr; + char *user_list = NULL; + int size = 11; + while (curr != NULL) { + size += strlen(curr->name) + 3; + curr = curr->next; + } + + user_list = malloc(sizeof(char)*size); + + if (user_list == NULL) { + perror(""malloc""); + exit(1); + } + else { + strcat(user_list, ""User List\r\n""); + curr = og_user; + while (curr != NULL) { + strcat(user_list, ""\t""); + strcat(user_list, curr->name); + strcat(user_list, ""\r\n""); + curr = curr->next; + } + } + return user_list; +}",, +63c6478f5f004bf4898cf4c3,2023-04-25T20:20:54.854Z,General Question,v2,how do i do stuff for sockets,,, +63c647905f004bf4898cf4cd,2023-02-08T02:13:13.970Z,General Question,v1,"When I call gcc on a c file in the terminal, what file type is the output? +",,, +63c647945f004bf4898cf4e6,2023-01-17T18:13:54.207Z,General Question,v1,what is a pointer variable?,,, +63c647945f004bf4898cf4e6,2023-01-19T03:06:59.172Z,Question from Code,v1,are there any bugs?,"int main() { + char phone[11]; + int i; + + scanf(""%s"", phone); + scanf(""%d"", &i); + + if (i < -1 || i > 9) { + printf(""ERROR""); + return 1; + } else if (i == -1) { + printf(""%s"", phone); + } else { + printf(""%c"", phone[i]); + } + + return 0; +}",, +63c647945f004bf4898cf4e6,2023-01-25T20:49:41.822Z,Question from Code,v1,this is not working,"int **split_array(const int *s, int length) { + int arr[2]; + int numEven, numOdd; + + numOdd = length / 2; + if (length % 2 == 0) { + numEven = length / 2; + } else { + numEven = (length / 2) + 1; + } + + arr[0] = malloc(sizeof(int) * numEven); + arr[1] = malloc(sizeof(int) * numOdd); + + for (int i = 0; i < length; i = i + 2) { + arr[0][i] = s[i]; + } + + for (int j = 0; j < length; j = j + 2) { + arr[1][j] = s[j]; + } + + return arr; +}",, +63c647945f004bf4898cf4e6,2023-01-25T20:52:09.726Z,Question from Code,v1,not working,"int **split_array(const int *s, int length) { + int **arr[2]; + int numEven, numOdd; + + numOdd = length / 2; + if (length % 2 == 0) { + numEven = length / 2; + } else { + numEven = (length / 2) + 1; + } + + arr[0] = malloc(sizeof(int) * numEven); + arr[1] = malloc(sizeof(int) * numOdd); + + for (int i = 0; i < length; i = i + 2) { + arr[0][i] = s[i]; + } + + for (int j = 0; j < length; j = j + 2) { + arr[1][j] = s[j]; + } + + return arr; +}",, +63c647945f004bf4898cf4e6,2023-01-25T20:54:28.026Z,Question from Code,v1,not working,"int **split_array(const int *s, int length) { + int *arr[2]; + int numEven, numOdd; + + numOdd = length / 2; + if (length % 2 == 0) { + numEven = length / 2; + } else { + numEven = (length / 2) + 1; + } + + arr[0] = malloc(sizeof(int) * numEven); + arr[1] = malloc(sizeof(int) * numOdd); + + for (int i = 0; i < length; i = i + 2) { + arr[0][i] = s[i]; + } + + for (int j = 1; j < length; j = j + 2) { + arr[1][j] = s[j]; + } + + return arr; +}",, +63c647945f004bf4898cf4e6,2023-01-25T20:55:07.236Z,Question from Code,v1,not working,"int **split_array(const int *s, int length) { + int *arr[2]; + int numEven, numOdd; + + numOdd = length / 2; + if (length % 2 == 0) { + numEven = length / 2; + } else { + numEven = (length / 2) + 1; + } + + arr[0] = malloc(sizeof(int) * numEven); + arr[1] = malloc(sizeof(int) * numOdd); + + for (int i = 0; i < length; i = i + 2) { + arr[0][i] = s[i]; + } + + for (int j = 1; j < length; j = j + 2) { + arr[1][j] = s[j]; + } + + return &arr; +}",, +63c647945f004bf4898cf4e6,2023-01-25T21:01:59.961Z,Question from Code,v1,how do I return a pointer to an array of pointers to integers?,"int **split_array(const int *s, int length) { + int *arr[2]; + int numEven, numOdd; + + numOdd = length / 2; + if (length % 2 == 0) { + numEven = length / 2; + } else { + numEven = (length / 2) + 1; + } + + arr[0] = malloc(sizeof(int) * numEven); + arr[1] = malloc(sizeof(int) * numOdd); + + for (int i = 0; i < length; i = i + 2) { + arr[0][i] = s[i]; + } + + for (int j = 1; j < length; j = j + 2) { + arr[1][j] = s[j]; + } + + return arr; +}",, +63c647945f004bf4898cf4e6,2023-01-25T21:16:21.977Z,Explain Code,v1,,"int **split_array(const int *s, int length) { + int **arr = malloc(sizeof(int *) * 2); + int numEven, numOdd; + + numOdd = length / 2; + if (length % 2 == 0) { + numEven = length / 2; + } else { + numEven = (length / 2) + 1; + } + + arr[0] = malloc(sizeof(int) * numEven); + arr[1] = malloc(sizeof(int) * numOdd); + + for (int i = 0; i < length; i = i + 2) { + arr[0][i] = s[i]; + } + + for (int j = 1; j < length; j = j + 2) { + arr[1][j] = s[j]; + } + + return arr; +}",, +63c647945f004bf4898cf4e6,2023-01-28T19:02:31.512Z,General Question,v1,give an example of a program that uses both scanf and command-line arguments,,, +63c647945f004bf4898cf4e6,2023-01-28T19:13:29.960Z,General Question,v1,given an example of a program that uses scanf for several lines of input,,, +63c647945f004bf4898cf4e6,2023-01-28T19:31:31.380Z,General Question,v1,give an example of a program that uses scanf,,, +63c647945f004bf4898cf4e6,2023-03-03T01:46:12.658Z,Question from Code,v1,how many processes are forked?,"#include +#include +#include + +int main(int argc, char **argv) +{ + if (argc != 2) + { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + int original_process = getpid(); + + for (int i = 0; i < iterations; i++) + { + if (getpid() == original_process) + { + int n = fork(); + if (n < 0) + { + perror(""fork""); + exit(1); + } + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",, +63c647945f004bf4898cf4e6,2023-03-03T01:47:08.246Z,Question from Code,v1,describe the processes and their relationship to each other,"#include +#include +#include + +int main(int argc, char **argv) +{ + if (argc != 2) + { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + int original_process = getpid(); + + for (int i = 0; i < iterations; i++) + { + if (getpid() == original_process) + { + int n = fork(); + if (n < 0) + { + perror(""fork""); + exit(1); + } + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",, +63c647945f004bf4898cf4e6,2023-03-03T01:47:53.368Z,Question from Code,v1,describe the processes and their relationship to each other,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +",, +63c647945f004bf4898cf4e6,2023-03-09T22:01:51.217Z,General Question,v2,how do i use dup2,,, +63c647945f004bf4898cf4e6,2023-03-09T22:03:13.753Z,General Question,v2,how can i make the child process read two lines of pipe from the parent process in an execl call?,,, +63c647945f004bf4898cf4e6,2023-03-09T22:04:11.924Z,General Question,v2,how can i use the execl call for standard input,,, +63c647945f004bf4898cf4e6,2023-03-09T22:06:25.228Z,General Question,v2,i want to pass standard input from the parent process to a program being called in the child process using the execl call,,, +63c647945f004bf4898cf4e6,2023-03-09T22:13:15.623Z,General Question,v2,i have an execl call in my child process that reads from standard input. where do i close my file descriptors?,,, +63c647945f004bf4898cf4e6,2023-03-14T02:03:31.511Z,Help Fix Code,v2,remove potential segmentation fault,"Rule *create_rule(const char *target, Rule **starting_rule) { + printf(""Creating a new rule.\n""); + Rule *new_rule = malloc(sizeof(Rule)); + if (new_rule == NULL) { + perror(""malloc""); + exit(1); + } + new_rule->target = """"; + strcpy(new_rule->target, target); + new_rule->dependencies = NULL; + new_rule->actions = NULL; + new_rule->next_rule = NULL; + + if (*starting_rule == NULL) { + *starting_rule = new_rule; + } else { + Rule *current_rule = *starting_rule; + while (current_rule->next_rule != NULL) { + current_rule = current_rule->next_rule; + } + current_rule->next_rule = new_rule; + } + printf(""Finished creating the new rule.\n""); + return new_rule; +}",, +63c647955f004bf4898cf4f0,2023-01-26T00:48:40.125Z,General Question,v1,"how do i get a slice of an array in c +",,, +63c647955f004bf4898cf4f0,2023-01-26T00:49:25.748Z,General Question,v1,"how do i convert a char array to a string array in c +",,, +63c647965f004bf4898cf4f5,2023-01-24T20:43:34.837Z,General Question,v1,what is the difference between * and &?,,, +63c647965f004bf4898cf4f5,2023-02-09T20:48:31.958Z,Question from Code,v1,where is the segmentation fault?,"int create_user(const char *name, User **user_ptr_add) { + User *prev; + User *curr = *user_ptr_add; + if (strlen(name) > 31) { + return 2; + } else { + + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + return 1; + } + prev = curr; + curr = curr->next; + } + + User *new_user = malloc(sizeof(User)); + + strcpy(new_user->name, name); + + new_user->name[strlen(name)] = '\0'; + + new_user->profile_pic[0] = '\0'; + + new_user->first_post = NULL; + + for (int i = 0; i < MAX_FRIENDS; i++) { + (new_user->friends)[i] = NULL; + } + + new_user->next = NULL; + if (prev != NULL) { + prev->next = new_user; + } else { + user_ptr_add[0] = curr; + } + + return 0; + } +}",, +63c647965f004bf4898cf4f5,2023-02-09T20:50:00.620Z,Question from Code,v1,where is the segmentation fault?,"int create_user(const char *name, User **user_ptr_add) { + User *prev; + User *curr = *user_ptr_add; + if (strlen(name) > 31) { + return 2; + } else { + + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + return 1; + } + prev = curr; + curr = curr->next; + } + + User *new_user = malloc(sizeof(User)); + + strncpy(new_user->name, name, 31); + + new_user->name[strlen(name)] = '\0'; + + new_user->profile_pic[0] = '\0'; + + new_user->first_post = NULL; + + for (int i = 0; i < MAX_FRIENDS; i++) { + (new_user->friends)[i] = NULL; + } + + new_user->next = NULL; + if (prev != NULL) { + prev->next = new_user; + } else { + user_ptr_add[0] = curr; + } + + return 0; + } +}",, +63c647965f004bf4898cf4f5,2023-02-10T01:46:26.963Z,Question from Code,v1,"where is the segmentation fault? +","int create_user(const char *name, User **user_ptr_add) { + User *prev; + User *curr = *user_ptr_add; + if (strlen(name) > 31) { + return 2; + } else { + + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + return 1; + } + prev = curr; + curr = curr->next; + } + + User *new_user = malloc(sizeof(User)); + + strcpy(new_user->name, name); + + new_user->name[strlen(name)] = '\0'; + + new_user->profile_pic[0] = '\0'; + + new_user->first_post = NULL; + + for (int i = 0; i < MAX_FRIENDS; i++) { + (new_user->friends)[i] = NULL; + } + + new_user->next = NULL; + if (prev != NULL) { + prev->next = new_user; + } else { + user_ptr_add[0] = curr; + } + + return 0; + } +}",, +63c647965f004bf4898cf4f5,2023-02-10T01:47:31.026Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *prev; + User *curr = *user_ptr_add; + if (strlen(name) > 31) { + return 2; + } else { + + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + return 1; + } + prev = curr; + curr = curr->next; + } + + User *new_user = malloc(sizeof(User)); + + strcpy(new_user->name, name); + + new_user->name[strlen(name)] = '\0'; + + new_user->profile_pic[0] = '\0'; + + new_user->first_post = NULL; + + for (int i = 0; i < MAX_FRIENDS; i++) { + (new_user->friends)[i] = NULL; + } + + new_user->next = NULL; + if (prev != NULL) { + prev->next = new_user; + } else { + user_ptr_add[0] = curr; + } + + return 0; + } +}","fix segmentation fault +", +63c647965f004bf4898cf4f5,2023-02-10T01:58:01.300Z,Question from Code,v1,where is the segmentation fault?,"int create_user(const char *name, User **user_ptr_add) { + User *prev; + User *curr = *user_ptr_add; + if (strlen(name) > 31) { + return 2; + } else { + + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + return 1; + } + prev = curr; + curr = curr->next; + } + + User *new_user = malloc(sizeof(User)); + + strncat(new_user->name, name, 31); + + new_user->name[strlen(name)] = '\0'; + + new_user->profile_pic[0] = '\0'; + + new_user->first_post = NULL; + + for (int i = 0; i < MAX_FRIENDS; i++) { + (new_user->friends)[i] = NULL; + } + + new_user->next = NULL; + if (prev != NULL) { + prev->next = new_user; + } else { + *user_ptr_add = new_user; + } + + return 0; + } +} +",, +63c647965f004bf4898cf4f5,2023-02-10T02:03:31.340Z,Question from Code,v1,where is the segmentation fault,"int create_user(const char *name, User **user_ptr_add) { + User *prev; + User *curr = *user_ptr_add; + if (strlen(name) > 31) { + return 2; + } else { + + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + return 1; + } + prev = curr; + curr = curr->next; + } + + User *new_user = malloc(sizeof(User)); + + strncat(new_user->name, name, 32); + + new_user->name[31] = '\0'; + + (new_user->profile_pic)[0] = '\0'; + + new_user->first_post = NULL; + + for (int i = 0; i < MAX_FRIENDS; i++) { + (new_user->friends)[i] = NULL; + } + + new_user->next = NULL; + if (prev != NULL) { + prev->next = new_user; + } else { + *user_ptr_add = new_user; + } + + return 0; + } +}",, +63c647965f004bf4898cf4f5,2023-02-10T02:32:48.756Z,Question from Code,v1,where is the segmentation fault?,"int create_user(const char *name, User **user_ptr_add) { + User *prev; + User *curr = *user_ptr_add; + if (strlen(name) > 31) { + return 2; + } else { + + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + return 1; + } + prev = curr; + curr = curr->next; + } + + User *new_user = malloc(sizeof(User)); + + strncpy(new_user->name, name, 31); + + new_user->name[31] = '\0'; + + new_user->profile_pic[0] = '\0'; + + new_user->first_post = NULL; + + for (int i = 0; i < MAX_FRIENDS; i++) { + (new_user->friends)[i] = NULL; + } + + new_user->next = NULL; + if (prev != NULL) { + prev->next = new_user; + } else { + *user_ptr_add = new_user; + } + + return 0; + } +} +",, +63c647965f004bf4898cf4f5,2023-02-12T07:15:52.803Z,General Question,v1,how do i set a time_t?,,, +63c647995f004bf4898cf513,2023-01-17T21:56:34.400Z,Explain Code,v1,,"#include +int main() { + char c = 'b'; + // declare and set variable c_ptr so that the code below prints 'b' +// Submitted code below + int *c_ptr = &c; +// Submitted code above + printf(""%c"", *c_ptr); + return 0; +}",, +63c647995f004bf4898cf513,2023-04-09T19:01:33.032Z,General Question,v2,"I received an error when trying to run my executable on teach.cs, a shared Linux system. What is this error? How can it be fixed? + +/lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found",,, +63c647995f004bf4898cf518,2023-01-20T22:54:36.083Z,Help Fix Code,v1,,"int main(){ + char phone[11]; + int num; + scanf(""%s"", phone); + scanf(""%d"", &num); + + if (num == -1){ + printf(""%s\n"", phone); + return 0; + } else if (num < -1 || num > 9){ + printf(""ERROR\n""); + return 1; + } else{ + printf(""%c\n"", phone[num]); + return 0; + } +}",, +63c647995f004bf4898cf518,2023-01-20T22:55:32.863Z,Help Fix Code,v1,,"int main(){ + char phone[11]; + int num; + scanf(""%s"", phone); + scanf(""%d"", &num); + + if (num == -1){ + printf(""%s\n"", phone); + return 0; + } else if (num < -1 || num > 9){ + printf(""ERROR\n""); + return 1; + } else{ + printf(""%c\n"", phone[num]); + return 0; + } +}","This program will again read from standard input using scanf and take no command-line arguments. Similar to phone.c, this program reads a 10-character string as the first input value but then it repeatedly reads integers until standard input is closed. (Hint: Use a while loop that checks the return value of scanf to accomplish this. Check the man page for scanf to determine the return value when the input stream is closed.) + +After each integer the output produced is as before: + +if the integer is -1, the full string is printed +if the integer is between 0 and 9, the individual character at that position is printed +if the integer is less than -1 or greater than 9, the message ""ERROR"" is printed (to stdout) +In each case the printing is followed by a newline character. + +When the program finishes running, main returns with a 0 return code if there were no errors and with a 1 return code otherwise.", +63c647995f004bf4898cf518,2023-01-27T19:10:50.597Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + int *int_arr = malloc(sizeof(int) * size); + char *leftover; + strtol(*strs, &leftover, 10); + for (int i = 0; i < size; i++){ + int_arr[i] = strtol(leftover, &leftover, 10); + printf(""%d"", int_arr[i]); + } + return int_arr; + +}","Want to return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the +", +63c647995f004bf4898cf518,2023-02-12T19:42:43.707Z,Explain Code,v1,,"(var) |= 1 << (flag) +",, +63c647995f004bf4898cf518,2023-02-12T19:51:20.056Z,General Question,v1,"what is static inline? + +",,, +63c647995f004bf4898cf518,2023-02-12T20:01:58.811Z,Explain Code,v1,,"char * (*x) (char *) = my_func; +",, +63c647995f004bf4898cf518,2023-02-12T20:42:26.279Z,General Question,v1,"strtol does not set errno when it fails because it isn't a system call. +why the above sentence wrong +",,, +63c647995f004bf4898cf518,2023-02-12T20:43:12.503Z,General Question,v1,strtol does not set errno when it fails because it isn't a system call.,,, +63c647995f004bf4898cf518,2023-02-12T20:44:02.552Z,General Question,v1,will system call set errno?,,, +63c647995f004bf4898cf518,2023-03-02T22:17:49.404Z,Explain Code,v1,,"char name[5]; +strncat(name, ""Hi"", 2); +",, +63c647995f004bf4898cf518,2023-03-03T03:29:23.449Z,Question from Code,v1,"How many processes are created, including the original parent, when the program is called with 2, 3, and 4 as arguments? n arguments?","#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",, +63c647995f004bf4898cf518,2023-03-13T03:33:03.086Z,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; +}",, +63c647995f004bf4898cf518,2023-04-22T17:30:52.297Z,Question from Code,v2,"what value stores in y at the end +","char *result[2]; +x = result[0]; +result[0] = ""read only""; +y = x[0];",, +63c647995f004bf4898cf518,2023-04-22T21:16:08.192Z,General Question,v2,"Is string literal differ than a char +",,, +63c647995f004bf4898cf518,2023-04-22T21:19:20.869Z,Question from Code,v2,"When call fun('a') in main stack, will it allocate space for 'a'?","int fun(char i) { + ... +} +int main() { + fun(’a’); +}",, +63c647995f004bf4898cf518,2023-04-22T22:30:03.313Z,Question from Code,v2,what kind of error will the code has? what is the difference between run-time error and compile error?,"char *third = malloc(sizeof(char) * 10); +third = ""Wednesday""; +third[0] = 'w';",, +63c647995f004bf4898cf518,2023-04-23T20:14:48.702Z,Question from Code,v2,"if argv[1] is zero, what kind of error will it incur","int main(int argc, char **argv) { + char *next = NULL; + errno = 0; + long longi = strtol(argv[1], &next, 0); + int i = longi; + + printf(""longi: %ld\n"", longi); + printf(""i: %d\n"", i); + printf(""next is |%s|\n"", next); + + if (errno != 0) { + perror(""strtol""); + } + return 0; +}",, +63c647995f004bf4898cf518,2023-04-24T02:32:59.217Z,General Question,v2,what is the difference between execl and execvp,,, +63c647995f004bf4898cf518,2023-04-24T02:43:07.367Z,General Question,v2,what does dup2 do,,, +63c647995f004bf4898cf518,2023-04-24T15:15:13.184Z,General Question,v2,what do signal mask do,,, +63c647995f004bf4898cf518,2023-04-24T19:39:49.365Z,General Question,v2,what does accpet() do,,, +63c647995f004bf4898cf518,2023-04-24T19:45:37.542Z,General Question,v2,what does sprintf() do,,, +63c647995f004bf4898cf518,2023-04-24T20:45:47.574Z,General Question,v2,when will read() block,,, +63c647995f004bf4898cf518,2023-04-24T21:08:39.795Z,General Question,v2,what does htons() do,,, +63c647995f004bf4898cf518,2023-04-24T22:45:24.442Z,General Question,v2,what kind of error will occur is we do not double quote filename in shell programming?,,, +63c647995f004bf4898cf518,2023-04-25T02:40:12.345Z,General Question,v2,what are the attributes in struct called,,, +63c647995f004bf4898cf518,2023-04-25T02:47:02.253Z,General Question,v2,what does socket do,,, +63c647995f004bf4898cf518,2023-04-25T02:49:48.591Z,General Question,v2,what does port do,,, +63c647995f004bf4898cf518,2023-04-25T02:53:55.228Z,General Question,v2,what does listen() do,,, +63c647995f004bf4898cf518,2023-04-25T02:56:10.670Z,General Question,v2,what does dup2() do,,, +63c647995f004bf4898cf518,2023-04-25T03:05:56.175Z,General Question,v2,why we use fprintf to output stderr,,, +63c647995f004bf4898cf518,2023-04-25T03:14:28.730Z,General Question,v2,will main function always return int or can we set to be void,,, +63c647995f004bf4898cf518,2023-04-25T03:35:59.497Z,General Question,v2,does sizeof int different on different machine,,, +63c647995f004bf4898cf518,2023-04-25T04:01:36.405Z,General Question,v2,how to send SIGUSR1 signal to a process in command line,,, +63c647995f004bf4898cf518,2023-04-25T04:05:11.359Z,General Question,v2,"If we want to modify permission of myscript.sh, when we use chmod, why we just write myscript rather than myscript.sh",,, +63c647995f004bf4898cf518,2023-04-25T04:07:50.444Z,General Question,v2,what does each of three permissions mean,,, +63c647995f004bf4898cf518,2023-04-25T04:18:23.868Z,General Question,v2,what does /dev/null 2 /dev/null mean,,, +63c647995f004bf4898cf518,2023-04-25T04:28:09.259Z,General Question,v2,"in shell programming, when to use test and when do not",,, +63c647995f004bf4898cf518,2023-04-25T15:32:10.023Z,General Question,v2,can you show the process and the steps of creating and setting up a socket,,, +63c647995f004bf4898cf518,2023-04-25T18:37:01.773Z,Question from Code,v2,why the code below copies the fields of b into a.,"struct node a, b; +a = b;",, +63c647995f004bf4898cf518,2023-04-25T18:41:24.967Z,Question from Code,v2,why this does not cause memory leak,"struct node { + int val; + struct node *next; +}; +void free_list(struct node *head) { + while (head != NULL) { + free(head); + head = head->next; + }",, +63c647995f004bf4898cf518,2023-04-25T18:49:07.460Z,General Question,v2,will exit after execl executed?,,, +63c647995f004bf4898cf518,2023-04-25T19:25:35.158Z,General Question,v2,"what is the difference between execl, execlp, execv, execvp",,, +63c647995f004bf4898cf518,2023-04-25T20:42:19.567Z,General Question,v2,"when calling select, what kind of fds will remain in the set",,, +63c647995f004bf4898cf518,2023-05-15T21:08:27.573Z,Question from Code,v2,"what does this code mean +","list(product(self.NodeType, repeat = 2))",, +63c6479c5f004bf4898cf531,2023-01-20T03:22:23.415Z,Explain Code,v1,,"char **last_names; + last_names = malloc(sizeof(char*) * 4); + last_names[0] = malloc(sizeof(char)* 20); + last_names[1] = malloc(sizeof(char) * 20); + last_names[2] = malloc(sizeof(char) * 20); + last_names[3] = malloc(sizeof(char) * 20);",, +63c6479c5f004bf4898cf531,2023-01-20T03:23:43.064Z,General Question,v1,"explain **last_names +",,, +63c6479f5f004bf4898cf545,2023-01-18T22:07:17.912Z,Question from Code,v1,would this allocate the exact amount of memory of a float to allocated_amount,allocated_amount = malloc(sizeof(float));,, +63c6479f5f004bf4898cf545,2023-01-18T22:17:50.520Z,Question from Code,v1,how do you set the a variable x to the exact amount of space that should be allocated and pointed to by float *rainfall.,float *rainfall = null;,, +63c6479f5f004bf4898cf545,2023-01-26T04:03:47.048Z,Question from Code,v1,will this program compile,"void main(int arc, char**argv) { + char *size = argv[1]; + if(argv == 3) { + char*x = argv[2]; + } +}",, +63c6479f5f004bf4898cf545,2023-01-26T04:04:36.627Z,Question from Code,v1,will this compile,"void main(int arc, char**argv) { + char *size = argv[1]; + if(argc == 3) { + char*x = argv[2]; + } +}",, +63c6479f5f004bf4898cf545,2023-01-26T04:06:35.194Z,Question from Code,v1,will this compile,"int main(int argc, char**argv) { + if (!(argc == 2 || argc ==3)) { + fprintf(stderr, ""x""); + return 1; + } + char *size = argv[1]; + if(argc == 3) { + char*x = argv[2]; + } + return 0; +}",, +63c6479f5f004bf4898cf545,2023-01-26T04:08:47.450Z,General Question,v1,How would i redirect information from a file as standard input using scanf? ,,, +63c6479f5f004bf4898cf545,2023-01-26T04:09:56.203Z,General Question,v1,How would i redirect information from a file as standard input using scanf when i do not know the name of the file?,,, +63c6479f5f004bf4898cf545,2023-01-26T04:11:33.693Z,Help Write Code,v1,,,,"You should use scanf to read the input. +Since your program reads from standard input, it would be possible to type all the input to your program from the keyboard. But typing in a listing that looks like the output from ls -l even once would be awful. Instead you could run ls -l and pipe its output to your program. Make sure you understand that last sentence and try it out. Even better, you should run ls -l and redirect its output to another file. Then edit that file to create different versions that thoroughly test your program. Now run your program and redirect standard input to read from one of the test input files that you just created." +63c6479f5f004bf4898cf545,2023-01-26T04:13:11.107Z,General Question,v1,how do i use scanf to read what i redirected to standard input,,, +63c6479f5f004bf4898cf545,2023-01-26T22:58:43.440Z,Help Fix Code,v1,,"int *func(char **strs, int size) { + int *arr = malloc(sizeof(int) * size); + for (int i = 0l i= size) { + result++; + } + } else if (argc == 3 && ls_size >= size && check_permissions(ls_permissions, permissions) == 0) { + result++; + } + } + } + //free(permissions); + printf(""%d\n"", result); + free(permissions); + return 0; +}",, +63c6479f5f004bf4898cf545,2023-01-29T09:00:41.728Z,Question from Code,v1,how can i make permissions equal to the last 9 characters of input,"int check_permissions(char *permission_field, char *required_permissions) { + for (int i = 0; i < 9; i++) { + if (required_permissions[i] == 'w' && permission_field[i] != 'w') { + return 1; + } else if (required_permissions[i] == 'r' && permission_field[i] != 'r') { + return 1; + } else if (required_permissions[i] == 'x' && permission_field[i] != 'x') { + return 1; + } + } + + return 0; +} + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + char ls_permissions[10]; + int ls_size; + + scanf(""%*s %*d""); + + // TODO: Process command line arguments. + int size = strtol(argv[1], NULL, 10); + char *input; + char *permissions = malloc(sizeof(char *) * 9); + + if (argc == 3) { + input = argv[2]; + for (int i = 0; i < 9; i++) { + permissions[i] = input[i + 1]; + printf(""%sB \n"", &input[2]); + } + } +int result = 0; + + while (scanf(""%s %*d %*s %*s %d %*s %*d %*s %*s"", ls_permissions, &ls_size) != EOF) { + if (input[0] != 'd') { + if (argc == 2) { + if (ls_size >= size) { + result++; + } + } else if (argc == 3 && ls_size >= size && check_permissions(ls_permissions, permissions) == 0) { + result++; + } + } + } + //free(permissions); + printf(""%d\n"", result); + free(permissions); + return 0; +}",, +63c6479f5f004bf4898cf545,2023-01-29T09:07:32.636Z,General Question,v1,"why is it that when i index a pointer to a char array at index n, it gives me the same array starting from index n to the end?",,, +63c6479f5f004bf4898cf545,2023-02-02T22:52:09.649Z,Help Fix Code,v1,,"int truncate(char *s, int n) { + char new_str[strlen(s)]; + strncpy(new_str, s, n); + new_str[strlen(s) - 1] = '\0'; + s = new_str; + + return strlen(s) - n; +} +","Modify s so it becomes the first n characters of s, and return how many letters in s were removed", +63c6479f5f004bf4898cf545,2023-02-09T21:16:44.198Z,Help Write Code,v1,,,," 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." +63c6479f5f004bf4898cf545,2023-02-09T21:22:10.042Z,Help Fix Code,v1,,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + int arr = malloc(height * sizeof(struct pixel *)); + for (int i = 0; i < height; i++) { + arr[i] = malloc(3 * width); + } + fseek(image, pixel_array_offset, SEEK_SET); + fread(arr, 3, 1, image); + return arr[0]; + +} +"," 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.", +63c6479f5f004bf4898cf545,2023-02-09T21:30:35.718Z,Question from Code,v1,how do i make this compile,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel arr = malloc(height * sizeof(struct pixel *)); + for (int i = 0; i < height; i++) { + &arr[i] = malloc(3 * width); + } + fseek(image, pixel_array_offset, SEEK_SET); + fread(arr, 3, 1, image); + return arr[0]; + +} +",, +63c6479f5f004bf4898cf545,2023-02-09T21:36:01.210Z,Help Fix Code,v1,,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **arr = malloc(height * sizeof(struct pixel *)); + for (int i = 0; i < height; i++) { + arr[i] = malloc(3 * width); + } + fseek(image, pixel_array_offset, SEEK_SET); + fread(arr, 3, width, image); + return arr[0]; + +}"," * 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. + */", +63c6479f5f004bf4898cf545,2023-02-09T21:56:44.520Z,Help Fix Code,v1,,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **arr = malloc(height * sizeof(struct pixel *)); + for (int i = 0; i < height; i++) { + arr[i] = malloc(3 * width); + } + fseek(image, pixel_array_offset, SEEK_SET); + for (int i = 0; i < height; i++) { + fread(arr, 3, width, image); + } + return arr; + +}","/* + * 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. + */", +63c6479f5f004bf4898cf545,2023-02-09T21:59:10.119Z,Help Fix Code,v1,," +void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { + + fseek(image,10, SEEK_SET); + fread(&pixel_array_offset, sizeof(int), 1, image); + + fseek(image, 18, SEEK_SET); + fread(&width, sizeof(int), 1, image); + + fseek(image, 22, SEEK_SET); + fread(&height, sizeof(int), 1, image); + + +}","/* + * Read in the location of the pixel array, the image width, and the image + * height in the given bitmap file. + */", +63c6479f5f004bf4898cf545,2023-02-10T04:52:44.413Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { + return 2; + } + User *curr = *user_ptr_add; + while (curr != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + } + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + curr->next = new_user; + + + return 0; +} +","/* + * 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) + */", +63c6479f5f004bf4898cf545,2023-02-10T04:54:55.268Z,Help Fix Code,v1,,"/* + * 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) { + return 2; + } + User *curr = *user_ptr_add; + while (curr != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + curr->next = new_user; + + + return 0; +}","/* + * 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)", +63c6479f5f004bf4898cf545,2023-02-10T04:55:38.151Z,Question from Code,v1,how do i add a user to the list,"/* + * 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) { + return 2; + } + User *curr = *user_ptr_add; + while (curr != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + curr->next = new_user; + + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-10T04:56:26.632Z,Question from Code,v1,what line of code do i need to add to add new_user to the list,"/* + * 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) { + return 2; + } + User *curr = *user_ptr_add; + while (curr != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + curr->next = new_user; + + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-10T05:35:03.322Z,Question from Code,v1,will this code add a user to the end of the linked list,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { + return 2; + } + User *curr = *user_ptr_add; + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + if (curr == NULL) { + curr = new_user; + } + + while (curr->next != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + + curr->next = new_user; + + + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-10T05:37:08.184Z,Question from Code,v1,will this code add a user to the end of the linked list,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { + return 2; + } + User *curr = *user_ptr_add; + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + if (curr == NULL) { + curr = new_user; + return 0; + } + + while (curr->next != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + + curr->next = new_user; + + + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-10T20:46:34.188Z,Question from Code,v1,why do i get a segmentation fault when i try to print the contents of the file? how can i fix it?,"int print_user(const User *user) { + if (user == NULL) { + return 1; + } + FILE *file; + file = fopen(user->profile_pic, ""r""); + char line[10]; + while (fgets(line, 10, file) != NULL) { + printf(""%s"", line); + } + fclose(file); + + + printf(""Name: %s\n"", (char *)user->name); + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + + int count = 0; + while ((char *)user->friends[count] != NULL) { + printf(""%s\n"", user->friends[count]->name); + count++; + } + printf(""------------------------------------------\n""); + printf(""Posts:\n""); + Post *curr = user->first_post; + while (curr != NULL) { + printf(""From: %s\n"", curr->author); + printf(""Date: %s\n"", ctime(curr->date)); + printf(""%s\n\n"", curr->contents); + + if (curr->next != NULL) { + printf(""===\n\n""); + + } + curr = curr->next; + } + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-10T22:20:45.466Z,Question from Code,v1,"how can i fix my code so that i can assign new_post's date, which is of type time_t, to curr time?","int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + int friend_mutuality = 0; + + for(int i = 0; i < MAX_FRIENDS; i++) { + if (author->friends[i] == target) { + friend_mutuality++; + } + } + + if (friend_mutuality == 0) { + return 1; + } + + // Make a new post + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + time_t curr_time= sizeof(time_t); + time(&curr_time); + new_post->date = ctime(&curr_time); + if (target->first_post == NULL) { + new_post->next = NULL; + } else { + new_post->next = target->first_post; + target->first_post = new_post; + } + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-10T22:23:03.446Z,Question from Code,v1,how can i fix this to get rid of the incompatible type error,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + int friend_mutuality = 0; + + for(int i = 0; i < MAX_FRIENDS; i++) { + if (author->friends[i] == target) { + friend_mutuality++; + } + } + + if (friend_mutuality == 0) { + return 1; + } + + // Make a new post + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + time_t curr_time= sizeof(time_t); + time(&curr_time); + new_post->date = ctime(&curr_time); + if (target->first_post == NULL) { + new_post->next = NULL; + } else { + new_post->next = target->first_post; + target->first_post = new_post; + } + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-10T22:26:21.906Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + int friend_mutuality = 0; + + for(int i = 0; i < MAX_FRIENDS; i++) { + if (author->friends[i] == target) { + friend_mutuality++; + } + } + + if (friend_mutuality == 0) { + return 1; + } + + // Make a new post + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + time_t curr_time= sizeof(time_t); + time(&curr_time); + new_post->date = ctime(&curr_time); + if (target->first_post == NULL) { + new_post->next = NULL; + } else { + new_post->next = target->first_post; + target->first_post = new_post; + } + + 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 + */", +63c6479f5f004bf4898cf545,2023-02-10T22:27:52.059Z,Question from Code,v1,will this compile and if not what i can i do to change it? new_post->date must be of type time_t,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + int friend_mutuality = 0; + + for(int i = 0; i < MAX_FRIENDS; i++) { + if (author->friends[i] == target) { + friend_mutuality++; + } + } + + if (friend_mutuality == 0) { + return 1; + } + + // Make a new post + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + time_t curr_time= sizeof(time_t); + time(&curr_time); + new_post->date = ctime(&curr_time); + if (target->first_post == NULL) { + new_post->next = NULL; + } else { + new_post->next = target->first_post; + } + target->first_post = new_post; + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-10T22:34:26.932Z,Question from Code,v1,how to fix this so it will compile?,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + int friend_mutuality = 0; + + for(int i = 0; i < MAX_FRIENDS; i++) { + if (author->friends[i] == target) { + friend_mutuality++; + } + } + + if (friend_mutuality == 0) { + return 1; + } + + // Make a new post + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + time_t curr_time= sizeof(time_t); + time(&curr_time); + new_post->date = curr_time; + if (target->first_post == NULL) { + new_post->next = NULL; + } else { + new_post->next = target->first_post; + } + target->first_post = new_post; + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-11T15:06:30.327Z,Question from Code,v1,how can i fix this so that it compiles,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + int friend_mutuality = 0; + + for(int i = 0; i < MAX_FRIENDS; i++) { + if (author->friends[i] == target) { + friend_mutuality++; + } + } + + if (friend_mutuality == 0) { + return 1; + } + + // Make a new post + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + time_t curr_time= sizeof(time_t); + time(&curr_time); + new_post->date = curr_time; + if (target->first_post == NULL) { + new_post->next = NULL; + } else { + new_post->next = target->first_post; + } + target->first_post = new_post; + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-11T15:19:21.125Z,Question from Code,v1,what can i change to get rid of the makes pointer from integer without a cast error," time_t curr_time = sizeof(time(NULL)); + time(&curr_time); + new_post->date = curr_time;",, +63c6479f5f004bf4898cf545,2023-02-11T15:21:17.586Z,Question from Code,v1,what can i do to fix the makes pointer from integer without a cast error," time_t curr_time = time(NULL); + time(&curr_time); + new_post->date = curr_time;",, +63c6479f5f004bf4898cf545,2023-02-11T21:06:37.717Z,Question from Code,v1,what causes the segmentation fault,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(curr); + } else { + return 1; + } + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + curr->next = curr->next->next; + } + + return -1; +}",, +63c6479f5f004bf4898cf545,2023-02-11T21:08:57.187Z,Question from Code,v1,how do i remove the segfault,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(curr); + } else { + return 1; + } + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + curr->next = curr->next->next; + } + + return -1; +}",, +63c6479f5f004bf4898cf545,2023-02-11T22:06:58.721Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + + // If 1 element in the list + + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(curr); + return 0; + } else { + return 1; + } + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + curr->next = curr->next->next; + } + + return 0; +}","/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */", +63c6479f5f004bf4898cf545,2023-02-11T22:09:19.549Z,Question from Code,v1,will this cause a segmentation fault? how do i fix it?,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + + // If 1 element in the list + + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(curr); + return 0; + } else { + return 1; + } + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + free(curr->next); + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + free(curr->next); + curr->next = curr->next->next; + } + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-11T22:10:39.209Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + + // If 1 element in the list + + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(curr); + return 0; + } else { + return 1; + } + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + free(curr->next); + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + free(curr->next); + curr->next = curr->next->next; + } + + return 0; +}"," /* * From the list pointed to by *user_ptr_del, delete the user * with the given name. * Return: * - 0 on success. * - 1 if a user with this name does not exist. */", +63c6479f5f004bf4898cf545,2023-02-11T22:15:57.363Z,Question from Code,v1,how do i properly delete and free the memory of a user? How do i fix the segmentation fault?,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + User *temp = curr; + // If 1 element in the list + + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = *user_ptr_del; + } else { + return 1; + } + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + free(temp); + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-11T22:17:09.923Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + User *temp = *user_ptr_del; + // If 1 element in the list + + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = *user_ptr_del; + } else { + return 1; + } + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + free(temp); + return 0; +}","/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */", +63c6479f5f004bf4898cf545,2023-02-11T23:08:45.904Z,Question from Code,v1,"/* + * 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) { + // Check for error code 2 + if (strlen(name) >= MAX_NAME) { + return 2; + } + + User *curr = *user_ptr_add; + + // Create a new user + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // If list is empty + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + + curr->next = new_user; + + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-11T23:09:22.955Z,Question from Code,v1,"/* + * 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) + */ + + + +how do i fix this code?","int create_user(const char *name, User **user_ptr_add) { + // Check for error code 2 + if (strlen(name) >= MAX_NAME) { + return 2; + } + + User *curr = *user_ptr_add; + + // Create a new user + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // If list is empty + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + + curr->next = new_user; + + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-11T23:09:56.728Z,Question from Code,v1,how can i check if the name exists in the code already or not?,"int create_user(const char *name, User **user_ptr_add) { + // Check for error code 2 + if (strlen(name) >= MAX_NAME) { + return 2; + } + + User *curr = *user_ptr_add; + + // Create a new user + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // If list is empty + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + + curr->next = new_user; + + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-12T01:58:09.175Z,Help Fix Code,v1,,"void remove_friend(const char *name, User *ptr_friend_user, int index) { + User *friend = ptr_friend_user; + for (int i = index; i < MAX_FRIENDS - 1; i++) { + friend->friends[i] = friend->friends[i + 1]; + } +} + +void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp(curr->friends[i]->name, name) == 0) { + remove_friend(name, curr, i); + } + } + while (curr->next != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp(curr->friends[i]->name, name) == 0) { + remove_friend(name, curr, i); + } + } + curr = curr->next; + } +} +",remove the deleted user from the friendlist, +63c6479f5f004bf4898cf545,2023-02-12T02:02:08.468Z,Help Fix Code,v1,,"void remove_friend(const char *name, User *ptr_friend_user, int index) { + User *friend = ptr_friend_user; + for (int i = index; i < MAX_FRIENDS - 1; i++) { + friend->friends[i] = friend->friends[i + 1]; + } +} + +void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp(curr->friends[i]->name, name) == 0) { + remove_friend(name, curr, i); + } + } + curr = curr->next; + } +} +","remove the deleted user from the friendlist +", +63c6479f5f004bf4898cf545,2023-02-12T02:05:42.996Z,Help Fix Code,v1,,"void remove_friend(const char *name, User *ptr_friend_user, int index) { + User *friend = ptr_friend_user; + for (int i = index; i < MAX_FRIENDS - 1; i++) { + friend->friends[i] = friend->friends[i + 1]; + } + friend->friends[MAX_FRIENDS - 1] = NULL; +} + +void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp(curr->friends[i]->name, name) == 0) { + remove_friend(name, curr, i); + } + } + curr = curr->next; + } +} +",remove the deleted user from the friendlist, +63c6479f5f004bf4898cf545,2023-02-12T02:12:18.794Z,Question from Code,v1,what can i do to fix this so that a user is removed from the friendslist of all users,"void remove_friend(const char *name, User *ptr_friend_user, int index) { + User *friend = ptr_friend_user; + for (int i = index; i < MAX_FRIENDS - 1; i++) { + friend->friends[i] = friend->friends[i + 1]; + } + friend->friends[MAX_FRIENDS - 1] = NULL; +} + +void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp(curr->friends[i]->name, name) == 0) { + remove_friend(name, curr, i); + } + } + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T02:30:18.589Z,Help Fix Code,v1,,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + User *temp = malloc(sizeof(User)); + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp(curr->friends[i]->name, name) == 0) { + temp = curr->friends[i]; + for (int j = i; i < MAX_FRIENDS - 1; j ++) { + curr->friends[j] = curr->friends[j + 1]; + } + curr->friends[MAX_FRIENDS - 1] = NULL; + } + } + curr = curr->next; + free(temp); + } + +}",delete user from all the friendlists of other users, +63c6479f5f004bf4898cf545,2023-02-12T02:33:28.503Z,Help Fix Code,v1,,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp(curr->friends[i]->name, name) == 0) { + User *temp = malloc(sizeof(User)); + temp = curr->friends[i]; + for (int j = i; i < MAX_FRIENDS - 1; j ++) { + curr->friends[j] = curr->friends[j + 1]; + } + curr->friends[MAX_FRIENDS - 1] = NULL; + free(temp); + } + } + curr = curr->next; + + } + +}",delete user from all the friendlists of other users, +63c6479f5f004bf4898cf545,2023-02-12T02:33:53.757Z,Question from Code,v1,how can i properly initialize temp,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp(curr->friends[i]->name, name) == 0) { + User *temp = malloc(sizeof(User)); + temp = curr->friends[i]; + for (int j = i; i < MAX_FRIENDS - 1; j ++) { + curr->friends[j] = curr->friends[j + 1]; + } + curr->friends[MAX_FRIENDS - 1] = NULL; + free(temp); + } + } + curr = curr->next; + + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T02:38:36.908Z,Help Fix Code,v1,,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp(curr->friends[i]->name, name) == 0) { + for (int j = i; i < MAX_FRIENDS - 1; j ++) { + curr->friends[j] = curr->friends[j + 1]; + } + curr->friends[MAX_FRIENDS - 1] = NULL; + } + } + curr = curr->next; + } + +}",delete the user in every friendlist, +63c6479f5f004bf4898cf545,2023-02-12T02:39:28.370Z,Question from Code,v1,does this remove the friend from the friendlist of every user? and if not how can i fix it,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp(curr->friends[i]->name, name) == 0) { + for (int j = i; i < MAX_FRIENDS - 1; j ++) { + curr->friends[j] = curr->friends[j + 1]; + } + curr->friends[MAX_FRIENDS - 1] = NULL; + } + } + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T02:46:51.876Z,Help Fix Code,v1,,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + printf(""%s"", curr->friends[i]->name); + if (strcmp(curr->friends[i]->name, name) == 0) { + for (int j = i; i < MAX_FRIENDS - 1; j ++) { + curr->friends[j] = curr->friends[j + 1]; + } + curr->friends[MAX_FRIENDS - 1] = NULL; + } + } + curr = curr->next; + } + +} + +int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + free(temp); + + remove_friends(name, user_ptr_del); + return 0; +} +","/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */", +63c6479f5f004bf4898cf545,2023-02-12T02:50:43.564Z,Help Fix Code,v1,,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + + if (strcmp(curr->friends[i]->name, name) == 0) { + for (int j = i; i < MAX_FRIENDS - 1; j ++) { + curr->friends[j] = curr->friends[j + 1]; + } + curr->friends[MAX_FRIENDS - 1] = NULL; + } + } + } + curr = curr->next; + } + +} + +int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + free(temp); + + remove_friends(name, user_ptr_del); + return 0; +} +","From the list pointed to by *user_ptr_del, delete the user * with the given name. * Remove the deleted user from any lists of friends. * * Return: * - 0 on success. * - 1 if a user with this name does not exist", +63c6479f5f004bf4898cf545,2023-02-12T02:52:47.218Z,Question from Code,v1,what can i fix to make sure memory is allocated and deallocated properly?,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + + if (strcmp(curr->friends[i]->name, name) == 0) { + for (int j = i; i < MAX_FRIENDS - 1; j ++) { + curr->friends[j] = curr->friends[j + 1]; + } + curr->friends[MAX_FRIENDS - 1] = NULL; + } + } + } + curr = curr->next; + } + +} + +int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + free(temp); + + remove_friends(name, user_ptr_del); + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-12T02:54:26.529Z,Question from Code,v1,what can i do to fix this code,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + + if (strcmp(curr->friends[i]->name, name) == 0) { + for (int j = i; i < MAX_FRIENDS - 1; j ++) { + curr->friends[j] = curr->friends[j + 1]; + } + curr->friends[MAX_FRIENDS - 1] = NULL; + } + } + } + curr = curr->next; + } + +} + +int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + free(temp); + + remove_friends(name, user_ptr_del); + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-12T02:57:32.435Z,Question from Code,v1,does remove_friends properly remove the user from the friendlist? what can i change?,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + + if (strcmp(curr->friends[i]->name, name) == 0) { + for (int j = i; i < MAX_FRIENDS - 1; j ++) { + curr->friends[j] = curr->friends[j + 1]; + } + curr->friends[MAX_FRIENDS - 1] = NULL; + } + } + } + curr = curr->next; + } + +} + +int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + free(temp); + + remove_friends(name, user_ptr_del); + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-12T03:09:51.633Z,Question from Code,v1,will this remove the deleted user from all user's friendlists?," +void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp(curr->friends[i]->name, name) == 0) { + for (int j = i; i < MAX_FRIENDS - 1; j ++) { + curr->friends[j] = curr->friends[j + 1]; + } + curr->friends[MAX_FRIENDS - 1] = NULL; + } + } + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T03:10:51.487Z,Question from Code,v1,"will this remove the deleted user from all user's friendlists? +","int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + free(temp); + + remove_friends(name, user_ptr_del); + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-12T03:11:21.004Z,Question from Code,v1,is this code correct,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + free(temp); + + remove_friends(name, user_ptr_del); + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-12T03:12:02.453Z,Question from Code,v1,is this correct,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp(curr->friends[i]->name, name) == 0) { + for (int j = i; i < MAX_FRIENDS - 1; j ++) { + curr->friends[j] = curr->friends[j + 1]; + } + curr->friends[MAX_FRIENDS - 1] = NULL; + } + } + curr = curr->next; + } + +} +int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + free(temp); + + remove_friends(name, user_ptr_del); + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-12T03:12:51.066Z,Question from Code,v1,how can i fix this code,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp(curr->friends[i]->name, name) == 0) { + for (int j = i; i < MAX_FRIENDS - 1; j ++) { + curr->friends[j] = curr->friends[j + 1]; + } + curr->friends[MAX_FRIENDS - 1] = NULL; + } + } + curr = curr->next; + } + +} +int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + free(temp); + + remove_friends(name, user_ptr_del); + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-12T03:15:38.159Z,Question from Code,v1,how can i make sure curr is initialized to *user_ptr_del in remove_friends,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp(curr->friends[i]->name, name) == 0) { + for (int j = i; i < MAX_FRIENDS - 1; j ++) { + curr->friends[j] = curr->friends[j + 1]; + } + curr->friends[MAX_FRIENDS - 1] = NULL; + } + } + curr = curr->next; + } + +} +int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + free(temp); + + remove_friends(name, user_ptr_del); + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-12T03:17:55.452Z,Question from Code,v1,how do i prevent the segmentation faults,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp(curr->friends[i]->name, name) == 0) { + for (int j = i; i < MAX_FRIENDS - 1; j ++) { + curr->friends[j] = curr->friends[j + 1]; + } + curr->friends[MAX_FRIENDS - 1] = NULL; + } + } + curr = curr->next; + } + +} +int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + free(temp); + + remove_friends(name, user_ptr_del); + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-12T04:13:36.631Z,Question from Code,v1,is an array of users the same as a linked list of users?,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (strcmp(curr->friends[i]->name, name) == 0) { + for (int j = i; i < MAX_FRIENDS - 1; j ++) { + curr->friends[j] = curr->friends[j + 1]; + } + curr->friends[MAX_FRIENDS - 1] = NULL; + } + } + curr = curr->next; + } + +} +int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + free(temp); + + remove_friends(name, user_ptr_del); + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-12T04:43:54.343Z,Question from Code,v1,will this code remove the user from every friendlist?,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + for (int x = u_count; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T04:45:21.374Z,Help Fix Code,v1,,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + for (int x = u_count; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +} + +int delete_user(const char *name, User **user_ptr_del) { + + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + + if (*user_ptr_del == NULL) { + return 1; + } + + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + free(temp); + remove_friends(name, user_ptr_del); + return 0; +} + + +"," * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */", +63c6479f5f004bf4898cf545,2023-02-12T04:48:31.549Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + // Check for User Non-existence + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1]->name != NULL || friend2->friends[MAX_FRIENDS - 1]->name != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + int f1_added = 0; + int f2_added = 0; + int count = 0; + + while (f1_added == 0 || f2_added == 0) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; + } + count++; + } + + return 0; +} +"," * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies.", +63c6479f5f004bf4898cf545,2023-02-12T04:53:24.277Z,Help Fix Code,v1,,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + for (int x = u_count; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +} +int delete_user(const char *name, User **user_ptr_del) { + + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + + if (*user_ptr_del == NULL) { + return 1; + } + + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + remove_friends(name, user_ptr_del); + free(temp); + return 0; +} +","/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */", +63c6479f5f004bf4898cf545,2023-02-12T04:56:45.975Z,Question from Code,v1,should temp be allocated size user or user *,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + for (int x = u_count; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +} +int delete_user(const char *name, User **user_ptr_del) { + + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + + if (*user_ptr_del == NULL) { + return 1; + } + + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + remove_friends(name, user_ptr_del); + free(temp); + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-12T05:02:51.374Z,Question from Code,v1,what can i change so this code properly deletes a user from the friendslists,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + for (int x = u_count; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +} +int delete_user(const char *name, User **user_ptr_del) { + + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + + if (*user_ptr_del == NULL) { + return 1; + } + + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + remove_friends(name, user_ptr_del); + free(temp); + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-12T05:03:25.420Z,Question from Code,v1,how can i fix the code,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + for (int x = u_count; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +} +int delete_user(const char *name, User **user_ptr_del) { + + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + + if (*user_ptr_del == NULL) { + return 1; + } + + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + remove_friends(name, user_ptr_del); + free(temp); + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-12T05:06:29.686Z,Help Fix Code,v1,,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + for (int x = u_count; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +} +int delete_user(const char *name, User **user_ptr_del) { + + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + + if (*user_ptr_del == NULL) { + return 1; + } + + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + free(temp); + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + remove_friends(name, user_ptr_del); + free(temp); + return 0; +} +"," + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */", +63c6479f5f004bf4898cf545,2023-02-12T05:07:30.956Z,Question from Code,v1,"will this code behave as intended? + +Here is what it should do: + * 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. + */","void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + for (int x = u_count; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +} +int delete_user(const char *name, User **user_ptr_del) { + + // Delete user from list + User *curr = *user_ptr_del; + User *temp = malloc(sizeof(User)); + + if (*user_ptr_del == NULL) { + return 1; + } + + // If 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + free(temp); + return 0; + } + + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + free(temp); + return 1; + } + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + remove_friends(name, user_ptr_del); + free(temp); + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-12T17:36:21.776Z,Question from Code,v1,"does this code properly remove a user by the name of variable name from all the friendlists of the linked list of users starting at ptr_users? +if not, how can fix it?","void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + for (int x = u_count; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T17:37:40.184Z,Question from Code,v1,does this properly shit all the friends after user in the list by one? ,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + for (int x = u_count; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T17:41:46.027Z,Question from Code,v1,does this code properly remove a friend from friendlists of all the users by shifting the list to the left by one after in the list after the user we want to remove? if not how can i fix it,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + for (int x = u_count; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T17:51:26.945Z,Question from Code,v1,"do lines 4-14 properly assign variable u_count to the position of the user by the name name in the friends list, and f_count to the number of friends in the friendslist? + + +do lines 16-19 then properly remove the user from the friendlist of the users?","void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + for (int x = u_count; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T17:53:45.572Z,Question from Code,v1,how do i properly assign u_count to the index of the removed user in the friendslist?,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + for (int x = u_count; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T17:54:20.572Z,Question from Code,v1,"do lines 4-14 properly assign variable u_count to the position of the user by the name name in the friends list, and f_count to the number of friends in the friendslist? + +if not, what can i change?","void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + for (int x = u_count; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T17:55:23.927Z,Question from Code,v1,do lines 16-19 then properly remove the user from the friendlist of the users? if not what can i change,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + for (int x = u_count; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T18:41:46.799Z,Question from Code,v1,do i need to delete the memory of the removed user in friendslist? and if so how?,"void remove_friends(const char *name, User **ptr_users) { + printf(""bye""); + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + printf(""%s"", curr->friends[0]->name); + if (f_count == 1 && u_count == 1) { + curr->friends[0] = NULL; + } + else { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T18:42:47.700Z,Question from Code,v1,Does this code properly remove friends from the friendlist? what do i need to change or add?,"void remove_friends(const char *name, User **ptr_users) { + printf(""bye""); + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + printf(""%s"", curr->friends[0]->name); + if (f_count == 1 && u_count == 1) { + curr->friends[0] = NULL; + } + else { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T18:44:06.096Z,Question from Code,v1,how can i fix this so i remove the user from the friendlists? ,"void remove_friends(const char *name, User **ptr_users) { + printf(""bye""); + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + printf(""%s"", curr->friends[0]->name); + if (f_count == 1 && u_count == 1) { + curr->friends[0] = NULL; + } + else { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T18:57:25.161Z,Question from Code,v1,how do i delete the user from the friendslist if there is only one friend in the friendslist?,"void remove_friends(const char *name, User **ptr_users) { + printf(""bye""); + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + printf(""%s"", curr->friends[0]->name); + if (f_count == 1 && u_count == 1) { + curr->friends[0] = NULL; + } + else { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + } + + curr->friends[f_count - 1] = NULL; + + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T20:00:53.828Z,Help Fix Code,v1,,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + if (u_count != 0) { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + curr = curr->next; + } + +}",remove the user from all the friendlists of the list of users, +63c6479f5f004bf4898cf545,2023-02-12T20:02:34.276Z,Question from Code,v1,will this properly remove the user from a 1 element long friendlist of a user?,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + if (u_count != 0) { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T20:03:03.114Z,Question from Code,v1,is this code correct?,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + if (u_count != 0) { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T20:03:22.291Z,Question from Code,v1,what do i need to do to fix this,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + if (u_count != 0) { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T20:05:00.263Z,Question from Code,v1,"will this code correctly remove a user from all friendlists? if not, how can i fix it","void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + if (u_count != 0) { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T20:12:54.684Z,Help Fix Code,v1,,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0;Avoid remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + if (u_count != 0) { + if (f_count == 1 && u_count == 1) { + User *blank = malloc(sizeof(User) * MAX_FRIENDS); + curr->friends = blank; + } else { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + } + curr = curr->next; + } + +} + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + if (u_count != 0) { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + curr = curr->next; + } + +}",remove user from all friendslists, +63c6479f5f004bf4898cf545,2023-02-12T20:13:40.085Z,Question from Code,v1,how do i fix it to make this compile,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0;Avoid remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + if (u_count != 0) { + if (f_count == 1 && u_count == 1) { + User *blank = malloc(sizeof(User) * MAX_FRIENDS); + curr->friends = blank; + } else { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + } + curr = curr->next; + } + +} + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + if (u_count != 0) { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T20:14:50.436Z,Question from Code,v1,how do i fix the assignment to expression with array type error,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0;Avoid remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + if (u_count != 0) { + if (f_count == 1 && u_count == 1) { + User *blank = malloc(sizeof(User) * MAX_FRIENDS); + curr->friends = blank; + } else { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + } + curr = curr->next; + } + +} + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + if (u_count != 0) { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T20:16:33.765Z,Question from Code,v1,is this correct,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + if (u_count != 0) { + if (f_count == 1) { + curr->friends[0] = NULL; + } else { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + } + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T20:22:37.451Z,Question from Code,v1,"will this code properly remove the user from a friendlist of length 1? if not, why?","void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + if (u_count != 0) { + if (f_count == 1) { + curr->friends[0] = NULL; + } else { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + } + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T20:23:52.118Z,Question from Code,v1,how do i remove a user from a friendlist of lenght 1,"void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + if (u_count != 0) { + if (f_count == 1) { + curr->friends[0] = NULL; + } else { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + } + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T20:27:57.419Z,Question from Code,v1,"will this code properly remove the user from a friendlist of length 1? if not, what can i do to fix it","void remove_friends(const char *name, User **ptr_users) { + User *curr = *ptr_users; + while (curr != NULL) { + int f_count = 0; + int u_count = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + if (u_count != 0) { + if (f_count == 1) { + curr->friends[0] = NULL; + } else { + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + } + curr = curr->next; + } + +}",, +63c6479f5f004bf4898cf545,2023-02-12T21:38:50.418Z,General Question,v1,how do i initialize time_t properly so i can get the current time,,, +63c6479f5f004bf4898cf545,2023-02-12T22:12:30.627Z,Question from Code,v1,will this properly set curr_time to the current time without junk bites? how can i fix it," time_t curr_time = sizeof(time_t); + time(&curr_time); + new_post->date = &curr_time; +",, +63c6479f5f004bf4898cf545,2023-02-12T22:13:58.742Z,Question from Code,v1,why do i keep getting the wrong date with this," time_t curr_time; + time(&curr_time); + new_post->date = &curr_time; +",, +63c6479f5f004bf4898cf545,2023-02-12T22:15:01.596Z,Question from Code,v1,what do i need to change to get the right time," time_t curr_time; + time(&curr_time); + new_post->date = &curr_time; +",, +63c6479f5f004bf4898cf545,2023-02-12T22:41:15.199Z,General Question,v1,how can i allocate space for a time_t date,,, +63c6479f5f004bf4898cf545,2023-02-12T22:45:36.645Z,Question from Code,v1,"what do i need to change so the current time is stored in new_post->date, a time_t variable?"," time_t *curr_time = malloc(sizeof(time_t)); + time(curr_time) ; + new_post->date = curr_time; + free(curr_time); +",, +63c6479f5f004bf4898cf545,2023-02-12T22:46:31.269Z,Question from Code,v1,do i need to dereference curr_time when iassign it to new_post->date? how do i do that," time_t *curr_time = malloc(sizeof(time_t)); + time(curr_time) ; + new_post->date = curr_time; + free(curr_time); +",, +63c6479f5f004bf4898cf545,2023-02-12T22:47:10.083Z,Question from Code,v1,on which line do i reference curr_time," time_t *curr_time = malloc(sizeof(time_t)); + time(curr_time) ; + new_post->date = curr_time; + free(curr_time); +",, +63c6479f5f004bf4898cf545,2023-02-12T22:47:40.113Z,Question from Code,v1,on which line do i need to reference curr_time so this program gives me the correct time," time_t *curr_time = malloc(sizeof(time_t)); + time(curr_time) ; + new_post->date = curr_time; + free(curr_time); +",, +63c6479f5f004bf4898cf545,2023-02-13T00:36:41.336Z,Question from Code,v1,on which line should i free new_user,"int create_user(const char *name, User **user_ptr_add) { + // Check if name is too long + if (strlen(name) >= MAX_NAME) { + return 2; + } + + // Create a pointer curr to traverse the list + User *curr = *user_ptr_add; + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""Failed Malloc""); + exit(-1); + } + strcpy(new_user->name, name); + new_user->next = NULL; + + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + // Traverse the list until the end + while (curr->next != NULL) { + // Check if name already exists + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + + // Check if the name is the same as the name of the last user + // in the list + if (strcmp(name, curr->name) == 0) { + return 1; + } + + // Make the last element equal to our new user + curr->next = new_user; + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-13T00:38:49.715Z,Question from Code,v1,do i need to deallocate memory?,"int create_user(const char *name, User **user_ptr_add) { + // Check if name is too long + if (strlen(name) >= MAX_NAME) { + return 2; + } + + // Create a pointer curr to traverse the list + User *curr = *user_ptr_add; + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""Failed Malloc""); + exit(-1); + } + strcpy(new_user->name, name); + new_user->next = NULL; + + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + // Traverse the list until the end + while (curr->next != NULL) { + // Check if name already exists + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + + // Check if the name is the same as the name of the last user + // in the list + if (strcmp(name, curr->name) == 0) { + return 1; + } + + // Make the last element equal to our new user + curr->next = new_user; + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-13T00:39:14.120Z,Question from Code,v1,where should i deallocate memory,"int create_user(const char *name, User **user_ptr_add) { + // Check if name is too long + if (strlen(name) >= MAX_NAME) { + return 2; + } + + // Create a pointer curr to traverse the list + User *curr = *user_ptr_add; + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""Failed Malloc""); + exit(-1); + } + strcpy(new_user->name, name); + new_user->next = NULL; + + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + // Traverse the list until the end + while (curr->next != NULL) { + // Check if name already exists + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + + // Check if the name is the same as the name of the last user + // in the list + if (strcmp(name, curr->name) == 0) { + return 1; + } + + // Make the last element equal to our new user + curr->next = new_user; + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-13T00:39:41.655Z,Question from Code,v1,"if i deallocate memory, will it delete the user","int create_user(const char *name, User **user_ptr_add) { + // Check if name is too long + if (strlen(name) >= MAX_NAME) { + return 2; + } + + // Create a pointer curr to traverse the list + User *curr = *user_ptr_add; + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""Failed Malloc""); + exit(-1); + } + strcpy(new_user->name, name); + new_user->next = NULL; + + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + // Traverse the list until the end + while (curr->next != NULL) { + // Check if name already exists + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + + // Check if the name is the same as the name of the last user + // in the list + if (strcmp(name, curr->name) == 0) { + return 1; + } + + // Make the last element equal to our new user + curr->next = new_user; + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-13T00:40:13.679Z,Question from Code,v1,should i deallocate memory here or in a function that deletes the user,"int create_user(const char *name, User **user_ptr_add) { + // Check if name is too long + if (strlen(name) >= MAX_NAME) { + return 2; + } + + // Create a pointer curr to traverse the list + User *curr = *user_ptr_add; + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""Failed Malloc""); + exit(-1); + } + strcpy(new_user->name, name); + new_user->next = NULL; + + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + // Traverse the list until the end + while (curr->next != NULL) { + // Check if name already exists + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + + // Check if the name is the same as the name of the last user + // in the list + if (strcmp(name, curr->name) == 0) { + return 1; + } + + // Make the last element equal to our new user + curr->next = new_user; + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-13T00:44:10.161Z,Question from Code,v1,on which line do i deallocate memory,"int make_post(const User *author, User *target, char *contents) { + // Check if users exist + if (author == NULL || target == NULL) { + return 2; + } + + // Create a variable to track if users are friends + int friend_mutuality = 0; + + // Iterate through the friendlist of author to check if author + // is friends with target + for(int i = 0; i < MAX_FRIENDS; i++) { + if (author->friends[i] == target) { + // Update the tracker variable + friend_mutuality++; + } + } + + // Return 1 if author is not friends with target + if (friend_mutuality == 0) { + return 1; + } + + // Make a new post + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""Failed malloc""); + exit(-1); + } + // Set the author + strcpy(new_post->author, author->name); + // Set the contents + new_post->contents = contents; + // Set the date + time_t *curr_time = malloc(sizeof(time_t)); + if (curr_time == NULL) { + perror(""Failed malloc""); + exit(-1); + } + time(curr_time) ; + new_post->date = curr_time; + + // If target has no posts, set the next post to NULL, + // Otherwise, set the next post to the second most recent post + if (target->first_post == NULL) { + new_post->next = NULL; + } else { + new_post->next = target->first_post; + } + + // Set the target's first post equal to newest post + target->first_post = new_post; + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-13T00:44:41.032Z,Question from Code,v1,should i deallocate memory in this function,"int make_post(const User *author, User *target, char *contents) { + // Check if users exist + if (author == NULL || target == NULL) { + return 2; + } + + // Create a variable to track if users are friends + int friend_mutuality = 0; + + // Iterate through the friendlist of author to check if author + // is friends with target + for(int i = 0; i < MAX_FRIENDS; i++) { + if (author->friends[i] == target) { + // Update the tracker variable + friend_mutuality++; + } + } + + // Return 1 if author is not friends with target + if (friend_mutuality == 0) { + return 1; + } + + // Make a new post + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""Failed malloc""); + exit(-1); + } + // Set the author + strcpy(new_post->author, author->name); + // Set the contents + new_post->contents = contents; + // Set the date + time_t *curr_time = malloc(sizeof(time_t)); + if (curr_time == NULL) { + perror(""Failed malloc""); + exit(-1); + } + time(curr_time) ; + new_post->date = curr_time; + + // If target has no posts, set the next post to NULL, + // Otherwise, set the next post to the second most recent post + if (target->first_post == NULL) { + new_post->next = NULL; + } else { + new_post->next = target->first_post; + } + + // Set the target's first post equal to newest post + target->first_post = new_post; + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-13T00:45:19.422Z,Question from Code,v1,should i deallocate the memory here or in the function that deletes the user,"int make_post(const User *author, User *target, char *contents) { + // Check if users exist + if (author == NULL || target == NULL) { + return 2; + } + + // Create a variable to track if users are friends + int friend_mutuality = 0; + + // Iterate through the friendlist of author to check if author + // is friends with target + for(int i = 0; i < MAX_FRIENDS; i++) { + if (author->friends[i] == target) { + // Update the tracker variable + friend_mutuality++; + } + } + + // Return 1 if author is not friends with target + if (friend_mutuality == 0) { + return 1; + } + + // Make a new post + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""Failed malloc""); + exit(-1); + } + // Set the author + strcpy(new_post->author, author->name); + // Set the contents + new_post->contents = contents; + // Set the date + time_t *curr_time = malloc(sizeof(time_t)); + if (curr_time == NULL) { + perror(""Failed malloc""); + exit(-1); + } + time(curr_time) ; + new_post->date = curr_time; + + // If target has no posts, set the next post to NULL, + // Otherwise, set the next post to the second most recent post + if (target->first_post == NULL) { + new_post->next = NULL; + } else { + new_post->next = target->first_post; + } + + // Set the target's first post equal to newest post + target->first_post = new_post; + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-13T00:48:53.214Z,General Question,v1,can i have more free calls than free calls in a function if i called malloc without calling free in another function?,,, +63c6479f5f004bf4898cf545,2023-02-13T01:29:03.176Z,Question from Code,v1,does this code properly free all memory from the linked list," + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = NULL; + curr_post = curr_post->next; + }",, +63c6479f5f004bf4898cf545,2023-02-13T01:29:30.309Z,Question from Code,v1,how can i make the code free all memory from the linked list," + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = NULL; + curr_post = curr_post->next; + }",, +63c6479f5f004bf4898cf545,2023-02-13T01:30:17.311Z,Question from Code,v1,do i need to free next ," + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = NULL; + curr_post = curr_post->next; + }",, +63c6479f5f004bf4898cf545,2023-02-13T01:30:46.349Z,Question from Code,v1,is this code correct," + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = NULL; + curr_post = curr_post->next; + }",, +63c6479f5f004bf4898cf545,2023-02-13T01:32:03.779Z,Question from Code,v1,is this code correct," + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + }",, +63c6479f5f004bf4898cf545,2023-02-13T02:48:53.986Z,Question from Code,v1,does this code properly remove the user from all friendlists,"void remove_friends(const char *name, User **ptr_users) { + // Create a pointer to traverse the list of users + User *curr = *ptr_users; + + // Traverse through the list of users + while (curr != NULL) { + // f_count stores the number of friends curr has + // u_count stores 1 + the index the deleted user is found in the friendlist + int f_count = 0; + int u_count = 0; + + // Iterate through the friendslist of current user to obtain values of + // f_count and u_count + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + // Remove the user from the friendlists + // Check if user exists in the friendlist + if (u_count != 0) { + // Check if curr only has one friend + if (f_count == 1) { + curr->friends[0] = NULL; + } else { + // Shift every element in the friendlist after the the deleted user + // to the left by 1 + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + } + curr = curr->next; + } +}",, +63c6479f5f004bf4898cf545,2023-02-13T17:23:08.446Z,Question from Code,v1,"will this code properly remove the friends from the removed user? if not, why?","int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + + // Create a variable to traverse the list + User *curr = *user_ptr_del; + // Create a variable to hold the deleted user + User *temp = malloc(sizeof(User)); + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + remove_friends(name, user_ptr_del); + free(temp); + return 0; + } + + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + free(temp); + return 1; + } + + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + + // Remove the user from the friendlists of every user + remove_friends(name, user_ptr_del); + + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-13T17:24:45.667Z,Help Fix Code,v1,,"void remove_friends(const char *name, User **ptr_users) { + // Create a pointer to traverse the list of users + User *curr = *ptr_users; + + // Traverse through the list of users + while (curr != NULL) { + // f_count stores the number of friends curr has + // u_count stores 1 + the index the deleted user is found in the friendlist + int f_count = 0; + int u_count = 0; + + // Iterate through the friendslist of current user to obtain values of + // f_count and u_count + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + f_count++; + if (strcmp(curr->friends[i]->name, name) == 0) { + u_count = f_count; + } + } + } + + // Remove the user from the friendlists + // Check if user exists in the friendlist + if (u_count != 0) { + // Check if curr only has one friend + if (f_count == 1) { + curr->friends[0] = NULL; + } else { + // Shift every element in the friendlist after the the deleted user + // to the left by 1 + for (int x = u_count - 1; x < f_count - 1; x++) { + curr->friends[x] = curr->friends[x + 1]; + } + curr->friends[f_count - 1] = NULL; + } + } + curr = curr->next; + } +} +int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + + // Create a variable to traverse the list + User *curr = *user_ptr_del; + // Create a variable to hold the deleted user + User *temp = malloc(sizeof(User)); + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + temp = curr; + *user_ptr_del = curr->next; + remove_friends(name, user_ptr_del); + free(temp); + return 0; + } + + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + // If name does not exist + if (curr->next == NULL) { + free(temp); + return 1; + } + + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + + // Remove the user from the friendlists of every user + remove_friends(name, user_ptr_del); + + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + return 0; +}","/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */", +63c6479f5f004bf4898cf545,2023-02-13T20:38:49.352Z,Question from Code,v1,is memory properly freed here," + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + }",, +63c6479f5f004bf4898cf545,2023-02-13T20:39:43.730Z,Question from Code,v1,am i traversing the list properly and deleting the memory properly," + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + }",, +63c6479f5f004bf4898cf545,2023-02-13T20:43:01.107Z,General Question,v1,do we always need the same number of malloc calls as free calls? what if there were multiple return statements in if statements and multiple free cals within if statements?,,, +63c6479f5f004bf4898cf545,2023-02-13T20:44:18.725Z,Question from Code,v1,"will this code cause memory leaks? if so, where?","int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + + // Create a variable to traverse the list + User *curr = *user_ptr_del; + // Create a variable to hold the deleted user + User *temp = *user_ptr_del; + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + remove_friends(name, user_ptr_del); + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = curr->next; + remove_friends(name, user_ptr_del); + free(temp); + return 0; + } + + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + + // If name does not exist + if (curr->next == NULL) { + return 1; + } + + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + + + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-13T20:44:51.218Z,Question from Code,v1,how can i fix the memory leak,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + + // Create a variable to traverse the list + User *curr = *user_ptr_del; + // Create a variable to hold the deleted user + User *temp = *user_ptr_del; + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + remove_friends(name, user_ptr_del); + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = curr->next; + remove_friends(name, user_ptr_del); + free(temp); + return 0; + } + + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + + // If name does not exist + if (curr->next == NULL) { + return 1; + } + + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + + + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-13T20:45:21.610Z,Question from Code,v1,am i missing any free(temp) statements?,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + + // Create a variable to traverse the list + User *curr = *user_ptr_del; + // Create a variable to hold the deleted user + User *temp = *user_ptr_del; + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + remove_friends(name, user_ptr_del); + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = curr->next; + remove_friends(name, user_ptr_del); + free(temp); + return 0; + } + + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + + // If name does not exist + if (curr->next == NULL) { + return 1; + } + + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + + + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-13T20:46:27.728Z,General Question,v1,"if i have a struct and i allocated memory for the struct, will i have to free the memory for that struct only, or every attribute of the struct as well?",,, +63c6479f5f004bf4898cf545,2023-02-13T20:47:56.774Z,Question from Code,v1,where are the memory leaks ,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + + // Create a variable to traverse the list + User *curr = *user_ptr_del; + // Create a variable to hold the deleted user + User *temp = *user_ptr_del; + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + remove_friends(name, user_ptr_del); + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = curr->next; + remove_friends(name, user_ptr_del); + free(temp); + return 0; + } + + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + + // If name does not exist + if (curr->next == NULL) { + return 1; + } + + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + + + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-13T20:48:38.047Z,Question from Code,v1,are there memory leaks in lines 56-62,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + + // Create a variable to traverse the list + User *curr = *user_ptr_del; + // Create a variable to hold the deleted user + User *temp = *user_ptr_del; + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + remove_friends(name, user_ptr_del); + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = curr->next; + remove_friends(name, user_ptr_del); + free(temp); + return 0; + } + + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + + // If name does not exist + if (curr->next == NULL) { + return 1; + } + + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + + + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-13T20:49:04.967Z,Question from Code,v1,what do i need to change to ensure the memory of the posts are being freed,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + + // Create a variable to traverse the list + User *curr = *user_ptr_del; + // Create a variable to hold the deleted user + User *temp = *user_ptr_del; + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + remove_friends(name, user_ptr_del); + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = curr->next; + remove_friends(name, user_ptr_del); + free(temp); + return 0; + } + + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + + // If name does not exist + if (curr->next == NULL) { + return 1; + } + + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + + + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-13T20:50:09.546Z,Question from Code,v1,"do lines 56-62 properly free the post memory? if not, how can i fix this? what line do i need to add or change and where?","int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + + // Create a variable to traverse the list + User *curr = *user_ptr_del; + // Create a variable to hold the deleted user + User *temp = *user_ptr_del; + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + remove_friends(name, user_ptr_del); + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = curr->next; + remove_friends(name, user_ptr_del); + free(temp); + return 0; + } + + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + + // If name does not exist + if (curr->next == NULL) { + return 1; + } + + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + + + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-13T20:52:02.123Z,Question from Code,v1,on which lines do the memory leaks occur?,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + + // Create a variable to traverse the list + User *curr = *user_ptr_del; + // Create a variable to hold the deleted user + User *temp = *user_ptr_del; + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + remove_friends(name, user_ptr_del); + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = curr->next; + remove_friends(name, user_ptr_del); + free(temp); + return 0; + } + + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + + // If name does not exist + if (curr->next == NULL) { + return 1; + } + + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + + + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-13T20:57:29.969Z,General Question,v1,how would i free all the memory by traversing through a linked list? what code should i write?,,, +63c6479f5f004bf4898cf545,2023-02-13T21:12:00.420Z,Question from Code,v1,why is there a memory leak after freeing the posts?,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + + // Create a variable to traverse the list + User *curr = *user_ptr_del; + // Create a variable to hold the deleted user + User *temp = *user_ptr_del; + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + remove_friends(name, user_ptr_del); + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = curr->next; + remove_friends(name, user_ptr_del); + free(temp); + return 0; + } + + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + + // If name does not exist + if (curr->next == NULL) { + return 1; + } + + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + + + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + if (curr_post != NULL) { + curr_post = curr_post->next; + } + } + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-13T21:13:30.358Z,Question from Code,v1,how can i fix the memory leak when freeing the posts,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + + // Create a variable to traverse the list + User *curr = *user_ptr_del; + // Create a variable to hold the deleted user + User *temp = *user_ptr_del; + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + remove_friends(name, user_ptr_del); + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = curr->next; + remove_friends(name, user_ptr_del); + free(temp); + return 0; + } + + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + + // If name does not exist + if (curr->next == NULL) { + return 1; + } + + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + + + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + if (curr_post != NULL) { + curr_post = curr_post->next; + } + } + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-13T21:14:44.502Z,Question from Code,v1,the memory leak is caused by the if (curr_post != NULL) statement. how can i fix this,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + + // Create a variable to traverse the list + User *curr = *user_ptr_del; + // Create a variable to hold the deleted user + User *temp = *user_ptr_del; + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + remove_friends(name, user_ptr_del); + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = curr->next; + remove_friends(name, user_ptr_del); + free(temp); + return 0; + } + + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + + // If name does not exist + if (curr->next == NULL) { + return 1; + } + + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + + + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + if (curr_post != NULL) { + curr_post = curr_post->next; + } + } + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-13T21:15:19.409Z,Question from Code,v1,how do i remove the last post of the linked list,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + + // Create a variable to traverse the list + User *curr = *user_ptr_del; + // Create a variable to hold the deleted user + User *temp = *user_ptr_del; + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + remove_friends(name, user_ptr_del); + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free(temp); + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = curr->next; + remove_friends(name, user_ptr_del); + free(temp); + return 0; + } + + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + + // If name does not exist + if (curr->next == NULL) { + return 1; + } + + // If user to be removed is the last one + if (curr->next->next == NULL) { + temp = curr->next; + curr->next = NULL; + } else { + // If user to be removed is in the middle of the list + temp = curr->next; + curr->next = curr->next->next; + } + + + Post *curr_post = temp->first_post; + while (curr_post != NULL) { + free(curr_post->date); + free(curr_post); + if (curr_post != NULL) { + curr_post = curr_post->next; + } + } + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-13T21:22:19.535Z,Help Fix Code,v1,," Post *curr_post = temp->first_post; + if (curr_post->next == NULL) { + free(curr_post->date); + free(curr_post); + } else if (curr_post->next !=NULL) { + while (curr_post->next != NULL) { + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + free(curr_post->date); + free(curr_post); + } +",delete all posts and free the memory, +63c6479f5f004bf4898cf545,2023-02-13T21:25:58.846Z,Help Fix Code,v1,," // Free post memory + Post *curr_post = temp->first_post; + Post *temp_post = temp->first_post; + if (curr_post->next == NULL) { + free(curr_post->date); + free(curr_post); + } else if (curr_post->next !=NULL) { + while (curr_post->next != NULL) { + temp_post = curr_post + free(curr_post->date); + free(curr_post); + curr_post = temp_post->next; + } + free(curr_post->date); + free(curr_post); + }",delete all posts and free the memory, +63c6479f5f004bf4898cf545,2023-02-13T21:26:42.535Z,Help Fix Code,v1,," // Free post memory + Post *curr_post = temp->first_post; + Post *temp_post = temp->first_post; + if (curr_post->next == NULL) { + free(curr_post->date); + free(curr_post); + } else if (curr_post->next !=NULL) { + while (curr_post->next != NULL) { + temp_post = curr_post; + free(curr_post->date); + free(curr_post); + curr_post = temp_post->next; + } + free(curr_post->date); + free(curr_post); + }",delete all posts and free the memory, +63c6479f5f004bf4898cf545,2023-02-13T21:27:33.076Z,Help Fix Code,v1,," // Free post memory + Post *curr_post = temp->first_post; + Post *temp_post = curr_post; + if (curr_post->next == NULL) { + free(curr_post->date); + free(curr_post); + } else if (curr_post->next !=NULL) { + while (curr_post->next != NULL) { + temp_post = curr_post; + free(curr_post->date); + free(curr_post); + curr_post = temp_post->next; + } + free(curr_post->date); + free(curr_post); + }",delete all posts and free the memory, +63c6479f5f004bf4898cf545,2023-02-13T21:29:34.317Z,Question from Code,v1,how can i fix the segmentation fault," // Free post memory + Post *curr_post = temp->first_post; + Post *temp_post = curr_post; + if (curr_post->next == NULL) { + free(curr_post->date); + free(curr_post); + } else if (curr_post->next !=NULL) { + while (curr_post->next != NULL) { + temp_post = curr_post; + free(curr_post->date); + free(curr_post); + curr_post = temp_post->next; + } + free(curr_post->date); + free(curr_post); + }",, +63c6479f5f004bf4898cf545,2023-02-13T21:31:35.043Z,Question from Code,v1,"will this give me a segfault? if so, how can i fix it"," // Free post memory + Post *curr_post = temp->first_post; + Post *temp_post = curr_post; + if (curr_post->next == NULL) { + free(curr_post->date); + free(curr_post); + } else if (curr_post->next !=NULL) { + while (curr_post->next != NULL) { + temp_post = curr_post; + free(temp->date); + free(temp); + curr_post = curr_post->next; + } + free(curr_post->date); + free(curr_post); + }",, +63c6479f5f004bf4898cf545,2023-02-13T21:33:34.283Z,Question from Code,v1,how do i fix the segfault," // Free post memory + Post *curr_post = temp->first_post; + Post *temp_post = curr_post; + if (curr_post->next == NULL) { + free(curr_post->date); + free(curr_post); + } else if (curr_post->next !=NULL) { + while (curr_post->next != NULL) { + temp_post = curr_post; + free(temp_post->date); + free(temp_post); + curr_post = curr_post->next; + } + free(curr_post->date); + free(curr_post); + }",, +63c6479f5f004bf4898cf545,2023-02-13T21:35:38.496Z,Question from Code,v1,what causes the segfault," // Free post memory + Post *curr_post = temp->first_post; + Post *temp_post = curr_post; + if (curr_post->next == NULL) { + free(curr_post->date); + free(curr_post); + } else if (curr_post->next !=NULL) { + while (curr_post->next != NULL) { + temp_post = curr_post; + curr_post = curr_post->next; + free(temp_post->date); + free(temp_post); + + } + free(curr_post->date); + free(curr_post); + }",, +63c6479f5f004bf4898cf545,2023-02-13T21:36:15.116Z,Question from Code,v1,how do i free the last post," // Free post memory + Post *curr_post = temp->first_post; + Post *temp_post = curr_post; + if (curr_post->next == NULL) { + free(curr_post->date); + free(curr_post); + } else if (curr_post->next !=NULL) { + while (curr_post->next != NULL) { + temp_post = curr_post; + curr_post = curr_post->next; + free(temp_post->date); + free(temp_post); + + } + free(curr_post->date); + free(curr_post); + }",, +63c6479f5f004bf4898cf545,2023-02-13T21:39:09.738Z,Question from Code,v1,is this code correct," Post *curr_post = temp->first_post; + Post *temp_post = curr_post; + if (curr_post->next == NULL) { + free(curr_post->date); + free(curr_post); + } else if (curr_post->next !=NULL) { + while (curr_post->next != NULL) { + temp_post = curr_post; + curr_post = curr_post->next; + free(temp_post->date); + free(temp_post); + if (curr_post->next == NULL) { + free(curr_post->date); + free(curr_post); + } + } + }",, +63c6479f5f004bf4898cf545,2023-02-13T21:44:15.078Z,Question from Code,v1,will this code properly remove the last post?," Post *curr_post = temp->first_post; + while (curr_post != NULL) { + Post *temp_post = curr_post; + curr_post = curr_post->next; + free(temp_post->date); + free(temp_post); + }",, +63c6479f5f004bf4898cf545,2023-02-13T23:04:06.725Z,Question from Code,v1,why does this cause a memory leak,"User *find_user(const char *name, const User *head) { + // Create a pointer to traverse the list + User *curr = (User *)head; + + // Traverse the list until the end + while (curr != NULL) { + // If the name matches the name of the current element, return + // the element + if (strcmp(curr->name, name) == 0) { + return curr; + } + curr = curr->next; + } + + // If end of the list is reached and user is not found, return NULL + return NULL; +}",, +63c6479f5f004bf4898cf545,2023-02-13T23:04:31.244Z,Question from Code,v1,does anything need to be freed,"User *find_user(const char *name, const User *head) { + // Create a pointer to traverse the list + User *curr = (User *)head; + + // Traverse the list until the end + while (curr != NULL) { + // If the name matches the name of the current element, return + // the element + if (strcmp(curr->name, name) == 0) { + return curr; + } + curr = curr->next; + } + + // If end of the list is reached and user is not found, return NULL + return NULL; +}",, +63c6479f5f004bf4898cf545,2023-02-13T23:04:50.122Z,Question from Code,v1,does this cause a memory leak,"User *find_user(const char *name, const User *head) { + // Create a pointer to traverse the list + User *curr = (User *)head; + + // Traverse the list until the end + while (curr != NULL) { + // If the name matches the name of the current element, return + // the element + if (strcmp(curr->name, name) == 0) { + return curr; + } + curr = curr->next; + } + + // If end of the list is reached and user is not found, return NULL + return NULL; +}",, +63c6479f5f004bf4898cf545,2023-02-13T23:05:15.139Z,Question from Code,v1,is this correct,"User *find_user(const char *name, const User *head) { + // Create a pointer to traverse the list + User *curr = (User *)head; + + // Traverse the list until the end + while (curr != NULL) { + // If the name matches the name of the current element, return + // the element + if (strcmp(curr->name, name) == 0) { + return curr; + } + curr = curr->next; + } + + // If end of the list is reached and user is not found, return NULL + return NULL; +}",, +63c6479f5f004bf4898cf545,2023-02-13T23:06:27.594Z,Question from Code,v1,is this correct,"int create_user(const char *name, User **user_ptr_add) { + // Check if name is too long + if (strlen(name) >= MAX_NAME) { + return 2; + } + + // Create a pointer curr to traverse the list + User *curr = *user_ptr_add; + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""Failed Malloc""); + exit(-1); + } + strcpy(new_user->name, name); + new_user->next = NULL; + + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + // Traverse the list until the end + while (curr->next != NULL) { + // Check if name already exists + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + } + + // Check if the name is the same as the name of the last user + // in the list + if (strcmp(name, curr->name) == 0) { + return 1; + } + // Make the last element equal to our new user + curr->next = new_user; + + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-14T20:18:23.962Z,Question from Code,v1,will this properly check for a full friendlist," // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + }",, +63c6479f5f004bf4898cf545,2023-02-14T20:19:22.681Z,Question from Code,v1,will this properly check if at least one of the users have a full friendslist," // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + }",, +63c6479f5f004bf4898cf545,2023-02-14T20:26:28.157Z,Question from Code,v1,"will this code work as intended: + + Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies.","int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while (f1_added == 0 || f2_added == 0) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + } + count++; + } + + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-14T20:27:32.209Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while (f1_added == 0 || f2_added == 0) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + } + count++; + } + + return 0; +} +"," Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies.", +63c6479f5f004bf4898cf545,2023-02-14T20:57:37.859Z,Question from Code,v1,why does the first element of friendlist get added to the end of the list when i add the MAX_FRIEND th friend to a friendlist,"int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while (f1_added == 0 || f2_added == 0) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + } + count++; + } + + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-14T20:58:27.988Z,Question from Code,v1,how can i check if the friendlist is already full,"int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while (f1_added == 0 || f2_added == 0) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + } + count++; + } + + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-14T20:59:23.036Z,Question from Code,v1,should i check if the MAX_FRIEND th number or the MAX_FRIEND - 1 th number of the friendlist is null,"int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while (f1_added == 0 || f2_added == 0) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + } + count++; + } + + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-14T21:06:33.121Z,Question from Code,v1,an extra element is added everytime i added the MAX_FRIENDS th friend. do you know why,"int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while (f1_added == 0 || f2_added == 0) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + } + count++; + } + + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-14T21:07:09.272Z,Question from Code,v1,"why will The while loop always run at least once, even if f1_added and f2_added are both 1?","int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while (f1_added == 0 || f2_added == 0) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + } + count++; + } + + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-14T21:08:50.320Z,Question from Code,v1,what do i need to change so that an extra element isnt added everytime i added the MAX_FRIENDS th friend,"int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while (f1_added == 0 || f2_added == 0) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + } + count++; + } + + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-14T21:14:40.153Z,Question from Code,v1,what do i need to change so that an extra element isnt added everytime i added the MAX_FRIENDS th friend? is this code correct?,"int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while (f1_added == 0 || f2_added == 0) { + if (friend1->friends[count] == NULL && f1_added == 0 && friend1->friends[MAX_FRIENDS - 1] != NULL) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + + } + if (friend2->friends[count] == NULL && f2_added == 0 && friend2->friends[MAX_FRIENDS - 1] != NULL) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + + } + count++; + } + + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-14T21:25:54.681Z,Question from Code,v1,"does my code properly check for if the friendlist is full, or if the friend already exists","int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while (f1_added == 0 || f2_added == 0) { + if (friend1->friends[count] == NULL && f1_added == 0 && friend1->friends[MAX_FRIENDS - 1] != NULL) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + + } + if (friend2->friends[count] == NULL && f2_added == 0 && friend2->friends[MAX_FRIENDS - 1] != NULL) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + + } + count++; + } + + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-14T21:30:27.712Z,Question from Code,v1,is it possible for a friend to be added to the same list twice in my code,"int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while (f1_added == 0 || f2_added == 0) { + if (friend1->friends[count] == NULL && f1_added == 0 && friend1->friends[MAX_FRIENDS - 1] != NULL) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + + } + if (friend2->friends[count] == NULL && f2_added == 0 && friend2->friends[MAX_FRIENDS - 1] != NULL) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + + } + count++; + } + + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-15T00:42:43.637Z,Question from Code,v1,"is this code correct? if not what do i need to to do to fix it +","int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while (f1_added == 0 || f2_added == 0) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + + } + count++; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-15T00:44:00.999Z,Question from Code,v1,"when i run the program, when i add the MAX_FRIEND th name, the first element is added as the MAX_FRIEND th + 1 element. is this function causing that kind of error? ","int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while (f1_added == 0 || f2_added == 0) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + + } + count++; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-15T00:45:18.551Z,Question from Code,v1,"when i run the program, when i add the MAX_FRIEND th name, the first element is added as the MAX_FRIEND th + 1 element. why might the find_user function cause this?","int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while (f1_added == 0 || f2_added == 0) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + + } + count++; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-15T00:46:37.032Z,Question from Code,v1,"when i run the program, when i add the MAX_FRIEND th name, the first element is added as the MAX_FRIEND th + 1 element. is the find_user function causing this? how can i fix it","User *find_user(const char *name, const User *head) { + // Create a pointer to traverse the list + User *curr = (User *)head; + + while (curr != NULL) { // Traverse the list + if (strcmp(curr->name, name) == 0) { // Return the user if found + return curr; + } + curr = curr->next; + } + + return NULL; // If end of the list is reached and user is not found, return NULL +} +int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while (f1_added == 0 || f2_added == 0) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + + } + count++; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-15T00:50:48.955Z,Question from Code,v1,"when i run the program, when i add the MAX_FRIEND th name, the first element is added as the MAX_FRIEND th + 1 element. is the find_user function causing this? how can i fix it","User *find_user(const char *name, const User *head) { + // Create a pointer to traverse the list + User *curr = (User *)head; + + while (curr != NULL) { // Traverse the list + if (strcmp(curr->name, name) == 0) { // Return the user if found + return curr; + } + curr = curr->next; + } + + return NULL; // If end of the list is reached and user is not found, return NULL +} +int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + + } + count++; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-15T00:52:50.019Z,Question from Code,v1,"when i run the program, when i add the MAX_FRIEND th name, the first element is re-added as the MAX_FRIEND th + 1 element. how can i fix it? Doesnt the f1_added and f2_added check prevent this extra addition?","User *find_user(const char *name, const User *head) { + // Create a pointer to traverse the list + User *curr = (User *)head; + + while (curr != NULL) { // Traverse the list + if (strcmp(curr->name, name) == 0) { // Return the user if found + return curr; + } + curr = curr->next; + } + + return NULL; // If end of the list is reached and user is not found, return NULL +} +int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + + } + count++; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-15T00:54:05.474Z,Question from Code,v1,why does this program work fine until i add the last element that friendlist is able to contain?,"User *find_user(const char *name, const User *head) { + // Create a pointer to traverse the list + User *curr = (User *)head; + + while (curr != NULL) { // Traverse the list + if (strcmp(curr->name, name) == 0) { // Return the user if found + return curr; + } + curr = curr->next; + } + + return NULL; // If end of the list is reached and user is not found, return NULL +} +int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + + } + count++; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-15T00:54:48.915Z,Question from Code,v1,"why does this program work fine until i add the last element that friendlist is able to contain, and then for some reason the first element is added to the end of the list?","User *find_user(const char *name, const User *head) { + // Create a pointer to traverse the list + User *curr = (User *)head; + + while (curr != NULL) { // Traverse the list + if (strcmp(curr->name, name) == 0) { // Return the user if found + return curr; + } + curr = curr->next; + } + + return NULL; // If end of the list is reached and user is not found, return NULL +} +int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == friend2 || friend2->friends[i] == friend1) { + return 1; + } + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + + } + count++; + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-15T00:58:35.272Z,Question from Code,v1,is this code correct?," + // Keep looping so long as a new friend is not added to both users + while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + + } + }",, +63c6479f5f004bf4898cf545,2023-02-15T00:59:33.052Z,Question from Code,v1,could this code be the reason why the first element of the list is added to the end of the list when the MAX_FRIEND th element is added to a list?," + // Keep looping so long as a new friend is not added to both users + while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + + } + }",, +63c6479f5f004bf4898cf545,2023-02-15T01:00:04.560Z,Question from Code,v1,"could this code be the reason why the first element of the list is added to the end of the list when the MAX_FRIEND th element is added to a list? +what do i need to add to fix it"," + // Keep looping so long as a new friend is not added to both users + while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) { + if (friend1->friends[count] == NULL && f1_added == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + + } + if (friend2->friends[count] == NULL && f2_added == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + + } + }",, +63c6479f5f004bf4898cf545,2023-02-15T01:19:06.077Z,Question from Code,v1,"i dont understand why when i add the MAX_FRIENDS th friend, the first element is added to the end of the list (so it appears twice)","int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + if (check_if_not_friends(friend1, friend2) == 1) { + return 1; + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) { + if (friend1->friends[count] == NULL && f1_added == 0) { + if (check_if_not_friends(friend1, friend2) == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + } + } + if (friend2->friends[count] == NULL && f2_added == 0) { + if (check_if_not_friends(friend2, friend1)) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + } + } + count++; + } + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T01:20:11.285Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + if (check_if_not_friends(friend1, friend2) == 1) { + return 1; + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) { + if (friend1->friends[count] == NULL && f1_added == 0) { + if (check_if_not_friends(friend1, friend2) == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + } + } + if (friend2->friends[count] == NULL && f2_added == 0) { + if (check_if_not_friends(friend2, friend1)) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + } + } + count++; + } + return 0; +}","Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies.", +63c6479f5f004bf4898cf545,2023-02-15T01:21:32.088Z,Question from Code,v1,"will this code always work as inteneded? + +Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies.","int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + if (check_if_not_friends(friend1, friend2) == 1) { + return 1; + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) { + if (friend1->friends[count] == NULL && f1_added == 0) { + if (check_if_not_friends(friend1, friend2) == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + } + } + if (friend2->friends[count] == NULL && f2_added == 0) { + if (check_if_not_friends(friend2, friend1)) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + } + } + count++; + } + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T01:22:22.152Z,Question from Code,v1,"will this code always work as inteneded? Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies."," +/* + * Helper function that determines if users are already friends + */ +int check_if_not_friends(User *user1, User *user2) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == user2) { + return 1; // If user already exists + } + } + return 0; // If user does not exist in friendlist +} + +int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + if (check_if_not_friends(friend1, friend2) == 1) { + return 1; + } + + // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) { + if (friend1->friends[count] == NULL && f1_added == 0) { + if (check_if_not_friends(friend1, friend2) == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + } + } + if (friend2->friends[count] == NULL && f2_added == 0) { + if (check_if_not_friends(friend2, friend1)) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + } + } + count++; + } + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T01:45:02.870Z,Question from Code,v1,what is a better more correct way to do this?," // Create variables to track if the friends were added to each other's + // friend lists + int f1_added = 0; + int f2_added = 0; + + // Create a counter to track list index + int count = 0; + + // Keep looping so long as a new friend is not added to both users + while ((f1_added == 0 || f2_added == 0) && count < MAX_FRIENDS) { + if (friend1->friends[count] == NULL && f1_added == 0) { + if (check_if_not_friends(friend1, friend2) == 0) { + friend1->friends[count] = friend2; + f1_added = 1; // Track that friend2 was added to friend1's friendlist + } + } + if (friend2->friends[count] == NULL && f2_added == 0) { + if (check_if_not_friends(friend2, friend1) == 0) { + friend2->friends[count] = friend1; + f2_added = 1; // Track that friend 1 was added to friend2's friendlist + } + } + count++; + }",, +63c6479f5f004bf4898cf545,2023-02-15T01:55:55.523Z,Question from Code,v1,is this code correct,"int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + if (check_if_not_friends(friend1, friend2) == 1) { + return 1; + } + + // Set the first NULL element to friend2 + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == NULL) { + friend1->friends[i] = friend2; + i = MAX_FRIENDS; // Update i to exit the loop + } + } + + // Set the first NULL element to friend1 + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend2->friends[i] == NULL) { + friend2->friends[i] = friend1; + i = MAX_FRIENDS; // Update i to exit the loop + } + } +}",, +63c6479f5f004bf4898cf545,2023-02-15T01:56:40.076Z,Question from Code,v1,is this correct,"int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + if (check_if_not_friends(friend1, friend2) == 1) { + return 1; + } + + // Set the first NULL element to friend2 + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == NULL) { + friend1->friends[i] = friend2; + i = MAX_FRIENDS; // Update i to exit the loop + } + } + + // Set the first NULL element to friend1 + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend2->friends[i] == NULL) { + friend2->friends[i] = friend1; + i = MAX_FRIENDS; // Update i to exit the loop + } + } + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T02:00:08.246Z,Question from Code,v1,is this code correct,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { // Check if name is too long + return 2; + } + + User *curr = *user_ptr_add; // Create a pointer curr to traverse the list + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(-1); + } + strcpy(new_user->name, name); + new_user->next = NULL; + + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { // Traverse the list + if (strcmp(name, curr->name) == 0) { // Check if name already exists + return 1; + } + curr = curr->next; + } + + if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name + return 1; + } + + curr->next = new_user; // Make the last element equal to our new user + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T02:00:45.908Z,Question from Code,v1,will this code edd a user if it is the only one in the list,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { // Check if name is too long + return 2; + } + + User *curr = *user_ptr_add; // Create a pointer curr to traverse the list + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(-1); + } + strcpy(new_user->name, name); + new_user->next = NULL; + + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { // Traverse the list + if (strcmp(name, curr->name) == 0) { // Check if name already exists + return 1; + } + curr = curr->next; + } + + if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name + return 1; + } + + curr->next = new_user; // Make the last element equal to our new user + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T02:01:24.473Z,Question from Code,v1,are both these functions correct,"/* + * Return a pointer to the user with this name in + * the list starting with head. Return NULL if no such user exists. + * + * NOTE: You'll likely need to cast a (const User *) to a (User *) + * to satisfy the prototype without warnings. + */ +User *find_user(const char *name, const User *head) { + // Create a pointer to traverse the list + User *curr = (User *)head; + + while (curr != NULL) { // Traverse the list + if (strcmp(curr->name, name) == 0) { // Return the user if found + return curr; + } + curr = curr->next; + } + + return NULL; // If end of the list is reached and user is not found, return NULL +} + + +/* + * Print the usernames of all users in the list starting at curr. + * Names should be printed to standard output, one per line. + */ +void list_users(const User *curr) { + printf(""User List\n""); // Print the header + User *curr_user = (User *)curr; // Create a pointer to traverse the list + + while (curr_user != NULL) { // Traverse the list + printf(""\t%s\n"", curr_user->name); // Print the names + curr_user = curr_user->next; + } +}",, +63c6479f5f004bf4898cf545,2023-02-15T02:04:20.287Z,General Question,v1,"I have a program that allows you to create users and make them friends with each other. There is a maximum number of friends allowed for each user. when i make two users friends and one of the friend's friendlist is now full, for some reason, in addition to the added friend being added to the friendlist, the first friend that was added to their friendlist is duplicated and added to the very end of the list. what is causing this?",,, +63c6479f5f004bf4898cf545,2023-02-15T02:11:10.904Z,Question from Code,v1,"I have a program that allows you to create users and make them friends with each other. There is a maximum number of friends allowed for each user. when i make two users friends and one of the friend's friendlist is now full, for some reason, in addition to the added friend being added to the friendlist, the first friend that was added to their friendlist is duplicated and added to the very end of the list. what is causing this?","int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + if (check_if_not_friends(friend1, friend2) == 1) { + return 1; + } + + // Set the first NULL element to friend2 + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == NULL) { + friend1->friends[i] = friend2; + i = MAX_FRIENDS; // Update i to exit the loop + } + } + + // Set the first NULL element to friend1 + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend2->friends[i] == NULL) { + friend2->friends[i] = friend1; + i = MAX_FRIENDS; // Update i to exit the loop + } + } + + return 0; +} + + +",, +63c6479f5f004bf4898cf545,2023-02-15T02:12:03.235Z,Question from Code,v1,"why is this the case?: If friend1 is added to friend2's friendlist and friend2 is added to friend1's friendlist, then friend1 is added to friend1's friendlist.","int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + if (check_if_not_friends(friend1, friend2) == 1) { + return 1; + } + + // Set the first NULL element to friend2 + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == NULL) { + friend1->friends[i] = friend2; + i = MAX_FRIENDS; // Update i to exit the loop + } + } + + // Set the first NULL element to friend1 + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend2->friends[i] == NULL) { + friend2->friends[i] = friend1; + i = MAX_FRIENDS; // Update i to exit the loop + } + } + + return 0; +} + + +",, +63c6479f5f004bf4898cf545,2023-02-15T02:13:06.300Z,Question from Code,v1,in which cases will this code be wrong?,"int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + if (check_if_not_friends(friend1, friend2) == 1) { + return 1; + } + + // Set the first NULL element to friend2 + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == NULL) { + friend1->friends[i] = friend2; + i = MAX_FRIENDS; // Update i to exit the loop + } + } + + // Set the first NULL element to friend1 + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend2->friends[i] == NULL) { + friend2->friends[i] = friend1; + i = MAX_FRIENDS; // Update i to exit the loop + } + } + + return 0; +} + + +",, +63c6479f5f004bf4898cf545,2023-02-15T02:14:11.165Z,Question from Code,v1,"when the MAX_FRIEND th element is added, the first element of the friendlist is added too. why are two friends being added when the last element of friendlist is being set?","int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + if (check_if_not_friends(friend1, friend2) == 1) { + return 1; + } + + // Set the first NULL element to friend2 + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == NULL) { + friend1->friends[i] = friend2; + i = MAX_FRIENDS; // Update i to exit the loop + } + } + + // Set the first NULL element to friend1 + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend2->friends[i] == NULL) { + friend2->friends[i] = friend1; + i = MAX_FRIENDS; // Update i to exit the loop + } + } + + return 0; +} + + +",, +63c6479f5f004bf4898cf545,2023-02-15T02:22:10.864Z,Question from Code,v1,is it possible that this function could add 2 elements to the array of friends instead of 1? when does this happen? how can i prevent this,"int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + if (check_if_not_friends(friend1, friend2) == 1) { + return 1; + } + + // Set the first NULL element to friend2 + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == NULL) { + friend1->friends[i] = friend2; + i = MAX_FRIENDS; // Update i to exit the loop + } + } + + // Set the first NULL element to friend1 + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend2->friends[i] == NULL) { + friend2->friends[i] = friend1; + i = MAX_FRIENDS; // Update i to exit the loop + } + } + + return 0; +} + + +",, +63c6479f5f004bf4898cf545,2023-02-15T02:26:03.077Z,Question from Code,v1,why is it that friend1->friends[i]->name is null when the code runs,"int make_friends(const char *name1, const char *name2, User *head) { + // Create users friend1 and friend2 + User *friend1 = find_user(name1, head); + User *friend2 = find_user(name2, head); + + // Check for user non-existence + if (friend1 == NULL || friend2 == NULL) { + return 4; + } + + // Check for same user passed twice + if (friend1 == friend2) { + return 3; + } + + // Check for full friends list + if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) { + return 2; + } + + // Check for friend already exists + if (check_if_not_friends(friend1, friend2) == 1) { + return 1; + } + + // Set the first NULL element to friend2 + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend1->friends[i] == NULL) { + friend1->friends[i] = friend2; + i = MAX_FRIENDS; // Update i to exit the loop + } + } + + // Set the first NULL element to friend1 + for (int i = 0; i < MAX_FRIENDS; i++) { + if (friend2->friends[i] == NULL) { + friend2->friends[i] = friend1; + i = MAX_FRIENDS; // Update i to exit the loop + } + } + + return 0; +} + + +",, +63c6479f5f004bf4898cf545,2023-02-15T02:40:12.830Z,Question from Code,v1,is a user struct properly being initialized here?,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { // Check if name is too long + return 2; + } + + User *curr = *user_ptr_add; // Create a pointer curr to traverse the list + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(-1); + } + + // Initialize attributes + strcpy(new_user->name, name); + new_user->next = NULL; + + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { // Traverse the list + if (strcmp(name, curr->name) == 0) { // Check if name already exists + return 1; + } + curr = curr->next; + } + + if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name + return 1; + } + + curr->next = new_user; // Make the last element equal to our new user + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T02:40:37.992Z,Question from Code,v1,ais a user struct and its attributes properly being initialized here?,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { // Check if name is too long + return 2; + } + + User *curr = *user_ptr_add; // Create a pointer curr to traverse the list + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(-1); + } + + // Initialize attributes + strcpy(new_user->name, name); + new_user->next = NULL; + + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { // Traverse the list + if (strcmp(name, curr->name) == 0) { // Check if name already exists + return 1; + } + curr = curr->next; + } + + if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name + return 1; + } + + curr->next = new_user; // Make the last element equal to our new user + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T03:16:54.057Z,Question from Code,v1,is space properly being allocated ,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { // Check if name is too long + return 2; + } + + User *curr = *user_ptr_add; // Create a pointer curr to traverse the list + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(-1); + } + strcpy(new_user->name, name); + new_user->next = NULL; + + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { // Traverse the list + if (strcmp(name, curr->name) == 0) { // Check if name already exists + return 1; + } + curr = curr->next; + } + + if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name + return 1; + } + + curr->next = new_user; // Make the last element equal to our new user + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T03:22:46.497Z,Question from Code,v1,will this correctly remove the data in post? should i set it to null as well?,"void free_posts(Post *post_ptr) { + Post *curr = post_ptr; // Create a variable to traverse the linked list + while (curr != NULL) { + Post *temp_post = curr; // Create a temporary variable to hold a post + curr = curr->next; + if (temp_post != NULL) { // If the post is not null, free the post and its contents + free(temp_post->date); + free(temp_post->contents); + free(temp_post); + } + } +}",, +63c6479f5f004bf4898cf545,2023-02-15T03:26:39.358Z,Question from Code,v1,will this correctly remove the user and their posts,"int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; // Variable to traverse the list + User *temp = curr; // Variable to hold the deleted user + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + // Remove the user from all friendlists, and clear user's list of friends + remove_friends(name, user_ptr_del); + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free_posts(temp->first_post); // Free the memory of the posts of the deleted user + if (temp != NULL) { + free(temp); // Free the memory of the user + } + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = curr->next; + free_posts(temp->first_post); // Free the memory of the posts of the deleted user + if (temp != NULL) { + free(temp); // Free the memory of the user + } + return 0; + } + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + + // If name does not exist + if (curr->next == NULL) { + return 1; + } + + if (curr->next->next == NULL) { // If the last user is to be removed + temp = curr->next; + curr->next = NULL; + } else { + temp = curr->next; // If the user to be removed is in the middle of the list + curr->next = curr->next->next; + } + + free_posts(temp->first_post); // Free the memory of the posts of the deleted user + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T03:27:30.816Z,Question from Code,v1,"why is it that when i add a post to the user, delete the user and add back the user with the same name,their posts are still there","int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; // Variable to traverse the list + User *temp = curr; // Variable to hold the deleted user + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + // Remove the user from all friendlists, and clear user's list of friends + remove_friends(name, user_ptr_del); + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free_posts(temp->first_post); // Free the memory of the posts of the deleted user + if (temp != NULL) { + free(temp); // Free the memory of the user + } + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = curr->next; + free_posts(temp->first_post); // Free the memory of the posts of the deleted user + if (temp != NULL) { + free(temp); // Free the memory of the user + } + return 0; + } + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + + // If name does not exist + if (curr->next == NULL) { + return 1; + } + + if (curr->next->next == NULL) { // If the last user is to be removed + temp = curr->next; + curr->next = NULL; + } else { + temp = curr->next; // If the user to be removed is in the middle of the list + curr->next = curr->next->next; + } + + free_posts(temp->first_post); // Free the memory of the posts of the deleted user + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T03:28:15.234Z,Question from Code,v1,do i need to call free_posts anywhere else to correctly remove the posts from the deleted user?," +int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; // Variable to traverse the list + User *temp = curr; // Variable to hold the deleted user + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + // Remove the user from all friendlists, and clear user's list of friends + remove_friends(name, user_ptr_del); + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free_posts(temp->first_post); // Free the memory of the posts of the deleted user + if (temp != NULL) { + free(temp); // Free the memory of the user + } + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = curr->next; + free_posts(temp->first_post); // Free the memory of the posts of the deleted user + if (temp != NULL) { + free(temp); // Free the memory of the user + } + return 0; + } + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + + // If name does not exist + if (curr->next == NULL) { + return 1; + } + + if (curr->next->next == NULL) { // If the last user is to be removed + temp = curr->next; + curr->next = NULL; + } else { + temp = curr->next; // If the user to be removed is in the middle of the list + curr->next = curr->next->next; + } + + free_posts(temp->first_post); // Free the memory of the posts of the deleted user + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T03:29:13.633Z,Question from Code,v1,will this code correctly remove all the posts from the deleted user,"void free_posts(Post *post_ptr) { + Post *curr = post_ptr; // Create a variable to traverse the linked list + while (curr != NULL) { + Post *temp_post = curr; // Create a temporary variable to hold a post + curr = curr->next; + if (temp_post != NULL) { // If the post is not null, free the post and its contents + free(temp_post->date); + free(temp_post->contents); + free(temp_post); + } + } +} + +int delete_user(const char *name, User **user_ptr_del) { + // Delete user from list + User *curr = *user_ptr_del; // Variable to traverse the list + User *temp = curr; // Variable to hold the deleted user + + // Check for empty users list + if (*user_ptr_del == NULL) { + return 1; + } + + // Remove the user from all friendlists, and clear user's list of friends + remove_friends(name, user_ptr_del); + + // If there is 1 element in the list + if (curr->next == NULL) { + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = NULL; + free_posts(temp->first_post); // Free the memory of the posts of the deleted user + if (temp != NULL) { + free(temp); // Free the memory of the user + } + return 0; + } else { + return 1; + } + } + + // If 1st element is to be removed + if (strcmp(curr->name, name) == 0) { + *user_ptr_del = curr->next; + free_posts(temp->first_post); // Free the memory of the posts of the deleted user + if (temp != NULL) { + free(temp); // Free the memory of the user + } + return 0; + } + // Iterate through the users until the end of the list is reached, + // or until the name of curr matches the name of the deleted user + while (strcmp(curr->next->name, name) != 0 && curr->next != NULL) { + curr = curr->next; + } + + // If name does not exist + if (curr->next == NULL) { + return 1; + } + + if (curr->next->next == NULL) { // If the last user is to be removed + temp = curr->next; + curr->next = NULL; + } else { + temp = curr->next; // If the user to be removed is in the middle of the list + curr->next = curr->next->next; + } + + free_posts(temp->first_post); // Free the memory of the posts of the deleted user + + // Free the memory in temp + if (temp != NULL) { + free(temp); + temp = NULL; + } + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T03:30:40.092Z,Question from Code,v1,does this code properly create a post and add it to a user,"int make_post(const User *author, User *target, char *contents) { + // Check if users exist + if (author == NULL || target == NULL) { + return 2; + } + + int friend_mutuality = 0; // A variable to track if users are friends + + // Check if author is friends with target + for(int i = 0; i < MAX_FRIENDS; i++) { + if (author->friends[i] == target) { + friend_mutuality++; // Update the tracking variable + } + } + + if (friend_mutuality == 0) { + return 1; + } + + // Make a new post + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); + exit(-1); + } + strcpy(new_post->author, author->name); // Set the post author + new_post->contents = contents; // Set the post contents + time_t *curr_time = malloc(sizeof(time_t)); // Set the post date + if (curr_time == NULL) { + perror(""malloc""); + exit(-1); + } + time(curr_time) ; + new_post->date = curr_time; + + // If target has no posts, set the next post to NULL, + // Otherwise, set the next post to the second most recent post + if (target->first_post == NULL) { + new_post->next = NULL; + } else { + new_post->next = target->first_post; + } + + // Set the target's first post equal to newest post + target->first_post = new_post; + + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-15T03:31:42.373Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + // Check if users exist + if (author == NULL || target == NULL) { + return 2; + } + + int friend_mutuality = 0; // A variable to track if users are friends + + // Check if author is friends with target + for(int i = 0; i < MAX_FRIENDS; i++) { + if (author->friends[i] == target) { + friend_mutuality++; // Update the tracking variable + } + } + + if (friend_mutuality == 0) { + return 1; + } + + // Make a new post + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); + exit(-1); + } + strcpy(new_post->author, author->name); // Set the post author + new_post->contents = contents; // Set the post contents + time_t *curr_time = malloc(sizeof(time_t)); // Set the post date + if (curr_time == NULL) { + perror(""malloc""); + exit(-1); + } + time(curr_time) ; + new_post->date = curr_time; + + // If target has no posts, set the next post to NULL, + // Otherwise, set the next post to the second most recent post + if (target->first_post == NULL) { + new_post->next = NULL; + } else { + new_post->next = target->first_post; + } + + // Set the target's first post equal to newest post + target->first_post = new_post; + + return 0; +} +",create a post and add it to the user, +63c6479f5f004bf4898cf545,2023-02-15T20:32:23.362Z,Question from Code,v1,why might this cause a segfault,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { // Check if name is too long + return 2; + } + + User *curr = *user_ptr_add; // Create a pointer curr to traverse the list + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(-1); + } + strcpy(new_user->name, name); + new_user->next = NULL; + new_user->first_post = NULL; + + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { // Traverse the list + if (strcmp(name, curr->name) == 0) { // Check if name already exists + return 1; + } + curr = curr->next; + } + + if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name + return 1; + } + + curr->next = new_user; // Make the last element equal to our new user + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T20:33:30.860Z,General Question,v1,"how does strncpy work +",,, +63c6479f5f004bf4898cf545,2023-02-15T20:35:28.536Z,Question from Code,v1,how do i initialize the friendlist,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { // Check if name is too long + return 2; + } + + User *curr = *user_ptr_add; // Create a pointer curr to traverse the list + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(-1); + } + strcpy(new_user->name, name); + new_user->next = NULL; + new_user->first_post = NULL; + + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { // Traverse the list + if (strcmp(name, curr->name) == 0) { // Check if name already exists + return 1; + } + curr = curr->next; + } + + if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name + return 1; + } + + curr->next = new_user; // Make the last element equal to our new user + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T20:36:00.775Z,Question from Code,v1,"how do i initialize curr->friends, which is an array","int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { // Check if name is too long + return 2; + } + + User *curr = *user_ptr_add; // Create a pointer curr to traverse the list + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(-1); + } + strcpy(new_user->name, name); + new_user->next = NULL; + new_user->first_post = NULL; + + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { // Traverse the list + if (strcmp(name, curr->name) == 0) { // Check if name already exists + return 1; + } + curr = curr->next; + } + + if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name + return 1; + } + + curr->next = new_user; // Make the last element equal to our new user + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T20:37:16.349Z,Question from Code,v1,"what should i initialize the array friends, which stores struct Users, to?","int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { // Check if name is too long + return 2; + } + + User *curr = *user_ptr_add; // Create a pointer curr to traverse the list + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(-1); + } + strcpy(new_user->name, name); + new_user->next = NULL; + new_user->first_post = NULL; + + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { // Traverse the list + if (strcmp(name, curr->name) == 0) { // Check if name already exists + return 1; + } + curr = curr->next; + } + + if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name + return 1; + } + + curr->next = new_user; // Make the last element equal to our new user + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T20:37:49.513Z,Question from Code,v1,"how do i intialize the array of users, curr->friends, to null","int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { // Check if name is too long + return 2; + } + + User *curr = *user_ptr_add; // Create a pointer curr to traverse the list + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(-1); + } + strcpy(new_user->name, name); + new_user->next = NULL; + new_user->first_post = NULL; + + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { // Traverse the list + if (strcmp(name, curr->name) == 0) { // Check if name already exists + return 1; + } + curr = curr->next; + } + + if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name + return 1; + } + + curr->next = new_user; // Make the last element equal to our new user + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T20:39:08.068Z,General Question,v1,do i need to free memset,,, +63c6479f5f004bf4898cf545,2023-02-15T20:44:15.645Z,Question from Code,v1,what is causing the segfault,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { // Check if name is too long + return 2; + } + + User *curr = *user_ptr_add; // Create a pointer curr to traverse the list + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(-1); + } + strncpy(new_user->name, name, strlen(name)); + new_user->next = NULL; + new_user->first_post = NULL; + memset(curr->friends, 0, sizeof(curr->friends)); + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { // Traverse the list + if (strcmp(name, curr->name) == 0) { // Check if name already exists + return 1; + } + curr = curr->next; + } + + if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name + return 1; + } + + curr->next = new_user; // Make the last element equal to our new user + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T20:44:45.565Z,General Question,v1,how do i initialize user_ptr_add,,, +63c6479f5f004bf4898cf545,2023-02-15T20:45:35.390Z,Question from Code,v1,does user_ptr_add need to be initialized,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { // Check if name is too long + return 2; + } + + User *curr = *user_ptr_add; // Create a pointer curr to traverse the list + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(-1); + } + strncpy(new_user->name, name, strlen(name)); + new_user->next = NULL; + new_user->first_post = NULL; + memset(curr->friends, 0, sizeof(curr->friends)); + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { // Traverse the list + if (strcmp(name, curr->name) == 0) { // Check if name already exists + return 1; + } + curr = curr->next; + } + + if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name + return 1; + } + + curr->next = new_user; // Make the last element equal to our new user + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T20:46:01.463Z,Question from Code,v1,does curr->friends need to be initialized,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { // Check if name is too long + return 2; + } + + User *curr = *user_ptr_add; // Create a pointer curr to traverse the list + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(-1); + } + strncpy(new_user->name, name, strlen(name)); + new_user->next = NULL; + new_user->first_post = NULL; + memset(curr->friends, 0, sizeof(curr->friends)); + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { // Traverse the list + if (strcmp(name, curr->name) == 0) { // Check if name already exists + return 1; + } + curr = curr->next; + } + + if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name + return 1; + } + + curr->next = new_user; // Make the last element equal to our new user + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T20:46:24.771Z,Question from Code,v1,what code do i add to initialize curr->friends ,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { // Check if name is too long + return 2; + } + + User *curr = *user_ptr_add; // Create a pointer curr to traverse the list + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(-1); + } + strncpy(new_user->name, name, strlen(name)); + new_user->next = NULL; + new_user->first_post = NULL; + memset(curr->friends, 0, sizeof(curr->friends)); + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { // Traverse the list + if (strcmp(name, curr->name) == 0) { // Check if name already exists + return 1; + } + curr = curr->next; + } + + if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name + return 1; + } + + curr->next = new_user; // Make the last element equal to our new user + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T20:46:42.863Z,Question from Code,v1,is line 17 correct,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { // Check if name is too long + return 2; + } + + User *curr = *user_ptr_add; // Create a pointer curr to traverse the list + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(-1); + } + strncpy(new_user->name, name, strlen(name)); + new_user->next = NULL; + new_user->first_post = NULL; + memset(curr->friends, 0, sizeof(curr->friends)); + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { // Traverse the list + if (strcmp(name, curr->name) == 0) { // Check if name already exists + return 1; + } + curr = curr->next; + } + + if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name + return 1; + } + + curr->next = new_user; // Make the last element equal to our new user + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-15T20:47:15.587Z,Question from Code,v1,"why is memset(curr->friends, 0, sizeof(curr->friends)); causing the segfault","int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { // Check if name is too long + return 2; + } + + User *curr = *user_ptr_add; // Create a pointer curr to traverse the list + + // Create a new user + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(-1); + } + strncpy(new_user->name, name, strlen(name)); + new_user->next = NULL; + new_user->first_post = NULL; + memset(curr->friends, 0, sizeof(curr->friends)); + // If list is empty, make the head pointer equal to our new user + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + while (curr->next != NULL) { // Traverse the list + if (strcmp(name, curr->name) == 0) { // Check if name already exists + return 1; + } + curr = curr->next; + } + + if (strcmp(name, curr->name) == 0) { // Check if last user in the list has the same name + return 1; + } + + curr->next = new_user; // Make the last element equal to our new user + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-16T22:19:37.610Z,Explain Code,v1,,"#include + +#define SIZE 4 +#define OVERFLOW 5 + +int main() { + int index = 0; + int i; + int before[SIZE] = {10, 10, 10, 10}; + int a[SIZE] = {0, 0, 0, 0}; + int after[SIZE] = {10, 10, 10, 10}; + + printf(""Address of the variables:\n""); + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &after[%d]\n"", (unsigned long) &after[index], index); + } + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &a[%d]\n"", (unsigned long) &a[index], index); + } + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &before[%d]\n"", (unsigned long) &before[index], index); + } + printf(""%lx -> &i\n"", (unsigned long)&i); + printf(""%lx -> &index\n"", (unsigned long)&index); + printf(""\n""); + + + printf(""Initial values:\n""); + printf(""i = %d\n"", i); + printf(""before = {%d, %d, %d, %d}\n"", before[0], before[1], before[2], before[3]); + printf(""a = {%d, %d, %d, %d}\n"", a[0], a[1], a[2], a[3]); + printf(""after = {%d, %d, %d, %d}\n"", after[0], after[1], after[2], after[3]); + printf(""\n""); + + + for (i = 0; i < OVERFLOW; i++) { + a[i] = i * 10; + printf(""i = %d\n"", i); + printf(""before = {%d, %d, %d, %d}\n"", before[0], before[1], before[2], before[3]); + printf(""a = {%d, %d, %d, %d}\n"", a[0], a[1], a[2], a[3]); + printf(""after = {%d, %d, %d, %d}\n"", after[0], after[1], after[2], after[3]); + } + + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-16T22:20:28.404Z,Question from Code,v1,what is this code doing,"#include + +#define SIZE 4 +#define OVERFLOW 5 + +int main() { + int index = 0; + int i; + int before[SIZE] = {10, 10, 10, 10}; + int a[SIZE] = {0, 0, 0, 0}; + int after[SIZE] = {10, 10, 10, 10}; + + printf(""Address of the variables:\n""); + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &after[%d]\n"", (unsigned long) &after[index], index); + } + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &a[%d]\n"", (unsigned long) &a[index], index); + } + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &before[%d]\n"", (unsigned long) &before[index], index); + } + printf(""%lx -> &i\n"", (unsigned long)&i); + printf(""%lx -> &index\n"", (unsigned long)&index); + printf(""\n""); + + + printf(""Initial values:\n""); + printf(""i = %d\n"", i); + printf(""before = {%d, %d, %d, %d}\n"", before[0], before[1], before[2], before[3]); + printf(""a = {%d, %d, %d, %d}\n"", a[0], a[1], a[2], a[3]); + printf(""after = {%d, %d, %d, %d}\n"", after[0], after[1], after[2], after[3]); + printf(""\n""); + + + for (i = 0; i < OVERFLOW; i++) { + a[i] = i * 10; + printf(""i = %d\n"", i); + printf(""before = {%d, %d, %d, %d}\n"", before[0], before[1], before[2], before[3]); + printf(""a = {%d, %d, %d, %d}\n"", a[0], a[1], a[2], a[3]); + printf(""after = {%d, %d, %d, %d}\n"", after[0], after[1], after[2], after[3]); + } + + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-02-16T22:34:35.498Z,Help Write Code,v1,,,,"Now start gdb again but before you start, run script gdb_example.txt to record your interaction. (You'll submit this interaction.) This time make sure you watch the array after. It is pretty slow to step through every line of your code, so use list to find the line number of the for loop where we start to assign values to the array. Set a breakpoint on that line number and also set a breakpoint somewhere before that line. Start your program using run and it should run up to the first breakpoint. Then use continue to jump to the second breakpoint you set which should be the for loop. At any point, you can use continue to execute up to the next breakpoint. If you tried it again now, it should jump to the second pass through the loop. + +Instead, use next to step through one line at a time. Watch the value of after[0] carefully. When it changes, print its address. (Yes, you can do ""print &(after[0])"" inside gdb.) Then, print the address of a[4]. Does that help you understand what happened? Exit gdb. (Say 'y' when it asks if you want to Quit anyway.) Then exit your script by typing exit. Make sure the script file you generated is named gdb_example.txt and add it to your repository. + +The last step is to try to make your program crash. (I had to set OVERFLOW to something like 5000 to get it to crash with a Segmentation fault.) Once you've found a value for OVERFLOW that produces a Segmentation fault, run the program in gdb without setting a breakpoint so that it runs automatically until the crash. Then use the backtrace command and the print command to investigate the value for i. Try backtrace full for even more helpful information. You don't need to record what you're doing on this step. We just want you to see the backtrace command." +63c6479f5f004bf4898cf545,2023-02-25T03:21:02.938Z,Help Write Code,v1,,,,"Your first task is to implement parse_file, so that it reads a makefile and constructs a corresponding linked data structure. The necessary structs are defined in pmake.h, and the comments at the top of the file explain what each line of a makefile will contain. You may not change the struct definitions. pmake.h also contains function prototypes for functions that are either already provided, or that you will need to write. + + +The starter code also contains a makefile named handout.mk. (This file and the required source and header files are in the subdirectory handout_example. ) The picture above shows a data structure that could result from parsing this file. Notice that there is one struct rule_node for each rule in the original makefile and an additional struct rule_node for each dependency that is not also a target. You must follow this design. You are are also required to have the head of the list be the first rule in the original makefile. However, depending on how you do your parsing, the struct rule_node elements may come in a different order in your linked list of rules. + +Before reading any more of this handout, spend time to make sure you understand this figure and how it connects to the original makefile and the structs defined in pmake.h. + +You will see that the actions are stored in a struct action_node that has a member args. This array has the format required by the second parameter to execvp. Use the man page for execvp to understand this format. In particular, notice that the first element is the executable name, subsequent elements are the arguments for that executable, and these are followed by an extra NULL pointer element." +63c6479f5f004bf4898cf545,2023-02-25T03:42:43.884Z,General Question,v1,how do you read from an open file passed into as a parameter,,, +63c6479f5f004bf4898cf545,2023-02-26T21:18:46.266Z,General Question,v1,when do we dereference variables?,,, +63c6479f5f004bf4898cf545,2023-02-26T21:19:36.693Z,Question from Code,v1,"in this code, why do we put a * in front of pt on line 5?","int num = 40; +int *pt; +pt = # +int *anotherpt = # +*pt = 30; +printf(""%d\n"", *anotherpt); +return 0;",, +63c6479f5f004bf4898cf545,2023-02-26T21:20:18.229Z,Question from Code,v1,what if we did pt = 30? does that work and why?,"int num = 40; +int *pt; +pt = # +int *anotherpt = # +*pt = 30; +printf(""%d\n"", *anotherpt); +return 0;",, +63c6479f5f004bf4898cf545,2023-02-26T21:20:40.509Z,General Question,v1,when do we use &?,,, +63c6479f5f004bf4898cf545,2023-02-26T22:20:43.472Z,Question from Code,v1,what would be the value of x[0],"char *result[2]; +x = result[0]; +// some hidden code +result[0] = ""read only""; +y = x[0];",, +63c6479f5f004bf4898cf545,2023-02-26T22:58:55.322Z,Question from Code,v1,why dont we pass in &arr to function change,"nt arr[3] = {1, 2, 3}; + double result = change(arr, 3); + // Comment the above, and uncomment the below, to see + // change only work on the 2nd and 3rd integers + // double result = change(arr + 1, 2); + printf(""The array is now: ""); + for(int i = 0; i < 3; i++) { + printf(""%d "", arr[i]); + } + printf(""\nAnd the average is %lf\n"", result);",, +63c6479f5f004bf4898cf545,2023-02-27T01:40:25.160Z,Question from Code,v1,whats wrong with this code,"int *mkarray(int a, int b, int c) { +int arr[3]; +arr[0] = a; +arr[1] = b; +arr[2] = c; +int *p = arr; +return p; +} +// Code for other_function() omitted. +int main() { +int *ptr = mkarray(10, 20, 30); +other_function(); +printf(""%d %d %d\n"", ptr[0], ptr[1], ptr[2]); +}",, +63c6479f5f004bf4898cf545,2023-02-27T01:44:48.421Z,General Question,v1,when should we be allocating heap space,,, +63c6479f5f004bf4898cf545,2023-02-27T01:46:10.972Z,Question from Code,v1,why doesnt this need malloc,"double change(int *a, int size) { + double sum = 0.0; + for(int i = 0; i < size; i++) { + a[i] += 10; + sum += a[i]; + } + return sum / size; // assume size > 0 +} +int main() { + int arr[3] = {1, 2, 3}; + double result = change(arr, 3); + // Comment the above, and uncomment the below, to see + // change only work on the 2nd and 3rd integers + // double result = change(arr + 1, 2); + printf(""The array is now: ""); + for(int i = 0; i < 3; i++) { + printf(""%d "", arr[i]); + } + printf(""\nAnd the average is %lf\n"", result); + return 0; +}",, +63c6479f5f004bf4898cf545,2023-02-27T01:50:22.758Z,General Question,v1,So is it true that we only need to allocate space on the heap when variables with data that we want to persist throughout the program are created in the helper functions?,,, +63c6479f5f004bf4898cf545,2023-02-28T05:54:04.047Z,Question from Code,v1,"if car.color had pointed to &color_ptr instead, would it change to Blue?","struct Car car; +char *color_ptr = ""Green""; +car.color = color_ptr; +car.mileage = 1000; +color_ptr = ""Blue""; +struct Car *car_ptr = &car; +",, +63c6479f5f004bf4898cf545,2023-02-28T05:54:31.474Z,Question from Code,v1,is there a way to change car.color to blue,"struct Car car; +char *color_ptr = ""Green""; +car.color = color_ptr; +car.mileage = 1000; +color_ptr = ""Blue""; +struct Car *car_ptr = &car; +",, +63c6479f5f004bf4898cf545,2023-03-02T01:56:23.981Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol (argv[1], NULL, 10); + + int is_parent = 0; + + for (int i = 0; i < iterations; i++) { + if (is_parent == 0) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + if (n == 0) { + is_parent = 0; + } + + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + return 0; +}","modify the program so that the new children do not create additional processes. Only the original parent calls fork. Keep the printf call for all processes. The resulting diagram will look something like the following when parentcreates 3 is run. In this case, the parent process creates 3 child processes. Note that the child process ids will not necessarily be in sequence.", +63c6479f5f004bf4898cf545,2023-03-02T01:59:01.092Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol (argv[1], NULL, 10); + + int is_parent = 0; + + for (int i = 0; i < iterations; i++) { + if (is_parent == 0) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + if (n == 0) { + is_parent = 1; + } + + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + return 0; +}"," modify the program so that the new children do not create additional processes. Only the original parent calls fork. Keep the printf call for all processes. The resulting diagram will look something like the following when parentcreates 3 is run. In this case, the parent process creates 3 child processes. Note that the child process ids will not necessarily be in sequence.", +63c6479f5f004bf4898cf545,2023-03-02T03:08:06.741Z,Help Fix Code,v1,," if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + if (getppid() == getpid()) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}","Create a copy of forkloop.c called childcreates.c. In the new file, modify the program so that each process creates exactly one a new process. Keep the printf call for all processes.", +63c6479f5f004bf4898cf545,2023-03-02T03:46:37.302Z,General Question,v1,"how can i read a file line by line if i dont know the line length, only the max line length?",,, +63c6479f5f004bf4898cf545,2023-03-03T00:23:53.799Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + int child_count = 0; + + for (int i = 0; i < iterations; i++) { + if (child_count == i) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + else if (n == 0) { + child_count++; + } + } + + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",each process creates exactly one new process, +63c6479f5f004bf4898cf545,2023-03-09T22:15:40.372Z,Help Write Code,v2,,,,"Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful." +63c6479f5f004bf4898cf545,2023-03-09T22:59:43.388Z,General Question,v2,how do you call wait ,,, +63c6479f5f004bf4898cf545,2023-03-09T23:07:44.392Z,General Question,v2,how do i perform write on a newline ,,, +63c6479f5f004bf4898cf545,2023-03-09T23:17:16.323Z,Question from Code,v2,how can i get rid of the error," write(fd[1], '\n', sizeof(char)); // Write the newline",, +63c6479f5f004bf4898cf545,2023-03-09T23:18:13.108Z,Question from Code,v2,how can i get rid of the error," execl(""./validate"", ""validate"", user_id, password);",, +63c6479f5f004bf4898cf545,2023-03-09T23:42:20.490Z,Question from Code,v2,why doesn't it properly check if the input is too long," if (write(fd[1], password, MAXLINE) == -1) { // Write the password + perror(""write to pipe""); + } + + if ((close(fd[1])) == -1) { // Close the write + perror(""close""); + } + + wait(&status); // wait + + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { // If it is equal to 0 + printf(SUCCESS); + } else { + printf(INVALID); + } + + } else if (r == 0) { // If child process + if ((close(fd[1])) == -1) { // Close the write + perror(""close""); + } + + if ((dup2(fd[0], fileno(stdin))) == -1) { // So it can read from pipe + perror(""dup2""); + exit(1); + } + + execl(""./validate"", ""validate"", user_id, password, NULL); // Validate + + } else { + perror(""fork""); // Check for failed fork + exit(1); + } + return 0; + + } +} +",, +63c6479f5f004bf4898cf545,2023-03-09T23:44:45.464Z,Help Fix Code,v2,"Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful."," if (write(fd[1], password, MAXLINE) == -1) { // Write the password + perror(""write to pipe""); + } + + if ((close(fd[1])) == -1) { // Close the write + perror(""close""); + } + + wait(&status); // wait + + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { // If it is equal to 0 + printf(SUCCESS); + } else { + printf(INVALID); + } + + } else if (r == 0) { // If child process + if ((close(fd[1])) == -1) { // Close the write + perror(""close""); + } + + if ((dup2(fd[0], fileno(stdin))) == -1) { // So it can read from pipe + perror(""dup2""); + exit(1); + } + + execl(""./validate"", ""validate"", user_id, password, NULL); // Validate + + } else { + perror(""fork""); // Check for failed fork + exit(1); + } + return 0; + + } +} +",, +63c6479f5f004bf4898cf545,2023-03-09T23:54:14.905Z,Question from Code,v2,would this code snippet correctly check for the status ," wait(&status); // wait + + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { // If it is equal to 0 + printf(SUCCESS); + } else { + printf(INVALID); + }",, +63c6479f5f004bf4898cf545,2023-03-10T03:33:45.392Z,Question from Code,v2,what is wrong with my code,"int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + // Check for exceeded length + if (strlen(user_id) > 10 || strlen(password) > 10) { + printf(NO_USER); + } else { + int fd[2]; + int r; + + // Create the pipe + if ((pipe(fd) == -1)) { + perror(""pipe""); + exit(1); + } + + if ((r = fork()) > 0) { // If parent process + int status; + + if ((close(fd[0])) == -1) { // Close the read + perror(""close""); + } + + if (write(fd[1], user_id, strlen(user_id) + 1) == -1) { // Write the user id + perror(""write to pipe""); + } + write(fd[1], ""\n"", 1); // Write the newline + + if (write(fd[1], password, strlen(password) + 1) == -1) { // Write the password + perror(""write to pipe""); + } + + if ((close(fd[1])) == -1) { // Close the write + perror(""close""); + } + + wait(&status); // wait + printf(""%d"", WEXITSTATUS(status)); + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { // If it is equal to 0 + printf(SUCCESS); + } else { + printf(INVALID); + } + + } else if (r == 0) { // If child process + if ((close(fd[1])) == -1) { // Close the write + perror(""close""); + } + + if ((dup2(fd[0], fileno(stdin))) == -1) { // So it can read from pipe + perror(""dup2""); + exit(1); + } + + if ((close(fd[0])) == -1) { + perror(""close""); + } + + execl(""./validate"", ""validate"", user_id, password, NULL); // Validate + + + } else { + perror(""fork""); // Check for failed fork + exit(1); + } + } + return 0; +} +",, +63c6479f5f004bf4898cf545,2023-03-10T03:37:42.217Z,Help Write Code,v2,,,,"Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful." +63c6479f5f004bf4898cf545,2023-03-10T13:24:07.689Z,Question from Code,v2,why do i get the wrong output,"int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + // Check for exceeded length + if (strlen(user_id) > 10 || strlen(password) > 10) { + printf(NO_USER); + exit(0); + } else { + int fd[2]; + int r; + + // Create the pipe + if ((pipe(fd) == -1)) { + perror(""pipe""); + exit(1); + } + + if ((r = fork()) > 0) { // If parent process + if ((close(fd[0])) == -1) { // Close the read + perror(""close""); + } + if (write(fd[1], user_id, MAXLINE) == -1) { // Write the user id + perror(""write to pipe""); + } + + if (write(fd[1], password, MAXLINE) == -1) { // Write the password + perror(""write to pipe""); + } + + if ((close(fd[1])) == -1) { // Close the write + perror(""close""); + } + + int status; + wait(&status); // wait + + + + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { // If it is equal to 0 + printf(SUCCESS); + } else if(WIFEXITED(status)) { + printf(INVALID); + } + + } else if (r == 0) { // If child process + if ((close(fd[1])) == -1) { // Close the write + perror(""close""); + } + + if (dup2(fd[0], fileno(stdin)) == -1) { // So it can read from pipe + perror(""dup2""); + exit(1); + } + + if ((close(fd[0])) == -1) { + perror(""close""); + } + + execl(""./validate"", ""validate"", NULL); // Validate + + } else { + perror(""fork""); // Check for failed fork + exit(1); + } + } + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-03-10T18:39:48.585Z,Question from Code,v2,is this right," +if ((r = fork()) > 0) { // If parent process + if ((close(fd[0])) == -1) { // Close the read + perror(""close""); + } + + if (write(fd[1], user_id, MAXLINE) == -1) { // Write the user id + perror(""write to pipe""); + } + + if (write(fd[1], password, MAXLINE) == -1) { // Write the password + perror(""write to pipe""); + } + + if ((close(fd[1])) == -1) { // Close the write + perror(""close""); + } + + int status; + wait(&status); // wait + + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { // If it is equal to 0 + printf(SUCCESS); + } else if (WIFEXITED(status)) { + printf(INVALID); + } +",, +63c6479f5f004bf4898cf545,2023-03-10T18:40:56.756Z,Question from Code,v2,why is user_id properly written to the pipe but password is not," +if ((r = fork()) > 0) { // If parent process + if ((close(fd[0])) == -1) { // Close the read + perror(""close""); + } + + if (write(fd[1], user_id, MAXLINE) == -1) { // Write the user id + perror(""write to pipe""); + } + + if (write(fd[1], password, MAXLINE) == -1) { // Write the password + perror(""write to pipe""); + } + + if ((close(fd[1])) == -1) { // Close the write + perror(""close""); + } + + int status; + wait(&status); // wait + + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { // If it is equal to 0 + printf(SUCCESS); + } else if (WIFEXITED(status)) { + printf(INVALID); + } +",, +63c6479f5f004bf4898cf545,2023-03-10T18:43:05.797Z,Question from Code,v2,would this code properly read 2 things that were written to it,"} +else if (r == 0) { // If child process + if ((dup2(fd[0], fileno(stdin))) == -1) { // So it can read from pipe + perror(""dup2""); + exit(1); + } + + if ((close(fd[1])) == -1) { + perror(""close""); + } + + if ((close(fd[0])) == -1) { + perror(""close""); + } + + execl(""./validate"", ""validate"", NULL); // Validate +} +else { + perror(""fork""); // Check for failed fork + exit(1); +} +}",, +63c6479f5f004bf4898cf545,2023-03-11T02:09:04.952Z,General Question,v2,how can i remove newline characters after calling fgets,,, +63c6479f5f004bf4898cf545,2023-03-11T02:23:25.100Z,Question from Code,v2,are these functions correct,"Rule *find_rule(char *target, Rule *head) { + if (head == NULL) { // If linked list is empty, return NULL + return NULL; + } + + // Traverse the linked list + Rule *curr = head; + while ((curr != NULL) && (strcmp(curr->target, target) != 0)) { + curr = curr->next_rule; + } + + return curr; // Return curr if found, NULL if not found +} + +Rule *create_rule(char *target) { + Rule *new_rule = malloc(sizeof(Rule)); // Allocate space for a rule + if (new_rule == NULL) { // Error checking + perror(""malloc""); + exit(1); + } + + strcpy(new_rule->target, target); // Set the target + new_rule->next_rule = NULL; // Set the next_rule + + return new_rule; // Return the new rule +}",, +63c6479f5f004bf4898cf545,2023-03-11T02:33:58.815Z,Question from Code,v2,why am i getting a segfault when create_rule is called,"Rule *parse_file(FILE *fp) { + char line[MAXLINE]; + Rule *curr_rule = NULL; + Rule *head_rule = NULL; + int num_actions = 0; + + // Iterate through each line of the makefile + while (fgets(line, sizeof(line), fp) != NULL) { + // Process newline characters + line[strcspn(line, ""\r"")] = '\0'; + line[strcspn(line, ""\n"")] = '\0'; + + if ((line[0] != ' ') && (line[0] != '\t') && (line[0] != '#')) { // If is action line + char *tok_line = strtok(line, "" ""); // Tokenize the line + num_actions = 0; // Initialize the number of actions (back to) 0 + + if (find_rule(tok_line, head_rule) == NULL) { // If rule doesn't exist + curr_rule = create_rule(tok_line); // Make a rule, set it to curr + } else { + curr_rule = find_rule(tok_line, head_rule); // Find the rule, set it to curr + } + + while ((strcmp(tok_line, "":"")) && (tok_line != NULL)) { // Iterate thru dependencies + tok_line = strtok(NULL, "" ""); + Dependency *new_dep = malloc(sizeof(Dependency)); // Dynamically allocate space for a dependency + + // Find or create a rule and assign it to the new dependency + if (find_rule(tok_line, head_rule) == NULL) { + new_dep->rule = create_rule(tok_line); + } else { + new_dep->rule = find_rule(tok_line, head_rule); + } + new_dep->next_dep = NULL; // Initialize next_dep to NULL + + // Add it to curr's list of dependencies + if (curr_rule->dependencies == NULL) { + curr_rule->dependencies = new_dep; + } else { + Dependency *curr_dep = curr_rule->dependencies; + while (curr_dep->next_dep != NULL) { + curr_dep = curr_dep->next_dep; + } + curr_dep->next_dep = new_dep; + } + } + + // Add the rule to the linked list + if (head_rule == NULL) { + head_rule = curr_rule; + } else { + Rule *curr = head_rule; + while (curr->next_rule != NULL) { + curr = curr->next_rule; + } + curr->next_rule = curr_rule; + } +",, +63c6479f5f004bf4898cf545,2023-03-11T02:47:58.704Z,Question from Code,v2,do you know if this code is properly ignoring blank lines in the file,"while (fgets(line, sizeof(line), fp) != NULL) { + // Process newline characters + line[strcspn(line, ""\r"")] = '\0'; + line[strcspn(line, ""\n"")] = '\0'; + + if ((line[0] != ' ') && (line[0] != '\t') && (line[0] != '#')) { // If is action line + char *tok_line = strtok(line, "" ""); // Tokenize the line + num_actions = 0; // Initialize the number of actions (back to) 0 + + if (find_rule(tok_line, head_rule) == NULL) { // If rule doesn't exist + curr_rule = create_rule(tok_line); // Make a rule, set it to curr + } else { + curr_rule = find_rule(tok_line, head_rule); // Find the rule, set it to curr + } + + while ((strcmp(tok_line, "":"")) && (tok_line != NULL)) { // Iterate thru dependencies + tok_line = strtok(NULL, "" ""); + Dependency *new_dep = malloc(sizeof(Dependency)); // Dynamically allocate space for a dependency + + // Find or create a rule and assign it to the new dependency + if (find_rule(tok_line, head_rule) == NULL) { + new_dep->rule = create_rule(tok_line); + } else { + new_dep->rule = find_rule(tok_line, head_rule); + } + new_dep->next_dep = NULL; // Initialize next_dep to NULL + + // Add it to curr's list of dependencies + if (curr_rule->dependencies == NULL) { + curr_rule->dependencies = new_dep; + } else { + Dependency *curr_dep = curr_rule->dependencies;",, +63c6479f5f004bf4898cf545,2023-03-11T02:51:32.276Z,Question from Code,v2,why is there a segfault when this function is called by the program,"Rule *create_rule(char *target) { + Rule *new_rule = malloc(sizeof(Rule)); // Allocate space for a rule + if (new_rule == NULL) { // Error checking + perror(""malloc""); + exit(1); + } + + strcpy(new_rule->target, target); // Set the target + new_rule->next_rule = NULL; // Set the next_rule + new_rule->dependencies = NULL; + new_rule->actions = NULL; + return new_rule; // Return the new rule +}",, +63c6479f5f004bf4898cf545,2023-03-11T22:20:29.482Z,General Question,v2,what can i use to make a copy of a string,,, +63c6479f5f004bf4898cf545,2023-03-11T22:26:51.969Z,Question from Code,v2,what does this code do,"if (line[0] == '\0') { + continue; +}",, +63c6479f5f004bf4898cf545,2023-03-11T22:44:08.013Z,Help Write Code,v2,,,," +A rule is evaluated using the following steps: + +Update each of the dependencies. In other words, recursively evaluate each dependency rule. +Compare the last modified time for each of the dependencies to the target. +If the target does not exist, or at least one of the dependencies refers to a file with a last modified time more recent than the target, execute the rule's actions." +63c6479f5f004bf4898cf545,2023-03-11T22:44:32.635Z,Help Write Code,v2,,,,"A rule is evaluated using the following steps: Update each of the dependencies. In other words, recursively evaluate each dependency rule. Compare the last modified time for each of the dependencies to the target. If the target does not exist, or at least one of the dependencies refers to a file with a last modified time more recent than the target, execute the rule's actions." +63c6479f5f004bf4898cf545,2023-03-11T22:46:06.213Z,Help Write Code,v2,,,,"Update each of the dependencies. In other words, recursively evaluate each dependency rule. + +Compare the last modified time for each of the dependencies to the target. + +If the target does not exist, or at least one of the dependencies refers to a file with a last modified time more recent than the target, execute the rule's actions." +63c6479f5f004bf4898cf545,2023-03-12T02:25:35.461Z,General Question,v2,how do you use stat with examples,,, +63c6479f5f004bf4898cf545,2023-03-12T03:24:44.549Z,General Question,v2,"if im writing a make system, can i: + +-code a while loop to iterate through the dependencies and recursively call the program on them +-after the while loop is finished, check if target exists. if not, execute the rule's' actions +-if it does exist, create a struct using stat to hold the modification time of the target rule. loop through its dependencies and check for their modification times. if the modification time of the dependencies are more recent than the target, execute the action",,, +63c6479f5f004bf4898cf545,2023-03-12T05:41:01.770Z,General Question,v2,how do you know if If the child terminates with a non-zero exit status,,, +63c6479f5f004bf4898cf545,2023-03-12T05:42:09.250Z,General Question,v2,how do you know if If the child terminates with a non-zero exit status after using execvp,,, +63c6479f5f004bf4898cf545,2023-03-12T05:46:02.561Z,General Question,v2,how do you print out multiple strings in an array into one line ,,, +63c6479f5f004bf4898cf545,2023-03-12T05:46:41.771Z,General Question,v2,how do i concatenate many strings together,,, +63c6479f5f004bf4898cf545,2023-03-12T05:50:52.316Z,General Question,v2,how to initiliaze a string array,,, +63c6479f5f004bf4898cf545,2023-03-12T16:24:58.742Z,General Question,v2,"can you explain this to me: + +When pmake is run with the -p option, a child should be created to update each dependency. The parent will create one child process for each dependency and after it has created all of them, the parent will wait for all children to terminate successfully before determining whether it needs to execute the actions.",,, +63c6479f5f004bf4898cf545,2023-03-12T16:33:19.272Z,General Question,v2,how do i check if a file/target exists ,,, +63c6479f5f004bf4898cf545,2023-03-12T16:42:02.341Z,Help Write Code,v2,,,,"When pmake is run with the -p option, a child should be created to update each dependency. The parent will create one child process for each dependency and after it has created all of them, the parent will wait for all children to terminate successfully before determining whether it needs to execute the actions." +63c6479f5f004bf4898cf545,2023-03-12T23:27:31.391Z,General Question,v2,"can i do this to parallelize a makefile: + +1. iterate through the dependencies, and at each dependency, fork the process. If its a child process, recursively call the program, then exit. if it is a parent, then wait. + +if so, what steps come next?",,, +63c6479f5f004bf4898cf545,2023-03-12T23:41:16.560Z,Help Fix Code,v2,"find the rule in the rules data structure corresponding to the target, and evaluate it. If target is NULL, then run_make will evaluate the first target in the rules list. + +this code will determine if the action of the rule in the makefile should be executed"," Rule *curr = rule; + while (curr->dependencies != NULL) { // Iterate through the linked list of dependencies + run_make(curr->target, rules, 0); // Recursive call to run_make + curr->dependencies = curr->dependencies->next_dep; + } + + int execute = 1; // Create a variable to track if action should be executed + + // Check if target exists + // access function suggested by Coding Assistant + if (access(target, F_OK) != 0) { // If target does not exist + execute = 0; // Update variable so action will be executed + } else { + struct stat rule_time; // Create a struct to hold the time + if (stat(rule->target, rule_time) != 0) { // Error Checking + perror(""stat""); + exit(1); + } + + // Iterate through the dependencies + Dependency *curr_dep = rule->dependencies; + while (curr_dep != NULL) { + struct stat dep_time; + if (stat(curr_dep->rule->target, dep_time) != 0) { // Error Checking + perror(""stat""); + exit(1); + } + if ((rule_time.st_mtim.tv_sec < dep_time.st_mtim.tvsec) // If dependency was modified more recently + || ((rule_time.st_mtim.tv_sec == dep_time.st_mtim.tv_sec) + && (rule_time.st_mtim.tv_nsec < rule_time.st_mtim.tv_nsec))) { + execute = 0; // Update variable so action will be executed + } + } + } + + ",, +63c6479f5f004bf4898cf545,2023-03-13T19:24:18.956Z,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 ""); + // POSITION A + struct sigaction act; + act.sa_handler = handler; + act.sa_flags = 0; + sigemptyset(&act.sa_mask); + sigaction(SIGINT, &act, NULL); + + // POSITION B + x += 2; + + // POSITION C + fprintf(stderr, ""outside %d"", x); + + return 0; +}",, +63c6479f5f004bf4898cf545,2023-03-13T20:28:12.118Z,General Question,v2,"if im writing a program that executes makefiles, when executing multiple actions, do i need to loop through the actions and then fork once per action?",,, +63c6479f5f004bf4898cf545,2023-03-14T03:33:58.092Z,Help Fix Code,v2,run make in parallel," Dependency *curr_dep = rule->dependencies; + int num_dep = 0; + + while (curr_dep != NULL) { + num_dep++; + curr_dep = curr_dep->next_dep; + } + + int child_pid_list[num_dep]; // Create a list to store the pid's of child processes + int child_count = 0; // Create a variable to hold the number of children + + curr_dep = rule->dependencies; // Re-initialize curr_dep + + // Iterate through the dependencies and fork new processes for each + while (curr_dep != NULL) { + int pid = fork(); // Fork a new process + + if (pid > 0) { // If is parent process + child_pid_list[child_count] = pid; // Add to the list of pids + child_count++; + } else if (pid == 0) { // If is child process + run_make(curr_dep->rule->target, rules, 1); // Update the dependency + exit(0); // Exit + } else { // If fork fails + perror(""fork""); + exit(1); + } + + curr_dep = curr_dep->next_dep; + } + + int status; + + // Check if all processes exited successfully + for (int i = 0; i < child_count; i++) { + waitpid(child_pid_list[i], &status, 0); + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { // If non-zero exit status + exit(1); + } + } + execute = 0; // Update the variable so that the actions are executed + }",, +63c6479f5f004bf4898cf545,2023-03-14T03:46:34.540Z,General Question,v2,how do i put a function prototype in another file,,, +63c6479f5f004bf4898cf545,2023-03-14T03:47:42.406Z,General Question,v2,how do i put a function prototype in another file without a header file,,, +63c6479f5f004bf4898cf545,2023-03-14T03:48:30.942Z,General Question,v2,how do i put a function prototype in another file without a header file and without extern,,, +63c6479f5f004bf4898cf545,2023-03-14T03:48:31.209Z,General Question,v2,how do i put a function prototype in another file without a header file and without extern,,, +63c6479f5f004bf4898cf545,2023-03-15T15:36:49.751Z,Question from Code,v2,does this parallelize make? Note that i later have a for loop that calls waitpid on the list of child_pids,"if (rule == NULL) { // Base case for recursion + return; +} + +// Iterate through dependencies to get the number of dependencies +if (rule->dependencies != NULL) { // Check if dependencies exist + Dependency *curr_dep = rule->dependencies; + int num_dep = 0; + + while (curr_dep != NULL) { + num_dep++; + curr_dep = curr_dep->next_dep; + } + + int child_pid_list[num_dep]; // Create a list to store the pid's of child processes + int child_count = 0; // Create a variable to hold the number of children + + curr_dep = rule->dependencies; // Re-initialize curr_dep + + // Iterate through the dependencies and fork new processes for each + while (curr_dep != NULL) { + int pid = fork(); // Fork a new process + + if (pid > 0) { // If is parent process + child_pid_list[child_count] = pid; // Add to the list of pids + child_count++; + } else if (pid == 0) { // If is child process + if ((access(rule->target, F_OK) != 0) || (dep_last_modified(rule, curr_dep) == 0)) { // If dependency was // last modified or non existent target + run_make(curr_dep->rule->target, rules, 1); // Update the dependency + exit(0); + } + exit(1); + } else { // If fork fails + perror(""fork""); + exit(1); + } + + curr_dep = curr_dep->next_dep; + } + + int status; +",, +63c6479f5f004bf4898cf545,2023-03-15T15:39:18.064Z,Question from Code,v2,is this parallelizeing make?,"if (rule == NULL) { // Base case for recursion + return; +} + +// Iterate through dependencies to get the number of dependencies +if (rule->dependencies != NULL) { // Check if dependencies exist + Dependency *curr_dep = rule->dependencies; + int num_dep = 0; + + while (curr_dep != NULL) { + num_dep++; + curr_dep = curr_dep->next_dep; + } + + int child_pid_list[num_dep]; // Create a list to store the pid's of child processes + int child_count = 0; // Create a variable to hold the number of children + + curr_dep = rule->dependencies; // Re-initialize curr_dep + + // Iterate through the dependencies and fork new processes for each + while (curr_dep != NULL) { + int pid = fork(); // Fork a new process + + if (pid > 0) { // If is parent process + child_pid_list[child_count] = pid; // Add to the list of pids + child_count++; + } else if (pid == 0) { // If is child process + if ((access(rule->target, F_OK) != 0) || (dep_last_modified(rule, curr_dep) == 0)) { // If dependency was // last modified or non existent target + run_make(curr_dep->rule->target, rules, 1); // Update the dependency + exit(0); + } + exit(1); + } else { // If fork fails + perror(""fork""); + exit(1); + } + + curr_dep = curr_dep->next_dep; + } + + int status; + + for (int i = 0; i < child_count; i++) { + waitpid(child_pid_list[i], &status, 0); + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { // If non-zero exit status + exit(1); + } + execute = 0; // Signal to execute. If any if statements do fail, the program would have exited + } +",, +63c6479f5f004bf4898cf545,2023-03-17T13:28:09.856Z,General Question,v2, how do you use sigaction to handle sigprofs,,, +63c6479f5f004bf4898cf545,2023-03-22T18:28:30.044Z,General Question,v2,what does updating inbuf mean,,, +63c6479f5f004bf4898cf545,2023-03-23T21:14:19.117Z,General Question,v2,"what do i need to do here: + +/ You want to move the stuff after the full line to the beginning // of the buffer. A loop can do it, or you can use memmove. // memmove(destination, source, number_of_bytes) ",,, +63c6479f5f004bf4898cf545,2023-03-23T21:27:07.701Z,Question from Code,v2,is this right so far?,"while ((nbytes = read(fd, after, room)) > 0) { + // Step 1: update inbuf (how many bytes were just added?) + inbuf = nbytes; + + int where; + + // Step 2: the loop condition below calls find_network_newline + // to determine if a full line has been read from the client. + // Your next task should be to implement find_network_newline + // (found at the bottom of this file). + // + // Note: we use a loop here because a single read might result in + // more than one full line. + while ((where = find_network_newline(buf, inbuf)) > 0) { + // where is now the index into buf immediately after + // the first network newline + // Step 3: Okay, we have a full line. + // Output the full line, not including the ""\r\n"", + // using print statement below. + // Be sure to put a '\0' in the correct place first; + // otherwise you'll get junk in the output. + buf[where - 2] = '\0'; + + printf(""Next message: %s\n"", buf); + // Note that we could have also used write to avoid having to + // put the '\0' in the buffer. Try using write later! + + // Step 4: update inbuf and remove the full line from the buffer + // There might be stuff after the line, so don't just do inbuf = 0. + + // You want to move the stuff after the full line to the beginning + // of the buffer. A loop can do it, or you can use memmove. + // memmove(destination, source, number_of_bytes) + inbuf -= where; + memmove(buf, inbuf, sizeof(buf) - sizeof(inbuf)); + }",, +63c6479f5f004bf4898cf545,2023-03-23T22:00:30.161Z,Question from Code,v2,is this good?,"int main() { + // This line causes stdout not to be buffered. + // Don't change this! Necessary for autotesting. + setbuf(stdout, NULL); + + struct sockaddr_in *self = init_server_addr(PORT); + int listenfd = set_up_server_socket(self, 5); + + while (1) { + int fd = accept_connection(listenfd); + if (fd < 0) { + continue; + } + + // Receive messages + char buf[BUFSIZE] = {'\0'}; + int inbuf = 0; // How many bytes currently in buffer? + int room = sizeof(buf); // How many bytes remaining in buffer? + char *after = buf; // Pointer to position after the data in buf + + int nbytes; + while ((nbytes = read(fd, after, room)) > 0) { + // Step 1: update inbuf (how many bytes were just added?) + inbuf = nbytes; + + int where; + + // Step 2: the loop condition below calls find_network_newline + // to determine if a full line has been read from the client. + // Your next task should be to implement find_network_newline + // (found at the bottom of this file). + // + // Note: we use a loop here because a single read might result in + // more than one full line. + while ((where = find_network_newline(buf, inbuf)) > 0) { + buf[where - 2] = '\0'; + + printf(""Next message: %s\n"", buf); + inbuf -= where; + memmove(buf, buf + where, sizeof(buf) - where); + } + // Step 5: update after and room, in preparation for the next read. + after = buf; + room = sizeof(buf) - find_network_newline(buf, inbuf); + } + close(fd); + printf(""The connection is now closed ...\n""); + } + + free(self); + close(listenfd); + return 0; +} + +int find_network_newline(const char *buf, int n) { + for (int i = 0; i < n - 1; i++) { + if ((buf[i] == '\r') && (buf[i + 1] == '\n')) { + return i + 2; + } + } + + return -1; +}",, +63c6479f5f004bf4898cf545,2023-03-23T22:25:13.783Z,Question from Code,v2,did i correctly update inbuf to the correct value,"char buf[BUFSIZE] = {'\0'}; +int inbuf = 0; // How many bytes currently in buffer? +int room = sizeof(buf); // How many bytes remaining in buffer? +char *after = buf; // Pointer to position after the data in buf + +int nbytes; +while ((nbytes = read(fd, after, room)) > 0) { + // Step 1: update inbuf (how many bytes were just added?) + inbuf = nbytes; + + int where;",, +63c6479f5f004bf4898cf545,2023-03-23T22:26:01.204Z,Question from Code,v2,is the correct message being printed,"while ((where = find_network_newline(buf, inbuf)) > 0) { + // where is now the index into buf immediately after + // the first network newline + // Step 3: Okay, we have a full line. + // Output the full line, not including the ""\r\n"", + // using print statement below. + // Be sure to put a '\0' in the correct place first; + // otherwise you'll get junk in the output. + buf[where - 2] = '\0'; + + printf(""Next message: %s\n"", buf);",, +63c6479f5f004bf4898cf545,2023-03-23T22:27:57.541Z,Question from Code,v2,is the stuff after the full line moved to the beginning of the buffer and is after and room being correctly updated,"// Note that we could have also used write to avoid having to +// put the '\0' in the buffer. Try using write later! + +// Step 4: update inbuf and remove the full line from the buffer +// There might be stuff after the line, so don't just do inbuf = 0. + +// You want to move the stuff after the full line to the beginning +// of the buffer. A loop can do it, or you can use memmove. +// memmove(destination, source, number_of_bytes) +inbuf -= where; +memmove(buf, buf + where, sizeof(buf) - where); +} +// Step 5: update after and room, in preparation for the next read. +after = buf; +room = sizeof(buf) - find_network_newline(buf, inbuf); +} +close(fd); +printf(""The connection is now closed ...\n""); +} + +free(self); +close(listenfd); +return 0; +}",, +63c6479f5f004bf4898cf545,2023-03-23T22:58:31.677Z,Help Fix Code,v2,"you have a buffer (a character array) where you store pieces of a message until you have a network newline (""\r\n""). Each message piece goes after the data that you've already placed in the buffer. When you find a network newline, you know that you have a complete message, so you can print that message and then shift any remaining bytes to the front of the buffer as the start of the next copy of the message.","int main() { + // This line causes stdout not to be buffered. + // Don't change this! Necessary for autotesting. + setbuf(stdout, NULL); + + struct sockaddr_in *self = init_server_addr(PORT); + int listenfd = set_up_server_socket(self, 5); + + while (1) { + int fd = accept_connection(listenfd); + if (fd < 0) { + continue; + } + + // Receive messages + char buf[BUFSIZE] = {'\1'}; + int inbuf = 0; // How many bytes currently in buffer? + int room = sizeof(buf); // How many bytes remaining in buffer? + char *after = buf; // Pointer to position after the data in buf + + int nbytes; + while ((nbytes = read(fd, after, room)) > 0) { + // Step 1: update inbuf (how many bytes were just added?) + inbuf = nbytes; + + int where; + while ((where = find_network_newline(buf, inbuf)) > 0) { + // where is now the index into buf immediately after + // the first network newline + buf[where + 2] = '\0'; + printf(""Next message: %s\n"", buf); + // Step 4: update inbuf and remove the full line from the buffer + // There might be stuff after the line, so don't just do inbuf = 0. + inbuf -= where; + memmove(buf, buf + where, sizeof(where) + 1); + + } + // Step 5: update after and room, in preparation for the next read. + after = buf; + room = BUFSIZE - inbuf; + } + close(fd); + printf(""The connection is now closed ...\n""); + } + + free(self); + close(listenfd); + return 0; +} + + +/* + * Search the first n characters of buf for a network newline (\r\n). + * Return one plus the index of the '\n' of the first network newline, + * or -1 if no network newline is found. The return value is the index into buf + * where the current line ends. + * + */ +int find_network_newline(const char *buf, int n) { + for (int i = 0; i < n - 1; i++) { + if ((buf[i] == '\r') && (buf[i + 1] == '\n')) { + return i + 2; + } + } + + return -1; +}",, +63c6479f5f004bf4898cf545,2023-03-23T22:59:51.752Z,Question from Code,v2,why arent i getting the right output,"int main() { + // This line causes stdout not to be buffered. + // Don't change this! Necessary for autotesting. + setbuf(stdout, NULL); + + struct sockaddr_in *self = init_server_addr(PORT); + int listenfd = set_up_server_socket(self, 5); + + while (1) { + int fd = accept_connection(listenfd); + if (fd < 0) { + continue; + } + + // Receive messages + char buf[BUFSIZE] = {'\1'}; + int inbuf = 0; // How many bytes currently in buffer? + int room = sizeof(buf); // How many bytes remaining in buffer? + char *after = buf; // Pointer to position after the data in buf + + int nbytes; + while ((nbytes = read(fd, after, room)) > 0) { + // Step 1: update inbuf (how many bytes were just added?) + inbuf = nbytes; + + int where; + while ((where = find_network_newline(buf, inbuf)) > 0) { + // where is now the index into buf immediately after + // the first network newline + buf[where + 2] = '\0'; + printf(""Next message: %s\n"", buf); + // Step 4: update inbuf and remove the full line from the buffer + // There might be stuff after the line, so don't just do inbuf = 0. + inbuf -= where; + memmove(buf, buf + where, sizeof(where) + 1); + } + // Step 5: update after and room, in preparation for the next read. + after = buf; + room = BUFSIZE - inbuf; + } + close(fd); + printf(""The connection is now closed ...\n""); + } + + free(self); + close(listenfd); + return 0; +} + +int find_network_newline(const char *buf, int n) { + for (int i = 0; i < n - 1; i++) { + if ((buf[i] == '\r') && (buf[i + 1] == '\n')) { + return i + 2; + } + } + + return -1; +}",, +63c6479f5f004bf4898cf545,2023-03-23T23:32:14.616Z,Question from Code,v2,which line or lines are giving me the undesired output,"int main() { + // This line causes stdout not to be buffered. + // Don't change this! Necessary for autotesting. + setbuf(stdout, NULL); + + struct sockaddr_in *self = init_server_addr(PORT); + int listenfd = set_up_server_socket(self, 5); + + while (1) { + int fd = accept_connection(listenfd); + if (fd < 0) { + continue; + } + + // Receive messages + char buf[BUFSIZE] = {'\1'}; + int inbuf = 0; // How many bytes currently in buffer? + int room = sizeof(buf); // How many bytes remaining in buffer? + char *after = buf; // Pointer to position after the data in buf + + int nbytes; + while ((nbytes = read(fd, after, room)) > 0) { + // Step 1: update inbuf (how many bytes were just added?) + inbuf += nbytes; + + int where; + + // Step 2: the loop condition below calls find_network_newline + // to determine if a full line has been read from the client. + // Your next task should be to implement find_network_newline + // (found at the bottom of this file). + // + // Note: we use a loop here because a single read might result in + // more than one full line. + while ((where = find_network_newline(buf, inbuf)) > 0) { + // where is now the index into buf immediately after + // the first network newline + // Step 3: Okay, we have a full line. + // Output the full line, not including the ""\r\n"", + // using print statement below. + // Be sure to put a '\0' in the correct place first; + buf[where - 2] = '\0'; + + printf(""Next message: %s\n"", buf); + + inbuf -= where; + ; + memmove(buf, buf + where + 1, inbuf); + } + // Step 5: update after and room, in preparation for the next read. + after = buf; + room = sizeof(buf) - inbuf; + } + close(fd); + printf(""The connection is now closed ...\n""); + } + + free(self); + close(listenfd); + return 0; +} + +int find_network_newline(const char *buf, int n) { + for (int i = 0; i < n - 1; i++) { + if ((buf[i] == '\r') && (buf[i + 1] == '\n')) { + return i + 2; + } + } + + return -1; +}",, +63c6479f5f004bf4898cf545,2023-03-31T21:17:36.028Z,General Question,v2,"how do i do this: + +Change the code to broadcast to all connected clients",,, +63c6479f5f004bf4898cf545,2023-03-31T21:24:41.321Z,General Question,v2,client: connect: Connection refused how to fix,,, +63c6479f5f004bf4898cf545,2023-03-31T21:28:58.340Z,General Question,v2,what steps can i take to fix the error client: connect: Connection refused,,, +63c6479f5f004bf4898cf545,2023-04-05T19:21:59.212Z,Question from Code,v2,how to change the error: initialization discards ‘const’ qualifier from pointer target type error,"char *list_users(const User *curr) { + // Get the length of the returned string + int num_char = 0; + User *curr_user = curr;",, +63c6479f5f004bf4898cf545,2023-04-05T19:29:49.417Z,Question from Code,v2,help me fix this error,"friends.c : 251 : 9 : error : format not a string literal and no format arguments[-Werror = format - security] 251 | num_bytes_written += + snprintf(user_buf + num_bytes_written, num_char - num_bytes_written, print_post(curr)); // Append the post contents",, +63c6479f5f004bf4898cf545,2023-04-06T13:51:02.998Z,Question from Code,v2,"will this handle partial reads, read one full line and then do something with the full line?","char *read(int client_index, struct sockname *users) { + int fd = users[client_index].sock.fd; + char buf[BUFSIZE + 1]; + + // Read user input and handle partial reads + int inbuf = 0; // Number of bytes in buf + int room = sizeof(buf); // How many bytes left in the buf + char *after = buf; // Pointer to the position after the data + + int nbytes; + + int command_read = 1; // Store if a full line has been read + + while ((nbytes = read(fd, after, room)) > 0 || command_read == 1) { + inbuf += nbytes; + int where; + + if ((where = find_network_newline(buf, inbuf)) > 0) { // When full line is read + buf[where - 2] = '\0'; // Null terminate the buffer + + // Update inbuf + inbuf -= where; + + // Move everything after the full + memmove(buf, buf + (sizeof(char) * where), inbuf); + command_read = 0; + } + // Update after and room + after = &buf[inbuf]; + room = BUFSIZE - inbuf; + } + close(fd); + + // Check for recieved username + if (users[client_index].username == NULL) { + users[client_index].username = malloc(sizeof(char) * strlen(buf) + 1); + if (users[client_index].username == NULL) { + perror(""malloc""); + free(users[client_index].username); + exit(1); + } + + // Set the username + strcpy(users[client_index].username, buf); + users[client_index].username[strlen(buf) + 1] = '\0'; + + return NULL; // Return NULL if username was set + } else { // It is a command + int cmd_len = strlen(buf) + 1; + char *command = malloc(sizeof(char) * cmd_len); + if (command == NULL) { + perror(""malloc""); + exit(1); + } + strcpy(command, buf); + command[length] = '\0'; + return command; // Return the command + } +}",, +63c6479f5f004bf4898cf545,2023-04-06T20:28:55.085Z,Question from Code,v2,why is this stuck in an infinite loop,"char *read_input(int fd) { + char buf[INPUT_BUFFER_SIZE + 1]; + + // Read user input and handle partial reads + int inbuf = 0; // Number of bytes in buf + int room = sizeof(buf); // How many bytes left in the buf + char *after = buf; // Pointer to the position after the data + + int nbytes; + + int command_read = 1; // Store if a full line has been read + + while ((nbytes = read(fd, after, room)) > 0 || command_read == 1) { + inbuf += nbytes; + int where; + + if ((where = find_network_newline(buf, inbuf)) > 0) { // When full line is read + buf[where - 2] = '\0'; // Null terminate the buffer + + // Update inbuf + inbuf -= where; + + // Move everything after the full + memmove(buf, buf + (sizeof(char) * where), inbuf); + command_read = 0; + } + // Update after and room + after = &buf[inbuf]; + room = INPUT_BUFFER_SIZE - inbuf; + + // Check for closed connection + if (nbytes == 0) { + close(fd); + return NULL; + } + } + close(fd); + + // Return the input + char *input = malloc(sizeof(char) * (strlen(buf) + 1)); + if (input == NULL) { + perror(""malloc""); + exit(1); + } + strcpy(input, buf); + + input[strlen(buf)] = '\0'; + return input; +}",, +63c6479f5f004bf4898cf545,2023-04-07T00:47:01.713Z,General Question,v2,How do you use EWOULDBLOCK or EAGAIN to make sure read does not block,,, +63c6479f5f004bf4898cf545,2023-04-07T01:38:13.872Z,General Question,v2,do i need to deal with the remaining data in buffer if I'm reading one line doing partial reads,,, +63c6479f5f004bf4898cf545,2023-04-07T01:39:51.103Z,Question from Code,v2,can i delete the code that deals with the remaining data,"// Read user input and handle partial reads +int inbuf = 0; // Number of bytes in buf +int room = sizeof(buf); // How many bytes left in the buf +char *after = buf; // Pointer to the position after the data + +int nbytes; + +while ((nbytes = read(fd, after, room)) > 0) { + inbuf += nbytes; + int where; + + if ((where = find_network_newline(buf, inbuf)) > 0) { // When full line is read + buf[where - 2] = '\0'; // NULL terminate buff + + // Handle remaining data in buffer + int remaining = inbuf - where; + char *rest = malloc(sizeof(char) * (remaining + 1)); + if (rest == NULL) { + perror(""malloc""); + exit(1); + } + + memcpy(rest, buf + where, remaining); + rest[remaining] = '\0'; + + // Return the input + char *input = malloc(sizeof(char) * (strlen(buf) + 1)); + if (input == NULL) { + perror(""malloc""); + exit(1); + } + strcpy(input, buf); + input[strlen(buf)] = '\0'; + + // Free the rest and return + free(rest); + return input; + } +",, +63c6479f5f004bf4898cf545,2023-04-07T01:48:48.794Z,Question from Code,v2,am i passing in the correct user name into process_args,"while (curr_client != NULL) { + if (curr_client->client_soc > -1 && FD_ISSET(curr_client->client_soc, &all_fds)) { + char *input = read_input(curr_client->client_soc); + + if (input == NULL) { // If connection closed + printf(""Client %d disconnected\n"", client_count); + } else if (read_from(client_count, users, input) == 1) { // If input is a username + if (find_user(input, users_list) == NULL) { // Check for existing or new user + // Write a welcome message + char *welcome_msg; + if (curr_client->welcome_status == 0) { + welcome_msg = ""Welcome\r\nEnter commands\r\n> ""; // If first time user + } else { + welcome_msg = ""Welcome back\r\nEnter commands\r\n> ""; // If returning user + } + int write_error = write(curr_client->client_soc, welcome_msg, strlen(welcome_msg)); // Write the message to the client + if (write_error < 0) { // Error Checking + perror(""write""); + exit(1); + } + curr_client->welcome_status = 1; // Update to flag that user recieved a welcome message + } + } else { // If input is a command + // Process the arguments + char *cmd_argv[INPUT_ARG_MAX_NUM]; + int cmd_argc = tokenize(input, cmd_argv, curr_client->client_soc); + + if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &users_list, users[client_count].username, curr_client->client_soc) == -1) { + break; // can only reach if quit command was entered + } + + // Print the prompt arrow + char *prompt_arrow = ""> ""; + int write_error = write(curr_client->client_soc, prompt_arrow, strlen(prompt_arrow)); + if (write_error < 0) { + perror(""write""); + exit(1); + } + } + } + client_count++; // Update the client count + --INSERT--",, +63c6479f5f004bf4898cf545,2023-04-07T01:54:26.837Z,Question from Code,v2,how will the server know which user is using it ,"while (curr_client != NULL) { + if (curr_client->client_soc > -1 && FD_ISSET(curr_client->client_soc, &all_fds)) { + char *input = read_input(curr_client->client_soc); + + if (input == NULL) { // If connection closed + printf(""Client %d disconnected\n"", client_count); + } else if (read_from(client_count, users, input) == 1) { // If input is a username + if (find_user(input, users_list) == NULL) { // Check for existing or new user + // Write a welcome message + char *welcome_msg; + if (curr_client->welcome_status == 0) { + welcome_msg = ""Welcome\r\nEnter commands\r\n> ""; // If first time user + } else { + welcome_msg = ""Welcome back\r\nEnter commands\r\n> ""; // If returning user + } + int write_error = write(curr_client->client_soc, welcome_msg, strlen(welcome_msg)); // Write the message to the client + if (write_error < 0) { // Error Checking + perror(""write""); + exit(1); + } + curr_client->welcome_status = 1; // Update to flag that user recieved a welcome message + } + } else { // If input is a command + // Process the arguments + char *cmd_argv[INPUT_ARG_MAX_NUM]; + int cmd_argc = tokenize(input, cmd_argv, curr_client->client_soc); + + if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &users_list, users[client_count].username, curr_client->client_soc) == -1) { + break; // can only reach if quit command was entered + } + + // Print the prompt arrow + char *prompt_arrow = ""> ""; + int write_error = write(curr_client->client_soc, prompt_arrow, strlen(prompt_arrow)); + if (write_error < 0) { + perror(""write""); + exit(1); + } + } + } + client_count++; // Update the client count + --INSERT--",, +63c6479f5f004bf4898cf545,2023-04-10T23:26:03.572Z,Help Write Code,v2,,,,a function called read_input that handles partial reads and returns 1 full line (newline terminated) of input +63c647a05f004bf4898cf54a,2023-01-23T20:39:05.298Z,General Question,v1,what is a pointer,,, +63c647a05f004bf4898cf54a,2023-01-24T22:09:39.163Z,Help Fix Code,v1,,"void fib(int **arr, int n){ + *arr = malloc(sizeof(int)*(n)); + for (int x = 0; x < n; x++){ + if (x == 0){ + *arr[x] = 0; + } + if (x == 1){ + *arr[x] = 1; + } + }",I want to assign the second value of the array to 1., +63c647a05f004bf4898cf54a,2023-01-24T22:11:25.646Z,Help Fix Code,v1,,"void fib(int **arr, int n){ + *arr = malloc(sizeof(int)*(n)); + for (int x = 0; x < n; x++){ + if (x == 0){ + *arr[x] = 0; + } + if (x == 1){ + *arr[x] = 1; + } + }",I'm getting a cygwin_exception::open_stackdumpfile error, +63c647a05f004bf4898cf54a,2023-01-24T22:16:30.746Z,Explain Code,v1,,"void fib(int **arr, int n){ + *arr = malloc(sizeof(int)*(n)); + for (int x = 0; x < n; x++){ + if (x == 0){ + *arr[x] = 0; + } + else if (x == 1){ + *arr[x] = 1; + } + else { + *arr[x] = *arr[x-1] + *arr[x-2]; + printf(""%d\n"", *arr[x]); + } + } + }",, +63c647a05f004bf4898cf54a,2023-02-10T02:53:55.521Z,General Question,v1,How do i cast a constant struct pointer to a struct pointer?,,, +63c647a05f004bf4898cf54a,2023-02-10T02:56:49.239Z,Question from Code,v1,Am I casting a constant to head?,"User *find_user(const char *name, const User *head) { + + while (head != NULL){ + if (strncmp(name, head->name, MAX_NAME) == 0){ + return (const User*)head; + } + head = head->next; + } + return NULL; +}",, +63c647a05f004bf4898cf54a,2023-02-10T03:05:09.922Z,Question from Code,v1,Why am i getting a segmentation fault,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME){ + return 2; + } + while (*user_ptr_add != NULL){ + if (strncmp(name, (*user_ptr_add)->name, MAX_NAME) == 0){ + return 1; + } + if((*user_ptr_add)->next == NULL){ + break; + } + *user_ptr_add = (*user_ptr_add)->next; + } + User *new_user = malloc(sizeof(User)); + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME-1] = '\0'; + (*user_ptr_add)->next = new_user; + + return 0;",, +63c647a05f004bf4898cf54f,2023-01-19T18:34:07.339Z,Help Fix Code,v1,,"int sum_card(int **score_card, int size) { + // TODO: write the body of sum_card according to its description. + int sum = 0; + for (i=0; i<=size; i++) { + sum += **score_card[i]; + } + return sum; +} +",, +63c647a05f004bf4898cf54f,2023-01-19T18:34:52.537Z,Help Fix Code,v1,,"int sum_card(int **score_card, int size) { + // TODO: write the body of sum_card according to its description. + int sum = 0; + for (i=0; i<=size; i++) { + sum += *score_card[i]; + } + return sum; +} +",, +63c647a05f004bf4898cf54f,2023-01-19T18:38:31.354Z,Help Fix Code,v1,,"int sum_card(int **score_card, int size) { + // TODO: write the body of sum_card according to its description. + int sum = 0; + for (i=0; i<=size; i++) { + sum += *score_card[i]; + } + return sum; +} + + +/* + * NOTE: don't change the main function! + * The command line arguments are a sequence of integers that will be + * used to initialize the array score_card. + * + * Sample usage: + * $ gcc -Wall -std=gnu99 -g -o score_card score_card.c + * $ ./score_card 10 -3 4 + * Sum: 11 + */ +int main(int argc, char **argv) { + int size = argc - 1; + int *score_card[size]; + + for (int i = 0; i < size; i++) { + // NOTE: We haven't covered malloc yet, so don't worry about this line. + score_card[i] = malloc(sizeof(int)); + *(score_card[i]) = strtol(argv[i + 1], NULL, 10); + } + + printf(""Sum: %d\n"", sum_card(score_card, size)); + + return 0; +} +",, +63c647a05f004bf4898cf54f,2023-01-19T19:49:23.743Z,Help Fix Code,v1,,"int sum_card(int **score_card, int size) { + // TODO: write the body of sum_card according to its description. + int sum = 0; + for (int i=0; i<=size; i++) { + sum += *score_card[i]; + } + return sum; +} + + +/* + * NOTE: don't change the main function! + * The command line arguments are a sequence of integers that will be + * used to initialize the array score_card. + * + * Sample usage: + * $ gcc -Wall -std=gnu99 -g -o score_card score_card.c + * $ ./score_card 10 -3 4 + * Sum: 11 + */ +int main(int argc, char **argv) { + int size = argc - 1; + int *score_card[size]; + + for (int i = 0; i < size; i++) { + // NOTE: We haven't covered malloc yet, so don't worry about this line. + score_card[i] = malloc(sizeof(int)); + *(score_card[i]) = strtol(argv[i + 1], NULL, 10); + } + + printf(""Sum: %d\n"", sum_card(score_card, size)); + + return 0; +} +",, +63c647a05f004bf4898cf54f,2023-01-19T20:58:57.094Z,Explain Code,v1,,"int main(int argc, char **argv) { + int size = argc - 1; + int *score_card[size]; + + for (int i = 0; i < size; i++) { + // NOTE: We haven't covered malloc yet, so don't worry about this line. + score_card[i] = malloc(sizeof(int)); + *(score_card[i]) = strtol(argv[i + 1], NULL, 10); + } + + printf(""Sum: %d\n"", sum_card(score_card, size)); + + return 0; +}",, +63c647a05f004bf4898cf54f,2023-01-19T21:39:25.965Z,Explain Code,v1,,"int sum_card(int **score_card, int size) { + // TODO: write the body of sum_card according to its description. + int sum = 0; + for (int i=0; i<=size; i++) { + sum += score_card[i]; + } + return sum; +}",, +63c647a35f004bf4898cf568,2023-01-31T04:06:04.090Z,Question from Code,v1,"Is this a valid way to truncate a string +","strncpy(s, s, n);",, +63c647a35f004bf4898cf568,2023-02-13T21:55:52.254Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + typedef struct user { + char name[32]; + char profile_pic[32]; + struct post *first_port; + struct user *friends[10]; + struct user *next; + } User; + User this_user; + this_user.next = NULL; + + /* Need to check if name can be fully copied over. */ + if (strlen(name) > sizeof(this_user.name) - 1) { + return 2;} + + /* Case 1: I am the first user. user_ptr_add is NULL? the linked list is empty. */ + if (*user_ptr_add == NULL) { + strncpy(this_user.name, name, sizeof(this_user.name)); + this_user.name[31] = '\0'; + *user_ptr_add = &this_user; + return 0; + } + /* Case 2: linked list already contains users. */ + User *curr = *user_ptr_add; + /* Find the last node <- its next is NULL. */ + while (curr->next != NULL) { + /* Check for duplicate names. */ + if (strcmp(curr->name, name) == 0) { + return 1; + } curr = curr->next; + } curr->next = &this_user; + strncpy(this_user.name, name, sizeof(this_user.name)); + this_user.name[31] = '\0'; + + return -1; +}","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) + ", +63c647a35f004bf4898cf568,2023-02-13T22:06:18.020Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *this_user = malloc(sizeof(User)); + this_user->next = NULL; + + /* Need to check if name can be fully copied over. */ + if (strlen(name) > sizeof(this_user->name) - 1) { + return 2;} + + /* Case 1: I am the first user. user_ptr_add is NULL? the linked list is empty. */ + if (*user_ptr_add == NULL) { + strncpy(this_user->name, name, sizeof(this_user->name)); + this_user->name[31] = '\0'; + *user_ptr_add = this_user; + return 0; + } + /* Case 2: linked list already contains users. */ + User *curr = *user_ptr_add; + /* Find the last node <- its next is NULL. */ + while (curr->next != NULL) { + /* Check for duplicate names. */ + if (strcmp(curr->name, name) == 0) { + return 1; + } curr = curr->next; + } curr->next = this_user; + strncpy(this_user->name, name, sizeof(this_user->name)); + this_user->name[31] = '\0'; + + return -1; +}",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), +63c647a35f004bf4898cf568,2023-02-13T22:07:35.683Z,Question from Code,v1,"I got an error: Undefined symbols for architecture arm64: + ""_main"", referenced from: + implicit entry/start for main executable +ld: symbol(s) not found for architecture arm64 +clang: error: linker command failed with exit code 1 (use -v to see invocation) + +How do I resolve this?","int create_user(const char *name, User **user_ptr_add) { + User *this_user = malloc(sizeof(User)); + this_user->next = NULL; + + /* Need to check if name can be fully copied over. */ + if (strlen(name) > sizeof(this_user->name) - 1) { + return 2;} + + /* Case 1: I am the first user. user_ptr_add is NULL? the linked list is empty. */ + if (*user_ptr_add == NULL) { + strncpy(this_user->name, name, sizeof(this_user->name)); + this_user->name[31] = '\0'; + *user_ptr_add = this_user; + return 0; + } + /* Case 2: linked list already contains users. */ + User *curr = *user_ptr_add; + /* Find the last node <- its next is NULL. */ + while (curr->next != NULL) { + /* Check for duplicate names. */ + if (strcmp(curr->name, name) == 0) { + return 1; + } curr = curr->next; + } curr->next = this_user; + strncpy(this_user->name, name, sizeof(this_user->name)); + this_user->name[31] = '\0'; + + return -1; +}",, +63c647a35f004bf4898cf568,2023-02-14T00:51:04.073Z,Explain Code,v1,,"void list_users(const User *curr) { + while (curr != NULL) { + printf(curr->name); + curr = curr->next; + } +}",, +63c647a35f004bf4898cf568,2023-02-14T00:51:42.488Z,Help Fix Code,v1,,"void list_users(const User *curr) { + while (curr != NULL) { + printf(curr->name); + curr = curr->next; + } +}","Print the usernames of all users in the list starting at curr. Names should be printed to standard output, one per line. Is there anything to be fixed?", +63c647a35f004bf4898cf568,2023-02-14T00:52:34.852Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + User *curr = (User *)head; + while (curr != NULL) { + if (strcmp(curr->name, name) == 0){ + return &curr; + } curr = curr->next; + } + return NULL; +}",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. Is there anything to fix?, +63c647a35f004bf4898cf568,2023-02-14T00:53:46.285Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + User *curr = (User *)head; + while (curr != NULL) { + if (strcmp(curr->name, name) == 0){ + return curr; + } curr = curr->next; + } + return NULL; +}",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. Is there anything to fix?, +63c647a35f004bf4898cf568,2023-02-14T01:52:28.153Z,Help Fix Code,v1,,"int update_pic(User *user, const char *filename) { + /* Check if filename is too long. */ + if (strlen(filename) > sizeof(user->profile_pic) - 1) { + return 2; + } elif (fopen(filename, ""r"") == NULL) { + return 1; + } else { + user->profile_pic = filename; + return 0; + } +}","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.", +63c647a35f004bf4898cf568,2023-02-14T01:56:17.495Z,Explain Code,v1,,"int update_pic(User *user, const char *filename) { + /* Check if filename is too long. */ + if (strlen(filename) > sizeof(user->profile_pic) - 1) { + return 2; + } else if (fopen(filename, ""r"") == NULL) { + return 1; + } else { + strncpy(user->profile_pic, filename, sizeof(user->profile_pic)); + return 0; + } +}",, +63c647a35f004bf4898cf568,2023-02-14T01:57:20.258Z,Question from Code,v1,"If the file does not exist, will fopen() also return NULL?","int update_pic(User *user, const char *filename) { + /* Check if filename is too long. */ + if (strlen(filename) > sizeof(user->profile_pic) - 1) { + return 2; + } else if (fopen(filename, ""r"") == NULL) { + return 1; + } else { + strncpy(user->profile_pic, filename, sizeof(user->profile_pic)); + return 0; + } +}",, +63c647a35f004bf4898cf568,2023-02-14T02:07:55.799Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *this_user = malloc(sizeof(User)); + this_user->next = NULL; + this_user->first_post = NULL; + + /* Need to check if name can be fully copied over. */ + if (strlen(name) > sizeof(this_user->name) - 1) { + return 2;} + + /* Case 1: I am the first user. user_ptr_add is NULL? the linked list is empty. */ + if (*user_ptr_add == NULL) { + strncpy(this_user->name, name, sizeof(this_user->name)); + this_user->name[31] = '\0'; + *user_ptr_add = this_user; + return 0; + } + /* Case 2: linked list already contains users. */ + User *curr = *user_ptr_add; + /* Find the last node <- its next is NULL. */ + while (curr->next != NULL) { + /* Check for duplicate names. */ + if (strcmp(curr->name, name) == 0) { + return 1; + } curr = curr->next; + } curr->next = this_user; + strncpy(this_user->name, name, sizeof(this_user->name)); + this_user->name[31] = '\0'; + + return 0; +}","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) + +However, my code is failing in the case where the name is already in the list. I think the while loop is not working. It does not loop from the head of the linked list. ", +63c647a35f004bf4898cf568,2023-02-14T02:20:03.693Z,Question from Code,v1,"I have this pointer to the head of a linked list, how do I traverse the linked list?",User **user_ptr_add,, +63c647a35f004bf4898cf568,2023-02-14T02:24:21.196Z,Explain Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *this_user = malloc(sizeof(User)); + this_user->next = NULL; + this_user->first_post = NULL; + + /* Need to check if name can be fully copied over. */ + if (strlen(name) > sizeof(this_user->name) - 1) { + return 2;} + + /* Case 1: I am the first user. user_ptr_add is NULL? the linked list is empty. */ + if (*user_ptr_add == NULL) { + strncpy(this_user->name, name, sizeof(this_user->name)); + this_user->name[31] = '\0'; + *user_ptr_add = this_user; + return 0; + } + /* Case 2: linked list already contains users. */ + User *curr = *user_ptr_add; + /* Find the last node <- its next is NULL. */ + while (curr != NULL) { + /* Check for duplicate names. */ + if (strcmp(curr->name, name) == 0) { + printf(""%s\n"", curr->name); + return 1; + } + // if (curr->next == NULL) { + // curr->next = this_user; + // } + curr = curr->next; + } curr = this_user; + strncpy(this_user->name, name, sizeof(this_user->name)); + this_user->name[31] = '\0'; + + return 0; +} +",, +63c647a35f004bf4898cf568,2023-02-14T03:32:54.407Z,Question from Code,v1,Is friends a pointer to an array that stores 1o user structs?,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User;",, +63c647a35f004bf4898cf568,2023-02-14T03:35:04.694Z,General Question,v1,"if I have an array of pointers, how can I determine its size?",,, +63c647a35f004bf4898cf568,2023-02-14T23:21:38.318Z,Question from Code,v1,How do i initialize a time_t,"typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +Post *this_post = malloc(sizeof(Post)); +strcpy(this_post->author, author->name); +this_post->contents = contents;",, +63c647a35f004bf4898cf568,2023-02-14T23:22:31.525Z,Question from Code,v1,Is this the correct way to initialize a time_t?,"typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +Post *this_post = malloc(sizeof(Post)); +strcpy(this_post->author, author->name); +this_post->contents = contents; +this_post->time_t = time(NULL);",, +63c647a35f004bf4898cf568,2023-02-14T23:24:14.102Z,Question from Code,v1,How do I initialize a time_t? I did that and got this error: incompatible integer to pointer conversion assigning to 'time_t *' (aka 'long *') from 'time_t' (aka 'long'),"typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +Post *this_post = malloc(sizeof(Post)); +strcpy(this_post->author, author->name); +this_post->contents = contents; +this_post->date = time(NULL);",, +63c647a35f004bf4898cf568,2023-02-15T17:23:38.274Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int delete_user(const char *name, User **user_ptr_del) { + User *this_user = find_user(name, *user_ptr_del); + // Case 1: User with this name does not exist + if (this_user == NULL) { + return 1; + } + // Case 0: Delete user + // Remove user from each friend's friend list + int this_user_idx; + for (int i = 0; i < sizeof(this_user->friends)/sizeof(this_user->friends[0]); i++) { + // Find the index of this_user + User *frd = this_user->friends[i]; + for (int j = 0; j < sizeof(frd->friends)/sizeof(frd->friends[0]); j++) { + if (frd->friends[j] == this_user) { + this_user_idx = j; + } + } printf(""%d"", this_user_idx); + }","I keep getting a seg fault in line 22 where I want to take this_user's friend list, loop through each friend, and find this_user's index in the friend's friend list.", +63c647a35f004bf4898cf568,2023-02-15T17:27:05.488Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int delete_user(const char *name, User **user_ptr_del) { + User *this_user = find_user(name, *user_ptr_del); + // Case 1: User with this name does not exist + if (this_user == NULL) { + return 1; + } + // Case 0: Delete user + // Remove user from each friend's friend list + int this_user_idx; + for (int i = 0; i < MAX_FRIENDS; i++) { + // Find the index of this_user + User *frd = this_user->friends[i]; + for (int j = 0; j < MAX_FRIENDS; j++) { + if (frd->friends[j] == this_user) { + this_user_idx = j; + } + } printf(""%d"", this_user_idx); + }","I keep getting a seg fault in line 22 where I want to take this_user's friend list, loop through each friend, and find this_user's index in the friend's friend list.", +63c647a35f004bf4898cf568,2023-02-15T17:28:29.429Z,Question from Code,v1,"I keep getting a seg fault in line 22. I want to take this_user's friend list, loop through each friend, and find this_user's index in the friend's friend list.","typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int delete_user(const char *name, User **user_ptr_del) { + User *this_user = find_user(name, *user_ptr_del); + // Case 1: User with this name does not exist + if (this_user == NULL) { + return 1; + } + // Case 0: Delete user + // Remove user from each friend's friend list + int this_user_idx; + for (int i = 0; i < MAX_FRIENDS; i++) { + // Find the index of this_user + User *frd = this_user->friends[i]; + for (int j = 0; j < MAX_FRIENDS; j++) { + if (frd->friends[j] == this_user) { + this_user_idx = j; + } + } printf(""%d"", this_user_idx); + }",, +63c647a35f004bf4898cf568,2023-02-15T21:01:22.117Z,Question from Code,v1,I want to free the memory in the heap but I am getting a memory leak.,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +Post *curr_post = this_user->first_post; + while (curr_post != NULL) { + Post* next_post = curr_post->next; + free(curr_post->contents); + free(curr_post->date); + free(curr_post); + curr_post = next_post; + } + free(this_user);",, +63c647a35f004bf4898cf568,2023-02-15T21:03:26.305Z,Question from Code,v1,"Still getting a memory leak +","typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +Post *curr_post = this_user->first_post; + while (curr_post != NULL) { + Post* next_post = curr_post->next; + free(curr_post->contents); + free(curr_post->date); + free(curr_post); + curr_post = next_post; + } + free(this_user->next); + free(this_user);",, +63c647a35f004bf4898cf568,2023-02-15T21:07:43.306Z,Question from Code,v1,Do I need to free the friends array that is an array of pointers pointing to User structs,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +Post *curr_post = this_user->first_post; + while (curr_post != NULL) { + Post* next_post = curr_post->next; + free(curr_post->contents); + free(curr_post->date); + free(curr_post); + curr_post = next_post; + } + free(this_user->next); + free(this_user);",, +63c647a35f004bf4898cf568,2023-02-15T21:08:39.696Z,Question from Code,v1,Do I need to free the next pointer in this_user? It points to the next user struct in the linked list.,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +Post *curr_post = this_user->first_post; + while (curr_post != NULL) { + Post* next_post = curr_post->next; + free(curr_post->contents); + free(curr_post->date); + free(curr_post); + curr_post = next_post; + } + free(this_user->next); + free(this_user);",, +63c647a35f004bf4898cf568,2023-02-15T23:27:35.592Z,Question from Code,v1,Is this how to allocate space for a User?,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +User *this_user = malloc(sizeof(User)); +",, +63c647a35f004bf4898cf568,2023-02-15T23:50:40.175Z,Question from Code,v1,I think I called malloc twice but I don't know where,"int create_user(const char *name, User **user_ptr_add) { + User *this_user = malloc(sizeof(User)); + this_user->next = NULL; + this_user->first_post = NULL; + strcpy(this_user->profile_pic, ""\0""); + for (int i = 0; i < MAX_FRIENDS; i++) { + this_user->friends[i] = NULL; + } + + // Case 2: Check if name can be fully copied over. + if (strlen(name) > sizeof(this_user->name) - 1) { + return 2;} + + // Case 1: User exists in linked list. + if (find_user(name, *user_ptr_add) != NULL) { + return 1; + } + + strncpy(this_user->name, name, sizeof(this_user->name)); + this_user->name[31] = '\0'; + + // Case 0: (a) Linked list is empty + if (*user_ptr_add == NULL) { + *user_ptr_add = this_user; + } else { + // Case 0: (b) Linked list is not empty + User *curr = *user_ptr_add; + while (curr->next != NULL) { + curr = curr->next; + } curr->next = this_user; + } + return 0; +}",, +63c647a35f004bf4898cf568,2023-02-15T23:56:27.511Z,Question from Code,v1,Did I implicitly called malloc more than once?,"int create_user(const char *name, User **user_ptr_add) { + User *this_user = malloc(sizeof(User)); + this_user->next = NULL; + this_user->first_post = NULL; + strcpy(this_user->profile_pic, ""\0""); + for (int i = 0; i < MAX_FRIENDS; i++) { + this_user->friends[i] = NULL; + } + + // Case 2: Check if name can be fully copied over. + if (strlen(name) > sizeof(this_user->name) - 1) { + return 2;} + + // Case 1: User exists in linked list. + if (find_user(name, *user_ptr_add) != NULL) { + return 1; + } + + strncpy(this_user->name, name, sizeof(this_user->name)); + this_user->name[31] = '\0'; + + // Case 0: (a) Linked list is empty + if (*user_ptr_add == NULL) { + *user_ptr_add = this_user; + } else { + // Case 0: (b) Linked list is not empty + User *curr = *user_ptr_add; + while (curr->next != NULL) { + curr = curr->next; + } curr->next = this_user; + } + return 0; +}",, +63c647a35f004bf4898cf568,2023-02-16T01:07:09.375Z,Question from Code,v1,What happens to my this_user->profile_pic after this line of code?,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +strcpy(this_user->profile_pic, ""\0"");",, +63c647a35f004bf4898cf568,2023-02-17T04:59:51.349Z,Question from Code,v1,does this code have the same implementation as strncpy?,"char *copy(char *dest, const char *src, int capacity) { + memset(dest, '\0', capacity); + // Case 1: dest < src; cannot fully copy over, need to null terminate + for (int i = 0; i < capacity && src[i] != '\0'; i++) { + dest[i] = src[i]; + } + // Case 2: dest > src; have space left in dest + // Already memset so no need to worry + return dest; +} +",, +63c647a35f004bf4898cf568,2023-02-17T05:01:33.343Z,Question from Code,v1,"Does this code do what it is supposed to do: Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated.","char *copy(char *dest, const char *src, int capacity) { + memset(dest, '\0', capacity); + // Case 1: dest < src; cannot fully copy over, need to null terminate + for (int i = 0; i < capacity && src[i] != '\0'; i++) { + dest[i] = src[i]; + } + // Case 2: dest > src; have space left in dest + // Already memset so no need to worry + return dest; +} +",, +63c647a35f004bf4898cf568,2023-02-17T05:02:59.804Z,Question from Code,v1,"Does this code do what it is supposed to do: Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated.","char *copy(char *dest, const char *src, int capacity) { + memset(dest, '\0', capacity); + // Case 1: dest < src; cannot fully copy over, need to null terminate + for (int i = 0; i < capacity && src[i] != '\0'; i++) { + dest[i] = src[i]; + } dest[-1] = '\0'; + // Case 2: dest > src; have space left in dest + // Already memset so no need to worry + return dest; +} +",, +63c647a35f004bf4898cf568,2023-02-17T05:04:11.712Z,Question from Code,v1,"Does this code do what it is supposed to do: Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated.","char *copy(char *dest, const char *src, int capacity) { + memset(dest, '\0', capacity); + // Case 1: dest < src; cannot fully copy over, need to null terminate + for (int i = 0; i < capacity; i++) { + dest[i] = src[i]; + } dest[-1] = '\0'; + // Case 2: dest > src; have space left in dest + // Already memset so no need to worry + return dest; +} +",, +63c647a35f004bf4898cf568,2023-02-17T05:05:01.449Z,Help Fix Code,v1,,"char *copy(char *dest, const char *src, int capacity) { + memset(dest, '\0', capacity); + // Case 1: dest < src; cannot fully copy over, need to null terminate + for (int i = 0; i < capacity; i++) { + dest[i] = src[i]; + } dest[-1] = '\0'; + // Case 2: dest > src; have space left in dest + // Already memset so no need to worry + return dest; +} +","Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. + +How can I fix the code?", +63c647a35f004bf4898cf568,2023-02-17T05:06:08.699Z,Help Fix Code,v1,,"char *copy(char *dest, const char *src, int capacity) { + memset(dest, '\0', capacity); + // Case 1: dest < src; cannot fully copy over, need to null terminate + for (int i = 0; i < capacity && src[i] != '\0'; i++) { + dest[i] = src[i]; + } dest[-1] = '\0'; + // Case 2: dest > src; have space left in dest + // Already memset so no need to worry + return dest; +} +","Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. How can I fix the code?", +63c647a35f004bf4898cf568,2023-02-17T05:06:58.068Z,Help Fix Code,v1,,"char *copy(char *dest, const char *src, int capacity) { + memset(dest, '\0', capacity); + // Case 1: dest < src; cannot fully copy over, need to null terminate + for (int i = 0; i < capacity && src[i] != '\0'; i++) { + dest[i] = src[i]; + } dest[capacity] = '\0'; + // Case 2: dest > src; have space left in dest + // Already memset so no need to worry + return dest; +} +","Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. How can I fix the code?", +63c647a35f004bf4898cf568,2023-02-17T05:07:46.571Z,Question from Code,v1,How do I add a null terminator to the end of the string?,"char *copy(char *dest, const char *src, int capacity) { + memset(dest, '\0', capacity); + // Case 1: dest < src; cannot fully copy over, need to null terminate + for (int i = 0; i < capacity && src[i] != '\0'; i++) { + dest[i] = src[i]; + } dest[capacity] = '\0'; + // Case 2: dest > src; have space left in dest + // Already memset so no need to worry + return dest; +} +",, +63c647a35f004bf4898cf568,2023-02-17T05:08:31.261Z,Question from Code,v1,How do I add a null terminator to the end of the string?,"/* Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset. + */ + +char *copy(char *dest, const char *src, int capacity) { + memset(dest, '\0', capacity); + // Case 1: dest < src; cannot fully copy over, need to null terminate + for (int i = 0; i < capacity && src[i] != '\0'; i++) { + dest[i] = src[i]; + } dest[capacity] = '\0'; + // Case 2: dest > src; have space left in dest + // Already memset so no need to worry + return dest; +} +",, +63c647a35f004bf4898cf568,2023-02-17T05:10:00.366Z,Help Fix Code,v1,,"/* Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset. + */ + +char *copy(char *dest, const char *src, int capacity) { + memset(dest, '\0', capacity); + // Case 1: dest < src; cannot fully copy over, need to null terminate + for (int i = 0; i < capacity && src[i] != '\0'; i++) { + dest[i] = src[i]; + } dest[capacity/sizeof(dest)] = '\0'; + // Case 2: dest > src; have space left in dest + // Already memset so no need to worry + return dest; +} +","Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. Capacity is expected to be the number of bytes of memory allocated to dest. + +Does this code do what it is supposed to do?", +63c647a35f004bf4898cf568,2023-02-17T05:21:36.308Z,Question from Code,v1,"Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. Capacity is expected to be the number of bytes of memory allocated to dest. Does this code do what it is supposed to do?","/* Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset. + */ + +char *copy(char *dest, const char *src, int capacity) { + int n = capacity/sizeof(dest)[0]; + memset(dest, '\0', n); + // Case 1: dest < src; cannot fully copy over, need to null terminate + for (int i = 0; i < capacity && src[i] != '\0'; i++) { + dest[i] = src[i]; + } dest[n] = '\0'; + // Case 2: dest > src; have space left in dest + // Already memset so no need to worry + return dest; +} +",, +63c647a35f004bf4898cf568,2023-02-17T05:22:24.126Z,Question from Code,v1,"Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. Capacity is expected to be the number of bytes of memory allocated to dest. Does this code do what it is supposed to do?","/* Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset. + */ + +char *copy(char *dest, const char *src, int capacity) { + int n = capacity/sizeof(dest)[0]; + memset(dest, '\0', n); + // Case 1: dest < src; cannot fully copy over, need to null terminate + for (int i = 0; i < n && src[i] != '\0'; i++) { + dest[i] = src[i]; + } dest[n] = '\0'; + // Case 2: dest > src; have space left in dest + // Already memset so no need to worry + return dest; +} +",, +63c647a35f004bf4898cf568,2023-02-17T05:23:41.111Z,Help Fix Code,v1,,"/* Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset. + */ + +char *copy(char *dest, const char *src, int capacity) { + int n = capacity/sizeof(dest)[0]; + memset(dest, '\0', n); + // Case 1: dest < src; cannot fully copy over, need to null terminate + for (int i = 0; i < n && src[i] != '\0'; i++) { + dest[i] = src[i]; + } dest[n] = '\0'; + // Case 2: dest > src; have space left in dest + // Already memset so no need to worry + return dest; +} +","Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. Capacity is expected to be the number of bytes of memory allocated to dest.", +63c647a35f004bf4898cf568,2023-02-17T05:24:16.044Z,Help Fix Code,v1,,"/* Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset. + */ + +char *copy(char *dest, const char *src, int capacity) { + int n = capacity/sizeof(dest)[0]; + memset(dest, '\0', n); + // Case 1: dest < src; cannot fully copy over, need to null terminate + for (int i = 0; i < n && src[i] != '\0'; i++) { + dest[i] = src[i]; + } dest[n] = '\0'; + // Case 2: dest > src; have space left in dest + // Already memset so no need to worry + return dest; +} +","Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. Capacity is expected to be the number of bytes of memory allocated to dest.", +63c647a35f004bf4898cf568,2023-02-17T05:24:47.834Z,Help Fix Code,v1,,"/* Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset. + */ + +char *copy(char *dest, const char *src, int capacity) { + int n = capacity/sizeof(dest)[0]; + memset(dest, '\0', n); + // Case 1: dest < src; cannot fully copy over, need to null terminate + for (int i = 0; i < n && src[i] != '\0'; i++) { + dest[i] = src[i]; + } dest[n] = '\0'; + // Case 2: dest > src; have space left in dest + // Already memset so no need to worry + return dest; +} +","Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. Capacity is expected to be the number of bytes of memory allocated to dest.", +63c647a35f004bf4898cf568,2023-02-17T05:55:45.606Z,Help Fix Code,v1,,"/* Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset. + */ + +char *copy(char *dest, const char *src, int capacity) { + // Case 1: dest < src; cannot fully copy over, need to null terminate + int i; + for (i = 0; i < capacity && src[i] != '\0'; i++) { + dest[i] = src[i]; + } + for ( ; i < capacity; i++) { + dest[i] = '\0';} + + dest[capacity - 1] = '\0'; + // Case 2: dest > src; have space left in dest + + return dest; +}","Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. Capacity is expected to be the number of bytes of memory allocated to dest. +", +63c647a35f004bf4898cf568,2023-02-17T05:56:39.200Z,Explain Code,v1,,"/* Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset. + */ + +char *copy(char *dest, const char *src, int capacity) { + // Case 1: dest < src; cannot fully copy over, need to null terminate + int i; + for (i = 0; i < capacity && src[i] != '\0'; i++) { + dest[i] = src[i]; + } + for ( ; i < capacity; i++) { + dest[i] = '\0';} + + dest[capacity - 1] = '\0'; + // Case 2: dest > src; have space left in dest + + return dest; +}",, +63c647a35f004bf4898cf568,2023-03-03T20:25:12.960Z,Question from Code,v1,"I am trying to make it such that only the parent can call fork(), but I am stuck.","int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + int pid; + + for (int i = 0; i < iterations; i++) { + if (i = 0) { + int n = fork(); + if (n != 0) { + pid = getppid(); + } + } + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",, +63c647a35f004bf4898cf568,2023-03-03T20:26:42.554Z,Question from Code,v1,How do I modify this code so that only the parent can call fork()?,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",, +63c647a35f004bf4898cf568,2023-03-03T20:32:19.497Z,Explain Code,v1,,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + int pid; + int n; + + for (int i = 0; i < iterations; i++) { + // if (i = 0) { + // int n = fork(); + // if (n != 0) { + // pid = getppid(); + // } + // } + + if (n != 0) { + n = fork(); + } + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",, +63c647a35f004bf4898cf568,2023-03-03T20:44:16.913Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + int pid; + int n; + + for (int i = 0; i < iterations; i++) { + if (n != 0) { + n = fork(); + } + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +","the new children do not create additional processes. Only the original parent calls fork. But currently, I get one extra fork", +63c647a35f004bf4898cf568,2023-03-03T20:50:46.768Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + int pid; + int n; + + for (int i = 0; i < iterations; i++) { + if (n != 0) { + n = fork(); + } + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +",I want to make sure only the original parent calls fork. , +63c647a35f004bf4898cf568,2023-03-03T21:24:36.724Z,Question from Code,v1,I want to make it such that only child calls fork().,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n == 0) { + n = fork(); + } + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",, +63c647a35f004bf4898cf568,2023-03-03T21:30:11.166Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + int n; + fork(); + + for (int i = 0; i < iterations; i++) { + if (n == 0) { + n = fork(); + } + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",I would like to make sure only child calls fork() but now it only calls it once and terminates when i = 2. So I only get one process from the child instead of 2., +63c647a35f004bf4898cf568,2023-03-03T21:31:50.085Z,Question from Code,v1,I would like to make sure only child calls fork() but now it only calls it once and terminates when i = 2. So I only get one process from the child instead of 2.,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + int n; + fork(); + + for (int i = 0; i < iterations; i++) { + if (n == 0) { + n = fork(); + } + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",, +63c647a55f004bf4898cf57c,2023-02-15T01:58:50.180Z,Question from Code,v1,what is the vaue of ptr,char *ptr;,, +63c647a55f004bf4898cf57c,2023-02-15T01:59:42.411Z,Question from Code,v1,what is the value of s,char s;,, +63c647a55f004bf4898cf57c,2023-02-15T02:00:39.520Z,General Question,v1,logical or in c,,, +63c647a55f004bf4898cf57c,2023-02-15T02:18:53.144Z,General Question,v1,"how to initialize a empty string? +",,, +63c647a55f004bf4898cf57c,2023-02-15T02:20:16.416Z,Question from Code,v1,what is the value of s,"char s[3] ="""";",, +63c647a55f004bf4898cf57c,2023-02-15T02:51:48.170Z,General Question,v1,logical not in c,,, +63c647a55f004bf4898cf57c,2023-02-15T02:52:27.507Z,General Question,v1,what is not in C,,, +63c647a55f004bf4898cf57c,2023-02-15T02:53:31.899Z,Explain Code,v1,,bool t = not false;,, +63c647a55f004bf4898cf57c,2023-02-15T02:55:03.239Z,General Question,v1,is operator not different than logical operator !,,, +63c647a55f004bf4898cf57c,2023-02-15T03:04:54.012Z,General Question,v1,"how to declare a boolean type variable +",,, +63c647a55f004bf4898cf57c,2023-02-15T03:07:55.139Z,General Question,v1,value of false,,, +63c647a55f004bf4898cf57c,2023-02-15T03:08:12.748Z,General Question,v1,what type is false,,, +63c647a55f004bf4898cf57c,2023-02-15T03:09:33.059Z,Explain Code,v1,,0 and 0 == false;,, +63c647a55f004bf4898cf57c,2023-02-15T03:10:53.292Z,Explain Code,v1,,0 || 1 == true;,, +63c647a55f004bf4898cf57c,2023-02-15T03:11:38.902Z,General Question,v1,format specifier for boolean type,,, +63c647a55f004bf4898cf57c,2023-02-15T03:58:12.307Z,General Question,v1,what is being compared when use == to compare two pointers,,, +63c647a55f004bf4898cf57c,2023-02-15T18:30:02.047Z,General Question,v1,what are switch stateemnts,,, +63c647a55f004bf4898cf57c,2023-02-15T18:57:42.381Z,General Question,v1,what does fclose return,,, +63c647a55f004bf4898cf57c,2023-02-15T19:20:13.002Z,Question from Code,v1,how to initialize user.profile_pic to an empty array,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User;",, +63c647a55f004bf4898cf57c,2023-02-15T19:22:02.579Z,Explain Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; +User user1; +char user1.profile_pic[20];",, +63c647a55f004bf4898cf57c,2023-02-15T19:22:52.915Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; +User user1; +char user1.profile_pic[20];",initialize user1.profile_pic to an empty array, +63c647a55f004bf4898cf57c,2023-02-15T19:24:07.359Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; +User user1; +char user1.profile_pic[20];",declare user1.profile_pic to be an array of char, +63c647a55f004bf4898cf57c,2023-02-15T20:47:52.732Z,General Question,v1,value of true,,, +63c647a55f004bf4898cf57c,2023-04-06T21:58:37.321Z,Explain Code,v2,,strlen(NULL);,, +63c647a55f004bf4898cf57c,2023-04-06T22:12:19.600Z,Explain Code,v2,,"char s[3]; +sprintf(s, ""%s"", NULL);",, +63c647a55f004bf4898cf581,2023-01-17T15:09:39.544Z,Question from Code,v1,Is there any problem of this code?,"int main() { + + return 0; +}",, +63c647a55f004bf4898cf581,2023-01-17T15:10:54.560Z,General Question,v1,write a socket program that can transfer information,,, +63c647a55f004bf4898cf581,2023-01-28T18:46:02.605Z,General Question,v1,do i need to free the pointer returned by ctime(),,, +63c647a55f004bf4898cf581,2023-01-28T18:46:21.534Z,General Question,v1,what is a static variable?,,, +63c647a55f004bf4898cf581,2023-03-14T15:36:29.947Z,Question from Code,v2,What does this code do?,"int (*complex(int index))(char *s, int l);",, +63c647ab5f004bf4898cf5a9,2023-01-24T21:31:28.693Z,Question from Code,v1,Why does this code generate a seg fault,"8 void fib(int **fib_sequence, int n) { + 19 *fib_sequence = malloc(sizeof(int) * n); + 20 for (int i = 0; i < n; i++) { + 21 if (i < 2) { + 22 *fib_sequence[i] = i; + 23 } else { + 24 *fib_sequence[i] = *fib_sequence[i - 2] + *fib_sequence[i - 1]; + 25 } + 26 } + 27 }",, +63c647ab5f004bf4898cf5a9,2023-01-24T22:21:14.309Z,Question from Code,v1,Why does this code only look at the first argument of the list?," int **split_array(const int *s, int length) { + 14 int **main_array = malloc(sizeof(int *) * 2); + 15 main_array[0] = malloc(sizeof(int) * length / 2); + 16 main_array[1] = malloc(sizeof(int) * length / 2); + 17 + 18 for (int i = 0; i < length; i++) { + 19 if (i % 2 == 0) { + 20 main_array[0][i] = s[i]; + 21 } else { + 22 main_array[1][i] = s[i]; + 23 } + 24 } + 25 + 26 return main_array; + 27 }",, +63c647ab5f004bf4898cf5a9,2023-01-24T22:22:16.167Z,Question from Code,v1,Does line 2 correctly create a pointer to an array of two dynamically allocated arrays of ints?," int **split_array(const int *s, int length) { + 14 int **main_array = malloc(sizeof(int *) * 2); + 15 main_array[0] = malloc(sizeof(int) * length / 2); + 16 main_array[1] = malloc(sizeof(int) * length / 2); + 17 + 18 for (int i = 0; i < length; i++) { + 19 if (i % 2 == 0) { + 20 main_array[0][i] = s[i]; + 21 } else { + 22 main_array[1][i] = s[i]; + 23 } + 24 } + 25 + 26 return main_array; + 27 }",, +63c647ab5f004bf4898cf5a9,2023-01-24T22:23:12.133Z,Question from Code,v1,Does line 2 correctly create a pointer to an array of two dynamically allocated arrays of ints?," int **split_array(const int *s, int length) { + 14 int **main_array = malloc(sizeof(int) * 2); + 15 main_array[0] = malloc(sizeof(int) * length / 2); + 16 main_array[1] = malloc(sizeof(int) * length / 2); + 17 + 18 for (int i = 0; i < length; i++) { + 19 if (i % 2 == 0) { + 20 main_array[0][i] = s[i]; + 21 } else { + 22 main_array[1][i] = s[i]; + 23 } + 24 } + 25 + 26 return main_array; + 27 }",, +63c647ab5f004bf4898cf5a9,2023-01-24T22:25:13.623Z,Help Fix Code,v1,," 13 int **split_array(const int *s, int length) { + 14 int **main_array = malloc(sizeof(int) * 2); + 15 main_array[0] = malloc(sizeof(int) * length / 2); + 16 main_array[1] = malloc(sizeof(int) * length / 2); + 17 + 18 for (int i = 0; i < length; i++) { + 19 if (i % 2 == 0) { + 20 main_array[0][i] = s[i]; + 21 } else { + 22 main_array[1][i] = s[i]; + 23 } + 24 } + 25 + 26 return main_array; + 27 }",line 2 should create a pointer to an array of two dynamically allocated arrays of ints, +63c647ab5f004bf4898cf5a9,2023-01-25T00:01:43.240Z,Help Fix Code,v1,,"int **split_array(const int *s, int length) { + int **main_array = malloc(sizeof(int *) * 2); + main_array[0] = malloc(sizeof(int) * (length / 2 + length % 2)); + main_array[1] = malloc(sizeof(int) * (length / 2)); + + for (int i = 0; i < length; i++) { + if (i % 2 == 0) { + main_array[0][i / 2] = s[i]; + } else { + main_array[1][i / 2] = s[i]; + } + } + + return main_array; +}",why does this overallocate memory on odd length arrays, +63c647ab5f004bf4898cf5a9,2023-01-25T14:49:13.252Z,Help Fix Code,v1,,"int **split_array(const int *s, int length) { + int **main_array = malloc(sizeof(int *) * 2); + main_array[0] = malloc(sizeof(int) * (length / 2 + length % 2)); + main_array[1] = malloc(sizeof(int) * (length / 2)); + + for (int i = 0; i < length; i++) { + if (i % 2 == 0) { + main_array[0][i / 2] = s[i]; + } else { + main_array[1][i / 2] = s[i]; + } + } + + return main_array; +}","This code should allocate the correct amount of memory to each of the 2 arrays based on the length of the input array. For example, if the array is length 3, it should allocate sizeof(int) * 2 to the first array and sizeof(int) to the second", +63c647ab5f004bf4898cf5a9,2023-02-04T18:29:36.379Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + 17 + 18 // Check the sizeof the name array + 19 if (strlen(name) > 31) { + 20 // Name is too long + 21 return 2; + 22 } + 23 + 24 // Initialize a new user with name pointing to NULL + 25 User new_user; + 26 strcpy(new_user.name, name); + 27 new_user.next = NULL; + 28 + 29 // Check if there are no users currently in the list + 30 if (*user_ptr_add == NULL) { + 31 *user_ptr_add = &new_user; + 32 return 0; + 33 } + 34 + 35 // Create a temp variable to iterate over the list + 36 User *curr = *user_ptr_add; + 37 + 38 while (curr != NULL) { + 39 + 40 // Check if this is the end of the list + 41 if (curr->next == NULL && curr->name != name) { + 42 // Insert the new user + 43 curr->next = &new_user; + 44 } else if (curr->name != name) { + 45 // If the name isnt taken, keep iterating + 46 curr = curr->next; + 47 } else { + 48 // The name is taken + 49 return 1; + 50 } + 51 } + 52 + 53 // Successfully added the new user + 54 return 0; + 55 }","For some reason, this is not correctly adding a new user to the list of users", +63c647ab5f004bf4898cf5a9,2023-02-04T18:34:34.816Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + 17 + 18 // Check the sizeof the name array + 19 if (strlen(name) > 31) { + 20 // Name is too long + 21 return 2; + 22 } + 23 + 24 // Initialize a new user with name pointing to NULL + 25 User new_user; + 26 strcpy(new_user.name, name); + 27 new_user.next = NULL; + 28 + 29 // Check if there are no users currently in the list + 30 if (*user_ptr_add == NULL) { + 31 *user_ptr_add = &new_user; + 32 return 0; + 33 } + 34 + 35 // Create a temp variable to iterate over the list + 36 User *curr = *user_ptr_add; + 37 + 38 while (curr != NULL) { + 39 + 40 // Check if this is the end of the list + 41 if (curr->next == NULL && curr->name != name) { + 42 // Insert the new user + 43 curr->next = &new_user; + 44 } else if (curr->name != name) { + 45 // If the name isnt taken, keep iterating + 46 curr = curr->next; + 47 } else { + 48 // The name is taken + 49 return 1; + 50 } + 51 } + 52 + 53 // Successfully added the new user + 54 return 0; + 55 }",How do I allocate memory when creating the user struct, +63c647ab5f004bf4898cf5a9,2023-02-08T02:48:41.839Z,Help Fix Code,v1,,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + 36 + 37 // Step 1 + 38 struct pixel **pixel_array = malloc(sizeof(struct pixel) * height); + 39 + 40 // Step 2 + 41 for (int i = 0; i < height; i++) { + 42 pixel_array[i] = malloc(sizeof(struct pixel) * width); + 43 } + 44 + 45 // Step 3 + 46 fseek(image, pixel_array_offset, SEEK_SET); + 47 for (int i = 0; i < height; i++) { + 48 for (int j = 0; j < width; j++) { + 49 struct pixel new_pixel; + 50 fread(&new_pixel, sizeof(struct pixel), 3, image); + 51 pixel_array[i][j] = new_pixel; + 52 } + 53 } + 54 return pixel_array; + 55 }",this code should read input from a binary file and add it into the pixel array, +63c647ab5f004bf4898cf5a9,2023-02-08T16:43:38.027Z,Help Fix Code,v1,,"char picture[MAX_NAME]; +262 FILE *profile = fopen(user->profile_pic, ""r""); +263 fgets(picture, MAX_NAME, profile); +264 printf(""%s\n"", picture);",Print the contents of an ASCII file, +63c647ab5f004bf4898cf5a9,2023-02-08T16:45:32.777Z,Help Fix Code,v1,,"char picture[MAX_NAME]; +262 FILE *profile = fopen(user->profile_pic, ""r""); +263 fgets(picture, MAX_NAME, profile); +264 printf(""%s"", picture);","This code should print the entire file, but it only prints the first line", +63c647ab5f004bf4898cf5a9,2023-02-08T17:25:59.545Z,Help Fix Code,v1,,"strcpy(new_post->contents, contents);","I am attempting to copy contents, which is type char * and allocated on the heap to contents, which is also type char * +", +63c647ab5f004bf4898cf5a9,2023-02-15T16:23:40.902Z,Help Fix Code,v1,,"char *copy(char *dest, const char *src, int capacity) { + 16 + 17 int i; + 18 for (i = 0; i < capacity && src[i] != '\0'; i++) { + 19 dest[i] = src[i]; + 20 } + 21 + 22 for (; i < capacity; i++) { + 23 dest[i] = '\0'; + 24 } + 25 + 26 return dest; + 27 }",This should work just the same as strncpy, +63c647ab5f004bf4898cf5a9,2023-02-28T02:00:56.099Z,Help Fix Code,v1,,"/* Read from the open file fp, and create the linked data structure + 9 that represents the Makefile contained in the file. + 10 See the top of pmake.h for the specification of Makefile contents. + 11 */ + 12 Rule *parse_file(FILE *fp) { + 13 // Implement this function and remove the stubbed return statement below. + 14 + 15 // Initialize the head node + 16 Rule *head = malloc(sizeof(Rule)); + 17 + 18 // Initialize the variable to store the output from each line of the file + 19 char output[MAXLINE]; + 20 + 21 // Create a variable to iterate over + 22 Rule *curr = head; + 23 + 24 // Traverse the file + 25 while (fgets(output, MAXLINE, fp) != NULL) { + 26 + 27 // Check if it's a target + 28 if (output[0] != '\t' && is_comment_or_empty(output) == 0) { + 29 // printf(""%s"", output); + 30 // Find the target from the current line + 31 char *ptr = strchr(output, ':'); + 32 + 33 // Copy the length of ptr - 1 to the target variable + 34 int size = strlen(ptr) - 1; + 35 curr->target = malloc(sizeof(char) * size); + 36 strncpy(curr->target, ptr, size - 1); + 37 curr->target[size] = '\0'; + 38 + 39 // TODO: Properly initialize rest of struct + 40 curr->dependencies = NULL; + 41 curr->actions = NULL; + 42 curr->next_rule = malloc(sizeof(Rule)); + 43 curr = curr->next_rule; + 44 + 45 // Otherwise it's an action line + 46 } else if (output[0] == '\t' && is_comment_or_empty(output) == 0) { + 47 // If its an action, parse the args into + 48 // a heap allocated list + 49 // TODO: Delete: + 50 // printf(""%s"", output); + 51 } + 52 } + 53 + 54 return head; + 55 }","Currently, this code always produces a segmentation fault", +63c647ab5f004bf4898cf5a9,2023-02-28T02:44:41.414Z,Help Fix Code,v1,,"char *output; +char *ptr = strchr(output, ':'); + 32 + 33 // Copy the length of ptr - 1 to the target variable + 34 int size = ptr - output;",this code is meant to find the length of the string before the colon, +63c647ab5f004bf4898cf5a9,2023-02-28T02:45:43.892Z,Help Fix Code,v1,,"char *output; +char *ptr = strchr(output, ':'); + 32 + 33 // Copy the length of ptr - 1 to the target variable + 34 int size = ptr - output - 1;",this code is meant to find the length of the string before the colon, +63c647ab5f004bf4898cf5a9,2023-03-09T20:53:34.549Z,Help Fix Code,v2,"This code is meant to read 2 lines from standard input, and send them to the child. The child is then meant to execute the validate process with the user and password sent from the parent and return an exit code. Currently, when run the code is on an infinite loop","#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + // TODO: Error checking + int fd[2], status, r, exit_status; + + if (pipe(fd) == -1) { + perror(""pipe""); + exit(1); + } + + // fork + r = fork(); + if (r < 0) { + perror(""fork""); + } + + // Child process + if (r == 0) { + // Read from the pipe + dup2(fd[0], fileno(stdin)); + char user[MAXLINE]; + char pswd[MAXLINE]; + + read(fd[0], user, MAXLINE); + read(fd[0], pswd, MAXLINE); + + // Close the read end of the pipe + close(fd[0]); + + dup2(fd[1], fileno(stdout)); + execl(""./validate"", ""validate"", user, pswd, NULL); + close(fd[1]); + perror(""execl""); + exit(-1); + } else if (r > 0) { + // Parent process + // Write to the pipe + dup2(fd[1], fileno(stdout)); + write(fd[1], user_id, strlen(user_id) + 1); + write(fd[1], password, strlen(password) + 1); + + close(fd[1]); + wait(&status); + if (WIFEXITED(status)) { + exit_status = WEXITSTATUS(status); + if (exit_status == 0) { + printf(SUCCESS); + } else if (exit_status == 2) { + printf(INVALID); + } else if (exit_status == 3) { + printf(NO_USER); + } else { + fprintf(stderr, ""Invalid exit status %d\n"", exit_status); + exit(EXIT_FAILURE); + } + } + } + + return 0; +} +",, +63c647ab5f004bf4898cf5a9,2023-03-12T17:01:31.733Z,Help Fix Code,v2,"This code is meant to tokenize a word off spaces and build a list of rules and dependencies. Currently, it only does one iteration","char *dep = strtok(NULL, "" ""); + Rule *rule_adder = curr; + Dependency *curr_dep; + while (dep != NULL) { + + // Allocate space for the dependency + Dependency *new_dep = malloc(sizeof(Dependency)); + + if (rule_adder->dependencies == NULL) { + rule_adder->dependencies = new_dep; + curr_dep = rule_adder->dependencies; + } else { + curr_dep->next_dep = new_dep; + curr_dep = curr_dep->next_dep; + } + + // Create the corresponding Rule + curr_dep->rule = malloc(sizeof(Rule)); + curr_dep->rule->target = malloc(strlen(dep) + 1); + strncpy(curr_dep->rule->target, dep, strlen(dep)); + + // Initialize all other fields to NULL + curr_dep->rule->actions = NULL; + curr_dep->rule->dependencies = NULL; + curr_dep->rule->next_rule = NULL; + + // Add the rule to the chain + rule_adder->next_rule = curr_dep->rule; + rule_adder = rule_adder->next_rule; + + dep = strtok(NULL, "" ""); + + curr_dep->next_dep = NULL; + }",, +63c647ac5f004bf4898cf5b3,2023-01-25T08:32:19.203Z,Explain Code,v1,,"#include +#include + +/* + * 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 **pt_fib, int n){ + *pt_fib = malloc(n * sizeof(int)); + *pt_fib[0] = 0; + //*pt_fib[1] = 1; + //**(pt_fib + 1) = 1; + //printf(""%d\n"",*pt_fib[0]); +} + + +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; +} +",, +63c647ac5f004bf4898cf5b3,2023-01-25T08:34:05.905Z,Help Fix Code,v1,,"#include +#include + +/* + * 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 **pt_fib, int n){ + *pt_fib = malloc(n * sizeof(int)); + *pt_fib[0] = 0; + *pt_fib[1] = 1; + //**(pt_fib + 1) = 1; + //printf(""%d\n"",*pt_fib[0]); +} + + +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; +} +",should print 1, +63c647ac5f004bf4898cf5b3,2023-01-25T08:36:31.645Z,Help Fix Code,v1,,"#include +#include + +/* + * 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 **pt_fib, int n){ + *pt_fib = malloc(n * sizeof(int)); + *pt_fib[0] = 0; + //*pt_fib[1] = 1; + **(pt_fib + 1) = 1; + //printf(""%d\n"",*pt_fib[0]); +} + + +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; +} +",should print 1, +63c647ac5f004bf4898cf5b3,2023-01-25T09:52:28.387Z,General Question,v1,"*pt[2] is equal to what expression in C +",,, +63c647ac5f004bf4898cf5b3,2023-01-25T09:53:30.887Z,General Question,v1,why *pt[2] = malloc(2 * size(int)) is invalid expression in C,,, +63c647ac5f004bf4898cf5b3,2023-01-25T09:58:14.403Z,Explain Code,v1,,"int **split_array(const int *s, int length) { + int **pt; + *pt = malloc(2 * sizeof(int *)); + **pt = malloc((length / 2 ) * sizeof(int)); + +}",, +63c647ac5f004bf4898cf5b3,2023-01-25T10:01:53.860Z,Explain Code,v1,,"int **split_array(const int *s, int length) { + int **pt; + *pt = malloc(2 * sizeof(int *)); + pt = malloc((length / 2 ) * sizeof(int)); + +}",, +63c647ac5f004bf4898cf5b3,2023-01-25T10:03:23.261Z,Help Fix Code,v1,,"int **split_array(const int *s, int length) { + int **pt; + *pt = malloc(2 * sizeof(int *)); + pt = malloc((length / 2 ) * sizeof(int)); + +}",Check whether the pt has structure pt[2][length / 2] , +63c647ac5f004bf4898cf5b3,2023-01-26T08:33:29.676Z,Help Fix Code,v1,,"(define (n-of n v) void) + (if (zero? n) + `() + (cons(x xl) (n-of n-1 v)))",why my racket program saying n unbound error, +63c647ac5f004bf4898cf5b3,2023-01-26T15:09:32.147Z,General Question,v1,How to implement greedy algorithm ,,, +63c647ac5f004bf4898cf5b3,2023-01-26T15:10:06.571Z,General Question,v1,You only know C?,,, +63c647ac5f004bf4898cf5b3,2023-02-07T15:20:59.665Z,General Question,v1,What is write in memory when run int I=5;,,, +63c647ac5f004bf4898cf5b8,2023-01-23T03:07:45.045Z,General Question,v1,"how do I read in la -l input to my files without using pipes +",,, +63c647ad5f004bf4898cf5bd,2023-01-18T00:25:10.526Z,General Question,v1,How do I prevent segfaults?,,, +63c647ad5f004bf4898cf5bd,2023-01-18T00:27:53.981Z,Explain Code,v1,,"int main(int b,char**i){long long n=B,a=I^n,r=(a/b&a)>>4,y=atoi(*++i),_=(((a^n/b)*(y>>T)|y>>S)&r)|(a^r);printf(""%.8s\n"",(char*)&_);}",, +63c647ad5f004bf4898cf5c2,2023-01-26T02:23:34.400Z,Explain Code,v1,,int arr[10];,, +63c647ad5f004bf4898cf5c2,2023-01-26T02:28:22.181Z,Explain Code,v1,,int arr[];,, +63c647ad5f004bf4898cf5c2,2023-01-26T02:28:51.444Z,Question from Code,v1,How much memory is allocated?,int arr[];,, +63c647ad5f004bf4898cf5c2,2023-02-17T15:45:36.180Z,Explain Code,v1,," +#include +#include +#include + +struct user{ + int age1; + int *age2; + int data1[10]; + int *data2; + char *name1; // string literal + char *name2; // dynamicallay allocated string + char name3[10]; // allocated when user is declared +}; + + + +void function(struct user user){ + user.age1 += 1; + *user.age2 += 1; + user.data1[0] = 5; + user.data2[0] = 5; +} + +int main(){ + struct user user; + user.age1 = 10; + user.age2 = malloc(sizeof(int)); + *user.age2 = 10; + user.data2 = malloc(sizeof(int) * 10); + for (int i = 1; i <= 10; i++){ + user.data1[i - 1] = i; + user.data2[i - 1] = i; + } + user.name1 = ""kai""; + user.name2 = malloc(sizeof(char) * 10); + strncpy(user.name2, ""kai"", 10); + user.name2[9] = '\0'; + strncpy(user.name3, ""kai"", sizeof(user.name3)); + user.name3[9] = '\0'; + + function(user); + + printf(""age1: %d *age2: %d\n"", user.age1, *user.age2); + printf(""data1: ""); + for (int i = 0; i < 10; i++){ + printf(""%d "", user.data1[i]); + } + + printf(""\n""); + printf(""data2: ""); + for (int i = 0; i < 10; i++){ + printf(""%d "", user.data2[i]); + } + + printf(""\n"") + printf(""name1: %s name2: %s name3: %s"", user.name1, user.name2, user.name3); + + return 0; +}",, +63c647ad5f004bf4898cf5c2,2023-04-04T16:28:24.397Z,Help Fix Code,v2,"remove target_client from the linked list, client","/* + * Remove target_client from the given clients and free the memory. + * Return the fd of the client if it was removed successfully. + * Return 0 otherwise. + */ +int remove_client(Client *target_client, Client **clients){ + Client *curr_client = *clients; + + // return 0 if the list is empty + if (curr_client == NULL){ + return 0; + } + + // find and remove target_client from the list, if it exists + if (curr_client == target_client){ + *clients = curr_client->next_client; + } + else{ + // find client with next client equal to the target_client + while (curr_client->next_client != NULL && curr_client->next_client != target_client){ + curr_client = curr_client->next_client; + } + + // return 0 if target_client doesn't exist in the list + if (curr_client->next_client == NULL){ + return 0; + } + + // remove the target_client + curr_client->next_client = curr_client->next_client->next_client; + } + + // free the memory and return the fd + int client_fd = target_client->fd; + free(target_client->username); + free(target_client); + return client_fd; +}",, +63c647ad5f004bf4898cf5c2,2023-04-04T16:51:56.384Z,General Question,v2,"how can i find length of int as a string +",,, +63c647ad5f004bf4898cf5c2,2023-04-04T18:31:34.130Z,General Question,v2,How do i use asprintf,,, +63c647ae5f004bf4898cf5c7,2023-02-13T00:40:00.300Z,General Question,v1,"I've used malloc to allocate the memory in the heap for a local-scope variable, but that still gets destroyed after the function call. What went wrong? ",,, +63c647ae5f004bf4898cf5c7,2023-02-13T00:43:41.725Z,Help Fix Code,v1,,"int make_friends (const char *name1, const char *name2, User *head) { + /* Find the users from the user list first, regardless of whether + they are valid users or not*/ + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + // To count for existing friends a user has (To be later) + int count = 1; + + // ====================== Checking assumptions ======================== + // If one of them is not even a user + if (user1 == NULL || user2 == NULL) { + return 4; + } // Now both the users beyond this point are valid + + // If they are the same user + if (strcmp(name1, name2) == 0) { + return 3; + } + + // Count for how many friends in each user's friendlist + if (*user1->friends) { + printf(""There is at least one friend for the first user\n""); + User *currfriend = *user1->friends; + while (currfriend->next) { + count++; + currfriend = currfriend->next; + } + if (count == MAX_FRIENDS) { + return 2; + } + } + if (*user2->friends) { + printf(""There is at least one friend for the second user\n""); + // Reset + count = 1; + User *currfriend = *user2->friends; + while (currfriend->next) { + printf(""%10s\n"", currfriend->name); + count++; + currfriend = currfriend->next; + } + if (count == MAX_FRIENDS) { + return 2; + } + } + + /* If both of them are already friends. + Note that friendship is symmetric, so checking one is good enough*/ + + User *first_currfriend = *user1->friends; + User *second_currfriend = *user2->friends; + // If the name is NULL <=> that user does NOT exist + + while (second_currfriend->name != NULL) { + if (strcmp(second_currfriend->name, name1) == 0) { + return 1; + } + second_currfriend = second_currfriend->next; + } + + + // ======================== Making friendships ============================= + // Loop till the end of each non-empty friendlists + /* User *copy1 = malloc(sizeof(User)); + User *copy2 = malloc(sizeof(User)); + + User user_copy1 = *user1; + User user_copy2 = *user2; + copy1 = &user_copy1; + copy2 = &user_copy2; + + copy1->next = NULL; + copy2->next = NULL; */ + + if (first_currfriend) { + while (first_currfriend->name) { + if (strcmp(first_currfriend->name, name2) == 0) { + return 1; + } + first_currfriend = first_currfriend->next; + } + // At the end of the friend list for first user + first_currfriend->next = user2; + } + if (second_currfriend) { + while (second_currfriend->name != NULL) { + if (strcmp(second_currfriend->name, name1) == 0) { + return 1; + } + second_currfriend = second_currfriend->next; + } + // At the end of the friend list for second user + second_currfriend->next = user1; + } + printf(""\n""); + + + + // Now is the case when the first or the second user does not have a friend yet + if (!*(user1->friends)) { + // Here the first user does not have any friend + printf(""There is not even a friend for user1\n""); + *(user1->friends) = user2; + } + if (!*(user2->friends)) { + // Here the second user does not have any friend + printf(""There is not even a friend for user2\n""); + *(user2->friends) = user1; + } + return 0; +} +",Having copy1 and copy2 lasted after the function call to make_friends() , +63c647ae5f004bf4898cf5c7,2023-02-13T00:45:47.705Z,Help Fix Code,v1,,"int make_friends (const char *name1, const char *name2, User *head) { + /* Find the users from the user list first, regardless of whether + they are valid users or not*/ + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + // To count for existing friends a user has (To be later) + int count = 1; + + // ====================== Checking assumptions ======================== + // If one of them is not even a user + if (user1 == NULL || user2 == NULL) { + return 4; + } // Now both the users beyond this point are valid + + // If they are the same user + if (strcmp(name1, name2) == 0) { + return 3; + } + + // Count for how many friends in each user's friendlist + if (*user1->friends) { + printf(""There is at least one friend for the first user\n""); + User *currfriend = *user1->friends; + while (currfriend->next) { + count++; + currfriend = currfriend->next; + } + if (count == MAX_FRIENDS) { + return 2; + } + } + if (*user2->friends) { + printf(""There is at least one friend for the second user\n""); + // Reset + count = 1; + User *currfriend = *user2->friends; + while (currfriend->next) { + printf(""%10s\n"", currfriend->name); + count++; + currfriend = currfriend->next; + } + if (count == MAX_FRIENDS) { + return 2; + } + } + + /* If both of them are already friends. + Note that friendship is symmetric, so checking one is good enough*/ + + User *first_currfriend = *user1->friends; + User *second_currfriend = *user2->friends; + // If the name is NULL <=> that user does NOT exist + + while (second_currfriend->name != NULL) { + if (strcmp(second_currfriend->name, name1) == 0) { + return 1; + } + second_currfriend = second_currfriend->next; + } + + + // ======================== Making friendships ============================= + // Loop till the end of each non-empty friendlists + User *copy1 = malloc(sizeof(User)); + User *copy2 = malloc(sizeof(User)); + + User user_copy1 = *user1; + User user_copy2 = *user2; + copy1 = &user_copy1; + copy2 = &user_copy2; + + copy1->next = NULL; + copy2->next = NULL; + + if (first_currfriend) { + while (first_currfriend->name) { + if (strcmp(first_currfriend->name, name2) == 0) { + return 1; + } + first_currfriend = first_currfriend->next; + } + // At the end of the friend list for first user + first_currfriend->next = user2; + } + if (second_currfriend) { + while (second_currfriend->name != NULL) { + if (strcmp(second_currfriend->name, name1) == 0) { + return 1; + } + second_currfriend = second_currfriend->next; + } + // At the end of the friend list for second user + second_currfriend->next = user1; + } + printf(""\n""); + + + + // Now is the case when the first or the second user does not have a friend yet + if (!*(user1->friends)) { + // Here the first user does not have any friend + printf(""There is not even a friend for user1\n""); + *(user1->friends) = user2; + } + if (!*(user2->friends)) { + // Here the second user does not have any friend + printf(""There is not even a friend for user2\n""); + *(user2->friends) = user1; + } + return 0; +} +",Having copy1 and copy2 stored in the heap and lasted after the function call to make_friends(), +63c647ae5f004bf4898cf5c7,2023-03-30T01:50:22.911Z,Explain Code,v2,,"int on = 1; +int status = setsockopt(soc, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)); +if (status < 0) { + perror(""setsockopt""); + exit(1); +}",, +63c647ae5f004bf4898cf5c7,2023-03-30T01:52:19.215Z,Explain Code,v2,,"if (bind(soc, (struct sockaddr *)self, sizeof(*self)) < 0) { + // bind failed; could be because port is in use. + perror(""bind""); + exit(1); +}",, +63c647ae5f004bf4898cf5c7,2023-03-30T02:29:00.746Z,Explain Code,v2,,"int accept_connection(int listenfd) { + struct sockaddr_in peer; + unsigned int peer_len = sizeof(peer); + peer.sin_family = AF_INET; + + fprintf(stderr, ""Waiting for a new connection...\n""); + int client_socket = accept(listenfd, (struct sockaddr *)&peer, &peer_len); + if (client_socket < 0) { + perror(""accept""); + return -1; + } else { + fprintf(stderr, ""New connection accepted from %s:%d\n"", inet_ntoa(peer.sin_addr), ntohs(peer.sin_port)); + return client_socket; + } +}",, +63c647ae5f004bf4898cf5c7,2023-03-30T02:35:37.657Z,Explain Code,v2,,"struct sockaddr_in addr; + +// Allow sockets across machines. +addr.sin_family = AF_INET; +// The port the server will be listening on. +addr.sin_port = htons(port); +// Clear this field; sin_zero is used for padding for the struct. +memset(&(addr.sin_zero), 0, 8); + +// Lookup host IP address. +struct hostent *hp = gethostbyname(hostname); +if (hp == NULL) { + fprintf(stderr, ""unknown host %s\n"", hostname); + exit(1); +} + +addr.sin_addr = *((struct in_addr *)hp->h_addr);",, +63c647ae5f004bf4898cf5c7,2023-03-30T02:45:35.158Z,Question from Code,v2,Accepted connection should be printed on line 181 ,"int client_fd = accept(fd, NULL, NULL); +if (client_fd < 0) { + perror(""server: accept""); + close(fd); + exit(1); +} + +// If this is a valid user +users[user_index].sock_fd = client_fd; +users[user_index].username = NULL;",, +63c647ae5f004bf4898cf5c7,2023-03-30T03:13:35.912Z,Explain Code,v2,,"char *string; +char array1[5] = ""Hola""; +strncpy(string, array1, 5);",, +63c647ae5f004bf4898cf5c7,2023-03-30T03:14:30.127Z,Help Fix Code,v2,"string should also be ""Hola"" ","char *string; +char array1[5] = ""Hola""; +strncpy(string, array1, 5);",, +63c647ae5f004bf4898cf5c7,2023-03-30T03:16:00.601Z,Help Fix Code,v2,"string should be ""Hola"" ","char *string = malloc(5); +char array1[5] = ""Hola""; +strncpy(string, array1, 5);",, +63c647ae5f004bf4898cf5c7,2023-03-30T03:30:27.812Z,Explain Code,v2,,"setbuf(stdout, NULL);",, +63c647ae5f004bf4898cf5c7,2023-03-30T03:56:44.919Z,Question from Code,v2,What does socket() return upon success?,"int server_soc = socket(AF_INET, SOCK_STREAM, 0); +if (server_soc < 0) { + perror(""Socket:""); +}",, +63c647ae5f004bf4898cf5c7,2023-03-30T16:11:07.300Z,General Question,v2,Should I use select() for clients? ,,, +63c647ae5f004bf4898cf5c7,2023-03-30T16:17:14.906Z,General Question,v2,Should I use select() on client program?,,, +63c647ae5f004bf4898cf5c7,2023-03-30T16:31:40.766Z,General Question,v2,Does 'return 0;' be read as 0 bytes for read()? ,,, +63c647ae5f004bf4898cf5c7,2023-03-30T16:35:58.996Z,General Question,v2,what does FDSET() do? ,,, +63c647ae5f004bf4898cf5c7,2023-03-30T16:40:49.371Z,General Question,v2,Could I write nothing to write()? ,,, +63c647ae5f004bf4898cf5c7,2023-03-30T16:43:39.092Z,General Question,v2,Should I specify the size of an array first before using asprintf?,,, +63c647ae5f004bf4898cf5c7,2023-03-30T16:45:47.065Z,General Question,v2,When should I clean up an array of char? ,,, +63c647ae5f004bf4898cf5c7,2023-03-30T17:47:49.168Z,General Question,v2,When should I use accept() when using select()?,,, +63c647ae5f004bf4898cf5c7,2023-03-30T17:49:34.748Z,General Question,v2,Can a server manage multiple connections by calling 1 accept()?,,, +63c647ae5f004bf4898cf5c7,2023-03-30T17:51:50.260Z,General Question,v2,How to check which fd is ready to read with FD_ISSET()?,,, +63c647ae5f004bf4898cf5c7,2023-03-30T17:53:49.339Z,General Question,v2,How can FD_ISSET() tell which client fd is ready?,,, +63c647ae5f004bf4898cf5c7,2023-03-30T17:59:02.457Z,General Question,v2,"What does inet_pton() do? +",,, +63c647ae5f004bf4898cf5c7,2023-03-30T18:02:35.732Z,General Question,v2,What does FD_SET() do?,,, +63c647ae5f004bf4898cf5c7,2023-03-30T18:07:25.796Z,General Question,v2,What does select() return?,,, +63c647ae5f004bf4898cf5c7,2023-03-30T18:10:28.411Z,General Question,v2,how to tell which fd is ready to read after using select()?,,, +63c647ae5f004bf4898cf5c7,2023-03-30T18:21:35.438Z,General Question,v2,What members are in struct sockaddr_in?,,, +63c647ae5f004bf4898cf5c7,2023-03-30T18:28:15.162Z,General Question,v2,What does connect() do to client socket fd?,,, +63c647ae5f004bf4898cf5c7,2023-03-30T18:29:57.656Z,General Question,v2,How do I know server socket fd via connect()? ,,, +63c647ae5f004bf4898cf5c7,2023-03-30T19:16:35.928Z,General Question,v2,How does connect() work on client socket? ,,, +63c647ae5f004bf4898cf5c7,2023-03-31T00:44:01.763Z,General Question,v2,Does a client know a server fd after connect()? ,,, +63c647ae5f004bf4898cf5c7,2023-03-31T01:30:39.086Z,Question from Code,v2,Why dereference a null pointer on line 31? ,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { + return 2; + } + + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(1); + } + strncpy(new_user->name, name, MAX_NAME); // name has max length MAX_NAME - 1 + + for (int i = 0; i < MAX_NAME; i++) { + new_user->profile_pic[i] = '\0'; + } + + new_user->first_post = NULL; + new_user->next = NULL; + for (int i = 0; i < MAX_FRIENDS; i++) { + new_user->friends[i] = NULL; + } + + // Add user to list + User *prev = NULL; + User *curr = *user_ptr_add; + while (curr != NULL && strcmp(curr->name, name) != 0) { + prev = curr; + curr = curr->next; + } + + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } else if (curr != NULL) { + free(new_user); + return 1; + } else { + prev->next = new_user; + return 0; + } +}",, +63c647ae5f004bf4898cf5c7,2023-03-31T01:34:58.007Z,General Question,v2,"Can I dereference a header to the null linked list in c? +",,, +63c647ae5f004bf4898cf5c7,2023-03-31T01:49:29.877Z,General Question,v2,Can I always modify the size of waiting queue for listen()? ,,, +63c647ae5f004bf4898cf5c7,2023-03-31T02:07:02.257Z,General Question,v2,How to use execl()? ,,, +63c647ae5f004bf4898cf5c7,2023-03-31T02:46:33.758Z,General Question,v2,How to run execl on one process and receive the return on the other process? ,,, +63c647ae5f004bf4898cf5c7,2023-03-31T03:27:31.687Z,General Question,v2,How to use read() and write()? ,,, +63c647ae5f004bf4898cf5c7,2023-03-31T23:25:33.092Z,General Question,v2,Is each element in argv[] separated by blank space? ,,, +63c647ae5f004bf4898cf5c7,2023-04-01T02:00:26.909Z,General Question,v2,When does read() return 0? ,,, +63c647ae5f004bf4898cf5c7,2023-04-01T02:01:32.253Z,General Question,v2,When does read() return 0 in socket programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-01T02:16:15.934Z,General Question,v2,Whatt does socket() return?,,, +63c647ae5f004bf4898cf5c7,2023-04-01T02:18:43.595Z,General Question,v2,What does accept() take on the server side? ,,, +63c647ae5f004bf4898cf5c7,2023-04-01T02:21:33.564Z,General Question,v2,Is accept() only be used on server side? ,,, +63c647ae5f004bf4898cf5c7,2023-04-01T02:23:10.085Z,General Question,v2,How can a client use accept()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-01T02:24:25.051Z,General Question,v2,How does a client use connect()?,,, +63c647ae5f004bf4898cf5c7,2023-04-01T02:27:53.874Z,General Question,v2,Does accept() return client fd upon success?,,, +63c647ae5f004bf4898cf5c7,2023-04-01T02:55:30.699Z,General Question,v2,"If I malloc a whole struct, is every member of it also malloced? ",,, +63c647ae5f004bf4898cf5c7,2023-04-01T03:13:54.257Z,General Question,v2,How do I get client fd in the waiting list from listen()?,,, +63c647ae5f004bf4898cf5c7,2023-04-01T03:36:02.989Z,General Question,v2,How to loop over set_fd? ,,, +63c647ae5f004bf4898cf5c7,2023-04-01T03:50:16.098Z,General Question,v2,How to find the second largest fd in fd_set? ,,, +63c647ae5f004bf4898cf5c7,2023-04-01T16:58:12.749Z,General Question,v2,Could FD_ISSET() prevent the program from being blocked by read()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-01T16:59:58.299Z,General Question,v2,How to prevent a program from being blocked by accept()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-01T19:36:20.082Z,General Question,v2,How to reconnect a client to their previous client fd? ,,, +63c647ae5f004bf4898cf5c7,2023-04-01T20:18:33.208Z,General Question,v2,How to avoid redefinition from a header file? ,,, +63c647ae5f004bf4898cf5c7,2023-04-01T20:20:12.414Z,General Question,v2,How to avoid function redefinition from a header file? ,,, +63c647ae5f004bf4898cf5c7,2023-04-01T23:18:30.228Z,General Question,v2,Can a server keep track of multiple client sockets at once after calling select() and FD_ISSET()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-01T23:32:57.529Z,General Question,v2,Do I need to malloc an int member inside a struct? ,,, +63c647ae5f004bf4898cf5c7,2023-04-01T23:45:38.221Z,General Question,v2,Arguments of read()?,,, +63c647ae5f004bf4898cf5c7,2023-04-04T01:03:09.377Z,Help Fix Code,v2,new_user->sock_fd should be assigned with int fd,"Usersockname *new_user = to_malloc(sizeof(Usersockname)); // to_malloc is from friends.c + new_user->username = to_malloc(strlen(name) + 1); + new_user->sock_fd = to_malloc(sizeof(int)); + new_user->next = to_malloc(sizeof(Usersockname)); + + // There was a previous check on length size, so no need to recheck it again + strncpy(new_user->username, name, strlen(name) + 1); // name has max length MAX_NAME - 1 + new_user->username[strlen(name)] = '\0'; // Null terminate + *(new_user->sock_fd) = fd; ",, +63c647ae5f004bf4898cf5c7,2023-04-04T02:12:58.616Z,General Question,v2,Do I have to malloc the ptr to the first node of linked list? ,,, +63c647ae5f004bf4898cf5c7,2023-04-04T02:18:16.939Z,General Question,v2,Malloc but still cannot hold memory,,, +63c647ae5f004bf4898cf5c7,2023-04-04T02:20:58.658Z,General Question,v2,Is it a good idea to use malloc() inside a local scope? ,,, +63c647ae5f004bf4898cf5c7,2023-04-04T02:21:41.091Z,General Question,v2,How should I malloc the ptr to the first node of array list? ,,, +63c647ae5f004bf4898cf5c7,2023-04-04T02:22:15.408Z,General Question,v2,How should I malloc the double ptr to the first node of array list?,,, +63c647ae5f004bf4898cf5c7,2023-04-04T15:26:02.576Z,General Question,v2,When to use dup2() instead of pipe()?,,, +63c647ae5f004bf4898cf5c7,2023-04-04T16:07:30.812Z,General Question,v2,How to properly initialize an array of char without any garbage?,,, +63c647ae5f004bf4898cf5c7,2023-04-04T16:44:18.094Z,General Question,v2,How to avoid read() from reading a blank line?,,, +63c647ae5f004bf4898cf5c7,2023-04-04T16:45:01.261Z,General Question,v2,How to check for an empty line in C?,,, +63c647ae5f004bf4898cf5c7,2023-04-04T16:45:54.287Z,General Question,v2,Should I use fgets() in socket programming?,,, +63c647ae5f004bf4898cf5c7,2023-04-04T16:47:22.890Z,General Question,v2,How to use strtok() to filter out empty line?,,, +63c647ae5f004bf4898cf5c7,2023-04-04T19:22:52.682Z,General Question,v2,What does FD_SET do?,,, +63c647ae5f004bf4898cf5c7,2023-04-04T19:27:57.092Z,General Question,v2,How can a server take care of multiple simultaneously requests from different clients at once?,,, +63c647ae5f004bf4898cf5c7,2023-04-04T19:41:22.753Z,General Question,v2,"I used select(), FD_SET() and FD_ISSET() but server still ignores upcoming requests",,, +63c647ae5f004bf4898cf5c7,2023-04-04T20:12:25.416Z,General Question,v2,Should I malloc a char[]?,,, +63c647ae5f004bf4898cf5c7,2023-04-05T02:29:39.306Z,General Question,v2,When does read() return -1? ,,, +63c647ae5f004bf4898cf5c7,2023-04-05T02:45:40.668Z,General Question,v2,Will close(fd) also close the pipe? ,,, +63c647ae5f004bf4898cf5c7,2023-04-05T03:02:15.722Z,General Question,v2,Why FD_SET() does not add new fd into fd_set? ,,, +63c647ae5f004bf4898cf5c7,2023-04-05T12:43:41.569Z,General Question,v2,why double free or corruption (out) when I only free() for the first time?,,, +63c647ae5f004bf4898cf5c7,2023-04-05T12:58:27.850Z,General Question,v2,When does free() gives corrupted error? ,,, +63c647ae5f004bf4898cf5c7,2023-04-05T13:25:49.649Z,General Question,v2,Could I copy a malloced string to a stack-allocated string? ,,, +63c647ae5f004bf4898cf5c7,2023-04-05T18:58:53.275Z,General Question,v2,Could I use strlen() on just dynamically allocated string without any input? ,,, +63c647ae5f004bf4898cf5c7,2023-04-05T18:59:56.304Z,General Question,v2,How to tell if a malloced string is empty or not? ,,, +63c647ae5f004bf4898cf5c7,2023-04-05T19:00:35.544Z,General Question,v2,Is all malloced string initialized with null characters? ,,, +63c647ae5f004bf4898cf5c7,2023-04-05T20:08:22.725Z,General Question,v2,Does memset() affect malloced string? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T00:35:52.449Z,General Question,v2,Will strtok() return a stack-allocated string after tokenizing heap-allocated string?,,, +63c647ae5f004bf4898cf5c7,2023-04-06T01:06:03.166Z,General Question,v2,What does select() return where there is a signal from a client? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T01:12:19.829Z,General Question,v2,How to know how many clients are talking via select() and FD_ISSET()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T01:20:26.388Z,General Question,v2,Should the server call accept() multiple times on the same client? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T01:22:25.095Z,General Question,v2,How to use the fcntl() function with the O_NONBLOCK flag for non-blocking select() call? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T01:28:39.696Z,General Question,v2,"How should I use fcntl() in socket programming? +",,, +63c647ae5f004bf4898cf5c7,2023-04-06T01:29:35.485Z,General Question,v2,How to use fcntl() with multiple fds? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T01:41:33.530Z,General Question,v2,Why fcntl() leads to broken pipe? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T01:43:15.899Z,General Question,v2,How to make a server unblocking on read() call from multiple clients? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T01:48:22.446Z,General Question,v2,I used select() and FD_ISSET() but the server still blocks inputs from clients?,,, +63c647ae5f004bf4898cf5c7,2023-04-06T02:02:10.895Z,General Question,v2,What makes select() call knows a client is ready to read? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T02:07:20.372Z,General Question,v2,What is the point of using FD_ISSET() after select() call? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T02:15:47.384Z,General Question,v2,How to make a read() call unblocking?,,, +63c647ae5f004bf4898cf5c7,2023-04-06T02:16:57.941Z,General Question,v2,How to use fcntl()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T02:27:22.232Z,General Question,v2,How to know if the read() call is blocking? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T02:30:15.718Z,General Question,v2,Is the server socket fd always set at 3? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T02:31:08.346Z,General Question,v2,is select() a blocking function? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T02:31:53.810Z,General Question,v2,How to set out a timeout for select()?,,, +63c647ae5f004bf4898cf5c7,2023-04-06T02:34:23.889Z,General Question,v2,what does select() return after being timed out? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T04:45:51.676Z,General Question,v2,"Why the server keeps ignoring clients' call even though using select(), FD_iSSET() and reset the set_fd to the original set_fd? ",,, +63c647ae5f004bf4898cf5c7,2023-04-06T04:47:54.165Z,General Question,v2,How to use FD_COPY()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T04:48:59.740Z,General Question,v2,What are the parameters of FD_COPY()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T04:55:39.753Z,General Question,v2,How to copy a set_fd object? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T12:43:04.763Z,General Question,v2,Why timeout (struct timeval) only waits on the first call of select() with a given client fd? ,,, +63c647ae5f004bf4898cf5c7,2023-04-06T14:57:15.211Z,General Question,v2,Does select() save the ready-to-read fd to its parameters?,,, +63c647ae5f004bf4898cf5c7,2023-04-06T15:13:19.336Z,General Question,v2,How to print out every fd inside fd_set?,,, +63c647ae5f004bf4898cf5c7,2023-04-22T20:27:45.737Z,General Question,v2,why does strtol need pointer to char array?,,, +63c647ae5f004bf4898cf5c7,2023-04-22T20:38:44.778Z,General Question,v2,What happens if I pass string of 0 to strtol? ,,, +63c647ae5f004bf4898cf5c7,2023-04-22T23:37:10.079Z,General Question,v2,"What happens if I pass a NULL endptr to strtol()? +",,, +63c647ae5f004bf4898cf5c7,2023-04-22T23:53:29.243Z,General Question,v2,Does strtol read negative values? ,,, +63c647ae5f004bf4898cf5c7,2023-04-22T23:54:12.565Z,General Question,v2,when does strtol return an error? ,,, +63c647ae5f004bf4898cf5c7,2023-04-22T23:55:59.144Z,General Question,v2,Does strtol return an error if there is a non-numeric value in a string?,,, +63c647ae5f004bf4898cf5c7,2023-04-23T01:15:46.787Z,General Question,v2,What happens if errno is not 0? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T01:38:05.809Z,General Question,v2,Which process id pick up orphanage processes? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T01:46:39.465Z,General Question,v2,Is waitpid() a blocking function? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T01:50:08.578Z,General Question,v2,Does waitpid return 0 mean that pid process is an orphan? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T01:54:39.932Z,General Question,v2,What return status of waitpid indicate that pid process is a zombie?,,, +63c647ae5f004bf4898cf5c7,2023-04-23T03:03:58.602Z,General Question,v2,How do we call pipe in C? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T03:14:05.594Z,General Question,v2,When does pipe() return -1? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T03:20:34.051Z,General Question,v2,Is a newer pid always greater than the older ones? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T03:22:08.963Z,General Question,v2,Are system calls returning -1 as errors? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T03:24:30.194Z,General Question,v2,What happens if I close a closed fd? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T03:25:39.636Z,General Question,v2,What happens if I close a non-existent fd? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T03:28:58.750Z,General Question,v2,How to close all previously forked children processes? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T15:58:01.970Z,General Question,v2,Why do I need to pass in ptr to read() and write() call? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T15:59:03.219Z,General Question,v2,What does buffer do in write() and read()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T16:01:16.517Z,General Question,v2,What does buffer do in read()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T16:07:26.449Z,General Question,v2,When does waitpid() return -1? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T16:08:42.630Z,General Question,v2,How to use WNOHANG with waitpid()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T16:21:31.050Z,General Question,v2,Why does wait() need a ptr to status? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T16:22:52.785Z,General Question,v2,Use wait() with WIFEXITED and WEXITSTATUS,,, +63c647ae5f004bf4898cf5c7,2023-04-23T16:25:32.569Z,General Question,v2,What argument does WEIXTSTATUS take? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T16:26:46.818Z,General Question,v2,What argument does WEXITSTATUS take? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T17:17:59.346Z,General Question,v2,open() vs. fopen()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T17:21:09.605Z,General Question,v2,What does dup2() do in C? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T17:53:01.112Z,General Question,v2,How to use execl()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-23T17:55:19.998Z,General Question,v2,What are the parameters of waitpid()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T00:23:53.643Z,General Question,v2,sigaddset() and sigemptyset() in C?,,, +63c647ae5f004bf4898cf5c7,2023-04-24T00:25:17.447Z,General Question,v2,sigprocmask()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T00:35:00.431Z,General Question,v2,Does sigprocmask() block a signal from reaching a process? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T03:04:24.196Z,General Question,v2,What does sigprocmask()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T03:06:40.563Z,General Question,v2,what does SIG_BLOCK do? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T03:07:27.032Z,General Question,v2,What does SIG_SETMASK do? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T03:26:07.006Z,General Question,v2,Can you use the `sigprocmask()` function to check if a signal is currently blocked or unblocked in your program?,,, +63c647ae5f004bf4898cf5c7,2023-04-24T03:54:27.656Z,General Question,v2,When to fill out oldset in sigcpromask()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T04:03:16.698Z,General Question,v2,Does an oldset and set in setprocmask()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T04:04:42.382Z,General Question,v2,What do oldset and set contain in sigprocmask()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T04:07:47.735Z,General Question,v2,What is a signal mask? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T04:09:39.478Z,General Question,v2,What does struct sigaction do? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T04:15:29.797Z,General Question,v2,What does sigaction() do in C? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T13:05:19.447Z,General Question,v2,What defines as a user-defined signal or SIGUSR? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T13:08:02.874Z,General Question,v2,Difference between signal() and sigaction()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T13:21:52.011Z,General Question,v2,Could sigaction() be used to unblock a masked signal? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T13:24:45.590Z,General Question,v2,Could sigaction() be used to modify sa_handler of struct sigaction? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T13:27:25.073Z,General Question,v2,Is sigaction() be used to update the system behavior according to the members of the given struct sigaction? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T13:30:27.921Z,General Question,v2,What is the purpose of signum in sigaction()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T13:32:18.576Z,General Question,v2,Could I use sigaddset() instead of passing a signum into sigaction()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T13:44:34.214Z,General Question,v2,What are the members of ps_set? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T13:46:02.966Z,General Question,v2,What does PS_ISSET() do? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T13:47:31.650Z,General Question,v2,Difference between PS_ZERO and PS_CLEAR? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T13:51:03.187Z,General Question,v2,Difference between PS_ZERO and PS_CLR?,,, +63c647ae5f004bf4898cf5c7,2023-04-24T14:31:36.010Z,General Question,v2,What does expr do in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T14:33:02.235Z,General Question,v2,` ` in shell programming ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T14:36:22.289Z,General Question,v2,Could I use ` instead of expr in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T14:56:33.428Z,General Question,v2,What does shebang do? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T14:59:17.419Z,General Question,v2,What does an intepreter do in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T15:37:55.393Z,General Question,v2,What does grep do in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T15:42:12.994Z,General Question,v2,Does test in shell programming used for checking for conditional if? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T15:42:54.783Z,General Question,v2,Could test replace expr in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T15:45:49.292Z,General Question,v2,What does match of expr do in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T17:11:21.813Z,General Question,v2,What does bind() do in socket programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T17:12:47.012Z,General Question,v2,Is the socket file descriptor remaining consistent throughout multiple calls of bind() with the given socket()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T17:14:16.159Z,General Question,v2,What does the socket file descriptor outputted by socket() contain? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T17:15:25.558Z,General Question,v2,What does it mean when saying socket file descriptor is the same as file descriptor?,,, +63c647ae5f004bf4898cf5c7,2023-04-24T17:16:36.087Z,General Question,v2,Why the socket file descriptor given by socket() must be reused for subsequent bind() calls? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T17:17:49.361Z,General Question,v2,Does any socket file descriptor have to have the port of the server?,,, +63c647ae5f004bf4898cf5c7,2023-04-24T19:25:38.562Z,General Question,v2,= vs. \eq in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T19:26:15.336Z,General Question,v2,Does == exist in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T19:27:37.743Z,General Question,v2,How to compare two different integers in shell programming besides \eq?,,, +63c647ae5f004bf4898cf5c7,2023-04-24T19:28:29.911Z,General Question,v2,How to check two integers are equal besides \eq in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T19:36:01.428Z,General Question,v2,Does test always require two operands? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T19:45:45.404Z,General Question,v2,Why does ambiguous redirect output in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T19:50:39.038Z,General Question,v2,What does ls | wc do in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T19:52:18.383Z,General Question,v2,What counts as a line in wc in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-24T19:57:03.896Z,Question from Code,v2,f,"#!/ bin / bash + +for +arg in ""$@"" do +#If you want the shared version + / u / csc209h / winter / pub / shell - + prog / fibonacci ""$arg"" done",, +63c647ae5f004bf4898cf5c7,2023-04-25T01:55:35.449Z,General Question,v2,When does waitpid() immediately return? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T02:01:43.849Z,General Question,v2,Is it true that connect() can be blocked when the listen queue is full? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T02:03:58.077Z,General Question,v2,When does fseek() return -1? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T02:05:34.704Z,General Question,v2,What does fseek() do? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T02:07:25.564Z,General Question,v2,How to copy a struct? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T02:08:51.649Z,General Question,v2,How does fwrite() work?,,, +63c647ae5f004bf4898cf5c7,2023-04-25T02:12:27.593Z,General Question,v2,What are the parameters of fread()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T02:13:50.146Z,General Question,v2,What are the flags of wc in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T02:16:27.010Z,Question from Code,v2,What does pipe do in the code? ,../ my_prog | wc - l > outputfile,, +63c647ae5f004bf4898cf5c7,2023-04-25T02:17:22.228Z,Question from Code,v2,Why are there two dots at the beginning? ,../ my_prog | wc - l > outputfile,, +63c647ae5f004bf4898cf5c7,2023-04-25T02:21:05.862Z,General Question,v2,What are the parameters of fread()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T02:23:42.051Z,General Question,v2,When piping is needed in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T02:26:51.741Z,General Question,v2,When to use (()) in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T02:27:35.659Z,General Question,v2,Can I use (()) instead of expr in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T02:28:16.093Z,General Question,v2,Does (()) work on string operator? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T02:41:47.856Z,General Question,v2,Is it good to leave an extra space for null terminator when mallocing a char ptr? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T02:55:32.337Z,General Question,v2,When to use htons()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T03:15:18.622Z,General Question,v2,Is a string literal always null terminated? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T03:16:39.063Z,General Question,v2,Are all memory blocks in heap initialized to \0 at first? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T03:55:31.077Z,General Question,v2,What are the parameters of fscanf()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T03:57:27.448Z,General Question,v2,Parameters of strtol()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T03:58:38.629Z,General Question,v2,Can you give usage of fprintf()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T12:28:43.548Z,General Question,v2,Could I use strlen on newly created array? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T12:30:47.848Z,General Question,v2,Why can't I strncpy \0 into a string? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T12:32:00.374Z,General Question,v2,How to set null terminator in a char pointer? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T12:32:49.369Z,General Question,v2,Could I append null terminator with strncat()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T12:35:08.057Z,General Question,v2,Do I need to append null terminator at the end of a string literal? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T12:36:40.962Z,General Question,v2,How to make sure every string literal is always null terminated? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T12:37:39.633Z,General Question,v2,Do I need to add +1 in strncat()? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T12:39:45.304Z,General Question,v2,Does strncat add null terminators on the leftover indexes of a string? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T12:41:13.342Z,General Question,v2,Does a string after strncat() always be null terminated if there's extra space left at the end? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T12:57:21.473Z,General Question,v2,What does WIFEXITED(status) return? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T13:41:51.849Z,General Question,v2,Why does a newly created ptr do not have any initialized value? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T13:42:49.391Z,General Question,v2,What does socket() do?,,, +63c647ae5f004bf4898cf5c7,2023-04-25T13:46:18.143Z,General Question,v2,What is the difference between socket domain and socket type? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T13:48:44.324Z,General Question,v2,Is it true that a port can only be reserved for one client to talk to their server? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T13:51:46.004Z,General Question,v2,Could I use dup() or dup2() on closed file fd? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T13:56:27.209Z,General Question,v2,What does while(1); do? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T13:57:43.651Z,General Question,v2,How do I do kill in shell programming to end one's program activity? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T13:58:31.394Z,General Question,v2,What are flags of kill in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T15:15:49.357Z,Question from Code,v2,What does this code do? ,./ myprog actual$i.out,, +63c647ae5f004bf4898cf5c7,2023-04-25T15:23:49.860Z,General Question,v2,Does waitpid() accept WNOHANG? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T15:38:40.611Z,General Question,v2,What is -a in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T15:46:59.638Z,General Question,v2,What does ${} do in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T15:51:54.804Z,General Question,v2,Would if test 1 get passed into then block in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T15:54:01.645Z,General Question,v2,"What is the difference between ""$parameter"" and ""${parameter}""? ",,, +63c647ae5f004bf4898cf5c7,2023-04-25T15:56:16.756Z,General Question,v2,What are special characters that I need to put \ before them in double quote in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T15:57:54.078Z,General Question,v2,"Could I use ""${parameter}"" instead of ""$parameter""? ",,, +63c647ae5f004bf4898cf5c7,2023-04-25T16:04:18.232Z,Question from Code,v2,What does the first line do? ,"for + i in *; +do echo $i is a file done",, +63c647ae5f004bf4898cf5c7,2023-04-25T16:05:50.248Z,Question from Code,v2,Why there is a semicolon on line 1? ,"for + i in *; +do echo $i is a file done",, +63c647ae5f004bf4898cf5c7,2023-04-25T16:16:57.479Z,General Question,v2,How to get the current directory in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T16:25:37.993Z,General Question,v2,What does : do in a block in shell programming?,,, +63c647ae5f004bf4898cf5c7,2023-04-25T16:27:01.684Z,General Question,v2,What does extern do in header files? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T16:28:21.564Z,General Question,v2,Could I use static variables in header files? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T16:29:32.562Z,General Question,v2,Can I compile more than one file with main() inside a makefile? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T16:36:25.900Z,General Question,v2,can I send a signal to a process that is neither a parent nor a child of the current process? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T16:39:01.933Z,General Question,v2,Are open descriptors not inherited across an exec() call?,,, +63c647ae5f004bf4898cf5c7,2023-04-25T16:40:24.879Z,General Question,v2,Are memory addresses not inherited across an exec() call?,,, +63c647ae5f004bf4898cf5c7,2023-04-25T16:43:51.265Z,General Question,v2,Could I dereference out-of-bound index? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T16:46:07.496Z,General Question,v2,How to check if a binary representation is a negative number? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T16:47:08.421Z,General Question,v2,Where is the sign bit in binary representation? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T16:51:15.112Z,General Question,v2,How to copy an array of characters? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T18:46:09.642Z,General Question,v2,redir in shell programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T20:40:40.188Z,General Question,v2,When to update max_fd in socket programming? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T20:42:35.780Z,General Question,v2,What happens if I initialized the members of a struct with values in a wrong way? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T20:49:48.361Z,General Question,v2,Name the three (3) categories that most preprocessing directives fall into,,, +63c647ae5f004bf4898cf5c7,2023-04-25T20:51:23.117Z,General Question,v2,What are the three categories most of C preprocessors are categorized as? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T20:52:26.538Z,General Question,v2,What is the point of calling fclose() when program will end anyway?,,, +63c647ae5f004bf4898cf5c7,2023-04-25T20:53:28.246Z,General Question,v2,What are 2 similarities between pipes and files? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T20:58:34.899Z,General Question,v2,How do I initialize an array of char? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T21:08:01.342Z,General Question,v2,How to identify if a process is a zombie? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T21:09:20.324Z,General Question,v2,What does ps aux do? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T21:09:58.879Z,General Question,v2,How does ps differ from ps aux? ,,, +63c647ae5f004bf4898cf5c7,2023-04-25T21:12:22.305Z,General Question,v2,Are System calls C programs' interface to the operating system and the computer’s hardware?,,, +63c647ae5f004bf4898cf5c7,2023-04-25T21:13:16.832Z,General Question,v2,Is it true that a server may read and write to multiple clients on the same socket?,,, +63c647ae5f004bf4898cf5c7,2023-04-25T21:15:12.207Z,General Question,v2,Is it true that Makefile will exit with an error if the target of a rule is not a file?,,, +63c647ae5f004bf4898cf5c7,2023-04-25T21:15:57.853Z,General Question,v2,Are all dependencies in a Makefile files? ,,, +63c647b05f004bf4898cf5d6,2023-01-19T02:45:07.072Z,Help Fix Code,v1,,"char phone[11]; +int num; +scanf(""%s%d"", phone, num);","phone should save the first 11 char from the input, which is followed by a number", +63c647b05f004bf4898cf5d6,2023-01-22T23:54:23.456Z,Explain Code,v1,,"int length = 0; +int num = 298475 +for (int i = 10; i > 10; i*10) { + num -= (num % i); + length ++ +}",, +63c647b05f004bf4898cf5d6,2023-01-23T00:06:33.860Z,Explain Code,v1,,"int c = 1; +int a = c; ",, +63c647b05f004bf4898cf5d6,2023-01-23T00:11:51.745Z,Explain Code,v1,,"int x = 123; +if (x / 10 == 12){ + printf(""yay""); +} ",, +63c647b05f004bf4898cf5d6,2023-01-23T00:12:41.562Z,Explain Code,v1,,"int x = 128; +if (x / 10 == 12){ + printf(""yay""); +} ",, +63c647b05f004bf4898cf5d6,2023-01-28T02:23:10.331Z,Help Fix Code,v1,,"int copy = 123456789; +int length = 1; + +for (int i = 10; copy > 10; i * 10){ + copy -= copy % i; // turning every digit to 0 + length += 1; +}",count the number of digits in copy, +63c647b05f004bf4898cf5d6,2023-01-28T03:03:19.327Z,Explain Code,v1,,"printf(""check: %d \n"", sin % (10^1));",, +63c647b05f004bf4898cf5d6,2023-01-28T03:03:46.784Z,Explain Code,v1,,"printf(""check: %d \n"", 123456789 % (10^1));",, +63c647b05f004bf4898cf5d6,2023-01-28T03:05:38.961Z,Explain Code,v1,,"int sin = 123456789; +for (int i = 0; i < 9; i ++) { + printf(""sin: %d \n"", sin); + int digit = (sin % (10^(1+i))); + printf(""digit: %d \n"", digit); + sin -= digit * 10^i; + sin_array[8 - i] = digit // dividing by 10 drops the decimals + printf(""array: %d \n"", sin_array[8 - i]); +}",, +63c647b05f004bf4898cf5d6,2023-01-28T03:10:24.036Z,Explain Code,v1,,"int sin = 123456789; +for (int i = 0; i < 9; i ++) { + printf(""sin: %d \n"", sin); + int digit = (sin % (10^(1+i))); + printf(""digit: %d \n"", digit); + sin -= digit * 10^i; + sin_array[8 - i] = digit; // dividing by 10 drops the decimals + printf(""array: %d \n"", sin_array[8 - i]); +}",, +63c647b05f004bf4898cf5d6,2023-01-28T03:17:52.627Z,Help Fix Code,v1,," +#include +#include + +int main() +{ + int sin = 123456789; + int sin_array[9]; + int digit; + for (int i = 0; i < 9; i ++) { + digit = sin % (10^(1+i)); + printf(""digit: %d \n"", digit); + sin -= digit * 10^i; + sin = sin / 10; + sin_array[8 - i] = digit; + } +}",should take each digit in sin and place it in sin_array at the corresponding index, +63c647b05f004bf4898cf5d6,2023-01-31T20:09:08.521Z,Help Fix Code,v1,,"if (strlen(str) > amount) { + // truncate str at amount characters + str[amount] = '\0'; + return (strlen(str) - amount); +} +return 0; ","if length of the string is greater than the amount given, then truncate the string to be of length amount and return the number of chars removed", +63c647b05f004bf4898cf5d6,2023-02-09T20:05:16.989Z,Help Fix Code,v1,,"struct *current; +int length = sizeof(*(current->friends))/sizeof(*(current->friends)[0]); +",save length of an array called friends that's saved in struct called current , +63c647b05f004bf4898cf5d6,2023-02-11T05:55:45.518Z,Question from Code,v1,What is the line to compile it into an executable named a.out that it will print the value 5 to stdout? ,"#include +int main() { + + #ifdef MACRO + printf(""%d\n"", MACRO); + #endif + return 0; +} +",, +63c647b05f004bf4898cf5d6,2023-02-11T05:57:09.215Z,Question from Code,v1,in the program. What will this declaration line become after the program has passed through the C pre-processor?,"#define MAXNAME = 32; +char name[MAXNAME]; +",, +63c647b05f004bf4898cf5d6,2023-02-27T16:13:32.016Z,Question from Code,v1,"why does the third line make w[1] = ""ello""?","char **w = malloc(2 * sizeof(char *)); +w[0] = ""hello""; +*(w + 1) = w[0] + 1; +printf(""%s, %s"", w[0], w[1]);",, +63c647b05f004bf4898cf5d6,2023-02-27T16:17:11.200Z,General Question,v1,"how can you tell the difference between compiling and run time error +",,, +63c647b05f004bf4898cf5d6,2023-02-27T16:20:29.923Z,Explain Code,v1,,char *names[2]; ,, +63c647b05f004bf4898cf5d6,2023-02-27T16:21:39.230Z,Question from Code,v1,"which takes precedence, * or [2]",char *names[2]; ,, +63c647b05f004bf4898cf5d6,2023-02-27T16:23:19.240Z,General Question,v1,fscanf vs fread?,,, +63c647b05f004bf4898cf5d6,2023-02-27T16:27:14.577Z,Question from Code,v1,is this run time or compiling error,"char *film = ""Moonlight"";  NO ERROR +char x; +x = film;  RUN-TIME ERROR +x[0] = ’L’;",, +63c647b05f004bf4898cf5d6,2023-02-27T16:27:40.912Z,Question from Code,v1,is this run time or compiling error,"char *film = ""Moonlight""; +char x; +x = film; +x[0] = ’L’;",, +63c647b05f004bf4898cf5d6,2023-02-27T16:35:14.609Z,Question from Code,v1,why is this a run time error,"int *mkpoint(int x, int y) { + int pt[2] = {x, y}; + return pt; +} +int *x; +x = mkpoint(3, 4);",, +63c647b05f004bf4898cf5d6,2023-02-27T20:11:33.357Z,Question from Code,v1,how do we make sure the new children don't create additional processes,"for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); +} +",, +63c647b05f004bf4898cf5d6,2023-02-27T21:34:30.308Z,Question from Code,v1,how do we make sure that when process only creates one child and control the total number of processes,"for (int i = 0; i < iterations; i++) { + if (created == 0){ + int created = fork(); + if (created < 0) { + perror(""fork""); + exit(1); + } else if (created > 0) { + wait(&waitstatus); + } + } + + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } +",, +63c647b05f004bf4898cf5d6,2023-02-27T22:52:24.060Z,Question from Code,v1,why does the parent process fork 2 processes? ,"int main() { + int iterations = 2; + int created = 0; + int n = 0; + + for (int i = 0; i < iterations; i++) { + if (created == 0){ + int created = fork(); + if (created < 0) { + perror(""fork""); + exit(1); + } else if(created > 0) { + wait(&n); + } + } + + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + return 0; +} +",, +63c647b05f004bf4898cf5d6,2023-02-27T22:57:10.932Z,Question from Code,v1,why does the parent process fork 2 processes when created changes to a non 0 value,"int main() { + int iterations = 2; + int created = 0; + int n = 0; + + for (int i = 0; i < iterations; i++) { + if (created == 0){ + int created = fork(); + if (created < 0) { + perror(""fork""); + exit(1); + } else if(created > 0) { + wait(&n); + } + } + + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + return 0; +} +",, +63c647b05f004bf4898cf5d6,2023-03-07T00:07:57.878Z,Question from Code,v1,"this program uses a different file called validate, which validates the inputed password and username. The input (from standard input) is in this form: file_name \n username \n password. But when I run my program (which forks a child and pipes the input to the child), stdin looks like this: file_name \n username \n password \n \n. Why?","int fds[2]; + if (pipe(fds) == -1) { + return -1; + } + + int n = fork(); + if (n == -1) { + return -1; + } else if (n == 0) { + // child process + dup2(fds[0], STDOUT_FILENO); + close(fds[1]); // closed unused end + execl(""./validate"", ""validate"", NULL); + + } else { + // parent process + close(fds[0]); + write(fds[1], user_id, 10); // hardcoded MAXPASSWORD + write(fds[1], password, 10); + int status; + + if(wait(&status) == -1) { + return -1; + } else if (WIFEXITED(status)) { // checks id process exits normally + int validation = WEXITSTATUS(status); + if (validation == 0) { + printf(SUCCESS); + } else if (validation == 1) { + // error + return -1; + } else if (validation == 2) { + printf(INVALID); + } else if (validation == 3) { + printf(NO_USER); + } else if (validation == 0) { + printf(SUCCESS); + } + } + + close(fds[1]); + }",, +63c647b05f004bf4898cf5d6,2023-03-07T00:12:39.831Z,Question from Code,v1,"this program uses a different file called validate, which validates the inputed password and username. The input (from standard input) is in this form: file_name \n username \n password. But when I run my program (which forks a child and pipes the input to the child), stdin looks like this: file_name \n username \n password \n \n. Why? +","int fds[2]; +pipe(fds) == -1; + +int n = fork(); +if (n == 0) { + // child process + dup2(fds[0], STDOUT_FILENO); + close(fds[1]); // closed unused end + execl(""./validate"", ""validate"", NULL); + + } else { + // parent process + close(fds[0]); + write(fds[1], user_id, 10); // hardcoded MAXPASSWORD + write(fds[1], password, 10); + int status; + + wait(&status); + if (WIFEXITED(status)) { // checks id process exits normally + int validation = WEXITSTATUS(status); + if (validation == 0) { + printf(SUCCESS); + } + } + + close(fds[1]); + }",, +63c647b05f004bf4898cf5d6,2023-03-07T00:39:01.946Z,Question from Code,v1," When the parent process exits, why does the shell print the prompt again, causing the shell to read from stdin? +","int fds[2]; +pipe(fds) == -1; + +int n = fork(); +if (n == 0) { + // child process + dup2(fds[0], STDOUT_FILENO); + close(fds[1]); // closed unused end + execl(""./validate"", ""validate"", NULL); + + } else { + // parent process + close(fds[0]); + write(fds[1], user_id, 10); // hardcoded MAXPASSWORD + write(fds[1], password, 10); + int status; + + wait(&status); + if (WIFEXITED(status)) { // checks id process exits normally + int validation = WEXITSTATUS(status); + if (validation == 0) { + printf(SUCCESS); + } + } + + close(fds[1]); + }",, +63c647b05f004bf4898cf5d6,2023-03-07T00:40:03.380Z,Question from Code,v1,"When the parent process exits, why does the shell print the prompt again, causing the shell to read from stdin? +","int fds[2]; + if (pipe(fds) == -1) { + return -1; + } + + int n = fork(); + if (n == -1) { + return -1; + } else if (n == 0) { + // child process + close(fds[1]); // closed unused end + dup2(fds[0], STDOUT_FILENO); + execl(""./validate"", ""validate"", NULL); + close(fds[0]); + + } else { + // parent process + close(fds[0]); + write(fds[1], user_id, 10); // hardcoded MAXPASSWORD + write(fds[1], password, 10); + close(fds[1]); + int status; + + if(wait(&status) == -1) { + return -1; + } else if (WIFEXITED(status)) { // checks id process exits normally + int validation = WEXITSTATUS(status); + if (validation == 0) { + printf(SUCCESS); + } + } + } + + return 0;",, +63c647b05f004bf4898cf5d6,2023-03-07T00:41:42.585Z,Question from Code,v1,"When the parent process exits, how do you keep shell from printing the prompt again, causing the shell to read from stdin? +","int fds[2]; + if (pipe(fds) == -1) { + return -1; + } + + int n = fork(); + if (n == -1) { + return -1; + } else if (n == 0) { + // child process + close(fds[1]); // closed unused end + dup2(fds[0], STDOUT_FILENO); + execl(""./validate"", ""validate"", NULL); + close(fds[0]); + + } else { + // parent process + close(fds[0]); + write(fds[1], user_id, 10); // hardcoded MAXPASSWORD + write(fds[1], password, 10); + close(fds[1]); + int status; + + if(wait(&status) == -1) { + return -1; + } else if (WIFEXITED(status)) { // checks id process exits normally + int validation = WEXITSTATUS(status); + if (validation == 0) { + printf(SUCCESS); + } + } + } + + return 0;",, +63c647b05f004bf4898cf5d6,2023-03-07T00:43:50.283Z,Question from Code,v1,"this program uses a different file called validate, which validates the inputed password and username. The input (from standard input) is in this form: file_name \n username \n password. But when I run my program (which forks a child and pipes the input to the child), stdin looks like this: file_name \n username \n password \n \n. Why does this happen, and how do I mitigate it? +","int fds[2]; + if (pipe(fds) == -1) { + return -1; + } + + int n = fork(); + if (n == -1) { + return -1; + } else if (n == 0) { + // child process + close(fds[1]); // closed unused end + dup2(fds[0], STDOUT_FILENO); + execl(""./validate"", ""validate"", NULL); + close(fds[0]); + + } else { + // parent process + close(fds[0]); + write(fds[1], user_id, 10); // hardcoded MAXPASSWORD + write(fds[1], password, 10); + close(fds[1]); + int status; + + if(wait(&status) == -1) { + return -1; + } else if (WIFEXITED(status)) { // checks id process exits normally + int validation = WEXITSTATUS(status); + if (validation == 0) { + printf(SUCCESS); + } + } + } + + return 0;",, +63c647b05f004bf4898cf5d6,2023-03-07T03:33:29.203Z,Question from Code,v1,why is it the validate file asks for input from stdin in the form: \n \n but this code asks for input in the form: \n \n \n \n \n? How do I fix this?,"#include +#include +#include +#include + +// NOTE: MAX_PASSWORD must be less than MAXLINE/2 +#define MAXLINE 32 +#define MAX_PASSWORD 10 + +#define PASSWORD_FILE ""pass.txt"" + +/* Reads two chunks from stdin, and checks if they match a user id + * and password pair from a password file. The first chunk (MAX_PASSWORD bytes) + * will contain a user id, and the second chunk (MAX_PASSWORD bytes) will + * contain a password. + * + * The program exits with a value of + * 0 if the user id and password match, + * 1 if there is an error, + * 2 if the user id is found but the password does not match, and + * 3 if the user id is not found in the password file. + */ + + +/* Note that read will read at most MAX_PASSWORD bytes. This means that the + * longest password we can support is MAX_PASSWORD. We can't count on the + * caller to send the null termination character, so we have to ensure + * that we terminate the string that is sent anyway. + * + * If the input is coming from the console/keyboard, then the write calls + * are managed by the shell which will process each piece of input + * separately. + * However, if the input is coming from a pipe, there is no guarantee that + * two writes generated by checkpasswd will send the data such that it + * will be read by the two reads below. The means that it is important for + * checkpasswd to send exactly as many bytes that validate is expecting. + * Otherwise the behaviour may be unpredicatable. + * + * This also means that checkpasswd should report an invalid password + * or no user_id if the user input is longer than MAX_PASSWORD. + */ + +int main(void){ + int n, user_length; + char userid[MAXLINE]; + char password[MAXLINE]; + + if((n = read(STDIN_FILENO, userid, MAX_PASSWORD)) == -1) { + perror(""read""); + exit(1); + } else if(n == 0) { + fprintf(stderr, ""Error: could not read from stdin""); + exit(1); + } + + // Make sure user id is null-terminated + if(n <= MAX_PASSWORD) { + userid[n] ='\0'; + } + + // Remove newline character if it exists + char *newline; + if((newline = strchr(userid, '\n')) != NULL) { + *newline = '\0'; + } + + if((n = read(STDIN_FILENO, password, MAX_PASSWORD)) == -1) { + perror(""read""); + exit(1); + } else if(n == 0) { + fprintf(stderr, ""Error: could not read from stdin""); + exit(1); + } + + // Make sure password is null-terminated + if(n <= MAX_PASSWORD) { + password[n] ='\0'; + } + + // Remove newline character if it exists + if((newline = strchr(password, '\n')) != NULL) { + *newline = '\0'; + } + + + // Prepare the userid:password string to compare to password file entries. + // We expect userid to have enough space to concatenate "":"" + password + // but we will play it safe and use strncat + strncat(userid, "":"", MAXLINE - strlen(userid) - 1); + user_length = strlen(userid); + strncat(userid, password, MAXLINE - strlen(userid) - 1); + + FILE *fp = fopen(PASSWORD_FILE, ""r""); + if(!fp) { + perror(""fopen""); + exit(1); + } + + char line[MAXLINE]; + while(fgets(line, sizeof(line) - 1, fp)) { + line[strlen(line) - 1] = '\0'; + if (strcmp(userid, line) == 0) { + fclose(fp); + exit(0); // found match + } + else if(strncmp(userid, line, user_length) == 0) { + fclose(fp); + exit (2); // invalid password + } + } + fclose(fp); + exit(3); // no such user +} +",, +63c647b05f004bf4898cf5d6,2023-03-07T03:36:23.083Z,Question from Code,v1,"why is it the validate file asks for input from stdin in the form: \n \n but this code asks for input in the form: \n \n \n \n \n? How do I fix this? +","#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + // TODO + // checking length + if (strlen(user_id) > 10) { + printf(NO_USER); + exit(1); + } + if (strlen(password) > 10) { + printf(INVALID); + exit(1); + } + + // the hard shit + int fds[2]; + if (pipe(fds) == -1) { + exit(1); + } + + int n = fork(); + if (n < 0) { + exit(1); + } else if (n == 0) { + // child process + dup2(fds[0], STDOUT_FILENO); + close(fds[0]); + close(fds[1]); + execl(""./validate"", ""validate"", NULL); + exit(1); + } else { + // parent process + close(fds[0]); + write(fds[1], user_id, 10); // hardcoded MAXPASSWORD + write(fds[1], password, 10); + close(fds[1]); + + int status; + if(wait(&status) == -1){ + exit(1); + } + + if (WIFEXITED(status)) { // checks id process exits normally + int validation = WEXITSTATUS(status); + if (validation == 0) { + printf(""%s"", SUCCESS); + } else if (validation == 1) { + // error + exit(1); + } else if (validation == 2) { + printf(""%s"", INVALID); + } else if (validation == 3) { + printf(""%s"", NO_USER); + } + } else { + exit(1); + } + } + return 0; +}",, +63c647b05f004bf4898cf5d6,2023-03-07T03:40:05.260Z,Help Fix Code,v1,,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + // TODO + // checking length + if (strlen(user_id) > 10) { + printf(NO_USER); + exit(1); + } + if (strlen(password) > 10) { + printf(INVALID); + exit(1); + } + + // the hard shit + int fds[2]; + if (pipe(fds) == -1) { + exit(1); + } + + int n = fork(); + if (n < 0) { + exit(1); + } else if (n == 0) { + // child process + dup2(fds[0], STDOUT_FILENO); + close(fds[0]); + close(fds[1]); + execl(""./validate"", ""validate"", NULL); + exit(1); + } else { + // parent process + close(fds[0]); + write(fds[1], user_id, 10); // hardcoded MAXPASSWORD + write(fds[1], password, 10); + close(fds[1]); + + int status; + if(wait(&status) == -1){ + exit(1); + } + + if (WIFEXITED(status)) { // checks id process exits normally + int validation = WEXITSTATUS(status); + if (validation == 0) { + printf(""%s"", SUCCESS); + } else if (validation == 1) { + // error + exit(1); + } else if (validation == 2) { + printf(""%s"", INVALID); + } else if (validation == 3) { + printf(""%s"", NO_USER); + } + } else { + exit(1); + } + } + return 0; +}","why is it the validate file asks for input from stdin in the form: \n \n but this code asks for input in the form: \n \n \n \n \n, which causes the program to take in the last 2 lines (which are blank) as inputs. How do I fix this?", +63c647b05f004bf4898cf5d6,2023-03-07T03:41:22.290Z,Help Fix Code,v1,,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + */ + + if (strlen(user_id) > 10) { + printf(NO_USER); + exit(1); + } + if (strlen(password) > 10) { + printf(INVALID); + exit(1); + } + + int fds[2]; + if (pipe(fds) == -1) { + exit(1); + } + + int n = fork(); + if (n < 0) { + exit(1); + } else if (n == 0) { + // child process + dup2(fds[0], STDOUT_FILENO); + close(fds[0]); + close(fds[1]); + execl(""./validate"", ""validate"", NULL); + exit(1); + } else { + // parent process + close(fds[0]); + write(fds[1], user_id, 10); // hardcoded MAXPASSWORD + write(fds[1], password, 10); + close(fds[1]); + + int status; + if(wait(&status) == -1){ + exit(1); + } + + if (WIFEXITED(status)) { // checks id process exits normally + int validation = WEXITSTATUS(status); + if (validation == 0) { + printf(""%s"", SUCCESS); + } else if (validation == 1) { + // error + exit(1); + } else if (validation == 2) { + printf(""%s"", INVALID); + } else if (validation == 3) { + printf(""%s"", NO_USER); + } + } else { + exit(1); + } + } + return 0; +}","why is it the validate file asks for input from stdin in the form: \n \n but this code asks for input in the form: \n \n \n \n \n, which causes the program to take in the last 2 lines (which are blank) as inputs. How do I fix this?", +63c647b05f004bf4898cf5d6,2023-03-09T19:11:11.358Z,Question from Code,v2,"what would this print +","int main() { + char *str; + strncat(str, ""hello"", 3); + printf(""%s\n"", str); + + return 0; +}",, +63c647b05f004bf4898cf5d6,2023-03-09T19:12:26.773Z,Question from Code,v2, what would this print,"int main() { + char *str; + strncpy(str, ""hello"", 3); + printf(""%s\n"", str); + + return 0; +}",, +63c647b05f004bf4898cf5d6,2023-03-09T19:35:51.956Z,General Question,v2,"how to use strtok() +",,, +63c647b05f004bf4898cf5d6,2023-03-10T00:14:17.612Z,Question from Code,v2,"is the tab ('\t') included in the first token? If so, how do I get rid of it? +","int main () { + char str[100] = ""\tgcc -Wall -g -std=gnu99 -c shit.c""; + const char s[2] = "" ""; + char *token; + + /* get the first token */ + token = strtok(str, s); + + /* walk through other tokens */ + while(token != NULL ) { + printf( ""\n%s"", token ); + token = strtok(NULL, s); + } + return(0); +}",, +63c647b05f004bf4898cf5d6,2023-03-10T00:17:13.711Z,General Question,v2,"if you tokenize a str, what is strlen(token)?",,, +63c647b05f004bf4898cf5d6,2023-03-10T00:21:31.867Z,Help Write Code,v2,,,,how to get number of tokens from strtok +63c647b05f004bf4898cf5d6,2023-03-10T00:29:30.833Z,Question from Code,v2,does this give me the str removing '\t'?,"strchr(tokens, '\t') + 1; ",, +63c647b05f004bf4898cf5d6,2023-03-10T01:18:52.676Z,General Question,v2,"if you have array = char[][], what does strlen(array) give you",,, +63c647b05f004bf4898cf5d6,2023-03-10T01:19:39.492Z,Question from Code,v2,"If you have double array = char[x][y], how do you find the x number?","strchr(tokens, '\t') + 1; ",, +63c647b05f004bf4898cf5d6,2023-03-10T01:20:06.333Z,General Question,v2,"If you have double array = char[x][y], how do you find the x number?",,, +63c647b05f004bf4898cf5d6,2023-03-10T01:23:46.128Z,Help Write Code,v2,,,,c how to print 2d array when you dont know the dimensions +63c647b05f004bf4898cf5d6,2023-03-10T01:28:26.739Z,Help Write Code,v2,,,,"how do I while loop through a 2d char array to print each word in the array +" +63c647b05f004bf4898cf5d6,2023-03-10T01:42:15.102Z,Help Write Code,v2,,,,how do I assign space for 2d array using malloc +63c647b05f004bf4898cf5d6,2023-03-10T01:43:36.468Z,Question from Code,v2,why does the *char give error,char **args = malloc(sizeof(*char) * len); ,, +63c647b05f004bf4898cf5d6,2023-03-10T01:53:17.449Z,Question from Code,v2,why does this give error and how do I fix it?,"fread(line, MAXLINE, sizeof(char), fp); + while (line != EOF) {",, +63c647b05f004bf4898cf5d6,2023-03-10T03:50:19.156Z,Question from Code,v2,"why does this line of code give error +","char *tokens = strtok(rest_of_line, ' '); ",, +63c647b05f004bf4898cf5d6,2023-03-10T04:08:32.288Z,Question from Code,v2,why does this give error,"char *line; +fgets(line, MAXLINE, fp); ",, +63c647b05f004bf4898cf5d6,2023-03-10T04:33:49.233Z,General Question,v2,how to run gdb for a file that doesn't have a main function? And how do I use gdb with input files (the code I want to debug needs an input file)?,,, +63c647b05f004bf4898cf5d6,2023-03-10T04:39:16.872Z,General Question,v2,"wen I use gdb: ""gdb pmake.c < handout_example/handout.mk"" I get there error: ""not in executable format: file format not recognized"". ",,, +63c647b05f004bf4898cf5d6,2023-03-10T04:41:23.948Z,General Question,v2,"when I use gdb: ""gdb pmake < handout_example/handout.mk"" I get there error: ""not in executable format: file format not recognized"".",,, +63c647b05f004bf4898cf5d6,2023-03-10T04:41:52.639Z,General Question,v2," when I use gdb: ""gdb pmake < handout_example/handout.mk"" I get there error: ""not in executable format: file format not recognized"". pmake has already been compiled by gcc",,, +63c647b05f004bf4898cf5d6,2023-03-10T04:42:15.622Z,General Question,v2," when I use gdb: ""gdb pmake < handout_example/handout.mk"" I get there error: ""not in executable format: file format not recognized"". pmake has already been compiled by gcc -g",,, +63c647b05f004bf4898cf5d6,2023-03-11T01:25:41.063Z,Question from Code,v2,should strncpy's last argument be strlen(token) or strlen(token) + 1 if strlen(token) = number of letters I want to copy (not include null terminator),"char **args = mallo(sizeof(char *) * len); + +for (int i = 0; i < len; i++) { + args[i] = malloc(sizeof(char) * strlen(token) + 1); + strncpy(args[i], token, strlen(token)); +}",, +63c647b05f004bf4898cf5d6,2023-03-11T01:39:13.392Z,General Question,v2,how do I check if 2 chars are equal (I want to check if the first char in a string is a tab),,, +63c647b05f004bf4898cf5d6,2023-03-11T01:54:31.075Z,Question from Code,v2,"Assume all function calls from nonstandard c libraries are helpers that work perfectly. This program is meant to create Node structures to organize make files. +There's a function that prints the structure in a format to mimic a make file on standard output. +But when I call on the corresponding print function in my terminal, it just keeps asking for input and I can't exit out (I have to kill terminal). What am I doing wrong?","Rule *parse_file(FILE *fp) { + Rule *target_cur; + Rule *target_head = NULL; + Rule *other_head = NULL; + char line[MAXLINE]; + fgets(line, MAXLINE, fp); + while (line != NULL) { + if (is_comment_or_empty(line) == 1) { + // line is empty or is a comment + } else if (strchr(line, ':') != NULL) { + // line contains start of rule (target & prerequisites) + // target: + target_cur = check_target(target_head, other_head, line); + + // dependencies + check_dependencies(target_cur, target_head, other_head, strchr(line, ':') + 1); + // curr is still pointed to the same rule (as it should) + + } else if (strncmp(line, ""\t"", sizeof(char)) == 0) { + // line starts with tab, AKA contains actions (should mitigate the \r\n issues, I had 'else' before) + char *tokens = strtok(line, "" ""); + int len = 0; + while (tokens != NULL) { + len++; + strtok(NULL, "" ""); + } + check_actions(target_cur, line, len - 1); // len - 1 cause of the tab = first token + } + } + return target_head; +}",, +63c647b05f004bf4898cf5d6,2023-03-11T03:26:37.243Z,Question from Code,v2,why does this code cause a segmentation fault?," +void check_dependencies(Rule *curr, Rule *target_head, Rule *other_head, char *line) { + // line passed in starts at space right after ':' + char *token = strtok(line, "" ""); + while (token != NULL) { + Dependency *new = malloc(sizeof(Dependency)); + Dependency *curr_dep; + + if (target_contains(target_head, token) != NULL) { + new->rule = target_contains(target_head, token); + } else if (other_contains(other_head, token) != NULL) { + new->rule = other_contains(other_head, token); + } else { + // this target doesn't exist yet, create an 'other' Rule and attach to end of other lst + Rule *new_rule = malloc(sizeof(Rule)); + Rule *curr_other = other_head; + new_rule->actions = NULL; + new_rule->dependencies = NULL; + new_rule->next_rule = NULL; + new_rule->target = malloc(sizeof(char) * (strlen(token) + 1)); + strncpy(new_rule->target, token, strlen(token) + 1); + + // attach to end of 'other' list + while (curr_other->next_rule != NULL) { + curr_other = curr_other->next_rule; + } + curr_other->next_rule = new_rule; + } + + if (curr->dependencies == NULL) { + curr->dependencies = new; + curr_dep = new; + } else { + curr_dep->next_dep = new; + curr_dep = new; + } + + token = strtok(NULL, "" ""); + } +}",, +63c647b05f004bf4898cf5d6,2023-03-11T03:37:54.478Z,Question from Code,v2,"Assuming curr cannot be NULL, why does this code cause a segmentation fault?"," +void check_dependencies(Rule *curr, Rule *target_head, Rule *other_head, char *line) { + // line passed in starts at space right after ':' + char *token = strtok(line, "" ""); + while (token != NULL) { + Dependency *new = malloc(sizeof(Dependency)); + Dependency *curr_dep; + + if (target_contains(target_head, token) != NULL) { + new->rule = target_contains(target_head, token); + } else if (other_contains(other_head, token) != NULL) { + new->rule = other_contains(other_head, token); + } else { + // this target doesn't exist yet, create an 'other' Rule and attach to end of other lst + Rule *new_rule = malloc(sizeof(Rule)); + Rule *curr_other = other_head; + new_rule->actions = NULL; + new_rule->dependencies = NULL; + new_rule->next_rule = NULL; + new_rule->target = malloc(sizeof(char) * (strlen(token) + 1)); + strncpy(new_rule->target, token, strlen(token) + 1); + + // attach to end of 'other' list + while (curr_other->next_rule != NULL) { + curr_other = curr_other->next_rule; + } + curr_other->next_rule = new_rule; + } + + if (curr->dependencies == NULL) { + curr->dependencies = new; + curr_dep = new; + } else { + curr_dep->next_dep = new; + curr_dep = new; + } + + token = strtok(NULL, "" ""); + } +}",, +63c647b05f004bf4898cf5d6,2023-03-11T03:47:39.425Z,Question from Code,v2,"Assuming curr cannot be NULL, why does this code cause a segmentation fault?"," +void check_dependencies(Rule *curr, Rule *target_head, Rule *other_head, char *line) { + // line passed in starts at space right after ':' + char *token = strtok(line, "" ""); + while (token != NULL) { + Dependency *new = malloc(sizeof(Dependency)); + Dependency *curr_dep; + + if (target_contains(target_head, token) != NULL) { + new->rule = target_contains(target_head, token); + } else if (other_contains(other_head, token) != NULL) { + new->rule = other_contains(other_head, token); + } else { + // this target doesn't exist yet, create an 'other' Rule and attach to end of other lst + Rule *new_rule = malloc(sizeof(Rule)); + Rule *curr_other = other_head; + new_rule->actions = NULL; + new_rule->dependencies = NULL; + new_rule->next_rule = NULL; + new_rule->target = malloc(sizeof(char) * (strlen(token) + 1)); + strncpy(new_rule->target, token, strlen(token) + 1); + + if (curr_other == NULL) { + other_head = new_rule; + } else { + while (curr_other->next_rule != NULL) { + curr_other = curr_other->next_rule; + } + curr_other->next_rule = new_rule; + } + } + + if (curr->dependencies == NULL) { + curr->dependencies = new; + curr_dep = new; + } else { + curr_dep->next_dep = new; + curr_dep = new; + } + + token = strtok(NULL, "" ""); + } +}",, +63c647b05f004bf4898cf5d6,2023-03-11T03:56:20.780Z,General Question,v2,"when I call strtok twice in a row on the same line, it gives me segmentation fault the second time around. How to I mitigate this so I can call strtok again?",,, +63c647b05f004bf4898cf5d6,2023-03-11T04:10:33.746Z,Question from Code,v2,"the first print statement prints but the following print statements don't instead, the terminal screen goes blank. What causes this error?","if (strncmp(line, ""\t"", sizeof(char)) == 0) { + printf(""enter into action line \n""); + // line starts with tab, AKA contains actions (should mitigate the \r\n issues, I had 'else' before) + char *copy = strdup(line); + char *tokens = strtok(copy, "" ""); + int len = 0; + while (tokens != NULL) { + printf(""token: %s\n"", tokens); + len++; + strtok(NULL, "" ""); + } + printf(""number of arguments: %d \n"", len); + check_actions(target_cur, line, len - 1); // len - 1 cause of the tab = first token + printf(""first argument: %s \n"", target_cur->actions->args[0]); +}",, +63c647b05f004bf4898cf5d6,2023-03-11T04:12:15.018Z,General Question,v2,what function can I use to count the number of tokens there are after calling strtok,,, +63c647b05f004bf4898cf5d6,2023-03-11T04:13:25.396Z,General Question,v2,is there a function that returns the number occurences of a char within a string? ,,, +63c647b05f004bf4898cf5d6,2023-03-11T04:20:28.490Z,Question from Code,v2,"Assuming curr cannot be null, what might be causing a segmentation error?"," +void check_actions(Rule *curr, char *line, int len) { + Action *new_action = malloc(sizeof(Action)); + new_action->next_act = NULL; + Action *cur_action = NULL; + if (curr->dependencies == NULL) { + curr->actions = new_action; + cur_action = new_action; + } else { + cur_action->next_act = new_action; + cur_action = new_action; + } + char *copy = strdup(line); + char *token = strtok(copy, "" ""); + char **a = malloc(sizeof(char *) * len); + int i = 0; + + while (token != NULL) { + if (strcmp(token, ""\t"") != 0) { + // AKA not a tab, update args + a[i] = malloc(sizeof(char) * (strlen(token) + 1)); + strncpy(a[i], token, strlen(token) + 1); + } + i++; + strtok(NULL, "" ""); + } + + cur_action->args = a; +}",, +63c647b05f004bf4898cf5d6,2023-03-11T04:24:38.691Z,Question from Code,v2,"Assuming curr cannot be null, what might be causing a segmentation error?"," +void check_actions(Rule *curr, char *line, int len) { + Action *new_action = malloc(sizeof(Action)); + new_action->next_act = NULL; + Action *cur_action = NULL; + if (curr->actions == NULL) { + curr->actions = new_action; + cur_action = new_action; + } else { + cur_action->next_act = new_action; + cur_action = new_action; + } + char *copy = strdup(line); + char *token = strtok(copy, "" ""); + char **a = malloc(sizeof(char *) * len); + int i = 0; + + while (token != NULL) { + if (strcmp(token, ""\t"") != 0) { + // AKA not a tab, update args + a[i] = malloc(sizeof(char) * (strlen(token) + 1)); + strncpy(a[i], token, strlen(token) + 1); + } + i++; + token = strtok(NULL, "" ""); + } + + cur_action->args = a; +}",, +63c647b05f004bf4898cf5d6,2023-03-11T04:37:27.649Z,General Question,v2,"I run a file with terminal command: ./pmake -f handout.mk -o, where -f and -o are defined in pmake.c, handout.mk is a file in the same folder, and pmake is compiled from pmake.c +How do I run gdb to test this exact input?",,, +63c647b05f004bf4898cf5d6,2023-03-11T04:51:57.749Z,General Question,v2,"im running gdb and when I write display where variable is a string from a file, gdb displays it in binary. Is there a way to have gdb display the variable in human readable text? +",,, +63c647b05f004bf4898cf5d6,2023-03-11T05:06:46.532Z,General Question,v2,"in gdb, how do I go back a line?",,, +63c647b05f004bf4898cf5d6,2023-03-11T05:44:22.585Z,Question from Code,v2,"I'm trying to remove the rest of the string line starting from '\r', but this gives me warnings when I compile. Why, and how do I fix this?","char *ptr = strchr(line, '\r'); +ptr = '\0';",, +63c647b05f004bf4898cf5d6,2023-03-11T05:47:20.198Z,Question from Code,v2,"when I run the code this is the warning that pops up in gcc +warning: null argument where non-null required (argument 2) [-Wnonnull] + 206 | strcpy(ptr, '\0'); +what does it mean and how do i fix this?","char *ptr = strchr(line, '\r'); +strcpy(ptr, '\0');",, +63c647b05f004bf4898cf5d6,2023-03-11T06:13:04.654Z,Question from Code,v2,"why doesn't this code print ""outside 7"" to standard error?","int x = 5; + +void handler(int sig) { + x += 3; + fprintf(stderr, ""inside %d "", x); +} + +int main() { + fprintf(stderr, ""start ""); + // POSITION A + struct sigaction act; + act.sa_handler = handler; + act.sa_flags = 0; + sigemptyset(&act.sa_mask); + sigaction(SIGINT, &act, NULL); + + // POSITION B + x += 2; + + // POSITION C + fprintf(stderr, ""outside %d"", x); + + return 0; +}",, +63c647b05f004bf4898cf5d6,2023-03-11T15:33:17.753Z,General Question,v2,"how do I remove whitespace from a string +",,, +63c647b05f004bf4898cf5d6,2023-03-11T15:33:55.273Z,General Question,v2,how do I remove whitespace from a string without strtok,,, +63c647b05f004bf4898cf5d6,2023-03-12T05:24:59.290Z,General Question,v2,"how to use struct stat to compare the last modified times of two files +",,, +63c647b05f004bf4898cf5d6,2023-03-12T20:45:39.270Z,Question from Code,v2,"line 3 gives ""warning: passing argument 1 of ‘stat’ from incompatible pointer type [-Wincompatible-pointer-types]"" error when I compile it on gcc. What causes this problem and how do I fix it?","Dependency *dep_cur = rule->dependencies; +struct stat check_stat; +stat(dep_cur, &check_stat);",, +63c647b05f004bf4898cf5d6,2023-03-12T20:55:38.337Z,Question from Code,v2,"the code is meant to execute rules from a make file in parallel by having the parent create a child for each dependency. When compiled, the perror on line 29 runs. Why, and how do I fix this?","void parallel_eval(Rule *rule) { + Dependency *dep_cur = rule->dependencies; + int parentPID = getpid(); + while (dep_cur != NULL) { + // create a child proccess for each dependency + if (parentPID == getpid()) { + // parent + int n = fork(); + if (n < 0) { + perror(""Error: forking\n""); + exit(1); + } + if (n == 0) { + // child + parallel_eval(dep_cur->rule); + } + } + dep_cur = dep_cur->next_dep; + } + if (parentPID == getpid()) { + int status; + wait(&status); + if (status < 0) { + perror(""Error: wait \n""); + exit(1); + } + if (WIFEXITED(status)) { // checks id process exits normally + if (WEXITSTATUS(status) != 0) { + perror(""Error: child exit incorrectly""); + exit(1); + } else if (WEXITSTATUS(status) == 1) { + Action *act_cur = rule->actions; + while (act_cur != NULL) { + execute(act_cur); + act_cur = act_cur->next_act; + } + } + } + } else { + exit(compare_time(rule)); // check on this line ??? + } +}",, +63c647b05f004bf4898cf5d6,2023-03-12T23:48:43.411Z,General Question,v2,I tried logging in to a linux server and this error popped up on the terminal : PTY allocation request failed on channel 0. Any idea what caused this?,,, +63c647b05f004bf4898cf5d6,2023-03-13T00:22:28.136Z,General Question,v2,"I have a program that contains two functions. The first function creates a struct variable called other, but doesn't assign it a value. It calls the second function, which gets the struct other passed in through parameters. It creates the struct variable in the heap using malloc and assigns the passed in parameter other to the new struct. But when the second function returns, other goes back to NULL. Why?",,, +63c647b05f004bf4898cf5d6,2023-03-13T00:23:08.959Z,General Question,v2,"Question: I have a program that contains two functions. The first function creates a struct pointer called other, but doesn't assign it a value. It calls the second function, which gets the struct other passed in through parameters. It creates the struct variable in the heap using malloc and assigns the passed in parameter other to the new struct. But when the second function returns, other goes back to NULL. Why? +",,, +63c647b05f004bf4898cf5d6,2023-03-13T01:19:11.454Z,General Question,v2,"I have a program that contains two functions. The first function creates a struct pointer called other and assigns it a value of NULL. It calls the second function, which gets the address of the struct pointer other passed in through its parameters. It creates the struct variable in the heap using malloc and assigns the new struct pointer to the passed in parameter. But when the second function returns, other goes back to NULL immediately. Why? +",,, +63c647b05f004bf4898cf5d6,2023-03-13T04:42:07.523Z,Question from Code,v2,"if the file that rule refers to has never been compiled, what does this function return?","int compare_time(Rule *rule) { + struct stat target_stat; + + Dependency *dep_cur = rule->dependencies; + + if (stat(rule->target, &target_stat) < 0) { + // target file was never executed, so need to run actions + return 1; + } + return 0; +}",, +63c647b05f004bf4898cf5d6,2023-03-13T04:43:49.117Z,Question from Code,v2,assume the file that rule refers to has never been compiled. But the function keeps returning 1 when I run it. What could be causing the problem?,"int compare_time(Rule *rule) { + struct stat target_stat; + + Dependency *dep_cur = rule->dependencies; + + if (stat(rule->target, &target_stat) < 0) { + // target file was never executed, so need to run actions + return 1; + } + while (dep_cur != NULL) { + struct stat check_stat; + stat(dep_cur->rule->target, &check_stat); // set up struct for dependency + struct timespec target_time = target_stat.st_mtim; // get last modified time from target + struct timespec dep_time = check_stat.st_mtim; + if (dep_time.tv_sec > target_time.tv_sec || dep_time.tv_nsec > target_time.tv_nsec) { + // dependency is more up to date, need to execute actions + return 1; + } + dep_cur = dep_cur->next_dep; + } + return 0; +}",, +63c647b05f004bf4898cf5d6,2023-03-13T04:44:21.506Z,Question from Code,v2,"Question: assume the file that rule refers to has never been compiled. But the function keeps returning 0 when I run it. What could be causing the problem? +","int compare_time(Rule *rule) { + struct stat target_stat; + + Dependency *dep_cur = rule->dependencies; + + if (stat(rule->target, &target_stat) < 0) { + // target file was never executed, so need to run actions + return 1; + } + while (dep_cur != NULL) { + struct stat check_stat; + stat(dep_cur->rule->target, &check_stat); // set up struct for dependency + struct timespec target_time = target_stat.st_mtim; // get last modified time from target + struct timespec dep_time = check_stat.st_mtim; + if (dep_time.tv_sec > target_time.tv_sec || dep_time.tv_nsec > target_time.tv_nsec) { + // dependency is more up to date, need to execute actions + return 1; + } + dep_cur = dep_cur->next_dep; + } + return 0; +}",, +63c647b05f004bf4898cf5d6,2023-03-17T05:10:12.141Z,Question from Code,v2,Why do I get this error: think the intro and conclusion of the internal validity section can be combined into the first and last examples. ?,"struct sigaction action; +action.sa_handler = handler; +sigemptyset(&action.sa_mask); +action.sa_flags = 0; +sigaction(SIGPROF, &action, NULL);",, +63c647b05f004bf4898cf5d6,2023-03-17T05:14:18.730Z,Question from Code,v2,"Why do I get this error: ""incomplete type is not allowed"" for the variable ""action""?","struct sigaction action; +action.sa_handler = handler; +sigemptyset(&action.sa_mask); +action.sa_flags = 0; +sigaction(SIGPROF, &action, NULL);",, +63c647b05f004bf4898cf5d6,2023-03-24T23:43:01.618Z,Help Write Code,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. +" +63c647b05f004bf4898cf5d6,2023-03-24T23:43:27.522Z,General Question,v2,"Variable x has been assigned a digit between 1 and 10. How do I write a shell command to calculate x minus 1 and display the result to standard output. +",,, +63c647b05f004bf4898cf5d6,2023-03-24T23:46:42.729Z,General Question,v2,"Variable x has been assigned a digit between 1 and 10. Write a sh shell command to calculate x minus 1 and display the result to standard output. +",,, +63c647b05f004bf4898cf5d6,2023-03-24T23:47:01.216Z,Help Write Code,v2,,,,"Variable x has been assigned a digit between 1 and 10. Write an sh shell command to calculate x minus 1 and display the result to standard output. +" +63c647b05f004bf4898cf5d6,2023-03-24T23:57:42.335Z,General Question,v2,"I'm trying to use expr in shell command but when I use * for multiplication, I get an error. What's the problem?",,, +63c647b05f004bf4898cf5d6,2023-03-25T00:53:17.449Z,General Question,v2,"if i call read(fd, &buf, BUF_SIZE); does read concatonate onto buf or does it overwrite buf?",,, +63c647b05f004bf4898cf5d6,2023-03-25T01:54:47.138Z,Question from Code,v2,should malloc be assigned sizeof(char) * num_read or sizeof(char) * (1 + num_read) ?,"int num_read = read(fd, &buf, BUF_SIZE); +buf[num_read] = '\0'; +users[client_index].username = malloc(sizeof(char) * num_read);",, +63c647b05f004bf4898cf5d6,2023-04-05T02:22:44.445Z,Question from Code,v2,"after using malloc, do I have to explicitly set the name[strlen(username)] = '\0' ? ","char *name = malloc(sizeof(char) * (strlen(username) + 1)); +strncpy(client->username, name, strlen(name));",, +63c647b05f004bf4898cf5d6,2023-04-05T02:27:23.884Z,Question from Code,v2,"the code is supposed to create a string name from the contents of username and allocate memory in the heap. However, when I run the code through gdb, name = """". What is causing this error?","char *name = malloc(sizeof(char) * (strlen(username) + 1)); +name[strlen(username)] = '\0'; +strncpy(client->username, name, strlen(name) + 1);",, +63c647b05f004bf4898cf5d6,2023-04-05T02:27:58.905Z,Question from Code,v2,"the code is supposed to create a string name from the contents of username and allocate memory in the heap. However, when I run the code through gdb, name = """". What is causing this error?","char *name = malloc(sizeof(char) * (strlen(username) + 1)); +name[strlen(username)] = '\0'; +",, +63c647b05f004bf4898cf5d6,2023-04-05T02:29:39.004Z,Question from Code,v2,I want to save the contents of username in the heap using malloc. is this correct?,"client->username = malloc(sizeof(char) * (strlen(username) + 1)); +client->username[strlen(username)] = '\0'; +strncpy(client->username, username, strlen(name) + 1);",, +63c647b05f004bf4898cf5d6,2023-04-05T02:30:26.430Z,Question from Code,v2,I want to save the contents of username in the heap using malloc. is this correct?,"client->username = malloc(sizeof(char) * (strlen(username) + 1)); +client->username[strlen(username)] = '\0'; +strncpy(client->username, username, strlen(username) + 1);",, +63c647b05f004bf4898cf5d6,2023-04-05T03:01:46.818Z,Question from Code,v2,"the code is supposed to take a string and tokenize it such that if the input string = ""hello world here I am"", cmd_argv[0] = ""hello"" +cmd_argv[1] = ""world"" +cmd_argv[2] = ""here I am"" +and if input string = ""hello world"" +cmd_argv[0] = ""hello"" +cmd_argv[1] = ""world"" +cmd_argv[2] = NULL +but for the second example, I still get 3 values. What is the error?","int cmd_argc = 0; +char *next_token = strtok(cmd, DELIM); +while (next_token != NULL && cmd_argc < 3) { + cmd_argv[cmd_argc] = next_token; + cmd_argc++; + if (cmd_argc == 2) { + char *rest = strtok(NULL, """"); + cmd_argv[cmd_argc - 1] = strndup(cmd_argv[cmd_argc - 1], rest - cmd_argv[cmd_argc - 1] - 1); // ??? + cmd_argv[cmd_argc] = rest; + cmd_argc++; + } + next_token = strtok(NULL, DELIM); +}",, +63c647b05f004bf4898cf5d6,2023-04-05T03:04:19.272Z,General Question,v2,"if I write ""print var"" and gdb outputs ""0x0"", is that the equivalent to var = NULL? +",,, +63c647b05f004bf4898cf5d6,2023-04-05T03:21:38.802Z,Question from Code,v2,why does this code give me a buffer overflow error?,"int len = 1; +const User *current = curr; + +while (current != NULL) { + len += strlen(current->name) + 2; + current = current->next; +} + +char *buf = malloc(sizeof(char) * len); +int index = 0; +int add = 0; + +while (curr != NULL) { + add = snprintf(buf + index, len - index, ""%s\r\n"", curr->name); + + if (add >= len - len) { + // buffer overflow (shouldn't happen though) + perror(""Buffer overflow""); + exit(1); + } + index += add; + curr = curr->next; +} +buf[len - 1] = '\0'; +return buf;",, +63c647b05f004bf4898cf5d6,2023-04-05T03:33:11.564Z,Question from Code,v2,"if the date is jan. 1, 2021, what is the value of len?",int len = strlen(asctime(localtime(post->date))),, +63c647b05f004bf4898cf5d6,2023-04-23T15:23:35.174Z,Question from Code,v2," +Does the malloc assignment statement only allocate enough room for the entire string s (except null terminator) or just enough space for a string pointer?","char *s = ""hello world""; +char *t = malloc(sizeof(s));",, +63c647b05f004bf4898cf5d6,2023-04-25T21:00:18.157Z,Help Write Code,v2,,,,"The file /u/ajr/t contains zero or more integers, one per line. Write shell command to output the sum of all of these integers plus 10. (For example, if the file is empty, the output would be 10, or if it contains ‘‘2\n3\n’’ the output would be 15.) +" +63c647b15f004bf4898cf5e0,2023-02-15T17:06:46.815Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + // traverse the linked list and check each user for a matching name + // return the pointer if they exist + + User *node = head; + while (node->next != NULL) { + if (strncmp(node->name, name, MAX_NAME) == 0) { + return node; + } + node = node->next; + } + return NULL; +}","traverse a linked list of User objects and return the pointer to the user with the given name, or NULL if it does not exist", +63c647b15f004bf4898cf5e0,2023-02-15T17:12:31.694Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + // traverse the linked list and check each user for a matching name + // return the pointer if they exist + + User *node = head; + while (node->next != NULL) { + if (strncmp(node->name, name, MAX_NAME) == 0) { + return node; + } + node = node->next; + } + // check the last element of the linked list + if (strncmp(node->name, name, MAX_NAME) == 0) { + return node; + } + return NULL; +}","traverse a linked list of User objects and return the pointer to the user with the given name, or NULL if it does not exist", +63c647b15f004bf4898cf5e0,2023-02-15T17:14:35.086Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + // traverse the linked list and check each user for a matching name + // return the pointer if they exist + + User *node = head; + while (node != NULL) { + if (strncmp(node->name, name, MAX_NAME) == 0) { + return node; + } + node = node->next; + } + return NULL; +}","traverse a linked list of User objects and return the pointer to the user with the given name, or NULL if it does not exist", +63c647b15f004bf4898cf5e0,2023-02-15T17:19:57.130Z,Help Fix Code,v1,,"void list_users(const User *curr) { + // traverse the linked list and print each name using printf + User *node = curr; + while (node != NULL) { + printf(""%s\n"", node->name); + node = node->next; + } +}",traverse a linked list of User objects and print each User's name and a new line, +63c647b15f004bf4898cf5e0,2023-02-15T17:20:54.321Z,Help Fix Code,v1,,"void list_users(const User *curr) { + // traverse the linked list and print each name using printf + const User *node = curr; + while (node != NULL) { + printf(""%s\n"", node->name); + node = node->next; + } +}",traverse a linked list of User objects and print each User's name and a new line, +63c647b15f004bf4898cf5e0,2023-02-15T18:40:14.686Z,Help Fix Code,v1,,"int i = 3; +printf(i);",print an integer , +63c647b15f004bf4898cf5e0,2023-02-15T22:08:32.459Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + // traverse the linked list and check each user for a matching name + // return the pointer if they exist + + const User *node = head; + while (node != NULL) { + if (strncmp(node->name, name, MAX_NAME) == 0) { + return (User *) node; + } + node = node->next; + } + + return NULL; +}","return a pointer to a User object if a User with name exists in a linked list of Users, and NULL if a User with name does not exist in the linked list of Users. ", +63c647b15f004bf4898cf5e0,2023-03-14T20:11:10.051Z,Help Fix Code,v2,add action_pt to the end of the actions list of the current rule," Action *last_act = curr_rule->actions; + while (last_act->next_act != NULL) { + last_act = last_act->next_act; + } + last_act->next_act = action_pt;",, +63c647b15f004bf4898cf5e0,2023-03-15T07:50:53.924Z,Help Fix Code,v2,determine if a file exists,"int file_exists(char *filename) { + FILE *f = fopen(filename, ""r""); + if (f != NULL) { + fclose(f); + return 1; + } + return 0; +}",, +63c647b25f004bf4898cf5e5,2023-01-31T15:10:35.021Z,General Question,v1,How does malloc work?,,, +63c647b25f004bf4898cf5e5,2023-01-31T15:38:36.138Z,Help Fix Code,v1,,"char **build_month_list() { + char jan[] = ""Januray""; + char feb[] = ""February""; + char mar[] = ""March""; + + char **array = malloc(sizeof(jan) * 3); + array[0] = jan; + array[1] = feb; + array[2] = mar; + return array; +} + +int main() { + + // For part 3: Once you've implemented build_month_list, uncomment this code. + + char **months = build_month_list(); + for(int i = 0; i < 3; i++) { + printf(""%s "", months[i]); + } + printf(""\n""); + + for(int i = 0; i < 3; i++) { + months[i][3] = '\0'; + printf(""%s "", months[i]); + } + printf(""\n""); + + + return 0; +}","Prints: +January February March +Jan Feb Mar", +63c647b25f004bf4898cf5e5,2023-01-31T15:46:36.676Z,Help Fix Code,v1,,"char **build_month_list() { + char *jan = malloc(sizeof(char * 8)); + char *feb = malloc(sizeof(char * 9)); + char *mar = malloc(sizeof(char * 6)); + + strcpy(jan, ""January""); + strcpy(feb, ""February""); + strcpy(mar, ""March""); + + + char **array = malloc(sizeof(jan) * 3); + array[0] = jan; + array[1] = feb; + array[2] = mar; + return array; +} + +int main() { + + // For part 3: Once you've implemented build_month_list, uncomment this code. + + char **months = build_month_list(); + for(int i = 0; i < 3; i++) { + printf(""%s "", months[i]); + } + printf(""\n""); + + for(int i = 0; i < 3; i++) { + months[i][3] = '\0'; + printf(""%s "", months[i]); + } + printf(""\n""); + + + return 0; +}","Prints: +January February March +Jan Feb Mar", +63c647b35f004bf4898cf5ef,2023-01-17T20:20:55.400Z,General Question,v1,what kind of question do i ask you,,, +63c647b35f004bf4898cf5ef,2023-01-17T20:21:55.024Z,General Question,v1,how to initialize a string in c,,, +63c647b35f004bf4898cf5ef,2023-01-17T20:31:13.818Z,General Question,v1,"what type is result[0] in +chat *result[2];",,, +63c647b35f004bf4898cf5ef,2023-01-26T11:10:16.106Z,General Question,v1,how can i initialize a pointer to a struct,,, +63c647b55f004bf4898cf603,2023-01-28T18:57:24.379Z,Explain Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +int check_permissions(char *, char *); +// testing to see if edits are working +//jan 26 +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + + return 0; +}",, +63c647b55f004bf4898cf603,2023-01-28T18:58:52.762Z,Help Write Code,v1,,,,"process command line arguments +" +63c647b55f004bf4898cf603,2023-01-29T02:39:13.651Z,Help Write Code,v1,,,,extract each digit of an integer +63c647b55f004bf4898cf603,2023-01-29T14:41:19.554Z,General Question,v1,how to define a string literal,,, +63c647b55f004bf4898cf603,2023-01-29T21:49:00.769Z,Explain Code,v1,,"// TODO: Implement populate_array +/* + * Convert a 9 digit int to a 9 element int array. + */ +int populate_array(int sin, int *sin_array) { + int digits = 0; + int num = sin; + while(num != 0){ + num = num / 10; + digits += 1; + } + if(digits != 9){ + return 1; + } + + int position = 0; + while(sin != 0){ + sin_array[position] = sin % 10; + position += 1; + sin = sin / 10; + } + return 0; +} + +// TODO: Implement check_sin +/* + * Return 0 if the given sin_array is a valid SIN, and 1 otherwise. + */ +int check_sin(int *sin_array) { + // total starts off at 0 + int sin_total = 0; + // loop over it + for (int position = 0; position < 8; position++){ + // if at an even position just multiply that by 1 + if(position % 2 == 0){ + sin_total += sin_array[position]; + } + // if not, multiply it by 2 + else{ + if (((2 * sin_array[position]) / 10) != 0){ + int temp = sin_array[position]; + while(temp != 0){ + sin_total += temp % 10; + temp = temp / 10; + } + } + else{ + sin_total += (2 * sin_array[position]); + } + // if the product is only one digit + // need to add some code to handle if it's 2 digits. + // in this case add the two digits together AND THEN add that to the total. + } + } + // if the total is divisible by 10 it is a valid SIN + if (sin_total % 10 == 0){ + return 0; + } + // else, it's NOT a valid SIN + else{ + return 1; + } +} +",, +63c647b55f004bf4898cf603,2023-01-29T22:33:16.120Z,Explain Code,v1,,"// TODO: Implement populate_array +/* + * Convert a 9 digit int to a 9 element int array. + */ +int populate_array(int sin, int *sin_array) { + int digits = 0; + int position = 0; + while(sin != 0){ + sin_array[position] = sin % 10; + digits += 1; + position += 1; + sin = sin / 10; + } + if (digits != 9){ + return 1; + } + else{ + return 0; + } +} + +// TODO: Implement check_sin +/* + * Return 0 if the given sin_array is a valid SIN, and 1 otherwise. + */ +int check_sin(int *sin_array) { + // total starts off at 0 + int sin_total = 0; + // loop over it + for (int position = 0; position < 8; position++){ + // if at an even position just multiply that by 1 + if(position % 2 == 0){ + sin_total += sin_array[position]; + } + // if not, multiply it by 2 + else{ + if (((2 * sin_array[position]) / 10) != 0){ + int temp = sin_array[position]; + while(temp != 0){ + sin_total += temp % 10; + temp = temp / 10; + } + } + else{ + sin_total += (2 * sin_array[position]); + } + // if the product is only one digit + // need to add some code to handle if it's 2 digits. + // in this case add the two digits together AND THEN add that to the total. + } + } + // if the total is divisible by 10 it is a valid SIN + if (sin_total % 10 == 0){ + return 0; + } + // else, it's NOT a valid SIN + else{ + return 1; + } +} +",, +63c647b55f004bf4898cf603,2023-01-29T22:37:32.858Z,Explain Code,v1,,"int populate_array(int sin, int *sin_array) { + int digits = 0; + int position = 0; + while(sin != 0){ + sin_array[position] = sin % 10; + digits += 1; + position += 1; + sin = sin / 10; + } + if (digits != 9){ + return 1; + } + else{ + return 0; + } +} + + +int check_sin(int *sin_array) { + int sin_total = 0; + for (int position = 0; position < 8; position++){ + if(position % 2 == 0){ + sin_total += sin_array[position]; + } + else{ + if (((2 * sin_array[position]) / 10) != 0){ + int temp = sin_array[position]; + while(temp != 0){ + sin_total += temp % 10; + temp = temp / 10; + } + } + else{ + sin_total += (2 * sin_array[position]); + } + } + } + if (sin_total % 10 == 0){ + return 0; + } + else{ + return 1; + } +} +",, +63c647b55f004bf4898cf603,2023-01-29T23:30:25.280Z,General Question,v1,"how do I check if an element of a character array is 'a', 'b', or 'c'",,, +63c647b55f004bf4898cf603,2023-01-29T23:37:03.573Z,Question from Code,v1,is this the right way to check if characters match," if(required[i] == 'r' || required[i] == 'w' || required[i] == 'x'){ + if (required[i] != permissions_field[i]){ + return 1; + } + } +",, +63c647b55f004bf4898cf603,2023-01-29T23:37:32.499Z,Question from Code,v1,is this checking the memory addresses," if(required[i] == 'r' || required[i] == 'w' || required[i] == 'x'){ + if (required[i] != permissions_field[i]){ + return 1; + } + } +",, +63c647b55f004bf4898cf603,2023-01-29T23:38:11.635Z,Question from Code,v1,what operators are being used in the if statements," if(required[i] == 'r' || required[i] == 'w' || required[i] == 'x'){ + if (required[i] != permissions_field[i]){ + return 1; + } + } +",, +63c647b55f004bf4898cf603,2023-01-30T00:25:53.894Z,Help Write Code,v1,,,,"take two 9-element character arrays as arguments and returns an integer. (The prototype for this function is in the starter code.) The first array will represent the permission field of a file and the second will represent the permissions that are required. The function will return 0 if the file has all the required permissions and 1 otherwise. The arrays do not have to be identical for a 0 to be returned. For example, if the first array holds the characters rwxr-x--- and the second array holds the characters r-x------, the function should return 0. " +63c647b55f004bf4898cf603,2023-01-30T00:30:07.402Z,Explain Code,v1,," if(required[i] == 'r' || required[i] == 'w' || required[i] == 'x'){ + if (required[i] != permissions_field[i]){ + return 1; + } + } +// TODO: Implement a helper named check_permissions that matches the prototype below. +//int check_permissions(char *, char *); +int check_permissions(char *permissions_field, char *required){ + //takes the permissions and required + //if it has all the required permissions + //check where in required it's not a dash and make sure the + // corresponding spot in permissions_field matches + //return 0 + //else + //return 1 + + //go through required + //if the ith element in the array is r or w or x, then + // compare it to the array at the same position in permissins_field + //if the position in both is equal then keep going + //if not then the positions do not match so return 1 + + //loop through required, an array of 9 characters + int i; + for (i = 0; i < 9; i++){ + // check it + if(required[i] != '-'){ + if (required[i] != permissions_field[i]){ + return 1; + } + } + return 0; + } +}",, +63c647b55f004bf4898cf603,2023-01-30T00:32:37.989Z,Help Write Code,v1,,,," // TODO: Process command line arguments. + + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + //check to make sure it's a regular file and not directory + //check size + // check permisions (or vice versa)" +63c647b55f004bf4898cf603,2023-01-30T00:33:29.698Z,Help Write Code,v1,,,,"Process command line arguments. Call check_permissions as part of your solution to count the files to compute and print the correct value. +" +63c647b55f004bf4898cf603,2023-01-30T01:04:11.659Z,Help Write Code,v1,,,,assign the variable p to be the permissions of the ls-l output +63c647b55f004bf4898cf603,2023-01-30T23:11:11.841Z,General Question,v1,how do I add together the two digits of an integer,,, +63c647b55f004bf4898cf603,2023-01-30T23:47:49.133Z,Explain Code,v1,,"/* + * Convert a 9 digit int to a 9 element int array. + */ +int populate_array(int sin, int *sin_array) { + int position = 8; + while(sin != 0){ + sin_array[position] = sin % 10; + sin = sin / 10; + position -= 1; + } + return 0; +}",, +63c647b55f004bf4898cf603,2023-01-30T23:49:17.011Z,Help Fix Code,v1,,"/* + * Convert a 9 digit int to a 9 element int array. + */ +int populate_array(int sin, int *sin_array) { + int position = 8; + while(sin != 0){ + sin_array[position] = sin % 10; + sin = sin / 10; + position -= 1; + } + return 0; +}","turn 9 digit integer into 9 digit array, in same order. +return 1 if the integer is less than 100000000", +63c647b55f004bf4898cf603,2023-01-30T23:51:23.998Z,Help Write Code,v1,,,,get the sum of the two digits of an integer between 10 and 20 +63c647b55f004bf4898cf603,2023-01-30T23:53:23.844Z,Help Fix Code,v1,,"int check_sin(int *sin_array) { + int sin_total = 0; + for(int i = 8; i>=0; i--){ + if(i % 2 == 0){ + sin_total += sin_array[i]; + } + else{ + if(sin_array[i] < 5){ + sin_total += (sin_array[i] * 2); + } + else{ + int temp1 = sin_array[i] % 2; + int temp2 = sin_array[i] / 10; + int add = temp1 + temp2; + sin_total += add; + // int temp = sin_array[i]; + // while(temp != 0){ + // sin_total += (temp % 10); + // temp = temp / 10; + } + } + } + if((sin_total % 10) != 0){ + return 1; + } + return 0; + }","if int i is even, add sin_array[I] to sin_total +if int i is ODD, add the digits of sin_array at the ith position to sin_total", +63c647b55f004bf4898cf603,2023-01-31T02:47:45.595Z,Help Write Code,v1,,,,"Write a loop which traverses a linked list starting at front and prints the value of each node in the list. + +Separate each value with a space." +63c647b55f004bf4898cf603,2023-02-14T16:53:08.396Z,Help Fix Code,v1,,"/* + * 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) { + User *curr = *user_ptr_add; + + // go thru the LL basically and check that no user with name already exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + } + + // if the given name can't fit the name array return -2 + if(strlen(name) >= MAX_NAME){ + return -2; + } + + // now that it's been determined that this User CAN be created: + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + if(*user_ptr_add == NULL){ + *user_ptr_add = new_user; + } + else{ + curr = *user_ptr_add; + while(curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + return 0; +} +",create a new user with the given name and insert it at the tail of the list of users whose head is pointed to by *user_ptr_add, +63c647b55f004bf4898cf603,2023-02-14T16:54:55.080Z,Help Fix Code,v1,,"/* + * 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) { + User *curr = *user_ptr_add; + + // go thru the LL basically and check that no user with name already exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + } + + // if the given name can't fit the name array return -2 + if(strlen(name) >= MAX_NAME){ + return -2; + } + + // now that it's been determined that this User CAN be created: + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + if(*user_ptr_add == NULL){ + *user_ptr_add = new_user; + } + else{ + curr = *user_ptr_add; + while(curr->next != NULL){ + curr = curr->next; + } + curr = curr->next; + curr->next = new_user; + } + return 0; +} +"," * 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)", +63c647b55f004bf4898cf603,2023-02-14T16:58:04.718Z,Help Fix Code,v1,,"/* + * 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) { + User *curr = *user_ptr_add; + + // go thru the LL basically and check that no user with name already exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + } + + // if the given name can't fit the name array return -2 + if(strlen(name) >= MAX_NAME){ + return -2; + } + + // now that it's been determined that this User CAN be created: + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + if(*user_ptr_add == NULL){ + *user_ptr_add = new_user; + } + else{ + curr = *user_ptr_add; + while(curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + return 0; +}"," * 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) + */", +63c647b55f004bf4898cf603,2023-02-14T17:01:12.345Z,Question from Code,v1,does this code effectively go through the entire linked list," // go thru the LL basically and check that no user with name already exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + curr = curr->next; + } + }",, +63c647b55f004bf4898cf603,2023-02-14T17:02:24.273Z,Help Fix Code,v1,," // go thru the LL basically and check that no user with name already exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + }",make this check every node in the linked list, +63c647b55f004bf4898cf603,2023-02-15T01:04:44.101Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + // start at the first user object in the linked list + User *current = head; + // check if the name of every user is this name + while(current->next != NULL){ + // if it is return a pointer to the user + if (strcmp(current->name, name) == 0){ + return *current; + } + } + // if that never happens, return NULL + return NULL; +}",go through the linked list and return a pointer to the User with the given name, +63c647b55f004bf4898cf603,2023-02-15T01:05:41.616Z,Explain Code,v1,,"User *find_user(const char *name, const User *head) { + // start at the first user object in the linked list + User *current = head; + // check if the name of every user is this name + while(current->next != NULL){ + // if it is return a pointer to the user + if (strcmp(current->name, name) == 0){ + return current; + } + } + // if that never happens, return NULL + return NULL; +} +",, +63c647b55f004bf4898cf603,2023-02-15T01:12:09.177Z,Question from Code,v1,how do I Cast a const User *,"/* + * 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) { + // start at the first user object in the linked list + const User *current = head; + // check if the name of every user is this name + while(current->next != NULL){ + // if it is return a pointer to the user + if (strcmp(current->name, name) == 0){ + // User *pointer_to_match = current; + // return pointer_to_match; + return current; + } + } + // if that never happens, return NULL + return NULL; +}",, +63c647b55f004bf4898cf603,2023-02-15T01:14:12.423Z,Explain Code,v1,,"/* + * 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) { + // start at the first user object in the linked list + const User *current = head; + // check if the name of every user is this name + while(current->next != NULL){ + // if it is return a pointer to the user + if (strcmp(current->name, name) == 0){ + // User *pointer_to_match = current; + // return pointer_to_match; + return current; + } + } + // if that never happens, return NULL + return NULL; +}",, +63c647b55f004bf4898cf603,2023-02-15T01:15:20.941Z,Help Fix Code,v1,,"/* + * 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) { + // start at the first user object in the linked list + const User *current = head; + // check if the name of every user is this name + while(current->next != NULL){ + // if it is return a pointer to the user + if (strcmp(current->name, name) == 0){ + // User *pointer_to_match = current; + // return pointer_to_match; + return current; + } + } + // if that never happens, return NULL + return NULL; +}",cast a (const User *) to a (User *) to satisfy prototype without warnings, +63c647b55f004bf4898cf603,2023-02-15T13:25:44.682Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + // find the two users + User *u1 = find_user(name1, head); + User *u2 = find_user(name2, head); + + // make sure that neither of them is NULL aka they both do exist + if(u1 == NULL || u2 == NULL){ + return 4; + } + + // check that the same user has not been passed in twice + if (strcmp(name1, name2) == 0){ + return 3; + } + + //check that they are not already friends + // iterate through each list of pointers to friends (aka other users) + // not covering the case where they're already friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (u1->friends[i] != NULL) { + if (strcmp(u2->friends[i]->name, name2) == 0) { + return 1; + } + } + } + + // last, check that neither has max friends + // check that u1 doesnt + int u1_friends = 0; + int u2_friends = 0; + while(u1->friends[u1_friends] != NULL){ + u1_friends += 1; + } + while(u2->friends[u2_friends] != NULL){ + u2_friends += 1; + } + if (u1_friends >= MAX_FRIENDS || u2_friends >= MAX_FRIENDS){ + return 2; + } + // go thorugh each array of pointers friends to find the first empty spot + // based on the checks already done, we know that there is at least one empty spot + // CERTAIN that i will find an empty spot before reaching the end of the list + // thats why the while loop stopping conditions are what they are + int spot1 = 0; + int spot2 = 0; + while(u1->friends[spot1] != NULL){ + spot1 ++; + } + while(u2->friends[spot2] != NULL){ + spot2 ++; + } + // once the first empty spot in both users' friends have been found + // add u2 to the first empty spot in the friends array of u1 + // not working when a user already has at least one friend!!!! + u1->friends[spot1] = u2; + // vice versa + u2->friends[spot2] = u1; + return 0; +}","if it gets to the end of the code, it is not adding each user to one another's array of pointers to friends (other users)", +63c647b55f004bf4898cf603,2023-02-15T13:29:32.034Z,Explain Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + // find the two users + User *u1 = find_user(name1, head); + User *u2 = find_user(name2, head); + + // make sure that neither of them is NULL aka they both do exist + if(u1 == NULL || u2 == NULL){ + return 4; + } + + // check that the same user has not been passed in twice + if (strcmp(name1, name2) == 0){ + return 3; + } + + //check that they are not already friends + // iterate through each list of pointers to friends (aka other users) + // not covering the case where they're already friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (u1->friends[i] != NULL) { + if (strcmp(u2->friends[i]->name, name2) == 0) { + return 1; + } + } + } + + // last, check that neither has max friends + // check that u1 doesnt + int u1_friends = 0; + int u2_friends = 0; + while(u1->friends[u1_friends] != NULL){ + u1_friends += 1; + } + while(u2->friends[u2_friends] != NULL){ + u2_friends += 1; + } + if (u1_friends >= MAX_FRIENDS || u2_friends >= MAX_FRIENDS){ + return 2; + } + // go thorugh each array of pointers friends to find the first empty spot + // based on the checks already done, we know that there is at least one empty spot + // CERTAIN that i will find an empty spot before reaching the end of the list + // thats why the while loop stopping conditions are what they are + int spot1 = 0; + int spot2 = 0; + while(u1->friends[spot1] != NULL){ + spot1 ++; + } + while(u2->friends[spot2] != NULL){ + spot2 ++; + } + // once the first empty spot in both users' friends have been found + // add u2 to the first empty spot in the friends array of u1 + // not working when a user already has at least one friend!!!! + u1->friends[spot1] = u2; + // vice versa + u2->friends[spot2] = u1; + return 0; +}",, +63c647b55f004bf4898cf603,2023-02-15T13:32:03.799Z,Explain Code,v1,,"/* + * 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) { + // check that both users exist + // check that the users are friends + // once both those things have been checked, create the new post + Post *new_post = malloc(sizeof(Post)); + + return 0; +}",, +63c647b55f004bf4898cf603,2023-02-15T13:34:52.997Z,Help Fix Code,v1,,"/* + * 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) { + // check that both users exist + // check that the users are friends + // once both those things have been checked, create the new post + Post *new_post = malloc(sizeof(Post)); + + return 0; +}","make a new post from author to target if the users both exist and are friends. +if both these conditions are met, make a new post +", +63c647b55f004bf4898cf603,2023-02-15T14:24:25.674Z,Help Fix Code,v1,,"void list_users(const User *curr) { + // start at the curr user + User *curr_user = (User *) curr; + // keep going through the linked list till the end is reached + while(curr_user != NULL){ + // THIS LINE BELOW IS A PROBLEM! + printf(stdout, ""%s\n"", curr_user->name); + //print_user(curr); + curr_user = curr_user->next; + } +}","/* + * Print the usernames of all users in the list starting at curr. + * Names should be printed to standard output, one per line. + */", +63c647b55f004bf4898cf603,2023-02-15T14:44:27.107Z,Help Fix Code,v1,,"/* + * 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) { + // start at the first user object in the linked list + User *current = (User *) head; + //const User *current = head; + // check if the name of every user is this name + while(current->next != NULL){ + // if it is return a pointer to the user + if (strcmp(current->name, name) == 0){ + // User *pointer_to_match = current; + // return pointer_to_match; + return (User*)current; + } + current = current->next; + } + // if that never happens, return NULL + return NULL; +}",not finding the user if it is the last one in the linked list, +63c647b55f004bf4898cf603,2023-02-15T14:53:29.764Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + // find the two users + User *u1 = find_user(name1, head); + User *u2 = find_user(name2, head); + + // make sure that neither of them is NULL aka they both do exist + if(u1 == NULL || u2 == NULL){ + return 4; + } + + // check that the same user has not been passed in twice + if (strcmp(name1, name2) == 0){ + return 3; + } + + // check that neither has max friends + // check that u1 doesnt + int u1_friends = 0; + int u2_friends = 0; + while(u1->friends[u1_friends] != NULL){ + u1_friends += 1; + } + while(u2->friends[u2_friends] != NULL){ + u2_friends += 1; + } + if (u1_friends >= MAX_FRIENDS || u2_friends >= MAX_FRIENDS){ + return 2; + } + + //check that they are not already friends + // iterate through each list of pointers to friends (aka other users) + // not covering the case where they're already friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (u1->friends[i] != NULL) { + if (strcmp(u1->friends[i]->name, name2) == 0) { + return 1; + } + } + if (u2->friends[i] != NULL){ + if(strcmp(u2->friends[i]->name, name1) == 0){ + return 1; + } + } + } + + // go thorugh each array of pointers friends to find the first empty spot + // based on the checks already done, we know that there is at least one empty spot + // CERTAIN that i will find an empty spot before reaching the end of the list + // thats why the while loop stopping conditions are what they are + int spot1 = 0; + int spot2 = 0; + while(u1->friends[spot1] != NULL){ + spot1 ++; + } + while(u2->friends[spot2] != NULL){ + spot2 ++; + } + // once the first empty spot in both users' friends have been found + // add u2 to the first empty spot in the friends array of u1 + // not working when a user already has at least one friend!!!! + u1->friends[spot1] = u2; + // vice versa + u2->friends[spot2] = u1; + return 0; +}",don't give a segmentation fault when I give a user a second friend, +63c647b55f004bf4898cf603,2023-02-15T14:55:54.447Z,Explain Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + // find the two users + User *u1 = find_user(name1, head); + User *u2 = find_user(name2, head); + + // make sure that neither of them is NULL aka they both do exist + if(u1 == NULL || u2 == NULL){ + return 4; + } + + // check that the same user has not been passed in twice + if (strcmp(name1, name2) == 0){ + return 3; + } + + // check that neither has max friends + // check that u1 doesnt + int u1_friends = 0; + int u2_friends = 0; + while(u1->friends[u1_friends] != NULL){ + u1_friends += 1; + } + while(u2->friends[u2_friends] != NULL){ + u2_friends += 1; + } + if (u1_friends >= MAX_FRIENDS || u2_friends >= MAX_FRIENDS){ + return 2; + } + + //check that they are not already friends + // iterate through each list of pointers to friends (aka other users) + // not covering the case where they're already friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (u1->friends[i] != NULL) { + if (strcmp(u1->friends[i]->name, name2) == 0) { + return 1; + } + } + if (u2->friends[i] != NULL){ + if(strcmp(u2->friends[i]->name, name1) == 0){ + return 1; + } + } + } + + // go thorugh each array of pointers friends to find the first empty spot + // based on the checks already done, we know that there is at least one empty spot + // CERTAIN that i will find an empty spot before reaching the end of the list + // thats why the while loop stopping conditions are what they are + int spot1 = 0; + int spot2 = 0; + while(u1->friends[spot1] != NULL){ + spot1 ++; + } + while(u2->friends[spot2] != NULL){ + spot2 ++; + } + // once the first empty spot in both users' friends have been found + // add u2 to the first empty spot in the friends array of u1 + // not working when a user already has at least one friend!!!! + u1->friends[spot1] = u2; + // vice versa + u2->friends[spot2] = u1; + return 0; +}",, +63c647b55f004bf4898cf603,2023-02-15T21:35:00.694Z,Help Fix Code,v1,,"/* + * 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) { + // check that the user exists + User *print_me = (User *) user; + if (print_me == NULL){ + return 1; + } + printf(""pfp here\n""); + printf(""Name: %s\n"", print_me->name); + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + // go through the friends of this user + for(int i = 0; i < MAX_FRIENDS; i++){ + // make sure that the user at the index is not NULL + if(print_me->friends[i] != NULL){ + // print the name of that friend (who is a User) + printf(""%s\n"", print_me->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts: \n""); + if(print_me->first_post != NULL){ + Post *curr_post = print_me->first_post; + while(curr_post != NULL){ + // do stuff to it here!!!!!! + printf(""From: %s\n"", curr_post->author); + printf(""Date: %s\n"", ctime(curr_post->date)); + printf(""%s\n"", curr_post->contents); + curr_post = curr_post->next; + } + } + 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) { + User *a = (User *) author; + User *t = target; + // check that both users exist + if(a == NULL || t == NULL){ + return 2; + } +// CHECK THAT THEY ARE FRIENDS AND RETURN 1 IF NOT + int are_friends = check_if_friends(a, t); + if(are_friends == 0){ + return 1; + } + // now that it has been verified that they both exist and are friends + // create the new post + time_t *post_time = malloc(sizeof(time_t)); + *post_time = time(post_time); + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->author, a->name); + new_post->contents = contents; + new_post->date = post_time; + new_post->next = NULL; + + // add it in + // if the target user does not have any posts just make it the first post + if(target->first_post == NULL){ + target->first_post = new_post; + } + else { + // otherwise + // store a pointer to the rest of the linked list + Post *rest_of_posts = target->first_post->next; + // make the target's first post point to the memory address of the new_post + target->first_post = new_post; + // make new_post next point to the rest of the linked list of posts + new_post->next = rest_of_posts; + // return 0 upon successfully making the post + } + return 0; +}","when print_user is called, make sure all of the user's posts are printed not just the most recent one", +63c647b55f004bf4898cf603,2023-02-15T21:38:35.189Z,Help Fix Code,v1,," +/* + * 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) { + User *a = (User *) author; + User *t = target; + // check that both users exist + if(a == NULL || t == NULL){ + return 2; + } +// CHECK THAT THEY ARE FRIENDS AND RETURN 1 IF NOT + int are_friends = check_if_friends(a, t); + if(are_friends == 0){ + return 1; + } + // now that it has been verified that they both exist and are friends + // create the new post + time_t *post_time = malloc(sizeof(time_t)); + *post_time = time(post_time); + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->author, a->name); + new_post->contents = contents; + new_post->date = post_time; + new_post->next = NULL; + + // add it in + // if the target user does not have any posts just make it the first post + if(target->first_post == NULL){ + target->first_post = new_post; + } + else { + // otherwise + // store a pointer to the rest of the linked list + Post *rest_of_posts = target->first_post->next; + // make the target's first post point to the memory address of the new_post + target->first_post = new_post; + // make new_post next point to the rest of the linked list of posts + new_post->next = rest_of_posts; + // return 0 upon successfully making the post + } + return 0; +} +",properly store the new post at the beginning of the linked list without losing the rest of the linked list, +63c647b55f004bf4898cf603,2023-02-15T21:45:13.546Z,Help Write Code,v1,,,,how do I print the contents of a file +63c647b55f004bf4898cf603,2023-02-15T21:48:03.553Z,Help Fix Code,v1,,"/* + * 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) { + // check that the user exists + User *print_me = (User *) user; + if (print_me == NULL){ + return 1; + } + printf(""pfp here\n""); + FILE *pfp = fopen(print_me->profile_pic); + printf(""Name: %s\n"", print_me->name); + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + // go through the friends of this user + for(int i = 0; i < MAX_FRIENDS; i++){ + // make sure that the user at the index is not NULL + if(print_me->friends[i] != NULL){ + // print the name of that friend (who is a User) + printf(""%s\n"", print_me->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts: \n""); + if(print_me->first_post != NULL){ + Post *curr_post = print_me->first_post; + while(curr_post != NULL){ + // do stuff to it here!!!!!! + printf(""From: %s\n"", curr_post->author); + printf(""Date: %s\n"", ctime(curr_post->date)); + printf(""%s\n"", curr_post->contents); + curr_post = curr_post->next; + } + } + return 0; +}","print the pfp (profile pic) in the beginning of the code. +open the file containing it and print its contents.", +63c647b55f004bf4898cf603,2023-02-15T22:18:18.323Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + // find the user with that username + User *delete_me = find_user(name, *user_ptr_del); + // if it does not exist, return 1 + if (delete_me == NULL){ + return 1; + } + + // once established that the user that u wanna delete does indeed exist in the + // list pointed to by *user_ptr_delete, start the process of DELETING it + + // start by removing the user from its friends lists of friends + + // remove the posts of the user that's being deleted + if(delete_me->first_post != NULL){ + Post *curr_post = delete_me->first_post; + while(curr_post != NULL){ + Post *next_post = curr_post->next; + free(curr_post->date); + free(curr_post->contents); + free(curr_post); + curr_post = next_post; + } + } + // go through the friends of the user we are deleting + for(int index = 0; index < MAX_FRIENDS; index++){ + // go to every spot in it + User *friend_of_deleted = delete_me->friends[index]; + // if there's a friend in that spot, gotta go delete delete_me from its friends array + if(friend_of_deleted != NULL){ + // go through its list of friends and find delete_me, then change it to null + for(int j = 0; j < MAX_FRIENDS; j++){ + if(friend_of_deleted->friends[j] == delete_me){ + friend_of_deleted->friends[j] = NULL; + break; + } + } + } + + } + + // go through the linked list of users and delete the user u want to delete + // special case if the user we want to delete IS at the beginning of the LL + if(*user_ptr_del == delete_me){ + User *after = delete_me->next; + *user_ptr_del = after; + } + // start at the beginning of the Linked List + User *curr_user = *user_ptr_del; + // find the node aka the user you want to delete + while(curr_user->next != delete_me){ + curr_user = curr_user->next; + } + // once delete_me has been found, store the link to the node (user) after it + User *after_deleted = curr_user->next->next; + // make the node before the one being deleted point to the user after the one being deleted + curr_user->next = after_deleted; + // free memory for the node (user) that's being deleted aka delete_me + free(delete_me); + + return 0; + +}","/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ +make sure there are no memory leaks", +63c647b55f004bf4898cf603,2023-02-15T22:20:43.376Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + // find the user with that username + User *delete_me = find_user(name, *user_ptr_del); + // if it does not exist, return 1 + if (delete_me == NULL){ + return 1; + } + + // once established that the user that u wanna delete does indeed exist in the + // list pointed to by *user_ptr_delete, start the process of DELETING it + + // start by removing the user from its friends lists of friends + + // remove the posts of the user that's being deleted + if(delete_me->first_post != NULL){ + Post *curr_post = delete_me->first_post; + while(curr_post != NULL){ + Post *next_post = curr_post->next; + free(curr_post->date); + free(curr_post->contents); + free(curr_post); + curr_post = next_post; + } + } + // go through the friends of the user we are deleting + for(int index = 0; index < MAX_FRIENDS; index++){ + // go to every spot in it + User *friend_of_deleted = delete_me->friends[index]; + // if there's a friend in that spot, gotta go delete delete_me from its friends array + if(friend_of_deleted != NULL){ + // go through its list of friends and find delete_me, then change it to null + for(int j = 0; j < MAX_FRIENDS; j++){ + if(friend_of_deleted->friends[j] == delete_me){ + friend_of_deleted->friends[j] = NULL; + break; + } + } + } + + } + + // go through the linked list of users and delete the user u want to delete + // special case if the user we want to delete IS at the beginning of the LL + if(*user_ptr_del == delete_me){ + User *after = delete_me->next; + *user_ptr_del = after; + } + // start at the beginning of the Linked List + User *curr_user = *user_ptr_del; + // find the node aka the user you want to delete + while(curr_user->next != delete_me){ + curr_user = curr_user->next; + } + // once delete_me has been found, store the link to the node (user) after it + User *after_deleted = curr_user->next->next; + // make the node before the one being deleted point to the user after the one being deleted + curr_user->next = after_deleted; + // free memory for the node (user) that's being deleted aka delete_me + free(delete_me); + + return 0; + +}",free the posts of this user, +63c647b55f004bf4898cf603,2023-02-15T22:23:01.803Z,Help Write Code,v1,,,,start from the first node of a linked list and free every node +63c647b55f004bf4898cf603,2023-03-03T19:49:14.552Z,Help Fix Code,v1,,"#include +#include +#include + +/* + * Modify the program so that new children do not create additional processes + */ + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",new children should not create additional processes, +63c647b55f004bf4898cf603,2023-03-12T17:19:00.784Z,Explain Code,v2,,"main : linked_list.o main.o gcc - Wall - g - std = gnu99 - + o main linked_list.o main + .o + + main.o + : main.c linked_list.h gcc - Wall - g - std = gnu99 - + c main + .c + + linked_list.o + : linked_list.c linked_list.h gcc - Wall - g - std = gnu99 - + c linked_list.c + +#notice that this target has no dependencies + clean + : rm - f main rm - f linked_list.o rm - f main.o",, +63c647b55f004bf4898cf603,2023-03-12T17:27:54.206Z,Help Write Code,v2,,,,write code to parse through the makefile and make a linked data structure +63c647b55f004bf4898cf603,2023-03-14T01:18:01.368Z,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; +}",, +63c647b55f004bf4898cf603,2023-03-14T01:20:00.760Z,Explain Code,v2,,"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; +}",, +63c647b55f004bf4898cf603,2023-03-14T18:54:38.312Z,General Question,v2,explain strtok to me,,, +63c647b55f004bf4898cf603,2023-03-15T18:27:17.138Z,Help Write Code,v2,,,,turn an action line of a makefile into a char **args +63c647b55f004bf4898cf603,2023-03-15T18:29:25.088Z,Help Fix Code,v2,create a new struct action_node,"typedef struct action_node { + char **args; // An array of strings suitable to be passed to execvp + struct action_node *next_act; +} Action; + + +/* Create a new action node. + * Return a pointer to this action node. + */ +Action *create_action_node(char **args){ + Action *new_action_node = malloc(sizeof(Action)); + return new_action_node; +}",, +63c647b55f004bf4898cf603,2023-03-15T18:30:36.379Z,Help Write Code,v2,,,,assign the args to the new action node +63c647b55f004bf4898cf603,2023-03-15T18:32:04.009Z,Help Fix Code,v2,read an action line of a makefile and turn it into a struct action_node,"typedef struct action_node { + char **args; // An array of strings suitable to be passed to execvp + struct action_node *next_act; +} Action; + + if(is_action(fp_line) == 1){ + char **act_args = malloc(sizeof(char **)); + char *action_token = strtok(fp_line, "" \t""); + action_token = strtok(NULL, "" \t""); + while(action_token != NULL){ + continue; + action_token = strtok(NULL, "" \t""); + } + Action *a = create_action_node(act_args); + add_action_node(current_rule, a); + }",, +63c647b55f004bf4898cf603,2023-03-15T18:36:18.220Z,Help Fix Code,v2,"go through an action line in the makefile and turn the actions into a char **, and create a struct_action_node","typedef struct action_node { + char **args; // An array of strings suitable to be passed to execvp + struct action_node *next_act; +} Action; + + if(is_action(fp_line) == 1){ + char **act_args; + char *action_token = strtok(fp_line, "" \t""); + action_token = strtok(NULL, "" \t""); + int act_count = 0; + while(action_token != NULL){ + act_count += 1; + action_token = strtok(NULL, "" \t""); + } + act_args = malloc(act_count * sizeof(char *)); + Action *a = create_action_node(act_args); + add_action_node(current_rule, a); + } + +/* Create a new action node. + * Return a pointer to this action node. + */ +Action *create_action_node(char **args){ + Action *new_action_node = malloc(sizeof(Action)); + return new_action_node; +} +",, +63c647b55f004bf4898cf603,2023-03-15T18:42:37.267Z,Help Write Code,v2,,,,write code to turn an action line of a makefile into an array of strings +63c647b55f004bf4898cf603,2023-03-15T18:50:31.020Z,Help Write Code,v2,,,,get the number of actions in an action line of a makefile using strtok +63c647b55f004bf4898cf603,2023-03-15T18:52:15.370Z,Explain Code,v2,," +char *args_to_string(char **args, char *buffer, int size) { + buffer[0] = '\0'; + int i = 0; + while (args[i] != NULL) { + strncat(buffer, args[i], size - strlen(buffer)); + strncat(buffer, "" "", size - strlen(buffer)); + i++; + } + return buffer; +} +",, +63c647b55f004bf4898cf603,2023-03-15T19:15:54.177Z,General Question,v2,what should the parameters of fgets be when reading through the file pointed to by a file pointer,,, +63c647b55f004bf4898cf603,2023-03-15T19:25:11.498Z,Help Fix Code,v2," go through an action line in the makefile and turn the actions into a char **, and create a struct_action_node"," if(is_action(fp_line) == 1){ + int num_actions = 0; + // number of actions + char *line_copy = NULL; + strcpy(line_copy, fp_line); + char **temp_args = NULL; + char *aa = strtok(line_copy, "" \t""); + while(aa != NULL){ + num_actions += 1; + aa = strtok(NULL, "" \t""); + } + // actually building it + char **act_args = malloc((num_actions + 1) * sizeof(char *)); + char *action_token = strtok(fp_line, "" \t""); + action_token = strtok(NULL, "" \t""); + int index = 0; + while(action_token != NULL){ + act_args[index] = action_token; + action_token = strtok(NULL, "" \t""); + } + Action *new_action_node = create_action_node(act_args); + add_action_node(current_rule, new_action_node); + + }",, +63c647b55f004bf4898cf603,2023-03-15T19:26:54.817Z,Help Write Code,v2,,,,"get the number of actions in the action line of a makefile +" +63c647b55f004bf4898cf603,2023-03-15T19:28:35.462Z,Help Write Code,v2,,,,Intended Behavior: get the number of actions in the action line of a makefile without modifying the makefile AT ALL +63c647b75f004bf4898cf617,2023-01-24T06:22:56.133Z,General Question,v1,what does (.text+0x1b): undefined reference to `main' mean,,, +63c647b75f004bf4898cf617,2023-02-11T21:15:28.191Z,Question from Code,v1,What is this code doing?,((var |= 1 <<(flag))),, +63c647b75f004bf4898cf617,2023-02-11T21:18:04.171Z,Explain Code,v1,,((var) & (1 << (flag))),, +63c647b75f004bf4898cf617,2023-02-12T00:34:10.073Z,Explain Code,v1,,char * (*x) (char *) = my_func;,, +63c647b75f004bf4898cf617,2023-02-12T00:38:52.369Z,Question from Code,v1,"why (*x)? +",char * (*x) (char *) = my_func;,, +63c647b75f004bf4898cf61c,2023-01-24T22:25:51.469Z,General Question,v1,how does scanf treat white spaces,,, +63c647b75f004bf4898cf61c,2023-01-26T01:53:18.076Z,Help Fix Code,v1,,"void update_contact(struct *contact, char *new_name, int number, char *address){ + strcpy(contact->name, new_name); + contact->phone_number= number; + strcpy(contact->address, address); + +} +",modify struct using pointer, +63c647b75f004bf4898cf61c,2023-01-26T18:08:37.740Z,Help Fix Code,v1,,"struct node{ + +int value; +struct node *next; + +}",create struct with value and pointer t next node, +63c647b75f004bf4898cf61c,2023-01-27T19:49:56.283Z,Help Fix Code,v1,,&((*argc)+1);,pointer arithmetic and then taking the address of result, +63c647b75f004bf4898cf61c,2023-01-27T19:51:31.861Z,Explain Code,v1,,&((*argc)+1);,, +63c647b75f004bf4898cf61c,2023-01-27T19:52:12.999Z,Help Fix Code,v1,,&((*argv)+1);,does pointer arithmetic then takes address of result, +63c647b85f004bf4898cf621,2023-01-18T22:37:07.814Z,General Question,v1,what does it mean when standard input is closed in c,,, +63c647b85f004bf4898cf621,2023-01-18T22:39:28.154Z,General Question,v1,how do you close standard input in c,,, +63c647b85f004bf4898cf621,2023-03-13T19:33:21.843Z,Question from Code,v2,"given the provided struct and code, why does the malloc call cause a segmentation fault","typedef struct action_node { + char **args; // An array of strings suitable to be passed to execvp + struct action_node *next_act; +} Action; + +action->args = malloc(sizeof(char *) * (9));",, +63c647b85f004bf4898cf621,2023-03-13T19:44:37.759Z,Question from Code,v2,"given the provided struct and code, why does the malloc call cause a segmentation fault","typedef struct action_node { + char **args; // An array of strings suitable to be passed to execvp + struct action_node *next_act; +} Action; + +action->args = malloc(sizeof(char **) * (9));",, +63c647b85f004bf4898cf621,2023-03-13T19:45:26.333Z,Help Fix Code,v2,is there a problem with this code,"typedef struct action_node { + char **args; // An array of strings suitable to be passed to execvp + struct action_node *next_act; +} Action; + +action->args = malloc(sizeof(char *) * (9));",, +63c647b85f004bf4898cf621,2023-03-13T19:46:59.573Z,Help Fix Code,v2,action->args should have space allocated for 9 strings,"typedef struct action_node { + char **args; // An array of strings suitable to be passed to execvp + struct action_node *next_act; +} Action; + +action->args = malloc(sizeof(char *) * (9));",, +63c647b85f004bf4898cf621,2023-03-13T21:44:17.408Z,General Question,v2,what does the function strtok do to the string it is called on,,, +63c647b85f004bf4898cf621,2023-03-13T22:00:43.879Z,General Question,v2,what happens when there is no delimiter in the string strtok is called on,,, +63c647bb5f004bf4898cf63a,2023-02-04T20:40:57.367Z,General Question,v1,How do you open a file,,, +63c647bb5f004bf4898cf63a,2023-02-04T21:25:10.900Z,General Question,v1,what are the all the ways to compare strings,,, +63c647bb5f004bf4898cf63a,2023-02-04T22:57:39.775Z,General Question,v1,generate a detailed documentation of `strcmp` with usage examples and explanations,,, +63c647bb5f004bf4898cf63a,2023-02-05T01:21:27.238Z,General Question,v1,how do you add elements to a char array,,, +63c647bb5f004bf4898cf63a,2023-02-05T01:22:06.154Z,General Question,v1,how do you add elements to an array,,, +63c647bb5f004bf4898cf63a,2023-02-05T01:25:39.215Z,General Question,v1,what library do you need for len(),,, +63c647bb5f004bf4898cf63a,2023-02-05T01:26:32.144Z,General Question,v1,how do you calculate the length of an array,,, +63c647bb5f004bf4898cf63a,2023-02-05T01:29:02.959Z,General Question,v1,how do you calculate the number of initialized spots in an array,,, +63c647bb5f004bf4898cf63a,2023-02-05T01:29:26.739Z,General Question,v1,How do you find the number of elements in an array,,, +63c647bb5f004bf4898cf63a,2023-02-05T01:40:55.193Z,Question from Code,v1,What are the initial values of the elements in friends,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User;",, +63c647bb5f004bf4898cf63a,2023-02-05T01:42:41.319Z,Question from Code,v1,What are the initial values of the elements in friends.,"int main() { + char *friends[10]; + return 0; +} ",, +63c647bb5f004bf4898cf63a,2023-02-05T01:43:43.876Z,Question from Code,v1,What are the initial values of the elements in name,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User;",, +63c647bb5f004bf4898cf63a,2023-02-05T01:48:37.143Z,Question from Code,v1,What is the initial values of the elements in user1->friends,"typedef struct user { + char name[32]; + char profile_pic[32]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[10]; + struct user *next; +} User; + +void create() { + User *user1 = malloc(sizeof(User)); +}",, +63c647bb5f004bf4898cf63a,2023-02-05T01:49:21.843Z,Question from Code,v1,What are the initial values of the elements of user1->name,"typedef struct user { + char name[32]; + char profile_pic[32]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[10]; + struct user *next; +} User; + +void create() { + User *user1 = malloc(sizeof(User)); +}",, +63c647bb5f004bf4898cf63a,2023-02-05T01:50:27.725Z,General Question,v1,Where are structs stored in memory,,, +63c647bb5f004bf4898cf63a,2023-02-05T02:01:31.641Z,General Question,v1,"What are the initial values of global data +",,, +63c647bb5f004bf4898cf63a,2023-02-05T02:02:04.966Z,General Question,v1,What are the initial values of heap data,,, +63c647bb5f004bf4898cf63a,2023-02-05T02:03:03.159Z,General Question,v1,What are the initial values of the data in structs that are defined in header files,,, +63c647bb5f004bf4898cf63a,2023-02-05T02:25:37.129Z,General Question,v1,explain and generate an example of printing the contents of a text file,,, +63c647bb5f004bf4898cf63a,2023-02-05T02:30:29.947Z,General Question,v1,generate example code of the fgets function being used on a file,,, +63c647bb5f004bf4898cf63a,2023-02-07T13:54:17.937Z,General Question,v1,"What is the keyword that indicates that a word is a legal target but isn't an actual file? + + + +",,, +63c647bb5f004bf4898cf63a,2023-02-07T13:55:12.323Z,Question from Code,v1,What is rm -f *.o print_ptree?,"FLAGS = -Wall -Werror -std=c99 +DEPENDENCIES = ptree.h + +all: print_ptree + +print_ptree: print_ptree.o ptree.o + gcc ${FLAGS} -o $@ $^ + +%.o: %.c ${DEPENDENCIES} + gcc ${FLAGS} -c $< + +clean: + rm -f *.o print_ptree",, +63c647bb5f004bf4898cf63a,2023-02-07T14:03:10.576Z,Question from Code,v1,What is print_ptree?,"FLAGS = -Wall -Werror -std=c99 +DEPENDENCIES = ptree.h + +all: print_ptree + +print_ptree: print_ptree.o ptree.o + gcc ${FLAGS} -o $@ $^ + +%.o: %.c ${DEPENDENCIES} + gcc ${FLAGS} -c $< + +clean: + rm -f *.o print_ptree",, +63c647bb5f004bf4898cf63a,2023-02-07T14:05:49.149Z,Question from Code,v1,is print_ptree a dependency?,"FLAGS = -Wall -Werror -std=c99 +DEPENDENCIES = ptree.h + +all: print_ptree + +print_ptree: print_ptree.o ptree.o + gcc ${FLAGS} -o $@ $^ + +%.o: %.c ${DEPENDENCIES} + gcc ${FLAGS} -c $< + +clean: + rm -f *.o print_ptree",, +63c647bb5f004bf4898cf63a,2023-02-07T14:06:07.573Z,Question from Code,v1,is print_ptree a variable,"FLAGS = -Wall -Werror -std=c99 +DEPENDENCIES = ptree.h + +all: print_ptree + +print_ptree: print_ptree.o ptree.o + gcc ${FLAGS} -o $@ $^ + +%.o: %.c ${DEPENDENCIES} + gcc ${FLAGS} -c $< + +clean: + rm -f *.o print_ptree",, +63c647bb5f004bf4898cf63a,2023-02-07T14:06:29.869Z,Question from Code,v1,is print_ptree a variable,"FLAGS = -Wall -Werror -std=c99 +DEPENDENCIES = ptree.h + +all: print_ptree + +print_ptree: print_ptree.o ptree.o + gcc ${FLAGS} -o $@ $^ + +%.o: %.c ${DEPENDENCIES} + gcc ${FLAGS} -c $< + +clean: + rm -f *.o print_ptree",, +63c647bb5f004bf4898cf63a,2023-02-07T14:06:57.519Z,Question from Code,v1,is print_ptree a recipe,"FLAGS = -Wall -Werror -std=c99 +DEPENDENCIES = ptree.h + +all: print_ptree + +print_ptree: print_ptree.o ptree.o + gcc ${FLAGS} -o $@ $^ + +%.o: %.c ${DEPENDENCIES} + gcc ${FLAGS} -c $< + +clean: + rm -f *.o print_ptree",, +63c647bb5f004bf4898cf63a,2023-02-07T14:07:22.332Z,Question from Code,v1,is print_ptree a rule,"FLAGS = -Wall -Werror -std=c99 +DEPENDENCIES = ptree.h + +all: print_ptree + +print_ptree: print_ptree.o ptree.o + gcc ${FLAGS} -o $@ $^ + +%.o: %.c ${DEPENDENCIES} + gcc ${FLAGS} -c $< + +clean: + rm -f *.o print_ptree",, +63c647bb5f004bf4898cf63a,2023-02-07T14:07:48.571Z,Question from Code,v1,is print_ptree a prerequisite,"FLAGS = -Wall -Werror -std=c99 +DEPENDENCIES = ptree.h + +all: print_ptree + +print_ptree: print_ptree.o ptree.o + gcc ${FLAGS} -o $@ $^ + +%.o: %.c ${DEPENDENCIES} + gcc ${FLAGS} -c $< + +clean: + rm -f *.o print_ptree",, +63c647bb5f004bf4898cf63a,2023-02-07T14:09:27.487Z,Question from Code,v1,"is print_ptree a rule, target, dependency, variable, recipe, or prerequisite?","FLAGS = -Wall -Werror -std=c99 +DEPENDENCIES = ptree.h + +all: print_ptree + +print_ptree: print_ptree.o ptree.o + gcc ${FLAGS} -o $@ $^ + +%.o: %.c ${DEPENDENCIES} + gcc ${FLAGS} -c $< + +clean: + rm -f *.o print_ptree",, +63c647bb5f004bf4898cf63a,2023-02-07T19:22:12.489Z,Explain Code,v1,,"typedef void(*SortFunc_t)(int *, int); +",, +63c647bb5f004bf4898cf63a,2023-02-08T02:44:16.337Z,General Question,v1,"What is the return value of fgets? +",,, +63c647bb5f004bf4898cf63a,2023-02-08T02:52:08.043Z,General Question,v1,explain the fread function,,, +63c647bb5f004bf4898cf63a,2023-02-08T02:54:42.859Z,General Question,v1,explain the fseek function,,, +63c647bb5f004bf4898cf63a,2023-02-08T02:56:05.132Z,General Question,v1,What is the difference between text I/O and binary I/O,,, +63c647bb5f004bf4898cf63a,2023-02-08T03:05:59.621Z,General Question,v1,What is size_t?,,, +63c647bb5f004bf4898cf63a,2023-02-08T03:07:06.934Z,General Question,v1,What does the od command do?,,, +63c647bb5f004bf4898cf63a,2023-02-08T03:07:55.194Z,Explain Code,v1,,od --address-radix=x --format=c --format=dC ,, +63c647bb5f004bf4898cf63a,2023-02-08T03:09:41.801Z,General Question,v1,generate a detailed documentation of `--format=dC` with usage examples and explanations,,, +63c647bb5f004bf4898cf63a,2023-02-08T03:10:40.319Z,General Question,v1,generate a detailed documentation of `--address-radix` with usage examples and explanations,,, +63c647bb5f004bf4898cf63a,2023-02-08T03:13:14.025Z,Explain Code,v1,,objdump --wide --sections ,, +63c647bb5f004bf4898cf63a,2023-02-08T03:13:54.164Z,General Question,v1,What are the sections of an object file?,,, +63c647bb5f004bf4898cf63a,2023-02-08T03:16:01.504Z,Explain Code,v1,,objdump --wide --sections section=.rodata ,, +63c647bb5f004bf4898cf63a,2023-02-08T03:17:22.975Z,Explain Code,v1,,objdump --full-contents section=.rodata ,, +63c647bb5f004bf4898cf63a,2023-02-08T03:19:44.776Z,General Question,v1,What is the size of a long?,,, +63c647bb5f004bf4898cf63a,2023-02-08T03:35:01.961Z,Explain Code,v1,,%#lx,, +63c647bb5f004bf4898cf63a,2023-02-26T13:36:55.523Z,Question from Code,v1,"What is wrong with this line of code? +","fprintf(fp, 'A');",, +63c647bb5f004bf4898cf63a,2023-02-26T14:18:53.473Z,General Question,v1,"what does -lm do? +",,, +63c647bb5f004bf4898cf63a,2023-02-26T14:20:15.977Z,Explain Code,v1,,gcc -Wall -g -std=gnu99 -o test_load_data dectree.o test_load_data.o -lm,, +63c647bb5f004bf4898cf63a,2023-02-26T14:20:48.721Z,Question from Code,v1,What does the -lm do here?,gcc -Wall -g -std=gnu99 -o test_load_data dectree.o test_load_data.o -lm,, +63c647bb5f004bf4898cf63a,2023-02-26T16:37:33.528Z,General Question,v1,what does .PHONY do?,,, +63c647bb5f004bf4898cf63a,2023-03-08T04:18:00.425Z,General Question,v1,Give me an example of a call to the wait function,,, +63c647bb5f004bf4898cf63a,2023-03-08T04:36:32.929Z,General Question,v1,generate example code for error checking with the fork function,,, +63c647bb5f004bf4898cf63a,2023-03-08T21:21:04.768Z,General Question,v1,give me an example of a call to the strtok function,,, +63c647bb5f004bf4898cf63a,2023-03-08T23:12:54.174Z,General Question,v1,give me an example of calling the execvp function,,, +63c647bb5f004bf4898cf63a,2023-03-08T23:26:57.876Z,General Question,v1,Is there a function to trim leading whitespace from a string?,,, +63c647bb5f004bf4898cf63a,2023-03-14T13:57:37.974Z,Question from Code,v2,What does the program print to stderr when it runs without interruption?,"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; +} +",, +63c647bb5f004bf4898cf63a,2023-03-14T13:58:09.485Z,Question from Code,v2,"What does the program above (from part 1) print to stderr if the user presses Ctrl+C at the moment when the program is at position A? +","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; +} +",, +63c647bb5f004bf4898cf63a,2023-03-14T13:59:34.741Z,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; +} +",, +63c647bb5f004bf4898cf63a,2023-03-31T23:00:19.343Z,General Question,v2,"what is a ""stubbed main file""?",,, +63c647bb5f004bf4898cf63a,2023-03-31T23:14:51.333Z,Explain Code,v2,,PORT = 51345 CFLAGS = DPORT = \$(PORT) - g - std = gnu99 - Wall - Werror,, +63c647bb5f004bf4898cf63a,2023-03-31T23:19:15.636Z,General Question,v2,"what does $< do in a makefile +",,, +63c647bb5f004bf4898cf63a,2023-03-31T23:21:06.844Z,General Question,v2,what do $@ and $^ do in makefiles?,,, +63c647bb5f004bf4898cf63a,2023-03-31T23:28:51.941Z,General Question,v2,How do you use -D in the command line?,,, +63c647bb5f004bf4898cf63a,2023-03-31T23:30:06.821Z,Help Write Code,v2,,,,Give me examples of using -D from the command line. +63c647bb5f004bf4898cf63a,2023-04-01T03:27:25.771Z,General Question,v2,does snprintf add a null terminator?,,, +63c647bb5f004bf4898cf63a,2023-04-02T15:34:42.389Z,General Question,v2,Can you have the same #include statement in two files that will be linked?,,, +63c647bb5f004bf4898cf63a,2023-04-02T19:58:31.734Z,Question from Code,v2,How do I print cmd_argv[0][5] as a single character instead of an int?,"printf(""cmd_argv[0][5]: %d\n"", cmd_argv[0][5]);",, +63c647bb5f004bf4898cf63a,2023-04-02T19:59:35.923Z,General Question,v2,what is the digit 108 as a char?,,, +63c647bb5f004bf4898cf63a,2023-04-02T20:00:14.063Z,General Question,v2,"what is the integer representation of the character +'\r'?",,, +63c647bb5f004bf4898cf63a,2023-04-02T20:01:08.980Z,Question from Code,v2,what will strcmp return in this case?,"strcmp(""Grace\r"", ""Grace"")",, +63c647bb5f004bf4898cf63a,2023-04-02T21:03:08.246Z,Question from Code,v2,Why is my code giving me a segmentation fault?,"while (i) { + fd_set select_fds = all_fds; + printf(""waiting...\n""); + if (select(max_fd + 1, &select_fds, NULL, NULL, NULL) == -1) { + perror(""server: select""); + exit(1); + } + printf(""done waiting\n""); + if (FD_ISSET(client_fd, &select_fds)) { + if (readn(client_fd, buffer, BUFSIZE - 1) == 0) { + printf(""client disconnected\n""); + break; + } + char *cmd_argv[INPUT_ARG_MAX_NUM]; + int cmd_argc = tokenize(buffer, cmd_argv); + printf(""cmd_argc: %d, cmd_argv[0]: %s | cmd_argv[1]: %s\n"", cmd_argc, cmd_argv[0], cmd_argv[1]); + fflush(stdout); + printf(""user: %s\n"", user_list->name); + fflush(stdout); + + if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list, user_name) == -1) { + break; + } + + printf(""> ""); + } + + i = 0; +} +return 0; +}",, +63c647bb5f004bf4898cf63a,2023-04-02T21:05:48.528Z,Question from Code,v2,Why could the printf statements on lines 4 and 9 causing a segmentation fault?,"while (i) { + fd_set select_fds = all_fds; + printf(""waiting...\n""); + if (select(max_fd + 1, &select_fds, NULL, NULL, NULL) == -1) { + perror(""server: select""); + exit(1); + } + printf(""done waiting\n""); + if (FD_ISSET(client_fd, &select_fds)) { + if (readn(client_fd, buffer, BUFSIZE - 1) == 0) { + printf(""client disconnected\n""); + break; + } + char *cmd_argv[INPUT_ARG_MAX_NUM]; + int cmd_argc = tokenize(buffer, cmd_argv); + printf(""cmd_argc: %d, cmd_argv[0]: %s | cmd_argv[1]: %s\n"", cmd_argc, cmd_argv[0], cmd_argv[1]); + fflush(stdout); + printf(""user: %s\n"", user_list->name); + fflush(stdout); + + if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list, user_name) == -1) { + break; + } + + printf(""> ""); + } + + i = 0; +} +return 0; +}",, +63c647bb5f004bf4898cf63a,2023-04-02T21:10:55.803Z,General Question,v2,Is it possible for a program to only work as intended when it is run through the gdb debugger?,,, +63c647bb5f004bf4898cf63a,2023-04-02T23:52:02.934Z,General Question,v2,what does the FD_CLR macro do?,,, +63c647bb5f004bf4898cf63a,2023-04-03T16:38:58.644Z,General Question,v2,How do you use strncat to safely concatenate two strings?,,, +63c647bb5f004bf4898cf63a,2023-04-03T18:01:24.513Z,General Question,v2,can I use snprintf on initialized memory? ,,, +63c647bb5f004bf4898cf63a,2023-04-03T18:01:59.458Z,General Question,v2,can I use snprintf on uninitialized memory?,,, +63c647bb5f004bf4898cf63a,2023-04-03T19:53:09.247Z,Question from Code,v2,does this Makefile have any redundancies?,"PORT = 51345 CFLAGS = -DPORT =\$(PORT) - g - std = + gnu99 - Wall - Werror OFLAGS = -g - std = gnu99 - Wall - + Werror + + all : friend_server + + friend_server : friend_server.o friend_socket.o friends.o partial_io.o clients.o gcc ${CFLAGS} - + o $ @$ ^ + + friend_server.o : friend_server.c friend_socket.h friends.h partial_io.h gcc ${CFLAGS} - c $ < + + %.o : %.c friend_socket.h friends.h partial_io.h gcc ${OFLAGS} - c $ < + + clean : rm - f *.o friend_server",, +63c647bb5f004bf4898cf63a,2023-04-06T04:45:46.156Z,General Question,v2,can you run strlen() on an integer?,,, +63c647bb5f004bf4898cf63a,2023-04-06T04:50:53.653Z,General Question,v2,is / integer division?,,, +63c647bc5f004bf4898cf63f,2023-01-22T08:49:29.188Z,General Question,v1,is there a built in function to remove element at specific index of an array,,, +63c647bc5f004bf4898cf63f,2023-01-22T10:24:07.206Z,Question from Code,v1,why does this for loop print new twice," char line[31]; + int in; + char new[9]; + + for (int i = 1; i < 10; i++) { + new[i-1] = line[i]; + printf(""%s\n"", new); + }",, +63c647bc5f004bf4898cf63f,2023-01-22T10:26:15.357Z,Question from Code,v1,why does this for loop print new twice.," char line[31]; + int in; + char new[9]; + + for (int i = 1; i < 10; i++) { + new[i-1] = line[i]; + } + printf(""%s\n"", new);",, +63c647bc5f004bf4898cf63f,2023-01-22T10:29:35.448Z,Question from Code,v1,"Why does this code print new twice. For example, output was ""rwx-------rwx------"" but expected output is rwx-------"," char line[31]; + int in; + char new[9]; + + for (int i = 1; i < 10; i++) { + new[i-1] = line[i]; + } + printf(""%s\n"", new);",, +63c647bc5f004bf4898cf63f,2023-01-22T10:30:45.774Z,Help Fix Code,v1,," char line[31]; + int in; + char new[9]; + + for (int i = 1; i < 10; i++) { + new[i-1] = line[i]; + } + printf(""%s\n"", new);","output is rwx-------rwx------, expected output is rwx-------", +63c647bc5f004bf4898cf63f,2023-01-22T10:38:28.465Z,General Question,v1,The string new is not properly terminated with a null character. -> This is fixed by adding \0 to the end of the string. How would I do that?,,, +63c647bc5f004bf4898cf63f,2023-01-27T07:48:44.749Z,Help Fix Code,v1,,"int main(int argc, char** argv) { + char line[11]; + int size; + int count = 0; + int int_size = strtol(argv[1], NULL, 10); + char new[10]; + + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + else if (argc == 3) { + // Skip first line + scanf(""%*s, %*d""); + while(scanf(""%10s %*d %*s %*s %d %*s %*d %*s %*s"",line, &size) != EOF) { + // Checks if first index is '-' because we ignore if 'd' + if (line[0] == '-') { + // input elements of line into new array without first element + for (int i = 0; i < 10; i++){ + new[i] = line[i + 1]; + } + if ((check_permissions(new, argv[2]) == 0) && (size > int_size)) { + count += 1; + } + } + } + printf(""%d\n"", count); + } + else { + scanf(""%*s, %*d""); + while(scanf(""%*s %*d %*s %*s %d %*s %*d %*s %*s"", &size) != EOF) { + if (size > int_size) { + count += 1; + } + } + printf(""%d\n"", count); + + } + return 0; +}","EXC_BAD_ACCESS (code=1, address=0x0) in line 5.", +63c647bc5f004bf4898cf63f,2023-02-11T19:31:19.614Z,General Question,v1,"is there a contains function? like if array contains item boolean +",,, +63c647bc5f004bf4898cf63f,2023-02-11T22:26:10.436Z,Question from Code,v1,do i also need to free target->first_post->date?,"int make_post(const User *author, User *target, char *contents) { + + if (author == NULL | target == NULL) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + strcpy(new_post->author, author->name); + new_post->contents = contents; + + new_post->date = malloc(sizeof(time_t)); + if (new_post->date == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + *(new_post->date) = time(NULL); + + new_post->next = target->first_post; + target->first_post = new_post; + target->first_post->date = (new_post->date); + + free(new_post->date); + free(new_post); + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-11T22:28:18.478Z,Question from Code,v1,is there anything else i should free?,"int make_post(const User *author, User *target, char *contents) { + + if (author == NULL | target == NULL) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + strcpy(new_post->author, author->name); + new_post->contents = contents; + + new_post->date = malloc(sizeof(time_t)); + if (new_post->date == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + *(new_post->date) = time(NULL); + + new_post->next = target->first_post; + target->first_post = new_post; + target->first_post->date = (new_post->date); + + free(new_post->date); + free(new_post); + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-11T22:29:42.686Z,Question from Code,v1,"so freeing + free(new_post->date); + free(target->first_post->date); + free(new_post->contents); + free(new_post); + +would be sufficient?","int make_post(const User *author, User *target, char *contents) { + + if (author == NULL | target == NULL) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + strcpy(new_post->author, author->name); + new_post->contents = contents; + + new_post->date = malloc(sizeof(time_t)); + if (new_post->date == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + *(new_post->date) = time(NULL); + + new_post->next = target->first_post; + target->first_post = new_post; + target->first_post->date = (new_post->date); + + free(new_post->date); + free(new_post); + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-11T22:30:41.631Z,Question from Code,v1,"so freeing + free(target->first_post->date); + free(new_post); +would be sufficient?","int make_post(const User *author, User *target, char *contents) { + + if (author == NULL | target == NULL) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + strcpy(new_post->author, author->name); + new_post->contents = contents; + + new_post->date = malloc(sizeof(time_t)); + if (new_post->date == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + *(new_post->date) = time(NULL); + + new_post->next = target->first_post; + target->first_post = new_post; + target->first_post->date = (new_post->date); + + free(new_post->date); + free(new_post); + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-11T22:31:37.645Z,Question from Code,v1,"you stated that I should free new_post->contents and new_post->date, would it be somewhere else outside of this function?","int make_post(const User *author, User *target, char *contents) { + + if (author == NULL | target == NULL) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + strcpy(new_post->author, author->name); + new_post->contents = contents; + + new_post->date = malloc(sizeof(time_t)); + if (new_post->date == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + *(new_post->date) = time(NULL); + + new_post->next = target->first_post; + target->first_post = new_post; + target->first_post->date = (new_post->date); + + free(new_post->date); + free(new_post); + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-14T06:19:44.540Z,Question from Code,v1,do i need to free() anything,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) > 33) { + return 2; + } + struct user *new_user = malloc(sizeof(struct user)); + strcpy(new_user->name, name); + new_user->next = NULL; + + struct user *last = *user_ptr_add; + while (last != NULL) { + if (strcmp(last->name, name) == 0) { + return 1; + } + last = last->next; + } + + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + } + else { + last = *user_ptr_add; + while (last->next != NULL) { + last = last->next; + } + last->next = new_user; + } + init(new_user); + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-14T06:20:54.337Z,Question from Code,v1,do i need to free() anything?,"int make_post(const User *author, User *target, char *contents) { + + if (author == NULL | target == NULL) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + strcpy(new_post->author, author->name); + new_post->contents = contents; + + + time_t *date = malloc(sizeof(time_t)); + if (date == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + *date = time(NULL); + new_post->date = date; + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-14T06:21:51.720Z,Question from Code,v1,do i need to free() anything else?,"#include ""friends.h"" +#include +#include +#include + +void init(User *user) { + user->friends[MAX_FRIENDS - 1] = NULL; +} +/* + * 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) > 33) { + return 2; + } + struct user *new_user = malloc(sizeof(struct user)); + strcpy(new_user->name, name); + new_user->next = NULL; + + struct user *last = *user_ptr_add; + while (last != NULL) { + if (strcmp(last->name, name) == 0) { + return 1; + } + last = last->next; + } + + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + } + else { + last = *user_ptr_add; + while (last->next != NULL) { + last = last->next; + } + last->next = new_user; + } + init(new_user); + free(new_user); + 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) { + + const User *curr = head; + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + return (User *)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) { + printf(""User List\n""); + while (curr != NULL) { + printf("" %s\n"", curr->name); + curr = curr->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) >= 33) { + return 2; + } + if (fopen(filename, ""r"") == NULL) { + return 1; + } else { + strcpy(user->profile_pic, filename); + return 0; + } +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int count_friends(const char *name, User *head){ + int count = 0; + User *user_1 = find_user(name, head); + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user_1->friends[i] != NULL) { + count ++; + } + } + return count; +} + + +int make_friends(const char *name1, const char *name2, User *head) { + User *user_1 = find_user(name1, head); + User *user_2 = find_user(name2, head); + int friends_name1 = count_friends(name1, head); + int friends_name2 = count_friends(name2, head); + if ((user_1 == NULL) | (user_2 == NULL)) { + return 4; + } + if (strcmp(name1, name2) == 0) { + return 3; + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if ((user_1->friends[i] != user_2) & ((friends_name1 == MAX_FRIENDS) | (friends_name2 == MAX_FRIENDS))) { + return 2; + } + if (user_1->friends[i] == user_2) { + return 1; + } + } + user_1->friends[friends_name1 + 1] = user_2; + user_2->friends[friends_name2 + 1] = user_1; + return 0; +} + + + +/* + * 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; + } + int c; + FILE *file; + file = fopen(user->profile_pic, ""r""); + if (file != NULL) { + while ((c = fgetc(file)) != EOF) + printf(""%c"", c); + fclose(file); + printf(""\nName: %s\n"", user->name); + }else { + printf(""Name: %s\n"", user->name); + } + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + for (int i = 0; i < 10; i++) { + if (user->friends[i] != NULL) { + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts: \n""); + Post *cur_post = user->first_post; + while (cur_post != NULL) { + if (cur_post->next != NULL) { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n\n"", cur_post->contents); + printf(""===\n""); + cur_post = cur_post->next; + printf(""\n""); + } + else { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n"", cur_post->contents); + cur_post = cur_post->next; + } + } + printf(""------------------------------------------\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 is_friends(const User *name1, User *name2) { + + int i = 0; + int friends = 0; + while (i < MAX_FRIENDS) { + if (name1 == name2->friends[i]) { + friends = 1; + } + i++; + } + return friends; +} + +int make_post(const User *author, User *target, char *contents) { + + if (author == NULL | target == NULL) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + strcpy(new_post->author, author->name); + new_post->contents = contents; + + + time_t *date = malloc(sizeof(time_t)); + if (date == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + *date = time(NULL); + new_post->date = date; + new_post->next = target->first_post; + target->first_post = new_post; + + free(new_post); + free(date); + + return 0; +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ + +void remove_friends(const char *name, User **user_ptr_r){ + + struct user *last = *user_ptr_r; + while (last != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (last->friends[i] != NULL && strcmp(last->friends[i]->name, name) == 0) { // find name wanted to be deleted + last->friends[i] = NULL; + } + } + if (strcmp(last->name, name) == 0) { + last->friends[MAX_FRIENDS - 1] = NULL; + } + last = last->next; + } +} + +int delete_user(const char *name, User **user_ptr_del) { + + struct user *temp = *user_ptr_del, *prev; + if (find_user(name, temp) == NULL) { + return 1; + } + + if (temp != NULL && strcmp(temp->name, name) == 0) { + *user_ptr_del = temp->next; // Changed head + free(temp); // free old head + return 0; + } + + while (temp != NULL && strcmp(temp->name, name) != 0) { + prev = temp; + temp = temp->next; + } + + Post *post = temp->first_post; + while (post != NULL) { + Post *next_post = post->next; + free(post->contents); + free(post); + post = next_post; + } + + // If key was not present in linked list + if (temp == NULL){ + return 1; + } + remove_friends(name, user_ptr_del); + + prev->next = temp->next; + free(temp); // Free memory + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-14T06:24:11.704Z,Question from Code,v1,"do i need to free() anything?/ +","#include ""friends.h"" +#include +#include +#include + +void init(User *user) { + user->friends[MAX_FRIENDS - 1] = NULL; +} +/* + * 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) > 33) { + return 2; + } + struct user *new_user = malloc(sizeof(struct user)); + strcpy(new_user->name, name); + new_user->next = NULL; + + struct user *last = *user_ptr_add; + while (last != NULL) { + if (strcmp(last->name, name) == 0) { + return 1; + } + last = last->next; + } + + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + } + else { + last = *user_ptr_add; + while (last->next != NULL) { + last = last->next; + } + last->next = new_user; + } + init(new_user); + free(new_user); + 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) { + + const User *curr = head; + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + return (User *)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) { + printf(""User List\n""); + while (curr != NULL) { + printf("" %s\n"", curr->name); + curr = curr->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) >= 33) { + return 2; + } + if (fopen(filename, ""r"") == NULL) { + return 1; + } else { + strcpy(user->profile_pic, filename); + return 0; + } +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int count_friends(const char *name, User *head){ + int count = 0; + User *user_1 = find_user(name, head); + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user_1->friends[i] != NULL) { + count ++; + } + } + return count; +} + + +int make_friends(const char *name1, const char *name2, User *head) { + User *user_1 = find_user(name1, head); + User *user_2 = find_user(name2, head); + int friends_name1 = count_friends(name1, head); + int friends_name2 = count_friends(name2, head); + if ((user_1 == NULL) | (user_2 == NULL)) { + return 4; + } + if (strcmp(name1, name2) == 0) { + return 3; + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if ((user_1->friends[i] != user_2) & ((friends_name1 == MAX_FRIENDS) | (friends_name2 == MAX_FRIENDS))) { + return 2; + } + if (user_1->friends[i] == user_2) { + return 1; + } + } + user_1->friends[friends_name1 + 1] = user_2; + user_2->friends[friends_name2 + 1] = user_1; + return 0; +} + + + +/* + * 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; + } + int c; + FILE *file; + file = fopen(user->profile_pic, ""r""); + if (file != NULL) { + while ((c = fgetc(file)) != EOF) + printf(""%c"", c); + fclose(file); + printf(""\nName: %s\n"", user->name); + }else { + printf(""Name: %s\n"", user->name); + } + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + for (int i = 0; i < 10; i++) { + if (user->friends[i] != NULL) { + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts: \n""); + Post *cur_post = user->first_post; + while (cur_post != NULL) { + if (cur_post->next != NULL) { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n\n"", cur_post->contents); + printf(""===\n""); + cur_post = cur_post->next; + printf(""\n""); + } + else { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n"", cur_post->contents); + cur_post = cur_post->next; + } + } + printf(""------------------------------------------\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 is_friends(const User *name1, User *name2) { + + int i = 0; + int friends = 0; + while (i < MAX_FRIENDS) { + if (name1 == name2->friends[i]) { + friends = 1; + } + i++; + } + return friends; +} + +int make_post(const User *author, User *target, char *contents) { + + if (author == NULL | target == NULL) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + strcpy(new_post->author, author->name); + new_post->contents = contents; + + + time_t *date = malloc(sizeof(time_t)); + if (date == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + *date = time(NULL); + new_post->date = date; + new_post->next = target->first_post; + target->first_post = new_post; + + free(new_post); + free(date); + + return 0; +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ + +void remove_friends(const char *name, User **user_ptr_r){ + + struct user *last = *user_ptr_r; + while (last != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (last->friends[i] != NULL && strcmp(last->friends[i]->name, name) == 0) { // find name wanted to be deleted + last->friends[i] = NULL; + } + } + if (strcmp(last->name, name) == 0) { + last->friends[MAX_FRIENDS - 1] = NULL; + } + last = last->next; + } +} + +int delete_user(const char *name, User **user_ptr_del) { + + struct user *temp = *user_ptr_del, *prev; + if (find_user(name, temp) == NULL) { + return 1; + } + + if (temp != NULL && strcmp(temp->name, name) == 0) { + *user_ptr_del = temp->next; // Changed head + free(temp); // free old head + return 0; + } + + while (temp != NULL && strcmp(temp->name, name) != 0) { + prev = temp; + temp = temp->next; + } + + Post *post = temp->first_post; + while (post != NULL) { + Post *next_post = post->next; + free(post->contents); + free(post); + post = next_post; + } + + // If key was not present in linked list + if (temp == NULL){ + return 1; + } + remove_friends(name, user_ptr_del); + + prev->next = temp->next; + free(temp); // Free memory + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-14T06:24:51.746Z,Question from Code,v1,what do i free() specifically?,"#include ""friends.h"" +#include +#include +#include + +void init(User *user) { + user->friends[MAX_FRIENDS - 1] = NULL; +} +/* + * 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) > 33) { + return 2; + } + struct user *new_user = malloc(sizeof(struct user)); + strcpy(new_user->name, name); + new_user->next = NULL; + + struct user *last = *user_ptr_add; + while (last != NULL) { + if (strcmp(last->name, name) == 0) { + return 1; + } + last = last->next; + } + + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + } + else { + last = *user_ptr_add; + while (last->next != NULL) { + last = last->next; + } + last->next = new_user; + } + init(new_user); + free(new_user); + 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) { + + const User *curr = head; + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + return (User *)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) { + printf(""User List\n""); + while (curr != NULL) { + printf("" %s\n"", curr->name); + curr = curr->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) >= 33) { + return 2; + } + if (fopen(filename, ""r"") == NULL) { + return 1; + } else { + strcpy(user->profile_pic, filename); + return 0; + } +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int count_friends(const char *name, User *head){ + int count = 0; + User *user_1 = find_user(name, head); + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user_1->friends[i] != NULL) { + count ++; + } + } + return count; +} + + +int make_friends(const char *name1, const char *name2, User *head) { + User *user_1 = find_user(name1, head); + User *user_2 = find_user(name2, head); + int friends_name1 = count_friends(name1, head); + int friends_name2 = count_friends(name2, head); + if ((user_1 == NULL) | (user_2 == NULL)) { + return 4; + } + if (strcmp(name1, name2) == 0) { + return 3; + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if ((user_1->friends[i] != user_2) & ((friends_name1 == MAX_FRIENDS) | (friends_name2 == MAX_FRIENDS))) { + return 2; + } + if (user_1->friends[i] == user_2) { + return 1; + } + } + user_1->friends[friends_name1 + 1] = user_2; + user_2->friends[friends_name2 + 1] = user_1; + return 0; +} + + + +/* + * 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; + } + int c; + FILE *file; + file = fopen(user->profile_pic, ""r""); + if (file != NULL) { + while ((c = fgetc(file)) != EOF) + printf(""%c"", c); + fclose(file); + printf(""\nName: %s\n"", user->name); + }else { + printf(""Name: %s\n"", user->name); + } + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + for (int i = 0; i < 10; i++) { + if (user->friends[i] != NULL) { + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts: \n""); + Post *cur_post = user->first_post; + while (cur_post != NULL) { + if (cur_post->next != NULL) { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n\n"", cur_post->contents); + printf(""===\n""); + cur_post = cur_post->next; + printf(""\n""); + } + else { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n"", cur_post->contents); + cur_post = cur_post->next; + } + } + printf(""------------------------------------------\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 is_friends(const User *name1, User *name2) { + + int i = 0; + int friends = 0; + while (i < MAX_FRIENDS) { + if (name1 == name2->friends[i]) { + friends = 1; + } + i++; + } + return friends; +} + +int make_post(const User *author, User *target, char *contents) { + + if (author == NULL | target == NULL) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + strcpy(new_post->author, author->name); + new_post->contents = contents; + + + time_t *date = malloc(sizeof(time_t)); + if (date == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + *date = time(NULL); + new_post->date = date; + new_post->next = target->first_post; + target->first_post = new_post; + + free(new_post); + free(date); + + return 0; +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ + +void remove_friends(const char *name, User **user_ptr_r){ + + struct user *last = *user_ptr_r; + while (last != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (last->friends[i] != NULL && strcmp(last->friends[i]->name, name) == 0) { // find name wanted to be deleted + last->friends[i] = NULL; + } + } + if (strcmp(last->name, name) == 0) { + last->friends[MAX_FRIENDS - 1] = NULL; + } + last = last->next; + } +} + +int delete_user(const char *name, User **user_ptr_del) { + + struct user *temp = *user_ptr_del, *prev; + if (find_user(name, temp) == NULL) { + return 1; + } + + if (temp != NULL && strcmp(temp->name, name) == 0) { + *user_ptr_del = temp->next; // Changed head + free(temp); // free old head + return 0; + } + + while (temp != NULL && strcmp(temp->name, name) != 0) { + prev = temp; + temp = temp->next; + } + + Post *post = temp->first_post; + while (post != NULL) { + Post *next_post = post->next; + free(post->contents); + free(post); + post = next_post; + } + + // If key was not present in linked list + if (temp == NULL){ + return 1; + } + remove_friends(name, user_ptr_del); + + prev->next = temp->next; + free(temp); // Free memory + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-14T06:29:59.103Z,Question from Code,v1,Anything else i need to free()?,"#include ""friends.h"" +#include +#include +#include + +void init(User *user) { + user->friends[MAX_FRIENDS - 1] = NULL; +} +/* + * 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) > 33) { + return 2; + } + struct user *new_user = malloc(sizeof(struct user)); + strcpy(new_user->name, name); + new_user->next = NULL; + + struct user *last = *user_ptr_add; + while (last != NULL) { + if (strcmp(last->name, name) == 0) { + return 1; + } + last = last->next; + } + + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + } + else { + last = *user_ptr_add; + while (last->next != NULL) { + last = last->next; + } + last->next = new_user; + } + init(new_user); + free(new_user); + 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) { + + const User *curr = head; + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + return (User *)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) { + printf(""User List\n""); + while (curr != NULL) { + printf("" %s\n"", curr->name); + curr = curr->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) >= 33) { + return 2; + } + if (fopen(filename, ""r"") == NULL) { + return 1; + } else { + strcpy(user->profile_pic, filename); + return 0; + } +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int count_friends(const char *name, User *head){ + int count = 0; + User *user_1 = find_user(name, head); + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user_1->friends[i] != NULL) { + count ++; + } + } + return count; +} + + +int make_friends(const char *name1, const char *name2, User *head) { + User *user_1 = find_user(name1, head); + User *user_2 = find_user(name2, head); + int friends_name1 = count_friends(name1, head); + int friends_name2 = count_friends(name2, head); + if ((user_1 == NULL) | (user_2 == NULL)) { + return 4; + } + if (strcmp(name1, name2) == 0) { + return 3; + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if ((user_1->friends[i] != user_2) & ((friends_name1 == MAX_FRIENDS) | (friends_name2 == MAX_FRIENDS))) { + return 2; + } + if (user_1->friends[i] == user_2) { + return 1; + } + } + user_1->friends[friends_name1 + 1] = user_2; + user_2->friends[friends_name2 + 1] = user_1; + return 0; +} + + + +/* + * 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; + } + int c; + FILE *file; + file = fopen(user->profile_pic, ""r""); + if (file != NULL) { + while ((c = fgetc(file)) != EOF) + printf(""%c"", c); + fclose(file); + printf(""\nName: %s\n"", user->name); + }else { + printf(""Name: %s\n"", user->name); + } + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + for (int i = 0; i < 10; i++) { + if (user->friends[i] != NULL) { + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts: \n""); + Post *cur_post = user->first_post; + while (cur_post != NULL) { + if (cur_post->next != NULL) { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n\n"", cur_post->contents); + printf(""===\n""); + cur_post = cur_post->next; + printf(""\n""); + } + else { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n"", cur_post->contents); + cur_post = cur_post->next; + } + } + printf(""------------------------------------------\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 is_friends(const User *name1, User *name2) { + + int i = 0; + int friends = 0; + while (i < MAX_FRIENDS) { + if (name1 == name2->friends[i]) { + friends = 1; + } + i++; + } + return friends; +} + +int make_post(const User *author, User *target, char *contents) { + + if (author == NULL | target == NULL) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + strcpy(new_post->author, author->name); + new_post->contents = contents; + + + time_t *date = malloc(sizeof(time_t)); + if (date == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + *date = time(NULL); + new_post->date = date; + new_post->next = target->first_post; + target->first_post = new_post; + + + return 0; +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ + +void remove_friends(const char *name, User **user_ptr_r){ + + struct user *last = *user_ptr_r; + while (last != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (last->friends[i] != NULL && strcmp(last->friends[i]->name, name) == 0) { // find name wanted to be deleted + last->friends[i] = NULL; + } + } + if (strcmp(last->name, name) == 0) { + last->friends[MAX_FRIENDS - 1] = NULL; + } + last = last->next; + } +} + +int delete_user(const char *name, User **user_ptr_del) { + + struct user *temp = *user_ptr_del, *prev; + if (find_user(name, temp) == NULL) { + return 1; + } + + if (temp != NULL && strcmp(temp->name, name) == 0) { + *user_ptr_del = temp->next; // Changed head + free(temp); // free old head + return 0; + } + + while (temp != NULL && strcmp(temp->name, name) != 0) { + prev = temp; + temp = temp->next; + } + + Post *post = temp->first_post; + while (post != NULL) { + Post *next_post = post->next; + free(post->contents); + free(post->date) + free(post); + post = next_post; + } + + // If key was not present in linked list + if (temp == NULL){ + return 1; + } + remove_friends(name, user_ptr_del); + + prev->next = temp->next; + free(temp); // Free memory + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-14T06:30:57.515Z,Question from Code,v1,"specifically, what do i need to still free()","#include ""friends.h"" +#include +#include +#include + +void init(User *user) { + user->friends[MAX_FRIENDS - 1] = NULL; +} +/* + * 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) > 33) { + return 2; + } + struct user *new_user = malloc(sizeof(struct user)); + strcpy(new_user->name, name); + new_user->next = NULL; + + struct user *last = *user_ptr_add; + while (last != NULL) { + if (strcmp(last->name, name) == 0) { + return 1; + } + last = last->next; + } + + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + } + else { + last = *user_ptr_add; + while (last->next != NULL) { + last = last->next; + } + last->next = new_user; + } + init(new_user); + free(new_user); + 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) { + + const User *curr = head; + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + return (User *)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) { + printf(""User List\n""); + while (curr != NULL) { + printf("" %s\n"", curr->name); + curr = curr->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) >= 33) { + return 2; + } + if (fopen(filename, ""r"") == NULL) { + return 1; + } else { + strcpy(user->profile_pic, filename); + return 0; + } +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int count_friends(const char *name, User *head){ + int count = 0; + User *user_1 = find_user(name, head); + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user_1->friends[i] != NULL) { + count ++; + } + } + return count; +} + + +int make_friends(const char *name1, const char *name2, User *head) { + User *user_1 = find_user(name1, head); + User *user_2 = find_user(name2, head); + int friends_name1 = count_friends(name1, head); + int friends_name2 = count_friends(name2, head); + if ((user_1 == NULL) | (user_2 == NULL)) { + return 4; + } + if (strcmp(name1, name2) == 0) { + return 3; + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if ((user_1->friends[i] != user_2) & ((friends_name1 == MAX_FRIENDS) | (friends_name2 == MAX_FRIENDS))) { + return 2; + } + if (user_1->friends[i] == user_2) { + return 1; + } + } + user_1->friends[friends_name1 + 1] = user_2; + user_2->friends[friends_name2 + 1] = user_1; + return 0; +} + + + +/* + * 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; + } + int c; + FILE *file; + file = fopen(user->profile_pic, ""r""); + if (file != NULL) { + while ((c = fgetc(file)) != EOF) + printf(""%c"", c); + fclose(file); + printf(""\nName: %s\n"", user->name); + }else { + printf(""Name: %s\n"", user->name); + } + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + for (int i = 0; i < 10; i++) { + if (user->friends[i] != NULL) { + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts: \n""); + Post *cur_post = user->first_post; + while (cur_post != NULL) { + if (cur_post->next != NULL) { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n\n"", cur_post->contents); + printf(""===\n""); + cur_post = cur_post->next; + printf(""\n""); + } + else { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n"", cur_post->contents); + cur_post = cur_post->next; + } + } + printf(""------------------------------------------\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 is_friends(const User *name1, User *name2) { + + int i = 0; + int friends = 0; + while (i < MAX_FRIENDS) { + if (name1 == name2->friends[i]) { + friends = 1; + } + i++; + } + return friends; +} + +int make_post(const User *author, User *target, char *contents) { + + if (author == NULL | target == NULL) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + strcpy(new_post->author, author->name); + new_post->contents = contents; + + + time_t *date = malloc(sizeof(time_t)); + if (date == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + *date = time(NULL); + new_post->date = date; + new_post->next = target->first_post; + target->first_post = new_post; + + + return 0; +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ + +void remove_friends(const char *name, User **user_ptr_r){ + + struct user *last = *user_ptr_r; + while (last != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (last->friends[i] != NULL && strcmp(last->friends[i]->name, name) == 0) { // find name wanted to be deleted + last->friends[i] = NULL; + } + } + if (strcmp(last->name, name) == 0) { + last->friends[MAX_FRIENDS - 1] = NULL; + } + last = last->next; + } +} + +int delete_user(const char *name, User **user_ptr_del) { + + struct user *temp = *user_ptr_del, *prev; + if (find_user(name, temp) == NULL) { + return 1; + } + + if (temp != NULL && strcmp(temp->name, name) == 0) { + *user_ptr_del = temp->next; // Changed head + free(temp); // free old head + return 0; + } + + while (temp != NULL && strcmp(temp->name, name) != 0) { + prev = temp; + temp = temp->next; + } + + Post *post = temp->first_post; + while (post != NULL) { + Post *next_post = post->next; + free(post->contents); + free(post->date) + free(post); + post = next_post; + } + + // If key was not present in linked list + if (temp == NULL){ + return 1; + } + remove_friends(name, user_ptr_del); + + prev->next = temp->next; + free(temp); // Free memory + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-14T07:25:31.771Z,Question from Code,v1,"I am receiving memory leaks, where else can I use free()?","#include ""friends.h"" +#include +#include +#include + +void init(User *user) { + for (int i = 0; i < MAX_FRIENDS; i++) { + user->friends[i] = NULL; + } +} +/* + * 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) > 33) { + return 2; + } + struct user *new_user = malloc(sizeof(struct user)); + strcpy(new_user->name, name); + new_user->next = NULL; + + struct user *last = *user_ptr_add; + while (last != NULL) { + if (strcmp(last->name, name) == 0) { + return 1; + } + last = last->next; + } + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + } + else { + last = *user_ptr_add; + while (last->next != NULL) { + last = last->next; + } + last->next = new_user; + } + init(new_user); + 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) { + + const User *curr = head; + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + return (User *)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) { + printf(""User List\n""); + while (curr != NULL) { + printf("" %s\n"", curr->name); + curr = curr->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) >= 33) { + return 2; + } + if (fopen(filename, ""r"") == NULL) { + return 1; + } else { + strcpy(user->profile_pic, filename); + return 0; + } +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int count_friends(const char *name, User *head){ + int count = 0; + User *user_1 = find_user(name, head); + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user_1->friends[i] != NULL) { + count ++; + } + } + return count; +} + + +int make_friends(const char *name1, const char *name2, User *head) { + User *user_1 = find_user(name1, head); + User *user_2 = find_user(name2, head); + int friends_name1 = count_friends(name1, head); + int friends_name2 = count_friends(name2, head); + if ((user_1 == NULL) || (user_2 == NULL)) { + return 4; + } + if (strcmp(name1, name2) == 0) { + return 3; + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if ((user_1->friends[i] != user_2) & ((friends_name1 == MAX_FRIENDS) || (friends_name2 == MAX_FRIENDS))) { + return 2; + } + if (user_1->friends[i] == user_2) { + return 1; + } + } + user_1->friends[friends_name1] = user_2; + user_2->friends[friends_name2] = user_1; + return 0; +} + + + +/* + * 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; + } + int c; + FILE *file; + file = fopen(user->profile_pic, ""r""); + if (file != NULL) { + while ((c = fgetc(file)) != EOF) + printf(""%c"", c); + fclose(file); + printf(""\nName: %s\n"", user->name); + }else { + printf(""Name: %s\n"", user->name); + } + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + for (int i = 0; i < 10; i++) { + if (user->friends[i] != NULL) { + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts: \n""); + Post *cur_post = user->first_post; + while (cur_post != NULL) { + if (cur_post->next != NULL) { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n\n"", cur_post->contents); + printf(""===\n""); + cur_post = cur_post->next; + printf(""\n""); + } + else { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n"", cur_post->contents); + cur_post = cur_post->next; + } + } + printf(""------------------------------------------\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 is_friends(const User *name1, User *name2) { + + int i = 0; + int friends = 0; + while (i < MAX_FRIENDS) { + if (name1 == name2->friends[i]) { + friends = 1; + } + i++; + } + return friends; +} + +int make_post(const User *author, User *target, char *contents) { + + if (author == NULL | target == NULL) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + strcpy(new_post->author, author->name); + new_post->contents = contents; + + + time_t *date = malloc(sizeof(time_t)); + if (date == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + *date = time(NULL); + new_post->date = date; + new_post->next = target->first_post; + target->first_post = new_post; + + + return 0; +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ + +void remove_friends(const char *name, User **user_ptr_r){ + + struct user *last = *user_ptr_r; + while (last != NULL) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (last->friends[i] != NULL && strcmp(last->friends[i]->name, name) == 0) { // find name wanted to be deleted + last->friends[i] = NULL; + } + } + if (strcmp(last->name, name) == 0) { + last->friends[MAX_FRIENDS - 1] = NULL; + } + last = last->next; + } +} + +int delete_user(const char *name, User **user_ptr_del) { + + struct user *temp = *user_ptr_del, *prev; + if (find_user(name, temp) == NULL) { + return 1; + } + + if (temp != NULL && strcmp(temp->name, name) == 0) { + *user_ptr_del = temp->next; // Changed head + free(temp); // free old head + return 0; + } + + while (temp != NULL && strcmp(temp->name, name) != 0) { + prev = temp; + temp = temp->next; + } + + Post *post = temp->first_post; + while (post != NULL) { + Post *next_post = post->next; + free(post->contents); + free(post->date) + free(post); + post = next_post; + } + + // If key was not present in linked list + if (temp == NULL){ + return 1; + } + remove_friends(name, user_ptr_del); + + prev->next = temp->next; + free(temp); // Free memory + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-14T07:31:55.308Z,Question from Code,v1,do i need to free() anything in this function?,"int make_post(const User *author, User *target, char *contents) { + + if (author == NULL | target == NULL) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + strcpy(new_post->author, author->name); + new_post->contents = contents; + + + time_t *date = malloc(sizeof(time_t)); + if (date == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + *date = time(NULL); + new_post->date = date; + new_post->next = target->first_post; + target->first_post = new_post; + + + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-14T08:17:24.023Z,Question from Code,v1,"Receive still reachable byts, but i am closing the file how can I fix?","int print_user(const User *user) { + if (user == NULL) { + return 1; + } + int c; + FILE *file; + file = fopen(user->profile_pic, ""r""); + if (file != NULL) { + while ((c = fgetc(file)) != EOF) + printf(""%c"", c); + fclose(file); + printf(""\nName: %s\n"", user->name); + }else { + printf(""Name: %s\n"", user->name); + } + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + for (int i = 0; i < 10; i++) { + if (user->friends[i] != NULL) { + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts: \n""); + Post *cur_post = user->first_post; + while (cur_post != NULL) { + if (cur_post->next != NULL) { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n\n"", cur_post->contents); + printf(""===\n""); + cur_post = cur_post->next; + printf(""\n""); + } + else { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n"", cur_post->contents); + cur_post = cur_post->next; + } + } + printf(""------------------------------------------\n""); + return 0; +} +",, +63c647bc5f004bf4898cf63f,2023-02-14T08:24:25.411Z,Question from Code,v1,"i am receiving 472 still reachable byts in this code even though I'm closing file, why?","int print_user(const User *user) { + if (user == NULL) { + return 1; + } + int c; + FILE *file; + file = fopen(user->profile_pic, ""r""); + if (file != NULL) { + while ((c = fgetc(file)) != EOF) + printf(""%c"", c); + fclose(file); + printf(""\nName: %s\n"", user->name); + }else { + fclose(file); + printf(""Name: %s\n"", user->name); + } + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + for (int i = 0; i < 10; i++) { + if (user->friends[i] != NULL) { + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts: \n""); + Post *cur_post = user->first_post; + while (cur_post != NULL) { + if (cur_post->next != NULL) { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n\n"", cur_post->contents); + printf(""===\n""); + cur_post = cur_post->next; + printf(""\n""); + } + else { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n"", cur_post->contents); + cur_post = cur_post->next; + } + } + printf(""------------------------------------------\n""); + return 0; +} +",, +63c647bc5f004bf4898cf63f,2023-02-14T08:28:08.526Z,Question from Code,v1,Receiving still reachable byts why? whats a fix,"int print_user(const User *user) { + if (user == NULL) { + return 1; + } + int c; + FILE *file; + file = fopen(user->profile_pic, ""r""); + if (file != NULL) { + while ((c = fgetc(file)) != EOF) + printf(""%c"", c); + fclose(file); + printf(""\nName: %s\n"", user->name); + }else { + fclose(file); + printf(""Name: %s\n"", user->name); + } + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + for (int i = 0; i < 10; i++) { + if (user->friends[i] != NULL) { + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts: \n""); + Post *cur_post = user->first_post; + while (cur_post != NULL) { + if (cur_post->next != NULL) { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n\n"", cur_post->contents); + printf(""===\n""); + cur_post = cur_post->next; + printf(""\n""); + } + else { + printf(""From: %s\n"", cur_post->author); + char *date_str = ctime(cur_post->date); + printf(""Date: %s\n"", date_str); + printf(""%s\n"", cur_post->contents); + cur_post = cur_post->next; + } + } + printf(""------------------------------------------\n""); + return 0; +} +",, +63c647bc5f004bf4898cf63f,2023-02-14T09:09:28.856Z,Question from Code,v1,"I am receiving memory leaks, due to this code how would I fix?","int make_post(const User *author, User *target, char *contents) { + + if ((author == NULL) || (target == NULL)) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + new_post->contents = malloc(strlen(contents) + 1); + if (new_post->contents == NULL) { + perror(""malloc""); + return 2; + } + + strcpy(new_post->author, author->name); + strcpy(new_post->contents, contents); + + + time_t *date = malloc(sizeof(time_t)); + if (date == NULL) { + perror(""malloc""); // Look up the documentation for perror + free(new_post->contents); + free(new_post); + return 2; + } + *date = time(NULL); + new_post->date = date; + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-14T09:15:33.457Z,Question from Code,v1,"date is receiving junk when i am printing, whats a fix","int make_post(const User *author, User *target, char *contents) { + + if ((author == NULL) || (target == NULL)) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + new_post->contents = malloc(strlen(contents) + 1); + if (new_post->contents == NULL) { + perror(""malloc""); + return 2; + } + + strcpy(new_post->author, author->name); + strcpy(new_post->contents, contents); + + + time_t *date = malloc(sizeof(time_t)); + if (date == NULL) { + perror(""malloc""); // Look up the documentation for perror + free(new_post->contents); + free(new_post); + return 2; + } + *date = time(NULL); + time_t = date; + new_post->date = *date; + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-14T15:03:02.789Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + + if (author == NULL | target == NULL) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + new_post->contents = malloc(strlen(contents) + 1); + if (new_post->contents == NULL) { + perror(""malloc""); + return 2; + } + + strcpy(new_post->author, author->name); + strcpy(new_post->contents, contents); + + new_post->date = malloc(sizeof(time_t)); + if (new_post->date == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + time(new_post->date); + new_post->next = target->first_post; + target->first_post = new_post; + + time(new_post->date); + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; +}",create post, +63c647bc5f004bf4898cf63f,2023-02-14T15:04:22.914Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + + if (author == NULL | target == NULL) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + new_post->contents = malloc(strlen(contents) + 1); + if (new_post->contents == NULL) { + perror(""malloc""); + return 2; + } + + strcpy(new_post->author, author->name); + strcpy(new_post->contents, contents); + + new_post->date = malloc(sizeof(time_t)); + if (new_post->date == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + time(new_post->date); + new_post->next = target->first_post; + target->first_post = new_post; + + + free(new_post->date); + free(new_post->contents); + free(new_post); + + 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. +", +63c647bc5f004bf4898cf63f,2023-02-14T15:19:05.961Z,Question from Code,v1,why does free(new_user) cause problems,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) > 33) { + return 2; + } + struct user *new_user = malloc(sizeof(struct user)); + strcpy(new_user->name, name); + new_user->next = NULL; + + struct user *last = *user_ptr_add; + while (last != NULL) { + if (strcmp(last->name, name) == 0) { + return 1; + } + last = last->next; + } + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + } + else { + last = *user_ptr_add; + while (last->next != NULL) { + last = last->next; + } + last->next = new_user; + } + init(new_user); + free(new_user); + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-14T16:11:09.608Z,Question from Code,v1,"I receive a segmentation error if i use free() in this code, but if I don't use free I have a memory leak. How would I fix this?","int make_post(const User *author, User *target, char *contents) { + + if (author == NULL | target == NULL) { + return 2; + } + if (!(is_friends(author, target))) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + + strcpy(new_post->author, author->name); + new_post->contents = contents; + + new_post->date = malloc(sizeof(time_t)); + if (new_post->date == NULL) { + perror(""malloc""); // Look up the documentation for perror + return 2; + } + time(new_post->date); + new_post->next = target->first_post; + target->first_post = new_post; + + free(new_post->date); + free(new_post); + + return 0; +} +",, +63c647bc5f004bf4898cf63f,2023-02-14T19:04:19.816Z,Question from Code,v1,"for this code I am receiving a segmentation error, how can i fix +","void cleanup(Post **pst) { + free((*pst)->date); + free((*pst)->contents); + free((*pst)); + *pst = NULL; +} + +int delete_user(const char *name, User **user_ptr_del) { + + struct user *temp = *user_ptr_del, *prev; + if (find_user(name, temp) == NULL) { + return 1; + } + + if (temp != NULL && strcmp(temp->name, name) == 0) { + *user_ptr_del = temp->next; // Changed head + free(temp); // free old head + return 0; + } + + while (temp != NULL && strcmp(temp->name, name) != 0) { + prev = temp; + temp = temp->next; + } + + Post *post = temp->first_post; + while (post != NULL) { + Post *next_post = post->next; + free(post->contents); + free(post->date); + free(post); + post = next_post; + } + post = NULL; + + // If key was not present in linked list + if (temp == NULL){ + return 1; + } + remove_friends(name, user_ptr_del); + + prev->next = temp->next; + + free(temp); // Free memory + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-14T19:05:18.807Z,Question from Code,v1,"for this code I am receiving a segmentation error, how can i fix","void cleanup(Post **pst) { + free((*pst)->date); + free((*pst)->contents); + free((*pst)); + *pst = NULL; +} + +int delete_user(const char *name, User **user_ptr_del) { + + struct user *temp = *user_ptr_del, *prev; + if (find_user(name, temp) == NULL) { + return 1; + } + + if (temp != NULL && strcmp(temp->name, name) == 0) { + *user_ptr_del = temp->next; // Changed head + free(temp); // free old head + return 0; + } + + while (temp != NULL && strcmp(temp->name, name) != 0) { + prev = temp; + temp = temp->next; + } + + Post *post = temp->first_post; + cleanup(&post); + // If key was not present in linked list + if (temp == NULL){ + return 1; + } + remove_friends(name, user_ptr_del); + + prev->next = temp->next; + + free(temp); // Free memory + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-14T19:09:55.895Z,Question from Code,v1,"for this code I am receiving a segmentation error, how can i fix?","void cleanup(Post **pst) { + free((*pst)->date); + free((*pst)->contents); + free((*pst)); + *pst = NULL; +} + +int delete_user(const char *name, User **user_ptr_del) { + + struct user *temp = *user_ptr_del, *prev; + if (find_user(name, temp) == NULL) { + return 1; + } + + if (temp != NULL && strcmp(temp->name, name) == 0) { + *user_ptr_del = temp->next; // Changed head + free(temp); // free old head + return 0; + } + + while (temp != NULL && strcmp(temp->name, name) != 0) { + prev = temp; + temp = temp->next; + } + + Post *post = temp->first_post; + cleanup(&post); + // If key was not present in linked list + if (temp == NULL){ + return 1; + } + + prev->next = temp->next; + + free(temp); // Free memory + return 0; +}",, +63c647bc5f004bf4898cf63f,2023-02-14T19:10:43.507Z,Help Fix Code,v1,,"void cleanup(Post **pst) { + free((*pst)->date); + free((*pst)->contents); + free((*pst)); + *pst = NULL; +} + +int delete_user(const char *name, User **user_ptr_del) { + + struct user *temp = *user_ptr_del, *prev; + if (find_user(name, temp) == NULL) { + return 1; + } + + if (temp != NULL && strcmp(temp->name, name) == 0) { + *user_ptr_del = temp->next; // Changed head + free(temp); // free old head + return 0; + } + + while (temp != NULL && strcmp(temp->name, name) != 0) { + prev = temp; + temp = temp->next; + } + + Post *post = temp->first_post; + cleanup(&post); + // If key was not present in linked list + if (temp == NULL){ + return 1; + } + + prev->next = temp->next; + + free(temp); // Free memory + return 0; +}"," * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends.", +63c647bc5f004bf4898cf63f,2023-02-15T15:22:44.242Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + + const User *curr = head; + while (curr != NULL) { + if (strcmp(curr->name, name) == 0) { + return (User *)curr; + } + curr = curr->next; + } + return NULL; +} +",Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists., +63c647bc5f004bf4898cf63f,2023-03-04T00:08:03.879Z,General Question,v1,how to cast deserialized object without unchecked type error?,,, +63c647bc5f004bf4898cf63f,2023-04-06T00:32:50.134Z,Question from Code,v2,how can i write a newline without creating new write(),"write(client->sock_fd, users->name, strlen(users->name)",, +63c647c15f004bf4898cf671,2023-01-27T05:24:21.591Z,Help Fix Code,v1,,"/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + // nested array -> [[elements at even indicies],[elems at odd indicies]] + //int *even_array = (int *) malloc(sizeof(int) * (length/2 + length%2)); + // int *even_array = malloc(sizeof(int)*(length/2 + length%2)); + // int *odd_array = (int *) malloc(sizeof(int) * (length/ 2)); + // int *odd_array = malloc(sizeof(int)*(length/2)); + + + int **final_array = malloc(sizeof(int *) * 2); + final_array[0] = malloc(sizeof(int)*(length/2 + length%2)); + final_array[1] = malloc(sizeof(int)*(length/2)); + + for (int i = 0; i < length; i++) + { + if (i % 2 == 0) { + final_array[0][i/2] = s[i]; + } else { + final_array[1][i/2] = s[i]; + } + } + + //int **final_array = (int **) malloc(sizeof(int *) * 2); + // int **final_array = malloc(sizeof(int *) * 2); + // final_array[0] = even_array; + // final_array[1] = odd_array; + + return final_array; + + +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + //int *arr = (int *) malloc(sizeof(int) * size); + int *arr = malloc(sizeof(int *)*size); + + for (int i = 0; i < size; i++) { + arr[i] = strtol(strs[i + 1], NULL, 10); + } + + return arr; +}",gives me errors, +63c647c15f004bf4898cf671,2023-01-27T05:26:16.571Z,Help Fix Code,v1,,"/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + // nested array -> [[elements at even indicies],[elems at odd indicies]] + //int *even_array = (int *) malloc(sizeof(int) * (length/2 + length%2)); + // int *even_array = malloc(sizeof(int)*(length/2 + length%2)); + // int *odd_array = (int *) malloc(sizeof(int) * (length/ 2)); + // int *odd_array = malloc(sizeof(int)*(length/2)); + + + int **final_array = malloc(sizeof(int *) * 2); + final_array[0] = malloc(sizeof(int)*(length/2 + length%2)); + final_array[1] = malloc(sizeof(int)*(length/2)); + + for (int i = 0; i < length; i++) + { + if (i % 2 == 0) { + final_array[0][i/2] = s[i]; + } else { + final_array[1][i/2] = s[i]; + } + } + + //int **final_array = (int **) malloc(sizeof(int *) * 2); + // int **final_array = malloc(sizeof(int *) * 2); + // final_array[0] = even_array; + // final_array[1] = odd_array; + + return final_array; + + +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + //int *arr = (int *) malloc(sizeof(int) * size); + int *arr = malloc(sizeof(int)*size); + + for (int i = 0; i < size; i++) { + arr[i] = strtol(strs[i + 1], NULL, 10); + } + + return arr; +} +","I get errors +", +63c647c15f004bf4898cf671,2023-01-28T02:34:53.098Z,Help Fix Code,v1,,"char season[] = 'winter'; +char holiday[20] = ""Valentine's Day"";","Define a string literal called season that has the value +""winter"".", +63c647c15f004bf4898cf671,2023-01-28T02:35:56.111Z,Help Fix Code,v1,,"char season[] = ""winter""; +char holiday[20] = ""Valentine's Day"";","Define a string literal called season that has the value +""winter"".", +63c647c15f004bf4898cf671,2023-02-11T23:56:44.403Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + + User *tail, *new_user; + + // if entered user already exists in this list + for (tail = *user_ptr_add; tail != NULL; tail = tail->next) { + if (strcmp(tail->name, name) == 0) { + return 1; + } + } + + // if given name cannot fit in the 'name' array + if(strlen(name) >= MAX_NAME) { + return 2; + } + + // allocating space for new user + new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""Space for user not allocated properly""); + } + + // init a new user + strncpy(new_user->name, name, MAX_NAME - 1); + new_user->name[MAX_NAME - 1] = '\0'; + new_user->first_post = NULL; + for (int i = 0; i < MAX_FRIENDS; i++) { + new_user->friends[i] = NULL; + } + new_user->next = NULL; + + // inserting new user at tail of linked list + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + } + else { + tail->next = new_user; + } + + return 0; +}","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)", +63c647c25f004bf4898cf676,2023-02-14T21:00:25.993Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + User *curr_user = malloc(sizeof(User)); + curr_user = (User *)head; //TODO: try without typecast + + int user_is_null; + if(curr_user == NULL){ + user_is_null = 1; + }else{ + user_is_null = 0; + } + printf(""%d\n"", user_is_null); + + //traverse the list of users + while(user_is_null < 1){ + //check if the given name and the name of the current user are the same + if(strcmp(name, curr_user->name) == 0){ + return curr_user; + } + curr_user = curr_user->next; + + if(curr_user == NULL){ + user_is_null = 1; + }else{ + user_is_null = 0; + + } + printf(""gonna return NULL""); + return NULL; +}",Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists., +63c647c25f004bf4898cf676,2023-02-14T21:03:32.642Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + User *curr_user = malloc(sizeof(User)); + curr_user = (User *)head; //TODO: try without typecast + + int user_is_null = 0; + if(curr_user == NULL){ + user_is_null = 1; + } + printf(""%d\n"", user_is_null); + + //traverse the list of users + while(user_is_null < 1){ + //check if the given name and the name of the current user are the same + if(strcmp(name, curr_user->name) == 0){ + return curr_user; + } + curr_user = curr_user->next; + + if(curr_user == NULL){ + user_is_null = 1; + }else{ + user_is_null = 0; + + } + printf(""gonna return NULL""); + return NULL; +}",Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists., +63c647c25f004bf4898cf676,2023-02-14T21:10:29.613Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + User *curr_user = head->next; //TODO: try without typecast + + if(strcmp(head->name, name) == 0){ + return (User *)head; + } + + //traverse the list of users + while(curr_user != NULL){ + //check if the given name and the name of the current user are the same + if(strcmp(name, curr_user->name) == 0){ + return curr_user; + } + curr_user = curr_user->next; + } + printf(""gonna return NULL""); + return NULL; +}","Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists. +", +63c647c25f004bf4898cf676,2023-02-14T21:12:07.578Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + User *curr_user = head->next; //TODO: try without typecast + + if(head != NULL && strcmp(head->name, name) == 0){ + return (User *)head; + } + + //traverse the list of users + while(curr_user != NULL){ + //check if the given name and the name of the current user are the same + if(strcmp(name, curr_user->name) == 0){ + return curr_user; + } + curr_user = curr_user->next; + } + printf(""gonna return NULL""); + return NULL; +}",The head pointer is not checked for the name. -> This is fixed by adding a check for the head pointer., +63c647c25f004bf4898cf676,2023-02-14T21:13:26.090Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + User *curr_user = head->next; //TODO: try without typecast + + if(head != NULL && strcmp(head->name, name) == 0){ + return (User *)head; + } + + //traverse the list of users + while(curr_user != NULL){ + //check if the given name and the name of the current user are the same + if(strcmp(name, curr_user->name) == 0){ + return curr_user; + } + curr_user = curr_user->next; + } + printf(""gonna return NULL""); + return NULL; +}",Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists., +63c647c25f004bf4898cf676,2023-02-14T22:00:34.671Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + if(head == NULL){ + printf(""got in!""); + return NULL; + }else{ + if(strcmp(head->name, name) == 0){ + return (User *)head; + }else{ + User *curr_user = head->next; + + //traverse the list of users + while(curr_user){ + //check if the given name and the name of the current user are the same + if(strcmp(name, curr_user->name) == 0){ + User *user_to_return = malloc(sizeof(User)); + user_to_return = curr_user; + return user_to_return; + } + curr_user = curr_user->next; + } + + } + } + printf(""gonna return NULL""); + return NULL; +}",Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists., +63c647c25f004bf4898cf676,2023-02-14T22:03:03.465Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + if(head == NULL){ + printf(""got in!""); + return NULL; + }else{ + if(strcmp(head->name, name) == 0){ + return (User *)head; + }else{ + User *curr_user = head->next; + + //traverse the list of users + while(curr_user){ + //check if the given name and the name of the current user are the same + if(strcmp(name, curr_user->name) == 0){ + return curr_user; + } + curr_user = curr_user->next; + } + + } + } + printf(""gonna return NULL""); + return NULL; +}",Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists., +63c647c25f004bf4898cf676,2023-02-14T22:21:25.946Z,General Question,v1,can i compare unsigned long and int,,, +63c647c25f004bf4898cf676,2023-02-14T22:22:57.682Z,Help Fix Code,v1,,"//first check if the name is too long + if((int)strlen(name) > MAX_NAME){ + return 2; + }",return 2 if the string name is longer than the value stored in MAX_NAME, +63c647c25f004bf4898cf676,2023-02-15T04:15:26.978Z,General Question,v1,how do i use gdb,,, +63c647c25f004bf4898cf676,2023-02-15T04:18:36.286Z,General Question,v1,how do i use valgrand,,, +63c647c25f004bf4898cf676,2023-02-15T05:25:24.392Z,General Question,v1,are pointers in functions freed after the function executes,,, +63c647c25f004bf4898cf676,2023-02-15T05:27:16.423Z,General Question,v1,are pointers created in functions freed after the function executes,,, +63c647c25f004bf4898cf676,2023-02-15T05:27:45.212Z,General Question,v1,are pointers created in function in the heap,,, +63c647c25f004bf4898cf67b,2023-02-15T14:46:36.582Z,General Question,v1,does fgets work on files that are .ascii in c,,, +63c647c25f004bf4898cf67b,2023-02-15T14:47:23.198Z,General Question,v1,"how do i read the contents of an ascii file and print the contents line by line in c? +",,, +63c647c55f004bf4898cf694,2023-01-19T04:05:18.648Z,General Question,v1,What is the return value when the input stream to scanf is closed?,,, +63c647c55f004bf4898cf694,2023-01-19T04:21:24.298Z,General Question,v1,"is this a valid syntax +else if (0 <= temp <= 9)",,, +63c647c55f004bf4898cf694,2023-01-19T04:21:55.202Z,General Question,v1,"Is this a valid syntax in C +else if (0 <= temp <= 9)",,, +63c647c55f004bf4898cf694,2023-01-20T02:41:04.073Z,Question from Code,v1,what is being compared here?,"while (scanf(""%d"", &input) != ""EOF"") {",, +63c647c55f004bf4898cf694,2023-01-20T02:41:35.592Z,Question from Code,v1,"what is the return type of scanf +","while (scanf(""%d"", &input) != ""EOF"") {",, +63c647c55f004bf4898cf694,2023-01-20T02:42:32.793Z,General Question,v1,"in C, how to convert int to string +",,, +63c647c55f004bf4898cf694,2023-01-20T02:43:46.081Z,General Question,v1,how to compare an int with a string,,, +63c647c55f004bf4898cf694,2023-01-20T02:44:29.557Z,General Question,v1,"how would i compare the return value of scanf with a string, without compilation error",,, +63c647ca5f004bf4898cf6bc,2023-01-18T01:34:35.466Z,General Question,v1,What would be an example of how to use scanf,,, +63c647ca5f004bf4898cf6bc,2023-01-18T01:53:31.579Z,General Question,v1,does a c file need a main,,, +63c647ca5f004bf4898cf6bc,2023-01-18T01:58:26.689Z,General Question,v1,what is stdout used for,,, +63c647ca5f004bf4898cf6bc,2023-02-10T19:48:44.430Z,General Question,v1,how to allocate space with malloc,,, +63c647ca5f004bf4898cf6bc,2023-02-14T04:33:38.674Z,General Question,v1,How to change a file name in C,,, +63c647ca5f004bf4898cf6bc,2023-02-14T04:34:23.646Z,General Question,v1,generate a detailed documentation of `rename` with usage examples and explanations,,, +63c647ca5f004bf4898cf6bc,2023-02-14T04:35:31.031Z,General Question,v1,"generate a detailed documentation of `rename(old_name, new_name)` with usage examples and explanations",,, +63c647ca5f004bf4898cf6bc,2023-02-14T04:36:16.426Z,General Question,v1,How to check if a file can be opened?,,, +63c647ca5f004bf4898cf6bc,2023-02-14T16:27:13.543Z,General Question,v1,How to iterate through an ascii code.,,, +63c647ca5f004bf4898cf6bc,2023-02-14T16:28:02.820Z,General Question,v1,How to iterate through an ascii file to print out everything,,, +63c647ca5f004bf4898cf6bc,2023-02-14T18:03:59.830Z,General Question,v1,how to print a time_t?,,, +63c647ca5f004bf4898cf6bc,2023-02-14T18:06:50.897Z,General Question,v1,how to print a blank line?,,, +63c647ca5f004bf4898cf6bc,2023-02-14T18:42:51.839Z,General Question,v1,how to make a variable with the current time,,, +63c647ca5f004bf4898cf6bc,2023-02-14T19:16:17.715Z,Help Fix Code,v1,,"nt create_user(const char *name, User **user_ptr_add) { + User **curr = user_ptr_add; + if(strlen(name) >= MAX_NAME){ + return 2; + } + if(user_ptr_add == NULL){ + User *newbie = (User *) malloc(sizeof(User)); + strcpy(newbie->name, name); + user_ptr_add = newbie; + // add user with name to head of list + return 0; + } + + while(curr->next != NULL){ //traverse through link list + if(curr->name == name){ //check for case 1 at each node + return 1; + } + curr = curr->next //traversalstep + } + //create new user at curr next + curr->next = (User *) malloc(sizeof(User)); + curr->next->name = name; + return 0; +}",fix newbie so it can be added to the User stored at user_ptr_add, +63c647ca5f004bf4898cf6bc,2023-02-14T19:35:57.194Z,General Question,v1,how to check if a file exitsts,,, +63c647ca5f004bf4898cf6bc,2023-02-14T19:36:24.822Z,General Question,v1,how to check if a file can be opened,,, +63c647ca5f004bf4898cf6bc,2023-02-14T20:03:13.039Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + if(strlen(name) >= MAX_NAME){ + return 2; + } + if(user_ptr_add == NULL){ + User *newbie = (User *) malloc(sizeof(User)); + strcpy(newbie->name, name); + user_ptr_add = &newbie; + // add user with name to head of list + return 0; + } + + while(curr->next != NULL){ //traverse through link list + if(curr->name == name){ //check for case 1 at each node + return 1; + } + curr = curr->next; //traversalstep + } + //create new user at curr next + curr->next = (User *) malloc(sizeof(User)); + strcpy(curr->next->name, name); + return 0; +}",Remove address boundary error, +63c647ca5f004bf4898cf6bc,2023-02-14T20:07:36.898Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + if(strlen(name) >= MAX_NAME){ + return 2; + } + if(user_ptr_add == NULL){ + User *newbie = (User *) malloc(sizeof(User)); + strcpy(newbie->name, name); + *user_ptr_add = newbie; + // add user with name to head of list + return 0; + } + + while(curr->next != NULL){ //traverse through link list + if(curr->name == name){ //check for case 1 at each node + return 1; + } + curr = curr->next; //traversalstep + } + //create new user at curr next + curr->next = (User *) malloc(sizeof(User)); + strcpy(curr->next->name, name); + return 0; +}",Remove address boundary error, +63c647ca5f004bf4898cf6bc,2023-02-14T20:09:41.823Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + if(strlen(name) >= MAX_NAME){ + return 2; + } + if(user_ptr_add == NULL){ + User *newbie = (User *) malloc(sizeof(User)); + strcpy(newbie->name, name); + *user_ptr_add = newbie; + // add user with name to head of list + return 0; + } + + while(curr->next != NULL){ //traverse through link list + if(strcmp(curr->name, name)){ //check for case 1 at each node + return 1; + } + curr = curr->next; //traversalstep + } + //create new user at curr next + curr->next = (User *) malloc(sizeof(User)); + strcpy(curr->next->name, name); + return 0; +} +",Remove address boundary error, +63c647ca5f004bf4898cf6bc,2023-02-14T21:30:57.675Z,Question from Code,v1,is this code modifying the *user stored at *user_ptr_add,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + if(strlen(name) >= MAX_NAME){ + return 2; + } + if(user_ptr_add == NULL){ + User *newbie = (User *) malloc(sizeof(User)); + strcpy(newbie->name, name); + *user_ptr_add = newbie; + // add user with name to head of list + return 0; + } + + while(curr != NULL){ //traverse through link list + if(strcmp(curr->name, name)){ //check for case 1 at each node + return 1; + } + curr = curr->next; //traversalstep + } + //create new user at curr + curr = (User *) malloc(sizeof(User)); + strcpy(curr->name, name); + for(int i = 0; i<10; i++){ + curr->friends[i] = NULL; + } + strcpy(curr->profile_pic, """"); + curr->first_post = NULL; + curr->next = NULL; + return 0; +}",, +63c647ca5f004bf4898cf6bc,2023-02-14T21:32:29.420Z,Question from Code,v1,Why is curr always null?,"void list_users(const User *curr) { + printf(""User List\n""); + while(curr != NULL){ + printf("" %s\n"", curr->name); + curr = curr->next; + } + +}",, +63c647ca5f004bf4898cf6bc,2023-02-15T01:21:17.424Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + User *leaver = find_user(name, *user_ptr_del); + User *curr = *user_ptr_del; + if(leaver == NULL || curr == NULL){ + return 1; + } + if(curr == leaver){ + *user_ptr_del = curr->next; + } + for(int i = 0; i < 10; i++){ + if(curr->next->friends[i]==leaver){ + curr->next->friends[i] = NULL; + } + } + while(curr->next != NULL){ + if(curr->next == leaver){ + User *a = curr->next; + free(a); + curr->next = curr->next->next; + } + else{ + for(int i = 0; i < 10; i++){ + if(curr->next->friends[i]==leaver){ + curr->next->friends[i] = NULL; + } + } + } + curr = curr->next; + } + return 1; +}",fix segmentation fault, +63c647ca5f004bf4898cf6bc,2023-02-15T01:22:49.678Z,General Question,v1,how do you remove an element from an array inside a struct,,, +63c647ca5f004bf4898cf6bc,2023-02-15T01:48:27.702Z,General Question,v1,"how to make an array of the same type as: struct user *friends[MAX_FRIENDS]; +",,, +63c647ca5f004bf4898cf6bc,2023-02-15T01:49:41.041Z,General Question,v1,How do you make a struct pointer array of size 10?,,, +63c647ca5f004bf4898cf6bc,2023-02-15T02:01:39.108Z,General Question,v1,how to change an array to point to another array,,, +63c647ca5f004bf4898cf6bc,2023-02-15T02:05:37.656Z,General Question,v1,How to point an array of equal size with an array,,, +63c647ca5f004bf4898cf6bc,2023-02-15T02:07:04.100Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + User *leaver = find_user(name, *user_ptr_del); + User *curr = *user_ptr_del; + if(leaver == NULL || curr == NULL){ + return 1; + } + if(curr == leaver){ + *user_ptr_del = curr->next; + } + for(int i = 0; i < 10; i++){ + if(curr->next->friends[i]==leaver){ + curr->next->friends[i] = NULL; + } + } + while(curr->next != NULL){ + if(curr->next == leaver){ + User *a = curr->next; + free(a); + curr->next = curr->next->next; + } + else{ + User *friends_prime[MAX_FRIENDS]; //making new friends list to avoid segmentation faults + for(int i = 0; i < 10; i++){ + friends_prime[i] = (User *) malloc(sizeof(User)); + int j = 0; + if(curr->next->friends[i] != leaver){ + friends_prime[j] = curr->next->friends[i]; + j++; + } + if(i == 9 && j < i){ + friends_prime[i] = NULL; + } + } + + free(curr->friends); + curr->friends = *friends_prime; + } + curr = curr->next; + } + return 1; +}",replace curr->friends with the array friends_prime, +63c647ca5f004bf4898cf6bc,2023-02-15T02:08:59.829Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + User *leaver = find_user(name, *user_ptr_del); + User *curr = *user_ptr_del; + if(leaver == NULL || curr == NULL){ + return 1; + } + if(curr == leaver){ + *user_ptr_del = curr->next; + } + for(int i = 0; i < 10; i++){ + if(curr->next->friends[i]==leaver){ + curr->next->friends[i] = NULL; + } + } + while(curr->next != NULL){ + if(curr->next == leaver){ + User *a = curr->next; + free(a); + curr->next = curr->next->next; + } + else{ + User *friends_prime[MAX_FRIENDS]; //making new friends list to avoid segmentation faults + for(int i = 0; i < 10; i++){ + friends_prime[i] = (User *) malloc(sizeof(User)); + int j = 0; + if(curr->next->friends[i] != leaver){ + friends_prime[j] = curr->next->friends[i]; + j++; + } + if(i == 9 && j < i){ + friends_prime[i] = NULL; + } + } + + free(curr->friends); + curr->friends = friends_prime; + } + curr = curr->next; + } + return 1; +}",replace curr->friends with the array friends_prime, +63c647ca5f004bf4898cf6bc,2023-02-15T02:59:19.937Z,Question from Code,v1,how to change curr->friends to point to the array friends_prime,"int delete_user(const char *name, User **user_ptr_del) { + User *leaver = find_user(name, *user_ptr_del); + User *curr = *user_ptr_del; + if(leaver == NULL || curr == NULL){ + return 1; + } + if(curr == leaver){ + *user_ptr_del = curr->next; + } + for(int i = 0; i < 10; i++){ + if(curr->next->friends[i]==leaver){ + curr->next->friends[i] = NULL; + } + } + while(curr->next != NULL){ + if(curr->next == leaver){ + User *a = curr->next; + free(a); + curr->next = curr->next->next; + } + else{ + User *friends_prime[MAX_FRIENDS]; //making new friends list to avoid segmentation faults + for(int i = 0; i < 10; i++){ + friends_prime[i] = (User *) malloc(sizeof(User)); + int j = 0; + if(curr->next->friends[i] != leaver){ + friends_prime[j] = curr->next->friends[i]; + j++; + } + if(i == 9 && j < i){ + friends_prime[i] = NULL; + } + } + + free(curr->friends); + curr->friends = friends_prime; + } + curr = curr->next; + } + return 1; +}",, +63c647ca5f004bf4898cf6bc,2023-02-15T03:22:56.953Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + User *leaver = find_user(name, *user_ptr_del); //The user we want to delete + User *curr = *user_ptr_del; //for traversal + if(leaver == NULL || curr == NULL){ + return 1; + } + if(curr == leaver){ + *user_ptr_del = curr->next; + } + else if(curr != leaver){ + int j = 0; + for(int i = 0; i<10; i++){ + if(curr->next->friends[i] != leaver){ + curr->next->friends[j] = curr->next->friends[i]; + j++; + } + } + if(j == 8){ + curr->friends[9] = NULL; + } + } + + while(curr->next != NULL){ + if(curr->next == leaver){ + User *a = curr->next; + free(a); + curr->next = curr->next->next; + } + else{ + int j = 0; + for(int i = 0; i < 10; i++){ + if(curr->next->friends[i] != leaver){ + curr->next->friends[j] = curr->next->friends[i]; + j++; + } + } + if(j == 8){ + curr->next->friends[9] = NULL; + } + } + curr = curr->next; + } + return 0; +} +",remove segmentation fault, +63c647ca5f004bf4898cf6bc,2023-02-15T03:25:53.012Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + User *leaver = find_user(name, *user_ptr_del); //The user we want to delete + User *curr = *user_ptr_del; //for traversal + if(leaver == NULL || curr == NULL){ + return 1; + } + if(curr == leaver){ + *user_ptr_del = curr->next; + } + else if(curr != leaver){ + int j = 0; + for(int i = 0; i<10; i++){ + if(curr->next->friends[i] != leaver){ + curr->next->friends[j] = curr->next->friends[i]; + j++; + } + } + if(j == 8){ + curr->friends[9] = NULL; + } + } + + while(curr->next != NULL){ + if(curr->next == leaver){ + User *a = curr->next; + free(a); + curr->next = curr->next->next; + } + else{ + int j = 0; + for(int i = 0; i < 10; i++){ + if(curr->next->friends[i] != leaver){ + curr->next->friends[j] = curr->next->friends[i]; + j++; + } + } + if(j == 8){ + curr->next->friends[9] = NULL; + } + } + curr = curr->next; + } + return 0; +} +",help remove segmentation fault, +63c647ca5f004bf4898cf6bc,2023-02-15T16:26:19.730Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + User *leaver = find_user(name, *user_ptr_del); //The user we want to delete + User *curr = *user_ptr_del; //for traversal + if(leaver == NULL || curr == NULL){ //check if leaver exists and if the userlist has anything in it + return 1; + } + if(curr == leaver){ //checks base case if leaver is there + *user_ptr_del = curr->next; + } + else if(curr != leaver){ //checks friendlist of curr to remove leaver if leaver is not the first element in the linked list + int j = 0; + for(int i = 0; i<10; i++){ + if(curr->next->friends[i] != leaver){ + curr->next->friends[j] = curr->next->friends[i]; + j++; + } + } + if(j == 8){ + curr->friends[9] = NULL; + } + } + while(curr->next != NULL){ //how we traverse through the loop + if(curr->next == leaver){ //action if a user is the leaver + curr->next = curr->next->next; + } + else{ //checkes elements for leaver as a friend + int j = 0; + for(int i = 0; i < 10; i++){ + if(curr->next->friends[i] != leaver){ + curr->next->friends[j] = curr->next->friends[i]; + j++; + } + } + if(j == 8){ + curr->next->friends[9] = NULL; + } + } + curr = curr->next; + } + free(leaver->profile_pic); + free(leaver->name); + for(int i = 0; i<10; i++){ + leaver->friends[i] = NULL; + } + free(leaver->friends); + while(leaver->first_post != NULL){ + Post *b = leaver->first_post->next; + free(leaver->first_post->author); + free(leaver->first_post->contents); + free(leaver->first_post->date); + free(leaver->first_post); + leaver->first_post = b; + } + free(leaver); + + return 0; +}",remove non-zero offset errors, +63c647ca5f004bf4898cf6bc,2023-02-15T16:28:23.168Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + User *leaver = find_user(name, *user_ptr_del); //The user we want to delete + User *curr = *user_ptr_del; //for traversal + if(leaver == NULL || curr == NULL){ //check if leaver exists and if the userlist has anything in it + return 1; + } + if(curr == leaver){ //checks base case if leaver is there + *user_ptr_del = curr->next; + } + else if(curr != leaver){ //checks friendlist of curr to remove leaver if leaver is not the first element in the linked list + int j = 0; + for(int i = 0; i<10; i++){ + if(curr->next->friends[i] != leaver){ + curr->next->friends[j] = curr->next->friends[i]; + j++; + } + } + if(j == 8){ + curr->friends[9] = NULL; + } + } + while(curr->next != NULL){ //how we traverse through the loop + if(curr->next == leaver){ //action if a user is the leaver + curr->next = curr->next->next; + } + else{ //checkes elements for leaver as a friend + int j = 0; + for(int i = 0; i < 10; i++){ + if(curr->next->friends[i] != leaver){ + curr->next->friends[j] = curr->next->friends[i]; + j++; + } + } + if(j == 8){ + curr->next->friends[9] = NULL; + } + } + curr = curr->next; + } + free(leaver->profile_pic); + free(leaver->name); + for(int i = 0; i<10; i++){ + leaver->friends[i] = NULL; + } + free(leaver->friends); + while(leaver->first_post != NULL){ + Post *b = leaver->first_post->next; + free(leaver->first_post->author); + free(leaver->first_post->contents); + free(leaver->first_post->date); + free(leaver->first_post); + leaver->first_post = b; + } + free(leaver); + + return 0; +}",help with freeing memory stored in leaver, +63c647ca5f004bf4898cf6bc,2023-02-15T16:30:26.607Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + User *leaver = find_user(name, *user_ptr_del); //The user we want to delete + User *curr = *user_ptr_del; //for traversal + if(leaver == NULL || curr == NULL){ //check if leaver exists and if the userlist has anything in it + return 1; + } + if(curr == leaver){ //checks base case if leaver is there + *user_ptr_del = curr->next; + } + else if(curr != leaver){ //checks friendlist of curr to remove leaver if leaver is not the first element in the linked list + int j = 0; + for(int i = 0; i<10; i++){ + if(curr->next->friends[i] != leaver){ + curr->next->friends[j] = curr->next->friends[i]; + j++; + } + } + if(j == 8){ + curr->friends[9] = NULL; + } + } + while(curr->next != NULL){ //how we traverse through the loop + if(curr->next == leaver){ //action if a user is the leaver + curr->next = curr->next->next; + } + else{ //checkes elements for leaver as a friend + int j = 0; + for(int i = 0; i < 10; i++){ + if(curr->next->friends[i] != leaver){ + curr->next->friends[j] = curr->next->friends[i]; + j++; + } + } + if(j == 8){ + curr->next->friends[9] = NULL; + } + } + curr = curr->next; + } + free(leaver->profile_pic); + free(leaver->name); + for(int i = 0; i<10; i++){ + leaver->friends[i] = NULL; + } + free(leaver->friends); + while(leaver->first_post != NULL){ + Post *b = leaver->first_post->next; + free(leaver->first_post->author); + free(leaver->first_post->contents); + free(leaver->first_post->date); + free(leaver->first_post); + leaver->first_post = b; + } + free(leaver); + + return 0; +}",friends.c:348:5: error: ‘free’ called on pointer ‘leaver’ with nonzero offset 72, +63c647ca5f004bf4898cf6bc,2023-02-15T16:32:12.182Z,General Question,v1,how do you free an element in a linked list struct?,,, +63c647ca5f004bf4898cf6bc,2023-02-15T16:34:31.794Z,Question from Code,v1,"given these structs, how would you free a user?","typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post;",, +63c647ca5f004bf4898cf6bc,2023-02-15T16:35:44.805Z,Question from Code,v1,"given the structs, make a program that frees the memory associated with a user's friends","typedef struct user { + char name[32]; + char profile_pic[32]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[10]; + struct user *next; +} User; + +typedef struct post { + char author[32]; + char *contents; + time_t *date; + struct post *next; +} Post;",, +63c647ca5f004bf4898cf6bc,2023-02-16T00:06:43.481Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + User *leaver = find_user(name, *user_ptr_del); //The user we want to delete + User *curr = *user_ptr_del; //for traversal + if(leaver == NULL || curr == NULL){ //check if leaver exists and if the userlist has anything in it + return 1; + } + if(curr == leaver){ //checks head case if leaver is there + *user_ptr_del = curr->next; + } + while(curr->next != NULL){ + if(curr->next == leaver){ + curr->next = curr->next->next; + } + curr = curr->next; + } + for(int i = 0; i< 10; i++){ + if(leaver->friends[i] == NULL){ + continue; + } + int k = 0; + for(int j = 0; j<10; j++){ + if(leaver->friends[i]->friends[j] != leaver){ + leaver->friends[i]->friends[k] = leaver->friends[i]->friends[j]; + k++; + } + } + while(k<9){ + leaver->friends[i]->friends[k+1] = NULL; + k+=1; + } + } + // free(leaver->name); + for(int i = 0; i<10; i++){ + leaver->friends[i] = NULL; + } + while(leaver->first_post != NULL){ + Post *temp = leaver->first_post->next; //temp post + free(leaver->first_post->contents); + free(leaver->first_post->date); + free(leaver->first_post); + leaver->first_post = temp; + } + free(leaver); + + return 0; +}",fix memory leak, +63c647ca5f004bf4898cf6bc,2023-02-16T00:07:53.037Z,Help Fix Code,v1,,"#include ""friends.h"" +#include +#include +#include + +/* + * 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) { + User *curr = *user_ptr_add; // pointer to head of user list + if(strlen(name) >= MAX_NAME){ + return 2; + } + if(*user_ptr_add == NULL){ // if user list is empty + User *newbie = (User *) malloc(sizeof(User)); + if(newbie == NULL){ + exit(1); + } + strcpy(newbie->name, name); //copy name + *user_ptr_add = newbie; // add user with name to head of list + return 0; + } + + while(curr->next != NULL){ //traverse through link list + if(strcmp(curr->name, name) == 0){ //check for case 1 at each node + return 1; + } + curr = curr->next; //traversal step + } + if(strcmp(name, curr->name)==0){ + return 1; + } + User *user = (User *) malloc(sizeof(User)); + if(user == NULL){ + exit(1); + } + strcpy(user->name, name); //copy name into user + for(int i = 0; i<10; i++){ + user->friends[i] = NULL; + } + strcpy(user->profile_pic, """"); //set profile pic to empty string + user->first_post = NULL; //set first post to NULL + user->next = NULL; //set next to NULL + curr->next = user; //add user to tail of list + 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; //current user in head after first if statement + while(curr != NULL){ + if(strcmp(curr->name,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 *user = (User *) curr; // pointer to head of user list + printf(""User List\n""); + while(user != NULL){ + printf(""\t%s\n"", user->name); + user = user->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) { + FILE *f = fopen(filename, ""r""); // Open the file for reading. + + // Check for if the filename is too long. + if(strlen(filename) >= MAX_NAME){ + fclose(f); + return 2; + } + // Check for if the file could not be opened. + else if(f == NULL){ + fclose(f); + return 1; + } + else{ + strcpy(user->profile_pic, filename); // Copy the filename to the user's profile picture field. + fclose(f); + return 0; + } +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already fri ends, 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) { + // Find user a(name1) and b(name2) using find_user + User *a = find_user(name1, head); + User *b = find_user(name2, head); + + // Initialize counters for user a and b + int num_a = 0; //number of friends of user a + int num_b = 0; //number of friends of user b + + // Check if either user does not exist + if(a == NULL || b == NULL){ + return 4; + } + + // Check if the two users are the same + if(a == b){ + return 3; + } + + // Find an empty spot in user a's friend list + while(num_a < 10 && a->friends[num_a] != NULL){ + if(a->friends[num_a] == b){ + return 1; + } + num_a += 1; + } + + // Find an empty spot in user b's friend list + while(num_b < 10 && b->friends[num_b] != NULL){ + if(b->friends[num_b] == a){ + return 1; + } + num_b += 1; + } + + // Check if either user has reached the friend limit (10) + if(num_a > 9 || num_b > 9){ + return 2; + } + + // Add user b to user a's friend list + a->friends[num_a] = b; + + // Add user a to user b's friend list + b->friends[num_b] = a; + + return 0; +} + + +/* + * 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) { + // check if user is NULL + if(user == NULL){ + return 1; + } + + // declare a file pointer, a char and a Post pointer + FILE *f; + char c; + Post *currpost = user->first_post; + + // set the file pointer to the opened profile picture file for reading + f = fopen(user->profile_pic, ""r""); + + // if the file exists, print its contents + if(f!=NULL){ + while ((c = fgetc(f)) != EOF) { + printf(""%c"", c); + } + fclose(f); + } + // print the user's name + printf(""\n""); + printf(""%s %s\n"", ""Name:"", user->name); + printf(""------------------------------------------\n""); + + // print the user's friends + printf(""Friends:\n""); + for(int i = 0; i < 10; i++){ + if(user->friends[i] != NULL){ + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + + // print the user's posts + printf(""Posts:\n""); + if(currpost != NULL){ + printf(""From: %s\n"", currpost->author); + printf(""Date: %s\n"", ctime(currpost->date)); + printf(""%s\n"", currpost->contents); + + // iterate over all other elements of the linked list and print each post + while(currpost->next != NULL){ + printf(""\n""); + printf(""===\n""); + printf(""\n""); + printf(""From: %s\n"", currpost->next->author); + printf(""Date: %s\n"", ctime(currpost->next->date)); + printf(""%s\n"", currpost->next->contents); + currpost = currpost->next; + } + } + + // print the separator line and return 0 to indicate success + printf(""------------------------------------------\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) { + Post *target_first_post = target->first_post; // the first post of the target user + time_t *t = malloc(sizeof(time_t)); // allocate memory for time + if(t == NULL){ //checking for malloc error + exit(1); + } + time(t); // get the current time and store it in the time t + Post *newpost = (Post *) malloc(sizeof(Post)); // allocate memory for the new post + if(newpost == NULL){ + exit(1); + } + if(author == NULL || target == NULL){ + return 2; + } + for(int i = 0; i < 10; i++){ // check if target is a friend of author + if(author->friends[i] == target){ + strcpy(newpost->author, author->name); //copy author name into new post + newpost->date = t; // set the date of the new post to the current time + newpost->contents = contents; // set the contents of the new post to the given contents + newpost->next = target_first_post; // adds the old first post to newpost's next to keep order consistent + target->first_post = newpost; // update the first post of the target user to the new post + 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 *leaver = find_user(name, *user_ptr_del); //The user we want to delete + User *curr = *user_ptr_del; //for traversal + if(leaver == NULL || curr == NULL){ //check if leaver exists and if the userlist has anything in it + return 1; + } + if(curr == leaver){ //checks head case if leaver is there + *user_ptr_del = curr->next; + } + while(curr->next != NULL){ + if(curr->next == leaver){ + curr->next = curr->next->next; + } + curr = curr->next; + } + for(int i = 0; i< 10; i++){ + if(leaver->friends[i] == NULL){ + continue; + } + int k = 0; + for(int j = 0; j<10; j++){ + if(leaver->friends[i]->friends[j] != leaver){ + leaver->friends[i]->friends[k] = leaver->friends[i]->friends[j]; + k++; + } + } + while(k<9){ + leaver->friends[i]->friends[k+1] = NULL; + k+=1; + } + } + free(leaver->name); + for(int i = 0; i<10; i++){ + leaver->friends[i] = NULL; + } + while(leaver->first_post != NULL){ + Post *temp = leaver->first_post->next; //temp post + free(leaver->first_post->contents); + free(leaver->first_post->date); + free(leaver->first_post); + leaver->first_post = temp; + } + free(leaver); + + return 0; +}",fix memory leak, +63c647ca5f004bf4898cf6bc,2023-02-28T22:17:57.421Z,General Question,v1,What does char arr[] mean?,,, +63c647ca5f004bf4898cf6bc,2023-02-28T22:18:22.040Z,General Question,v1,is char arr[] stack or read only memory,,, +63c647ca5f004bf4898cf6bc,2023-02-28T22:22:59.168Z,General Question,v1,what does a regular file mean,,, +63c647ca5f004bf4898cf6bc,2023-02-28T22:23:38.244Z,General Question,v1,how to chmod,,, +63c647ca5f004bf4898cf6bc,2023-03-03T21:43:31.914Z,General Question,v1,How do we make sure only a parent can call fork?,,, +63c647ca5f004bf4898cf6bc,2023-03-03T22:00:01.021Z,Question from Code,v1,Why is each the pid of each child class the same?,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",, +63c647ca5f004bf4898cf6bc,2023-03-03T22:07:38.925Z,General Question,v1,How do you make sure a process only creates one fork?,,, +63c647ca5f004bf4898cf6bc,2023-03-03T22:13:34.457Z,Question from Code,v1,how could I make sure each process only produces one distinct process?,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",, +63c647ca5f004bf4898cf6bc,2023-03-10T20:30:25.097Z,Explain Code,v2,,"#include +#include +#include +#include + +#define MAXLINE 32 +#define MAX_PASSWORD 10 + +#define PASSWORD_FILE ""pass.txt"" +int main(void) { + int n, user_length; + char userid[MAXLINE]; + char password[MAXLINE]; + + if ((n = read(STDIN_FILENO, userid, MAX_PASSWORD)) == -1) { + perror(""read""); + exit(1); + } else if (n == 0) { + fprintf(stderr, ""Error: could not read from stdin""); + exit(1); + } + + if (n <= MAX_PASSWORD) { + userid[n] = '\0'; + } + + char *newline; + if ((newline = strchr(userid, '\n')) != NULL) { + *newline = '\0'; + } + + if ((n = read(STDIN_FILENO, password, MAX_PASSWORD)) == -1) { + perror(""read""); + exit(1); + } else if (n == 0) { + fprintf(stderr, ""Error: could not read from stdin""); + exit(1); + } + + if (n <= MAX_PASSWORD) { + password[n] = '\0'; + } + + if ((newline = strchr(password, '\n')) != NULL) { + *newline = '\0'; + } + + strncat(userid, "":"", MAXLINE - strlen(userid) - 1); + user_length = strlen(userid); + strncat(userid, password, MAXLINE - strlen(userid) - 1); + + FILE *fp = fopen(PASSWORD_FILE, ""r""); + if (!fp) { + perror(""fopen""); + exit(1); + } + + char line[MAXLINE]; + while (fgets(line, sizeof(line) - 1, fp)) { + line[strlen(line) - 1] = '\0'; + if (strcmp(userid, line) == 0) { + fclose(fp); + exit(0); + } else if (strncmp(userid, line, user_length) == 0) { + fclose(fp); + exit(2); + } + } + fclose(fp); + exit(3); +}",, +63c647ca5f004bf4898cf6bc,2023-03-10T20:49:27.936Z,Help Write Code,v2,,,,"Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful. " +63c647ca5f004bf4898cf6bc,2023-03-10T21:51:32.999Z,Help Fix Code,v2," The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. +","#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + int fd[2]; + pipe(fd); + + int pid = fork(); + if (pid == 0) { + close(fd[1]); + dup2(fd[0], 0); + execl(""./validate"", ""validate"", user_id, password, NULL); + } + else { + close(fd[0]); + write(fd[1], user_id, strlen(user_id)); + write(fd[1], ""\n"", 1); + write(fd[1], password, strlen(password)); + close(fd[1]); + int status; + wait(&status); + if (WEXITSTATUS(status) == 1) { + printf(SUCCESS""\n""); + } + else if (WEXITSTATUS(status) == 2) { + printf(INVALID""\n""); + } + else if (WEXITSTATUS(status) == 3) { + printf(NO_USER""\n""); + } + } + + return 0; +}",, +63c647ca5f004bf4898cf6bc,2023-03-10T22:03:33.059Z,Help Fix Code,v2,"prints if password verified, invalid password, or no such user","#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + int fd[2]; + pipe(fd); + + int pid = fork(); + if (pid == 0) { + close(fd[1]); + dup2(fd[0], 0); + execl(""./validate"", ""validate"", user_id, password, NULL); + } + else { + close(fd[0]); + write(fd[1], user_id, strlen(user_id)); + write(fd[1], ""\n"", 1); + write(fd[1], password, strlen(password)); + close(fd[1]); + int status; + wait(&status); + if (WEXITSTATUS(status) == 1) { + printf(SUCCESS""\n""); + } + else if (WEXITSTATUS(status) == 2) { + printf(INVALID""\n""); + } + else if (WEXITSTATUS(status) == 3) { + printf(NO_USER""\n""); + } + } + + return 0; +}",, +63c647cb5f004bf4898cf6c6,2023-01-19T23:37:13.639Z,Question from Code,v1,What does this code do?,"#include +#include + +int main() { + char phone[11]; + int num; + int error = 0; + + scanf(""%10s"", phone); + + while (scanf(""%d"", &num) > 0) { + if (num == -1) { + printf(""%s\n"", phone); + } else if (num >= 0 && num <= 9) { + printf(""%c\n"", phone[num]); + } else { + printf(""ERROR\n""); + error = 1; + } + } + + return error; +}",, +63c647cb5f004bf4898cf6c6,2023-01-19T23:38:48.037Z,Explain Code,v1,,"#include +#include + +int main() { + char phone[11]; + int num; + int error = 0; + + scanf(""%10s"", phone); + + while (scanf(""%d"", &num) > 0) { + if (num == -1) { + printf(""%s\n"", phone); + } else if (num >= 0 && num <= 9) { + printf(""%c\n"", phone[num]); + } else { + printf(""ERROR\n""); + error = 1; + } + } + + return error; +}",, +63c647cb5f004bf4898cf6c6,2023-01-22T03:05:56.037Z,General Question,v1,How do you convert string to int?,,, +63c647cc5f004bf4898cf6d0,2023-01-30T00:42:01.850Z,Question from Code,v1,why does this code have segmentation fault?,"int make_post(const User *author, User *target, char *contents) { + + if (author == NULL || target == NULL){ + return 2; } + int are_friends = 0; + for (int i = 0; i < MAX_FRIENDS; i++){ + if ((author->friends)[i] == target){ + are_friends = 1; + } + + } + + if (are_friends == 0){ + + return 1; + } + + struct post *temp1=malloc(sizeof(struct post)); + + temp1 = author->first_post; + struct post *temp2=malloc(sizeof(struct post)); + temp2 = target->first_post; + strcpy((char *)author->name, author->first_post->author); + author->first_post->contents = contents; + + time(author-> first_post->date); + time(target-> first_post->date); + strcpy((char*)author->name, author->first_post->author); + target->first_post->contents = contents; + author->first_post->next = temp1; + target->first_post->next = temp2; + + + + + return 0; +} + +",, +63c647cc5f004bf4898cf6d0,2023-01-30T01:06:05.303Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + + if (author == NULL || target == NULL){ + return 2; } + int are_friends = 0; + for (int i = 0; i < MAX_FRIENDS; i++){ + if ((author->friends)[i] == target){ + are_friends = 1; + } + + } + + if (are_friends == 0){ + + return 1; + } + + Post *temp1=malloc(sizeof(Post)); + + *temp1 = *(author->first_post); + Post *temp2=malloc(sizeof(Post)); + *temp2 = *(target->first_post); + strcpy((char *)author->name, author->first_post->author); + author->first_post->contents = contents; + + time(author-> first_post->date); + time(target-> first_post->date); + strcpy((char*)(author->name), author->first_post->author); + target->first_post->contents = contents; + (author->first_post)->next = temp1; + (target->first_post)->next = temp2; + + + + + return 0; +}",adds the post to the front of the linkedlist of author's and target's posts., +63c647cc5f004bf4898cf6d0,2023-03-08T03:53:11.102Z,Question from Code,v1,why does this code result in segmentation fault?,"Rule *parse_file(FILE *fp) { + // Implement this function and remove the stubbed return statement below. + int count; + char *placeholder = malloc(sizeof(char)*MAXLINE); + char *line = fgets(placeholder, MAXLINE, fp); + Rule *curr = malloc(sizeof(Rule)); + Rule *head = curr; + int com_count=0; + int act = 0; + char **tab; + while (line != NULL){ + if (is_comment_or_empty(placeholder)){ + line = fgets(placeholder, MAXLINE, fp); + continue; + } + char *target = malloc(sizeof(char)*MAXLINE); + char *colon = strchr(placeholder, ':'); + Action *acurr; + if (colon != NULL){ + act = 0; + strncpy(target, placeholder, colon - line-1); + target[colon-line-1] = '\0'; + + strcpy(curr->target, target); + curr -> next_rule = malloc(sizeof(Rule)); + curr = curr->next_rule; + char *dep = colon + 2; + char *tok = strtok(dep, "" ""); + + count = 0; + while (tok != NULL){ + tab[com_count][count] = *tok; + tok = strtok(NULL, "" ""); + count += 1; + + } + tab[com_count][count] = '\0'; + + } + else{ + + if (act==0){ + act = 1; + acurr = malloc(sizeof(Action)); + char *tok = strtok(placeholder, "" ""); + int index = 0; + while (tok != NULL){ + + (acurr -> args)[index] = tok; + tok = strtok(NULL, "" ""); + index ++; + + } + + (acurr -> args)[index] = NULL; + + + } + + else{ + + Action *next = malloc(sizeof(Action)); + char *tok = strtok(placeholder, "" ""); + int index = 0; + while (tok != NULL){ + (next->args)[index] = tok; + tok = strtok(NULL, "" ""); + index ++; + + } + + (acurr -> args)[index] = NULL; + acurr->next_act = next; + acurr = acurr->next_act; + + } + + + + } + + com_count ++; + free(target); + line = fgets(placeholder, MAXLINE, fp); + } + Rule *cur = head; + com_count = 0; + while (cur != NULL){ + count = 0; + + + while (tab[com_count][count] != '\0'){ + Rule *inner_curr = head; + Dependency *dep_cur = malloc(sizeof(Dependency)); + while (inner_curr != NULL){ + if(strcmp(inner_curr->target, &tab[com_count][count]) == 0){ + dep_cur->rule = inner_curr; + dep_cur->next_dep = malloc(sizeof(Dependency)); + dep_cur = dep_cur->next_dep; + + } + inner_curr = inner_curr->next_rule; + } + count ++; + } + com_count ++; + cur = cur->next_rule; + } + return head; +} +",, +63c647cc5f004bf4898cf6d0,2023-03-08T10:21:16.907Z,Help Fix Code,v1,,"#include +#include +#include + +#include ""pmake.h"" +#define deli "" \t\n\r\f\v\\\0"" +int is_comment_or_empty(const char *line) { + for (int i = 0; i < strlen(line); i++){ + if (line[i] == '#') { + return 1; + } + if (line[i] != '\t' && line[i] != ' ') { + return 0; + } + } + return 1; +} + +int main() { + char *filename = ""Makefile""; + FILE *fp=fopen(filename, ""r""); + int count; + char *placeholder = malloc(sizeof(char)*MAXLINE); + char *line = fgets(placeholder, MAXLINE, fp); + int line_count=0; + Rule *curr = malloc(sizeof(Rule)); + + while (line != NULL){ + line = fgets(placeholder, MAXLINE, fp); + line_count ++; + } + + + fseek(fp, 0, SEEK_SET); + + int start = 0; + Rule *temp = curr; + int com_count=0; + + char ***tab = malloc(sizeof(char**)*line_count); + line = fgets(placeholder, MAXLINE, fp); + while (line != NULL){ + if (is_comment_or_empty(placeholder)){ + line = fgets(placeholder, MAXLINE, fp); + continue; + } + + else{ + + char *target = malloc(sizeof(char*)*MAXLINE); + + char *colon = strchr(placeholder, ':'); + Action *acurr; + if (colon != NULL){ + + strncpy(target, placeholder, colon - line); + target[colon-line-1] = '\0'; + + curr->target=target; + + char *dep = malloc(sizeof(Dependency)*MAXLINE); + strcpy(dep, colon + 2); + char *tok = strtok(dep, deli); + + count = 0; + char **str = malloc(sizeof(char*)*MAXLINE); + while (tok != NULL){ + str[count] = tok; + + tok = strtok(NULL, deli); + count += 1; + + } + str[count] = NULL; + tab[com_count] = str; + colon = NULL; + com_count++; + } + + else{ + Action *acurr = malloc(sizeof(Action)); + Action *aprev; + aprev = NULL; + char *tok1 = strtok(placeholder, deli); + int index = 0; + int space_count = 0; + + for (int i = 1; i < MAXLINE; i++){ + if (placeholder[i-1] == ' ' && placeholder[i]!= ' '){ + space_count ++; + } + } + + char **args = malloc((space_count +1)* sizeof(char*)); + + while (tok1 != NULL){ + args[index]= tok1; + tok1 = strtok(NULL, deli); + index ++; + } + args[index]=NULL; + acurr->args=args; + if (aprev != NULL){ + aprev->next_act= acurr;} + acurr=NULL; + + + } + + } + + + + line = fgets(placeholder, MAXLINE, fp); + + if (line==NULL){ + curr->next_rule = NULL; + + + } + else { + curr->next_rule = malloc(sizeof(Rule)); + curr=curr->next_rule; + } + + } + + printf(""%s\n"", temp->target); + return 0; +} +",there shouldn't be an abortion., +63c647cc5f004bf4898cf6d0,2023-03-09T01:51:03.692Z,Help Fix Code,v1,,"void tokenize(char *line, Action *act; Action *next_a){ + + int white_spaces = 0; + int count = 0; + for (int i = 1; i < MAXLINE; i++){ + if (line[i-1] == ' ' &&line[i] != ' '){ + white_spaces += 1; + } + if (line[i] == '\t' || line[i] == '\n' || line[i] == '\r'){ + break; + } + + } + char **args = malloc(sizeof(char*)*(white_spaces+1)); + char *tok = strtok(placeholder, deli); + while (tok != NULL){ + args[count] = tok; + tok = strtok(NULL, deli); + count ++; + } + act->args = args; + act->next_act = next_a; + +} +",avoid forward declaration error/, +63c647cc5f004bf4898cf6d0,2023-03-09T01:52:52.016Z,General Question,v1,"what should i do if i encounter a forward declaration error? +",,, +63c647cc5f004bf4898cf6d0,2023-03-09T03:44:31.299Z,Help Fix Code,v1,,"#include +#include +#include + +#include ""pmake.h"" +#define deli "" \t\n\r\f\v\\"" +int is_comment_or_empty(const char *line) { + for (int i = 0; i < strlen(line); i++){ + if (line[i] == '#') { + return 1; + } + if (line[i] != '\t' && line[i] != ' ') { + return 0; + } + } + return 1; +} + +void tokenize(char *line, Action *act, Action *next_a){ + + int white_spaces = 0; + int count = 0; + for (int i = 1; i < MAXLINE; i++){ + if (line[i-1] == ' ' &&line[i] != ' '){ + white_spaces += 1; + } + if (line[i] == '\t' || line[i] == '\n' || line[i] == '\r'){ + break; + } + + } + char *placeholder; + char **args = malloc(sizeof(char*)*(white_spaces+1)); + char *tok = strtok(placeholder, deli); + while (tok != NULL){ + args[count] = tok; + tok = strtok(NULL, deli); + count ++; + } + act->args = args; + act->next_act = next_a; + +} + +int main() { + char *filename = ""Makefile""; + FILE *fp=fopen(filename, ""r""); + Action *acurr=NULL; + int count=0; + int com_count=0; + char *tok1; + Action *aprev=NULL; + Action *ahead=NULL; + char *placeholder = malloc(sizeof(char)*MAXLINE); + char *line = fgets(placeholder, MAXLINE, fp); + int line_count=0; + Rule *head = malloc(sizeof(Rule)); + Rule *prev = NULL; + while (line != NULL){ + line = fgets(placeholder, MAXLINE, fp); + line_count ++; + } + fseek(fp, 0, SEEK_SET); + Rule *curr = head; + com_count=0; + char ***tab = malloc(sizeof(char**)*line_count); + line = fgets(placeholder, MAXLINE, fp); + char *colon = strchr(line, ':'); + + while (line != NULL){ + if (is_comment_or_empty(placeholder)){ + line = fgets(placeholder, MAXLINE, fp); + } + else{ + colon = strchr(placeholder, ':'); + + + char *target = malloc(sizeof(char)*MAXLINE); + + if (colon != NULL){ + if (ahead != NULL){ + curr->actions=ahead; + + } + if (aprev != NULL){ + aprev->next_act = NULL;} + aprev = NULL; + acurr == NULL; + ahead=NULL; + + + + strncpy(target, placeholder, colon - line); + + target[colon-line-1] = '\0'; + + curr->target=target; + + + + char *dep = malloc(sizeof(Dependency)*MAXLINE); + strcpy(dep, colon + 2); + char *tok = strtok(dep, deli); + count = 0; + char **str = malloc(sizeof(char*)*MAXLINE); + while (tok != NULL){ + str[count] = tok; + + tok = strtok(NULL, deli); + count += 1; + + } + str[count] = NULL; + tab[com_count] = str; + + com_count++;} + + } + + + + + + line = fgets(placeholder, MAXLINE, fp); + if (colon != NULL){ + + curr->next_rule = malloc(sizeof(Rule)); + prev = curr; + curr=curr->next_rule; + } + } + prev->next_rule = NULL; + free(curr); + curr = head; + Action *act = NULL; + Action *act_head; + Action *prev_a = NULL; + int state = 0; + + fseek(fp, 0, SEEK_SET); + line = fgets(placeholder, MAXLINE, fp); + Rule *last_state = NULL; + Action *next_a = NULL; + + while (line != NULL && curr != NULL){ + colon = strchr(placeholder, ':'); + char *target = malloc(sizeof(char)*MAXLINE); + strncpy(target, placeholder, colon-line); + target[colon-line-1]='\0'; + if (colon != NULL && strcmp(curr->target, target)==0&& state==0){ + last_state = curr; + curr = curr -> next_rule; + state = 1;} + else if (colon == NULL && state == 1){ + act = malloc(sizeof(Action)); + tokenize(line, act, NULL); + curr->actions = act_head; + last_state -> actions = act_head; + state = 2; + } + else if (state == 2 && colon == NULL){ + prev_a = act; + next_a = malloc(sizeof(Action)); + tokenize(line, act, next_a); + + + } + + else if (state == 2 && colon != NULL){ + state = 1; + act->next_act = NULL; + next_a = NULL; + act_head=NULL; + last_state = curr; + + } + + else if (state == 1 && colon != NULL){ + state = 1; + next_a = NULL; + act_head = NULL; + last_state = curr; + } + + + line = fgets(placeholder, MAXLINE, fp); + colon = strchr(placeholder, ':'); + } + + + + + curr = head; + com_count = 0; + Rule *cur = head; + fseek(fp, 0, SEEK_SET); + + while (cur != NULL){ + count = 0; + while (tab[com_count][count] != NULL){ + + + Rule *inner_curr = head; + Dependency *dep_curr = NULL; + Dependency *dep_head = NULL; + int found=0; + while (inner_curr != NULL){ + + if(strcmp(inner_curr->target, tab[com_count][count]) == 0){ + found=1; + if (dep_curr==NULL){dep_curr = malloc(sizeof(Dependency)); + dep_head = dep_curr;} + dep_curr->rule = inner_curr; + if (tab[com_count][count+1]!=NULL){ + dep_curr->next_dep = malloc(sizeof(Dependency)); + dep_curr = dep_curr->next_dep; + } + else{ + + dep_curr->next_dep=NULL; + cur->dependencies = dep_head; + } + } + inner_curr = inner_curr->next_rule; + } + + if (found == 0 && dep_head == NULL){ + + dep_curr = malloc(sizeof(Dependency)); + dep_head = dep_curr; + dep_curr->rule = malloc(sizeof(Rule)); + dep_curr->rule->target = tab[com_count][count]; + dep_curr->rule->dependencies = NULL; + dep_curr->rule->actions = NULL; + dep_curr->rule->next_rule = NULL; + curr->next_rule = dep_curr->rule; + curr = curr->next_rule; + cur = cur->next_rule; + + + } + else if(found == 0){ + dep_curr->next_dep = malloc(sizeof(Dependency)); + dep_curr->next_dep->rule=malloc(sizeof(Rule)); + dep_curr->next_dep->rule->target = tab[com_count][count]; + dep_curr->next_dep->rule->next_rule=NULL; + dep_curr->next_dep->rule->dependencies = NULL; + curr->next_rule = dep_curr->next_dep->rule; + curr = curr->next_rule; + dep_curr = dep_curr->next_dep; + cur = cur->next_rule; + + + } + count ++; + } + com_count ++; + cur = cur->next_rule; + } + curr->next_rule = NULL; + + + + while (head != NULL){ + printf(""Targ: %s\n"",head->target); + Dependency *dep = head->dependencies; + while (dep != NULL){ + printf(""Dep: %s\n"", dep->rule->target); + dep = dep->next_dep; + } + + Action *act = head->actions; + while (act != NULL){ + printf(""Act: %s\n"", *(head->actions->args)); + act = act->next_act; + } + head = head->next_rule; + + } + + + + + + + return 0; +}",segmentation fault, +63c647cd5f004bf4898cf6da,2023-01-28T19:13:05.920Z,Question from Code,v1,How to create a post with the current time?,"typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post;",, +63c647cd5f004bf4898cf6da,2023-01-28T19:14:00.507Z,Question from Code,v1,Can you generate a c code segment that creates a post with the current timestamp?,"typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post;",, +63c647cf5f004bf4898cf6e4,2023-02-09T17:03:03.937Z,General Question,v1,"When I declare a struct, can I use it as a pointer the same way as an array?",,, +63c647cf5f004bf4898cf6e4,2023-02-09T17:04:33.094Z,General Question,v1,how do I create a struct,,, +63c647cf5f004bf4898cf6e4,2023-02-09T17:08:44.428Z,General Question,v1,How do I assign values to parameters of structs,,, +63c647cf5f004bf4898cf6e4,2023-02-09T17:09:38.107Z,General Question,v1,"When I use the dot operator on structs, does it give me a pointer?",,, +63c647cf5f004bf4898cf6e4,2023-02-09T17:10:13.036Z,General Question,v1,How do I assign a value to a struct?,,, +63c647cf5f004bf4898cf6e4,2023-02-09T17:15:34.092Z,General Question,v1,How do I close a bitmap file,,, +63c647cf5f004bf4898cf6e4,2023-02-09T17:17:04.911Z,General Question,v1,how do I deallocate memory,,, +63c647cf5f004bf4898cf6e4,2023-02-09T17:28:06.769Z,General Question,v1,what does segmentation fault mean,,, +63c647cf5f004bf4898cf6e4,2023-02-09T17:31:54.493Z,General Question,v1,how do I use gdb,,, +63c647cf5f004bf4898cf6e4,2023-02-09T17:34:17.441Z,General Question,v1,how do I use gdb to find the value of a variable,,, +63c647cf5f004bf4898cf6e4,2023-02-09T17:36:53.750Z,General Question,v1,how to step out using gdb,,, +63c647cf5f004bf4898cf6e4,2023-02-09T17:39:10.557Z,Question from Code,v1,"I'm getting a segmentation fault, and I think its from this function. Do you know why?","struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **to_return = malloc(sizeof(struct pixel **) * height); + for (int i = 0; i < height; i++) { + to_return[i] = malloc(sizeof(struct pixel *) * width); + } + fseek(image, pixel_array_offset, SEEK_SET); + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + struct pixel my_pixel; + fread(my_pixel.blue, sizeof(unsigned char), 1, image); + fread(my_pixel.green, sizeof(unsigned char), 1, image); + fread(my_pixel.red, sizeof(unsigned char), 1, image); + to_return[i][j] = my_pixel; + } + } + return to_return; +}",, +63c647cf5f004bf4898cf6e4,2023-02-09T17:41:41.736Z,General Question,v1,Can you explain a little more from your previous answer?,,, +63c647cf5f004bf4898cf6e4,2023-02-09T17:43:09.951Z,Question from Code,v1,I still don't understand why I'm getting a segmentation fault,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **to_return = malloc(sizeof(struct pixel **) * height); + for (int i = 0; i < height; i++) { + to_return[i] = malloc(sizeof(struct pixel *) * width); + } + fseek(image, pixel_array_offset, SEEK_SET); + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + struct pixel my_pixel; + fread(my_pixel.blue, sizeof(unsigned char), 1, image); + fread(my_pixel.green, sizeof(unsigned char), 1, image); + fread(my_pixel.red, sizeof(unsigned char), 1, image); + to_return[i][j] = my_pixel; + } + } + return to_return; +}",, +63c647cf5f004bf4898cf6e4,2023-02-10T16:44:34.961Z,General Question,v1,"what does ""expression must be a modifiable lvalue"" mean?",,, +63c647cf5f004bf4898cf6e4,2023-02-10T17:06:03.444Z,Question from Code,v1,I'm getting a segmentation fault at line 27. Why?,"int create_user(const char *name, User **user_ptr_add) { + // MAX_NAME - 1 since we need the null terminator in a name, we need the last char to be \0 + if (strlen(name) > MAX_NAME - 1) { + return 2; + } + + // Head of the linked list + User *curr = *user_ptr_add; + + // Case where curr is null + if (curr == NULL) { + User *user = malloc(sizeof(User)); + strncpy(user->name, name, MAX_NAME - 1); + curr = user; + return 0; + } + + // Keep looping until curr is null + while (curr != NULL) { + + // Return if curr has the same name as name i.e. name alraedy exists in linked list + if (curr->name == name) { + return 1; + } + + // Add a new user with name as name to end of list if we reach the end of the linked list + if (curr->next == NULL) { + User *user = malloc(sizeof(User)); + strncpy(user->name, name, MAX_NAME - 1); + user->name[MAX_NAME - 1] = '\0'; + curr->next = user; + return 0; + } + + // Keep traversing linked list + curr = curr->next; + } +}",, +63c647cf5f004bf4898cf6e4,2023-02-14T04:29:40.413Z,Question from Code,v1,What happens to the declaration on line 2 after the c pre-processor?,"#define MAXNAME = 32; +char name[MAXNAME];",, +63c647cf5f004bf4898cf6e4,2023-02-15T00:53:26.734Z,Question from Code,v1,"I'm getting the error ""Conditional jump or move depends on uninitialised value(s)"" from this function at line 26.","int print_user(const User *user) { + // Return if the user doesn't exist + if (user == NULL) { + return 1; + } + + // Print the user's profile pic only if the file exists + FILE *file = fopen(user->profile_pic, ""r""); + if (file != NULL) { + char line[MAX_NAME + 100]; + while(fgets(line, MAX_NAME + 100, file) != NULL) { + printf(""%s"", line); + } + printf(""\n""); + fclose(file); + } + + // Print the user's name + printf(""%s"", ""Name: ""); + printf(""%s\n"", user->name); + printf(""%s\n"", ""------------------------------------------""); + + // Print the user's friends' names + printf(""%s\n"", ""Friends:""); + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user->friends[i] != NULL) { + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""%s\n"", ""------------------------------------------""); + + // Print posts + printf(""%s\n"", ""Posts:""); + Post *curr = user->first_post; + while (curr != NULL) { + printf(""%s"", ""From: ""); + printf(""%s\n"", curr->author); + printf(""%s"", ""Date: ""); + printf(""%s\n"", ctime(curr->date)); + printf(""%s\n"", curr->contents); + + if (curr->next != NULL) { + printf(""\n""); + printf(""%s\n"", ""===""); + printf(""\n""); + } + curr = curr->next; + } + + printf(""%s\n"", ""------------------------------------------""); + + return 0; +}",, +63c647cf5f004bf4898cf6e4,2023-02-28T07:47:24.424Z,General Question,v1,"Which macro is used with wait's status to check if the child process was terminated by a signal? +",,, +63c647cf5f004bf4898cf6e4,2023-02-28T07:49:48.739Z,General Question,v1,What is the name for a child process whose parent terminates before it does?,,, +63c647cf5f004bf4898cf6e4,2023-03-10T21:13:35.206Z,Question from Code,v2,"my code looks like this. +Validate is a program that will validate the username and password. +If the password is correct for this user, it exits with 0. +If the password is incorrect, it exits with 2. +My program keeps exiting with 2 even with a correct password. I'm wondering if my pipes are incorrect","int fd[2]; +if (pipe(fd) == -1) { + perror(""pipe""); +} + +int r = fork(); + +if (r > 0) { // Parent + close(fd[0]); + // // Connect stdout to pipe + // dup2(fd[1], fileno(stdout)); + // close(fd[1]); + + // Feed information into the pipe + if (write(fd[1], user_id, MAXLINE) == -1) { + perror(""write to pipe""); + } + if (write(fd[1], password, MAXLINE) == -1) { + perror(""write to pipe""); + } + + close(fd[1]); + + int status; + if (wait(&status) != -1) { + if (WIFEXITED(status)) { + printf(""[%d] Child exited with %d\n"", getpid(), WEXITSTATUS(status)); + } else { + printf(""[%d] Child exited abnormally\n"", getpid()); + } + } +} else if (r == 0) { // Child + // Connect stdin to pipe + dup2(fd[0], fileno(stdin)); + close(fd[0]); + close(fd[1]); + + // Call exec for validate + execl(""./validate"", ""validate"", NULL); +}",, +63c647cf5f004bf4898cf6e4,2023-03-10T21:28:03.526Z,Question from Code,v2,Valgrind gives an error for lines 17 and 21. What should I do to fix it?,"int fd[2]; +if (pipe(fd) == -1) { + perror(""pipe""); +} + +int r = fork(); + +if (r > 0) { // Parent + close(fd[0]); + // // Connect stdout to pipe + // dup2(fd[1], fileno(stdout)); + // close(fd[1]); + + // Feed information into the pipe + if (write(fd[1], user_id, MAXLINE) == -1) { + perror(""write to pipe""); + } + if (write(fd[1], password, MAXLINE) == -1) { + perror(""write to pipe""); + } + + close(fd[1]); + + int status; + if (wait(&status) != -1) { + if (WIFEXITED(status)) { + printf(""[%d] Child exited with %d\n"", getpid(), WEXITSTATUS(status)); + } else { + printf(""[%d] Child exited abnormally\n"", getpid()); + } + } +} else if (r == 0) { // Child + // Connect stdin to pipe + dup2(fd[0], fileno(stdin)); + close(fd[0]); + close(fd[1]); + + // Call exec for validate + execl(""./validate"", ""validate"", NULL); +}",, +63c647cf5f004bf4898cf6e4,2023-03-12T22:53:46.977Z,Question from Code,v2,I'm getting a segmentation fault at line 50,"typedef struct rule_node { + char *target; + Dependency *dependencies; + Action *actions; + struct rule_node *next_rule; +} Rule; + +Rule *parse_file(FILE *fp) { + // Implement this function and remove the stubbed return statement below. + + // For reading lines + char line[MAXLINE + 1]; + + // For linked list traversal + Rule *curr = NULL; + + // Create a pointer to the head of our rules linked list + Rule *head = curr; + + while (fgets(line, MAXLINE + 1, fp) != NULL) { + // Change new line character to null terminator + int len = strlen(line); + line[len - 1] = '\0'; + + // Only do things to lines that are not comments or empty + if (!is_comment_or_empty(line)) { + // Case for action line + if (line[0] == '\t') { + } + // Case for target line + else { + // Getting target + char *p = strstr(line, "" :""); + int length = p - line; + line[length] = '\0'; + + // Creating a new rule + Rule *rule = malloc(sizeof(Rule)); + if (curr == NULL) { + curr = rule; + head = curr; + } else { + curr->next_rule = rule; + curr = curr->next_rule; + } + rule->next_rule = NULL; + strcpy(rule->target, line); + rule->dependencies = NULL; + rule->actions = NULL; + + // TODO: deal with dependencies + char *dependencies = p + 2; + } + } + } + curr = head; + while (curr != NULL) { + printf(""%s\n"", curr->target); + curr = curr->next_rule; + } + return head; +}",, +63c647cf5f004bf4898cf6e4,2023-03-13T00:51:12.972Z,Question from Code,v2,I'm getting a segmentation fault. Line is a character array.,"char *action; +strcpy(action, line); +printf(""%s\n"", action);",, +63c647cf5f004bf4898cf6e4,2023-03-13T02:49:04.919Z,General Question,v2,how do I use stat,,, +63c647cf5f004bf4898cf6e4,2023-03-13T03:01:31.764Z,Question from Code,v2,I'm confused how to use stat to find last modified time. Here's what I have.,"struct stat *stats = NULL; +stat(""testy.txt"", stats);",, +63c647cf5f004bf4898cf6e4,2023-03-17T18:09:13.254Z,General Question,v2,How does setitimer work,,, +63c647cf5f004bf4898cf6e4,2023-03-17T18:21:12.870Z,General Question,v2,How do I use a timer to have a program wait,,, +63c647d05f004bf4898cf6f3,2023-01-25T02:00:26.085Z,Help Fix Code,v1,,"void fib(int **arr, int count){ + *arr = malloc(sizeof(int) * count); + if (count == 1){ + *arr[0] = 0; + printf(""*arr[0] %d"", *arr[0]); + } + else { + *arr[0] = 0; + printf(""*arr[0] %d"", *arr[0]); + *arr[1] = 1; + printf(""*arr[1] %d"", *arr[1]); + for (int i = 2; i < count; i++){ + *arr[i] = *arr[i-2] + *arr[i-1]; + printf(""*arr[%d] %d"", i, *arr[i]); + } + } +} + +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; +} +",, +63c647d05f004bf4898cf6f3,2023-01-27T21:42:54.854Z,Help Fix Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +int check_permissions(char *, char *); + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + int cutoff = strtol(argv[1], NULL, 10); + + char type; + char permission[10]; + int link; + char owner[32]; + char group[32]; + int size; + char remainder[50]; + + int n; + int eof; + + eof = scanf(""%s %d"", remainder, &n); + + int count = 0; + + if (argc == 3){ + while (eof != EOF){ + eof = scanf(""%c%s %d %s %s %d %[^\n]s\n"", &type, permission, &link, owner, group, &size, remainder); + printf(""type: %c permission: %s link: %d owner: %s group: %s size: %d remainder: %s"", type, permission, link, owner, group, size, remainder); + if ((type == '-') && (check_permissions(permission, argv[2]) == 0) && (size > cutoff)){ + count++; + } + printf("" COUNT %d\n"", count); + } + } + else { + while (eof != EOF){ + eof = scanf(""%c%s %d %s %s %d %s"", &type, permission, &link, owner, group, &size, remainder); + printf(""type: %c permission: %s link: %d owner: %s group: %s size: %d remainder: %s"", type, permission, link, owner, group, size, remainder); + if ((type == '-') && (size > cutoff)){ + count++; + } + printf("" COUNT %d\n"", count); + } + } + + printf(""%d\n"", count); + + return 0; +} + +int check_permissions(char* p1, char* p2){ + for (int i = 0; i < 9; i++){ + if((p2[i] != '-') && (p1[i] == '-')) return 1; + } + return 0; +} +",read each line from standard input and parse it, +63c647d05f004bf4898cf6f3,2023-01-27T21:57:16.276Z,Help Fix Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +int check_permissions(char *, char *); + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + int cutoff = strtol(argv[1], NULL, 10); + + char type; + char permission[10]; + int link; + char owner[32]; + char group[32]; + int size; + char remainder[50]; + + int n; + int eof; + + eof = scanf(""%s %d"", remainder, &n); + + int count = 0; + + while (eof != EOF){ + eof = scanf(""%c%s %d %s %s %d %[^\n]s\n"", &type, permission, &link, owner, group, &size, remainder); + printf(""type: %c permission: %s link: %d owner: %s group: %s size: %d remainder: %s"", type, permission, link, owner, group, size, remainder); + if ((type == '-') && ((argc == 2) || (check_permissions(permission, argv[2]) == 0)) && (size > cutoff)){ + count++; + } + printf("" COUNT %d\n"", count); + } + + printf(""%d\n"", count); + + return 0; +} + +int check_permissions(char* p1, char* p2){ + for (int i = 0; i < 9; i++){ + if((p2[i] != '-') && (p1[i] == '-')) return 1; + } + return 0; +} +",parse each line read from the standard input. The first character from each line is not being read correctly., +63c647d05f004bf4898cf6f3,2023-01-27T21:58:48.693Z,General Question,v1,"how can I read from standard input, line by line (separated by newline)?",,, +63c647d05f004bf4898cf6f3,2023-01-28T00:50:05.030Z,Help Fix Code,v1,,"#include +#include + +int populate_array(int, int *); +int check_sin(int *); + + +int main(int argc, char **argv) { + // TODO: Verify that command line arguments are valid. + if (argc != 2) { + return 2; + } + + int sin = strtol(argv[1], NULL, 10); + int sin_array[10]; + + // TODO: Parse arguments and then call the two helpers in sin_helpers.c + // to verify the SIN given as a command line argument. + + int valid = populate_array(sin, sin_array); + + if (valid != 0){ + return 1; + } + + return check_sin(sin_array); +} + +int populate_array(int sin, int *sin_array) { + for (int i = 8; i >= 0; i--){ + if (sin == 0) { + return 1; // less than 9 digits + } + sin_array[i] = sin % 10; + sin /= 10; + } + if (sin != 0){ + return 1; // more than 9 digits + } + return 0; +} + +// TODO: Implement check_sin +/* + * Return 0 if the given sin_array is a valid SIN, and 1 otherwise. + */ +int check_sin(int *sin_array) { + int sum = 0; + for (int i = 0; i < 9; i += 2){ + sum += sin_array[i]; + } + for (int i = 1; i < 9; i += 2){ + int doubled = sin_array[i] * 2; + sum += doubled % 10 + doubled / 10; + } + if (sum % 10 == 0){ + return 0; + } + else { + return 1; + } +} +","check if the SIN given by command line argument is valid, using the Luhn algorithm", +63c647d65f004bf4898cf720,2023-01-26T20:44:46.458Z,Help Fix Code,v1,,"#include + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + int *array = malloc(sizeof(int) * length); + int i1 = 0; + int i2 = 0; + for (int i = 0; i < length; i++) { + if (i1 != i2) { + array[1][i2] = s[i]; + i2++; + } + else { + array[0][i1] = s[i]; + i2++; + } + } + return array; +} +",split an array from s, +63c647d65f004bf4898cf720,2023-01-26T20:51:28.556Z,General Question,v1,i keep getting the error subscripted value is neither array nor pointer nor vector,,, +63c647d65f004bf4898cf720,2023-01-26T21:06:33.713Z,Help Fix Code,v1,,"#include +#include + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + int **array = malloc(sizeof(int) * length); + array[0][0] = s[0]; + return array; +} +",split the array s into two arrays of index 0 and 1 in array, +63c647d75f004bf4898cf72a,2023-01-18T15:18:48.951Z,General Question,v1,"what does this mean + +c_helper.py:148: in _t + self.assertEqual(stdout, expected_stdout) +E AssertionError: '16416123456733' != '1\n6\n4161234567\n3' +E - 16416123456733 +E ? ^ +E + 1 +E 6 +E 4161234567 +E 3 +E ? + + ^",,, +63c647d75f004bf4898cf72a,2023-01-18T15:20:12.081Z,Question from Code,v1,"how do I fix this error +c_helper.py:148: in _t + self.assertEqual(stdout, expected_stdout) +E AssertionError: '16416123456733' != '1\n6\n4161234567\n3' +E - 16416123456733 +E ? ^ +E + 1 +E 6 +E 4161234567 +E 3 +E ? + + ^","#include + +int main() { + + char phone[11]; + scanf(""%s"", phone); + + + int val = 1; + int num; + while (val == 1) { + val = scanf(""%d"", &num); + if (num == -1) { + printf(""%s"", phone); + } else if (num >= 0 && num <= 9) { + char arg = phone[num]; + printf(""%c"", arg); + } else { + printf(""ERROR""); + return 1; + } + + + + + } + return 0; + +}",, +63c647d75f004bf4898cf72a,2023-01-18T15:22:14.677Z,Help Fix Code,v1,,"#include + +int main() { + + char phone[11]; + scanf(""%s"", phone); + + + int val = 1; + int num; + while (val == 1) { + val = scanf(""%d"", &num); + if (num == -1) { + printf(""%s"", phone); + } else if (num >= 0 && num <= 9) { + char arg = phone[num]; + printf(""%c"", arg); + } else { + printf(""ERROR""); + return 1; + } + + + + + } + return 0; + +}","why does this code raise this error +c_helper.py:148: in _t + self.assertEqual(stdout, expected_stdout) +E AssertionError: '16416123456733' != '1\n6\n4161234567\n3' +E - 16416123456733 +E ? ^ +E + 1 +E 6 +E 4161234567 +E 3 +E ? + + ^", +63c647d75f004bf4898cf72a,2023-01-18T15:27:04.894Z,Help Fix Code,v1,,"#include + +int main() { + + char phone[11]; + scanf(""%s"", phone); + + + int val = 1; + int num; + while (val == 1) { + val = scanf(""%d"", &num); + if (num == -1) { + printf(""%s"", phone); + } else if (num >= 0 && num <= 9) { + char arg = phone[num]; + printf(""%c"", arg); + } else { + printf(""ERROR""); + return 1; + } + + + + + } + return 0; + +}","c_helper.py:148: in _t + self.assertEqual(stdout, expected_stdout) +E AssertionError: '1 \n6 \n41612345673 \n3' != '1\n6\n4161234567\n3' +E - 1 +E ? - +E + 1 +E - 6 +E ? - +E + 6 +E - 41612345673 +E ? -- +E + 4161234567 +E 3", +63c647d75f004bf4898cf72a,2023-01-18T15:28:40.594Z,Help Fix Code,v1,,"#include + +int main() { + + char phone[11]; + scanf(""%s"", phone); + + + int val = 1; + int num; + while (val == 1) { + val = scanf(""%d"", &num); + if (num == -1) { + printf(""%s"", phone); + } else if (num >= 0 && num <= 9) { + char arg = phone[num]; + printf(""%c \n"", arg); + } else { + printf(""ERROR""); + return 1; + } + + + + + } + return 0; + +}","c_helper.py:148: in _t + self.assertEqual(stdout, expected_stdout) +E AssertionError: '1 \n6 \n41612345673 \n3' != '1\n6\n4161234567\n3' +E - 1 +E ? - +E + 1 +E - 6 +E ? - +E + 6 +E - 41612345673 +E ? -- +E + 4161234567 +E 3", +63c647d75f004bf4898cf72a,2023-01-18T15:32:15.943Z,Help Fix Code,v1,,"#include + +int main() { + + char phone[11]; + scanf(""%s"", phone); + + + int val = 1; + int num; + while (val == 1) { + val = scanf(""%d"", &num); + if (num == -1) { + printf(""%s\n"", phone); + } else if (num >= 0 && num <= 9) { + char arg = phone[num]; + printf(""%c\n"", arg); + } else { + printf(""ERROR""); + return 1; + } + + + + + } + return 0; + +}","c_helper.py:148: in _t + self.assertEqual(stdout, expected_stdout) +E AssertionError: '1\n6\n4161234567\n3\n3' != '1\n6\n4161234567\n3' +E 1 +E 6 +E 4161234567 +E - 3 +E 3", +63c647d75f004bf4898cf72a,2023-01-18T15:33:46.938Z,Help Fix Code,v1,,"#include + +int main() { + + char phone[11]; + scanf(""%s"", phone); + + + int val = 1; + int num; + while (val == 1) { + val = scanf(""%d"", &num); + if (num == -1) { + printf(""%s\n"", phone); + } else if (num >= 0 && num <= 9) { + char arg = phone[num]; + printf(""%c\n"", arg); + } else { + printf(""ERROR""); + return 1; + } + + + + + } + return 0; + +}","Your task is to write a C program called phone_loop.c. This program will again read from standard input using scanf and take no command-line arguments. Similar to phone.c, this program reads a 10-character string as the first input value but then it repeatedly reads integers until standard input is closed. (Hint: Use a while loop that checks the return value of scanf to accomplish this. Check the man page for scanf to determine the return value when the input stream is closed.) + +After each integer the output produced is as before: + +if the integer is -1, the full string is printed +if the integer is between 0 and 9, the individual character at that position is printed +if the integer is less than -1 or greater than 9, the message ""ERROR"" is printed (to stdout) +In each case the printing is followed by a newline character. + +When the program finishes running, main returns with a 0 return code if there were no errors and with a 1 return code otherwise.", +63c647d75f004bf4898cf72a,2023-01-18T15:38:27.299Z,Help Fix Code,v1,,"#include + +int main() { + + char phone[11]; + scanf(""%s"", phone); + + + int val = 1; + int num; + while (val == 1) { + val = scanf(""%d"", &num); + if (num == -1) { + printf(""%s\n"", phone); + } else if (num >= 0 && num <= 9) { + char arg = phone[num]; + printf(""%c\n"", arg); + } else { + printf(""ERROR""); + return 1; + } + + + + + } + return 0; + +}","I have this error: +c_helper.py:148: in _t + self.assertEqual(stdout, expected_stdout) +E AssertionError: '1\n6\n4161234567\n3\n3' != '1\n6\n4161234567\n3' + +I want my code to do this. +Your task is to write a C program called phone_loop.c. This program will again read from standard input using scanf and take no command-line arguments. Similar to phone.c, this program reads a 10-character string as the first input value but then it repeatedly reads integers until standard input is closed. (Hint: Use a while loop that checks the return value of scanf to accomplish this. Check the man page for scanf to determine the return value when the input stream is closed.) + +After each integer the output produced is as before: + +if the integer is -1, the full string is printed +if the integer is between 0 and 9, the individual character at that position is printed +if the integer is less than -1 or greater than 9, the message ""ERROR"" is printed (to stdout) +In each case the printing is followed by a newline character. + +When the program finishes running, main returns with a 0 return code if there were no errors and with a 1 return code otherwise. + +Note: the program must not print anything other than then the values specified.", +63c647d75f004bf4898cf72a,2023-01-18T15:40:05.188Z,Help Fix Code,v1,,"#include + +int main() { + + char phone[11]; + scanf(""%s"", phone); + + + int val = 1; + int num; + while (val == 1) { + val = scanf(""%d"", &num); + if (num == -1) { + printf(""%s\n"", phone); + } else if (num >= 0 && num <= 9) { + char arg = phone[num]; + printf(""%c\n"", arg); + } else { + printf(""ERROR""); + return 1; + } + + + + + } + return 0; + +}"," I have this error: c_helper.py:148: in _t self.assertEqual(stdout, expected_stdout) E AssertionError: '1\n6\n4161234567\n3\n3' != '1\n6\n4161234567\n3' I want my code to do this. Your task is to write a C program called phone_loop.c. This program will again read from standard input using scanf and take no command-line arguments. Similar to phone.c, this program reads a 10-character string as the first input value but then it repeatedly reads integers until standard input is closed. (Hint: Use a while loop that checks the return value of scanf to accomplish this. Check the man page for scanf to determine the return value when the input stream is closed.) After each integer the output produced is as before: if the integer is -1, the full string is printed if the integer is between 0 and 9, the individual character at that position is printed if the integer is less than -1 or greater than 9, the message ""ERROR"" is printed (to stdout) In each case the printing is followed by a newline character. When the program finishes running, main returns with a 0 return code if there were no errors and with a 1 return code otherwise. Note: the program must not print anything other than then the values specified. + +I do have a return 0", +63c647d75f004bf4898cf72a,2023-01-18T15:42:05.506Z,General Question,v1,in C how can I check if standard input is closed using scanf,,, +63c647d75f004bf4898cf72a,2023-01-18T15:49:05.292Z,Help Fix Code,v1,,"#include + +int main() { + + char phone[11]; + scanf(""%s"", phone); + + + int val = 1; + int num; + while (val == 1) { + val = scanf(""%d"", &num); + if (num == -1) { + printf(""%s\n"", phone); + } else if (num >= 0 && num <= 9) { + char arg = phone[num]; + printf(""%c\n"", arg); + } else { + printf(""ERROR""); + return 1; + } + + + + + } + return 0; + +}","why does my code raise this error? + +c_helper.py:148: in _t + self.assertEqual(stdout, expected_stdout) +E AssertionError: '1\n6\n4161234567\n3\n3' != '1\n6\n4161234567\n3' +E 1 +E 6 +E 4161234567 +E - 3 +E 3", +63c647d75f004bf4898cf72a,2023-01-24T18:17:02.449Z,General Question,v1,how can scanf read certain lines,,, +63c647d75f004bf4898cf72a,2023-01-24T18:18:02.362Z,General Question,v1,how to use scanf to read permissions and file size from redirected input,,, +63c647d75f004bf4898cf72a,2023-01-25T01:44:10.583Z,Question from Code,v1,why does this function give me a Segmentation fault,"void fib(int **heap_loc, int n) { + *heap_loc = malloc(sizeof(int) * n); + for (int i = 2; i <= n; n++) { + if (i == 0) { + *heap_loc[i] = 0; + } else if (i == 1) { + *heap_loc[i] = 1; + } else { + *heap_loc[i] = *heap_loc[i - 1] + *heap_loc[i - 2]; + } + } +}",, +63c647d75f004bf4898cf72a,2023-01-25T01:49:41.277Z,General Question,v1,"what is this error + malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.",,, +63c647d75f004bf4898cf72a,2023-01-25T01:50:56.566Z,Question from Code,v1,how to fix this error malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.,"void fib(int **heap_loc, int n) { + *heap_loc = malloc(sizeof(int) * n); + for (int i = 0; i <= n; i++) { + if (i == 0) { + (*heap_loc)[i] = 0; + } else if (i == 1) { + (*heap_loc)[i] = 1; + } else { + (*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2]; + } + } +}",, +63c647d75f004bf4898cf72a,2023-01-25T01:52:42.864Z,Question from Code,v1,"fix this error + +malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.","void fib(int **heap_loc, int n) { + *heap_loc = malloc(sizeof(int) * n); + for (int i = 0; i < n + 1; i++) { + if (i == 0) { + (*heap_loc)[i] = 0; + } else if (i == 1) { + (*heap_loc)[i] = 1; + } else { + (*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2]; + } + } +} + +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; +} +",, +63c647d75f004bf4898cf72a,2023-01-25T03:38:14.327Z,Question from Code,v1,how to fix this error malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.,"void fib(int **heap_loc, int n) { + *heap_loc = malloc(sizeof(int) * n); + for (int i = 0; i <= n; i++) { + if (i == 0) { + (*heap_loc)[i] = 0; + } else if (i == 1) { + (*heap_loc)[i] = 1; + } else { + (*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2]; + } + } +}",, +63c647d75f004bf4898cf72a,2023-01-25T03:38:58.776Z,Question from Code,v1," error malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed. + +why does changing loop condition <= to < fix the problem?","void fib(int **heap_loc, int n) { + *heap_loc = malloc(sizeof(int) * n); + for (int i = 0; i <= n; i++) { + if (i == 0) { + (*heap_loc)[i] = 0; + } else if (i == 1) { + (*heap_loc)[i] = 1; + } else { + (*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2]; + } + } +}",, +63c647d75f004bf4898cf72a,2023-01-25T03:39:40.523Z,Question from Code,v1,why would an incorrect loop condition result in malloc error,"void fib(int **heap_loc, int n) { + *heap_loc = malloc(sizeof(int) * n); + for (int i = 0; i <= n; i++) { + if (i == 0) { + (*heap_loc)[i] = 0; + } else if (i == 1) { + (*heap_loc)[i] = 1; + } else { + (*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2]; + } + } +}",, +63c647d75f004bf4898cf72a,2023-01-25T03:40:20.223Z,Question from Code,v1,"why would an incorrect loop condition result in malloc error, but only for certain n values (such as n = 10, n = 14)","void fib(int **heap_loc, int n) { + *heap_loc = malloc(sizeof(int) * n); + for (int i = 0; i <= n; i++) { + if (i == 0) { + (*heap_loc)[i] = 0; + } else if (i == 1) { + (*heap_loc)[i] = 1; + } else { + (*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2]; + } + } +}",, +63c647d75f004bf4898cf72a,2023-01-27T00:49:08.439Z,Question from Code,v1,why does this result in an segmentation fault.,"#include +#include + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + + } + return arr_indexes; +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int **ints = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) { + ints[i] = (int *) strtol(strs[i], NULL, 10); + } + return *ints; + + +} + + +int main(int argc, char **argv) { + /* Replace the comments in the next two lines with the appropriate + arguments. Do not add any additional lines of code to the main + function or make other changes. + */ + int *full_array = build_array(argv, sizeof(argv) / sizeof(char)); + int **result = split_array(full_array, sizeof(argv) / sizeof(char)); + + printf(""Original array:\n""); + for (int i = 0; i < argc - 1; i++) { + printf(""%d "", full_array[i]); + } + printf(""\n""); + + printf(""result[0]:\n""); + for (int i = 0; i < argc / 2; i++) { + printf(""%d "", result[0][i]); + } + printf(""\n""); + + printf(""result[1]:\n""); + for (int i = 0; i < (argc - 1) / 2; i++) { + printf(""%d "", result[1][i]); + } + printf(""\n""); + free(full_array); + free(result[0]); + free(result[1]); + free(result); + return 0; +} +",, +63c647d75f004bf4898cf72a,2023-01-27T00:49:59.031Z,Question from Code,v1,where would an segmentation fault arise in this code?,"#include +#include + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + + } + return arr_indexes; +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int **ints = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) { + ints[i] = (int *) strtol(strs[i], NULL, 10); + } + return *ints; + + +} + + +int main(int argc, char **argv) { + /* Replace the comments in the next two lines with the appropriate + arguments. Do not add any additional lines of code to the main + function or make other changes. + */ + int *full_array = build_array(argv, sizeof(argv) / sizeof(char)); + int **result = split_array(full_array, sizeof(argv) / sizeof(char)); + + printf(""Original array:\n""); + for (int i = 0; i < argc - 1; i++) { + printf(""%d "", full_array[i]); + } + printf(""\n""); + + printf(""result[0]:\n""); + for (int i = 0; i < argc / 2; i++) { + printf(""%d "", result[0][i]); + } + printf(""\n""); + + printf(""result[1]:\n""); + for (int i = 0; i < (argc - 1) / 2; i++) { + printf(""%d "", result[1][i]); + } + printf(""\n""); + free(full_array); + free(result[0]); + free(result[1]); + free(result); + return 0; +} +",, +63c647d75f004bf4898cf72a,2023-01-27T00:50:24.677Z,Question from Code,v1,which line causes an segmentation fault,"#include +#include + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + + } + return arr_indexes; +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int **ints = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) { + ints[i] = (int *) strtol(strs[i], NULL, 10); + } + return *ints; + + +} + + +int main(int argc, char **argv) { + /* Replace the comments in the next two lines with the appropriate + arguments. Do not add any additional lines of code to the main + function or make other changes. + */ + int *full_array = build_array(argv, sizeof(argv) / sizeof(char)); + int **result = split_array(full_array, sizeof(argv) / sizeof(char)); + + printf(""Original array:\n""); + for (int i = 0; i < argc - 1; i++) { + printf(""%d "", full_array[i]); + } + printf(""\n""); + + printf(""result[0]:\n""); + for (int i = 0; i < argc / 2; i++) { + printf(""%d "", result[0][i]); + } + printf(""\n""); + + printf(""result[1]:\n""); + for (int i = 0; i < (argc - 1) / 2; i++) { + printf(""%d "", result[1][i]); + } + printf(""\n""); + free(full_array); + free(result[0]); + free(result[1]); + free(result); + return 0; +} +",, +63c647d75f004bf4898cf72a,2023-01-27T00:52:14.255Z,Help Fix Code,v1,,"#include +#include + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + + } + return arr_indexes; +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int **ints = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) { + ints[i] = (int *) strtol(strs[i], NULL, 10); + } + return *ints; + + +} + + +int main(int argc, char **argv) { + /* Replace the comments in the next two lines with the appropriate + arguments. Do not add any additional lines of code to the main + function or make other changes. + */ + int *full_array = build_array(argv, sizeof(argv) / sizeof(char)); + int **result = split_array(full_array, sizeof(argv) / sizeof(char)); + + printf(""Original array:\n""); + for (int i = 0; i < argc - 1; i++) { + printf(""%d "", full_array[i]); + } + printf(""\n""); + + printf(""result[0]:\n""); + for (int i = 0; i < argc / 2; i++) { + printf(""%d "", result[0][i]); + } + printf(""\n""); + + printf(""result[1]:\n""); + for (int i = 0; i < (argc - 1) / 2; i++) { + printf(""%d "", result[1][i]); + } + printf(""\n""); + free(full_array); + free(result[0]); + free(result[1]); + free(result); + return 0; +} +",see doctoring, +63c647d75f004bf4898cf72a,2023-01-27T00:53:48.571Z,Question from Code,v1,does this function raise segmentation fault,"int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + + } + return arr_indexes; +}",, +63c647d75f004bf4898cf72a,2023-01-27T00:54:34.389Z,Question from Code,v1,does this code raise segmentation fault,"int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(*int)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + + } + return arr_indexes; +}",, +63c647d75f004bf4898cf72a,2023-01-27T00:55:21.009Z,Question from Code,v1,does this function raise segmentation fault,"int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + + } + return arr_indexes; +}",, +63c647d75f004bf4898cf72a,2023-01-27T00:56:47.211Z,Question from Code,v1,does this code raise segmentation fault,"int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + } + + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + return arr_indexes; +}",, +63c647d75f004bf4898cf72a,2023-01-27T00:59:14.839Z,Question from Code,v1,how do I fix segmentation fault,"int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + } + + arr_indexes[0] = &even_pt; + arr_indexes[1] = &odd_pt; + return arr_indexes; +}",, +63c647d75f004bf4898cf72a,2023-01-27T01:02:16.071Z,Help Fix Code,v1,,"int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + } + + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + return arr_indexes; +}",segmentation fault needs fix, +63c647d75f004bf4898cf72a,2023-01-27T01:04:14.022Z,Help Fix Code,v1,,"int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + } + + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + return arr_indexes; +}","Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division.", +63c647d75f004bf4898cf72a,2023-01-27T01:05:02.342Z,Help Fix Code,v1,,"int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + } + + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + return arr_indexes; +}","Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division.", +63c647d75f004bf4898cf72a,2023-01-27T01:05:27.038Z,Help Fix Code,v1,,"int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + } + + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + return arr_indexes; +}","Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division.", +63c647d75f004bf4898cf72a,2023-01-27T01:06:03.109Z,Question from Code,v1,"what's wrong with the code with behaviour described as: +Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division.","int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + } + + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + return arr_indexes; +}",, +63c647d75f004bf4898cf72a,2023-01-27T01:06:33.657Z,Question from Code,v1,how do I fix the code with behaviour described as: Return a pointer to an array of two dynamically allocated arrays of ints. The first array contains the elements of the input array s that are at even indices. The second array contains the elements of the input array s that are at odd indices. Do not allocate any more memory than necessary. You are not permitted to include math.h. You can do the math with modulo arithmetic and integer division.,"int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + } + + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + return arr_indexes; +}",, +63c647d75f004bf4898cf72a,2023-01-27T01:18:12.428Z,Question from Code,v1,what's wrong with the code," +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int *ints = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) { + ints[i] = (int) strtol(strs[i], NULL, 10); + } + return ints; + + +} +",, +63c647d75f004bf4898cf72a,2023-01-27T01:18:38.005Z,Question from Code,v1,what's wrong with this function?," +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int *ints = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) { + ints[i] = (int) strtol(strs[i], NULL, 10); + } + return ints; + + +} +",, +63c647d75f004bf4898cf72a,2023-01-27T01:19:11.858Z,Help Fix Code,v1,," +int *build_array(char **strs, int size) { + int *ints = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) { + ints[i] = (int) strtol(strs[i], NULL, 10); + } + return ints; + + +} +"," +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ +", +63c647d75f004bf4898cf72a,2023-01-27T01:19:34.690Z,Help Fix Code,v1,," +int *build_array(char **strs, int size) { + int *ints = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) { + ints[i] = (int) strtol(strs[i], NULL, 10); + } + return ints; + + +} +"," +Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + +", +63c647d75f004bf4898cf72a,2023-01-27T01:20:06.049Z,Explain Code,v1,," +int *build_array(char **strs, int size) { + int *ints = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) { + ints[i] = (int) strtol(strs[i], NULL, 10); + } + return ints; + + +} +",, +63c647d75f004bf4898cf72a,2023-01-27T01:22:04.315Z,Explain Code,v1,,"int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + } + + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + return arr_indexes; +}",, +63c647d75f004bf4898cf72a,2023-01-27T01:25:14.977Z,Question from Code,v1,which line causes error,"int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + } + + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + return arr_indexes; +}",, +63c647d75f004bf4898cf72a,2023-01-27T01:26:06.497Z,Question from Code,v1,why does arr_indexes[0] = even_pt; raise an error and how to fix it?,"int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + } + + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + return arr_indexes; +}",, +63c647d75f004bf4898cf72a,2023-01-27T01:28:29.519Z,Question from Code,v1,which line causes error and how to fix?,"#include +#include + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + } + + // arr_indexes[0] = even_pt; + // arr_indexes[1] = odd_pt; + return arr_indexes; +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int *ints = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) { + ints[i] = (int) strtol(strs[i], NULL, 10); + } + return ints; + + +} + + +int main(int argc, char **argv) { + /* Replace the comments in the next two lines with the appropriate + arguments. Do not add any additional lines of code to the main + function or make other changes. + */ + int *full_array = build_array(argv, sizeof(argv) / sizeof(char)); + int **result = split_array(full_array, sizeof(argv) / sizeof(char)); + + printf(""Original array:\n""); + for (int i = 0; i < argc - 1; i++) { + printf(""%d "", full_array[i]); + } + printf(""\n""); + + printf(""result[0]:\n""); + for (int i = 0; i < argc / 2; i++) { + printf(""%d "", result[0][i]); + } + printf(""\n""); + + printf(""result[1]:\n""); + for (int i = 0; i < (argc - 1) / 2; i++) { + printf(""%d "", result[1][i]); + } + printf(""\n""); + free(full_array); + free(result[0]); + free(result[1]); + free(result); + return 0; +} +",, +63c647d75f004bf4898cf72a,2023-01-27T01:46:25.966Z,Question from Code,v1,"why does this code result +Original array: +1 2 3 +result[0]: +2 0 +result[1]: +1 + +instead of +Original array: +1 2 3 +result[0]: +1 3 +result[1]: +2","#include +#include + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + } + + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + return arr_indexes; +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int *ints = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) { + ints[i] = (int) strtol(strs[i], NULL, 10); + } + return ints; + + +} + + +int main(int argc, char **argv) { + /* Replace the comments in the next two lines with the appropriate + arguments. Do not add any additional lines of code to the main + function or make other changes. + */ + int *full_array = build_array(argv + 1, argc - 1); + int **result = split_array(full_array, argc - 1); + + printf(""Original array:\n""); + for (int i = 0; i < argc - 1; i++) { + printf(""%d "", full_array[i]); + } + printf(""\n""); + + printf(""result[0]:\n""); + for (int i = 0; i < argc / 2; i++) { + printf(""%d "", result[0][i]); + } + printf(""\n""); + + printf(""result[1]:\n""); + for (int i = 0; i < (argc - 1) / 2; i++) { + printf(""%d "", result[1][i]); + } + printf(""\n""); + free(full_array); + free(result[0]); + free(result[1]); + free(result); + return 0; +} +",, +63c647d75f004bf4898cf72a,2023-01-27T01:47:24.333Z,Question from Code,v1,why is the output not as desired ,"#include +#include + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + } + + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + return arr_indexes; +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int *ints = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) { + ints[i] = (int) strtol(strs[i], NULL, 10); + } + return ints; + + +} + + +int main(int argc, char **argv) { + /* Replace the comments in the next two lines with the appropriate + arguments. Do not add any additional lines of code to the main + function or make other changes. + */ + int *full_array = build_array(argv + 1, argc - 1); + int **result = split_array(full_array, argc - 1); + + printf(""Original array:\n""); + for (int i = 0; i < argc - 1; i++) { + printf(""%d "", full_array[i]); + } + printf(""\n""); + + printf(""result[0]:\n""); + for (int i = 0; i < argc / 2; i++) { + printf(""%d "", result[0][i]); + } + printf(""\n""); + + printf(""result[1]:\n""); + for (int i = 0; i < (argc - 1) / 2; i++) { + printf(""%d "", result[1][i]); + } + printf(""\n""); + free(full_array); + free(result[0]); + free(result[1]); + free(result); + return 0; +} +",, +63c647d75f004bf4898cf72a,2023-01-27T01:48:37.104Z,Help Fix Code,v1,,"#include +#include + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + } + + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + return arr_indexes; +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int *ints = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) { + ints[i] = (int) strtol(strs[i], NULL, 10); + } + return ints; + + +} + + +int main(int argc, char **argv) { + /* Replace the comments in the next two lines with the appropriate + arguments. Do not add any additional lines of code to the main + function or make other changes. + */ + int *full_array = build_array(argv + 1, argc - 1); + int **result = split_array(full_array, argc - 1); + + printf(""Original array:\n""); + for (int i = 0; i < argc - 1; i++) { + printf(""%d "", full_array[i]); + } + printf(""\n""); + + printf(""result[0]:\n""); + for (int i = 0; i < argc / 2; i++) { + printf(""%d "", result[0][i]); + } + printf(""\n""); + + printf(""result[1]:\n""); + for (int i = 0; i < (argc - 1) / 2; i++) { + printf(""%d "", result[1][i]); + } + printf(""\n""); + free(full_array); + free(result[0]); + free(result[1]); + free(result); + return 0; +} +",-, +63c647d75f004bf4898cf72a,2023-01-27T01:50:07.891Z,Help Fix Code,v1,,"#include +#include + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + int **arr_indexes = malloc(2 * sizeof(int*)); + int even_count = 0; + int odd_count = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_count += 1; + } else { + odd_count += 1; + } + } + int *even_pt = malloc(even_count * sizeof(int)); + int *odd_pt = malloc(odd_count * sizeof(int)); + int even_idx = 0; + int odd_idx = 0; + for (int i = 0; i < length; i++) { + if (s[i] % 2 == 0) { + even_pt[even_idx] = s[i]; + even_idx += 1; + } else { + odd_pt[odd_idx] = s[i]; + odd_idx += 1; + } + } + + arr_indexes[0] = even_pt; + arr_indexes[1] = odd_pt; + return arr_indexes; +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int *ints = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) { + ints[i] = (int) strtol(strs[i], NULL, 10); + } + return ints; + + +} + + +int main(int argc, char **argv) { + /* Replace the comments in the next two lines with the appropriate + arguments. Do not add any additional lines of code to the main + function or make other changes. + */ + int *full_array = build_array(argv + 1, argc - 1); + int **result = split_array(full_array, argc - 1); + + printf(""Original array:\n""); + for (int i = 0; i < argc - 1; i++) { + printf(""%d "", full_array[i]); + } + printf(""\n""); + + printf(""result[0]:\n""); + for (int i = 0; i < argc / 2; i++) { + printf(""%d "", result[0][i]); + } + printf(""\n""); + + printf(""result[1]:\n""); + for (int i = 0; i < (argc - 1) / 2; i++) { + printf(""%d "", result[1][i]); + } + printf(""\n""); + free(full_array); + free(result[0]); + free(result[1]); + free(result); + return 0; +} +",return array of two pointers to one with odd other with even, +63c647d75f004bf4898cf72a,2023-01-27T18:55:04.054Z,Question from Code,v1,"with input ./count_large 1000 rwx------ +with command line input +total 329 +-rwx------ 1 reid staff 1734 Jun 22 14:52 prog +-rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt +-rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz +-rw-r--r-- 1 reid staff 88 Feb 15 2013 xyz.c + +why does the function return 1 instead of 2","#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +// int check_permissions(char *, char *); + +int check_permissions(char *a, char *b) { + for (int i = 0; i < 9; i++) { + if (b[i] == '-') { + ; + } else if (b[i] != a[i]) { + return 1; + } + } + return 0; +} + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + // char input[50]; + int count = 0; + int val = 0; + char permissions[10]; + int file_size; + scanf(""%*s, %*d""); + while (val != -1) { + val = scanf(""%s %*d %*s %*s %d %*s %*d %*s %*s"", permissions, &file_size); + if (val == -1) { + break; + } + // printf(""%s "", permissions); + // printf(""%d\n"", file_size); + int size = strtol(argv[1], NULL, 10); + if (file_size >= size) { + if (argc == 3) { + if (check_permissions(permissions, argv[2]) == 0) { + count += 1; + } + } else { + count += 1; + } + } + } + printf(""%d \n"", count );",, +63c647d75f004bf4898cf72a,2023-01-27T19:03:14.749Z,Question from Code,v1,what's wrong with this code,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +// int check_permissions(char *, char *); + +int check_permissions(char *a, char *b) { + for (int i = 0; i < 9; i++) { + if (b[i] == '-') { + ; + } else if (b[i] != a[i]) { + return 1; + } + } + return 0; +} + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + // char input[50]; + int count = 0; + int val = 0; + char permissions[10]; + int file_size; + scanf(""%*s, %*d""); + while (val != -1) { + val = scanf(""%s %*d %*s %*s %d %*s %*d %*s %*s"", permissions, &file_size); + if (val == -1) { + break; + } + // printf(""%s "", permissions); + // printf(""%d\n"", file_size); + int size = strtol(argv[1], NULL, 10); + if (file_size >= size) { + if (argc == 3) { + if (check_permissions(permissions, argv[2]) == 0) { + count += 1; + } + } else { + count += 1; + } + } + } + printf(""%d \n"", count ); + ",, +63c647d75f004bf4898cf72a,2023-01-27T19:03:51.456Z,Question from Code,v1,which part of the code is wrong,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +// int check_permissions(char *, char *); + +int check_permissions(char *a, char *b) { + for (int i = 0; i < 9; i++) { + if (b[i] == '-') { + ; + } else if (b[i] != a[i]) { + return 1; + } + } + return 0; +} + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + // char input[50]; + int count = 0; + int val = 0; + char permissions[10]; + int file_size; + scanf(""%*s, %*d""); + while (val != -1) { + val = scanf(""%s %*d %*s %*s %d %*s %*d %*s %*s"", permissions, &file_size); + if (val == -1) { + break; + } + // printf(""%s "", permissions); + // printf(""%d\n"", file_size); + int size = strtol(argv[1], NULL, 10); + if (file_size >= size) { + if (argc == 3) { + if (check_permissions(permissions, argv[2]) == 0) { + count += 1; + } + } else { + count += 1; + } + } + } + printf(""%d \n"", count ); + ",, +63c647d75f004bf4898cf72a,2023-01-27T19:05:34.075Z,Help Fix Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +// int check_permissions(char *, char *); + +int check_permissions(char *a, char *b) { + for (int i = 0; i < 9; i++) { + if (b[i] == '-') { + ; + } else if (b[i] != a[i]) { + return 1; + } + } + return 0; +} + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + // char input[50]; + int count = 0; + int val = 0; + char permissions[10]; + int file_size; + scanf(""%*s, %*d""); + while (val != -1) { + val = scanf(""%s %*d %*s %*s %d %*s %*d %*s %*s"", permissions, &file_size); + if (val == -1) { + break; + } + // printf(""%s "", permissions); + // printf(""%d\n"", file_size); + int size = strtol(argv[1], NULL, 10); + if (file_size >= size) { + if (argc == 3) { + if (check_permissions(permissions, argv[2]) == 0) { + count += 1; + } + } else { + count += 1; + } + } + } + printf(""%d \n"", count ); + ",print out permissions match and file size is larger or equal, +63c647d75f004bf4898cf72a,2023-01-27T19:07:02.904Z,Explain Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +// int check_permissions(char *, char *); + +int check_permissions(char *a, char *b) { + for (int i = 0; i < 9; i++) { + if (b[i] == '-') { + ; + } else if (b[i] != a[i]) { + return 1; + } + } + return 0; +} + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + // char input[50]; + int count = 0; + int val = 0; + char permissions[10]; + int file_size; + scanf(""%*s, %*d""); + while (val != -1) { + val = scanf(""%s %*d %*s %*s %d %*s %*d %*s %*s"", permissions, &file_size); + if (val == -1) { + break; + } + // printf(""%s "", permissions); + // printf(""%d\n"", file_size); + int size = strtol(argv[1], NULL, 10); + if (file_size >= size) { + if (argc == 3) { + if (check_permissions(permissions, argv[2]) == 0) { + count += 1; + } + } else { + count += 1; + } + } + } + printf(""%d \n"", count ); + ",, +63c647d75f004bf4898cf72a,2023-01-28T16:22:55.551Z,General Question,v1,"how can I ignore first line in scanf +",,, +63c647d75f004bf4898cf72a,2023-01-28T16:24:40.985Z,General Question,v1,ignore the first character using scanf,,, +63c647d75f004bf4898cf72a,2023-01-28T17:05:51.398Z,General Question,v1,convert int to char[9],,, +63c647d75f004bf4898cf72a,2023-01-28T17:06:21.071Z,General Question,v1,convert int to array,,, +63c647d75f004bf4898cf72a,2023-01-28T17:10:18.028Z,General Question,v1,index an int,,, +63c647d75f004bf4898cf72a,2023-02-08T19:19:36.850Z,Question from Code,v1,why does this raise segmentation fault,"int create_user(const char *name, User **user_ptr_add) { + User *curr = user_ptr_add[0]; + while (curr->next != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + }",, +63c647d75f004bf4898cf72a,2023-02-08T19:20:09.900Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *curr = user_ptr_add[0]; + while (curr->next != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + }",fix the segmentation fault, +63c647d75f004bf4898cf72a,2023-02-08T19:21:30.388Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + while (curr->next != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + }",fix segmentation fault, +63c647d75f004bf4898cf72a,2023-02-08T19:22:55.249Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + while (curr != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + }",fix segmentation fault, +63c647d75f004bf4898cf72a,2023-02-08T19:32:23.616Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + while (curr != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + curr = curr->next; + }",fix this code, +63c647d75f004bf4898cf72a,2023-02-08T19:36:42.121Z,Question from Code,v1,what are the problems of this function,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + User *prev = NULL; + while (curr != NULL) { + if (strcmp(name, curr->name) == 0) { + return 1; + } + prev = curr; + curr = curr->next; + } + // This stage is reached if curr is NULL + if (strlen(name) > MAX_NAME - 1) { + return 2; + } + User *usr = malloc(sizeof(User)); + strncpy(usr->name, name, MAX_NAME); // not a literal so we must copy + usr->next = NULL; + prev->next = usr; + return 0; +}",, +63c647d75f004bf4898cf72a,2023-03-08T19:19:53.305Z,Explain Code,v1,,"/* Convert an array of args to a single space-separated string in buffer. + Returns buffer. Note that memory for args and buffer should be allocated + by the caller. + */ +char *args_to_string(char **args, char *buffer, int size) { + buffer[0] = '\0'; + int i = 0; + while (args[i] != NULL) { + strncat(buffer, args[i], size - strlen(buffer)); + strncat(buffer, "" "", size - strlen(buffer)); + i++; + } + return buffer; +} +",, +63c647d75f004bf4898cf72a,2023-03-08T19:20:31.315Z,Question from Code,v1,what's a buffer?,"/* Convert an array of args to a single space-separated string in buffer. + Returns buffer. Note that memory for args and buffer should be allocated + by the caller. + */ +char *args_to_string(char **args, char *buffer, int size) { + buffer[0] = '\0'; + int i = 0; + while (args[i] != NULL) { + strncat(buffer, args[i], size - strlen(buffer)); + strncat(buffer, "" "", size - strlen(buffer)); + i++; + } + return buffer; +} +",, +63c647d75f004bf4898cf72a,2023-03-10T22:43:54.277Z,Help Fix Code,v1,,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 +#define MAX_PASSWORD 10 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + // TODO + // Remove newline character from end of user_id + + if (strlen(user_id) > MAX_PASSWORD-1) { + printf(""%s"", NO_USER); + return 0; + } + if (strlen(password) > MAX_PASSWORD-1) { + printf(""%s"", INVALID); + return 0; + } + + int pipefd[2]; + pid_t pid; + pipe(pipefd); + + pid = fork(); + if (pid == 0) { + close(pipefd[0]); + if (dup2(pipefd[1], STDOUT_FILENO) == -1) { + perror(""dup2""); + exit(1); + } + close(pipefd[1]); + + execl(""./validate"", ""validate"", user_id, password, NULL); + perror(""execl""); + exit(1); + } else { + close(pipefd[1]); + int status; + pid_t wpid; + + char buffer[MAXLINE]; + ssize_t n; + while ((n = read(pipefd[0], buffer, MAXLINE)) > 0) { + write(STDOUT_FILENO, buffer, n); + } + close(pipefd[0]); + + wpid = waitpid(pid, &status, 0); + if (wpid == -1) { + perror(""waitpid""); + exit(1); + } + + wpid = waitpid(pid, &status, 0); + if (WIFEXITED(status)) { + int exit_status = WEXITSTATUS(status); + if (exit_status == 0) { + printf(""%s"", SUCCESS); + } else if (exit_status == 2) { + printf(""%s"", INVALID); + } else { + printf(""%s"", NO_USER); + } + } + } + + + return 0; + +}","Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful. + +Your program should use the exit status of the validate program to determine which of the three following messages to print: + +""Password verified"" if the user id and password match. +""Invalid password"" if the user id exists, but the password does not match. +""No such user"" if the user id is not recognized +The exact messages are given in the starter code as defined constants. + +The only case that should be handled directly in checkpasswd is the case where either the userid or password are too long. This is to prevent sending the wrong number of bytes to validate. + +Note that in the given password file pass.txt, the ""killerwhales:swim"" has a user id that is too large, and ""monkeys:eatcoconuts"" has a password that is too long. The examples are expected to fail, but the other cases should work correctly. + +You will find the following system calls useful: fork, exec, pipe, dup2, write, wait (along with WIFEXITED, WEXITSTATUS). You may not use popen or pclose in your solution. + +Important: execl arguments +Week 7 Video 6 ""Running Different Programs"" demonstrates a version of execl that takes only two arguments. The signature for execl is: + +int execl(const char *path, const char *arg0, ... /*, (char *)NULL */); +In the video, the execl call only passed two arguments (execl(""./hello"", NULL)), but that shortcut doesn't work on teach.cs. Instead, you need to pass the middle argument (respresenting argv[0]) explicitly: execl(""./hello"", ""hello"", NULL). + +Let's consider two more examples. If you want to call the executable ./giant with the arguments fee fi fo, you would do it like this: execl(""./giant"", ""giant"", ""fee"", ""fi"", ""fo"", NULL); If you want to call ./giant with no arguments you would call it like this: execl(""./giant"", ""giant"", NULL); +", +63c647db5f004bf4898cf748,2023-01-17T21:10:46.418Z,Explain Code,v1,,"include +int main() { + char name[30]; + printf(""Hello CSC209 students!\n""); + fgets(name, 30, stdin); + printf(""Hi %s\n"", name); + return 0; +}",, +63c647db5f004bf4898cf748,2023-01-17T21:12:19.765Z,General Question,v1,what is standard input?,,, +63c647db5f004bf4898cf748,2023-01-18T15:49:29.893Z,General Question,v1,what does scanf do in c?,,, +63c647db5f004bf4898cf748,2023-01-23T20:07:57.797Z,Explain Code,v1,,"scanf(""%[^\n]"", line); +",, +63c647db5f004bf4898cf748,2023-03-08T21:40:14.925Z,Explain Code,v1,,"if (pid == 0) { + //child process; read from pipe and write to stdin of validate + close(pipefd[1]); + + if (dup2(pipefd[0], STDIN_FILENO) == -1) { + perror(""Dup2 failed""); + exit(1); + } + close(pipefd[0]); + execl(""./validate"", ""validate"", NULL); + }",, +63c647db5f004bf4898cf748,2023-03-08T21:41:10.077Z,Question from Code,v1,"when execl calls validate, where is the standard input read from?","if (pid == 0) { + //child process; read from pipe and write to stdin of validate + close(pipefd[1]); + + if (dup2(pipefd[0], STDIN_FILENO) == -1) { + perror(""Dup2 failed""); + exit(1); + } + close(pipefd[0]); + execl(""./validate"", ""validate"", NULL); + }",, +63c647db5f004bf4898cf748,2023-03-08T21:52:00.105Z,General Question,v1,"what does execl return? +",,, +63c647dc5f004bf4898cf752,2023-01-23T20:08:13.088Z,Help Fix Code,v1,,"int main() { + char **last_names; + last_names = malloc(sizeof(char*)); + last_names[0] = malloc(sizeof(char) * 20); + last_names[1] = malloc(sizeof(char) * 20); + last_names[2] = malloc(sizeof(char) * 20); + last_names[3] = malloc(sizeof(char) * 20); + return 0; +}","Assign a dynamically allocated char * array of length 4 to last_names. +Then, allocate a character array of length 20 for each element of the array pointed to by last_names. +", +63c647dc5f004bf4898cf752,2023-01-23T20:10:21.189Z,Help Fix Code,v1,," +}int main(int argc, char **argv) { + float *rainfall; + float rain_today; + // rainfall has been dynamically allocated space for a floating point number. + // Both rainfall and rain_today have been initialized in hidden code. + // Assign the amount in rain_today to the space rainfall points to. + return 0; +}","rainfall has been dynamically allocated space for a floating point number. +Both rainfall and rain_today have been initialized in hidden code. +Assign the amount in rain_today to the space rainfall points to. +", +63c647dc5f004bf4898cf752,2023-01-23T20:11:29.364Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + float *rainfall; + float rain_today; + // rainfall has been dynamically allocated space for a floating point number. + // Both rainfall and rain_today have been initialized in hidden code. + // Assign the amount in rain_today to the space rainfall points to. +// Submitted code below + rain_today = rainfall; +// Submitted code above + return 0; +}","rainfall has been dynamically allocated space for a floating point number. +Both rainfall and rain_today have been initialized in hidden code. +Assign the amount in rain_today to the space rainfall points to.", +63c647dc5f004bf4898cf752,2023-01-23T20:19:08.657Z,Help Fix Code,v1,,"int get_sum(const char *input_line) { + +} + ","Returns the sum of two integers in the string input_line. + + input_line is a string containing two integers separated by one + or more spaces. + + Assume that the string will not be more than 20 characters + long and that it contains two integers separated only by whitespace. + There may also be spaces before the first integer + and/or after the last, but there won't be any other characters + in the string. + + For example, get_sum("" 123 5 "") should return the integer 128, + and get_sum(""-14 30"") should return the integer 16. + + Hint: use strtol(). + + ", +63c647dc5f004bf4898cf752,2023-01-25T01:13:20.476Z,General Question,v1,is the sizeof(char *) and sizeof(&char) the same?,,, +63c647dc5f004bf4898cf752,2023-01-25T17:29:19.026Z,Help Fix Code,v1,,"int main(){ + char greetings[15] = ""salutations""; + char *farewell[] = ""goodbye""; +} +","Define a stack-allocated string called greetings that has space for 15 characters (including the null terminator) and set its initial value to ""salutations"". Then define a string literal with the name farewell that has the value ""goodbye"".", +63c647dc5f004bf4898cf752,2023-01-25T17:30:24.660Z,Help Fix Code,v1,,"int main(){ + char greetings[15] = ""salutations""; + char *farewell = ""goodbye""; +} +","Define a stack-allocated string called greetings that has space for 15 characters (including the null terminator) and set its initial value to ""salutations"". Then define a string literal with the name farewell that has the value ""goodbye"".", +63c647dc5f004bf4898cf752,2023-01-25T17:36:45.462Z,Help Fix Code,v1,,"int main(){ + char greetings[15] = ""salutations""; + char *farewell = ""goodbye""; +} +","Define a stack-allocated string called greetings that has space for 15 characters (including the null terminator) and set its initial value to ""salutations"". Then define a string literal with the name farewell that has the value ""goodbye"". +", +63c647dc5f004bf4898cf752,2023-01-25T17:40:14.193Z,Help Fix Code,v1,,"#include + +int main() { + char greetings[15] = ""salutations""; + char *farewell = ""goodbye""; +} +","Define a stack-allocated string called greetings that has space for 15 characters (including the null terminator) and set its initial value to ""salutations"". Then define a string literal with the name farewell that has the value ""goodbye"".", +63c647dc5f004bf4898cf752,2023-01-25T17:50:49.710Z,Help Fix Code,v1,,"#include +#include +#include + +int main() { + char *sweet = malloc(20 * sizeof(char)); + strcpy(*sweet, ""tiramisu""); + return 0; +}","The string sweet has been declared on the heap and has space for 20 characters. +Set the value of sweet to ""tiramisu"" without changing its address.", +63c647dc5f004bf4898cf752,2023-01-25T18:10:10.539Z,Help Fix Code,v1,,"#include +#include +#include + +struct contact { + char name[30]; + int phone_number; + char address[30]; +}; + +void update_contact(struct contact *friend,char *name, int number, char* address){ + strcpy((*friend).name, *name); + (*friend).phone_number = number; + strcpy((*friend).address, *address); +} + + +int main(int argc, char **argv) { + struct contact friend; + // set initial values for friend + strcpy(friend.name, ""Jane Doe""); + friend.phone_number = 377177377; + strcpy(friend.address, ""3771 University Avenue""); + + char *name; + int number; + char *address; + // in hidden code set values for these 3 variables + + // call update_contact to change the values in friend + update_contact(&friend, name, number, address); + return 0; +}","Given the following program, write the function update_contact which takes a pointer to the struct contact, a new name, a new number, and a new address to change the struct’s members. Look at the function call for the order of arguments and appropriate types.", +63c647dc5f004bf4898cf752,2023-01-25T22:50:55.418Z,Help Fix Code,v1,,"struct Node{ + int value; + struct Node *next; +};","Create a node struct with the following struct members: +- value, an integer +- next, a pointer to another node + +Typedef your structure as Node.", +63c647dc5f004bf4898cf752,2023-01-25T22:52:15.784Z,Help Fix Code,v1,,"typedef struct Node{ + int value; + struct Node *next; +};","Create a node struct with the following struct members: +- value, an integer +- next, a pointer to another node + +Typedef your structure as Node. +", +63c647dc5f004bf4898cf752,2023-01-27T00:50:44.124Z,Help Fix Code,v1,,"int **split_array(const int *s, int length) { + int *arrays; + int half_length = ceil(length/2); + int *array1 = malloc(sizeof(int) * half_length); + int *array2 = malloc(sizeof(int) * half_length); + for (int i = 0; i < length; i = i + 2){ + array1[i] = *s[i]; + } + for (int i = 1; i < length; i = i + 2){ + array2[i] = *s[i]; + } + arrays[0] = *array1; + arrays[1] = *array2; + return arrays; +} +","Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division.", +63c647dc5f004bf4898cf752,2023-01-27T01:00:49.551Z,Question from Code,v1,Why is 'arrays' uninitialized?,"int **split_array(const int *s, int length) { + int **arrays; + int half_length; + if (length%2 == 0){ + half_length = length/2 + 1; + } + else { + half_length = length/2; + } + int *array1 = malloc(sizeof(int) * half_length); + int *array2 = malloc(sizeof(int) * half_length); + for (int i = 0; i < length; i = i + 2){ + array1[i] = s[i]; + } + for (int i = 1; i < length; i = i + 2){ + array2[i] = s[i]; + } + arrays[0] = array1; + arrays[1] = array2; + return arrays; +} +",, +63c647dc5f004bf4898cf752,2023-01-27T01:05:22.264Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + int *ptr; + for (int i = 0; i < size; i++){ + ptr[i] = (int) strtol(strs[i], NULL, 10); + } + return ptr; +} +","Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array", +63c647dc5f004bf4898cf752,2023-01-27T01:16:22.562Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + int *ptr = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++){ + ptr[i] = (int) strtol(strs[i], NULL, 10); + } + return ptr; +} +","Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array", +63c647dc5f004bf4898cf752,2023-01-27T01:18:42.644Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + int *ptr = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++){ + ptr[i] = strtol(strs[i], NULL, 10); + } + return ptr; +} +","Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array +", +63c647dc5f004bf4898cf752,2023-01-27T01:20:29.904Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + int *ptr = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++){ + ptr[i] = strtol(strs[i], NULL, 10); + } + return ptr; +} +","Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array", +63c647dc5f004bf4898cf752,2023-01-27T01:27:10.295Z,Help Fix Code,v1,,"int **split_array(const int *s, int length) { + int half_length; + if (length%2 == 0){ + half_length = length/2 + 1; + } + else { + half_length = length/2; + } + int *array1 = malloc(sizeof(int) * half_length); + int *array2 = malloc(sizeof(int) * half_length); + for (int i = 0; i < length; i = i + 2){ + array1[i] = s[i]; + } + for (int i = 1; i < length; i = i + 2){ + array2[i] = s[i]; + } + int **arrays = malloc(sizeof(int *) * 2); + arrays[0] = array1; + arrays[1] = array2; + return arrays; +} + +","Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division.", +63c647dc5f004bf4898cf752,2023-01-27T02:54:47.321Z,Help Fix Code,v1,,"int **split_array(const int *s, int length) { + int half_length; + int **arrays = malloc(sizeof(int *) * 2); + if (length%2 == 0){ + length1 = length/2; + length2 = length/2; + } + else { + length1 = length/2; + length2 = length/2 + 1; + } + int *array1 = malloc(sizeof(int) * length1); + int *array2 = malloc(sizeof(int) * length2); + for (int i = 0; i < length; i = i + 2){ + array1[i] = s[i]; + } + for (int i = 1; i < length; i = i + 2){ + array2[i] = s[i]; + } + arrays[0] = array1; + arrays[1] = array2; + return arrays; +}","Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division.", +63c647dc5f004bf4898cf752,2023-01-27T03:02:40.718Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + int *ptr = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++){ + ptr[i] = strtol(strs[i + 1], NULL, 10); + } + return ptr; +}","Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array", +63c647dc5f004bf4898cf752,2023-01-27T03:34:33.833Z,Help Fix Code,v1,,"int *fib(int **fib_sequence, int n){ + int *fib = malloc(sizeof(int) * n); + *fib_sequence = fib; + int num1 = 0; + int num2 = 1; + for (int i = 0; i < n; i++){ + fib[i] = num1 + num2; + num2 = fib[i]; + num1 = num2; + } + return 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.", +63c647dc5f004bf4898cf752,2023-01-27T18:24:04.004Z,Help Fix Code,v1,,"int *fib(int **fib_sequence, int n){ + int *fib = malloc(sizeof(int) * n); + *fib_sequence = fib; + if (n == 0){ + fib[0] = 0; + } + else if (n == 1){ + fib[0] = 0; + fib[1] = 1; + } + else { + for (int i = 2; i < n; i++){ + fib[i] = fib[i-2] + fib[i-1]; + } + } + return 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. +", +63c647dc5f004bf4898cf752,2023-01-27T18:42:31.692Z,Help Fix Code,v1,,"int check_permissions(char *file_permission, char *required_permission){ + for (int i; i < 10; i++){ + if ((file_permission[i] != required_permission[i]) && (required_permission[i] != '-')){ + return 1; + } + } + return 0; +} +","You are required to write (and use) a helper function named check_permissions that takes two 9-element character arrays as arguments and returns an integer. (The prototype for this function is in the starter code.) The first array will represent the permission field of a file and the second will represent the permissions that are required. The function will return 0 if the file has all the required permissions and 1 otherwise. The arrays do not have to be identical for a 0 to be returned. For example, if the first array holds the characters rwxr-x--- and the second array holds the characters r-x------, the function should return 0. Often when we pass arrays as parameters to functions, we also pass a size so that the function can work on arrays of varying lengths. In this case we don't need to pass a size because both arrays will always have nine elements.", +63c647dc5f004bf4898cf752,2023-01-29T00:39:34.996Z,General Question,v1,How do you get multiple command line arguments and save them as variables?,,, +63c647dc5f004bf4898cf752,2023-01-29T00:50:41.545Z,General Question,v1,"For example, given the output of ls -l: + + total 329 + -rwx------ 1 reid staff 1734 Jun 22 14:52 prog + -rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt + -rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz + -rw-r--r-- 1 reid staff 88 Feb 15 2013 xyz.c + +how could I store each line in a separate string using scanf?",,, +63c647dc5f004bf4898cf752,2023-01-29T20:18:50.827Z,Help Fix Code,v1,,"while (scanf(""%10s %*d %*s %*s %d %*s %*d %*s %*s"", permission, &file_size)!= EOF){ +// printf(""%d"", file_size); + + int count = 0; + if (file_size > size){ + if (argc == 2){ + count = count + 1; + } + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + else if (argc == 3){ + int a = check_permissions(permission, required); + printf(""%d\n"", a); + if (a==1){ + count = count + 1; + } + } + } + } + printf(""%s"", permission); + printf(""%d"", count);",print permission and count, +63c647dc5f004bf4898cf752,2023-01-29T20:22:32.396Z,Help Fix Code,v1,,"int main(int argc, char** argv) { + 17 if (!(argc == 2 || argc == 3)) { + 18 fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + 19 return 1; + 20 } + 21 + 22 int size; + 23 char required[11]; + 24 int *count; + 25 + 26 // TODO: Process command line arguments. + 27 if (argc == 2){ + 28 size = *argv[1]; + 29 } else { + 30 size = *argv[1]; + 31 required[11] = *argv[2]; + 32 } + 33 + 34 // Process arguments passed from pipe + 35 char permission[11]; + 36 int file_size; + 37 + 38 while (scanf(""%10s %*d %*s %*s %d %*s %*d %*s %*s"", permission, &file_size)!= EOF){ + 39 // printf(""%d"", file_size); + 40 + 41 + 42 if (file_size > size){ + 43 if (argc == 2){ + 44 *count = *count + 1; + 45 } + 46 // TODO: Call check_permissions as part of your solution to count the files to + 47 // compute and print the correct value. + 48 else if (argc == 3){ + 49 int a = check_permissions(permission, required); + 50 printf(""%d\n"", a); + 51 if (a==1){ + 52 *count = *count + 1; + 53 } + 54 } + 55 } + 56 } + 57 printf(""%s"", permission); + 58 printf(""%d"", *count); + 59 return 0; + 60 } +",warning: ‘count’ may be used uninitialized in this function, +63c647dc5f004bf4898cf752,2023-01-29T22:26:39.669Z,Help Fix Code,v1,,"int populate_array(int sin, int *sin_array) { + // check if sin is 9 digits + int count = 0; + int num = sin; + + while (num != 0){ + count++; + num /= 10; + } + + if (count != 9){ + return 1; + } + + // populate sin_array + for (int i = 8; i > 0; i--){ + sin_array[i] = sin % 10; + sin = sin / 10; + } + return 0; +} +","It takes an integer and an integer array as its arguments, and returns an integer. This function's job is to populate the given integer array so that it contains the 9 digits of the given integer, in the same order as in the integer. Hint: use % 10 and / 10 to calculate the digits. The function must return 0 when it completes successfully, and 1 if the given integer is not 9 digits.", +63c647dc5f004bf4898cf752,2023-01-29T22:27:20.236Z,Help Fix Code,v1,,"int populate_array(int sin, int *sin_array) { + // check if sin is 9 digits + int count = 0; + int num = sin; + + while (num != 0){ + count++; + num /= 10; + } + + if (count != 9){ + return 1; + } + + // populate sin_array + for (int i = 8; i >= 0; i--){ + sin_array[i] = sin % 10; + sin = sin / 10; + } + return 0; +} +","It takes an integer and an integer array as its arguments, and returns an integer. This function's job is to populate the given integer array so that it contains the 9 digits of the given integer, in the same order as in the integer. Hint: use % 10 and / 10 to calculate the digits. The function must return 0 when it completes successfully, and 1 if the given integer is not 9 digits.", +63c647dc5f004bf4898cf752,2023-01-29T22:44:57.046Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + // TODO: Verify that command line arguments are valid. + if (argc != 2){ + return 2; + } + // TODO: Parse arguments and then call the two helpers in sin_helpers.c + // to verify the SIN given as a command line argument. + int *populated_array; + populate_array((int) argv[1], populated_array); + int b = check_sin(populated_array); + if (b == 0){ + printf(""%s\n"", ""Valid SIN""); + return 0; + } else{ + printf(""%s\n"", ""Invalid SIN""); + return 1; + } + +} +","If the user calls the program with too few or too many arguments, the program should not print anything, but should return from main with return code 2. main should return 0 when the program runs successfully and the SIN is valid and 1 when the program runs successfully but the SIN is invalid.", +63c647dc5f004bf4898cf752,2023-01-30T02:53:01.831Z,Help Fix Code,v1,,"int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + int size; + char required[11]; + int count = 0; + printf(""argc: %d, argv: %s\n"", argc, argv[2]); + // TODO: Process command line arguments. + if (argc == 2){ + size = *argv[1]; + } else { + size = *argv[1]; + required = argv[2]; + } +}",Set required as the string in argv[2], +63c647dc5f004bf4898cf752,2023-02-02T21:45:32.961Z,Help Fix Code,v1,,"int main(int argc, char ** argv){ + if (argc != 3){ + printf(""Invalid\n""); + } + if (argv[1] == argc[2]){ + printf(""Same\n""); + } else{ + printf(""Different\n""); + } + return 0; +} ","Write the main() function of a program that takes exactly two arguments, + and prints one of the following: + - ""Same\n"" if the arguments are the same. + - ""Different\n"" if the arguments are different. + - ""Invalid\n"" if the program is called with an incorrect number of + arguments. + + NOTE: Cut and paste these strings into your printf statements to + ensure that they will be exactly the strings above. + + Your main function should return 0, regardless of what's printed.", +63c647dc5f004bf4898cf752,2023-02-02T21:58:23.670Z,Help Fix Code,v1,,"int main(int argc, char ** argv){ + if (argc != 3){ + printf(""Invalid\n""); + return 0; + } + if (*argv[1] == *argv[2]){ + printf(""Same\n""); + return 0; + } else{ + printf(""Different\n""); + return 0; + } +} +","/* + Write the main() function of a program that takes exactly two arguments, + and prints one of the following: + - ""Same\n"" if the arguments are the same. + - ""Different\n"" if the arguments are different. + - ""Invalid\n"" if the program is called with an incorrect number of + arguments. + + NOTE: Cut and paste these strings into your printf statements to + ensure that they will be exactly the strings above. + + Your main function should return 0, regardless of what's printed. +*/", +63c647dc5f004bf4898cf752,2023-02-02T21:59:54.178Z,Help Fix Code,v1,,"int main(int argc, char ** argv){ + if (argc != 3){ + printf(""Invalid\n""); + return 0; + } + if strcmp(*argv[1], *argv[2]){ + printf(""Same\n""); + return 0; + } else{ + printf(""Different\n""); + return 0; + } +} +","Write the main() function of a program that takes exactly two arguments, + and prints one of the following: + - ""Same\n"" if the arguments are the same. + - ""Different\n"" if the arguments are different. + - ""Invalid\n"" if the program is called with an incorrect number of + arguments. + + NOTE: Cut and paste these strings into your printf statements to + ensure that they will be exactly the strings above. + + Your main function should return 0, regardless of what's printed.", +63c647dc5f004bf4898cf752,2023-02-02T22:00:33.781Z,Help Fix Code,v1,,"int main(int argc, char ** argv){ + if (argc != 3){ + printf(""Invalid\n""); + return 0; + } + if !strcmp(*argv[1], *argv[2]){ + printf(""Same\n""); + return 0; + } else{ + printf(""Different\n""); + return 0; + } +} +","Write the main() function of a program that takes exactly two arguments, + and prints one of the following: + - ""Same\n"" if the arguments are the same. + - ""Different\n"" if the arguments are different. + - ""Invalid\n"" if the program is called with an incorrect number of + arguments. + + NOTE: Cut and paste these strings into your printf statements to + ensure that they will be exactly the strings above. + + Your main function should return 0, regardless of what's printed.", +63c647dc5f004bf4898cf752,2023-02-02T22:10:35.914Z,Help Fix Code,v1,,"int main(int argc, char ** argv){ + if (argc != 3){ + printf(""Invalid\n""); + return 0; + } + if !strcmp(argv[1], argv[2]){ + printf(""Same\n""); + return 0; + } else{ + printf(""Different\n""); + return 0; + } +} +","Write the main() function of a program that takes exactly two arguments, and prints one of the following: - ""Same\n"" if the arguments are the same. - ""Different\n"" if the arguments are different. - ""Invalid\n"" if the program is called with an incorrect number of arguments. NOTE: Cut and paste these strings into your printf statements to ensure that they will be exactly the strings above. Your main function should return 0, regardless of what's printed.", +63c647dc5f004bf4898cf752,2023-02-02T22:12:00.702Z,Help Fix Code,v1,,"int main(int argc, char ** argv){ + if (argc != 3){ + printf(""Invalid\n""); + return 0; + } + if strcmp(argv[1], argv[2]){ + printf(""Same\n""); + return 0; + } else{ + printf(""Different\n""); + return 0; + } +} +","Write the main() function of a program that takes exactly two arguments, and prints one of the following: - ""Same\n"" if the arguments are the same. - ""Different\n"" if the arguments are different. - ""Invalid\n"" if the program is called with an incorrect number of arguments. NOTE: Cut and paste these strings into your printf statements to ensure that they will be exactly the strings above. Your main function should return 0, regardless of what's printed.", +63c647dc5f004bf4898cf752,2023-02-02T22:20:17.173Z,Help Fix Code,v1,,"int truncate(char s[], int n){ + if(strlen(s) > n){ + strncpy(s, s, n); + int removed = strlen(s) - n; + return removed; + } else { + return 0; + }"," Write a function named truncate() that takes a string s and a + non-negative integer n. If s has more than n characters (not including the + null terminator), the function should truncate s at n characters and + return the number of characters that were removed. If s has n or + fewer characters, s is unchanged and the function returns 0. For example, + if s is the string ""function"" and n is 3, then truncate() changes s to + the string ""fun"" and returns 5.", +63c647dc5f004bf4898cf752,2023-02-02T22:23:59.449Z,Help Fix Code,v1,,"int truncate(char s[], int n){ + if(strlen(s) > n){ + strncpy(s, s, n); + int removed = strlen(s) - n; + return removed; + } else { + return 0; + } +}","Write a function named truncate() that takes a string s and a non-negative integer n. If s has more than n characters (not including the null terminator), the function should truncate s at n characters and return the number of characters that were removed. If s has n or fewer characters, s is unchanged and the function returns 0. For example, if s is the string ""function"" and n is 3, then truncate() changes s to the string ""fun"" and returns 5.", +63c647dc5f004bf4898cf752,2023-02-02T22:28:48.178Z,Help Fix Code,v1,,"int truncate(char s[], int n){ + if(strlen(s) > n){ + char copy[n+1]; + strcpy(copy, s); + strncpy(s, copy, n); + int removed = strlen(s) - n; + return removed; + } else { + return 0; + } +} +"," Write a function named truncate() that takes a string s and a + non-negative integer n. If s has more than n characters (not including the + null terminator), the function should truncate s at n characters and + return the number of characters that were removed. If s has n or + fewer characters, s is unchanged and the function returns 0. For example, + if s is the string ""function"" and n is 3, then truncate() changes s to + the string ""fun"" and returns 5.", +63c647dc5f004bf4898cf752,2023-02-02T22:29:28.244Z,Help Fix Code,v1,,"int truncate(char s[], int n){ + if(strlen(s) > n){ + char copy[n+1]; + strcpy(copy, s); + strncpy(s, copy, n-1); + s[n] = \0; + int removed = strlen(s) - n; + return removed; + } else { + return 0; + } +} +"," Write a function named truncate() that takes a string s and a + non-negative integer n. If s has more than n characters (not including the + null terminator), the function should truncate s at n characters and + return the number of characters that were removed. If s has n or + fewer characters, s is unchanged and the function returns 0. For example, + if s is the string ""function"" and n is 3, then truncate() changes s to + the string ""fun"" and returns 5.", +63c647dc5f004bf4898cf752,2023-02-02T22:54:28.307Z,Help Fix Code,v1,,"int strip_q_marks(char *string_ptr){ + int n = strlen(*string_ptr); + int count = 0; + for (int i = n; i > 0; i--){ + if (string_ptr[i] == ""?""){ + string_prt[i] = '\0'; + count = count + 1; + } else { + return count; + } + } +} +"," Complete this program by writing the function strip_q_marks that takes + a single string and returns an integer. + + The function should modify the string to remove any trailing question marks + and return the number of question marks that were removed. + + Note that you should put the command-line argument in double quotes when you + type it in. This prevents the shell from interpreting characters such as ""?"" + or a space as special characters, and passes them to the program as is.", +63c647dc5f004bf4898cf752,2023-02-02T22:59:13.570Z,Help Fix Code,v1,,"int strip_q_marks(char *string_ptr){ + int n = strlen(string_ptr); + int count = 0; + for (int i = n; i > 0; i--){ + if (string_ptr[i] == '?'){ + string_ptr[i] = '\0'; + count = count + 1; + } else { + break; + } + } + return count; +}","Complete this program by writing the function strip_q_marks that takes + a single string and returns an integer. + + The function should modify the string to remove any trailing question marks + and return the number of question marks that were removed. + + Note that you should put the command-line argument in double quotes when you + type it in. This prevents the shell from interpreting characters such as ""?"" + or a space as special characters, and passes them to the program as is. + +", +63c647dc5f004bf4898cf752,2023-02-02T23:03:17.758Z,Help Fix Code,v1,,"int truncate(char s[], int n){ + if(strlen(s) > n){ + int removed = strlen(s) - n; + char copy[n+1]; + strcpy(copy, s); + strncpy(s, copy, n); + return removed; + } else { + return 0; + } +}"," Write a function named truncate() that takes a string s and a + non-negative integer n. If s has more than n characters (not including the + null terminator), the function should truncate s at n characters and + return the number of characters that were removed. If s has n or + fewer characters, s is unchanged and the function returns 0. For example, + if s is the string ""function"" and n is 3, then truncate() changes s to + the string ""fun"" and returns 5.", +63c647dc5f004bf4898cf752,2023-02-02T23:06:31.536Z,Help Fix Code,v1,,"int truncate(char s[], int n){ + if(strlen(s) > n){ + int removed = strlen(s) - n; + char copy[n+1]; + strcpy(copy, s); + strncpy(s, copy, n); + s[n] = '\0'; + return removed; + } else { + return 0; + } +} +"," Write a function named truncate() that takes a string s and a + non-negative integer n. If s has more than n characters (not including the + null terminator), the function should truncate s at n characters and + return the number of characters that were removed. If s has n or + fewer characters, s is unchanged and the function returns 0. For example, + if s is the string ""function"" and n is 3, then truncate() changes s to + the string ""fun"" and returns 5.", +63c647dc5f004bf4898cf752,2023-02-02T23:07:13.832Z,Help Fix Code,v1,,"int truncate(char s[], int n){ + if(strlen(s) > n){ + int removed = strlen(s) - n; + char copy[n+1]; + strcpy(copy, s); + strncpy(s, copy, n-1); + s[n] = '\0'; + return removed; + } else { + return 0; + } +} +","Write a function named truncate() that takes a string s and a non-negative integer n. If s has more than n characters (not including the null terminator), the function should truncate s at n characters and return the number of characters that were removed. If s has n or fewer characters, s is unchanged and the function returns 0. For example, if s is the string ""function"" and n is 3, then truncate() changes s to the string ""fun"" and returns 5.", +63c647dc5f004bf4898cf752,2023-02-02T23:20:20.949Z,Help Fix Code,v1,,"int truncate(char s[], int n){ + if(strlen(s) > n){ + int removed = strlen(s) - n; + s[n] = '\0'; + return removed; + } else { + return 0; + } +} + +","Write a function named truncate() that takes a string s and a + non-negative integer n. If s has more than n characters (not including the + null terminator), the function should truncate s at n characters and + return the number of characters that were removed. If s has n or + fewer characters, s is unchanged and the function returns 0. For example, + if s is the string ""function"" and n is 3, then truncate() changes s to + the string ""fun"" and returns 5.", +63c647dc5f004bf4898cf752,2023-02-02T23:46:24.057Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 3) { + fprintf(stderr, ""Usage: greeting message name\n""); + return 1; + } + char greeting[20]; + char *name = argv[2]; + + // Your code goes here + strncpy(greeting, argv[1], 20); + int len_greet = strlen(argv[1]); + greeting[len_greet + 1] = '\0'; + strcat(greeting, "" ""); + strcat(greeting, *name); + + printf(""%s\n"", greeting); + return 0; +} +","This program has two arguments: the first is a greeting message, and the + second is a name. + + The message is an impersonal greeting, such as ""Hi"" or ""Good morning"". + name is set to refer to a string holding a friend's name, such as + ""Emmanuel"" or ""Xiao"". + + First copy the first argument to the array greeting. (Make sure it is + properly null-terminated.) + + Write code to personalize the greeting string by appending a space and + then the string pointed to by name. + So, in the first example, greeting should be set to ""Hi Emmanuel"", and + in the second it should be ""Good morning Xiao"". + + If there is not enough space in greeting, the resulting greeting should be + truncated, but still needs to hold a proper string with a null terminator. + For example, ""Good morning"" and ""Emmanuel"" should result in greeting + having the value ""Good morning Emmanu"" and ""Top of the morning to you"" and + ""Patrick"" should result in greeting having the value ""Top of the morning "". + + Do not make changes to the code we have provided other than to add your + code where indicated.", +63c647dc5f004bf4898cf752,2023-02-08T02:28:07.849Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) i{ + if (strlen(name) > 31){ + return 2; + } else { + while (*user_ptr_add->next != NULL){ + if (**user_ptr_add == name){ + return 1; + } + user_ptr_add++; + } + *user_ptr_add->next = malloc(sizeof(User)); + *user_ptr_add->next->name = name; + *user_ptr_add->next->next = NULL; + return 0; +} + +","/* + * 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) + */ + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User;", +63c647dc5f004bf4898cf752,2023-02-08T02:30:49.195Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) i{ + if (strlen(name) > 31){ + return 2; + } else { + while (*user_ptr_add->next != NULL){ + if (strcmp(**user_ptr_add, name)){ + return 1; + } + **user_ptr_add++; + } + *user_ptr_add->next = malloc(sizeof(User)); + strcpy(*user_ptr_add->next->name, name); + *user_ptr_add->next->next = NULL; + return 0; +} + +","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)", +63c647dc5f004bf4898cf752,2023-02-08T02:31:55.929Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) i{ + if (strlen(name) > 31){ + return 2; + } else { + while (*user_ptr_add->next != NULL){ + if (strcmp(**user_ptr_add, name)){ + return 1; + } + **user_ptr_add++; + } + *user_ptr_add->next = malloc(sizeof(User)); + strcpy(*user_ptr_add->next->name, name); + *user_ptr_add->next->next = NULL; + return 0; +} + +","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)", +63c647dc5f004bf4898cf752,2023-02-08T02:33:17.137Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) i{ + if (strlen(name) > 31){ + return 2; + } else { + while (**user_ptr_add->next != NULL){ + if (strcmp(**user_ptr_add, name)){ + return 1; + } + **user_ptr_add++; + } + **user_ptr_add->next = malloc(sizeof(User)); + strcpy(**user_ptr_add->next->name, name); + **user_ptr_add->next->next = NULL; + return 0; +} + +","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)", +63c647dc5f004bf4898cf752,2023-02-08T02:58:06.778Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) i{ + if (strlen(name) > 31){ + return 2; + } else { + while (**user_ptr_add->next != NULL){ + if (strcmp(**user_ptr_add, name)){ + return 1; + } + **user_ptr_add++; + } + *user_ptr_add->next = malloc(sizeof(User)); + strcpy(*user_ptr_add->next->name, name); + *user_ptr_add->next->next = NULL; + return 0; +} + +","/* + * 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) + */", +63c647dc5f004bf4898cf752,2023-02-08T02:59:50.611Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) i{ + if (strlen(name) > 31){ + return 2; + } else { + while (**user_ptr_add->next != NULL){ + if (strcmp(**user_ptr_add, name)){ + return 1; + } + *user_ptr_add++; + } + **user_ptr_add->next = malloc(sizeof(User)); + strcpy(**user_ptr_add->next->name, name); + **user_ptr_add->next->next = NULL; + return 0; +} + +",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), +63c647dc5f004bf4898cf752,2023-02-08T03:08:09.227Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) i{ + if (strlen(name) > 31){ + return 2; + } else { + while (*user_ptr_add->next != NULL){ + if (strcmp(*user_ptr_add, name)){ + return 1; + } + **user_ptr_add = **user_ptr_add + sizeof(User); + } + *user_ptr_add->next = malloc(sizeof(User)); + strcpy(*user_ptr_add->next->name, name); + *user_ptr_add->next->next = NULL; + return 0; +} + + +","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) +", +63c647dc5f004bf4898cf752,2023-02-08T03:17:11.195Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) i{ + if (strlen(name) > 31){ + return 2; + } else { + while ((*user_ptr_add)->next != NULL){ + if (strcmp(*user_ptr_add, name)){ + return 1; + } + **user_ptr_add = **user_ptr_add + sizeof(User); + } + (*user_ptr_add)->next = malloc(sizeof(User)); + strcpy((*user_ptr_add)->next->name, name); + (*user_ptr_add)->next->next = NULL; + return 0; +} + + +","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)", +63c647dc5f004bf4898cf752,2023-02-08T03:18:49.979Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) i{ + if (strlen(name) > 31){ + return 2; + } else { + while ((*user_ptr_add)->next != NULL){ + if (strcmp((*user_ptr_add)->name, name)){ + return 1; + } + **user_ptr_add = (**user_ptr_add)->next + sizeof(User); + } + (*user_ptr_add)->next = malloc(sizeof(User)); + strcpy((*user_ptr_add)->next->name, name); + (*user_ptr_add)->next->next = NULL; + return 0; +} + + +","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)", +63c647dc5f004bf4898cf752,2023-02-08T03:20:15.525Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) i{ + if (strlen(name) > 31){ + return 2; + } else { + while ((*user_ptr_add)->next != NULL){ + if (strcmp((*user_ptr_add)->name, name)){ + return 1; + } + **user_ptr_add = (**user_ptr_add)->next + sizeof(User); + } + (**user_ptr_add)->next = malloc(sizeof(User)); + strcpy((**user_ptr_add)->next->name, name); + (**user_ptr_add)->next->next = NULL; + return 0; +} + + +","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)", +63c647dc5f004bf4898cf752,2023-02-08T03:38:52.214Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + struct User *temp; + *temp = *head; + while (*temp != NULL){ + if (strcmp((*temp)->name, name) == 0){ + return temp; + } + *temp = (*temp)->next; + } + return NULL; +} +","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.", +63c647dc5f004bf4898cf752,2023-02-08T04:01:53.435Z,Question from Code,v1,"The code is giving a segmentation fault. The code should 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) > 31){ + return 2; + } else { + while ((*user_ptr_add)->next != NULL){ + if (strcmp((*user_ptr_add)->name, name)){ + return 1; + } + *user_ptr_add = (*user_ptr_add)->next; + } + (*user_ptr_add)->next = malloc(sizeof(User)); + strcpy((*user_ptr_add)->next->name, name); + (*user_ptr_add)->next->next = NULL; + return 0; + } +} +",, +63c647dc5f004bf4898cf752,2023-02-08T04:20:22.105Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add){ + if (strlen(name) > 31){ + return 2; + } else { + while ((**user_ptr_add)->next != NULL){ + if (strcmp((**user_ptr_add)->name, name)){ + return 1; + } + **user_ptr_add = (**user_ptr_add)->next; + } + (**user_ptr_add)->next = malloc(sizeof(User)); + strcpy((**user_ptr_add)->next->name, name); + (**user_ptr_add)->next->next = NULL; + return 0; + } +} + +","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)", +63c647dc5f004bf4898cf752,2023-02-08T15:24:37.665Z,Question from Code,v1,"this line of code is giving me a segmentation fault +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User;",while ((*user_ptr_add)->next != NULL){},, +63c647dc5f004bf4898cf752,2023-02-08T15:41:05.495Z,Help Fix Code,v1,,"void list_users(const User *curr) { + User *temp; + temp = (User *) curr; + while (temp != NULL){ + printf(""%s\n"", temp->name); + temp = temp->next; + } +} +","Print the usernames of all users in the list starting at curr. + * Names should be printed to standard output, one per line.", +63c647dc5f004bf4898cf752,2023-02-08T15:52:43.230Z,Question from Code,v1,Why is this function is only printing the last two nodes instead of all nodes?,"void list_users(const User *curr) { + printf(""User List\n""); + User *temp; + User *head = (User *) curr; + temp = (User *) curr; + while (temp != NULL){ + printf(""\t%s\n"", temp->name); + temp = temp->next; + } + curr = head; +} +",, +63c647dc5f004bf4898cf752,2023-02-08T16:25:31.987Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + if (strcmp(name1, name2) == 0){ + return 3; + } + + if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){ + return 4; + } + + User *user1 = find_user(name1, head); + User **temp1 = user1->friends; + User *user2 = find_user(name2, head); + User **temp2 = user2->friends; + while (temp1 != NULL){ + if (strcmp((**temp1)->name, (*user2)->name) == 0){ + return 1; + } + temp1 = temp1->next; + } + + if (**temp1 > 10 || **temp2 > 10){ + return 2; + } + // There are no errors and can proceed + while (temp1->next != NULL){ + temp1 = temp1->next; + } + strcpy(temp1->next, user2->name); + User *temp2 = user2->friends; + while (temp2->next != NULL){ + temp2 = temp2->next; + } + strcpy(temp2->next, user1->name); + return 0; +} + + +","Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies.", +63c647dc5f004bf4898cf752,2023-02-08T16:41:51.213Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { +114 if (strcmp(name1, name2) == 0){ +115 return 3; +116 } +117 +118 if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){ +119 return 4; +120 } +121 +122 User *user1 = find_user(name1, head); +123 User **friends1 = user1->friends; +124 User *user2 = find_user(name2, head); +125 User **friends2 = user2->friends; +126 int count1 = 0; +127 int count2 = 0; +128 +129 while ((*friends1)->next != NULL){ +130 if (strcmp((*friends1)->name,(*user2)->name) == 0){ +131 return 1; +132 } +133 *friends1 = (*friends1)->next; +134 count1++; +135 if (count1 > 10){ +136 return 2; +137 } +138 } +139 while ((*friends2)->next != NULL){ +140 *friends2 = (*friends2)->next; +141 count2++; +142 if (count2 > 10){ +143 return 2; +144 } +145 } +146 strcpy((*friends1)->next, (*user2)->name); +147 strcpy((*friends2)->next, (*user1)->name); +148 return 0; +149 } +","Make two users friends with each other. This is symmetric - a pointer to + 98 * each user must be stored in the 'friends' array of the other. + 99 * +100 * New friends must be added in the first empty spot in the 'friends' array. +101 * +102 * Return: +103 * - 0 on success. +104 * - 1 if the two users are already friends. +105 * - 2 if the users are not already friends, but at least one already has +106 * MAX_FRIENDS friends. +107 * - 3 if the same user is passed in twice. +108 * - 4 if at least one user does not exist. +109 * +110 * Do not modify either user if the result is a failure. +111 * NOTE: If multiple errors apply, return the *largest* error code that applies. +", +63c647dc5f004bf4898cf752,2023-02-08T16:59:13.191Z,General Question,v1,How to print the contents of a file?,,, +63c647dc5f004bf4898cf752,2023-02-08T17:16:08.344Z,Help Fix Code,v1,,"int print_user(const User *user) { + + while (user->friends != NULL){ + printf(""\t%s\n"", (user->friends)->name); + user->friends = user->friends->next; + } + +} +",tranverse through user's friends and print the name of each one, +63c647dc5f004bf4898cf752,2023-02-08T18:09:33.770Z,Help Fix Code,v1,,"int print_user(const User *user) { + if (user == NULL){ + return 1; + } + printf(""Name: %s"", user->name); + printf(""------------------------------------------""); + printf(""Friends:\n""); + User *friends = (User *) user->friends; + while (friends != NULL){ + printf(""\t%s\n"", friends->name); + friends = friends->next; + } + printf(""------------------------------------------""); + printf(""Posts:""); + + FILE *profile_pic; + profile_pic = fopen(user->profile_pic, ""r""); + c = fgetc(profile_pic); + if( feof(profile_pic) ) { + break ; + } + printf(""%c"", c); + fclose(profile_pic); + return 0; +} +","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.", +63c647dc5f004bf4898cf752,2023-02-08T18:10:03.357Z,General Question,v1,how to print characters of a file one by one?,,, +63c647dc5f004bf4898cf752,2023-02-08T18:24:17.160Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + Post new_post; + //*contents = new_post; + new_post.author = author; + time_t curtime; + time(&curtime); + *new_post.date = ctime(&curtime); + return -1; +} + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; ","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", +63c647dc5f004bf4898cf752,2023-02-08T18:28:35.244Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + Post new_post; + new_post.contents = contents; + new_post.author = *author; + + time_t curtime; + time(&curtime); + new_post.date = ctime(&curtime); + + new_post->next = target->first_post; + target->first_post = new_post; + return 0; +} + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; ","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", +63c647dc5f004bf4898cf752,2023-02-08T18:34:19.117Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + Post new_post; + *new_post.contents = *contents; + strcpy(new_post.author, (*author)->name); + + time_t curtime; + time(&curtime); + new_post.date = ctime(&curtime); + + new_post->next = target->first_post; + target->first_post = new_post; + return 0; +} + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; ","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", +63c647dc5f004bf4898cf752,2023-02-09T03:27:06.099Z,Question from Code,v1,"why is it giving me an error warning: control reaches end of non-void function [-Wreturn-type] + 38 | } +","void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { + fseek(image, 10, SEEK_SET); + fread(pixel_array_offset, 4, 1, image); + fseek(image, 18, SEEK_SET); + fread(width, 4, 1, image); + fseek(image, 22, SEEK_SET); + fread(height, 4, 1, image); + return; +}",, +63c647dc5f004bf4898cf752,2023-02-09T03:34:22.340Z,Help Fix Code,v1,,struct pixel *pointers = malloc(sizeof(struct pixel) * m);,"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.", +63c647dc5f004bf4898cf752,2023-02-09T03:36:25.415Z,Help Fix Code,v1,,"struct pixel *pointers = malloc(sizeof(struct pixel) * m); + for (int i = 0; i < m; i++){ + pointers[i] = pointer[0] + i * sizeof(struct pixel); + }","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. +", +63c647dc5f004bf4898cf752,2023-02-10T01:47:00.304Z,Help Fix Code,v1,,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + int m = height; + struct pixel *pointers = malloc(sizeof(struct pixel) * m); + for (int i = 0; i < m; i++){ + pointers[i] = pointers[i*3]; + for (int j = 0; j < width; j++){ + fseek(image, pixel_array_offset, SEEK_SET); + fread(pointers[i][j], 3, 1, image); + pixel_array_offset = pixel_array_offset + 3; + } + } + return **pointers; +} + +","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.", +63c647dc5f004bf4898cf752,2023-02-10T02:04:49.002Z,Question from Code,v1,Why is there a memory leak?,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + int m = height; + struct pixel **pointers = malloc(sizeof(struct pixel) * m); + for (int i = 0; i < m; i++){ + pointers[i] = malloc(sizeof(struct pixel) * width); + for (int j = 0; j < width; j++){ + fseek(image, pixel_array_offset, SEEK_SET); + fread(&pointers[i][j], 3, 1, image); + pixel_array_offset = pixel_array_offset + 3; + } + } + return pointers; +} + +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; + read_bitmap_metadata(image, &pixel_array_offset, &width, &height); + + // Print out metadata. + printf(""Pixel array offset: %d\n"", pixel_array_offset); + printf(""Width: %d\n"", width); + printf(""Height: %d\n"", height); + + // Read in the pixel data + struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height); + + // Print out some pixels from each of the image's corners. + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 5; j++) { + print_pixel(pixels[i][j]); + print_pixel(pixels[i][width - 1 - j]); + print_pixel(pixels[height - 1 - i][j]); + print_pixel(pixels[height - 1 - i][width - 1 - j]); + } + } + + // Clean up: you need to do this! + fclose(image); + free(pixels); + return 0; +} +~ + +",, +63c647dc5f004bf4898cf752,2023-02-10T02:39:28.832Z,Help Fix Code,v1,,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + int m = height; + struct pixel **pointers = malloc(sizeof(struct pixel*) * m); + for (int i = 0; i < m; i++){ + pointers[i] = malloc(sizeof(struct pixel*) * width); + for (int j = 0; j < width; j++){ + fseek(image, pixel_array_offset, SEEK_SET); + fread(&pointers[i][j], sizeof(struct pixel), 1, image); + pixel_array_offset = pixel_array_offset + sizeof(struct pixel); + } + } + return pointers; +} + +int main(int argc, char **argv) { + for (int i = 0; i < height; i++){ + for (int j = 0; j < width; j++){ + free(pointers[i]); + } + free(**pointers); + } + return 0; +} +",free the pointers in main, +63c647dc5f004bf4898cf752,2023-02-10T02:42:32.455Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + for (int i = 0; i < height; i++){ + for (int j = 0; j < width; j++){ + free(pixels[i]); + } + free(pixels); + } + return 0; +} +struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + int m = height; + struct pixel **pointers = malloc(sizeof(struct pixel*) * m); + for (int i = 0; i < m; i++){ + pointers[i] = malloc(sizeof(struct pixel*) * width); + for (int j = 0; j < width; j++){ + fseek(image, pixel_array_offset, SEEK_SET); + fread(&pointers[i][j], sizeof(struct pixel), 1, image); + pixel_array_offset = pixel_array_offset + sizeof(struct pixel); + } + } + return pointers; +} + + +",free the malloc memory in main with a for loop, +63c647dc5f004bf4898cf752,2023-02-10T19:03:09.627Z,General Question,v1,"suppose we have the following line at the top of our program + +#define MAXNAME = 32; + +and then the declaration + +char name[MAXNAME]; + +in the program. What will this declaration line become after the program has passed +through the C pre-processor?",,, +63c647dc5f004bf4898cf752,2023-02-10T19:08:22.460Z,General Question,v1,"At our company, an employee has a regular pay rate but then gets 5 dollars +extra per hour whenever they are working as a supervisor. +Consider the following definition of a MACRO to represent this: +#define SUPERVISOR(regular) regular + 5 + +What will be the output of the following program fragment? + +#define SUPERVISOR(regular) regular + 5 +int main() { + int regular_pay = 20; + int hours_worked = 10; + printf(""pay is %d\n"", (hours_worked * SUPERVISOR(regular_pay))); + // rest omitted +} +",,, +63c647dc5f004bf4898cf752,2023-02-10T22:09:29.881Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + if (strcmp(name1, name2) == 0){ + return 3; + } + + if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){ + return 4; + } + + User *user1 = find_user(name1, head); + User **friends1 = (*user1).friends; + User *user2 = find_user(name2, head); + User **friends2 = (*user2).friends; + int count1 = 0; + int count2 = 0; + + while ((*friends1)->next != NULL){ + if (strcmp((*friends1)->name, user2->name) == 0){ + return 1; + } + *friends1 = (*friends1)->next; + count1++; + if (count1 > 10){ + return 2; + } + } + while ((*friends2)->next != NULL){ + *friends2 = (*friends2)->next; + count2++; + if (count2 > 10){ + return 2; + } + } + (*friends1)->next = user2; + (*friends2)->next = user1; + return 0; +} +","Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies.", +63c647dc5f004bf4898cf752,2023-02-10T22:16:49.160Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + if (strcmp(name1, name2) == 0){ + return 3; + } + + if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){ + return 4; + } + + User *user1 = find_user(name1, head); + User *friends1 = (*user1).friends; + User *user2 = find_user(name2, head); + User *friends2 = (*user2).friends; + int count1 = 0; + int count2 = 0; + + while ((*friends1)->next != NULL){ + if (strcmp((*friends1)->name, user2.name) == 0){ + return 1; + } + *friends1 = (*friends1)->next; + count1++; + if (count1 > 10){ + return 2; + } + } + while ((*friends2)->next != NULL){ + *friends2 = (*friends2)->next; + count2++; + if (count2 > 10){ + return 2; + } + } + (*friends1)->next = user2; + (*friends2)->next = user1; + return 0; +} + +","Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies.", +63c647dc5f004bf4898cf752,2023-02-13T15:30:16.611Z,Help Fix Code,v1,,"void list_users(const User *curr) { + printf(""User List\n""); + User *temp; + temp = (User *) curr; + while (temp != NULL){ + printf(""\t%s\n"", temp->name); + temp = temp->next; + } +}","Print the usernames of all users in the list starting at curr. + * Names should be printed to standard output, one per line.", +63c647dc5f004bf4898cf752,2023-02-13T15:43:41.575Z,Question from Code,v1, Why is this initialization for friends1[] and friends2[] invalid?," char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + + +User *user1 = find_user(name1, head); +User friends1[] = (*user1).friends; +User *user2 = find_user(name2, head); +User friends2[] = (*user2).friends;",, +63c647dc5f004bf4898cf752,2023-02-13T16:01:14.600Z,Question from Code,v1,"why am I getting this error: error: ‘friends1’ may be used uninitialized in this function [-Werror=maybe-uninitialized] + 126 | friends1[i] = (*user1).friends[i];"," User *user1 = find_user(name1, head); + User **friends1; + int i = 0; + while ((*user1).friends[i] != NULL){ + friends1[i] = (*user1).friends[i]; + } + User *user2 = find_user(name2, head); + User **friends2; + int j = 0; + while ((*user2).friends[j] != NULL){ + friends2[j] = (*user2).friends[j]; + } +",, +63c647dc5f004bf4898cf752,2023-02-13T16:09:40.239Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + if (strcmp(name1, name2) == 0){ + return 3; + } + + if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){ + return 4; + } + + User *user1 = find_user(name1, head); + User **friends1; + friends1 = malloc(sizeof(User*)*MAX_FRIENDS); + int i = 0; + while ((*user1).friends[i] != NULL){ + friends1[i] = (*user1).friends[i]; + } + User *user2 = find_user(name2, head); + User **friends2; + friends2 = malloc(sizeof(User*)*MAX_FRIENDS); + int j = 0; + while ((*user2).friends[j] != NULL){ + friends2[j] = (*user2).friends[j]; + } + + int count1 = 0; + int count2 = 0; + + int x = 0; + int y = 0; + while (*friends1[x] != NULL){ + if (strcmp((*friends1[x])->name, (*user2).name) == 0){ + return 1; + } + x++; + count1++; + if (count1 > 10){ + return 2; + } + } + while (*friends2[y] != NULL){ + y++; + count2++; + if (count2 > 10){ + return 2; + } + } + *friends1[x] = user2; + *friends2[y] = user1; + return 0; +}"," Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies.", +63c647dc5f004bf4898cf752,2023-02-13T18:05:49.218Z,Help Fix Code,v1,," User *user1 = find_user(name1, head); + User friends1[] = malloc(sizeof(User*)*MAX_FRIENDS); + int i = 0; + while ((*user1).friends[i] != NULL){ + friends1[i] = (*user1).friends[i]; + } + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; ",friends1 should be an array that stores all user1's friends, +63c647dc5f004bf4898cf752,2023-02-13T20:38:36.657Z,Question from Code,v1,why is the initializer to friends1[] invalid?,"User friends1[] = malloc(sizeof(User)*MAX_FRIENDS); + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; +",, +63c647dc5f004bf4898cf752,2023-02-13T20:47:06.822Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int make_friends(const char *name1, const char *name2, User *head) { + if (strcmp(name1, name2) == 0){ + return 3; + } + + if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){ + return 4; + } + + User *user1 = find_user(name1, head); + User friends1[MAX_FRIENDS]; + int i = 0; + while ((*user1).friends[i] != NULL){ + friends1[i] = *(*user1).friends[i]; + } + User *user2 = find_user(name2, head); + User friends2[MAX_FRIENDS]; + int j = 0; + while ((*user2).friends[j] != NULL){ + friends2[j] = *(*user2).friends[j]; + } + + int count1 = 0; + int count2 = 0; + + int x = 0; + int y = 0; + while (friends1[x] != NULL){ + if (friends1[x] == *user2){ + return 1; + } + x++; + count1++; + if (count1 > 10){ + return 2; + } + } + while (friends2[y] != NULL){ + y++; + count2++; + if (count2 > 10){ + return 2; + } + } + friends1[x] = *user2; + friends2[y] = *user1; + return 0; +} +","Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies.", +63c647dc5f004bf4898cf752,2023-02-13T20:52:36.944Z,General Question,v1,how to check if an element in the array is equal to NULL?,,, +63c647dc5f004bf4898cf752,2023-02-13T20:58:33.229Z,Question from Code,v1,why is friends1[x] != NULL giving me an error," User *user1 = find_user(name1, head); + User friends1[MAX_FRIENDS]; + int i = 0; + while ((*user1).friends[i] != NULL){ + friends1[i] = *(*user1).friends[i]; + } + User *user2 = find_user(name2, head); + User friends2[MAX_FRIENDS]; + int j = 0; + while ((*user2).friends[j] != NULL){ + friends2[j] = *(*user2).friends[j]; + } + + int count1 = 0; + int count2 = 0; + + int x = 0; + int y = 0; + while (friends1[x] != NULL){ + if (friends1[x] == *user2){ + return 1; + } + x++; + count1++; + if (count1 > 10){ + return 2; + } + } + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; ",, +63c647dc5f004bf4898cf752,2023-02-13T23:27:58.523Z,Question from Code,v1,"I want user_ptr_add to be unchanged after the function returns, why is it not working?","int create_user(const char *name, User **user_ptr_add){ + User *temp = *user_ptr_add; + if (strlen(name) >= MAX_NAME){ + return 2; + } else { + if (*user_ptr_add == NULL){ + temp = *user_ptr_add; + *user_ptr_add = malloc(sizeof(User)); + strcpy((*user_ptr_add)->name, name); + (*user_ptr_add)->next = NULL; + } else { + while ((*user_ptr_add)->next != NULL){ + if (strcmp((*user_ptr_add)->name, name) == 0){ + (*user_ptr_add) = temp; + return 1; + } + *user_ptr_add = (*user_ptr_add)->next; + } + (*user_ptr_add)->next = malloc(sizeof(User)); + strcpy((*user_ptr_add)->next->name, name); + (*user_ptr_add)->next->next = NULL; + } + (*user_ptr_add) = temp; + return 0; + } +} +",, +63c647dc5f004bf4898cf752,2023-02-13T23:28:51.874Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add){ + User *temp = *user_ptr_add; + if (strlen(name) >= MAX_NAME){ + return 2; + } else { + if (*user_ptr_add == NULL){ + temp = *user_ptr_add; + *user_ptr_add = malloc(sizeof(User)); + strcpy((*user_ptr_add)->name, name); + (*user_ptr_add)->next = NULL; + } else { + while ((*user_ptr_add)->next != NULL){ + if (strcmp((*user_ptr_add)->name, name) == 0){ + (*user_ptr_add) = temp; + return 1; + } + *user_ptr_add = (*user_ptr_add)->next; + } + (*user_ptr_add)->next = malloc(sizeof(User)); + strcpy((*user_ptr_add)->next->name, name); + (*user_ptr_add)->next->next = NULL; + } + (*user_ptr_add) = temp; + return 0; + } +} +",keep **user_ptr_add unchanged after the function returns, +63c647dc5f004bf4898cf752,2023-02-13T23:43:08.178Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add){ + if (strlen(name) >= MAX_NAME){ + return 2; + } else { + User *temp = *user_ptr_add; + if (*user_ptr_add == NULL){ + *user_ptr_add = malloc(sizeof(User)); + temp = *user_ptr_add; + strcpy((*user_ptr_add)->name, name); + (*user_ptr_add)->next = NULL; + } else { + while ((*user_ptr_add)->next != NULL){ + if (strcmp((*user_ptr_add)->name, name) == 0){ + (*user_ptr_add) = temp; + return 1; + } + *user_ptr_add = (*user_ptr_add)->next; + } + (*user_ptr_add)->next = malloc(sizeof(User)); + strcpy((*user_ptr_add)->next->name, name); + (*user_ptr_add)->next->next = NULL; + } + (*user_ptr_add) = temp; + return 0; + } +} +",Ensure that user_ptr_add is not mutated when the function returns, +63c647dc5f004bf4898cf752,2023-02-14T02:00:24.973Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL){ + return 2; + } + for (int i = 0; i < MAX_FRIENDS; i++){ + if (author->friends[i] == target){ + Post new_post; + new_post.contents = contents; + strcpy(new_post.author, author->name); + + time_t curtime; + time(&curtime); + *new_post.date = curtime; + + new_post.next = target->first_post; + target->first_post = new_post; + return 0; + } + } + return 1; +} + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; ","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.", +63c647dc5f004bf4898cf752,2023-02-14T02:22:01.621Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { +218 if (author == NULL || target == NULL){ +219 return 2; +220 } +221 for (int i = 0; i < MAX_FRIENDS; i++){ +222 if (author->friends[i] == target){ +223 Post new_post; +224 strcpy(new_post.contents, contents); +225 strcpy(new_post.author, author->name); +226 +227 time_t curtime; +228 time(&curtime); +229 *new_post.date = curtime; +230 +231 new_post.next = target->first_post; +232 target->first_post = &new_post; +233 return 0; +234 } +235 } +236 return 1; +237 }","Make a new post from 'author' to the 'target' user, +205 * containing the given contents, IF the users are friends. +206 * +207 * Insert the new post at the *front* of the user's list of posts. +208 * +209 * 'contents' is a pointer to heap-allocated memory - you do not need +210 * to allocate more memory to store the contents of the post. +211 * +212 * Return: +213 * - 0 on success +214 * - 1 if users exist but are not friends +215 * - 2 if either User pointer is NULL", +63c647dc5f004bf4898cf752,2023-02-14T02:27:39.893Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { +218 if (author == NULL || target == NULL){ +219 return 2; +220 } +221 for (int i = 0; i < MAX_FRIENDS; i++){ +222 if (author->friends[i] == target){ +223 Post new_post = malloc(sizeof(Post)); +224 strcpy(new_post.contents, contents); +225 strcpy(new_post.author, author->name); +226 +227 time_t curtime; +228 time(&curtime); +229 *new_post.date = curtime; +230 +231 new_post.next = target->first_post; +232 target->first_post = &new_post; +233 return 0; +234 } +235 } +236 return 1; +237 } + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; ","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", +63c647dc5f004bf4898cf752,2023-02-14T04:01:37.249Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { +218 if (author == NULL || target == NULL){ +219 return 2; +220 } +221 for (int i = 0; i < MAX_FRIENDS; i++){ +222 if (author->friends[i] == target){ +223 Post *new_post = malloc(sizeof(Post)); +224 strcpy((*new_post->contents), contents); +225 strcpy((*new_post->author), author->name); +226 +227 time_t curtime; +228 time(&curtime); +229 (*new_post)->date = curtime; +230 +231 new_post->next = target->first_post; +232 target->first_post = &new_post; +233 return 0; +234 } +235 } +236 return 1; +237 } +238 + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; ","Make a new post from 'author' to the 'target' user, +205 * containing the given contents, IF the users are friends. +206 * +207 * Insert the new post at the *front* of the user's list of posts. +208 * +209 * 'contents' is a pointer to heap-allocated memory - you do not need +210 * to allocate more memory to store the contents of the post. +211 * +212 * Return: +213 * - 0 on success +214 * - 1 if users exist but are not friends +215 * - 2 if either User pointer is NULL", +63c647dc5f004bf4898cf752,2023-02-14T04:03:28.388Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL){ + return 2; + } + for (int i = 0; i < MAX_FRIENDS; i++){ + if (author->friends[i] == target){ + Post *new_post = malloc(sizeof(Post)); + strcpy((new_post->contents), contents); + strcpy((new_post->author), author->name); + + time_t curtime; + time(&curtime); + (new_post)->date = curtime; + + new_post->next = target->first_post; + target->first_post = new_post; + return 0; + } + } + return 1; +} + +","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 +", +63c647dc5f004bf4898cf752,2023-02-14T04:19:29.129Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL){ + return 2; + } + for (int i = 0; i < MAX_FRIENDS; i++){ + if (author->friends[i] == target){ + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->contents, contents); + strcpy(new_post->author, author->name); + + time_t curtime; + time(&curtime); + new_post->date = &curtime; + + new_post->next = target->first_post; + target->first_post = new_post; + return 0; + } + } + return 1; +} + +","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 + +Incorrect assignment of new_post->next and target->first_post", +63c647dc5f004bf4898cf752,2023-02-14T04:22:36.565Z,Help Fix Code,v1,,"int print_user(const User *user) { + if (user == NULL){ + return 1; + } + FILE *profile_pic; + if ((profile_pic = fopen(user->profile_pic, ""r"")) != NULL){ + int c; + while ((c = getc(profile_pic)) != EOF){ + printf(""%c"", c); + } + fclose(profile_pic); + } + printf(""\n""); + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + int i = 0; + while(user->friends[i] != NULL){ + printf(""%s\n"", user->friends[i]->name); + i++; + } + printf(""------------------------------------------\n""); + printf(""Posts:\n""); + Post *temp = user->first_post; + while(user->first_post != NULL){ + printf(""From: %s\n"", user->first_post->author); + printf(""Date: %s\n"", user->first_post->date); + printf(""\n""); + printf(""%s\n"", user->first_post->contents); + user->first_post = user->first_post->next; + } + user->first_post = temp; + printf(""------------------------------------------\n""); + return 0; +} +",Error in assigning first_post, +63c647dc5f004bf4898cf752,2023-02-14T16:45:44.397Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { +250 User *temp = *user_ptr_del; +251 if (strcmp((*user_ptr_del)->name, name) == 0){ +252 for (int i = 0; i < MAX_FRIENDS; i++){ +253 for (int j = 0; j < MAX_FRIENDS; j++){ +254 if (strcmp(((*user_ptr_del)->next->friends[i]->f riends[j]->name), name)== 0){ +255 (*user_ptr_del)->next->friends[i]->frien ds[j] = (*user_ptr_del)->next->friends[i]->friends[j+1]; +256 } +257 } +258 *user_ptr_del = temp; +259 return 0; +260 } +261 *user_ptr_del = (*user_ptr_del)->next; +262 } else { +263 while ((*user_ptr_del)->next != NULL){ +264 if (strcmp((*user_ptr_del)->next->name, name) == 0){ +265 for (int i = 0; i < MAX_FRIENDS; i++){ +266 User *friend = (*user_ptr_del)->next->friends[i]; +267 for (int j = 0; j < MAX_FRIENDS; j++){ +268 if (strcmp((friend->friends[j]->name), name)== 0 ){ +269 friend->friends[j] = friend->friends[j+1 ]; +270 } +271 } +272 } +273 (*user_ptr_del)->next = (*user_ptr_del)->next->next; +274 (*user_ptr_del) = temp; +275 return 0; +276 } +277 *user_ptr_del = (*user_ptr_del)->next; +278 } +279 } +"," * From the list pointed to by *user_ptr_del, delete the user +242 * with the given name. +243 * Remove the deleted user from any lists of friends. +244 * +245 * Return: +246 * - 0 on success. +247 * - 1 if a user with this name does not exist.", +63c647dc5f004bf4898cf752,2023-02-14T16:46:36.448Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { +250 User *temp = *user_ptr_del; +251 if (strcmp((*user_ptr_del)->name, name) == 0){ +252 for (int i = 0; i < MAX_FRIENDS; i++){ +253 for (int j = 0; j < MAX_FRIENDS; j++){ +254 if (strcmp(((*user_ptr_del)->next->friends[i]->f riends[j]->name), name)== 0){ +255 (*user_ptr_del)->next->friends[i]->frien ds[j] = (*user_ptr_del)->next->friends[i]->friends[j+1]; +256 } +257 } +258 *user_ptr_del = temp; +259 return 0; +260 } +261 *user_ptr_del = (*user_ptr_del)->next; +262 } else { +263 while ((*user_ptr_del)->next != NULL){ +264 if (strcmp((*user_ptr_del)->next->name, name) == 0){ +265 for (int i = 0; i < MAX_FRIENDS; i++){ +266 User *friend = (*user_ptr_del)->next->friends[i]; +267 for (int j = 0; j < MAX_FRIENDS; j++){ +268 if (strcmp((friend->friends[j]->name), name)== 0 ){ +269 friend->friends[j] = friend->friends[j+1 ]; +270 } +271 } +272 } +273 (*user_ptr_del)->next = (*user_ptr_del)->next->next; +274 (*user_ptr_del) = temp; +275 return 0; +276 } +277 *user_ptr_del = (*user_ptr_del)->next; +278 } +279 } +"," * From the list pointed to by *user_ptr_del, delete the user +242 * with the given name. +243 * Remove the deleted user from any lists of friends. +244 * +245 * Return: +246 * - 0 on success. +247 * - 1 if a user with this name does not exist.", +63c647dc5f004bf4898cf752,2023-02-14T18:24:20.475Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + User *temp = *user_ptr_del; + if (strcmp((*user_ptr_del)->name, name) == 0){ + for (int i = 0; i < MAX_FRIENDS; i++){ + while ((*user_ptr_del)->friends[i] != NULL){ + for (int j = 0; j < MAX_FRIENDS; j++){ + while ((*user_ptr_del)->friends[i]->friends[j] != NULL){ + if (strcmp(((*user_ptr_del)->friends[i]->friends[j]->name), name)== 0){ + (*user_ptr_del)->friends[i]->friends[j] = (*user_ptr_del)->friends[i]->friends[j+1]; + } + } + } + } + (*user_ptr_del) = (*user_ptr_del)->next; + *user_ptr_del = temp; + return 0; + } + *user_ptr_del = (*user_ptr_del)->next; + } else { + while ((*user_ptr_del)->next != NULL){ + if (strcmp((*user_ptr_del)->next->name, name) == 0){ + for (int i = 0; i < MAX_FRIENDS; i++){ + while ((*user_ptr_del)->next->friends[i] != NULL){ + for (int j = 0; j < MAX_FRIENDS; j++){ + while ((*user_ptr_del)->next->friends[i]->friends[j] != NULL){ + if (strcmp((*user_ptr_del)->next->friends[j]->name, name)== 0){ + (*user_ptr_del)->next->friends[j] = (*user_ptr_del)->next->friends[j+1]; + } + }} + }} + (*user_ptr_del)->next = (*user_ptr_del)->next->next; + (*user_ptr_del) = temp; + return 0; + } + *user_ptr_del = (*user_ptr_del)->next; + } + } + return 0; +} +","From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist.", +63c647dc5f004bf4898cf752,2023-02-14T18:47:05.323Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + if (strcmp((*user_ptr_del)->name, name) == 0){ + for (int i = 0; i < MAX_FRIENDS; i++){ + while ((*user_ptr_del)->friends[i] != NULL){ + for (int j = 0; j < MAX_FRIENDS; j++){ + while ((*user_ptr_del)->friends[i]->friends[j] != NULL){ + if (strcmp(((*user_ptr_del)->friends[i]->friends[j]->name), name)== 0){ + (*user_ptr_del)->friends[i]->friends[j] = (*user_ptr_del)->friends[i]->friends[j+1]; + break; + } + } + } + } + (*user_ptr_del) = (*user_ptr_del)->next; + return 0; + } + *user_ptr_del = (*user_ptr_del)->next; + } else { + while ((*user_ptr_del)->next != NULL){ + if (strcmp((*user_ptr_del)->next->name, name) == 0){ + for (int i = 0; i < MAX_FRIENDS; i++){ + while ((*user_ptr_del)->next->friends[i] != NULL){ + for (int j = 0; j < MAX_FRIENDS; j++){ + while ((*user_ptr_del)->next->friends[i]->friends[j] != NULL){ + if (strcmp((*user_ptr_del)->next->friends[j]->name, name)== 0){ + (*user_ptr_del)->next->friends[j] = (*user_ptr_del)->next->friends[j+1]; + } + }} + }} + (*user_ptr_del)->next = (*user_ptr_del)->next->next; + return 0; + } + *user_ptr_del = (*user_ptr_del)->next; + } + } + 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. + +There is an error between lines 3 and 13", +63c647dc5f004bf4898cf752,2023-02-14T20:46:57.762Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + if (strcmp((*user_ptr_del)->name, name) == 0){ + int i = 0; + while (((*user_ptr_del)->friends[i] != NULL) && (i < MAX_FRIENDS)){ + int j = 0; + while (((*user_ptr_del)->friends[i]->friends[j] != NULL) && (j < MAX_FRIENDS)){ + if (strcmp(((*user_ptr_del)->friends[i]->friends[j]->name), name)== 0){ + (*user_ptr_del)->friends[i]->friends[j] = (*user_ptr_del)->friends[i]->friends[j+1]; + } + j++; + } + i++; + } + (*user_ptr_del) = (*user_ptr_del)->next; + return 0; + } +",delete the first user From the list pointed to by *user_ptr_del, +63c647dc5f004bf4898cf752,2023-02-15T01:28:01.408Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL){ + return 2; + } + for (int i = 0; i < MAX_FRIENDS; i++){ + if (author->friends[i] == target){ + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->contents, contents); + strcpy(new_post->author, author->name); + + time_t curtime; + time(&curtime); + new_post->date = &curtime; + + new_post->next = target->first_post; + target->first_post = new_post; + return 0; + } + } + return 1; +} + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; "," 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. + +Why is strcpy(new_post->contents, contents); giving me a segmentation fault?", +63c647dc5f004bf4898cf752,2023-02-15T01:31:32.774Z,Question from Code,v1,why is *(user->first_post) = *(user->first_post)->next; giving me a segmentation fault?,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL){ + return 2; + } + for (int i = 0; i < MAX_FRIENDS; i++){ + if (author->friends[i] == target){ + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->contents, contents); + strcpy(new_post->author, author->name); + + time_t curtime; + time(&curtime); + new_post->date = &curtime; + + new_post->next = target->first_post; + target->first_post = new_post; + return 0; + } + } + return 1; +} + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; ",, +63c647dc5f004bf4898cf752,2023-02-15T01:32:46.181Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL){ + return 2; + } + for (int i = 0; i < MAX_FRIENDS; i++){ + if (author->friends[i] == target){ + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->contents, contents); + strcpy(new_post->author, author->name); + + time_t curtime; + time(&curtime); + new_post->date = &curtime; + + new_post->next = target->first_post; + target->first_post = new_post; + return 0; + } + } + return 1; +} + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; ","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 + +Why is *(user->first_post) = *(user->first_post)->next; giving me a segmentation fault?", +63c647dc5f004bf4898cf752,2023-02-15T01:34:08.038Z,Help Fix Code,v1,,"int print_user(const User *user) { + if (user == NULL){ + return 1; + } + FILE *profile_pic; + if ((profile_pic = fopen(user->profile_pic, ""r"")) != NULL){ + int c; + while ((c = getc(profile_pic)) != EOF){ + printf(""%c"", c); + } + fclose(profile_pic); + } + printf(""\n""); + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + int i = 0; + while(user->friends[i] != NULL){ + printf(""%s\n"", user->friends[i]->name); + i++; + } + printf(""------------------------------------------\n""); + printf(""Posts:\n""); + while(user->first_post != NULL){ + printf(""From: %s\n"", user->first_post->author); + printf(""Date: %s\n"", ctime(user->first_post->date)); + printf(""\n""); + printf(""%s\n"", user->first_post->contents); + user->first_post = user->first_post->next; + } + printf(""------------------------------------------\n""); + return 0; +} + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; ","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. + +Why is *(user->first_post) = *(user->first_post)->next; giving me an segmentation fault?", +63c647dc5f004bf4898cf752,2023-02-17T01:57:09.128Z,Help Fix Code,v1,,"char *copy(char *dest, const char *src, int capacity) { + for (int i = 0; i < capacity - 1; i++){ + dest[i] = src[i]; + } + dest[capacity] = '\0'; + return dest; +}","Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset.", +63c647dc5f004bf4898cf752,2023-02-17T01:58:07.555Z,Help Fix Code,v1,,"char *copy(char *dest, const char *src, int capacity) { + for (int i = 0; i < capacity - 1; i++){ + dest[i] = src[i]; + } + dest[capacity] = ""\0""; + return dest; +}","Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset.", +63c647dc5f004bf4898cf752,2023-02-17T02:15:29.774Z,Explain Code,v1,,"#include + +#define SIZE 4 +#define OVERFLOW 4 + +int main() { + int index = 0; + int i; + int before[SIZE] = {10, 10, 10, 10}; + int a[SIZE] = {0, 0, 0, 0}; + int after[SIZE] = {10, 10, 10, 10}; + + printf(""Address of the variables:\n""); + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &after[%d]\n"", (unsigned long) &after[index], index); + } + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &a[%d]\n"", (unsigned long) &a[index], index); + } + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &before[%d]\n"", (unsigned long) &before[index], index); + } + printf(""%lx -> &i\n"", (unsigned long)&i); + printf(""%lx -> &index\n"", (unsigned long)&index); + printf(""\n""); + + + printf(""Initial values:\n""); + printf(""i = %d\n"", i); + printf(""before = {%d, %d, %d, %d}\n"", before[0], before[1], before[2], before[3]); + printf(""a = {%d, %d, %d, %d}\n"", a[0], a[1], a[2], a[3]); + printf(""after = {%d, %d, %d, %d}\n"", after[0], after[1], after[2], after[3]); + printf(""\n""); + + + for (i = 0; i < OVERFLOW; i++) { + a[i] = i * 10; + printf(""i = %d\n"", i); + printf(""before = {%d, %d, %d, %d}\n"", before[0], before[1], before[2], before[3]); + printf(""a = {%d, %d, %d, %d}\n"", a[0], a[1], a[2], a[3]); + printf(""after = {%d, %d, %d, %d}\n"", after[0], after[1], after[2], after[3]); + } + + return 0; +} +",, +63c647dc5f004bf4898cf752,2023-02-17T13:46:23.712Z,General Question,v1,How to add a null terminator to the last value of a string without using strcpy or strncpy?,,, +63c647dc5f004bf4898cf752,2023-02-17T13:48:07.225Z,General Question,v1,How to add a null terminator to the last value of a string without using any string functions??,,, +63c647dc5f004bf4898cf752,2023-02-17T13:48:59.757Z,Question from Code,v1,Why is this string not properly terminated with a null character?,"char *copy(char *dest, const char *src, int capacity) { + for (int i = 0; i < capacity - 1; i++){ + dest[i] = src[i]; + } + dest[capacity] = '\0'; + return dest; +}",, +63c647dc5f004bf4898cf752,2023-02-17T13:50:16.534Z,Help Fix Code,v1,,"char *copy(char *dest, const char *src, int capacity) { + for (int i = 0; i < capacity - 1; i++){ + dest[i] = src[i]; + } + dest[capacity - 1] = '\0'; + return dest; +} +","Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset.", +63c647dc5f004bf4898cf752,2023-02-17T13:52:10.220Z,Help Fix Code,v1,,"char *copy(char *dest, const char *src, int capacity) { + for (int i = 0; i < capacity - 1; i++){ + if (src[i] == '\0'){ + break; + } else { + dest[i] = src[i]; + + } + return dest; +} +","Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset.", +63c647dc5f004bf4898cf752,2023-02-17T13:52:35.457Z,Help Fix Code,v1,,"char *copy(char *dest, const char *src, int capacity) { + for (int i = 0; i < capacity - 1; i++){ + if (src[i] == '\0'){ + break; + } else { + dest[i] = src[i]; + + } + dest[capacity - 1] = '\0'; + return dest; +} +","Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset.", +63c647dc5f004bf4898cf752,2023-02-25T18:57:17.406Z,Question from Code,v1,Why can't I dereference s twice?,"char *s = malloc(6 * sizeof(char)); +strcpy(s, ""hello""); +// missing code +x = **s;",, +63c647dc5f004bf4898cf752,2023-02-25T18:57:59.876Z,Question from Code,v1,This gives me an error. Why can't I dereference s twice?,"char *s = malloc(6 * sizeof(char)); +strcpy(s, ""hello""); +// missing code +x = **s;",, +63c647dc5f004bf4898cf752,2023-02-25T19:00:51.185Z,Question from Code,v1,What is the missing code?,"char *course = ""csc207""; +// missing code +x = course; +x[5] = ’9’;",, +63c647dc5f004bf4898cf752,2023-02-25T19:29:26.475Z,Explain Code,v1,,"struct event { +char *month; +int day; +}; +/* Set the month and day of an event struct given a string in the format +* ""MonthName DayNumber"" +* +* For example, if arg is ""December 25"", the event’s month should be set to ""December"", +* and its day should be set to 25. +*/ +void set_struct(struct event *e, char *arg) { +char *tmp = strchr(arg, ’ ’); +e->date = strtol(tmp + 1, NULL, 10); +int size = strlen(arg) - strlen(tmp) + 1; +e->month = malloc(sizeof(char) * size); +strncpy(e->month, arg, size - 1); +e->month[size] = ’\0’; +}",, +63c647dc5f004bf4898cf752,2023-02-25T20:01:32.969Z,Explain Code,v1,,"char *intertwine(char *s1, char *s2) { +retval[size_ret] = ’\0’; +return retval; +char *result = malloc(strlen(s1) + strlen(s2) + 1); +int min = strlen(s1); +if (strlen(s2) < min) { +min = strlen(s2); +} +for (int i = 0; i < min; i++) { +result[i*2] = s1[i]; +result[i*2 + 1] = s2[i]; +} +result[min*2] = ’\0’; +strcat(result, &s1[min]); +strcat(result, &s2[min]); +return result; +}",, +63c647dc5f004bf4898cf752,2023-02-25T20:12:05.941Z,Question from Code,v1,"Why do we need the address of s1 in strcat(result, &s1[min]); ?","char *intertwine(char *s1, char *s2) { +retval[size_ret] = ’\0’; +return retval; +char *result = malloc(strlen(s1) + strlen(s2) + 1); +int min = strlen(s1); +if (strlen(s2) < min) { +min = strlen(s2); +} +for (int i = 0; i < min; i++) { +result[i*2] = s1[i]; +result[i*2 + 1] = s2[i]; +} +result[min*2] = ’\0’; +strcat(result, &s1[min]); +strcat(result, &s2[min]); +return result; +}",, +63c647dc5f004bf4898cf752,2023-02-26T15:08:54.126Z,Explain Code,v1,,"char **w = malloc(2 * sizeof(char *)); +w[0] = ""hello""; +*(w + 1) = w[0] + 1; +printf(""%s, %s"", w[0], w[1]);",, +63c647dc5f004bf4898cf752,2023-02-27T14:53:36.159Z,General Question,v1,What is the difference between -> and . in c?,,, +63c647dc5f004bf4898cf752,2023-02-27T15:23:50.290Z,General Question,v1,What is a dangling pointer?,,, +63c647dc5f004bf4898cf752,2023-02-27T18:31:24.145Z,General Question,v1,When should I allocate dynamic memory?,,, +63c647dc5f004bf4898cf752,2023-02-27T18:34:52.928Z,Help Fix Code,v1,,"struct node { +int ID; +char *name; // Points to a dynamically allocated string. +struct node *next; +}; + +void format_name(struct node *head) { +Struct node *temp = head; +while (temp){ + char *last; + char *first; + fscanf(temp->name, ""%s"", &last); + fsacnf(temp->name, ""%s"", &first); + char *name; + strcat(name, first); + strcat(name, ""-""); + strcat(name, last); + strcy(temp->name, name); + temp = temp->next; +} +return 0; +}","Considering that the name of each linked list node has the form ""lastname, firstname"", for each +node starting at the specified head, reorder the two names and convert them into the following form: +""firstname-lastname"". Write your code so that it does not have a memory leak.", +63c647dc5f004bf4898cf752,2023-02-27T19:11:21.682Z,Question from Code,v1,Why does this code give a run-time error for returning stack memory?,"int *mkpoint(int x, int y) { +int pt[2] = {x, y}; +return pt; +} + +x = mkpoint(3, 4);",, +63c647dc5f004bf4898cf752,2023-02-27T21:46:30.455Z,General Question,v1,when does a segmentation fault happen?,,, +63c647dc5f004bf4898cf752,2023-02-27T21:47:38.452Z,Question from Code,v1,"If I removed result[size] = '\0';, what error would occur and why?","char *every_nth(char *s, int n) { +3 +4 int size = strlen(s) / n; +5 +6 char *result = malloc(sizeof(char) * size + 1); +7 +8 int i = 0; +9 for(i = 0; i < size; i++) { +10 result[i] = s[i * n]; +11 } +12 +13 result[size] = ’\0’; +14 +15 return result; +16 +17 }",, +63c647dc5f004bf4898cf752,2023-02-27T21:48:05.219Z,General Question,v1,when would extra output happen?,,, +63c647dc5f004bf4898cf752,2023-02-27T21:49:16.362Z,General Question,v1,Are char * and strings the same in all cases?,,, +63c647dc5f004bf4898cf752,2023-02-28T00:58:54.588Z,General Question,v1,What is the difference between gcc -c and GCC -o?,,, +63c647dc5f004bf4898cf752,2023-02-28T01:02:35.749Z,Question from Code,v1,What is **w?,"char **w = malloc(2 * sizeof(char *)); +w[0] = ""hello""; +*(w + 1) = w[0] + 1; +printf(""%s, %s"", w[0], w[1]);",, +63c647dc5f004bf4898cf752,2023-02-28T01:03:31.512Z,Question from Code,v1,"Why is ""hello"" spread across w[0] to w[4], instead of being pointed to by w[0]?","char **w = malloc(2 * sizeof(char *)); +w[0] = ""hello""; +*(w + 1) = w[0] + 1; +printf(""%s, %s"", w[0], w[1]);",, +63c647dc5f004bf4898cf752,2023-03-02T02:21:01.547Z,Explain Code,v1,,"#include +#include +#include + +int main() { + int ret; + + printf(""A\n""); + + ret = fork(); + + printf(""B\n""); + + if (ret < 0) { + perror(""fork""); + exit(1); + } else if (ret == 0) { + printf(""C\n""); + } else { + printf(""D\n""); + } + + printf(""E\n""); + return 0; +} + +",, +63c647dc5f004bf4898cf752,2023-03-02T02:26:13.375Z,Question from Code,v1,"How many processes are created, including the original parent, when forkloop is called with 2, 3, and 4 as arguments? n arguments?","int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +",, +63c647dc5f004bf4898cf752,2023-03-02T02:28:57.123Z,Question from Code,v1,"If we run forkloop 1, two processes are created, the original parent process and its child. Assuming the process id of the parent is 414 and the process id of the child is 416, we can represent the relationship between these processes using the following ASCII diagram: + +414 -> 416 +Use a similar ASCII diagram to show the processes created and their relationships when you run forkloop 3. In other words, how many processes does the parent create? How many do each of the child processes create?","#include +#include +#include + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +",, +63c647dc5f004bf4898cf752,2023-03-02T02:37:01.256Z,Explain Code,v1,,"#include +#include +#include + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +",, +63c647dc5f004bf4898cf752,2023-03-02T02:40:42.123Z,Help Fix Code,v1,,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +~ ",Only the original parent calls fork and the new children do not create additional processes., +63c647dc5f004bf4898cf752,2023-03-02T02:41:41.224Z,Help Fix Code,v1,,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + if (n = 0){ + break; + } + + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +~ ",Only the original parent calls fork and the new children do not create additional processes., +63c647dc5f004bf4898cf752,2023-03-02T02:45:04.096Z,Help Fix Code,v1,,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + if (n == 0){ + break; + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",Only the original parent calls fork and the new children do not create additional processes., +63c647dc5f004bf4898cf752,2023-03-02T02:46:05.301Z,Help Fix Code,v1,,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + break; + + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",Only the original parent calls fork and the new children do not create additional processes., +63c647dc5f004bf4898cf752,2023-03-02T02:47:27.471Z,Help Fix Code,v1,,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +",each process creates exactly one a new process. Keep the printf call for all processes., +63c647dc5f004bf4898cf752,2023-03-02T02:48:12.770Z,Help Fix Code,v1,,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +",Each fork process creates exactly one new fork process. , +63c647dc5f004bf4898cf752,2023-03-02T02:53:32.723Z,Help Fix Code,v1,,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + if (n > 0){ + break; + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +",The original parent calls fork and new children do not create additional processes, +63c647dc5f004bf4898cf752,2023-03-02T03:10:10.287Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +","Modify the program so that each process creates exactly one a new process. Keep the printf call for all processes. The resulting diagram will look something like the following when childcreates 3 is called: + +414 -> 416 -> 417 -> 420", +63c647dc5f004bf4898cf752,2023-03-02T03:10:54.147Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + break; + + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +",Modify the program so that each process creates exactly one a new process. Keep the printf call for all processes. The resulting diagram will look something like the following when childcreates 3 is called: 414 -> 416 -> 417 -> 420, +63c647dc5f004bf4898cf752,2023-03-02T03:11:42.674Z,Help Fix Code,v1,,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + if (n == 0){ + break; + } + + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +",Modify the program so that each process creates exactly one a new process. Keep the printf call for all processes. The resulting diagram will look something like the following when childcreates 3 is called: 414 -> 416 -> 417 -> 420, +63c647dc5f004bf4898cf752,2023-03-02T03:12:32.903Z,Help Fix Code,v1,,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + if (n == 0){ + break; + } + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + return 0; +} +",Modify the program so that each process creates exactly one a new process. Keep the printf call for all processes. The resulting diagram will look something like the following when childcreates 3 is called: 414 -> 416 -> 417 -> 420, +63c647dc5f004bf4898cf752,2023-03-02T18:46:27.667Z,General Question,v1,What is the syntax for wait in a parent call that waits for the child fork call to complete?,,, +63c647dc5f004bf4898cf752,2023-03-02T18:58:46.152Z,Question from Code,v1,Why are all pid the same?,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + printf(""ppid = %d, pid = %d\n"", getppid(), getpid()); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + if (n == 0){ + break; + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + + } + + return 0; +} +",, +63c647dc5f004bf4898cf752,2023-03-02T19:07:24.872Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + printf(""ppid = %d, pid = %d\n"", getppid(), getpid()); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + if (n > 0){ + break; + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + + } + + return 0; +} +",have the parent wait for all children forks to finish using the wait call., +63c647dc5f004bf4898cf752,2023-03-02T19:08:09.273Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + printf(""ppid = %d, pid = %d\n"", getppid(), getpid()); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + if (n > 0){ + wait(NULL); + break; + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + + } + + return 0; +} +",have the parent wait for all children forks to finish using the wait call., +63c647dc5f004bf4898cf752,2023-03-02T19:08:55.885Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + printf(""ppid = %d, pid = %d\n"", getppid(), getpid()); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + if (n > 0){ + break; + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + + } + wait(NULL); + return 0; +} +",have the parent wait for all children forks to finish using the wait call., +63c647dc5f004bf4898cf752,2023-03-02T19:13:01.995Z,Question from Code,v1,Why does all children fork have the same pid?,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + printf(""ppid = %d, pid = %d\n"", getppid(), getpid()); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + if (n == 0){ + break; + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + + } + wait(NULL); + return 0; +} +",, +63c647dc5f004bf4898cf752,2023-03-02T19:15:42.677Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + printf(""ppid = %d, pid = %d\n"", getppid(), getpid()); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + if (n == 0){ + break; + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + + } + wait(NULL); + return 0; +} +",the command line argument is the number of forks done by this program. Have it such that all the forks have different process ids but are forked from the same parent (have the same ppid)., +63c647dc5f004bf4898cf752,2023-03-02T19:32:05.736Z,Help Fix Code,v1,,"#include +#include +#include +#include + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + printf(""ppid = %d, pid = %d\n"", getppid(), getpid()); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + if (n > 0){ + exit(0); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + + } + + return 0; +} +","In the new file, modify the program so that each process creates exactly one a new process. Keep the printf call for all processes. Add the appropriate wait calls to ensure that each parent does not terminate before its children.", +63c647dc5f004bf4898cf752,2023-03-02T19:34:05.971Z,Help Fix Code,v1,,"#include +#include +#include +#include + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + printf(""ppid = %d, pid = %d\n"", getppid(), getpid()); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + if (n > 0){ + wait(NULL); + exit(0); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + + } + + return 0; +} +","In the new file, modify the program so that each process creates exactly one a new process. Keep the printf call for all processes. Add the appropriate wait calls to ensure that each parent does not terminate before its children.", +63c647dc5f004bf4898cf752,2023-03-08T18:13:32.328Z,General Question,v1,What is the default standard input?,,, +63c647dc5f004bf4898cf752,2023-03-08T18:30:50.901Z,General Question,v1,how do I run a c file validate.c and have it return the return code to stdout?,,, +63c647dc5f004bf4898cf752,2023-03-08T18:37:11.436Z,General Question,v1,"when running a compiled file using the ./ command, how can I have the exit code in stdout?",,, +63c647dc5f004bf4898cf752,2023-03-08T20:34:36.926Z,General Question,v1,How to fork into another file in c?,,, +63c647dc5f004bf4898cf752,2023-03-09T20:12:49.404Z,Help Fix Code,v2,"This program should take in two arguments from stdin, then write them to a pipe. Then, have fork the program and have the child process call the program ./validate with the two arguments from the pipe. Depending on the exit code of ./validate, the program should output SUCCESS, INVALID or NO_USER."," + int fd[2]; + if(pipe(fd) = -1){ + perror(""pipe""); + } + + int status; + int n = fork(); + if(n < 0){ + perror(""fork""); + exit(1); + } else if (n > 0){ + close(fd[0]); + + if (write(fd[1], user_id, MAXLINE) == -1){ + perror(""write to pipe""); + } + if (write(fd[1], password, MAXLINE) == -1){ + perror(""write to pipe""); + } + close(fd[1]); + + if (wait(&status) == -1){ + perror(""wait""); + exit(1); + } + if (WIFEXITED(status)){ + int child_status = WEXITSTATUS(status); + if (child_status == 0){ + printf(SUCCESS); + } else if (child_status == 2){ + printf(INVALID); + } else { + printf(NO_USER); + } + } + } else if (n == 0){ + close(fd[1]); + execl(""./validate"", ""validate"", NULL); + read(fd[0], STDIN_FILENO, MAXLINE); + read(fd[0], STDIN_FILENO, MAXLINE); + execl(""echo $?"", ""echo"", ""$?"", NULL); + close(fd[0]); + exit(0); + } +",, +63c647dc5f004bf4898cf752,2023-03-09T20:22:54.029Z,Help Fix Code,v2,"The program should use the exit status of the validate program to determine which of the three following messages to print: + +""Password verified"" the exit code is 0. +""Invalid password"" if the exit code is 2. +NO_USER otherwise. + +Currently, the problem is that my program returns NO_ +USER even if the exit code should be 0.","#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 +#define MAX_PASSWORD 10 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + //TODO + if((strlen(user_id) > MAX_PASSWORD) || strlen(password) > MAX_PASSWORD){ + printf(NO_USER); + } + int fd[2]; + if(pipe(fd) == -1){ + perror(""pipe""); + } + + int status; + int n = fork(); + if(n < 0){ + perror(""fork""); + exit(1); + } else if (n > 0){ + close(fd[0]); + + if (write(fd[1], user_id, MAXLINE) == -1){ + perror(""write to pipe""); + } + if (write(fd[1], password, MAXLINE) == -1){ + perror(""write to pipe""); + } + close(fd[1]); + + if (wait(&status) == -1){ + perror(""wait""); + exit(1); + } + if (WIFEXITED(status)){ + int child_status = WEXITSTATUS(status); + if (child_status == 0){ + printf(SUCCESS); + } else if (child_status == 2){ + printf(INVALID); + } else { + printf(NO_USER); + } + } + } else if (n == 0){ + close(fd[1]); + execl(""./validate"", ""validate"", NULL); + dup2(fd[0], STDIN_FILENO); + dup2(fd[0], STDIN_FILENO); + execl(""echo $?"", ""echo"", ""$?"", NULL); + close(fd[0]); + exit(0); + } + + + return 0; + + +} + + 1,18 Top + +",, +63c647dc5f004bf4898cf752,2023-03-09T20:30:21.136Z,Question from Code,v2,Why is the code asking me for 4 stdin inputs when there should only be 2 stdin inputs?,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 +#define MAX_PASSWORD 10 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + //TODO + if((strlen(user_id) > MAX_PASSWORD) || strlen(password) > MAX_PASSWORD){ + printf(NO_USER); + } + int fd[2]; + if(pipe(fd) == -1){ + perror(""pipe""); + } + + int status; + int n = fork(); + if(n < 0){ + perror(""fork""); + exit(1); + } else if (n > 0){ + close(fd[0]); + + if (write(fd[1], user_id, MAXLINE) == -1){ + perror(""write to pipe""); + } + if (write(fd[1], password, MAXLINE) == -1){ + perror(""write to pipe""); + } + close(fd[1]); + + if (wait(&status) == -1){ + perror(""wait""); + exit(1); + } + if (WIFEXITED(status)){ + int child_status = WEXITSTATUS(status); + if (child_status == 0){ + printf(SUCCESS); + } else if (child_status == 2){ + printf(INVALID); + } else { + printf(NO_USER); + } + } + } else if (n == 0){ + close(fd[1]); + execl(""./validate"", ""validate"", NULL); + dup2(fd[0], STDIN_FILENO); + dup2(fd[0], STDIN_FILENO); + execl(""echo $?"", ""echo"", ""$?"", NULL); + close(fd[0]); + exit(0); + } + + + return 0; + + +} + 1,1 Top +",, +63c647dc5f004bf4898cf752,2023-03-09T20:49:11.928Z,General Question,v2,How to stop fork from rerunning fgets,,, +63c647dc5f004bf4898cf752,2023-03-09T22:43:59.049Z,Help Fix Code,v2,The code is returning invalid even when the exit code of validate is 0,"int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + //TODO + if((strlen(user_id) > MAX_PASSWORD) || strlen(password) > MAX_PASSWORD){ + printf(NO_USER); + exit(1); + } + int fd[2]; + if(pipe(fd) == -1){ + perror(""pipe""); + } + + int status; + int n = fork(); + if(n < 0){ + perror(""fork""); + exit(1); + } else if (n > 0){ + close(fd[0]); + + if (write(fd[1], user_id, MAXLINE) == -1){ + perror(""write to pipe""); + } + if (write(fd[1], password, MAXLINE) == -1){ + perror(""write to pipe""); + } + close(fd[1]); + + if (wait(&status) == -1){ + perror(""wait""); + exit(1); + } + if (WIFEXITED(status)){ + int child_status = WEXITSTATUS(status); + printf(""%d"", child_status); + if (child_status == 0){ + printf(SUCCESS); + } else if (child_status == 2){ + printf(INVALID); + } else { + printf(NO_USER); + } + } + } else if (n == 0){ + close(fd[1]); +// execl(""./validate"", ""validate"", NULL); + dup2(fd[0], STDIN_FILENO); + dup2(fd[0], STDIN_FILENO); + execl(""./validate"", ""validate"", NULL); + execl(""echo $?"", ""echo"", ""$?"", NULL); + close(fd[0]); + exit(0); + } + + + return 0; + + +} + 14,0-1 21% +",, +63c647dc5f004bf4898cf752,2023-03-10T00:08:29.940Z,Explain Code,v2,,"int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + //TODO + if((strlen(user_id) > MAX_PASSWORD) || strlen(password) > MAX_PASSWORD){ + printf(NO_USER); + exit(1); + } + int fd[2]; + if(pipe(fd) == -1){ + perror(""pipe""); + } + + int status; + int n = fork(); + if(n < 0){ + perror(""fork""); + exit(1); + } else if (n > 0){ + close(fd[0]); + + if (write(fd[1], user_id, MAXLINE) == -1){ + perror(""write to pipe""); + } + if (write(fd[1], password, MAXLINE) == -1){ + perror(""write to pipe""); + } + close(fd[1]); + + if (wait(&status) == -1){ + perror(""wait""); + exit(1); + } + if (WIFEXITED(status)){ + int child_status = WEXITSTATUS(status); + printf(""%d"", child_status); + if (child_status == 0){ + printf(SUCCESS); + } else if (child_status == 2){ + printf(INVALID); + } else { + printf(NO_USER); + } + } + } else if (n == 0){ + close(fd[1]); +// execl(""./validate"", ""validate"", NULL); + dup2(fd[0], STDIN_FILENO); + dup2(fd[0], STDIN_FILENO); + execl(""./validate"", ""validate"", NULL); + execl(""echo $?"", ""echo"", ""$?"", NULL); + close(fd[0]); + exit(0); + } + + + return 0; + + +} + + ",, +63c647dc5f004bf4898cf752,2023-03-10T19:44:19.579Z,General Question,v2,If there are two things in the pipe do you use dup2 once or twice?,,, +63c647dc5f004bf4898cf752,2023-03-10T22:25:17.693Z,Help Fix Code,v2,"Initialize memory for fd[1], however, the code is giving me an error as expected ‘void *’ but argument is of type ‘int’ +","int fd[2]; + if(pipe(fd) == -1){ + perror(""pipe""); + } +memset(fd[1], MAX_PASSWORD * 2, sizeof(char));",, +63c647dc5f004bf4898cf752,2023-03-10T22:29:35.815Z,Help Write Code,v2,,,,How to deference fd and get the first element of it? +63c647dc5f004bf4898cf752,2023-03-10T22:30:24.880Z,General Question,v2,"*memset (void *__s, int __c, size_t __n) __THROW __nonnull ((1)); what does this mean?",,, +63c647dc5f004bf4898cf752,2023-03-10T22:31:36.554Z,Help Write Code,v2,,,,"int fd[2]; + if(pipe(fd) == -1){ + perror(""pipe""); + } + + +Given the above code, use memset to allocate space of 2 *sizeof(char) for the first element in fd." +63c647dc5f004bf4898cf752,2023-03-10T22:32:32.173Z,Help Write Code,v2,,,,"int fd[2]; + if(pipe(fd) == -1){ + perror(""pipe""); + } + + +Given the above code, use memset to allocate space of 2*MAX_PASSWORD *sizeof(char) for the second element in fd. + +" +63c647dc5f004bf4898cf752,2023-03-10T22:36:02.138Z,General Question,v2,What does this error mean? /usr/include/string.h:61:14: note: declared here,,, +63c647dc5f004bf4898cf752,2023-03-13T14:11:21.448Z,Help Write Code,v2,,,,"How to parse through a line like ""main : linked_list.o main.o"" to get all the dependencies and target?" +63c647dc5f004bf4898cf752,2023-03-13T14:31:57.680Z,Help Write Code,v2,,,,"How can I name a variable with the counter I? For example, if I want string1, string2 and string3 when i=1,2,3 respectively?" +63c647dc5f004bf4898cf752,2023-03-13T14:34:50.451Z,General Question,v2,"error: variable-sized object may not be initialized + 30 | Dependency *dep[i] = malloc(sizeof(Dependency)); + + +Why is the compiler giving me this error?",,, +63c647dc5f004bf4898cf752,2023-03-13T14:41:27.484Z,Help Write Code,v2,,,,"How to parse through a line like ""main : linked_list.o main.o"" to get all the dependencies and target (where the number of dependencies is unknown), while mallocing space for each dependency with a data type dependency, where dependency->name be the name of the dependency?" +63c647dc5f004bf4898cf752,2023-03-13T14:43:42.402Z,Help Fix Code,v2,I want a Dependency and a Rule for every i value," int i = 1; + while (token != NULL){ + Dependency *dep[i] = malloc(sizeof(Dependency)); + if (dep[i] == NULL){ + perror(""malloc""); + exit(1); + } + Rule *rule[i] = malloc(sizeof(Rule)); + if (rule[i] == NULL){ + perror(""malloc""); + exit(1); + } + dep[i]->rule_node = rule[i]; + i++; + } +",, +63c647dc5f004bf4898cf752,2023-03-13T15:29:54.634Z,Help Fix Code,v2,"If I have multiple tokens, how can I fix this code such that I can easily access the previous dep and rule spaces?"," token = strtok(line, "" ""); + while (token != NULL){ + Dependency *dep = malloc(sizeof(Dependency)); + if (dep == NULL){ + perror(""malloc""); + exit(1); + } + Rule *rule = malloc(sizeof(Rule)); + if (rule == NULL){ + perror(""malloc""); + exit(1); + } + rule->target = token; + dep->rule = rule; + } +",, +63c647dc5f004bf4898cf752,2023-03-13T15:30:38.529Z,Help Write Code,v2,,,,How to add code to go to the next token? +63c647dc5f004bf4898cf752,2023-03-13T16:18:59.054Z,General Question,v2,How to check if the first character in a string is a tab?,,, +63c647dc5f004bf4898cf752,2023-03-13T16:35:50.906Z,General Question,v2,What are arguements in make files?,,, +63c647dc5f004bf4898cf752,2023-03-13T16:39:03.415Z,Question from Code,v2,"WHat would the output of this code be if +args = ""gcc -Wall -g -std=gnu99 -o main linked_list.o main.o""","char *args_to_string(char **args, char *buffer, int size) { + buffer[0] = '\0'; + int i = 0; + while (args[i] != NULL) { + strncat(buffer, args[i], size - strlen(buffer)); + strncat(buffer, "" "", size - strlen(buffer)); + i++; + } + return buffer; +} +",, +63c647dc5f004bf4898cf752,2023-03-13T16:41:47.665Z,Question from Code,v2,"WHat would the output of this code be if args = ""main : linked_list.o main.o""","char *args_to_string(char **args, char *buffer, int size) { + buffer[0] = '\0'; + int i = 0; + while (args[i] != NULL) { + strncat(buffer, args[i], size - strlen(buffer)); + strncat(buffer, "" "", size - strlen(buffer)); + i++; + } + return buffer; +} +",, +63c647dc5f004bf4898cf752,2023-03-13T16:56:11.999Z,General Question,v2,"How to fix this code + +error: lvalue required as unary ‘&’ operand + 60 | char **argument = &strtok(line, "" "");",,, +63c647dc5f004bf4898cf752,2023-03-13T16:58:20.250Z,Question from Code,v2,"How to fix the error + +error: lvalue required as unary ‘&’ operand + 60 | char **argument = &(strtok(line, "" ""));","fgets(line, MAXLINE, fp); + +if (strstr(line, ""-o"") != NULL) { + strtok(line, "" ""); + Action *action = malloc(sizeof(Action)); + char **argument = &(strtok(line, "" "")); + action->args = argument; +} +",, +63c647dc5f004bf4898cf752,2023-03-13T17:04:13.030Z,General Question,v2,"How to fix this error + +parse.c:24:22: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [-Wint-conversion] + 24 | if (strchr(line, ""\t"") != line){ + | ^~~~ + | | + | char *",,, +63c647dc5f004bf4898cf752,2023-03-13T17:33:02.344Z,Help Fix Code,v2,"Read from the open file fp, and create the linked data structure that creates a Dependency node, Rule node and Action node for each dependency, rule and action. + +The code is currently giving me a no stack, malloc error when I try to run it in gdb, how do I fix that? +","Rule *parse_file(FILE *fp) { + // Implement this function and remove the stubbed return statement below. + Rule *head = malloc(sizeof(Rule)); + if (head == NULL){ + perror(""malloc""); + exit(1); + } + + char line[MAXLINE]; + while(fgets(line, MAXLINE, fp) != NULL){ + + // checking if the first character is a tab to determine whether it is a target line or an action line + if (strchr(line, ""\t"") != line){ + if (is_comment_or_empty(line) == 0){ + head->target = strtok(line, "" : ""); + Rule *prev_rule = head; + Dependency *prev_dep; + + char *token; + token = strtok(line, "" ""); + while (token != NULL){ + Dependency *dep = malloc(sizeof(Dependency)); + if (dep == NULL){ + perror(""malloc""); + exit(1); + } + Rule *rule = malloc(sizeof(Rule)); + if (rule == NULL){ + perror(""malloc""); + exit(1); + } + prev_rule->dependencies = dep; + prev_rule->next_rule = rule; + rule->target = token; + dep->rule = rule; + + // change prev_rule and prev_dep to current dep and rule + prev_rule = rule; + prev_dep = dep; + + token = strtok(line, "" ""); + } + } + } else { // the first character is a tab + if (is_comment_or_empty(line) == 0){ + if((strstr(line, ""-o"") != NULL) || (strstr(line, ""-c"") != NULL)){ + strtok(line, "" ""); + Action *action = malloc(sizeof(Action)); + char *argument = strtok(line, "" ""); + action->args = &argument; + } + } + } + } + + return NULL; +} + + +typedef struct action_node { + char **args; // An array of strings suitable to be passed to execvp + struct action_node *next_act; +} Action; + +typedef struct dep_node { + struct rule_node *rule; + struct dep_node *next_dep; +} Dependency; + +typedef struct rule_node { + char *target; + Dependency *dependencies; + Action *actions; + struct rule_node *next_rule; +} Rule; + +",, +63c647dc5f004bf4898cf752,2023-03-13T17:36:41.158Z,Help Fix Code,v2,This code is giving me a malloc: Cannot allocate memory error,"Rule *parse_file(FILE *fp) { + // Implement this function and remove the stubbed return statement below. + Rule *head = malloc(sizeof(Rule)); + if (head == NULL){ + perror(""malloc""); + exit(1); + } + + char line[MAXLINE]; + while(fgets(line, MAXLINE, fp) != NULL){ + + // checking if the first character is a tab to determine whether it is a target line or an action line + if (strchr(line, ""\t"") != line){ + if (is_comment_or_empty(line) == 0){ + head->target = strtok(line, "" : ""); + Rule *prev_rule = head; + Dependency *prev_dep; + + char *token; + token = strtok(line, "" ""); + while (token != NULL){ + Dependency *dep = malloc(sizeof(Dependency)); + if (dep == NULL){ + perror(""malloc""); + exit(1); + } + Rule *rule = malloc(sizeof(Rule)); + if (rule == NULL){ + perror(""malloc""); + exit(1); + } + prev_rule->dependencies = dep; + prev_rule->next_rule = rule; + rule->target = token; + dep->rule = rule; + + // change prev_rule and prev_dep to current dep and rule + prev_rule = rule; + prev_dep = dep; + + token = strtok(line, "" ""); + } + } + } else { // the first character is a tab + if (is_comment_or_empty(line) == 0){ + if((strstr(line, ""-o"") != NULL) || (strstr(line, ""-c"") != NULL)){ + strtok(line, "" ""); + Action *action = malloc(sizeof(Action)); + char *argument = strtok(line, "" ""); + action->args = &argument; + } + } + } + } + + return head; +} + + +",, +63c647dc5f004bf4898cf752,2023-03-13T17:40:01.691Z,General Question,v2,What is the code to check where the memory error happened with valgrind,,, +63c647dc5f004bf4898cf752,2023-03-13T17:47:01.178Z,General Question,v2,What does memset do and how do I use it?,,, +63c647dc5f004bf4898cf752,2023-03-13T18:13:29.405Z,General Question,v2,"How to fix this warning? + +warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [-Wint-conversion] + 8 | FILE *fp = fopen(argv[1], ""r""); + | ~~~~^~~ +",,, +63c647dc5f004bf4898cf752,2023-03-13T20:27:04.592Z,General Question,v2,"How to set action->args[1] to be arg_name with open and close quotation marks """"?",,, +63c647dc5f004bf4898cf752,2023-03-14T11:55:31.270Z,Question from Code,v2," + + +What does the program print to stderr when it runs without interruption?","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; +} +",, +63c647dc5f004bf4898cf752,2023-03-14T15:36:26.810Z,General Question,v2,"How do I fix this error? + +Invalid read of size 4 +==13752== at 0x4900657: fgets (iofgets.c:47) +==13752== by 0x1094B2: parse_file (parse.c:17) +==13752== by 0x10984B: main (parse.c:197) +==13752== Address 0x0 is not stack'd, malloc'd or (recently) free'd + +This is line 17: while(fgets(line, MAXLINE, fp) != NULL){",,, +63c647dc5f004bf4898cf752,2023-03-14T19:10:53.550Z,Help Fix Code,v2,Why is this chuck of code giving me a segmentation fault and how do I fix it?,"int found = 0; + Rule *dep_rule = head; + while(dep_rule != NULL){ + if(strcmp(dep_rule->target, dep_name)==0){ + dep->rule = dep_rule; + found = 1; + break; + } + dep_rule = dep_rule->next_rule; + } + if(found == 0){ + dep_rule = malloc(sizeof(Rule)); + if(dep_rule == NULL){ + perror(""malloc""); + exit(1); + } + dep_rule->target = malloc(sizeof(char) * strlen(dep_name)); + if(dep_rule->target == NULL){ + perror(""malloc""); + exit(1); + } + strncpy(dep_rule->target, dep_name, strlen(dep_name)); + dep_rule->dependencies = NULL; + dep_rule->actions = NULL; + dep_rule->next_rule = NULL; + }",, +63c647dc5f004bf4898cf752,2023-03-14T19:13:35.988Z,Help Fix Code,v2,"Why is this code giving me a segmentation fault at the following line: + +if(dep->rule->target == NULL) {","int found = 0; +Rule *dep_rule = head; +while(dep_rule != NULL){ + if(strcmp(dep_rule->target, dep_name)==0){ + dep->rule = dep_rule; + found = 1; + break; + } + dep_rule = dep_rule->next_rule; +} +if(found == 0){ + dep_rule = malloc(sizeof(Rule)); + if(dep_rule == NULL){ + perror(""malloc""); + exit(1); + } + dep_rule->target = malloc(sizeof(char) * strlen(dep_name)); + if(dep_rule->target == NULL){ + perror(""malloc""); + exit(1); + } + strncpy(dep_rule->target, dep_name, strlen(dep_name)); + dep_rule->dependencies = NULL; + dep_rule->actions = NULL; + dep_rule->next_rule = NULL; +} +",, +63c647dc5f004bf4898cf752,2023-03-15T00:26:59.944Z,Help Write Code,v2,,,,"Make sure that the last character of a string is not \n, if it is \n, delete \n." +63c647dc5f004bf4898cf752,2023-03-15T00:34:38.397Z,Question from Code,v2,what might be the reasons of my output missing one line after clearn : ,"main : linked_list.o main.o + + gcc - + Wall - g - std = gnu99 - + o main linked_list.o main + .o + + main.o : linked_list.h + + gcc - + Wall - g - std = gnu99 - + c main + .c + + linked_list.o + : linked_list.c linked_list.h + + gcc - Wall - g - std = gnu99 - + c linked_list.c + + clean : rm - + f linked_list.o + + rm - + f main.o +",, +63c647dc5f004bf4898cf752,2023-03-15T13:28:34.149Z,Help Write Code,v2,,,,"Use stat to get the last modified time (mtime) of a file. Read the man page (using man 2 stat) to learn how to call stat. The field of the stat struct that you want is called st_mtim on Linux. + +The struct timespec st_mtim; has two fields, one for seconds (tv_sec) and one for nanoseconds (tv_nsec). You will need both of these values to find out which time is more recent." +63c647dc5f004bf4898cf752,2023-03-15T13:38:18.559Z,Help Fix Code,v2,go through target_rule's nodes of dependencies and save the last modified time present in any dependency,"struct stat st; + st.st_mtim.tv_sec = 0; + struct stat last_mod_time; + last_mod_time.st_mtim.tv_sec = 0; + while (target_rule->dependencies != NULL){ + if(stat(dep->rule->target, &st) == 0){ + if(last_mod_time.st_mtim.tv_sec == 0){ + last_mod_time = st; + } else { + if(difftime(st, last_mod_time) > 0){ + last_mod_time = st; + } + } + } + } +",, +63c647dc5f004bf4898cf752,2023-03-15T15:14:28.859Z,General Question,v2,How to fix st_mtim: No such file or directory error,,, +63c647dc5f004bf4898cf752,2023-03-15T15:25:26.944Z,General Question,v2,What header files to include for st_mtim?,,, +63c647dc5f004bf4898cf752,2023-03-15T15:34:46.406Z,Question from Code,v2,"How to fix warning: ‘st’ may be used uninitialize, same with 'last_mod_time' and 'target_mod_time'","struct stat *st; +53 st->st_mtim.tv_sec = 0; +54 struct stat *last_mod_time; +55 last_mod_time->st_mtim.tv_sec = 0; +56 while (target_rule->dependencies != NULL) { + 57 if (stat(dep->rule->target, st) == 0) { + 58 if (last_mod_time->st_mtim.tv_sec == 0) { + 59 last_mod_time = st; + 60 + } + else { + 61 if (last_mod_time->st_mtim.tv_sec > st->st_mtim.tv_sec) { + 62 last_mod_time = st; + 63 + } + else if (last_mod_time->st_mtim.tv_sec == st->st_mtim.tv_sec) { + 64 if (last_mod_time->st_mtim.tv_nsec > st->st_mtim.tv_nsec) { + 65 last_mod_time = st; + 66 + } + 67 + } + 68 + } + 69 + } + else if (stat(dep->rule->target, st) == -1) { + 70 perror(""st_mtim""); + 71 exit(1); + 72 + } + 73 74 +} +75 struct stat *target_mod_time; +76 if (stat(target, target_mod_time) == 0) { + 77 printf(""successfully got target time""); + 78 +} +else if (stat(target, target_mod_time) == -1) { + 79 perror(""st_mtim""); + 80 exit(1); + 81 +} +",, +63c647dc5f004bf4898cf752,2023-03-15T16:10:34.141Z,Help Fix Code,v2,"I am receiving a st_mtim: No such file or directory +error"," +#include + + +struct stat target_mod_time; + // struct timespec target_time = target_mod_time->st_mtim; + if(stat(target, &target_mod_time) == 0){ + printf(""successfully got target time""); + } else if(stat(target, &target_mod_time) == -1){ + perror(""st_mtim""); + exit(1); + } + struct stat st; + while (target_rule->dependencies != NULL){ + if(stat(dep->rule->target, &st) == 0){ + // comparing the last modified time for the dependency to that of the target + if(target_mod_time.st_mtim.tv_sec > st.st_mtim.tv_sec){ + //execute the rule's actions + execute_actions(target_rule); + } else if (target_mod_time.st_mtim.tv_sec == st.st_mtim.tv_sec){ + if (target_mod_time.st_mtim.tv_nsec > st.st_mtim.tv_nsec){ + //execute the rule's actions + execute_actions(target_rule); + } + } + } else if(stat(dep->rule->target, &st) == -1){ + perror(""st_mtim""); + exit(1); + } + } +",, +63c647dc5f004bf4898cf752,2023-03-15T17:03:44.865Z,Help Fix Code,v2,Trying to check if target exit with stat,"struct stat target_exist; + stat(target, &target_exist); + if(stat(target, &target_exist) != 0){ + perror(""target non-existent""); + exit(1); + }",, +63c647dc5f004bf4898cf752,2023-03-17T15:31:47.293Z,Help Fix Code,v2,"Change this code such that it prints out random numbers from 0 to 99 instead of in that order, use the function random","for (int i = 0; i < 100; i++){ + fprintf(fp, ""%d\n"", i); + }",, +63c647dc5f004bf4898cf752,2023-03-17T15:47:35.406Z,Help Fix Code,v2,It is giving me an error that buffer may not be initialized.,"int *buffer; + 41 for (;;) { + 42 fseek(fp, (rand()%99)*sizeof(int), SEEK_SET); + 43 fread(buffer, sizeof(int), 1, fp); + 44 + 45 }",, +63c647dc5f004bf4898cf752,2023-03-17T15:48:52.568Z,Help Fix Code,v2,How to fix the code giving me an error that buffer may not be initialized.,"24 int main(int argc, char **argv) { + 25 if (argc != 3) { + 26 fprintf(stderr, ""Usage: time_reads s filename\n""); + 27 exit(1); + 28 } + 29 seconds = strtol(argv[1], NULL, 10); + 30 + 31 FILE *fp; + 32 if ((fp = fopen(argv[2], ""r"")) == NULL) { + 33 perror(""fopen""); + 34 exit(1); + 35 } + 36 + 37 /* In an infinite loop, read an int from a random location in the file, + 38 * and print it to stderr. + 39 */ + 40 int *buffer; + 41 for (;;) { + 42 fseek(fp, (rand()%99)*sizeof(int), SEEK_SET); + 43 fread(buffer, sizeof(int), 1, fp); + 44 + 45 } + 46 return 1; // something is wrong if we ever get here! + 47 } +",, +63c647dc5f004bf4898cf752,2023-03-17T16:05:10.417Z,Help Write Code,v2,,,,"Now, use sigactionLinks to an external site. to add a signal handler to your program. Start with something simple that just prints a message to standard out and exits (with termination code 0) when it receives a SIGPROF. " +63c647dc5f004bf4898cf752,2023-03-17T16:25:36.708Z,Help Fix Code,v2,"Fix the code such that the integers printed out are from 0 to 99. Currectly, the integers printed out are 9 digits","int main(int argc, char **argv) { + if (argc != 3) { + fprintf(stderr, ""Usage: time_reads s filename\n""); + exit(1); + } + seconds = strtol(argv[1], NULL, 10); + + FILE *fp; + if ((fp = fopen(argv[2], ""r"")) == NULL) { + perror(""fopen""); + exit(1); + } + + /* In an infinite loop, read an int from a random location in the file, + * and print it to stderr. + */ + int buffer = 0; + for (;;) { + fseek(fp, (rand()%99)*sizeof(int), SEEK_SET); + fread(&buffer, sizeof(int), 1, fp); + fprintf(stderr, ""%d\n"", buffer); + + } + return 1; // something is wrong if we ever get here! +} +",, +63c647dc5f004bf4898cf752,2023-03-23T21:55:42.883Z,Help Fix Code,v2,"THis code only successfully prints out the first line input into the server, and not any lines after that. ","int main() { + setbuf(stdout, NULL); + + struct sockaddr_in *self = init_server_addr(PORT); + int listenfd = set_up_server_socket(self, 5); + + while (1) { + int fd = accept_connection(listenfd); + if (fd < 0) { + continue; + } + + // Receive messages + char buf[BUFSIZE] = {'\0'}; + int inbuf = 0; // How many bytes currently in buffer? + int room = sizeof(buf); // How many bytes remaining in buffer? + char *after = buf; // Pointer to position after the data in buf + + int nbytes; + while ((nbytes = read(fd, after, room)) > 0) { + // Step 1: update inbuf (how many bytes were just added?) + inbuf = nbytes; + + int where; + + + while ((where = find_network_newline(buf, inbuf)) > 0) { + + buf[where-1] = '\0'; + printf(""Next message: %s\n"", buf); + // Note that we could have also used write to avoid having to + // put the '\0' in the buffer. Try using write later! + + // Step 4: update inbuf and remove the full line from the buffer + // There might be stuff after the line, so don't just do inbuf = 0. + inbuf = inbuf-where; + memmove(buf, &buf[where], BUFSIZE-where); + + // You want to move the stuff after the full line to the beginning + // of the buffer. A loop can do it, or you can use memmove. + // memmove(destination, source, number_of_bytes) + } + // Step 5: update after and room, in preparation for the next read. + after = &buf[inbuf]; + room = BUFSIZE - inbuf; + } + close(fd); + printf(""The connection is now closed ...\n""); + } + + free(self); + close(listenfd); + return 0; +} +",, +63c647dc5f004bf4898cf752,2023-03-27T19:45:04.588Z,Explain Code,v2,,"int main(void) { + int sock_fd = socket(AF_INET, SOCK_STREAM, 0); + if (sock_fd < 0) { + perror(""client: socket""); + exit(1); + } + + struct sockaddr_in server; + server.sin_family = AF_INET; + server.sin_port = htons(PORT); + if (inet_pton(AF_INET, ""127.0.0.1"", &server.sin_addr) < 1) { + perror(""client: inet_pton""); + close(sock_fd); + exit(1); + } + + if (connect(sock_fd, (struct sockaddr *)&server, sizeof(server)) == -1) { + perror(""client: connect""); + close(sock_fd); + exit(1); + } + + char buf[2 * BUF_SIZE + 2]; + printf(""Please enter a username: ""); + fflush(stdout); + int num_read = read(STDIN_FILENO, buf, BUF_SIZE); + if (num_read == 0) { + close(sock_fd); + exit(0); + } + buf[num_read] = '\0'; + if (write(sock_fd, buf, num_read) != num_read) { + perror(""client: write""); + close(sock_fd); + exit(1); + } + + while (1) { + num_read = read(STDIN_FILENO, buf, BUF_SIZE); + if (num_read == 0) { + break; + } + } + buf[num_read] = '\0'; + + if (write(sock_fd, buf, num_read) != num_read) { + perror(""client: write""); + close(sock_fd); + exit(1); + } + + num_read = read(sock_fd, buf, sizeof(buf) - 1); + if (num_read == 0) { + break; + } + buf[num_read] = '\0'; + printf(""[Server] %s"", buf); +} + +close(sock_fd); +return 0; +} +",, +63c647dc5f004bf4898cf752,2023-03-27T20:53:48.454Z,Question from Code,v2,"There are now two different types of input that we could receive: a username, or a message. How could we tell if we have if we have received a username yet?","int read_from(int client_index, struct sockname *users) { + int fd = users[client_index].sock_fd; + char buf[BUF_SIZE + 1]; + + int num_read = read(fd, &buf, BUF_SIZE); + buf[num_read] = '\0'; + if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) { + users[client_index].sock_fd = -1; + return fd; + } + + return 0; +} +",, +63c647dc5f004bf4898cf752,2023-03-27T21:09:31.810Z,Help Fix Code,v2,"your task is to modify read_from to see if we have received a username yet (how can we tell?), and allocate memory for the username field in the sockname struct and copy the name to the field for that client. After the username has been set, all new incoming messages from that connection will be messages to echo.","int read_from(int client_index, struct sockname *users) { + int fd = users[client_index].sock_fd; + char buf[BUF_SIZE + 1]; + + /* In Lab 10, you focused on handling partial reads. For this lab, you do + * not need handle partial reads. Because of that, this server program + * does not check for ""\r\n"" when it reads from the client. + */ + int num_read = read(fd, &buf, BUF_SIZE); + buf[num_read] = '\0'; + if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) { + users[client_index].sock_fd = -1; + return fd; + } + + return 0; +} +",, +63c647dc5f004bf4898cf752,2023-03-27T21:16:29.733Z,Explain Code,v2,,"int read_from(int client_index, struct sockname *users) { + int fd = users[client_index].sock_fd; + char buf[BUF_SIZE + 1]; + + int num_read = read(fd, &buf, BUF_SIZE); + buf[num_read] = '\0'; + if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) { + users[client_index].sock_fd = -1; + return fd; + } + + return 0; +} +",, +63c647dc5f004bf4898cf752,2023-03-27T21:44:40.247Z,Help Write Code,v2,,,,"Write read_from to see if we have received a username yet (how can we tell?), and allocate memory for the username field in the sockname struct and copy the name to the field for that client. After the username has been set, all new incoming messages from that connection will be messages to echo. +" +63c647dc5f004bf4898cf752,2023-03-27T21:56:33.609Z,Help Write Code,v2,,,,I am writing code in the server. How do I broadcast to all clients that are connected to this server? +63c647dc5f004bf4898cf752,2023-03-27T22:10:54.622Z,Help Fix Code,v2,How to add to this code such that it will print buf to all of the clients connected to this server?,"int read_from(int client_index, struct sockname *users) { + int fd = users[client_index].sock_fd; + char buf[BUF_SIZE + 1]; + + /* In Lab 10, you focused on handling partial reads. For this lab, you do + * not need handle partial reads. Because of that, this server program + * does not check for ""\r\n"" when it reads from the client. + */ + int num_read = read(fd, &buf, BUF_SIZE); + buf[num_read] = '\0'; + if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) { + users[client_index].sock_fd = -1; + return fd; + } + + return 0; +} +",, +63c647dc5f004bf4898cf752,2023-03-27T22:12:33.706Z,Question from Code,v2,How to loop through the users and check if each one is connected?,"int read_from(int client_index, struct sockname *users) { + int fd = users[client_index].sock_fd; + char buf[BUF_SIZE + 1]; + + int num_read = read(fd, &buf, BUF_SIZE); + buf[num_read] = '\0'; + if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) { + users[client_index].sock_fd = -1; + return fd; + } + + return 0; +} +",, +63c647dc5f004bf4898cf752,2023-03-27T22:14:21.568Z,Help Fix Code,v2,broadcasts any message received to all connected clients,"int read_from(int client_index, struct sockname *users) { + int fd = users[client_index].sock_fd; + char buf[BUF_SIZE + 1]; + int first_read = 1; + /* In Lab 10, you focused on handling partial reads. For this lab, you do + * not need handle partial reads. Because of that, this server program + * does not check for ""\r\n"" when it reads from the client. + */ + int num_read = read(fd, &buf, BUF_SIZE); + buf[num_read] = '\0'; + if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) { + users[client_index].sock_fd = -1; + return fd; + } + if (first_read == 1){ // it is a username + users[client_index].username = malloc(sizeof(char) * num_read); + strcpy(users[client_index].username, buf); + first_read = 0; + // add to the table of users? + } else { // it is not a username + for(int i = 0; i < client_index + 1; i++){ + if(users[i].sock_fd > 0){ + printf(""%s\n"", buf); + } + } + } + + return 0; +} +",, +63c647dc5f004bf4898cf752,2023-03-28T01:53:28.487Z,Explain Code,v2,,"while (1) { + fd_set listen_fds = all_fds; + if (select(max_fd + 1, &listen_fds, NULL, NULL, NULL) == -1) { + perror(""server: select""); + exit(1); + } + + if (FD_ISSET(sock_fd, &listen_fds)) { + int client_fd = accept_connection(sock_fd, users); + if (client_fd > max_fd) { + max_fd = client_fd; + } + FD_SET(client_fd, &all_fds); + printf(""Accepted connection\n""); + } + + for (int index = 0; index < MAX_CONNECTIONS; index++) { + if (users[index].sock_fd > -1 && FD_ISSET(users[index].sock_fd, &listen_fds)) { + int client_closed = read_from(index, users); + if (client_closed > 0) { + FD_CLR(client_closed, &all_fds); + printf(""Client %d disconnected\n"", client_closed); + } else { + printf(""Echoing message from client %d\n"", users[index].sock_fd); + } + } + } +",, +63c647dc5f004bf4898cf752,2023-03-31T16:40:05.713Z,General Question,v2,"Why does printf(""[Server] %s"", buf); prints successfully but printf(""[Server]""); not print anything?",,, +63c647dc5f004bf4898cf752,2023-03-31T16:40:43.454Z,General Question,v2,"How to fix printf(""[Server]""); such that only [server] is printed?",,, +63c647dc5f004bf4898cf752,2023-04-03T20:38:19.905Z,Help Fix Code,v2,Why does it say ‘temp2’ may be used uninitialized in this function line 227?,"char *temp2; +226 for (int i = 0; i < MAX_FRIENDS && user->friends[i] != NULL; i++) { +227 snprintf(temp2, strlen(user->friends[i]->name + 2), ""%s\r\n"", user->friends[i]->name); +}",, +63c647dc5f004bf4898cf752,2023-04-03T20:48:01.725Z,Question from Code,v2,Why is it printing 0?,"char *user_string = calloc(sizeof(char) * 171, 1); +printf(""%ld\n"", strlen(user_string));",, +63c647dc5f004bf4898cf752,2023-04-03T20:58:50.604Z,Help Fix Code,v2,"Why is this code not copying name, friends and posts into user_string?"," snprintf(user_string, sizeof(user), ""%s%s%s"", name, friends, posts);",, +63c647dc5f004bf4898cf752,2023-04-03T22:05:28.302Z,Explain Code,v2,,"int main(int argc, char *argv[]) { + 145 int batch_mode = (argc == 2); + 146 char input[INPUT_BUFFER_SIZE]; + 147 FILE *input_stream; + 148 149 150 User *user_list = NULL; + 151 152 if (batch_mode) { + 153 input_stream = fopen(argv[1], ""r""); + 154 if (input_stream == NULL) { + 155 perror(""Error opening file""); + 156 exit(1); + 157 + } + 158 + } + else { + 159 160 input_stream = stdin; + 161 + } + 162 163 printf(""Welcome to FriendMe! (Local version)\nPlease type a command:\n> ""); + 164 165 while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) { + 166 167 168 if (batch_mode) { + 169 printf(""%s"", input); + 170 + } + 171 172 char *cmd_argv[INPUT_ARG_MAX_NUM]; + 173 int cmd_argc = tokenize(input, cmd_argv); + 174 175 if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { + 176 break; + 177 + } + 178 179 printf(""> ""); + 180 + } + 181 182 if (batch_mode) { + 183 fclose(input_stream); + 184 + } + 185 186 return 0; + 187 +}",, +63c647dc5f004bf4898cf752,2023-04-03T22:06:55.090Z,Help Write Code,v2,,,,How can I add code to make this a server where other clients can connect to it? +63c647dc5f004bf4898cf752,2023-04-03T22:11:50.131Z,Explain Code,v2,,"int main() { + setbuf(stdout, NULL); + + struct sockaddr_in *self = init_server_addr(PORT); + int listenfd = set_up_server_socket(self, 5); + + while (1) { + int fd = accept_connection(listenfd); + if (fd < 0) { + continue; + } + + char buf[BUFSIZE] = {'\0'}; + int inbuf = 0; + int room = sizeof(buf); + char *after = buf; + int nbytes; + while ((nbytes = read(fd, after, room)) > 0) { + inbuf += nbytes; + + int where; + + while ((where = find_network_newline(buf, inbuf)) > 0) { + buf[where - 2] = '\0'; + printf(""Next message: %s\n"", buf); + + inbuf = inbuf - where; + + for (int i = 0; i < where; i++) { + buf[i] = '\0'; + } + + memmove(buf, buf + where, inbuf); + + for (int i = where; i < BUFSIZE; i++) { + buf[i] = '\0'; + } + } + after = buf + inbuf; + room = BUFSIZE - inbuf; + } + close(fd); + printf(""The connection is now closed ...\n""); + } + + free(self); + close(listenfd); + return 0; +}",, +63c647dc5f004bf4898cf752,2023-04-04T00:55:33.095Z,Help Fix Code,v2,"How do I change (what do I add, where should I add them, and what should I remove) from this code if I want to change it from local host to hosting on a server?","main{ + int batch_mode = (argc == 2); + char input[INPUT_BUFFER_SIZE]; + FILE *input_stream; + + // Create the heads of the empty data structure + User *user_list = NULL; + + if (batch_mode) { + input_stream = fopen(argv[1], ""r""); + if (input_stream == NULL) { + perror(""Error opening file""); + exit(1); + } + } else { + // interactive mode + input_stream = stdin; + } + + printf(""Welcome to FriendMe! (Local version)\nPlease type a command:\n> ""); + + while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) { + // only echo the line in batch mode since in interactive mode the user + // just typed the line + if (batch_mode) { + printf(""%s"", input); + } + + char *cmd_argv[INPUT_ARG_MAX_NUM]; + int cmd_argc = tokenize(input, cmd_argv); + + if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { + break; // can only reach if quit command was entered + } + + printf(""> ""); + } + + if (batch_mode) { + fclose(input_stream); + } + + return 0; + } +}",, +63c647dc5f004bf4898cf752,2023-04-04T01:06:54.317Z,General Question,v2,"How to solve this two errors? friend_server.c:22:25: error: expected expression before ‘[’ token + 22 | int status = setsockopt([sock_fd], SOL_SOCKET, SO_REUSEADDR, + | ^ +friend_server.c:22:26: error: ‘sock_fd’ undeclared here (not in a function) + 22 | int status = setsockopt([sock_fd], SOL_SOCKET, SO_REUSEADDR, +",,, +63c647dc5f004bf4898cf752,2023-04-04T01:17:23.790Z,Question from Code,v2,what is the sock fd in this code?,"int listen_soc = socket(AF_INET, SOCK_STREAM, 0); +if (listen_soc == -1) { + perror(""server: socket""); + exit(1); +} + +struct sockaddr_in server; +server.sin_family = AF_INET; +server.sin_port = htons(54321); +memset(&server.sin_zero, 0, 8); +server.sin_addr.s_addr = INADDR_ANY; + +if (bind(listen_soc, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) == -1) { + perror(""server: bind""); + close(listen_soc); + exit(1); +} + +if (listen(listen_soc, 5) < 0) { + perror(""listen""); + exit(1); +} + +struct sockaddr_in client_addr; +unsigned int client_len = sizeof(struct sockaddr_in); +client_addr.sin_family = AF_INET; + +int client_socket = accept(listen_soc, (struct sockaddr *)&client_addr, &client_len); +if (client_socket == -1) { + perror(""accept""); + return -1; +} + +write(client_socket, ""Welcome to FriendMe!\nPlease type a command:\n> "", 49); + +char line[INPUT_BUFFER_SIZE]; +read(client_socket, line, INPUT_BUFFER_SIZE); +line[INPUT_BUFFER_SIZE - 1] = '\0'; +printf(""reading line""); +",, +63c647dc5f004bf4898cf752,2023-04-04T01:31:54.841Z,Help Fix Code,v2,"How to fix this error error: ‘g’ undeclared (first use in this function) +friend_server.c:160:29: note: in expansion of macro ‘PORT’ + 160 | server.sin_port = htons(PORT); + | ^~~~ +: note: each undeclared identifier is reported only once for each function it appears in +friend_server.c:160:29: note: in expansion of macro ‘PORT’ + 160 | server.sin_port = htons(PORT); + | ^~~~","// Create socket + int listen_soc = socket(AF_INET, SOCK_STREAM, 0); + if (listen_soc == -1) { + perror(""server: socket""); + exit(1); + } + + //initialize server address + struct sockaddr_in server; + server.sin_family = AF_INET; + server.sin_port = htons(PORT); + memset(&server.sin_zero, 0, 8); + server.sin_addr.s_addr = INADDR_ANY; + + // bind socket to an address + if (bind(listen_soc, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) == -1) { + perror(""server: bind""); + close(listen_soc); + exit(1); + } + + + // Set up a queue in the kernel to hold pending connections. + if (listen(listen_soc, 5) < 0) { + // listen failed + perror(""listen""); + exit(1); + } + + struct sockaddr_in client_addr; + unsigned int client_len = sizeof(struct sockaddr_in); + client_addr.sin_family = AF_INET; + + int client_socket = accept(listen_soc, (struct sockaddr *)&client_addr, &client_len); + if (client_socket == -1) { + perror(""accept""); + return -1; + } + + write(client_socket, ""Welcome to FriendMe!\nPlease type a command:\n> "", 49); + + char line[INPUT_BUFFER_SIZE]; + read(client_socket, line, INPUT_BUFFER_SIZE); + line[INPUT_BUFFER_SIZE - 1] = '\0'; + printf(""reading line""); +",, +63c647dc5f004bf4898cf752,2023-04-04T02:07:14.859Z,Explain Code,v2,,"int batch_mode = (argc == 2); +char input[INPUT_BUFFER_SIZE]; +FILE *input_stream; + +User *user_list = NULL; + +if (batch_mode) { + input_stream = fopen(argv[1], ""r""); + if (input_stream == NULL) { + perror(""Error opening file""); + exit(1); + } +} else { + input_stream = stdin; +} + +while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) { + if (batch_mode) { + printf(""%s"", input); + } + + char *cmd_argv[INPUT_ARG_MAX_NUM]; + int cmd_argc = tokenize(input, cmd_argv); + + if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { + break; + } + + printf(""> ""); +} + +if (batch_mode) { + fclose(input_stream); +}",, +63c647dc5f004bf4898cf752,2023-04-04T19:11:10.709Z,Help Write Code,v2,,,,"every time a client connects to a server, increment the client_count variable and change curr (which is a specific node in a linked list) to the new client." +63c647dc5f004bf4898cf752,2023-04-04T19:29:35.502Z,Help Fix Code,v2,"Edit this code to read from the client's standard input. check if the client's input is in a linked list called user. If it is, return ""already in list"", if not, create a node that is the name.","char buf[INPUT_BUFFER_SIZE] = {'\0'}; + int inbuf = 0; // How many bytes currently in buffer? + int room = sizeof(buf); // How many bytes remaining in buffer? + char *after = buf; // Pointer to position after the data in buf + + int nbytes; + while ((nbytes = read(fd, after, room)) > 0) { + inbuf += nbytes; + int where; + while ((where = find_network_newline(buf, inbuf)) > 0){ + buf[where-2] = '\0'; + curr->name = calloc(sizeof(char) * (MAXNAME + 2), 1); + strcpy(curr->name, buf); + strcat(curr->name, '\r\n'); + inbuf = inbuf-where; + for (int i = 0; i < where; i++){ + buf[i] = '\0'; + } + memmove(buf, buf+where, inbuf); + for (int i = where; i < INPUT_BUFFER_SIZE; i++){ + buf[i] = '\0'; + } + } + after = buf+inbuf; + room = BUFSIZE - inbuf; + } + close(fd); + printf(""The connection is now closed ...\n"");",, +63c647dc5f004bf4898cf752,2023-04-04T20:07:42.402Z,General Question,v2,"How to fix this error: assignment to expression with array type + 212 | curr->name = calloc(sizeof(char) * (MAX_NAME + 2), 1);",,, +63c647dc5f004bf4898cf752,2023-04-04T21:53:41.191Z,Explain Code,v2,,"while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) { + if (batch_mode) { + printf(""%s"", input); + } + + char *cmd_argv[INPUT_ARG_MAX_NUM]; + int cmd_argc = tokenize(input, cmd_argv); + + if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { + break; + }",, +63c647dc5f004bf4898cf752,2023-04-04T22:31:07.925Z,Help Fix Code,v2,How to change the code such that it prints out whether it is an old socket or new socket accordingly?,"if (FD_ISSET(sock_fd, &listen_fds)) { + int client_fd = accept_connection(sock_fd, users); + if (client_fd > max_fd) { + max_fd = client_fd; + } + FD_SET(client_fd, &all_fds); + write(client_socket, ""What is your user name?\n"", 24); + } + +",, +63c647dc5f004bf4898cf752,2023-04-05T00:49:30.347Z,General Question,v2,What is the difference between accept and accept connection in c?,,, +63c647dc5f004bf4898cf752,2023-04-05T01:04:56.715Z,General Question,v2,"How to fix this error? 'User’ declared inside parameter list will not be visible outside of this definition or declaration [-Werror] + 212 | void cleanup_args_for_processing(struct User *user_list, int client_fd)",,, +63c647dc5f004bf4898cf752,2023-04-05T01:09:15.238Z,General Question,v2,"What is the problem with this? + expected expression before ‘User’ + 408 | check_and_add_user(User * user_list, int client_fd); + | ^~~~ +error: too few arguments to function ‘check_and_add_user’ + 408 | check_and_add_user(User * user_list, int client_fd); + | ^~~~~~~~~~~~~~~~~~ + note: declared here + 256 | void check_and_add_user(User *user_list, int client_fd)",,, +63c647dc5f004bf4898cf752,2023-04-05T01:25:40.218Z,General Question,v2,"Why is it asking for more arguments? +function call: check_and_add_user(User *user_list, int client_fd); + +function header: 256 void check_and_add_user(User *user_list, int client_fd) +",,, +63c647dc5f004bf4898cf752,2023-04-05T02:10:13.699Z,Help Fix Code,v2,"How to change this code such that instead of using sockname with is an array, it uses a linked list structure and loops through the list?","int accept_connection(int fd, struct sockname *users) +{ + int user_index = 0; + while (users[user_index].sock_fd != -1) + { + user_index++; + } + + int client_fd = accept(fd, NULL, NULL); + if (client_fd < 0) + { + perror(""server: accept""); + close(fd); + exit(1); + } + + users[user_index].sock_fd = client_fd; + users[user_index].username = NULL; + return client_fd; +}",, +63c647dc5f004bf4898cf752,2023-04-05T14:30:15.569Z,General Question,v2,"How to fix this error invalid type argument of ‘->’ (have ‘fd_set’ {aka ‘struct ’}) + 471 | if (curr_client->fd > -1 && FD_ISSET(curr_client->fd, listen_fds)) +",,, +63c647dc5f004bf4898cf752,2023-04-05T14:46:58.120Z,Help Fix Code,v2,"Read the first line from the user input, that is the username. Check if the username is already in user_list, if not, add it as the last node in user_list.","void check_and_add_user(User *user_list, int client_fd) +{ + char buf[INPUT_BUFFER_SIZE] = {'\0'}; + int inbuf = 0; // How many bytes currently in buffer? + int room = sizeof(buf); // How many bytes remaining in buffer? + char *after = buf; // Pointer to position after the data in buf + char name[MAX_NAME]; + User *curr = user_list; + + int nbytes; + while ((nbytes = read(client_fd, after, room)) > 0) + { + inbuf += nbytes; + int where; + while ((where = find_network_newline(buf, inbuf)) > 0) + { + buf[where - 2] = '\0'; + strcpy(name, buf); + printf(""test (name): %s\n"", name); + if (curr != NULL) + { + while (curr->next != NULL) + { + if (strcmp(name, curr->name) == 0) + { // there is already a user with this name + write(client_fd, ""Welcome Back\n"", 13); + break; + } + curr = curr->next; + } + if (curr->next == NULL) + { // create a new user + struct user *new_user = calloc(sizeof(User), 1); + strncpy(new_user->name, buf, MAX_NAME); // didn't malloc space for name, thus, it is read-only + strcat(new_user->name, ""\r\n""); + new_user->next = NULL; + curr->next = new_user; + } + } + else + { // curr is NULL and the linkedlist is empty + struct user *new_user = calloc(sizeof(User), 1); + strncpy(new_user->name, buf, MAX_NAME); + strcat(new_user->name, ""\r\n""); + new_user->next = NULL; + curr = new_user; + } + inbuf = inbuf - where; + for (int i = 0; i < where; i++) + { + buf[i] = '\0'; + } + memmove(buf, buf + where, inbuf); + for (int i = where; i < INPUT_BUFFER_SIZE; i++) + { + buf[i] = '\0'; + } + } + after = buf + inbuf; + room = INPUT_BUFFER_SIZE - inbuf; + } +}",, +63c647dc5f004bf4898cf752,2023-04-05T16:00:41.850Z,Help Fix Code,v2,"how to fix friend_server.c:369:12: error: function returns address of local variable [-Werror=return-local-addr] + 369 | return name; +","char * check_and_add_user(User *user_list, int client_fd) +{ + char buf[INPUT_BUFFER_SIZE] = {'\0'}; + int inbuf = 0; // How many bytes currently in buffer? + int room = sizeof(buf); // How many bytes remaining in buffer? + char *after = buf; // Pointer to position after the data in buf + char name[MAX_NAME]; + User *curr = user_list; + + int nbytes; + while ((nbytes = read(client_fd, after, room)) > 0) + { + inbuf += nbytes; + int where; + while ((where = find_network_newline(buf, inbuf)) > 0) + { + buf[where - 2] = '\0'; + strcpy(name, buf); + printf(""test (name): %s\n"", name); + if (curr != NULL) + { + while (curr->next != NULL) + { + if (strcmp(name, curr->name) == 0) + { // there is already a user with this name + write(client_fd, ""Welcome Back\n"", 13); + break; + } + curr = curr->next; + } + if (curr->next == NULL) + { // create a new user + struct user *new_user = calloc(sizeof(User), 1); + strncpy(new_user->name, buf, MAX_NAME); // didn't malloc space for name, thus, it is read-only + strcat(new_user->name, ""\r\n""); + new_user->next = NULL; + curr->next = new_user; + printf(""added_user succesfully, not first\n""); + } + } + else + { // curr is NULL and the linkedlist is empty + struct user *new_user = calloc(sizeof(User), 1); + strncpy(new_user->name, buf, MAX_NAME); + strcat(new_user->name, ""\r\n""); + new_user->next = NULL; + curr = new_user; + printf(""added_user succesfully, first\n""); + } + inbuf = inbuf - where; + for (int i = 0; i < where; i++) + { + buf[i] = '\0'; + } + memmove(buf, buf + where, inbuf); + for (int i = where; i < INPUT_BUFFER_SIZE; i++) + { + buf[i] = '\0'; + } + } + after = buf + inbuf; + room = INPUT_BUFFER_SIZE - inbuf; + printf(""check and add user complete \n""); + + } + return name; +}",, +63c647dc5f004bf4898cf752,2023-04-05T16:43:47.015Z,Explain Code,v2,,"if (FD_ISSET(listen_soc, &listen_fds)) { + client_fd = accept_connection(listen_soc, client_list); + printf(""main (test) client fd: %d\n"", client_fd); + if (client_fd == -1) { + perror(""accept""); + return -1; + } + printf(""clinet_fd: %d\n"", client_fd); + printf(""max_fd: %d\n"", max_fd); + + Client *client_ptr = find_client(client_fd, client_list); + + if (client_ptr != NULL) { + printf(""client found\n""); + } + if (client_ptr == NULL) { + printf(""main _ new connection\n""); + write(client_fd, ""What is your user name?\n"", 24); + User *user = check_and_add_user(user_list, client_fd); + + Client *temp_client = client_list; + while (temp_client != NULL) { + temp_client = temp_client->next; + } + temp_client->fd = client_fd; + temp_client->user = user; + temp_client->username = user->name; + temp_client->next = NULL; + + } else { + cleanup_args_for_processing(user_list, client_fd, client_list); + }",, +63c647dc5f004bf4898cf752,2023-04-05T16:44:23.921Z,Help Fix Code,v2,"This code checks if a new connection has been made on the listening socket. If a new connection has been made, it accepts the connection and adds the new client to the client list. If the client is already in the list, it cleans up the arguments for processing. If the client is not in the list, it prompts the client for their username and adds the user to the user list. It then creates a new client object and adds it to the client list.","if (FD_ISSET(listen_soc, &listen_fds)) + { + client_fd = accept_connection(listen_soc, client_list); + printf(""main (test) client fd: %d\n"", client_fd); + if (client_fd == -1) + { + perror(""accept""); + return -1; + } + printf(""clinet_fd: %d\n"", client_fd); + printf(""max_fd: %d\n"", max_fd); + + Client *client_ptr = find_client(client_fd, client_list); + + if (client_ptr != NULL){ + printf(""client found\n""); + } + if (client_ptr == NULL){ // this is a new socket + printf(""main _ new connection\n""); + write(client_fd, ""What is your user name?\n"", 24); + User *user = check_and_add_user(user_list, client_fd); + // strncpy(client_ptr->username, name, MAX_NAME); + + // add client_fd into client_list + Client *temp_client = client_list; + while (temp_client != NULL){ + temp_client = temp_client->next; + } + temp_client->fd = client_fd; + temp_client->user = user; + temp_client->username = user->name; + temp_client->next = NULL; + + } else { // this is not a new socket + cleanup_args_for_processing(user_list, client_fd, client_list); + } +",, +63c647dc5f004bf4898cf752,2023-04-05T16:45:54.978Z,Help Fix Code,v2,How to check if client is already in the list?,"if (FD_ISSET(listen_soc, &listen_fds)) + { + client_fd = accept_connection(listen_soc, client_list); + printf(""main (test) client fd: %d\n"", client_fd); + if (client_fd == -1) + { + perror(""accept""); + return -1; + } + printf(""clinet_fd: %d\n"", client_fd); + printf(""max_fd: %d\n"", max_fd); + + Client *client_ptr = find_client(client_fd, client_list); + + if (client_ptr != NULL){ + printf(""client found\n""); + } + if (client_ptr == NULL){ // this is a new socket + printf(""main _ new connection\n""); + write(client_fd, ""What is your user name?\n"", 24); + User *user = check_and_add_user(user_list, client_fd); + // strncpy(client_ptr->username, name, MAX_NAME); + + // add client_fd into client_list + Client *temp_client = client_list; + while (temp_client != NULL){ + temp_client = temp_client->next; + } + temp_client->fd = client_fd; + temp_client->user = user; + temp_client->username = user->name; + temp_client->next = NULL; + + } else { // this is not a new socket + cleanup_args_for_processing(user_list, client_fd, client_list); + } +",, +63c647dc5f004bf4898cf752,2023-04-05T17:08:37.613Z,Question from Code,v2,Why is my code running check_and_add_user even when the client already exist?,"if (FD_ISSET(listen_soc, &listen_fds)) { + client_fd = accept_connection(listen_soc, client_list); + printf(""main (test) client fd: %d\n"", client_fd); + if (client_fd == -1) { + perror(""accept""); + return -1; + } + printf(""clinet_fd: %d\n"", client_fd); + printf(""max_fd: %d\n"", max_fd); + + Client *client_ptr = find_client(client_fd, client_list); + + if (client_ptr != NULL) { + printf(""client found\n""); + } + if (client_ptr == NULL) { + printf(""main _ new connection\n""); + write(client_fd, ""What is your user name?\n"", 24); + User *user = check_and_add_user(user_list, client_fd); + + Client *new_client = (Client *)malloc(sizeof(Client)); + new_client->fd = client_fd; + new_client->user = user; + new_client->username = user->name; + new_client->next = NULL; + if (client_list == NULL) { + client_list = new_client; + } else { + Client *temp_client = client_list; + while (temp_client->next != NULL) { + temp_client = temp_client->next; + } + temp_client->next = new_client; + } + + } else { + cleanup_args_for_processing(user_list, client_fd, client_list); + }",, +63c647dc5f004bf4898cf752,2023-04-05T17:09:32.593Z,Help Fix Code,v2,"return a pointer to the Client structure that corresponds to the given file descriptor, or NULL if no such Client structure exists","Client *find_client(int client_fd, const Client *head) +{ + while (head != NULL && head->fd != client_fd) + { + head = head->next; + } + return (Client *)head; +}",, +63c647dc5f004bf4898cf752,2023-04-05T17:10:44.972Z,Help Fix Code,v2,"return a pointer to the Client structure that corresponds to the given file descriptor, or NULL if no such Client structure exists","Client *find_client(int client_fd, const Client *client_list) +{ + Client *curr = client_list; + while (curr != NULL && curr->fd != client_fd) + { + curr = curr->next; + } + return (Client *)curr; +} +",, +63c647dc5f004bf4898cf752,2023-04-05T22:27:40.213Z,Question from Code,v2,Why does this not write even when there is a user->name that is equal name?,"while (curr->next != NULL) { + if (strcmp(curr->name, name) == 0) { + write(client_fd, ""Welcome Back\n"", 13); + break; + } + curr = curr->next; +}",, +63c647dc5f004bf4898cf752,2023-04-05T23:39:21.429Z,General Question,v2,how to set input stream as client_socket?,,, +63c647dc5f004bf4898cf752,2023-04-06T00:26:43.626Z,Question from Code,v2,"What would be the output if this function is ran on (""list_users"", """")?","int tokenize(char *cmd, char **cmd_argv) { + int cmd_argc = 0; + char *next_token = strtok(cmd, DELIM); + while (next_token != NULL) { + if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) { + error(""Too many arguments!""); + cmd_argc = 0; + break; + } + cmd_argv[cmd_argc] = next_token; + cmd_argc++; + next_token = strtok(NULL, DELIM); + } + + return cmd_argc; +}",, +63c647dc5f004bf4898cf752,2023-04-06T01:35:41.859Z,Question from Code,v2,why is strncpy giving me a segmentation fault?,"for (int i = 0; i < cmd_argc; i++) { + char new_str[strlen(cmd_argv[i])]; + while (cmd_argv[i][j]) { + if (cmd_argv[i][j] != '\r' && cmd_argv[i][j] != '\n' && cmd_argv[i][j] != '\0') { + len++; + new_str[j] = cmd_argv[i][j]; + } + strncpy(cmd_argv[i], new_str, len); + } +}",, +63c647dc5f004bf4898cf752,2023-04-06T02:37:01.150Z,General Question,v2,"How to solve (.text+0x24): undefined reference to `main' +collect2: error: ld returned 1 exit status",,, +63c647dc5f004bf4898cf752,2023-04-06T18:57:17.335Z,Help Fix Code,v2,"Why is this code not listening for new clients? when there is a new client, it should print hello"," +int main(int argc, char *argv[]) +{ + User *user_list = NULL; + // User *curr = user_list; // curr is the head of the user_list + Client *client_list = NULL; + // Create socket + int listen_soc = socket(AF_INET, SOCK_STREAM, 0); + if (listen_soc == -1) + { + perror(""server: socket""); + exit(1); + } + // initialize server address + struct sockaddr_in server; + server.sin_family = AF_INET; + server.sin_port = htons(PORT); + memset(&server.sin_zero, 0, 8); + server.sin_addr.s_addr = INADDR_ANY; + /* + * Releasing port as soon as my server process terminates. + */ + int on = 1; + int status = setsockopt(listen_soc, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)); + if (status == -1) + { + perror(""setsockopt -- REUSEADDR""); + } + memset(&server.sin_zero, 0, 8); + // bind socket to an address + if (bind(listen_soc, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) == -1) + { + perror(""server: bind""); + close(listen_soc); + exit(1); + } + // Set up a queue in the kernel to hold pending connections. + if (listen(listen_soc, 5) < 0) + { + // listen failed + perror(""listen""); + exit(1); + } + int max_fd = listen_soc; + fd_set all_fds; + FD_ZERO(&all_fds); + FD_SET(listen_soc, &all_fds); + int client_fd; + while (1) + { + // select updates the fd_set it receives, so we always use a copy and retain the original. + fd_set listen_fds = all_fds; + if (select(max_fd + 1, &listen_fds, NULL, NULL, NULL) == -1) + { + perror(""server: select""); + exit(1); + } + // Is it the original socket? Create a new connection ... + if (FD_ISSET(listen_soc, &listen_fds)) + { + client_fd = accept_connection(listen_soc, client_list); + if (client_fd > max_fd) + { printf(""hello""); } + FD_SET(client_fd, &all_fds); + } + // Check the clients + Client *curr_client = client_list; + while (curr_client != NULL) + { + if (curr_client->fd > -1 && FD_ISSET(curr_client->fd, &listen_fds)) + { + int client_closed = cleanup_args_for_processing(user_list, curr_client->fd, client_list); } + curr_client = curr_client->next; + } + } + return 0; +} +",, +63c647dc5f004bf4898cf752,2023-04-06T19:40:40.683Z,Help Fix Code,v2,why is this code never adding to client_list?,"if (client_fd > max_fd) + { // this is a new socket + max_fd = client_fd; + + write(client_fd, ""What is your user name?\n"", 24); + check_and_add_user(user_list, client_fd, client_list); + + // add client_fd into client_list + Client *new_client = (Client *)malloc(sizeof(Client)); + if (new_client == NULL){ + perror(""malloc""); + exit(1); + } + new_client->fd = client_fd; + // new_client->user = user; + new_client->user = NULL; + new_client->username = NULL; + new_client->next = NULL; + if (client_list == NULL) + { + client_list = new_client; + } + else + { + Client *temp_client = client_list; + while (temp_client->next != NULL) + { + temp_client = temp_client->next; + } + temp_client->next = new_client; + } + }",, +63c647dc5f004bf4898cf752,2023-04-06T19:41:50.023Z,Help Fix Code,v2,why is this code never adding to client_list?," if (client_fd > max_fd) + { // this is a new socket + + + write(client_fd, ""What is your user name?\n"", 24); + check_and_add_user(user_list, client_fd, client_list); + + // add client_fd into client_list + Client *new_client = (Client *)malloc(sizeof(Client)); + if (new_client == NULL){ + perror(""malloc""); + exit(1); + } + new_client->fd = client_fd; + // new_client->user = user; + new_client->user = NULL; + new_client->username = NULL; + new_client->next = NULL; + if (client_list == NULL) + { + client_list = new_client; + } + else + { + Client *temp_client = client_list; + while (temp_client->next != NULL) + { + temp_client = temp_client->next; + } + temp_client->next = new_client; + } + max_fd = client_fd; + }",, +63c647dc5f004bf4898cf752,2023-04-25T15:56:42.963Z,Explain Code,v2,,"int write_inode(char *filename int index, char *name, int offset, int length) { + Inode inode; + FILE *fs = fopen(filenname, ""r+""); + strncpy(inode.name, name, MAXNAME); + inode.offset = offset; + inode.length = length; + if (fseek(fs, index * sizeof(Inode), SEEK_SET) == -1) { + perror(""fseek""); + return -1; + }; + fwrite(&inode, sizeof(Inode), 1, fs); + return 0; +}",, +63c647dc5f004bf4898cf752,2023-04-25T21:18:22.315Z,Explain Code,v2,,"int main() { + pipe(fd); + int pid = fork(); + if (pid == 0) { + close(fd[0]); + struct sigaction sa; + sa.sa_handler = send_one; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + sigaction(SIGUSR1, &sa, NULL); + while (1) + ; + } else if (pid > 0) { + close(fd[1]); + int count = 0; + while (1) { + } + } + return 0; +}",, +63c647dd5f004bf4898cf757,2023-01-18T02:17:19.605Z,Question from Code,v1,why does it print nothing and exit when stdin gets -1?,"#include + +int main() { + char phone[11]; + int integer; + scanf(""%s"", phone); + scanf(""%d"", &integer); + + while (integer != EOF) { + if (integer == -1) { + for (int i = 0; i < 10; i++) { + printf(""%c"", phone[i]); + } + printf(""\n""); + } + + else if (integer > -1 && integer < 10) { + printf(""%c\n"", phone[integer]); + } + + else { + printf(""ERROR\n""); + return 1; + } + + scanf(""%d"", &integer); + } + + return 0; +} +",, +63c647dd5f004bf4898cf757,2023-03-12T23:14:49.758Z,Explain Code,v2,,"Rule *curr = rules; // want to iterate to end of rules +while (curr->next_rule != NULL) { + curr = curr->next_rule; +} // curr->next_rule == NULL +curr->next_rule = new_rule; // insert new rule to end of rules",, +63c647dd5f004bf4898cf757,2023-03-12T23:15:32.652Z,Help Fix Code,v2,insert new rule to the end of rules,"Rule *curr = rules; // want to iterate to end of rules + while (curr->next_rule != NULL) { + curr = curr->next_rule; + } // curr->next_rule == NULL + curr->next_rule = new_rule; // insert new rule to end of rules",, +63c647dd5f004bf4898cf757,2023-03-12T23:16:35.012Z,Help Fix Code,v2,insert new rule to end of rules,"if (rules == NULL) {rules = new_rule;} // if rules == NULL, new_rule becomes first rule + else { // if rules != NULL + Rule *curr = rules; // want to iterate to end of rules + while (curr->next_rule != NULL) { + curr = curr->next_rule; + } // curr->next_rule == NULL + curr->next_rule = new_rule; // insert new rule to end of rules + }",, +63c647dd5f004bf4898cf75c,2023-01-24T05:55:41.783Z,Help Fix Code,v1,,"char *ch = ""S"";",, +63c647dd5f004bf4898cf75c,2023-01-24T05:56:14.498Z,General Question,v1,Declare a char variable named ch and set its value to the first letter of your own name.,,, +63c647dd5f004bf4898cf75c,2023-01-31T02:34:26.209Z,Explain Code,v1,,"*delicious = ""ice cream"";",, +63c647dd5f004bf4898cf75c,2023-01-31T02:34:50.028Z,General Question,v1,"Set the value of delicious (which has already been declared) to ""ice cream"".",,, +63c647dd5f004bf4898cf75c,2023-01-31T02:42:01.806Z,General Question,v1,"The string sweet has been declared on the heap and has space for 20 characters. +Set the value of sweet to ""tiramisu"" without changing its address.",,, +63c647dd5f004bf4898cf75c,2023-01-31T04:35:08.069Z,General Question,v1,"Create a node struct with the following struct members: +- value, an integer +- next, a pointer to another node",,, +63c647dd5f004bf4898cf75c,2023-01-31T04:36:00.551Z,General Question,v1,"generate a detailed documentation of `typedef struct node { + int value; + struct node *next; +} node;` with usage examples and explanations",,, +63c647dd5f004bf4898cf75c,2023-01-31T04:38:00.766Z,General Question,v1,Write a loop which traverses a linked list starting at front and prints the value of each node in the list.,,, +63c647dd5f004bf4898cf75c,2023-01-31T04:46:43.905Z,General Question,v1,Which of these scenarios cause a memory leak?,,, +63c647dd5f004bf4898cf75c,2023-01-31T04:58:03.503Z,General Question,v1,"Define a string literal called season that has the value +""winter"". + +Define a stack-allocated string called holiday that has space for 20 characters +including the null terminator. Set its initial value to ""Valentine's Day"".",,, +63c647dd5f004bf4898cf75c,2023-01-31T04:59:28.764Z,General Question,v1,The difference between stack-allocated string and heap-allocated string,,, +63c647dd5f004bf4898cf75c,2023-02-13T23:14:00.074Z,Question from Code,v1,"What will be the output of the following program fragment? +"," +#define SUPERVISOR(regular) regular + 5 +int main() { + int regular_pay = 20; + int hours_worked = 10; + printf(""pay is %d\n"", (hours_worked * SUPERVISOR(regular_pay))); + // rest omitted +}",, +63c647dd5f004bf4898cf75c,2023-02-13T23:17:41.342Z,Question from Code,v1,"What is the line to compile it into an executable named a.out that it will print the value 5 to stdout? (You do not need to include the options to display warnings, build the symbol table for the debugger or select the C standard.)","#include +int main() { + + #ifdef MACRO + printf(""%d\n"", MACRO); + #endif + return 0; +}",, +63c647dd5f004bf4898cf75c,2023-02-13T23:19:00.695Z,General Question,v1,"suppose we have the following line at the top of our program + + +#define MAXNAME = 32; + + +and then the declaration + + +char name[MAXNAME]; + + +in the program. What will this declaration line become after the program has passed +through the C pre-processor?",,, +63c647dd5f004bf4898cf75c,2023-02-13T23:35:25.970Z,General Question,v1," +Which of the following functions are system calls? + +exit() +fgets() +fopen() +printf() +read() +scanf() +write() +",,, +63c647dd5f004bf4898cf75c,2023-02-13T23:35:55.272Z,General Question,v1," +Which of the following functions are library calls? + +exit() +fgets() +fopen() +printf() +read() +scanf() +write() +",,, +63c647dd5f004bf4898cf75c,2023-02-13T23:44:25.704Z,Question from Code,v1,"What will this declaration line become after the program has passed +through the C pre-processor?","#define MAXNAME = 32; +char name[MAXNAME];",, +63c647dd5f004bf4898cf75c,2023-02-13T23:53:31.008Z,Help Fix Code,v1,,"#define MAXNAME = 32; +char name[MAXNAME];",What will this declaration line become after the program has passed through the C pre-processor?, +63c647dd5f004bf4898cf75c,2023-02-21T16:12:02.103Z,Help Fix Code,v1,,"int update_pic(User *user, const char *filename) { + return -1; +}","/* + * 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. + */", +63c647dd5f004bf4898cf75c,2023-02-21T16:42:09.073Z,Help Fix Code,v1,,"int update_pic(User *user, const char *filename) { + if (strlen(filename) + 1 > MAX_NAME) { + return 2; + } + + struct stat foo; + if (stat(filename, &foo) != 0) { + return 1; + } + + strcpy(user->profile_pic, filename); + return 0; +}",/* * 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. */, +63c647dd5f004bf4898cf75c,2023-02-21T16:47:35.270Z,Help Fix Code,v1,,"int update_pic(User *user, const char *filename) { + if (strlen(filename) + 1 > MAX_NAME) { + return 2; + } + + struct stat foo; + if (stat(filename, &foo) != 0) { + return 1; + } + + strcpy(user->profile_pic, filename); + return 0; +}","/* * 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. */ +Has to be valid for C99", +63c647dd5f004bf4898cf75c,2023-02-21T19:32:58.156Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { + return 2; + } + + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(1); + } + strncpy(new_user->name, name, MAX_NAME); // name has max length MAX_NAME - 1 + + for (int i = 0; i < MAX_NAME; i++) { + new_user->profile_pic[i] = '\0'; + } + + new_user->first_post = NULL; + new_user->next = NULL; + for (int i = 0; i < MAX_FRIENDS; i++) { + new_user->friends[i] = NULL; + } + + // Add user to list + User *prev = NULL; + User *curr = *user_ptr_add; + while (curr != NULL && strcmp(curr->name, name) != 0) { + prev = curr; + curr = curr->next; + } + + if (prev == NULL) { + *user_ptr_add = new_user; + return 0; + } else if (curr != NULL) { + free(new_user); + return 1; + } else { + prev->next = new_user; + return 0; + } +}","/* + * 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) + */", +63c647dd5f004bf4898cf75c,2023-02-21T19:36:59.787Z,Explain Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= MAX_NAME) { + return 2; + } + + User *new_user = malloc(sizeof(User)); + if (new_user == NULL) { + perror(""malloc""); + exit(1); + } + strncpy(new_user->name, name, MAX_NAME); // name has max length MAX_NAME - 1 + + for (int i = 0; i < MAX_NAME; i++) { + new_user->profile_pic[i] = '\0'; + } + + new_user->first_post = NULL; + new_user->next = NULL; + for (int i = 0; i < MAX_FRIENDS; i++) { + new_user->friends[i] = NULL; + } + + // Add user to list + User *prev = NULL; + User *curr = *user_ptr_add; + while (curr != NULL && strcmp(curr->name, name) != 0) { + prev = curr; + curr = curr->next; + } + + if (prev == NULL) { + *user_ptr_add = new_user; + return 0; + } else if (curr != NULL) { + free(new_user); + return 1; + } else { + prev->next = new_user; + return 0; + } +}",, +63c647dd5f004bf4898cf75c,2023-02-21T21:36:22.659Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + for (User *ptr = (User *)head; ptr != NULL; ptr = ptr->next) { + if (strcmp(ptr->name, name) == 0) { + return ptr; + } + } + + return NULL; +}","/* + * 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. + */", +63c647dd5f004bf4898cf75c,2023-02-21T21:49:11.616Z,Explain Code,v1,,"User *find_user(const char *name, const User *head) { + for (User *ptr = (User *)head; ptr != NULL; ptr = ptr->next) { + if (strcmp(ptr->name, name) == 0) { + return ptr; + } + } + + return NULL; +}",, +63c647dd5f004bf4898cf75c,2023-02-22T18:59:54.627Z,Help Fix Code,v1,," while (strcmp(curr->name, name) && curr != NULL != 0) { + prev = curr; + curr = curr->next; + }","* + iterates through the linked list until curr is NULL or the + name field of curr is equal to the string + */", +63c647dd5f004bf4898cf75c,2023-02-22T20:59:15.488Z,Explain Code,v1,,"void printPicture(const User *user) { + + //if they have no profile picture we can just return since there is nothing + //to print at all. + if (strlen(user->profile_pic) <= 0) { + return; + } + + //continues onward because there is something that is larger than 0, + //in the profile pic object. + FILE *picture = fopen(user->profile_pic, ""r""); + + int edpoint; + char printBuffer[1023]; + + while (fgets(printBuffer, 1023, picture) != NULL) { + edpoint = strlen(printBuffer) - 1; + if (printBuffer[edpoint] == '\n') { + printBuffer[edpoint] = '\0'; + } + + printEntry(printBuffer); + } + + fclose(picture); +}",, +63c647dd5f004bf4898cf75c,2023-02-22T21:07:39.103Z,Help Fix Code,v1,,"void printPicture(const User *user) { + + //if they have no profile picture we can just return since there is nothing + //to print at all. + if (strlen(user->profile_pic) <= 0) { + return; + } + + //continues onward because there is something that is larger than 0, + //in the profile pic object. + FILE *picture = fopen(user->profile_pic, ""r""); + + int edpoint; + char printBuffer[1023]; + + while (fgets(printBuffer, 1023, picture) != NULL) { + edpoint = strlen(printBuffer) - 1; + //checks if the last character of the string in printBuffer is a newline + if (printBuffer[edpoint] == '\n') { + /*replaces the last character of the string + in printBuffer with a null character*/ + printBuffer[edpoint] = '\0'; + } + //prints the string in printBuffer + printEntry(printBuffer); + } + //close the file + fclose(picture); +} + +/*Helper function that deletes all of the posts for a user and thenfrees all of +the memory for the all posts for that user*/ +void removePosts(Post *post) { + //if the user has no posts then return because there is nothing to free. + if (!post) { + return; + } + + //recursive function until we run out of posts to free. + removePosts(post->next); + //free the memory for the contents of the posts + free(post->contents); + + free(post); +}",remove all the posts in the linked list., +63c647dd5f004bf4898cf75c,2023-02-22T21:09:11.051Z,Explain Code,v1,,"void removePosts(Post *post) { + //if the user has no posts then return because there is nothing to free. + if (!post) { + return; + } + + //recursive function until we run out of posts to free. + removePosts(post->next); + //free the memory for the contents of the posts + free(post->contents); + + free(post); +}",, +63c647dd5f004bf4898cf75c,2023-02-22T21:26:56.080Z,Explain Code,v1,,"/*Purges the user from the friends list of all users in the targets list.*/ +void unAddfriend(User *exiledFriend, const User *user) { + if (!exiledFriend) { + return; + } + + int difference, iterator = 0; + for (iterator = 0; iterator < MAX_FRIENDS && + exiledFriend->friends[iterator]; iterator++) { + if (exiledFriend->friends[iterator] == user) { + difference++; + } + + if (iterator + difference < MAX_FRIENDS) { + exiledFriend->friends[iterator] = + exiledFriend->friends[iterator + difference]; + } else { + exiledFriend->friends[iterator] = NULL; + } + } + + unAddfriend(exiledFriend->next, user); +}",, +63c647dd5f004bf4898cf75c,2023-02-22T21:39:36.065Z,Explain Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + + int friendstatus = 0; + + User *friendo1 = find_user(name1, head); + User *friendo2 = find_user(name2, head); + + if (friendo1 == NULL || friendo2 == NULL) { + friendstatus = 4; + + } else if (friendo1 == friendo2) { + friendstatus = 3; + + } + + int a; + for (a = 0; a < MAX_FRIENDS; a++) { + if (friendo1->friends[a] == NULL) { + break; + + } else if (friendo1->friends[a] == friendo2) { + friendstatus = 1; + } + } + + int b; + for (b = 0; b < MAX_FRIENDS; b++) { + if (friendo2->friends[b] == NULL) { + break; + } + } + + if (a == MAX_FRIENDS || b == MAX_FRIENDS) { + + friendstatus = 2; + } + + friendo1->friends[a] = friendo2; + friendo2->friends[b] = friendo1; + + return friendstatus; +}",, +63c647dd5f004bf4898cf75c,2023-02-22T21:55:55.866Z,Help Fix Code,v1,,"int difference, iterator = 0;",initialise two variables with the same initial value. , +63c647dd5f004bf4898cf75c,2023-02-22T22:21:20.523Z,Explain Code,v1,,"int make_post(const User *author, User *target, char *contents) { + if (target == NULL || author == NULL) { + return 2; + } + + int friends = 0; + for (int i = 0; i < MAX_FRIENDS && target->friends[i] != NULL; i++) { + if (strcmp(target->friends[i]->name, author->name) == 0) { + friends = 1; + break; + } + } + + if (friends == 0) { + return 1; + } + + // Create post + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); + exit(1); + } + strncpy(new_post->author, author->name, MAX_NAME); + new_post->contents = contents; + new_post->date = malloc(sizeof(time_t)); + if (new_post->date == NULL) { + perror(""malloc""); + exit(1); + } + time(new_post->date); + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; +}",, +63c647dd5f004bf4898cf75c,2023-02-22T22:22:11.690Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + if (target == NULL || author == NULL) { + return 2; + } + + int friends = 0; + for (int i = 0; i < MAX_FRIENDS && target->friends[i] != NULL; i++) { + if (strcmp(target->friends[i]->name, author->name) == 0) { + friends = 1; + break; + } + } + + if (friends == 0) { + return 1; + } + + // Create post + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); + exit(1); + } + strncpy(new_post->author, author->name, MAX_NAME); + new_post->contents = contents; + new_post->date = malloc(sizeof(time_t)); + if (new_post->date == NULL) { + perror(""malloc""); + exit(1); + } + time(new_post->date); + new_post->next = target->first_post; + target->first_post = new_post; + + 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 + */ +", +63c647dd5f004bf4898cf75c,2023-02-22T22:25:31.705Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + if (target == NULL || author == NULL) { + return 2; + } + + int friends = 0; + for (int i = 0; i < MAX_FRIENDS && target->friends[i] != NULL; i++) { + if (strcmp(target->friends[i]->name, author->name) == 0) { + friends = 1; + break; + } + } + + if (friends == 0) { + return 1; + } + + // Create post + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); + exit(1); + } + strncpy(new_post->author, author->name, MAX_NAME); + new_post->contents = contents; + new_post->date = malloc(sizeof(time_t)); + if (new_post->date == NULL) { + perror(""malloc""); + exit(1); + } + time(new_post->date); + + + target->first_post = new_post; + new_post->next = target->first_post; + + 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 */ +", +63c647dd5f004bf4898cf75c,2023-02-22T23:06:52.061Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents)","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 */", +63c647dd5f004bf4898cf75c,2023-02-22T23:07:45.964Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) +{ + if (target == NULL || author == NULL) + { + return 2; + } + + int friends = 0; + for (int i = 0; i < MAX_FRIENDS && target->friends[i] != NULL; i++) + { + if (strcmp(target->friends[i]->name, author->name) == 0) + { + friends = 1; + break; + } + } + + if (friends == 0) + { + return 1; + } + + /*Creating a post. allocates memory for a post and stores the address in + new_post */ + Post *newlyPosted = malloc(sizeof(Post)); + + //checks if new post is NULL. + if (newlyPosted == NULL) + { + perror(""malloc couldn't allocate memory""); + exit(1); + } + strncpy(newlyPosted->author, author->name, MAX_NAME); + newlyPosted->contents = contents; + newlyPosted->date = malloc(sizeof(time_t)); + if (newlyPosted->date == NULL) + { + perror(""malloc couldn't allocate memory""); + exit(1); + } + time(newlyPosted->date); + + //newlyPosted->next = target->first_post; + //target->first_post = newlyPosted; + + target->first_post = newlyPosted; + + + + 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 */", +63c647dd5f004bf4898cf75c,2023-02-22T23:09:39.431Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) +{ + if (target == NULL || author == NULL) + { + return 2; + } + + int friends = 0; + for (int i = 0; i < MAX_FRIENDS && target->friends[i] != NULL; i++) + { + if (strcmp(target->friends[i]->name, author->name) == 0) + { + friends = 1; + break; + } + } + + if (friends == 0) + { + return 1; + } + + /*Creating a post. allocates memory for a post and stores the address in + new_post */ + Post *newlyPosted = malloc(sizeof(Post)); + + //checks if new post is NULL. + if (newlyPosted == NULL) + { + perror(""malloc couldn't allocate memory""); + exit(1); + } + strncpy(newlyPosted->author, author->name, MAX_NAME); + newlyPosted->contents = contents; + newlyPosted->date = malloc(sizeof(time_t)); + if (newlyPosted->date == NULL) + { + perror(""malloc couldn't allocate memory""); + exit(1); + } + time(newlyPosted->date); + + newlyPosted->next = target->first_post; + target->first_post = newlyPosted; + + + 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 */", +63c647dd5f004bf4898cf75c,2023-02-22T23:12:11.647Z,Explain Code,v1,,"int make_post(const User *author, User *target, char *contents) +{ + if (target == NULL || author == NULL) + { + return 2; + } + + int friends = 0; + for (int i = 0; i < MAX_FRIENDS && target->friends[i] != NULL; i++) + { + if (strcmp(target->friends[i]->name, author->name) == 0) + { + friends = 1; + break; + } + } + + if (friends == 0) + { + return 1; + } + + /*Creating a post. allocates memory for a post and stores the address in + new_post */ + Post *newlyPosted = malloc(sizeof(Post)); + + //checks if new post is NULL. + if (newlyPosted == NULL) + { + perror(""malloc couldn't allocate memory""); + exit(1); + } + strncpy(newlyPosted->author, author->name, MAX_NAME); + newlyPosted->contents = contents; + newlyPosted->date = malloc(sizeof(time_t)); + if (newlyPosted->date == NULL) + { + perror(""malloc couldn't allocate memory""); + exit(1); + } + time(newlyPosted->date); + + newlyPosted->next = target->first_post; + target->first_post = newlyPosted; + + + return 0; +}",, +63c647dd5f004bf4898cf75c,2023-02-28T02:29:04.856Z,General Question,v1,"Select the sentence fragments that complete this sentence so that it is true. + +When one process forks another: + + +the process that calls fork is called the parent and the newly-created process is the child. +the code segments of the two processes are identical. +the values of all the variables of the two processes are identical. +the two processes have all the same variables but each process has its own copy and the return value from the fork system call is different for the two processes because in each case it is set to the other's PID. +the operating system determines which of the two process gets to execute first. +the two process are siblings.",,, +63c647dd5f004bf4898cf75c,2023-02-28T02:30:25.810Z,General Question,v1,"A parent process with pid 1123 forks a child process with pid 1126. In the child process, what is the return value of fork()? +",,, +63c647dd5f004bf4898cf75c,2023-02-28T02:30:56.002Z,General Question,v1,"A parent process with pid 1123 forks a child process with pid 1126. In the parent process, what is the return value of fork()?",,, +63c647dd5f004bf4898cf75c,2023-02-28T02:32:29.078Z,General Question,v1,"On teach.cs, you can run man 2 wait to learn about the wait system call. Which macro is used with wait's status to check if the child process was terminated by a signal?",,, +63c647dd5f004bf4898cf75c,2023-03-03T02:59:42.533Z,Explain Code,v1,,"#include +#include +#include + + +int main(int argc, char **argv) { + int iterations; + + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + //Get the parent id + int parentPID = getpid(); + iterations = strtol(argv[1], NULL, 10); + + for (int loopval; = 0; loopval < iterations; loopval++) { + //Only fork if the PID matches the parent PID + if (getpid() == parentPID){ + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",, +63c647dd5f004bf4898cf75c,2023-03-03T03:06:47.983Z,Explain Code,v1,,"#include +#include +#include + + +int main(int argc, char **argv) { + + //iterations is an integer that stores + //the number of iterations the program will run + int loopval, iterations; + + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int forked; + //iterations is set to the integer representation of the + //command line argument (the second element of the array argv) + iterations = strtol(argv[1], NULL, 10); + + for (loopval = 0; loopval < iterations; + loopval++) { + //Fork once for the parent + if(loopval == 0){ + forked = fork(); + if(forked < 0) { + perror(""fork""); + exit(1); + } + } + else{ + //Fork once per child + if(forked == 0){ + forked = fork(); + if(forked < 0) { + perror(""fork""); + exit(1); + } + } + } + + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",, +63c647dd5f004bf4898cf75c,2023-03-03T03:27:32.750Z,Explain Code,v1,,"#include +#include +#include +#include + +int main(int argc, char **argv) { + int iterations, loopval, status; + + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + iterations = strtol(argv[1], NULL, 10); + + for (loopval = 0; loopval < iterations; + loopval++) { + + int forkedval = fork(); + + if (forkedval < 0) { + perror(""fork""); + exit(1); + } else if (forkedval == 0){ + loopval = iterations; + } else { + wait(&status); + } + + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",, +63c647dd5f004bf4898cf75c,2023-03-03T03:31:35.912Z,Explain Code,v1,,"#include +#include +#include +#include + +int main(int argc, char **argv) { + int iterations, loopval, status; + + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + iterations = strtol(argv[1], NULL, 10); + + for (loopval = 0; loopval < iterations; + loopval++) { + + int forkedval = fork(); + + if (forkedval < 0) { + perror(""fork""); + exit(1); + } else if (n > 0){ + loopval = iterations; + wait(&status); + } + + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",, +63c647dd5f004bf4898cf75c,2023-03-10T00:11:53.779Z,Help Fix Code,v1,,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + // pipe + int fd[2]; + if (pipe(fd) < 0) { + perror(""pipe""); + exit(1); + } + + // fork + pid_t pid = fork(); + if (pid == -1) { + perror(""fork""); + exit(1); + } + + // parent + if (pid > 0) { + // close the read end of the pipe for parent + if (close(fd[0]) == -1) { + perror(""close""); + exit(1); + } + // write user and pass to pipe + if (write(fd[1], user_id, strlen(user_id)) == -1) { + perror(""write""); + exit(1); + } + if (write(fd[1], password, strlen(password)) == -1) { + perror(""write""); + exit(1); + } + if (close(fd[1]) == -1) { // close the write pipe + perror(""close""); + exit(1); + } + + } + + else { + // close the write end of the pipe for child + if (close(fd[1]) == -1) { + perror(""close""); + exit(1); + } + // redirect stdin of child process to come from pipe + if (dup2(fd[0], STDIN_FILENO) == -1) { + perror(""dup2""); + exit(1); + } + // close file descriptors + if (close(fd[0]) == -1) { + perror(""close""); + exit(1); + } + // execute validate + execl(""./validate"", ""validate"", NULL); + perror(""execl""); + exit(1); + } + + int status; + // use child to verify user + wait(&status); + + if (WIFEXITED(status)) { + int exit_code = WEXITSTATUS(status); + switch(exit_code) { + case 0: + printf(SUCCESS); + break; + case 2: + printf(INVALID); + break; + case 3: + printf(NO_USER); + break; + } + } + + return 0; +} +","This is a C program that prompts the user to enter a user ID and password, passes them to a child process through a pipe, and uses the child process to validate the user's credentials by executing a separate program called ""validate"". + +The program reads the user ID and password from standard input using the fgets function, and stores them in the user_id and password variables respectively. + +It then creates a pipe using the pipe system call to establish a unidirectional communication channel between the parent and child processes. + +Next, it forks a child process using the fork system call, which creates a copy of the parent process. The parent process writes the user ID and password to the write end of the pipe, and the child process reads from the read end of the pipe. + +The child process redirects its standard input to read from the read end of the pipe using the dup2 system call, and then executes the ""validate"" program using the execl system call. + +The ""validate"" program reads the user ID and password from standard input and validates them against a database of user credentials. It returns an exit code to indicate whether the user credentials are valid or not. + +The parent process waits for the child process to terminate using the wait system call, and then checks the exit code returned by the child process. Depending on the exit code, it prints one of three possible messages to standard output: ""Password verified"", ""Invalid password"", or ""No such user"". + +Finally, the program returns 0 to indicate successful execution. + +Some of the improvements made to the original code include adding comments to explain the purpose of each section of code, removing unnecessary header files, defining constants for the possible output messages, and improving error handling by checking the return values of system calls and printing error messages when necessary.", +63c647dd5f004bf4898cf75c,2023-03-13T23:35:23.016Z,Question from Code,v2,"Assume the following program which compiles and runs without error. + + +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 ""); + // POSITION A + struct sigaction act; + act.sa_handler = handler; + act.sa_flags = 0; + sigemptyset(&act.sa_mask); + sigaction(SIGINT, &act, NULL); + + // POSITION B + x += 2; + + // POSITION C + fprintf(stderr, ""outside %d"", x); + + return 0; +}",, +63c647dd5f004bf4898cf75c,2023-03-13T23:40:00.268Z,Question from Code,v2,Assume the following code runs without error. ,"int x = 5; + +void handler(int sig) { + x += 3; + fprintf(stderr, ""inside %d "", x); +} + +int main() { + fprintf(stderr, ""start ""); + // POSITION A + struct sigaction act; + act.sa_handler = handler; + act.sa_flags = 0; + sigemptyset(&act.sa_mask); + sigaction(SIGINT, &act, NULL); + + // POSITION B + x += 2; + + // POSITION C + fprintf(stderr, ""outside %d"", x); + + return 0; +}",, +63c647dd5f004bf4898cf75c,2023-03-27T21:41:58.169Z,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. +",,, +63c647dd5f004bf4898cf75c,2023-03-27T21:44:18.560Z,General Question,v2,"Variables width and height have been initialized. Calculate the area of a rectangle using width and height, and assign the resulting value to a variable named area.",,, +63c647e15f004bf4898cf77a,2023-01-17T20:56:39.598Z,General Question,v1,what is string in C?,,, +63c647e25f004bf4898cf77f,2023-02-01T06:20:00.414Z,General Question,v1,"what is the error ""zsh: trace trap"" +",,, +63c647e25f004bf4898cf77f,2023-02-16T16:32:06.332Z,General Question,v1,What is the memset syntax?,,, +63c647e55f004bf4898cf798,2023-01-17T20:14:54.732Z,Help Fix Code,v1,,"void swapA(int *num1, int *num2) { + int *temp = num1; + num1 = num2; + num2 = temp; +}",swap num1 and num2, +63c647e55f004bf4898cf798,2023-01-17T20:16:07.998Z,Explain Code,v1,,"void swapA(int *num1, int *num2) { + int *temp = num1; + num1 = num2; + num2 = temp; +}",, +63c647e65f004bf4898cf79d,2023-01-22T21:29:16.935Z,Explain Code,v1,,"#include + +int main() { + + printf(""Hello, World!\n""); + + return 0; +} +",, +63c647e65f004bf4898cf79d,2023-02-02T00:42:57.663Z,Explain Code,v1,,"int main() { + // Declare a struct faculty named p1. + struct faculty p1; + + // Initialize p1 to represent Professor Roger Grosse, whose research area + // is ML (Machine Learning). He is supervising 11 graduate students. + p1.name = ""Professor Roger Grosse""; + strcpy(pl.area, ""ML""); + pl.num_students = 11; + + + struct faculty *p2_pt; + + // allocate space for the faculty on the heap + p2_pt = malloc(sizeof(struct faculty)); + + // Error checking + if (p2_pt == NULL) { + perror(""malloc""); //look at perror documentation + return 1; + } + + + // Set the values of *p2_pt to represent Professor Sheila McIlraith. Her research area + // is KR (Knowledge Representation). She is supervising 5 graduate students. + p2_pt->name = ""Professor Sheila McIlraith""; + strcpy(p2_pt->area, ""KR""); + p2_pt->num_students = 5; + + + return 0; +}",, +63c647e65f004bf4898cf79d,2023-02-12T16:13:25.938Z,General Question,v1,"how to get a human-readable representation of the date and time of each post in the format, ""Friday Jan 27 23:01:00 2023"" using time and ctime functions?",,, +63c647e65f004bf4898cf79d,2023-02-12T16:56:25.479Z,General Question,v1,generate a detailed documentation of `time` with usage examples and explanations,,, +63c647e65f004bf4898cf79d,2023-02-12T17:00:15.238Z,General Question,v1,Why is time and ctime not giving me the current time but an arbitrary time?,,, +63c647e65f004bf4898cf79d,2023-02-12T17:04:38.663Z,General Question,v1,generate a detailed documentation of `time` with usage examples and explanations,,, +63c647e65f004bf4898cf79d,2023-02-12T17:05:36.221Z,General Question,v1,how do I use time to get the current time? Not the time since epoch?,,, +63c647e65f004bf4898cf79d,2023-03-13T01:36:22.061Z,General Question,v2,how to use stat?,,, +63c647e65f004bf4898cf7a2,2023-01-17T15:09:44.024Z,General Question,v1,how do I append to a list in C?,,, +63c647e85f004bf4898cf7b1,2023-01-17T23:47:08.392Z,Question from Code,v1,"what does this do? +",char *result[2];,, +63c647e85f004bf4898cf7b1,2023-01-17T23:47:59.799Z,Question from Code,v1,"can you show me a visual representation? +","char *result[2]; +",, +63c647e85f004bf4898cf7b1,2023-02-10T21:56:06.541Z,Explain Code,v1,,"struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height);",, +63c647e85f004bf4898cf7b1,2023-02-10T21:59:22.720Z,Question from Code,v1,"So the result is a pixel struct, where each strcuts is apointer?","struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height);",, +63c647e85f004bf4898cf7b1,2023-02-10T22:12:15.212Z,General Question,v1,"how to allocate space on stack for many values? + + +",,, +63c647e85f004bf4898cf7b1,2023-02-10T22:13:06.058Z,General Question,v1,how to allocate space on the stack for a struct pointer?,,, +63c647e85f004bf4898cf7b1,2023-02-10T22:26:52.408Z,Question from Code,v1,what does this look like in memory?,"struct pixel **st_pixels; + st_pixels = malloc(sizeof(struct pixel *) * height); +",, +63c647e85f004bf4898cf7b1,2023-02-11T23:46:00.410Z,General Question,v1,an array of pointers to structs ,,, +63c647e85f004bf4898cf7b1,2023-02-12T02:24:26.354Z,General Question,v1,how to traverse a linked list to find a specific struct,,, +63c647e85f004bf4898cf7b1,2023-03-03T00:01:19.043Z,General Question,v1,explain fork(),,, +63c647e85f004bf4898cf7b1,2023-03-10T19:25:23.883Z,Explain Code,v2,,"int main(int argc, char **argv) { + // Declare any variables you need + int fd[argc][2]; + int one_value; + + // Write the code to loop over the command line arguments + for (int i = 1; i < argc; i++) { + // Call pipe before we fork + if (pipe(fd[i]) == -1) { + perror(""pipe""); + exit(1); + } + // Call fork + int result = fork(); + if (result < 0) { + perror(""fork""); + exit(1); + } else if (result == 0) { + // Child only writes to the pipe, so close reading end + Close(fd[i][0]); + // Before we forked, parent had open the reading ends to + // all previously forked children; so close those. + for (int j = 1; j < i; j++) { + Close(fd[j][0]); + } + // Now do the work - write the value in binary to the pipe + int length = strlen(argv[i]); + write(fd[i][1], &length, sizeof(int)); + // Close the pipe since we are done with it. + Close(fd[i][1]); + exit(0); + } else { + // In the parent, but before doing the next loop iteration, + // close the end of the pipe that we don't want open + Close(fd[i][1]); + } + } + // Only the parent gets here + int sum = 0; + + for (int i = 1; i < argc; i++) { + // Read one integer from each child + read(fd[i][0], &one_value, sizeof(int)); + printf(""I just read %d from pipe %d\n"", one_value, i); + + sum += one_value; + } + + printf(""Sum is %d\n"", sum); + return 0; +}",, +63c647e85f004bf4898cf7b1,2023-03-10T19:58:03.625Z,Question from Code,v2,when would we ever get to the else branch,"int main(int argc, char **argv) { + // Declare any variables you need + int fd[argc][2]; + int one_value; + + // Write the code to loop over the command line arguments + for (int i = 1; i < argc; i++) { + // Call pipe before we fork + if (pipe(fd[i]) == -1) { + perror(""pipe""); + exit(1); + } + // Call fork + int result = fork(); + if (result < 0) { + perror(""fork""); + exit(1); + } else if (result == 0) { + // Child only writes to the pipe, so close reading end + Close(fd[i][0]); + // Before we forked, parent had open the reading ends to + // all previously forked children; so close those. + for (int j = 1; j < i; j++) { + Close(fd[j][0]); + } + // Now do the work - write the value in binary to the pipe + int length = strlen(argv[i]); + write(fd[i][1], &length, sizeof(int)); + // Close the pipe since we are done with it. + Close(fd[i][1]); + exit(0); + } else { + // In the parent, but before doing the next loop iteration, + // close the end of the pipe that we don't want open + Close(fd[i][1]); + } + } + // Only the parent gets here + int sum = 0; + + for (int i = 1; i < argc; i++) { + // Read one integer from each child + read(fd[i][0], &one_value, sizeof(int)); + printf(""I just read %d from pipe %d\n"", one_value, i); + + sum += one_value; + } + + printf(""Sum is %d\n"", sum); + return 0; +}",, +63c647e85f004bf4898cf7b1,2023-03-10T21:01:58.427Z,General Question,v2,STDIN_FILENO what is that,,, +63c647e85f004bf4898cf7b1,2023-03-10T21:01:58.737Z,General Question,v2,STDIN_FILENO what is that,,, +63c647e85f004bf4898cf7b1,2023-03-10T22:04:12.758Z,General Question,v2,what is excel,,, +63c647e85f004bf4898cf7b1,2023-03-10T22:04:34.780Z,General Question,v2,excel() funtion,,, +63c647e85f004bf4898cf7b1,2023-03-10T22:05:10.125Z,General Question,v2,execl() what is it?,,, +63c647e85f004bf4898cf7b1,2023-03-10T22:14:36.624Z,General Question,v2,how does dup2 work,,, +63c647e85f004bf4898cf7b1,2023-03-10T22:48:22.949Z,General Question,v2,what are argyments to pipe,,, +63c647e85f004bf4898cf7b1,2023-03-13T23:35:58.059Z,General Question,v2,how to check if a file in a directory exists?,,, +63c647e85f004bf4898cf7b1,2023-03-13T23:51:59.226Z,General Question,v2,how are makefiles evaluated,,, +63c647e85f004bf4898cf7b1,2023-03-14T00:25:37.580Z,General Question,v2,can you use printf on an array of char pointers?,,, +63c647e85f004bf4898cf7b1,2023-03-14T00:34:32.988Z,General Question,v2,how does execvp work,,, +63c647e85f004bf4898cf7b1,2023-03-14T04:06:49.914Z,General Question,v2,how to use stat to get the last modified time of a file,,, +63c647e85f004bf4898cf7b1,2023-03-14T05:05:49.785Z,General Question,v2,what is struct timespec;,,, +63c647e85f004bf4898cf7b1,2023-03-14T15:03:24.946Z,General Question,v2,how do I use the tv_sec member of stat.st_mtim?,,, +63c647e85f004bf4898cf7b1,2023-03-27T21:13:20.431Z,Question from Code,v2,what is this code chunk doing,"// Set the IP and port of the server to connect to. +struct sockaddr_in server; +server.sin_family = AF_INET; +server.sin_port = htons(PORT); +if (inet_pton(AF_INET, ""127.0.0.1"", &server.sin_addr) < 1) { + perror(""client: inet_pton""); + close(sock_fd); + exit(1); +}",, +63c647e85f004bf4898cf7b1,2023-03-30T02:59:13.310Z,General Question,v2,explain asprintf and snprintf and their differences,,, +63c647e85f004bf4898cf7b1,2023-03-30T03:20:13.028Z,General Question,v2,what are the arguments to asprintf(),,, +63c647e85f004bf4898cf7b1,2023-04-01T15:28:44.329Z,General Question,v2,I want to dynamically alocate a string that keeps appending charcters to the buffer with asprintf(),,, +63c647e85f004bf4898cf7b1,2023-04-01T15:48:09.863Z,General Question,v2,is there a way to use asprintf and continually append to the same character buffer?,,, +63c647e85f004bf4898cf7b1,2023-04-03T00:44:15.702Z,Explain Code,v2,,"struct sockaddr_in *init_server_addr(int port) { + struct sockaddr_in *addr = malloc(sizeof(struct sockaddr_in)); + + // Allow sockets across machines. + addr->sin_family = AF_INET; + + // The port the process will listen on. + addr->sin_port = htons(port); + + // Clear this field; sin_zero is used for padding for the struct. + memset(&(addr->sin_zero), 0, 8); + + // Listen on all network interfaces. + addr->sin_addr.s_addr = INADDR_ANY; + + return addr; +}",, +63c647e85f004bf4898cf7b1,2023-04-03T00:46:17.586Z,Question from Code,v2,why is this in a function,"struct sockaddr_in *init_server_addr(int port) { + struct sockaddr_in *addr = malloc(sizeof(struct sockaddr_in)); + + // Allow sockets across machines. + addr->sin_family = AF_INET; + + // The port the process will listen on. + addr->sin_port = htons(port); + + // Clear this field; sin_zero is used for padding for the struct. + memset(&(addr->sin_zero), 0, 8); + + // Listen on all network interfaces. + addr->sin_addr.s_addr = INADDR_ANY; + + return addr; +}",, +63c647e85f004bf4898cf7b1,2023-04-03T01:15:33.841Z,Explain Code,v2,,"int accept_connection(int listenfd) { + struct sockaddr_in peer; + unsigned int peer_len = sizeof(peer); + peer.sin_family = AF_INET; + + fprintf(stderr, ""Waiting for a new connection...\n""); + int client_socket = accept(listenfd, (struct sockaddr *)&peer, &peer_len); + if (client_socket < 0) { + perror(""accept""); + return -1; + } else { + fprintf(stderr, ""New connection accepted from %s:%d\n"", inet_ntoa(peer.sin_addr), ntohs(peer.sin_port)); + return client_socket; + } +}",, +63c647e85f004bf4898cf7b1,2023-04-03T22:03:03.499Z,Explain Code,v2,,"while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) { + // only echo the line in batch mode since in interactive mode the user + // just typed the line + if (batch_mode) { + printf(""%s"", input); + } + + char *cmd_argv[INPUT_ARG_MAX_NUM]; + int cmd_argc = tokenize(input, cmd_argv); + + if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { + break; // can only reach if quit command was entered + } + + printf(""> ""); +}",, +63c647e85f004bf4898cf7b1,2023-04-03T22:15:15.298Z,Explain Code,v2,,"int main(int argc, char *argv[]) { + int batch_mode = (argc == 2); + char input[INPUT_BUFFER_SIZE]; + FILE *input_stream; + + // Create the heads of the empty data structure + User *user_list = NULL; + + if (batch_mode) { + input_stream = fopen(argv[1], ""r""); + if (input_stream == NULL) { + perror(""Error opening file""); + exit(1); + } + } else { + // interactive mode + input_stream = stdin; + } + + printf(""Welcome to FriendMe! (Local version)\nPlease type a command:\n> ""); + + while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) { + // only echo the line in batch mode since in interactive mode the user + // just typed the line + if (batch_mode) { + printf(""%s"", input); + } + + char *cmd_argv[INPUT_ARG_MAX_NUM]; + int cmd_argc = tokenize(input, cmd_argv); + + if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { + break; // can only reach if quit command was entered + } + + printf(""> ""); + } + + if (batch_mode) { + fclose(input_stream); + } + + return 0; +}",, +63c647e85f004bf4898cf7b1,2023-04-25T14:33:59.897Z,Explain Code,v2,,"upper=$1 +#we need the double quotes in case the filename has a space +filename=""$2"" + +for i in `seq 1 $upper` +do +#note that we need to double quotes here as well around filename + if /u/csc209h/winter/pub/shell-prog/floop $i ""$filename"" > /dev/null + then + echo $i and $filename are a floopy combination + fi +done",, +63c647e85f004bf4898cf7b1,2023-04-25T14:34:43.138Z,Explain Code,v2,,"upper=$1 +#we need the double quotes in case the filename has a space +filename=""$2"" + +for i in `seq 1 $upper` +do +#note that we need to double quotes here as well around filename + if /u/csc209h/winter/pub/shell-prog/floop $i ""$filename"" > /dev/null + then + echo $i and $filename are a floopy combination + fi +done",, +63c647e85f004bf4898cf7b6,2023-01-18T16:19:41.036Z,Explain Code,v1,,"int i; +while((scanf(%d), i) != EOF){ + +}",, +63c647e85f004bf4898cf7b6,2023-01-18T16:20:25.876Z,Explain Code,v1,,"int i; +while((scanf(%d), i) != EOF){ + printf(i) +}",, +63c647e85f004bf4898cf7b6,2023-02-04T23:08:25.396Z,Explain Code,v1,,NumberOfElements = sizeof(arr)/sizeof(arr[0]);,, +63c647e85f004bf4898cf7b6,2023-02-07T00:49:08.765Z,Help Fix Code,v1,,"int print_user(const User *user) { + // characters to get from file + char line[81]; + FILE *pfp; + // int error; + int valid_pfp = 1; // checks if pfp can be accessed + + // checks if user doesn't exist + if (user == NULL) { + return 1; + } + + // checking if error opening file + pfp = fopen(user->profile_pic, ""r""); + if (pfp == NULL){ + valid_pfp = 0; + } + // print everything, skip profile picture if error with opening + if (valid_pfp) { + // print profile picture + while (fgets(line, 81, pfp) != NULL){ + printf(""%s"", line); + } + // close file after use + fclose(pfp); + printf(""\n""); + } + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + // print out friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user->friends[i] != NULL){ + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts: \n""); + Post *curr = user->first_post; + if (curr != NULL){ + while (curr->next != NULL){ + printf(""From: %s\n"", curr->author); + printf(""Date: %s\n"", ctime(curr->date)); + printf(""%s\n"", curr->contents); + printf(""\n""); + printf(""===\n""); + printf(""\n""); + + curr = curr->next; + } + printf(""From: %s\n"", curr->author); + printf(""Date: %s\n"", ctime(curr->date)); + printf(""%s\n"", curr->contents); + } + printf(""------------------------------------------\n""); + + return 0; +}","print user information, fix set fault", +63c647e85f004bf4898cf7b6,2023-02-07T00:50:26.800Z,Help Fix Code,v1,,"int print_user(const User *user) { + // characters to get from file + char line[81]; + FILE *pfp; + // int error; + int valid_pfp = 1; // checks if pfp can be accessed + + // checks if user doesn't exist + if (user == NULL) { + return 1; + } + + // checking if error opening file + pfp = fopen(user->profile_pic, ""r""); + if (pfp == NULL){ + valid_pfp = 0; + } + // print everything, skip profile picture if error with opening + if (valid_pfp) { + // print profile picture + while (fgets(line, 81, pfp) != NULL){ + printf(""%s"", line); + } + // close file after use + fclose(pfp); + printf(""\n""); + } + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + // print out friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user->friends[i] != NULL){ + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts: \n""); + Post *curr = user->first_post; + if (curr != NULL){ + while (curr->next != NULL){ + printf(""From: %s\n"", curr->author); + printf(""Date: %s\n"", ctime(curr->date)); + printf(""%s\n"", curr->contents); + printf(""\n""); + printf(""===\n""); + printf(""\n""); + + curr = curr->next; + } + printf(""From: %s\n"", curr->author); + printf(""Date: %s\n"", ctime(curr->date)); + printf(""%s\n"", curr->contents); + } + printf(""------------------------------------------\n""); + + return 0; +}",segmentation fault, +63c647e85f004bf4898cf7b6,2023-03-12T00:05:02.534Z,Help Fix Code,v2,"recursively run the function +"," Dependency *curr_dep = rule_eval->dependencies; + while (curr_dep != NULL) { + // evaluate dependencies + if (curr_dep->rule->actions != NULL) { + if (stat(rule_eval->target, &target_time) == -1){ + run_make(curr_dep->rule->target, rules, pflag); + } + else { + // check times + printf(""FOUND FILE\n""); + if (stat(curr_dep->rule->target, &dep_time) == -1) { + perror(""stat""); + exit(1); + } + if (dep_time.st_mtim.tv_sec >= target_time.st_mtim.tv_sec) { + if (dep_time.st_mtim.tv_nsec > target_time.st_mtim.tv_nsec) { + run_make(curr_dep->rule->target, rules, pflag); + } + + } + } + } + curr_dep = curr_dep->next_dep; + } + // execute actions + pid_t ret = fork(); + if (ret < 0) { + perror(""fork""); + exit(1); + } + else if (ret == 0) { + char *cmd = (rule_eval->actions->args)[0]; + int error = execvp(cmd, rule_eval->actions->args); + if (error == -1) { + exit(1); + } + } + else { + int status; + wait(&status); + if (WIFEXITED(status)) { + exit(1); + } + return; + } +",, +63c647e85f004bf4898cf7b6,2023-03-23T15:56:52.944Z,Explain Code,v2,,"while (1) { + fd_set listen_fds = all_fds; + if (select(max_fd + 1, &listen_fds, NULL, NULL, NULL) == -1) { + perror(""server: select""); + exit(1); + } + + if (FD_ISSET(sock_fd, &listen_fds)) { + int client_fd = accept_connection(sock_fd, users); + if (client_fd > max_fd) { + max_fd = client_fd; + } + FD_SET(client_fd, &all_fds); + printf(""Accepted connection\n""); + } + + for (int index = 0; index < MAX_CONNECTIONS; index++) { + if (users[index].sock_fd > -1 && FD_ISSET(users[index].sock_fd, &listen_fds)) { + int client_closed = read_from(index, users); + if (client_closed > 0) { + FD_CLR(client_closed, &all_fds); + printf(""Client %d disconnected\n"", client_closed); + } else { + printf(""Echoing message from client %d\n"", users[index].sock_fd); + } + } + } +}",, +63c647e85f004bf4898cf7b6,2023-03-25T17:45:17.929Z,General Question,v2,when using asprintf multiple times with the same buffer is it neccessary to free memory multiple times or just once at the end,,, +63c647e95f004bf4898cf7bb,2023-01-27T00:14:51.146Z,Help Fix Code,v1,,"int **split_array(const int *s, int length) { + int num_evens; + int num_odds = length / 2; + + if (length % 2 == 0){ + num_evens = length / 2; + } else{ + num_evens = (length / 2) + 1; + } + + int *array1 = malloc(sizeof(int) * num_evens); + int *array2 = malloc(sizeof(int) * num_odds); + int count1 = 0; + int count2 = 0; + + for (int i = 0; i < length; i++){ + if (i % 2 == 0){ + array1[count1] = s[i]; + count1 = count1 + 1; + } else{ + array2[count2] = s[i]; + count2 = count2 + 1; + } + } + + //int **arrays_ptr = malloc(sizeof(int*) * 2); + + // Not stored in heap since itself is not, but it consists of 2 dynamically allocatted arrays of ints + int *arrays_ptr[2]; + + arrays_ptr[0] = array1; + arrays_ptr[1] = array2; + + return arrays_ptr; +}",return int **, +63c647e95f004bf4898cf7bb,2023-01-27T00:19:15.402Z,General Question,v1,how to declare a pointer to point to dynamically allocated arrays,,, +63c647e95f004bf4898cf7bb,2023-01-27T00:34:13.410Z,General Question,v1,how to access elements from a pointer that points to an array,,, +63c647e95f004bf4898cf7bb,2023-01-27T01:12:25.102Z,General Question,v1,how to store something on the heap,,, +63c647e95f004bf4898cf7bb,2023-01-27T01:12:44.540Z,General Question,v1,how to store an array on the heap,,, +63c647e95f004bf4898cf7bb,2023-01-27T16:32:39.122Z,Help Fix Code,v1,,"int **split_array(const int *s, int length) { + int num_evens; + int num_odds = length / 2; + + if (length % 2 == 0){ + num_evens = length / 2; + } else{ + num_evens = (length / 2) + 1; + } + printf(""OMG: %d\n"", s[0]); + printf(""OMG: %d\n"", s[1]); + printf(""OMG: %d\n"", s[2]); + int *array1 = malloc(sizeof(int) * num_evens); + int *array2 = malloc(sizeof(int) * num_odds); + int count1 = 0; + int count2 = 0; + + printf(""OMG2: %d\n"", s[0]); + printf(""OMG2: %d\n"", s[1]); + printf(""OMG2: %d\n"", s[2]); + printf(""evens: %d odds: %d\n"", num_evens, num_odds); + for (int i = 0; i < length; i++){ + printf(""HERRRRE: %d\n"", s[i]); + if (i % 2 == 0){ + array1[count1] = s[i]; + count1 = count1 + 1; + //printf(""HERRRRE: %d\n"", s[i]); + } else{ + array2[count2] = s[i]; + count2 = count2 + 1; + //printf(""OK: %d\n"", s[i]); + } + } + + int **arrays_ptr = malloc(sizeof(int *) * 2); + + // Not stored in heap since itself is not, but it consists of 2 dynamically allocatted arrays of ints + //int *arrays_ptr[2]; + + arrays_ptr[0] = array1; + arrays_ptr[1] = array2; + + return arrays_ptr; +}",why is s being changed?, +63c647e95f004bf4898cf7bb,2023-01-27T16:33:43.167Z,Help Fix Code,v1,,"int **split_array(const int *s, int length) { + int num_evens; + int num_odds = length / 2; + + if (length % 2 == 0){ + num_evens = length / 2; + } else{ + num_evens = (length / 2) + 1; + } + + int *array1 = malloc(sizeof(int) * num_evens); + int *array2 = malloc(sizeof(int) * num_odds); + int count1 = 0; + int count2 = 0; + + for (int i = 0; i < length; i++){ + if (i % 2 == 0){ + array1[count1] = s[i]; + count1 = count1 + 1; + } else{ + array2[count2] = s[i]; + count2 = count2 + 1; + } + } + + int **arrays_ptr = malloc(sizeof(int *) * 2); + + // Not stored in heap since itself is not, but it consists of 2 dynamically allocatted arrays of ints + //int *arrays_ptr[2]; + + arrays_ptr[0] = array1; + arrays_ptr[1] = array2; + + return arrays_ptr; +}",is s being changed?, +63c647e95f004bf4898cf7bb,2023-01-27T16:34:59.458Z,Help Fix Code,v1,,"int **split_array(const int *s, int length) { + int num_evens; + int num_odds = length / 2; + + if (length % 2 == 0){ + num_evens = length / 2; + } else{ + num_evens = (length / 2) + 1; + } + + int *array1 = malloc(sizeof(int) * num_evens); + int *array2 = malloc(sizeof(int) * num_odds); + int count1 = 0; + int count2 = 0; + + for (int i = 0; i < length; i++){ + if (i % 2 == 0){ + array1[count1] = s[i]; + count1 = count1 + 1; + } else{ + array2[count2] = s[i]; + count2 = count2 + 1; + } + } + + int **arrays_ptr = malloc(sizeof(int *) * 2); + + // Not stored in heap since itself is not, but it consists of 2 dynamically allocatted arrays of ints + //int *arrays_ptr[2]; + + arrays_ptr[0] = array1; + arrays_ptr[1] = array2; + + return arrays_ptr; +}",s does not change, +63c647e95f004bf4898cf7bb,2023-01-27T16:35:18.065Z,Question from Code,v1,does s change?,"int **split_array(const int *s, int length) { + int num_evens; + int num_odds = length / 2; + + if (length % 2 == 0){ + num_evens = length / 2; + } else{ + num_evens = (length / 2) + 1; + } + + int *array1 = malloc(sizeof(int) * num_evens); + int *array2 = malloc(sizeof(int) * num_odds); + int count1 = 0; + int count2 = 0; + + for (int i = 0; i < length; i++){ + if (i % 2 == 0){ + array1[count1] = s[i]; + count1 = count1 + 1; + } else{ + array2[count2] = s[i]; + count2 = count2 + 1; + } + } + + int **arrays_ptr = malloc(sizeof(int *) * 2); + + // Not stored in heap since itself is not, but it consists of 2 dynamically allocatted arrays of ints + //int *arrays_ptr[2]; + + arrays_ptr[0] = array1; + arrays_ptr[1] = array2; + + return arrays_ptr; +}",, +63c647e95f004bf4898cf7bb,2023-01-27T16:35:48.105Z,Question from Code,v1,what are the values of s in the beginning?,"int **split_array(const int *s, int length) { + int num_evens; + int num_odds = length / 2; + + if (length % 2 == 0){ + num_evens = length / 2; + } else{ + num_evens = (length / 2) + 1; + } + + int *array1 = malloc(sizeof(int) * num_evens); + int *array2 = malloc(sizeof(int) * num_odds); + int count1 = 0; + int count2 = 0; + + for (int i = 0; i < length; i++){ + if (i % 2 == 0){ + array1[count1] = s[i]; + count1 = count1 + 1; + } else{ + array2[count2] = s[i]; + count2 = count2 + 1; + } + } + + int **arrays_ptr = malloc(sizeof(int *) * 2); + + // Not stored in heap since itself is not, but it consists of 2 dynamically allocatted arrays of ints + //int *arrays_ptr[2]; + + arrays_ptr[0] = array1; + arrays_ptr[1] = array2; + + return arrays_ptr; +}",, +63c647e95f004bf4898cf7bb,2023-01-27T16:36:13.693Z,Question from Code,v1,should s change at the end of the code?,"int **split_array(const int *s, int length) { + int num_evens; + int num_odds = length / 2; + + if (length % 2 == 0){ + num_evens = length / 2; + } else{ + num_evens = (length / 2) + 1; + } + + int *array1 = malloc(sizeof(int) * num_evens); + int *array2 = malloc(sizeof(int) * num_odds); + int count1 = 0; + int count2 = 0; + + for (int i = 0; i < length; i++){ + if (i % 2 == 0){ + array1[count1] = s[i]; + count1 = count1 + 1; + } else{ + array2[count2] = s[i]; + count2 = count2 + 1; + } + } + + int **arrays_ptr = malloc(sizeof(int *) * 2); + + // Not stored in heap since itself is not, but it consists of 2 dynamically allocatted arrays of ints + //int *arrays_ptr[2]; + + arrays_ptr[0] = array1; + arrays_ptr[1] = array2; + + return arrays_ptr; +}",, +63c647e95f004bf4898cf7bb,2023-01-27T16:36:44.173Z,Question from Code,v1,"where is s, array1, and array2 stored?","int **split_array(const int *s, int length) { + int num_evens; + int num_odds = length / 2; + + if (length % 2 == 0){ + num_evens = length / 2; + } else{ + num_evens = (length / 2) + 1; + } + + int *array1 = malloc(sizeof(int) * num_evens); + int *array2 = malloc(sizeof(int) * num_odds); + int count1 = 0; + int count2 = 0; + + for (int i = 0; i < length; i++){ + if (i % 2 == 0){ + array1[count1] = s[i]; + count1 = count1 + 1; + } else{ + array2[count2] = s[i]; + count2 = count2 + 1; + } + } + + int **arrays_ptr = malloc(sizeof(int *) * 2); + + // Not stored in heap since itself is not, but it consists of 2 dynamically allocatted arrays of ints + //int *arrays_ptr[2]; + + arrays_ptr[0] = array1; + arrays_ptr[1] = array2; + + return arrays_ptr; +}",, +63c647e95f004bf4898cf7bb,2023-01-27T16:40:49.572Z,Help Write Code,v1,,,,write code to return a pointer to an array of two dynamically allocated arrays of ints. The first array contains the elements of the input array s that are at even indices. The second array contains the elements of the input array s that are at odd indices. +63c647e95f004bf4898cf7bb,2023-01-27T16:45:00.905Z,Explain Code,v1,,"int **split_array(const int *s, int length) { + int num_evens; + int num_odds = length / 2; + + if (length % 2 == 0){ + num_evens = length / 2; + } else{ + num_evens = (length / 2) + 1; + } + + int *array1 = malloc(sizeof(int) * num_evens); + int *array2 = malloc(sizeof(int) * num_odds); + int count1 = 0; + int count2 = 0; + + for (int i = 0; i < length; i++){ + if (i % 2 == 0){ + array1[count1] = s[i]; + count1 = count1 + 1; + } else{ + array2[count2] = s[i]; + count2 = count2 + 1; + } + } + + int **arrays_ptr = malloc(sizeof(int *) * 2); + + // Not stored in heap since itself is not, but it consists of 2 dynamically allocatted arrays of ints + //int *arrays_ptr[2]; + + arrays_ptr[0] = array1; + arrays_ptr[1] = array2; + + return arrays_ptr; +}",, +63c647e95f004bf4898cf7bb,2023-02-02T05:49:18.409Z,General Question,v1,How do you declare an array of chars?,,, +63c647e95f004bf4898cf7bb,2023-02-02T05:49:51.132Z,General Question,v1,how do you declare an array of chars of unknown size?,,, +63c647e95f004bf4898cf7bb,2023-02-02T06:12:04.315Z,General Question,v1,How to copy a string to an empty string,,, +63c647e95f004bf4898cf7bb,2023-02-02T06:12:54.022Z,General Question,v1,How to declare empty string as destination string to for strcpy?,,, +63c647e95f004bf4898cf7bb,2023-02-10T03:49:26.682Z,General Question,v1,How to allocate space for 5 `struct pixel *` values,,, +63c647e95f004bf4898cf7bb,2023-02-10T04:12:06.097Z,General Question,v1,How to store two dimensional array on heap,,, +63c647e95f004bf4898cf7bb,2023-02-10T04:12:41.563Z,General Question,v1,What is code to store two dimensional array on heap,,, +63c647e95f004bf4898cf7bb,2023-02-14T06:14:04.622Z,General Question,v1,How to declare a pointer to the current date?,,, +63c647e95f004bf4898cf7bb,2023-02-14T06:14:41.139Z,General Question,v1,How to get the current date?,,, +63c647e95f004bf4898cf7bb,2023-02-14T11:41:00.472Z,General Question,v1,"#define MAXNAME = 32; + +and then the declaration + +char name[MAXNAME]; + +What will this declaration line become after the program has passed +through the C pre-processor?",,, +63c647e95f004bf4898cf7bb,2023-02-14T11:42:04.108Z,Question from Code,v1,What will the declaration on line 2 become after the program has passed through the C pre-processor?,"#define MAXNAME = 32; +char name[MAXNAME];",, +63c647e95f004bf4898cf7bb,2023-02-14T11:47:12.002Z,Question from Code,v1,What is the output?,"#define SUPERVISOR(regular) regular + 5 +int main() { + int regular_pay = 20; + int hours_worked = 10; + printf(""pay is %d\n"", (hours_worked * SUPERVISOR(regular_pay))); + // rest omitted +}",, +63c647e95f004bf4898cf7bb,2023-02-14T11:48:23.535Z,Question from Code,v1,What is the output?,"#define SUPERVISOR(regular) regular + 5 +#define SUPERVISOR(regular) regular + 5 +int main() { + int regular_pay = 20; + int hours_worked = 10; + printf(""pay is %d\n"", (hours_worked * SUPERVISOR(regular_pay))); + // rest omitted +}",, +63c647e95f004bf4898cf7c0,2023-01-17T15:52:51.674Z,General Question,v1,how to declare strings,,, +63c647e95f004bf4898cf7c0,2023-02-09T04:57:13.776Z,Question from Code,v1,"why is it causing segmentation fault while I run it? +","struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + + struct pixel ** my_pixels = malloc(sizeof(struct pixel *) * height); + // initialize it to point for an entire row of pixel data + for(int i = 0; i < height; i++){ + my_pixels[i] = malloc(sizeof(struct pixel *) * width); + } + + // fill it with data + // + fseek(image, pixel_array_offset, SEEK_SET); + int val = fread(my_pixels, sizeof(struct pixel), height*width, image); + printf(""%d\n"", val); + return my_pixels; +}",, +63c647e95f004bf4898cf7c0,2023-02-09T04:58:20.096Z,General Question,v1,how to use fread?,,, +63c647e95f004bf4898cf7c0,2023-02-09T05:41:16.261Z,General Question,v1,using SEEK_CUR of fread,,, +63c647e95f004bf4898cf7c0,2023-02-26T20:30:21.303Z,General Question,v1,"How can I use the line of a file as the arguments to my c program in a shell script +",,, +63c647e95f004bf4898cf7c0,2023-02-26T21:29:28.680Z,Help Fix Code,v1,,"#include +#include + + +int main(){ + char * my_string; + char * second_one = ""hola mundo""; + strcpy(my_string, second_one); + + printf(""%s\n"", my_string); + + return 0; +}",copy the contents of second_one into my_string, +63c647e95f004bf4898cf7c0,2023-03-15T06:01:01.630Z,Help Fix Code,v2,if the file exists set target_time to the st_mtime which is a struct," struct stat sb; + struct stat sb_dep; + struct timespec * target_time = NULL; + struct timespec dependency_time; + int status; + + if(access(target, F_OK) == 0){ + + if (stat(target, &sb) == -1) { + perror(""stat""); + exit(1); + } + + printf(""line test\n""); + *(target_time) = sb.st_mtim; + }",, +63c647e95f004bf4898cf7c0,2023-03-15T06:03:35.314Z,Help Write Code,v2,,,,how to use struct timespec st_mtim; to get the most recent file between two files +63c647ea5f004bf4898cf7c5,2023-02-10T21:26:59.270Z,General Question,v1,"What is Fseek +",,, +63c647ea5f004bf4898cf7c5,2023-02-11T04:03:12.911Z,General Question,v1,"how to open a file +",,, +63c647ea5f004bf4898cf7c5,2023-02-11T04:04:23.389Z,General Question,v1,how to check if opening file caused error,,, +63c647ea5f004bf4898cf7c5,2023-02-11T04:05:47.378Z,General Question,v1,"read contents in a file +",,, +63c647ea5f004bf4898cf7c5,2023-02-13T23:45:17.132Z,Question from Code,v1,is there a segmentation fault here,"FILE *picture = fopen(user->profile_pic, ""r"");",, +63c647ea5f004bf4898cf7c5,2023-02-13T23:46:11.996Z,Question from Code,v1,what if user is a valid pointer to a struct with an attribute profile_pic,"FILE *picture = fopen(user->profile_pic, ""r"");",, +63c647ea5f004bf4898cf7c5,2023-02-13T23:47:07.263Z,Help Fix Code,v1,,"if (user->profile_pic != NULL){ + FILE *picture = fopen(user->profile_pic, ""r""); + if (picture != NULL){ + char line[32]; + while(fgets(line, sizeof(line), picture) != NULL){ + printf(""%s\n"", line); + } + } + fclose(picture); + }",Supposed to read the contents in the file picture and print it , +63c647ea5f004bf4898cf7c5,2023-02-13T23:49:51.804Z,General Question,v1,"==41828== Syscall param openat(filename) points to uninitialised byte(s) +what does this mean",,, +63c647ea5f004bf4898cf7c5,2023-02-13T23:51:02.012Z,Question from Code,v1,Fix this error Syscall param openat(filename) points to uninitialised byte(s),"if (user->profile_pic != NULL){ + FILE *picture = fopen(user->profile_pic, ""r""); + if (picture != NULL){ + char line[32]; + while(fgets(line, sizeof(line), picture) != NULL){ + printf(""%s\n"", line); + } + fclose(picture); + } + }",, +63c647ea5f004bf4898cf7c5,2023-02-14T00:02:24.147Z,Question from Code,v1,Does it matter if I read the file in chunks of 32 bits,"if (user->profile_pic != NULL){ + FILE *picture = fopen(user->profile_pic, ""r""); + if (picture != NULL){ + char line[32]; + while(fgets(line, sizeof(line), picture) != NULL){ + printf(""%s\n"", line); + } + fclose(picture); + } + }",, +63c647ea5f004bf4898cf7c5,2023-02-14T00:03:04.530Z,Question from Code,v1,Should I change the fgets to 1 bit ,"if (user->profile_pic != NULL){ + FILE *picture = fopen(user->profile_pic, ""r""); + if (picture != NULL){ + char line[32]; + while(fgets(line, sizeof(line), picture) != NULL){ + printf(""%s\n"", line); + } + fclose(picture); + } + }",, +63c647ea5f004bf4898cf7c5,2023-02-14T00:04:59.180Z,General Question,v1,"If I do not know the number of characters in a file, but need to use fgets. What should the second parameter be",,, +63c647ea5f004bf4898cf7c5,2023-02-14T04:38:25.097Z,Question from Code,v1,I dont see why I am getting a Invalid read of size 8 error on line 8,"User *curr = (User*) target->friends; + int friends = 0; + while (curr != NULL){ + // Boolean = 1 if they are friends + if (strcmp(author->name, target->name) == 0/){ + friends = 1; + } + curr = curr->next; + }",, +63c647ea5f004bf4898cf7c5,2023-02-14T04:54:04.779Z,Help Fix Code,v1,," new_post->next = target->first_post; + target->first_post = new_post;",I am supposed to put new_post in the beginning of the target->first_post and I need help with malloc, +63c647ea5f004bf4898cf7c5,2023-02-14T05:01:10.771Z,Help Fix Code,v1,,"// Put post at front + if (target->first_post == NULL){ + target->first_post = malloc(sizeof(Post)); + target->first_post = new_post; + } + else{ + Post *curr_first = (Post*) malloc(sizeof(Post)); + curr_first = target->first_post; + target->first_post = new_post; + target->first_post->next = curr_first; + }",This is causing the new post to be added countless times , +63c647ea5f004bf4898cf7c5,2023-02-14T05:04:04.957Z,Help Fix Code,v1,,"// Put post at front + if (target->first_post == NULL){ + target->first_post = new_post; + } + else{ + Post *curr_first = target->first_post; + target->first_post = new_post; + target->first_post->next = curr_first; + }",I want it to add the new post at the beginning of the target->first_post, +63c647ea5f004bf4898cf7c5,2023-02-14T05:09:54.000Z,Explain Code,v1,,"// Assign space for new post + Post *new_post = (Post*) malloc(sizeof(Post)); + + // Initialize poster + strncpy(new_post->author, author->name, MAX_NAME - 1); + new_post->author[MAX_NAME-1] = '\0'; + + // Initialize content + new_post->contents = malloc(sizeof(contents)) ; + long size_content = strlen(contents); + strncpy(new_post->contents, contents, size_content - 1); + new_post->contents[size_content - 1] = '\0'; + + // Initialize date + time_t *current_time = malloc(sizeof(time_t)); + time(current_time); + new_post->date = current_time; + + // Put post at front + if (target->first_post == NULL){ + target->first_post = new_post; + } + else{ + Post *curr_first = target->first_post; + new_post -> next = curr_first; + target->first_post = new_post; + } + + return 0;",, +63c647ea5f004bf4898cf7c5,2023-02-14T05:12:12.011Z,Help Fix Code,v1,,"// Assign space for new post + Post *new_post = (Post*) malloc(sizeof(Post)); + + // Initialize poster + strncpy(new_post->author, author->name, MAX_NAME - 1); + new_post->author[MAX_NAME-1] = '\0'; + + // Initialize content + new_post->contents = malloc(sizeof(contents)) ; + long size_content = strlen(contents); + strncpy(new_post->contents, contents, size_content - 1); + new_post->contents[size_content - 1] = '\0'; + + // Initialize date + time_t *current_time = malloc(sizeof(time_t)); + time(current_time); + new_post->date = current_time; + + // Put post at front + if (target->first_post == NULL){ + target->first_post = new_post; + } + else{ + Post *curr_first = target->first_post; + new_post -> next = curr_first; + target->first_post = new_post; + } + + return 0;","The code above creates a new post, initializes its author, contents, and date, and then puts the post at the front of the list of posts for the target user.", +63c647ea5f004bf4898cf7c5,2023-02-14T05:14:30.751Z,General Question,v1,how do I print a time_t data,,, +63c647ea5f004bf4898cf7c5,2023-02-14T05:15:04.578Z,General Question,v1,print from a time_t *,,, +63c647ea5f004bf4898cf7c5,2023-02-14T05:19:51.479Z,Explain Code,v1,,"/printf(""Posts:\n""); + Post *curr = user->first_post; + while (curr != NULL){ + printf(""From: %s\n"", curr->author); + printf(""Date: %s\n"", ctime(curr->date)); + printf(""\n""); + printf(""%s\n"", curr->contents); + if (curr->next != NULL){ + printf(""\n""); + printf(""===""); + printf(""\n""); + } + }",, +63c647ea5f004bf4898cf7c5,2023-02-14T06:02:31.007Z,Question from Code,v1,Why am I getting a curr may not be initialized error,"// Print post list + printf(""Posts:\n""); + Post *curr = user->first_post; + while (curr != NULL){ + printf(""From: %s\n"", curr->author); + printf(""Date: %s\n"", ctime(curr->date)); + printf(""%s\n"", curr->contents); + if (curr->next != NULL){ + printf(""\n""); + printf(""===""); + printf(""\n""); + } + curr = curr->next; + } + printf(""------------------------------------------\n""); +",, +63c647ea5f004bf4898cf7c5,2023-02-14T06:03:24.469Z,General Question,v1,How do I use calloc,,, +63c647ea5f004bf4898cf7c5,2023-02-14T07:13:07.158Z,Question from Code,v1,Can you spot the conditional move depends on uninitialized error here,"if (user->first_post){ + curr = user->first_post; + } + while (curr != NULL){ + if (curr != user->first_post){ + printf(""\n""); + printf(""===\n""); + printf(""\n""); + } + printf(""From: %s\n"", curr->author); + printf(""Date: %s\n"", ctime(curr->date)); + printf(""%s\n"", curr->contents); + curr = curr->next; + } + printf(""------------------------------------------\n"");",, +63c647ea5f004bf4898cf7c5,2023-02-14T07:14:25.851Z,Question from Code,v1,Are there any issues," + while (curr != NULL){ + if (curr != user->first_post){ + printf(""\n""); + printf(""===\n""); + printf(""\n""); + } + printf(""From: %s\n"", curr->author); + printf(""Date: %s\n"", ctime(curr->date)); + printf(""%s\n"", curr->contents); + curr = curr->next; + } + printf(""------------------------------------------\n"");",, +63c647ea5f004bf4898cf7c5,2023-02-14T22:58:42.720Z,Explain Code,v1,,"// Go through all the user's posts and free memory allocated for it + Post *posts = NULL; + Post *prev_post = to_delete->first_post; + while (prev_post != NULL){ + posts = prev_post; + free(posts->contents); + free(posts->date); + prev_post = prev_post->next; + free(posts); + } + + // Free user itself's memory + free(to_delete); + return 0;",, +63c647ea5f004bf4898cf7c5,2023-02-14T22:59:37.987Z,Help Fix Code,v1,,"// Go through all the user's posts and free memory allocated for it + Post *posts = NULL; + Post *prev_post = to_delete->first_post; + while (prev_post != NULL){ + posts = prev_post; + free(posts->contents); + free(posts->date); + prev_post = prev_post->next; + free(posts); + } + + // Free user itself's memory + free(to_delete); + return 0;",I have a never terminating loop here, +63c647ea5f004bf4898cf7c5,2023-02-17T19:36:59.903Z,Help Fix Code,v1,," // Allocate Null space to all the characters in dest + memset(dest, '\0', capacity); + // Go through the src string and copy characters + int i = 0; + for (i; i < (capacity-1); i++){ + if (src[i] != '\0'){ + dest[i] = src[i]; + } + } + return dest;",It is supposed to act like strncopy, +63c647ea5f004bf4898cf7c5,2023-04-06T09:08:24.923Z,Help Fix Code,v2,The code is not outputting Welcome.\r\n even though its a new user," + // New user + if (found == 0){ + // Welcome the client + char *message = ""Welcome.\r\n""; + write(new_user->fd, message, strlen(message)); + + // Note down the name + strncpy(new_user->name, buf, strlen(new_user->name)); + new_user->name[31] = '\0'; + } + } +",, +63c647ec5f004bf4898cf7d4,2023-01-28T05:08:17.395Z,General Question,v1,how do i ignore a line of input using scanf,,, +63c647ec5f004bf4898cf7d4,2023-02-09T22:31:37.938Z,Question from Code,v1,what is the complexity of this code,"AVL_Node* search(AVL_Node* node, int key) { //*** + if (node == NULL) { + return NULL; + } + if (key == node->key) { + return node; + } + if (key < node->key) { + return search(node->left, key); + } + return search(node->right, key); +}",, +63c647ec5f004bf4898cf7d4,2023-02-09T22:32:10.783Z,General Question,v1,pseudocode for avl insert?,,, +63c647ec5f004bf4898cf7d4,2023-03-10T19:34:41.001Z,General Question,v2,what are processes in C,,, +63c647ec5f004bf4898cf7d4,2023-03-10T21:44:20.724Z,General Question,v2,"what does dup2 do +",,, +63c647ec5f004bf4898cf7d4,2023-03-10T21:44:51.210Z,General Question,v2,what is a file descriptor,,, +63c647ec5f004bf4898cf7d4,2023-03-10T22:02:32.926Z,Question from Code,v2,explain what this program does,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 +#define MAX_PASSWORD 8 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + if (fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if (fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + int fd[2]; + if (pipe(fd) == -1) { + perror(""pipe""); + exit(1); + } + + pid_t pid = fork(); + if (pid == -1) { + perror(""fork""); + exit(1); + } + + if (pid == 0) { + close(fd[0]); + dup2(fd[1], STDOUT_FILENO); + close(fd[1]); + execlp(""./validate"", ""./validate"", NULL); + perror(""execlp""); + exit(1); + } else { + close(fd[1]); + dup2(fd[0], STDIN_FILENO); + close(fd[0]); + wait(NULL); + char buf[MAXLINE]; + if (fgets(buf, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if (strcmp(buf, SUCCESS) == 0) { + printf(""Password verified""); + } else if (strcmp(buf, INVALID) == 0) { + printf(""Invalid password""); + } else if (strcmp(buf, NO_USER) == 0) { + printf(""No such user""); + } else { + printf(""Unknown error""); + } + } + + return 0; +} +",, +63c647ec5f004bf4898cf7d4,2023-03-10T22:04:32.087Z,Question from Code,v2,explain the purpose of the buf variable,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 +#define MAX_PASSWORD 8 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + if (fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if (fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + int fd[2]; + if (pipe(fd) == -1) { + perror(""pipe""); + exit(1); + } + + pid_t pid = fork(); + if (pid == -1) { + perror(""fork""); + exit(1); + } + + if (pid == 0) { + close(fd[0]); + dup2(fd[1], STDOUT_FILENO); + close(fd[1]); + execlp(""./validate"", ""./validate"", NULL); + perror(""execlp""); + exit(1); + } else { + close(fd[1]); + dup2(fd[0], STDIN_FILENO); + close(fd[0]); + wait(NULL); + char buf[MAXLINE]; + if (fgets(buf, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if (strcmp(buf, SUCCESS) == 0) { + printf(""Password verified""); + } else if (strcmp(buf, INVALID) == 0) { + printf(""Invalid password""); + } else if (strcmp(buf, NO_USER) == 0) { + printf(""No such user""); + } else { + printf(""Unknown error""); + } + } + + return 0; +} +",, +63c647ec5f004bf4898cf7d4,2023-03-10T22:21:13.267Z,Question from Code,v2,what's the purpose of dup2 in this code,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 +#define MAX_PASSWORD 8 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + if (fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if (fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + int fd[2]; + if (pipe(fd) == -1) { + perror(""pipe""); + exit(1); + } + + pid_t pid = fork(); + if (pid == -1) { + perror(""fork""); + exit(1); + } + + if (pid == 0) { + close(fd[0]); + dup2(fd[1], STDOUT_FILENO); + close(fd[1]); + execlp(""./validate"", ""./validate"", NULL); + perror(""execlp""); + exit(1); + } else { + close(fd[1]); + dup2(fd[0], STDIN_FILENO); + close(fd[0]); + wait(NULL); + char buf[MAXLINE]; + if (fgets(buf, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if (strcmp(buf, SUCCESS) == 0) { + printf(""Password verified""); + } else if (strcmp(buf, INVALID) == 0) { + printf(""Invalid password""); + } else if (strcmp(buf, NO_USER) == 0) { + printf(""No such user""); + } else { + printf(""Unknown error""); + } + } + + return 0; +} +",, +63c647ec5f004bf4898cf7d4,2023-03-10T22:42:51.303Z,Question from Code,v2,why does this give invalid file descriptor error when run,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 +#define MAX_PASSWORD 10 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + if (fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if (fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + int fd[2]; + if (pipe(fd) == -1) { + perror(""pipe""); + exit(1); + } + + pid_t pid = fork(); + if (pid == -1) { + perror(""fork""); + exit(1); + } + + if (pid == 0) { + close(fd[0]); + dup2(fd[1], STDOUT_FILENO); + close(fd[1]); + execlp(""./validate"", ""./validate"", NULL); + perror(""execlp""); + exit(1); + } else { + close(fd[1]); + close(fd[0]); + int status; + wait(&status); + close(fd[0]); + if (WIFEXITED(status)) { + int exit_status = WEXITSTATUS(status); + if (exit_status == 0) { + printf(""%s"", SUCCESS); + } else if (exit_status == 1) { + printf(""%s"", INVALID); + } else if (exit_status == 2) { + printf(""%s"", NO_USER); + } else { + printf(""Unknown error\n""); + } + } else { + printf(""Unknown error\n""); + } + } + + return 0; +} +",, +63c647ec5f004bf4898cf7d4,2023-03-13T23:49:21.842Z,Help Fix Code,v2,lldb is telling me line 3 is causing a segfault. how do i fix this,"target = malloc(sizeof(char) * (strchr(makefile_line, ':') - makefile_line)); +strncpy(target, makefile_line, strchr(makefile_line, ':') - makefile_line - 1); +target[strchr(makefile_line, ':') - makefile_line - 1] = '\0';",, +63c647ec5f004bf4898cf7d4,2023-03-23T02:48:39.895Z,Question from Code,v2,what algorithm is this,"Records *records = initRecords(graph, startVertex); +while (!isEmpty(records->heap)) { + Edge *min = extractMin(records->heap); + records->finished[min->toVertex] = true; + if (min->fromVertex != min->toVertex) { + addTreeEdge(records, records->numTreeEdges, min->fromVertex, min->toVertex, min->weight); + } + AdjList *adj = graph->adjLists[min->toVertex]; + for (int i = 0; i < adj->numNeighbors; i++) { + if (!records->finished[adj->neighbors[i].vertex]) { + int weight = adj->neighbors[i].weight; + if (weight < records->heap->array[adj->neighbors[i].vertex]->weight) { + decreasePriority(records->heap, adj->neighbors[i].vertex, weight); + records->predecessors[adj->neighbors[i].vertex] = min->toVertex; + } + } + } +} +return records->tree;",, +63c647ec5f004bf4898cf7d4,2023-03-23T02:56:17.134Z,General Question,v2,how do you implement dijkstra's algorithm,,, +63c647ec5f004bf4898cf7d4,2023-03-23T02:59:48.550Z,Question from Code,v2,what algorithm is this,"Records *records = initRecords(graph, startVertex); +for (int i = 0; i < graph->numVertices; i++) { + Edge *min = extractMin(records->heap); + records->finished[min->toVertex] = true; + if (min->fromVertex != min->toVertex) { + addTreeEdge(records, records->numTreeEdges, min->fromVertex, min->toVertex, min->weight); + } + AdjList *adj = graph->adjLists[min->toVertex]; + for (int i = 0; i < adj->numNeighbors; i++) { + if (!records->finished[adj->neighbors[i].vertex]) { + int weight = adj->neighbors[i].weight + min->weight; + if (weight < records->heap->array[adj->neighbors[i].vertex]->weight) { + decreasePriority(records->heap, adj->neighbors[i].vertex, weight); + records->predecessors[adj->neighbors[i].vertex] = min->toVertex; + } + } + } +}",, +63c647ec5f004bf4898cf7d4,2023-03-23T03:25:06.269Z,Question from Code,v2,consider these two functions,"Records *initRecords(Graph *graph, int startVertex) { + Records *records = malloc(sizeof(Records)); + records->numVertices = graph->numVertices; + records->heap = initHeap(graph, startVertex); + records->finished = malloc(sizeof(bool) * graph->numVertices); + records->predecessors = malloc(sizeof(int) * graph->numVertices); + records->tree = malloc(sizeof(Edge) * (graph->numVertices - 1)); + records->numTreeEdges = 0; + for (int i = 0; i < graph->numVertices; i++) { + records->finished[i] = false; + records->predecessors[i] = NOTHING; + } + return records; +} + +MinHeap *initHeap(Graph *graph, int startVertex) { + MinHeap *heap = newMinHeap(graph->numVertices); + for (int i = 0; i < graph->numVertices; i++) { + if (i == startVertex) { + insert(heap, newEdge(startVertex, startVertex, 0)); + } else { + insert(heap, newEdge(startVertex, i, INT_MAX)); + } + } + return heap; +}",, +63c647ec5f004bf4898cf7d4,2023-03-23T03:40:20.502Z,Question from Code,v2,what is getPaths returning here,"Edge *getShortestPaths(Graph *graph, int startVertex) { + Records *records = initRecords(graph, startVertex); + while (!isEmpty(records->heap)) { + Edge *min = extractMin(records->heap); + records->finished[min->toVertex] = true; + if (min->fromVertex != min->toVertex) { + addTreeEdge(records, records->numTreeEdges, min->fromVertex, min->toVertex, min->weight); + } + AdjList *adj = graph->adjLists[min->toVertex]; + for (int i = 0; i < adj->numNeighbors; i++) { + if (!records->finished[adj->neighbors[i].vertex]) { + int weight = adj->neighbors[i].weight + min->weight; + if (weight < records->heap->array[adj->neighbors[i].vertex]->weight) { + decreasePriority(records->heap, adj->neighbors[i].vertex, weight); + records->heap->array[adj->neighbors[i].vertex]->weight = weight; + records->predecessors[adj->neighbors[i].vertex] = min->toVertex; + } + } + } + } +} + +AdjList *getPaths(Edge *distTree, int startVertex, int endVertex) { + Edge *path = getShortestPaths(distTree, startVertex); + return makePath(path, endVertex, startVertex); +}",, +63c647ec5f004bf4898cf7d4,2023-03-23T03:41:22.909Z,Question from Code,v2,what algorithm(s) are being displayed here,"Edge *getShortestPaths(Graph *graph, int startVertex) { + Records *records = initRecords(graph, startVertex); + while (!isEmpty(records->heap)) { + Edge *min = extractMin(records->heap); + records->finished[min->toVertex] = true; + if (min->fromVertex != min->toVertex) { + addTreeEdge(records, records->numTreeEdges, min->fromVertex, min->toVertex, min->weight); + } + AdjList *adj = graph->adjLists[min->toVertex]; + for (int i = 0; i < adj->numNeighbors; i++) { + if (!records->finished[adj->neighbors[i].vertex]) { + int weight = adj->neighbors[i].weight + min->weight; + if (weight < records->heap->array[adj->neighbors[i].vertex]->weight) { + decreasePriority(records->heap, adj->neighbors[i].vertex, weight); + records->heap->array[adj->neighbors[i].vertex]->weight = weight; + records->predecessors[adj->neighbors[i].vertex] = min->toVertex; + } + } + } + } + return records->tree; +} + +AdjList *getPaths(Edge *distTree, int startVertex, int endVertex) { + Edge *path = getShortestPaths(distTree, startVertex); + return makePath(path, endVertex, startVertex); +}",, +63c647ec5f004bf4898cf7d4,2023-03-23T03:59:05.137Z,Explain Code,v2,,"Records *records = initRecords(graph, startVertex); +while (!isEmpty(records->heap)) { + Edge *min = extractMin(records->heap); + records->finished[min->toVertex] = true; + if (min->fromVertex != min->toVertex) { + addTreeEdge(records, records->numTreeEdges, min->fromVertex, min->toVertex, min->weight); + } + AdjList *adj = graph->adjLists[min->toVertex]; + for (int i = 0; i < adj->numNeighbors; i++) { + if (!records->finished[adj->neighbors[i].vertex]) { + int weight = adj->neighbors[i].weight + min->weight; + if (weight < records->heap->array[adj->neighbors[i].vertex]->weight) { + decreasePriority(records->heap, adj->neighbors[i].vertex, weight); + records->predecessors[adj->neighbors[i].vertex] = min->toVertex; + } + } + } +} +return makePath(records->tree, endVertex, startVertex);",, +63c647ec5f004bf4898cf7d4,2023-03-23T04:26:00.412Z,Question from Code,v2,what algorithm does this use,"Records *records = initRecords(graph, startVertex); +while (!isEmpty(records->heap)) { + HeapNode *min = extractMin(records->heap); + records->finished[min->edge.toVertex] = true; + if (min->edge.fromVertex != min->edge.toVertex) { + addTreeEdge(records, records->numTreeEdges, min->edge.fromVertex, min->edge.toVertex, min->edge.weight); + } + AdjList *neighbors = graph->vertices[min->edge.toVertex]; + for (int i = 0; i < neighbors->numNeighbors; i++) { + if (!records->finished[neighbors->neighbors[i].id]) { + int newWeight = neighbors->neighbors[i].weight; + int oldWeight = getWeight(records->heap, neighbors->neighbors[i].id); + if (newWeight < oldWeight) { + decreasePriority(records->heap, neighbors->neighbors[i].id, newWeight); + records->predecessors[neighbors->neighbors[i].id] = min->edge.toVertex; + } + } + } +} +return records->tree; +}",, +63c647ec5f004bf4898cf7d4,2023-03-23T05:11:58.221Z,Question from Code,v2,what algorithm does this use,"Records *records = initRecords(graph, startVertex); +while (!isEmpty(records->head)) { + HeapNode min = extractMin(records->heap); + records->finished[min.id] = true; + addTreeEdge(records, records->numTreeEdges, min.id, records->predecessors[min.id], min.priority); + AdjList *neighbors = graph->vertices[min.id]; + while (neighbors != NULL) { + int maybe_shorter = min.priority + neighbors->edge->weight; + int index = records->heap->indexMap[neighbors->edge->toVertex]; + if (records->finished[neighbors->edge->toVertex] == false && maybe_shorter < records->heap->arr[index].priority) { + decreasePriority(records->heap, neighbors->edge->toVertex, maybe_shorter) records->predecessors[neighbors->edge->toVertex] = min.id; + } + neighbors = neighbors->next; + } +}",, +63c647ec5f004bf4898cf7d4,2023-03-23T05:40:57.079Z,Question from Code,v2,what algorithm does this use,"Records *records = initRecords(graph, startVertex); +while (!isEmpty(records->heap)) { + Heapnode min = extractMin(records->heap); + records->finished[min.id] = true; + addTreeEdge(records, records->numTreeEdges, min.id, records->predecessors[min.id], min.priority); + AdjList *neighbors = graph->vertices[min.id]; + while (neighbors != NULL) { + if (records->finished[neighbors->edge->toVertex] == false) { + if (neighbors->edge->weight < records->heap->arr[records->heap->indexMap[neighbors->edge->toVertex]].priority) { + decreasePriority(records->heap, neighbors->edge->toVertex, neighbors->edge->weight); + records->predecessors[neighbors->edge->toVertex] = min.id; + } + } + } +} +return records->tree;",, +63c647ec5f004bf4898cf7d4,2023-03-23T17:43:24.346Z,Question from Code,v2,what algorithm is being implemented here,"Records *asdfg123 = initRecords(graph, startVertex); +while (!isEmpty(asdfg123->heap)) { + HeapNode min = extractMin(asdfg123->heap); + asdfg123->finished[min.id] = true; + addTreeEdge(asdfg123, asdfg123->numTreeEdges, min.id, asdfg123->predecessors[min.id], min.priority); + AdjList *neighbours = graph->vertices[min.id]; + while (neighbours != NULL) { + if (asdfg123->finished[neighbours->edge->toVertex] == false) { + int index = asdfg123->heap->arr[neighbours->edge->toVertex] if (neighbours->edge->weight < asdfg123->heap->arr[index].priority) { + decreasePriority(asdfg123->heap, neighbours->edge->toVertex, neighbours->edge->weight); + asdfg123->predecessors[neighbours->edge->toVertex] = min.id; + } + } + neighbours = neighbours->next; + } +} +return asdfg123->tree;",, +63c647ec5f004bf4898cf7d4,2023-03-23T17:44:14.425Z,Question from Code,v2,what algorithm is being used here,"Records *records = initRecords(graph, startVertex); +while (!isEmpty(records->heap)) { + HeapNode min = extractMin(records->heap); + records->finished[min.id] = true; + addTreeEdge(records, records->numTreeEdges, min.id, records->predecessors[min.id], min.priority); + AdjList *neighbors = graph->vertices[min.id]; + while (neighbors != NULL) { + int maybe_shorter = min.priority + neighbors->edge->weight; + int index = records->heap->indexMap[neighbors->edge->toVertex]; + if (records->finished[neighbors->edge->toVertex] == false && maybe_shorter < records->heap->arr[index].priority) { + decreasePriority(records->heap, neighbors->edge->toVertex, maybe_shorter) records->predecessors[neighbors->edge->toVertex] = min.id; + } + neighbors = neighbors->next; + } +} +return records->tree;",, +63c647ec5f004bf4898cf7d4,2023-03-23T17:57:02.682Z,Question from Code,v2,which algorithm is (1) using and which algorithm is (2) using?,"(1) Records *asdfg123 = initRecords(graph, startVertex); +while (!isEmpty(asdfg123->heap)) { + HeapNode min = extractMin(asdfg123->heap); + asdfg123->finished[min.id] = true; + addTreeEdge(asdfg123, asdfg123->numTreeEdges, min.id, asdfg123->predecessors[min.id], min.priority); + AdjList *neighbours = graph->vertices[min.id]; + while (neighbours != NULL) { + if (asdfg123->finished[neighbours->edge->toVertex] == false) { + int index = asdfg123->heap->arr[neighbours->edge->toVertex] if (neighbours->edge->weight < asdfg123->heap->arr[index].priority) { + decreasePriority(asdfg123->heap, neighbours->edge->toVertex, neighbours->edge->weight); + asdfg123->predecessors[neighbours->edge->toVertex] = min.id; + } + } + neighbours = neighbours->next; + } +} +return asdfg123->tree; + +(2) Records *records = initRecords(graph, startVertex); +while (!isEmpty(records->heap)) { + HeapNode min = extractMin(records->heap); + records->finished[min.id] = true; + addTreeEdge(records, records->numTreeEdges, min.id, records->predecessors[min.id], min.priority); + AdjList *neighbors = graph->vertices[min.id]; + while (neighbors != NULL) { + int maybe_shorter = min.priority + neighbors->edge->weight; + int index = records->heap->indexMap[neighbors->edge->toVertex]; + if (records->finished[neighbors->edge->toVertex] == false && maybe_shorter < records->heap->arr[index].priority) { + decreasePriority(records->heap, neighbors->edge->toVertex, maybe_shorter) records->predecessors[neighbors->edge->toVertex] = min.id; + } + neighbors = neighbors->next; + } +} +return records->tree;",, +63c647ec5f004bf4898cf7d4,2023-03-23T17:58:25.132Z,Question from Code,v2,what algorithm is this,"Records *records = initRecords(graph, startVertex); +while (!isEmpty(records->heap)) { + HeapNode min = extractMin(records->heap); + records->finished[min.id] = true; + addTreeEdge(records, records->numTreeEdges, min.id, records->predecessors[min.id], min.priority); + AdjList *neighbors = graph->vertices[min.id]; + while (neighbors != NULL) { + int maybe_shorter = min.priority + neighbors->edge->weight; + int index = records->heap->indexMap[neighbors->edge->toVertex]; + if (records->finished[neighbors->edge->toVertex] == false && maybe_shorter < records->heap->arr[index].priority) { + decreasePriority(records->heap, neighbors->edge->toVertex, maybe_shorter) records->predecessors[neighbors->edge->toVertex] = min.id; + } + neighbors = neighbors->next; + } +} +return records->tree;",, +63c647ec5f004bf4898cf7d4,2023-03-23T17:59:01.018Z,Question from Code,v2,what algorithm is this,"Records *asdfg123 = initRecords(graph, startVertex); +while (!isEmpty(asdfg123->heap)) { + HeapNode min = extractMin(asdfg123->heap); + asdfg123->finished[min.id] = true; + addTreeEdge(asdfg123, asdfg123->numTreeEdges, min.id, asdfg123->predecessors[min.id], min.priority); + AdjList *neighbours = graph->vertices[min.id]; + while (neighbours != NULL) { + if (asdfg123->finished[neighbours->edge->toVertex] == false) { + int index = asdfg123->heap->arr[neighbours->edge->toVertex] if (neighbours->edge->weight < asdfg123->heap->arr[index].priority) { + decreasePriority(asdfg123->heap, neighbours->edge->toVertex, neighbours->edge->weight); + asdfg123->predecessors[neighbours->edge->toVertex] = min.id; + } + } + neighbours = neighbours->next; + } +} +return asdfg123->tree;",, +63c647ec5f004bf4898cf7d4,2023-04-08T22:22:35.660Z,Explain Code,v2,,"import web_resource as web_r import os import threading import youtube_api import util import os import google.auth import datetime from googleapiclient.discovery import build from youtube_transcript_api import YouTubeTranscriptApi + + class YouTubeAPI(web_r.web_results) : + + def result_from_youtube(self, related_questions:dict)->dict: """" + "" + Given questions as a list of related questions to a user input question, return youtube video captions by searching the sub - questions on youtube + + input:a dictionary with a user input question as key and sub - questions as value return :a dictionary with following format: {""result"" : [{""question"" : ""What is the best way to learn Python?"", ""question_list"" : [{""name"" : ""What is the best way to learn Python?"", ""url"" : ""https: "" content "": "" I have been learning Python for a few months no "" +}, {... }, ...] }] } + + question:the user input question(key) question_list:name:one of the questions in the input url:the youtube video in answer to that question content:video caption.Should be after removing stop words """" + "" + + ##loop though questions and call youtube api on each result = self.search_youtube_transcript("""") + +#collect result and remove stop words + result = util.remove_stopwords(result) + + result_list =[] for key, value in related_questions.items() :question_list =[] for question in value:question_list.append({""name"" :question, ""url"" : """", ""content"" :result[question] }) result_list.append({""question"" :key, ""question_list"" :question_list }) return {""result"" :result_list } + + pass",, +63c647ec5f004bf4898cf7d9,2023-01-17T22:50:52.779Z,Explain Code,v1,," +int main(int argc, char **argv) +{ + if (argc == 2){ + int c; + enum { START, SAW_NL, SAW_TWO_NL } state = START; + while ((c = getchar()) != EOF) { + if (c == 10){ + if (state == START){ + putchar(c); + state = SAW_NL; + }else if (state == SAW_NL){ + putchar(c); + state = SAW_TWO_NL; + } + }else{ + state = START; + putchar(c); + } + }return (0); + }else{ + fprintf(stderr, ""usage: squ format error\n""); + return (1); + } +}",, +63c647ee5f004bf4898cf7ed,2023-01-17T19:22:57.773Z,Help Fix Code,v1,,"#include + +int main(){ + + char phone[11]; + int d; + int scanf_return; + + scanf(""%s"", phone); // getting first input + + int err = 0; // variable for return value of program + + do{ + scanf_return = scanf(""%d"", &d); + + + if (d == -1){ + printf(""%s\n"", phone); + } + + else if (d<-1 || d>9){ + printf(""ERROR\n""); + err = 1; + } + + else if (d>=0 && d<10){ + printf(""%c\n"", phone[d]); + } + + } while(scanf_return != EOF); + + return err; + + + +}","Your task is to write a small C program called phone.c that uses scanf to read two values from standard input. The first is a 10 character string and the second is an integer. The program takes no command-line arguments. (You will write this program from scratch, so remember to add, commit, and push.) + +If the integer is -1, the program prints the full string to stdout. If the integer i is between 0 and 9, the program prints only the corresponding character (i.e., at index i) from the string to stdout. In both of these cases the program returns 0. + +If the integer is less than -1 or greater than 9, the program prints the message ""ERROR"" to stdout and returns 1.", +63c647ee5f004bf4898cf7ed,2023-01-17T19:24:27.506Z,Help Fix Code,v1,,"#include + +int main(){ + + char phone[11]; + int d; + int scanf_return; + + scanf(""%s"", phone); // getting first input + + int err = 0; // variable for return value of program + + do{ + scanf_return = scanf(""%d"", &d); + + + if (d == -1){ + printf(""%s\n"", phone); + } + + else if (d<-1 || d>9){ + printf(""ERROR\n""); + err = 1; + } + + else if (d>=0 && d<10){ + printf(""%c\n"", phone[d]); + } + + } while(scanf_return != EOF); + + return err; + + + +}","Your task is to write a C program called phone_loop.c. This program will again read from standard input using scanf and take no command-line arguments. Similar to phone.c, this program reads a 10-character string as the first input value but then it repeatedly reads integers until standard input is closed. (Hint: Use a while loop that checks the return value of scanf to accomplish this. Check the man page for scanf to determine the return value when the input stream is closed.) + +After each integer the output produced is as before: + +if the integer is -1, the full string is printed +if the integer is between 0 and 9, the individual character at that position is printed +if the integer is less than -1 or greater than 9, the message ""ERROR"" is printed (to stdout) +In each case the printing is followed by a newline character. + +When the program finishes running, main returns with a 0 return code if there were no errors and with a 1 return code otherwise. +", +63c647ee5f004bf4898cf7ed,2023-01-21T00:05:31.760Z,Help Fix Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +int check_permissions(char *, char *); + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + char Sdud[31]; + int Idud; + + scanf(""%s %d"", Sdud, &Idud); // Getting rid of the first line of stdin + + int size; + int count = 0; + + if (argc == 2){ + + int scanf_return; + + do{ + printf(""hi""); + scanf_return = scanf(""%s %d %s %s %d %d %s %s %s"", + Sdud, &Idud, Sdud, Sdud, &size, &Idud, Sdud, Sdud, Sdud); + + + // Stopping the loop from running if stdin is closed + if (size > strtol(argv[2], NULL, 10)){ + count++; + } + } + while(scanf_return != EOF); + } + printf(""%d"", count); + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + + return 0; +} + +int check_permissions(char *actual, char *required){ + int flag = 0; + for (int i=0; i<9; i++){ + if (required[i] != '-' && required[i] != actual[i]){ + flag = 1; + } + } + return flag; + +} +",, +63c647ee5f004bf4898cf7ed,2023-01-21T00:06:21.115Z,Question from Code,v1,"why am I getting a segmentation fault +","#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +int check_permissions(char *, char *); + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + char Sdud[31]; + int Idud; + + scanf(""%s %d"", Sdud, &Idud); // Getting rid of the first line of stdin + + int size; + int count = 0; + + if (argc == 2){ + + int scanf_return; + + do{ + printf(""hi""); + scanf_return = scanf(""%s %d %s %s %d %d %s %s %s"", + Sdud, &Idud, Sdud, Sdud, &size, &Idud, Sdud, Sdud, Sdud); + + + // Stopping the loop from running if stdin is closed + if (size > strtol(argv[2], NULL, 10)){ + count++; + } + } + while(scanf_return != EOF); + } + printf(""%d"", count); + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + + return 0; +} + +int check_permissions(char *actual, char *required){ + int flag = 0; + for (int i=0; i<9; i++){ + if (required[i] != '-' && required[i] != actual[i]){ + flag = 1; + } + } + return flag; + +} +",, +63c647f05f004bf4898cf7f7,2023-01-28T19:59:59.714Z,Help Fix Code,v1,,"#include +#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +int check_permissions(const char *input_permissions, const char *required_permissions) { + + for (int i = 0; i < 9; i++) { + if (required_permissions[i] == '-') { + continue; + } + if (required_permissions[i] != input_permissions[i]) { + return 0; + } + } + + return 1; + +} + + +int main(int argc, char **argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // Process the command line arguments + long size = strtol(argv[1], NULL, 10); + char *required_permissions = argv[2]; + + // Initialize the variables used in the loop + int count = 0; + char input_permissions[10]; + char input_size_str[10]; + long input_size; +// int i = 0; + int permissions; + + // Skip the first line of ls -l + scanf(""%*s %*d""); + + // Scan each value from ls -l + while (scanf(""%s %*d %*s %*s %s %*s %*d %*s %*s"", input_permissions, input_size_str) != EOF) { + // Check if there are two command line arguments + // Therefore checking permissions and size + input_size = strtol(input_size_str, NULL, 10); + if (argc == 3) { + // Check if it is the first item of a line +// if (i % 9 == 0) { + if (check_permissions(&input_permissions[1], required_permissions) == 1) { + permissions = 1; + } else { + permissions = 0; + } +// } + //Check if it is the fifth item of a line +// if (i % 9 == 4) { + // Check if the permissions pass + if (permissions == 1) { + if (input_size > size) { + count++; + } + permissions = 0; + } +// } + } else { + // Only checking if the size is larger than the input + // Check if it is the fifth item of a line +// if (i % 9 == 4) { +// printf(""%s\n"", input_permissions); + if (input_size > size) { + count++; + } +// } + } +// i++; + } + + printf(""%d\n"", count); + return 0; +} +","take input from ls -l terminal command as well as one or two command line arguments and return the number of files greater in size +", +63c647f05f004bf4898cf7f7,2023-02-08T02:49:10.136Z,General Question,v1,"How do you printf a date of time_t +",,, +63c647f05f004bf4898cf7f7,2023-02-08T04:01:53.964Z,Question from Code,v1,The compiler is saying that this is invalid and that a long type is necessary,new_post->date = time(NULL);,, +63c647f05f004bf4898cf7f7,2023-02-08T04:02:41.050Z,Question from Code,v1,new_post->date is of type time_t but this line of code is being flagged as an error and invalid,new_post->date = time(NULL);,, +63c647f05f004bf4898cf7f7,2023-02-08T05:13:49.450Z,Question from Code,v1,Why is there a segmenation fault,"int make_post(const User *author, User *target, char *contents) { + + // Check if either User pointer is NULL + if (author == NULL || target == NULL) { + return 2; + } + + // Check if users exist but are not friends + int found = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + found = 1; + break; + } + } + if (found == 0) { + return 1; + } + + // Make a new post + Post *new_post = malloc(sizeof(Post)); + + strncpy(new_post->author, author->name, MAX_NAME); + new_post->contents = contents; + time(new_post->date); + printf(""%s"", ctime(new_post->date)); + + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; +}",, +63c647f05f004bf4898cf7f7,2023-02-08T05:21:16.705Z,Question from Code,v1,What is causing a segmentation fault,"int make_post(const User *author, User *target, char *contents) { + + // Check if either User pointer is NULL + if (author == NULL || target == NULL) { + return 2; + } + + // Check if users exist but are not friends + int found = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + found = 1; + break; + } + } + if (found == 0) { + return 1; + } + + // Make a new post + Post *new_post = malloc(sizeof(Post)); + + strncpy(new_post->author, author->name, MAX_NAME); + + new_post->contents = malloc(strlen(contents)); + strncpy(new_post->contents, contents, strlen(contents)); + free(contents); + + time(new_post->date); + printf(""%s"", ctime(new_post->date)); + + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; +}",, +63c647f05f004bf4898cf7f7,2023-02-08T05:22:11.042Z,Question from Code,v1,what is line 26 doing,"int make_post(const User *author, User *target, char *contents) { + + // Check if either User pointer is NULL + if (author == NULL || target == NULL) { + return 2; + } + + // Check if users exist but are not friends + int found = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + found = 1; + break; + } + } + if (found == 0) { + return 1; + } + + // Make a new post + Post *new_post = malloc(sizeof(Post)); + + strncpy(new_post->author, author->name, MAX_NAME); + + new_post->contents = malloc(strlen(contents)); + strncpy(new_post->contents, contents, strlen(contents)); + free(contents); + + time(new_post->date); + printf(""%s"", ctime(new_post->date)); + + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; +}",, +63c647f05f004bf4898cf7f7,2023-02-10T21:13:21.494Z,General Question,v1,"What are the values of an initialized array +",,, +63c647f15f004bf4898cf801,2023-01-20T18:40:07.189Z,Explain Code,v1,,"scanf +",, +63c647f15f004bf4898cf801,2023-01-27T18:45:23.039Z,Explain Code,v1,,"int *arr[2]; +",, +63c647f15f004bf4898cf801,2023-02-09T22:36:18.742Z,Explain Code,v1,,String.join(),, +63c647f15f004bf4898cf801,2023-02-09T22:38:23.931Z,Explain Code,v1,,String.concat(),, +63c647f15f004bf4898cf801,2023-02-09T22:47:06.926Z,General Question,v1,How to make string copy,,, +63c647f15f004bf4898cf801,2023-02-10T21:29:11.135Z,Explain Code,v1,,"strtol +",, +63c647f15f004bf4898cf801,2023-02-10T22:05:39.304Z,General Question,v1,what is struct,,, +63c647f15f004bf4898cf801,2023-02-10T22:06:05.271Z,General Question,v1,waht is stored in struct,,, +63c647f15f004bf4898cf806,2023-02-04T01:02:00.484Z,General Question,v1,"what is the syntax for copying a string +",,, +63c647f15f004bf4898cf806,2023-02-04T01:07:55.634Z,Help Fix Code,v1,,"#include +#include +#include + +/* + Write a function named truncate() that takes a string s and a + non-negative integer n. If s has more than n characters (not including the + null terminator), the function should truncate s at n characters and + return the number of characters that were removed. If s has n or + fewer characters, s is unchanged and the function returns 0. For example, + if s is the string ""function"" and n is 3, then truncate() changes s to + the string ""fun"" and returns 5. +*/ + +int truncate(char *s, int n){ + char rand[n]; + int charcount = 0; + strncpy(rand, s, sizeof(rand)); +} + +int main(int argc, char **argv) { + if (argc != 3) { + fprintf(stderr, ""Usage: truncate number string\n""); + return 1; + } + int amt = strtol(argv[1], NULL, 10); + + char *target = argv[2]; + int soln_val = truncate(target, amt); + printf(""%d %s\n"", soln_val, target); + + return 0; +}","truncate input at n +", +63c647f15f004bf4898cf806,2023-02-04T01:12:56.601Z,Help Fix Code,v1,,"#include +#include +#include + +/* + Write a function named truncate() that takes a string s and a + non-negative integer n. If s has more than n characters (not including the + null terminator), the function should truncate s at n characters and + return the number of characters that were removed. If s has n or + fewer characters, s is unchanged and the function returns 0. For example, + if s is the string ""function"" and n is 3, then truncate() changes s to + the string ""fun"" and returns 5. +*/ + +int truncate(char *s, int n){ + char rand[n]; + int charcount = 0; + strncpy(rand, s, sizeof(rand)); + rand[sizeof(rand)]= '\0'; +} + +int main(int argc, char **argv) { + if (argc != 3) { + fprintf(stderr, ""Usage: truncate number string\n""); + return 1; + } + int amt = strtol(argv[1], NULL, 10); + + char *target = argv[2]; + int soln_val = truncate(target, amt); + printf(""%d %s\n"", soln_val, target); + + return 0; +}",truncate string at n, +63c647f15f004bf4898cf806,2023-02-04T01:14:36.581Z,Help Fix Code,v1,,"#include +#include +#include + +/* + Write a function named truncate() that takes a string s and a + non-negative integer n. If s has more than n characters (not including the + null terminator), the function should truncate s at n characters and + return the number of characters that were removed. If s has n or + fewer characters, s is unchanged and the function returns 0. For example, + if s is the string ""function"" and n is 3, then truncate() changes s to + the string ""fun"" and returns 5. +*/ + +int truncate(char *s, int n){ + char rand[n]; + int charcount = 0; + strncpy(rand, s, sizeof(rand)); + rand[sizeof(rand)]= '\0'; +} + +int main(int argc, char **argv) { + if (argc != 3) { + fprintf(stderr, ""Usage: truncate number string\n""); + return 1; + } + int amt = strtol(argv[1], NULL, 10); + + char *target = argv[2]; + int soln_val = truncate(target, amt); + printf(""%d %s\n"", soln_val, target); + + return 0; +}",truncate s at n, +63c647f15f004bf4898cf806,2023-02-14T03:21:32.516Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + + + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + memset(new_user->friends, 0, MAX_FRIENDS * sizeof(new_user -> friends[0])); + + if(user_ptr_add == NULL){ + *user_ptr_add = new_user; + } + else if(strlen(name)>MAX_NAME){ + return 2; + } + User *curr = *user_ptr_add; + while(curr != NULL){ + if(strcmp(curr->name, name) ==0 ){ + return 1; + } + curr = curr->next; + } + curr->next = new_user; + return 0; + +}",the linked list tail insertion is not working, +63c647f15f004bf4898cf806,2023-02-15T22:50:52.680Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + User *curr = head; + User *user1 = NULL; + User *user2 = NULL; + //traverses through list, assigns curr to user 1 and user2 + if(strcmp(name1, name2)==0){ + return 3; + } + else{ + while(curr != NULL){ + if(strcmp(curr ->name, name1)==0 ){ + user1 = curr; + } + if(strcmp(curr ->name, name2)==0 ){ + user2 = curr; + } + curr= curr->next; + } + if(user1 == NULL || user2 ==NULL){ + return 4; + } + else{ + for(int x =0;x<=MAX_FRIENDS;x++){ + if(strcmp(user1->friends[x]->name, name2)==0){ + return 1; + } + } + for(int i =0; i<=MAX_FRIENDS; i++){ + if(user1->friends[i]==0){ + user1->friends[i]= user2; + break; + } + else{ + return 2; + } + } + for(int j= 0;j<=MAX_FRIENDS;j++){ + if(user2->friends[j]==0){ + user2->friends[j]= user1; + break; + } + else{ + return 2; + } + } + return 0; + } + } +}",replace first 0 in friends of User name1 with User name2 and replace first 0 in friends of User name2 with User name1, +63c647f15f004bf4898cf806,2023-02-15T22:53:45.704Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + User *curr = head; + User *user1 = NULL; + User *user2 = NULL; + //traverses through list, assigns curr to user 1 and user2 + if(strcmp(name1, name2)==0){ + return 3; + } + else{ + while(curr != NULL){ + if(strcmp(curr ->name, name1)==0 ){ + user1 = curr; + } + if(strcmp(curr ->name, name2)==0 ){ + user2 = curr; + } + curr= curr->next; + } + if(user1 == NULL || user2 ==NULL){ + return 4; + } + else{ + for(int x =0;xfriends[x]->name, name2)==0){ + return 1; + } + } + for(int i =0; ifriends[i]==0){ + user1->friends[i]= user2; + break; + } + else{ + return 2; + } + } + for(int j= 0;jfriends[j]==0){ + user2->friends[j]= user1; + break; + } + else{ + return 2; + } + } + return 0; + } + } +}",replace first 0 in friend array of User name1 with User name2 and replace the first 0 in friend array of User name 2 with User name1, +63c647f15f004bf4898cf806,2023-02-15T23:34:08.328Z,Help Fix Code,v1,,"memset(new_user->friends, 0, MAX_FRIENDS * sizeof(new_user -> friends[0]));",make all elements in new_user ->friends a 0, +63c647f15f004bf4898cf806,2023-02-15T23:37:58.571Z,General Question,v1,how to cast const User struct to User struct,,, +63c647f15f004bf4898cf806,2023-02-15T23:42:58.905Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + User *curr = head; + User *user1 = NULL; + User *user2 = NULL; + int found_zero = 1; + //traverses through list, assigns curr to user 1 and user2 + if(strcmp(name1, name2)==0){ + return 3; + } + else{ + while(curr != NULL){ + if(strcmp(curr ->name, name1)==0 ){ + user1 = curr; + } + if(strcmp(curr ->name, name2)==0 ){ + user2 = curr; + } + curr= curr->next; + } + if(user1 == NULL || user2 ==NULL){ + return 4; + } + else{ + for(int x =0;xfriends[x]->name, name2)==0){ + return 1; + } + } + for(int i =0; ifriends[i]==0){ + user1->friends[i]= user2; + break; + } + else{ + found_zero = 0; + } + } + for(int j= 0;jfriends[j]==0){ + user2->friends[j]= user1; + break; + } + else{ + found_zero = 0; + } + } + if(found_zero ==0){ + return 2; + } + else{ + return 0; + } + } + } +}",replace first 0 in friends array of struct User name1 with struct User name2 and replace first 0 in friends array of struct User name2 with struct User name1, +63c647f15f004bf4898cf806,2023-02-15T23:50:33.454Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + User *curr = head; + User *user1 = NULL; + User *user2 = NULL; + int *found_zero = 1; + //traverses through list, assigns curr to user 1 and user2 + if(strcmp(name1, name2)==0){ + return 3; + } + else{ + while(curr != NULL){ + if(strcmp(curr ->name, name1)==0 ){ + user1 = curr; + } + if(strcmp(curr ->name, name2)==0 ){ + user2 = curr; + } + curr= curr->next; + } + if(user1 == NULL || user2 ==NULL){ + return 4; + } + else{ + for(int x =0;xfriends[x]->name, name2)==0){ + return 1; + } + } + for(int i =0; ifriends[i]==0){ + user1->friends[i]= user2; + break; + } + else{ + found_zero = 0; + } + } + for(int j= 0;jfriends[j]==0){ + user2->friends[j]= user1; + break; + } + else{ + found_zero = 0; + } + } + if(found_zero ==0){ + return 2; + } + else{ + return 0; + } + } + } +}",replace first 0 in friends array of struct User name1 with struct User name2 and replace first 0 in friends array of struct User name2 with struct User name1, +63c647f15f004bf4898cf806,2023-02-15T23:51:48.418Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + User *curr = head; + User *user1 = NULL; + User *user2 = NULL; + int found_zero = 1; + //traverses through list, assigns curr to user 1 and user2 + if(strcmp(name1, name2)==0){ + return 3; + } + else{ + while(curr != NULL){ + if(strcmp(curr ->name, name1)==0 ){ + user1 = curr; + } + if(strcmp(curr ->name, name2)==0 ){ + user2 = curr; + } + curr= curr->next; + } + if(user1 == NULL || user2 ==NULL){ + return 4; + } + else{ + for(int x =0;xfriends[x]->name, name2)==0){ + return 1; + } + } + for(int i =0; ifriends[i]==0){ + user1->friends[i]= user2; + break; + } + else{ + found_zero = 0; + } + } + for(int j= 0;jfriends[j]==0){ + user2->friends[j]= user1; + break; + } + else{ + found_zero = 0; + } + } + if(found_zero ==0){ + return 2; + } + else{ + return 0; + } + } + } +}",replace first 0 in friends array of struct User name1 with struct User name2 and replace first 0 in friends array of struct User name2 with struct User name1, +63c647f15f004bf4898cf806,2023-02-15T23:57:44.599Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + User *curr = head; + User *user1 = NULL; + User *user2 = NULL; + int found_zero = 1; + //traverses through list, assigns curr to user 1 and user2 + if(strcmp(name1, name2)==0){ + return 3; + } + else{ + while(curr != NULL){ + if(strcmp(curr ->name, name1)==0 ){ + user1 = curr; + } + if(strcmp(curr ->name, name2)==0 ){ + user2 = curr; + } + curr= curr->next; + } + if(user1 == NULL || user2 ==NULL){ + return 4; + } + else{ + for(int x =0;xfriends[x]->name, name2)==0){ + return 1; + } + } + for(int i =0; ifriends[i]==0){ + user1->friends[i]= user2; + break; + } + else{ + found_zero = 0; + } + } + for(int j= 0;jfriends[j]==0){ + user2->friends[j]= user1; + break; + } + else{ + found_zero = 0; + } + } + if(found_zero ==0){ + return 2; + } + else{ + return 0; + } + } + } +}","Make two users friends with each other. This is symmetric. a pointer to each user must be stored in the 'friends' array of the other. +New friends must be added in the first 0 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. + ", +63c647f15f004bf4898cf806,2023-02-16T00:34:32.298Z,General Question,v1,"when do you use malloc +",,, +63c647f15f004bf4898cf806,2023-02-16T00:34:55.794Z,General Question,v1,"when should you use malloc for a struct +",,, +63c647f15f004bf4898cf806,2023-02-17T17:35:47.447Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + //check if users are friends + if(author != NULL && target != NULL){ + for(int i=0; i>0; i++){ + //users are friends + if((strcmp(author->friends[i]->name, target->name)==0) && author->name[i]!=0){ + Post *new_post = malloc(sizeof(Post)); + new_post->contents = contents; + strncpy(new_post->author, author->name, sizeof(new_post->author)); + time_t *date = malloc(sizeof (*date)); + time(&date); + new_post->date= date; + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; + } + //users aren't friends + else{ + return 1; + } + } + } + else{ + return 2; + } +}",insert new post at head of User target and assign current date to the new post, +63c647f15f004bf4898cf806,2023-02-17T17:37:36.892Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + //check if users are friends + if(author != NULL && target != NULL){ + for(int i=0; i>0; i++){ + //users are friends + if((strcmp(author->friends[i]->name, target->name)==0) && author->name[i]!=0){ + Post *new_post = malloc(sizeof(Post)); + new_post->contents = contents; + strncpy(new_post->author, author->name, sizeof(new_post->author)); + time_t *date = malloc(sizeof (*date)); + time(&date); + new_post->date= date; + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; + } + //users aren't friends + else{ + return 1; + } + } + } + else{ + return 2; + } +}",assign current date and time to User target's date, +63c647f15f004bf4898cf806,2023-02-17T18:04:20.449Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + //check if users are friends + if(author != NULL && target != NULL){ + for(int i=0; ifriends[i]!=0){ + if((strcmp(author->friends[i]->name, target->name)==0)){ + Post *new_post = malloc(sizeof(Post)); + new_post->contents = contents; + strncpy(new_post->author, author->name, sizeof(new_post->author)); + time_t *date = malloc(sizeof (*date)); + time(date); + new_post->date= date; + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; + } + } + //users aren't friends + else{ + return 1; + } + + } + } + else{ + return 2; + } +}",return 0 if success, +63c647f15f004bf4898cf806,2023-02-17T18:45:23.384Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + User *curr = *user_ptr_del; + User *prev = NULL; + + // If head node itself holds the key to be deleted + if (curr != NULL && curr->name == name) { + *user_ptr_del = curr->next; // Changed head + free(curr); // free old head + } + + // Search for the key to be deleted, keep track of the + // previous node as we need to change 'prev->next' + while (curr != NULL && strcmp(curr->name, name)!=0) { + prev = curr; + curr = curr->next; + } + + // If key was not present in linked list + if (curr == NULL) + return 1; + + // Unlink the node from linked list + prev->next = curr->next; + free(curr); // Free memory + return 0; +}",delete node with given name, +63c647f15f004bf4898cf806,2023-02-17T18:48:04.070Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + User *curr = *user_ptr_del; + User *prev = NULL; + + // If head node itself holds the key to be deleted + if (curr != NULL && strcmp(curr->name, name)==0) { + *user_ptr_del = curr->next; // Changed head + free(curr); // free old head + } + + // Search for the key to be deleted, keep track of the + // previous node as we need to change 'prev->next' + while (curr != NULL && strcmp(curr->name, name)!=0) { + prev = curr; + curr = curr->next; + } + + // If key was not present in linked list + if (curr == NULL) + return 1; + + // Unlink the node from linked list + prev->next = curr->next; + free(curr); // Free memory + return 0; +}",traverse linked list and delete node with given name, +63c647f25f004bf4898cf810,2023-01-20T21:38:25.695Z,Help Fix Code,v1,,"#include + +int main(){ + + char phone_num[11]; + scanf(""%s"", phone_num); + + int a, contains_error = 0; + + while(1){ + scanf(""%d"", &a); + + if(a == -1){ + + printf(""%s\n"", phone_num); + + }else if(a >= 0 && a <= 9){ + + printf(""%c\n"", phone_num[a]); + + }else{ + + printf(""ERROR\n""); + contains_error = 1; + + } + } + + if(contains_error){ + return 1; + }else{ + return 0; + } +}",, +63c647f25f004bf4898cf810,2023-01-20T21:53:10.925Z,General Question,v1,how do i detect the end of stdin,,, +63c647f25f004bf4898cf810,2023-01-20T21:55:42.179Z,General Question,v1,how to i detect the end of stdin IN C CODE,,, +63c647f25f004bf4898cf810,2023-01-20T21:56:48.934Z,General Question,v1,how to detect the end of stdin using scanf in C code,,, +63c647f35f004bf4898cf815,2023-01-24T22:41:57.741Z,General Question,v1,"How to separate a string by \n? +",,, +63c647f35f004bf4898cf81a,2023-01-27T01:21:32.335Z,Help Fix Code,v1,,"int **split_array(const int *s, int length) { + int **arr = (int **)malloc(sizeof(int)*length); + arr[0] = (int *) malloc(sizeof(int)* length/2); + arr[1] = (int *) malloc(sizeof(int)* length/2); + for(int i = 0; i < length; i++){ + if(i%2 == 0){ + arr[0][i] = s[i]; + } + if(i%2 == 1){ + arr[1][i] = s[i]; + } + } + return arr; +}",How can I assign arr[0][i] to s[i], +63c647f35f004bf4898cf81a,2023-01-27T01:25:53.632Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + int *arr = malloc(sizeof(int)*size); + for(int i = 0; i< size; i++){ + arr[i] = strtol(*strs,NULL,10); + } + return arr; +}",how can I assign each arr[i] for each part of a long type variable., +63c647f35f004bf4898cf81a,2023-01-27T01:27:40.306Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + int *arr = malloc(sizeof(int)*size); + for(int i = 0; i< size; i++){ + arr[i] = strtol(strs[i],NULL,10); + } + return arr; +}",how can I assign each arr[i] from the strtol, +63c647f35f004bf4898cf81a,2023-01-27T01:29:11.680Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + long i = strtol(strs,NULL,10); + i[0] = 0; +}",How can I assign i[0], +63c647f35f004bf4898cf81a,2023-01-27T01:30:55.376Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + long *arr = malloc(sizeof(long)*size); + arr = strtol(strs,NULL,10); + arr[0] = 0; + +}",How can I assign i[0], +63c647f35f004bf4898cf81a,2023-01-27T01:32:27.565Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + int *arr = malloc(sizeof(int)*size); + arr = strtol(strs,NULL,10); + arr[0] = 0; + +}",How can I assign i[0], +63c647f35f004bf4898cf81a,2023-01-27T01:33:22.120Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + int *arr = malloc(sizeof(int)*size); + arr[0] = strtol(strs[0],NULL,10); + +}",How can I assign i[0], +63c647f35f004bf4898cf81a,2023-01-27T01:34:02.305Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + int *arr = malloc(sizeof(int)*size); + for(int i = 0; i< size; i++){ + arr[i] = strtol(strs[i],NULL,10); + } + return arr; +}",How can I assign i[0], +63c647f35f004bf4898cf81a,2023-01-27T01:34:26.170Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + int *arr = malloc(sizeof(int)*size); + for(int i = 0; i< size; i++){ + arr[i] = strtol(strs[i],NULL,10); + } + return arr; +}",How can I assign arr[0], +63c647f35f004bf4898cf81a,2023-01-27T01:34:57.398Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + int *arr = malloc(sizeof(int)*size); + for(int i = 0; i< size; i++){ + arr[i] = strtol(strs,NULL,10); + } + return arr; +}",How can I assign arr[0], +63c647f35f004bf4898cf81a,2023-01-27T01:35:28.665Z,Explain Code,v1,,"int *build_array(char **strs, int size) { + int *arr = malloc(sizeof(int)*size); + for(int i = 0; i< size; i++){ + arr[i] = strtol(strs[i],NULL,10); + } + return arr; +}",, +63c647f35f004bf4898cf81a,2023-01-28T20:24:08.228Z,Help Fix Code,v1,,"int check_permissions(char *file[9], char *required[9]) { + for (int i = 0; i < 9; i++) { + if (required[i] != ""-"") { + if (file[i] != required[i]) { + return 1; + } + } + } + return 0; +}","How to compare required[i] with ""-""", +63c647f35f004bf4898cf81a,2023-01-28T20:27:14.725Z,Help Fix Code,v1,,"int check_permissions(char *file, char *required) { + for (int i = 0; i < 9; i++) { + if (required[i] != ""-"") { + if (file[i] != required[i]) { + return 1; + } + } + } + return 0; +}",how can I get value of index i in the required, +63c647f35f004bf4898cf81a,2023-01-28T21:15:26.395Z,General Question,v1,how can i extract permissions value from ls -l,,, +63c647f35f004bf4898cf81a,2023-01-28T21:16:39.326Z,General Question,v1,how can I assign permissions form ls -l to a variable,,, +63c647f35f004bf4898cf81a,2023-01-28T22:01:55.560Z,Question from Code,v1,Can access file's index which is the output from ls-l by file[i][4],"int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + char *file; + scanf(""%s"", file) + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + int count = 0; + for(int i = 0; i < sizeof(file); i++){ + if(file[i][4] >= argc){ + if(argv){ + if(check_permissions(file[i][0],*argv) == 0){ + count += 1; + } + } + else{ + count += 1; + } + } + } + printf(""%d\n"",count); + return 0; +}",, +63c647f35f004bf4898cf81a,2023-01-28T22:02:42.805Z,General Question,v1,Can assign the output from ls-l to a string?,,, +63c647f35f004bf4898cf81a,2023-01-28T22:03:32.300Z,General Question,v1,"How to user scanf for the output of ls-l +",,, +63c647f35f004bf4898cf81a,2023-01-28T22:04:26.405Z,General Question,v1,"The type of output of ls-l +",,, +63c647f35f004bf4898cf81a,2023-01-29T01:20:37.815Z,General Question,v1,How to know how many command-line arguments are being given?,,, +63c647f35f004bf4898cf81a,2023-01-29T01:23:49.104Z,General Question,v1,how can I use `argc` ?,,, +63c647f35f004bf4898cf81a,2023-02-01T21:07:01.667Z,General Question,v1,How to modify a string literal?,,, +63c647f35f004bf4898cf81a,2023-02-01T21:12:26.408Z,General Question,v1,How to reassign a string literal?,,, +63c647f35f004bf4898cf81a,2023-02-01T21:16:55.709Z,Help Fix Code,v1,,"int truncate(char *s, int n){ + char *s_copy = malloc(sizeof(char) * strlen(s) + 1); + strcpy(s_copy, s); + int l = (int)strlen(s); + int res = 0; + if(strlen(s) > n){ + s_copy[n] = '\0'; + s = s_copy; + res = l - n; + } + return res; +}",how can I change the s outside of the function?, +63c647f35f004bf4898cf81a,2023-02-15T19:44:20.302Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + int res = 0; +/*Check whether the user name already exists in the list + */ + while (curr->next){ + if (curr->name == name){ + res = 1; + } + curr = curr->next; + } + if (res == 1){ + return res; + } + +/*Check whether the given name cannot fit in the 'name' array + */ + if (strlen(name) > MAX_NAME - 1){ + return 2; + } + +/*Create the user with name + */ + User u1; + strcpy(u1.name,name); + curr->next = &u1; + return 0; +} ",how to solve the segmentation fault., +63c647f35f004bf4898cf81a,2023-02-15T19:46:58.434Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + /*Check whether the user name already exists in the list + */ + User *curr1 = *user_ptr_add; + User curr = *curr1; + int res = 0; + while (curr.next){ + if (strcmp(curr.name, name) == 0){ + res = 1; + } + curr = *curr.next; + } + if (res == 1){ + return res; + } + + +/*Check whether the given name cannot fit in the 'name' array + */ + if (strlen(name) > MAX_NAME - 1){ + return 2; + } + +/*Create the user with name + */ + User *u1ptr = malloc(sizeof(User)); + User u1 = *u1ptr; + strcpy(u1.name,name); + for (int i =0; i <10; i++){ + u1.friends[i] = NULL; + } + u1.next = NULL; + u1.first_post = NULL; + strcpy(u1.profile_pic,""""); + curr.next = u1ptr; + return 0; +}",how to solve the segmentation fault., +63c647f35f004bf4898cf81a,2023-02-15T19:52:45.958Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + int res = 0; + while (curr->next){ + if (strcmp(curr->name, name) == 0){ + res = 1; + } + curr = curr->next; + } + if (res == 1){ + return res; + } + if (strlen(name) > MAX_NAME - 1){ + return 2; + } + User *u1ptr = malloc(sizeof(User)); + strcpy(u1ptr->name,name); + for (int i =0; i <10; i++){ + u1ptr->friends[i] = NULL; + } + u1ptr->next = NULL; + u1ptr->first_post = NULL; + strcpy(u1ptr->profile_pic,""""); + curr->next = u1ptr; + return 0; +}",how to solve the segmentation fault., +63c647f35f004bf4898cf81a,2023-02-15T20:00:16.671Z,Question from Code,v1,how to fix the segmentation fault?,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + int res = 0; + while (curr->next){ + if (strcmp(curr->name, name) == 0){ + res = 1; + } + curr = curr->next; + } + if (res == 1){ + return res; + } + if (strlen(name) > MAX_NAME - 1){ + return 2; + } + User *u1ptr = malloc(sizeof(User)); + strcpy(u1ptr->name,name); + for (int i =0; i <10; i++){ + u1ptr->friends[i] = NULL; + } + u1ptr->next = NULL; + u1ptr->first_post = NULL; + strcpy(u1ptr->profile_pic,""""); + curr->next = u1ptr; + return 0; +}",, +63c647f35f004bf4898cf81a,2023-02-15T20:26:33.255Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { +/*Check whether the user name already exists in the list + */ + User *curr = *user_ptr_add; + int res = 0; + while (curr->next){ + if (strcmp(curr->name, name) == 0){ + res = 1; + } + curr = curr->next; + } + if (res == 1){ + return res; + } + +/*Check whether the given name cannot fit in the 'name' array + */ + if (strlen(name) > MAX_NAME - 1){ + return 2; + } + +/*Create the user with name + */ + User *u1ptr = malloc(sizeof(User)); + strcpy(u1ptr->name,name); + for (int i =0; i <10; i++){ + u1ptr->friends[i] = NULL; + } + u1ptr->next = NULL; + u1ptr->first_post = NULL; + strcpy(u1ptr->profile_pic,""""); + curr->next = u1ptr; + return 0; +}",how to fix the segmentation fault?, +63c647f35f004bf4898cf81a,2023-02-15T20:27:19.204Z,Question from Code,v1,How to solvr fault?,"int create_user(const char *name, User **user_ptr_add) { +/*Check whether the user name already exists in the list + */ + User *curr = *user_ptr_add; + int res = 0; + while (curr->next){ + if (strcmp(curr->name, name) == 0){ + res = 1; + } + curr = curr->next; + } + if (res == 1){ + return res; + } + +/*Check whether the given name cannot fit in the 'name' array + */ + if (strlen(name) > MAX_NAME - 1){ + return 2; + } + +/*Create the user with name + */ + User *u1ptr = malloc(sizeof(User)); + strcpy(u1ptr->name,name); + for (int i =0; i <10; i++){ + u1ptr->friends[i] = NULL; + } + u1ptr->next = NULL; + u1ptr->first_post = NULL; + strcpy(u1ptr->profile_pic,""""); + curr->next = u1ptr; + return 0; +}",, +63c647f35f004bf4898cf81a,2023-02-15T20:29:40.641Z,Question from Code,v1,Is there any issues?,"int check_friends(User *user1, User *user2){ + for(int i = 0; i < 10; i++){ + if(user1->friends[i]){ + if(user1->friends[i]->name == user2->name){ + return 0; + } + } + } + return 1; +}",, +63c647f35f004bf4898cf81a,2023-02-15T23:52:09.449Z,Help Fix Code,v1,,"char str[1000]; +FILE *fptr; +fptr = fopen(user->profile_pic,""r""); +if(fptr != NULL){ + if(fgets(str, 1000, fptr) != NULL){ + printf(""%s"",str); + } +}",How to fix segmentation fault?, +63c647f35f004bf4898cf81a,2023-02-15T23:53:02.065Z,Help Fix Code,v1,,"char str[1000]; +FILE *fptr; +fptr = fopen(user->profile_pic,""r""); +if(fptr != NULL){ + if(fgets(str, 1000, fptr) != NULL){ + printf(""%s"",str); + } +} +fclose(fptr);",How to fix segmentation fault?, +63c647f35f004bf4898cf81a,2023-02-15T23:54:29.673Z,Explain Code,v1,,"char str[1000]; +FILE *fptr; +fptr = fopen(user->profile_pic,""r""); +if(fptr != NULL){ + if(fgets(str, 1000, fptr) != NULL){ + printf(""%s"",str); + fclose(fptr); + } +}",, +63c647f55f004bf4898cf829,2023-01-29T01:08:20.639Z,General Question,v1,how can i check how many digits an integer has in c,,, +63c647f55f004bf4898cf829,2023-01-29T21:42:33.916Z,General Question,v1,what kind of info does ls -l give,,, +63c647f55f004bf4898cf829,2023-01-29T23:02:27.347Z,General Question,v1,why am i failing the sanity check,,, +63c647f55f004bf4898cf829,2023-01-29T23:44:20.857Z,Help Fix Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. + +//int check_permissions(char *, char *); +int check_permissions(char *given_perms, char *req_perms){ + for(int i = 0; i < 9; i++){ + if (req_perms[i]!='-'){ + if(given_perms[i]!=req_perms[i]){ + return 1; + } + } + } + return 0; +} + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } +else{ + int count = 0; + // TODO: Process command line arguments. + char total[5]; + int totalnum; + scanf(""%s %d\n"", total, &totalnum); + char filetype; + char perms[9]; + int misc0; + char misc1[31]; + char misc2[31]; + int size; + char month[31]; + int day; + char time[5]; + char name[31]; + + + while(scanf(""%c%s %d %s %s %d %s %d %s %s\n"", &filetype, perms, &misc0, misc1, misc2, &size, month, &day, time, name)==10){ + // checking size of file is big enough + int req_size = atoi(argv[0]); + if(size > req_size && filetype != 'd'){ + //checking file is not a directory + //checking required permissions, if applicable + if (argc==2){ + count = count + 1; + } + // TODO: Call check_permissions as part of your solution to count the files to compute and print the correct value. + + else if(argc==3 && check_permissions(perms, argv[2])==0){ + count = count + 1; + } + else{ + count = count + 0; + } + + } + } + printf(""%d\n"", count); + return 0; +} +} +",reasons for sanity check fail, +63c647f55f004bf4898cf829,2023-01-29T23:47:35.025Z,Help Fix Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. + +//int check_permissions(char *, char *); +int check_permissions(char *given_perms, char *req_perms){ + for(int i = 0; i < 9; i++){ + if (req_perms[i]!='-'){ + if(given_perms[i]!=req_perms[i]){ + return 1; + } + } + } + return 0; +} + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } +else{ + int count = 0; + // TODO: Process command line arguments. + char total[5]; + int totalnum; + scanf(""%s %d\n"", total, &totalnum); + char filetype; + char perms[9]; + int misc0; + char misc1[31]; + char misc2[31]; + int size; + char month[31]; + int day; + char time[5]; + char name[31]; + + + while(scanf(""%c%s %d %s %s %d %s %d %s %s\n"", &filetype, perms, &misc0, misc1, misc2, &size, month, &day, time, name)==10){ + // checking size of file is big enough + int req_size = atoi(argv[1]); + if(size > req_size && filetype != 'd'){ + //checking file is not a directory + //checking required permissions, if applicable + if (argc==2){ + count = count + 1; + } + // TODO: Call check_permissions as part of your solution to count the files to compute and print the correct value. + + else if(argc==3 && check_permissions(perms, argv[2])==0){ + count = count + 1; + } + else{ + count = count + 0; + } + + } + } + printf(""%d\n"", count); + return 0; +} +} +",read from a file, +63c647f55f004bf4898cf829,2023-02-15T16:37:51.021Z,General Question,v1,how can i cast a (const User *) to a (User *),,, +63c647f55f004bf4898cf829,2023-02-15T16:38:33.094Z,General Question,v1,what is a const,,, +63c647f55f004bf4898cf829,2023-02-15T16:40:35.031Z,General Question,v1,how do linked lists work in c,,, +63c647f55f004bf4898cf829,2023-02-15T18:01:52.279Z,General Question,v1,how can i print a text file to standard output,,, +63c647f55f004bf4898cf829,2023-02-15T19:03:15.270Z,Help Fix Code,v1,,"#include ""friends.h"" +#include +#include +#include + +/* + * 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(find_user(name, *user_ptr_add) != NULL){ + return 1; + } + else if(strlen(name) >= MAX_NAME){ + return 2; + } + else{ + User *curr = *user_ptr_add; + while(curr->next != NULL){ + curr = curr->next; + } + User *new_user = malloc(sizeof(User)); + if(new_user==NULL){ + exit(1); + } + strcpy(new_user->name, name); + strcpy(new_user->profile_pic, """"); + new_user->first_post = NULL; + for (int i = 0; i < MAX_FRIENDS; i++) { + new_user->friends[i] = NULL; + } + + new_user->next = NULL; + curr->next = new_user; + 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) { + while (head != NULL) { + if (strcmp(head->name, name) == 0){ + return (User *)head; + } + head = head->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) { + printf(""User List\n""); + while(curr->next != NULL){ + printf("" %s\n"", curr->name); + curr = curr->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) { + FILE *file = fopen(filename, ""r""); + int return_val; + if (file == NULL) { + return_val = 1; + } + else if(strlen(filename) >= MAX_NAME){ + return_val = 2; + } + else{ + return_val = 0; + strcpy(user->profile_pic, filename); + } + fclose(file); + return return_val; +} + + +/* + * 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) { + if((find_user(name1, head) == NULL) || (find_user(name2, head) == NULL)){ + return 4; + } + else if(strcmp(name1, name2) == 0){ + return 3; + } + else{ + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + + int existing_name_count = 0; + int friends1_size = 0; + int friends2_size = 0; + + //checking friends list + for(int i = 0; i < MAX_FRIENDS; i++){ + if((strcmp(user1->friends[i]->name, name2) == 0) || (strcmp(user2->friends[i]->name, name1) == 0)){ + existing_name_count = existing_name_count + 1; + } + if(user1->friends[i] != NULL){ + friends1_size = friends1_size + 1; + } + if(user2->friends[i] != NULL){ + friends2_size = friends2_size + 1; + } + } + if(existing_name_count == 0){ + if((friends1_size == MAX_FRIENDS) || (friends2_size == MAX_FRIENDS)){ + return 2; + } + else{ + user1->friends[friends1_size] = user2; + user2->friends[friends2_size] = user1; + return 0; + } + } + else{ + return 1; + } + } + return -1; +} + + +/* + * 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; + } + else{ + //Printing profile picture + FILE *file = fopen(user->profile_pic, ""r""); + if(file!=NULL){ + char line[500]; + while (fgets(line, sizeof(line), file)) { + printf(""%s"", line); + } + } + fclose(file); + //Printing name + printf(""%s %s\n%s\n"", ""Name: "", user->name, ""------------------------------------------""); + //Printing friends + printf(""%s\n"", ""Friends:""); + int i = 0; + while(user->friends[i] != NULL){ + printf(""%s\n"", user->friends[i]->name); + } + printf(""%s\n"", ""------------------------------------------""); + //Printing posts + printf(""%s\n"", ""Posts:""); + Post *curr_post = user->first_post; + while(curr_post != NULL){ + printf(""%s%s\n"", ""From: "", curr_post->author); + printf(""%s%s\n"", ""Date: "", ctime(curr_post->date)); + printf(""\n%s\n"", curr_post->contents); + if(curr_post->next != NULL){ + printf(""\n===\n\n""); + } + curr_post = curr_post->next; + } + printf(""%s\n"", ""------------------------------------------""); + } + return -1; +} + + +/* + * 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; + } + else{ + //Checking for friendship + int friends = 0; + for(int i = 0; i < MAX_FRIENDS; i++){ + if((strcmp(author->friends[i]->name, target->name) == 0) || (strcmp(target->friends[i]->name, author->name) == 0)){ + friends = friends + 1; + } + } + // they are friends + if(friends > 0){ + Post *new_post = malloc(sizeof(Post)); + if(new_post == NULL){ + exit(1); + } + strcpy(new_post->author, author->name); + new_post->contents = contents; + time(new_post->date); + new_post->next = target->first_post; + target->first_post = new_post; + return 0; + } + // not friends + else{ + 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) { + if(find_user(name, *user_ptr_del) == NULL){ + return 1; + } + else{ + User *user = find_user(name, *user_ptr_del); + //removing target from list of users + User *curr = *user_ptr_del; + User *curr2 = *user_ptr_del; + User *prev_from_target; + User *target; + while(curr!=NULL){ + if(strcmp(curr->next->name, name)==0){ + prev_from_target = curr; + target = curr->next; + } + curr = curr->next; + } + prev_from_target->next = target->next; + target->next = NULL; + + //removing target from list of friends + while(curr2!=NULL){ + for(int i = 0; i < MAX_FRIENDS; i++){ + //user in friends list + if(strcmp(curr2->friends[i]->name,name)==0){ + //shifting down friends list to overlap/delete the target user + for(int j = i; i < (MAX_FRIENDS - 1); j++){ + curr2->friends[j] = curr2->friends[j + 1]; + } + curr2->friends[MAX_FRIENDS - 1] = NULL; + } + } + curr2 = curr2->next; + } + + //freeing dynamically allocated memory + free(user); + Post *curr_post = user->first_post; + while(curr_post!=NULL){ + free(curr_post); + curr_post = curr_post->next; + } + return 0; + } +} + + +",adding david to users, +63c647f55f004bf4898cf829,2023-03-03T18:04:12.179Z,Help Fix Code,v1,,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + int child_num = 0; + for (int i = 0; i < iterations; i++) { + int n = fork(); + + if (n == 0) { + if (child_num < iterations) { + child_num++; + } + exit(0); + } + if (n < 0) { + perror(""fork""); + exit(1); + } + + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",each child process needs to have a different id, +63c647f55f004bf4898cf829,2023-03-16T01:03:54.557Z,General Question,v2,how can i check if a struct is null,,, +63c647f55f004bf4898cf829,2023-03-16T01:25:34.889Z,Help Write Code,v2,,,,create a linkedlist structure that keeps track of names +63c647f55f004bf4898cf829,2023-03-16T01:40:05.175Z,Help Write Code,v2,,,,check that a line starts with a tab +63c647f55f004bf4898cf829,2023-03-16T02:37:14.360Z,Help Fix Code,v2,change this to strncpy,"strcpy(copied_action_line, action_line);",, +63c647f55f004bf4898cf82e,2023-01-31T20:56:47.642Z,Question from Code,v1,difference between string literal and string,"char *s = ""hi""; +char s[] = ""hi"";",, +63c647f55f004bf4898cf82e,2023-01-31T21:00:41.860Z,Question from Code,v1,what is y,"char *s = ""hi""; +char x = ""hello"" +char **y = &x;",, +63c647f55f004bf4898cf82e,2023-02-14T19:34:50.095Z,Question from Code,v1,what is friends,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User;",, +63c647f55f004bf4898cf82e,2023-02-14T19:36:22.096Z,General Question,v1,what is the initial value of a string,,, +63c647f55f004bf4898cf82e,2023-02-14T19:37:10.788Z,General Question,v1,what is null character,,, +63c647f55f004bf4898cf82e,2023-02-14T19:38:02.480Z,Explain Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User;",, +63c647f55f004bf4898cf82e,2023-02-14T19:55:35.660Z,Question from Code,v1,what is the initial value of firends,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User;",, +63c647f55f004bf4898cf82e,2023-02-14T22:06:02.754Z,Explain Code,v1,,"int i, j = 0;",, +63c647f55f004bf4898cf82e,2023-02-14T22:06:39.172Z,Explain Code,v1,,"int i, j = 0, 0;",, +63c647f55f004bf4898cf82e,2023-02-14T22:15:33.316Z,General Question,v1,What is the pointer that point to NULL,,, +63c647f55f004bf4898cf82e,2023-02-14T22:18:14.597Z,Question from Code,v1,is p NULL,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +User u; +u = NULL; +User *p = &u;",, +63c647f55f004bf4898cf82e,2023-02-14T22:20:03.453Z,General Question,v1,how to figure out a pointer is pointing to NULL?,,, +63c647f55f004bf4898cf82e,2023-02-14T22:44:50.210Z,Explain Code,v1,,"char s[10]; +s = NULL;",, +63c647f55f004bf4898cf82e,2023-02-15T01:44:16.723Z,General Question,v1,what does the function time() do?,,, +63c647f55f004bf4898cf82e,2023-02-15T01:44:56.452Z,General Question,v1,what does the function ctime() do?,,, +63c647f55f004bf4898cf82e,2023-02-15T01:47:15.464Z,General Question,v1,what is the arguments of function time(),,, +63c647f55f004bf4898cf82e,2023-02-15T02:18:12.311Z,Question from Code,v1,what will User->next->name be?,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; +",, +63c647f55f004bf4898cf82e,2023-02-15T13:17:03.039Z,General Question,v1,what is the argument of free(),,, +63c647f55f004bf4898cf82e,2023-02-15T13:17:33.428Z,General Question,v1,Is string a pointer?,,, +63c647f55f004bf4898cf82e,2023-02-15T13:23:45.161Z,General Question,v1,"If the first part of and is False, will it check the second part of and?",,, +63c647f55f004bf4898cf82e,2023-02-15T13:24:20.352Z,General Question,v1,what is and symbol,,, +63c647f55f004bf4898cf82e,2023-02-15T13:24:46.702Z,General Question,v1,Difference between & and &&,,, +63c647f55f004bf4898cf82e,2023-02-15T13:28:05.161Z,General Question,v1,How to remove an element in an array,,, +63c647f55f004bf4898cf82e,2023-02-15T13:32:39.001Z,Question from Code,v1,what will i[2] be?,"int i[2]; +i[0] = 1; +i[1] = 2; +",, +63c647f55f004bf4898cf82e,2023-02-15T14:33:56.727Z,General Question,v1,What is the argument of ctime(),,, +63c647f55f004bf4898cf82e,2023-02-15T14:44:01.883Z,Question from Code,v1,can I set u.profile_pic to NULL,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +User u; +u.profile_pic = NULL; +",, +63c647f55f004bf4898cf82e,2023-02-15T14:46:29.919Z,Question from Code,v1,what is the initial value of profile_pic?,"char profile_pic[32] +",, +63c647f55f004bf4898cf82e,2023-02-15T14:47:14.253Z,Question from Code,v1,what is the value of profile_pic?,"char profile_pic[32] +profile_pic[0] = '\0'",, +63c647f55f004bf4898cf82e,2023-02-15T14:49:04.067Z,General Question,v1,Can I set a variable contains a struct to NULL?,,, +63c647f55f004bf4898cf82e,2023-02-15T14:51:58.958Z,Question from Code,v1,what is the value of empty_friends?,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +User *empty_friends[MAX_FRIENDS];",, +63c647f55f004bf4898cf82e,2023-02-15T16:00:41.383Z,General Question,v1,how to assign to expression with array,,, +63c647f55f004bf4898cf82e,2023-02-15T16:02:02.121Z,General Question,v1,"When I create an object of a struct, will the values in the struct be created automatically?",,, +63c647f55f004bf4898cf82e,2023-02-15T16:06:47.083Z,General Question,v1,how to assign an array to the attributes of a struct,,, +63c647f55f004bf4898cf82e,2023-02-15T16:07:45.641Z,General Question,v1,how to assign variable with array,,, +63c647f55f004bf4898cf82e,2023-02-15T16:39:03.979Z,Explain Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + //check whether the name is too long; + if (strlen(name) > MAX_NAME - 1){ + return 2; + } + //check whether the user already exists and find the previous user of new user + User *curr = *user_ptr_add; + while (curr->next != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + //create new user + User new_user; + strcpy(new_user.name, name); + char profile[MAX_NAME]; + profile[0] = '\0'; + strcpy(new_user.profile_pic, profile); + new_user.first_post = NULL; + for (int i = 0; i < MAX_FRIENDS; i++){ + new_user.friends[i] = NULL; + } + new_user.next = NULL; + //put the new_user in the list + curr->next = &new_user; + return 0; +}",, +63c647f55f004bf4898cf82e,2023-02-15T20:26:05.428Z,General Question,v1,what is the difference between ptr = &value and *ptr = value?,,, +63c647f55f004bf4898cf82e,2023-02-15T23:17:16.406Z,Question from Code,v1,does line 15 work?,"typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int main(){ + time_t date; + time(&date); + + Post *ptr; + ptr = malloc(sizeof(Post)); + strcpy(ptr->author, author->name); + strcpy(ptr->contents, contents); + ptr->date = &date; + ptr->next = NULL; +}",, +63c647f55f004bf4898cf82e,2023-02-15T23:20:39.442Z,General Question,v1,"if I want to assign value to a string literal which is a variable of a struct, what should I do?",,, +63c647f55f004bf4898cf82e,2023-02-15T23:23:03.111Z,Question from Code,v1,what segmentation fault the code may cause?,"typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int main(){ + time_t date; + time(&date); + + Post *ptr; + ptr = malloc(sizeof(Post)); + strcpy(ptr->author, author->name); + strcpy(ptr->contents, contents); + ptr->date = &date; + ptr->next = NULL; +}",, +63c647f55f004bf4898cf82e,2023-02-15T23:23:42.197Z,Question from Code,v1,how to allocate contents?,"typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int main(){ + time_t date; + time(&date); + + Post *ptr; + ptr = malloc(sizeof(Post)); + strcpy(ptr->author, author->name); + strcpy(ptr->contents, contents); + ptr->date = &date; + ptr->next = NULL; +}",, +63c647f55f004bf4898cf82e,2023-02-17T13:18:20.868Z,General Question,v1,Does the for-loop counter disappear after the loop stoped,,, +63c647f55f004bf4898cf82e,2023-02-17T13:20:02.965Z,Question from Code,v1,show the standard outpuy,"for (int i=0; i < 5; i++){ + printf(""%d\n"", i); +} + +printf(""%d\n"", i+1);",, +63c647f55f004bf4898cf82e,2023-03-10T11:53:08.513Z,General Question,v2,what does dup2 function do,,, +63c647f55f004bf4898cf82e,2023-03-13T10:39:09.789Z,General Question,v2,do you know current CSC209 A3?,,, +63c647f55f004bf4898cf82e,2023-03-13T10:41:04.888Z,General Question,v2,How to read lines of a makefile?,,, +63c647f55f004bf4898cf82e,2023-03-13T14:04:12.367Z,Question from Code,v2,What does this function return if line is empty?,"int is_comment_or_empty(const char *line) { + for (int i = 0; i < strlen(line); i++) { + if (line[i] == '#') { + return 1; + } + if (line[i] != '\t' && line[i] != ' ') { + return 0; + } + } + return 1; +}",, +63c647f55f004bf4898cf82e,2023-03-13T14:05:38.657Z,General Question,v2,"How to separate a string by "" ""?",,, +63c647f55f004bf4898cf82e,2023-03-13T15:21:47.712Z,General Question,v2,should we always allocate spaces on the heap for structs if we want to get access to it after function returns?,,, +63c647f55f004bf4898cf82e,2023-03-13T15:29:59.077Z,Question from Code,v2,what will this file print?,"#include +#include + +int main() { + char str[80] = ""This is - www.tutorialspoint.com - website""; + const char s[2] = ""-""; + char *token; + + token = strtok(str, s); + + while (token != NULL) { + printf("" %s\n"", token); + + token = strtok(NULL, s); + } + + return (0); +}",, +63c647f55f004bf4898cf82e,2023-03-13T17:46:50.070Z,General Question,v2,What will fgets() function get if the line is empty,,, +63c647f55f004bf4898cf82e,2023-03-13T17:49:18.102Z,General Question,v2,How to strip a string after using fgets()?,,, +63c647f55f004bf4898cf82e,2023-03-13T18:28:09.774Z,Question from Code,v2,what will it print?,"#include +#include + +int main() { + char str[100] = ""Hello, world!\n""; + char *token; + + token = strtok(str, ""\n""); // split the string at ""\n"" + strcat(token, """"); // concatenate the tokens to get the final string + + printf(""%s"", token); // print the string without ""\n"" + + return 0; +}",, +63c647f55f004bf4898cf82e,2023-03-13T18:28:51.032Z,General Question,v2,what will it print?,,, +63c647f55f004bf4898cf82e,2023-03-13T18:29:21.579Z,Question from Code,v2,what will it print?,"#include +#include + +int main() { + char str[100] = ""Hello, world!\n""; + char *token; + + str = strtok(str, ""\n""); // split the string at ""\n"" + strcat(str, """"); // concatenate the tokens to get the final string + + printf(""%s"", str); // print the string without ""\n"" + + return 0; +}",, +63c647f55f004bf4898cf82e,2023-03-13T18:30:02.128Z,Question from Code,v2,what will it print?,"#include +#include + +int main() { + char str[100] = ""Hello, world!\n""; + char *token; + + str = strtok(str, ""\n""); // split the string at ""\n"" + // strcat(str, """"); // concatenate the tokens to get the final string + + printf(""%s"", str); // print the string without ""\n"" + + return 0; +}",, +63c647f55f004bf4898cf82e,2023-03-13T19:55:52.117Z,General Question,v2,"If I have a pointer of a struct Rule called rule1, and another pointer pointing to the same thing. Then the first pointer point to another struct Rule called rule2. What will pointer 2 be?",,, +63c647f55f004bf4898cf82e,2023-03-13T20:16:53.617Z,Question from Code,v2,is line 7 correct?,"#include +#include + +int main() { + char str[100] = ""Hello, world!\n""; + + str = strtok(str, ""\n""); // split the string at ""\n"" + strcat(str, """"); // concatenate the tokens to get the final string + + printf(""%s"", str); // print the string without ""\n"" + + return 0; +}",, +63c647f55f004bf4898cf82e,2023-03-13T20:19:09.446Z,Question from Code,v2,is line 7 correct?,"#include +#include + +int main() { + char *str = ""Hello, world!\n""; + + str = strtok(str, ""\n""); // split the string at ""\n"" + strcat(str, """"); // concatenate the tokens to get the final string + + printf(""%s"", str); // print the string without ""\n"" + + return 0; +}",, +63c647f55f004bf4898cf82e,2023-03-13T20:22:18.349Z,Question from Code,v2,is line 7 correct?,"#include +#include + +int main() { + char str[100] = ""Hello, world!\n""; + char *token; + token = strtok(str, ""\n""); // split the string at ""\n"" + strcpy(str, token); + printf(""%s"", str); // print the string without ""\n"" + + return 0; +}",, +63c647f55f004bf4898cf82e,2023-03-13T20:23:53.994Z,Question from Code,v2,is line 7 correct?,"#include +#include + +int main() { + char str[100] = ""Hello, world!\n""; + char *token; + token = strtok(str, ""\n""); // split the string at ""\n"" + + printf(""%s"", token); // print the string without ""\n"" + + return 0; +}",, +63c647f55f004bf4898cf82e,2023-03-13T20:27:30.385Z,Question from Code,v2,what will str be after line 8?,"#include +#include + +int main() { + char str[100] = ""Hello, world!\n""; + char *token; + token = strtok(str, ""\n""); // split the string at ""\n"" + strcpy(str, token); + printf(""%s"", token); // print the string without ""\n"" + + return 0; +}",, +63c647f55f004bf4898cf82e,2023-03-13T23:31:50.979Z,General Question,v2,How to read a file again?,,, +63c647f55f004bf4898cf82e,2023-03-13T23:33:14.134Z,General Question,v2,how to read a file twice in a function,,, +63c647f55f004bf4898cf82e,2023-03-13T23:40:59.960Z,General Question,v2,what does fgets() return?,,, +63c647f55f004bf4898cf82e,2023-03-14T17:41:53.345Z,General Question,v2,how to report error if error occurred during malloc?,,, +63c647f55f004bf4898cf82e,2023-03-14T20:25:28.265Z,General Question,v2,"if I split a string ""\t hello world"" by strtok, what will the result be?",,, +63c647f55f004bf4898cf82e,2023-03-14T20:28:53.200Z,General Question,v2,"If I use strtok() to a string contains '\t' by delimiter "" "", and then I keep printing strtok(NULL, space), will '\t' be printed out?",,, +63c647f55f004bf4898cf82e,2023-03-14T21:17:13.541Z,General Question,v2,How to debug using terminal,,, +63c647f55f004bf4898cf82e,2023-03-14T21:53:41.562Z,General Question,v2,how to copy a string?,,, +63c647f55f004bf4898cf82e,2023-03-14T22:04:35.138Z,General Question,v2,what does \r mean?,,, +63c647f55f004bf4898cf82e,2023-03-14T22:14:08.467Z,General Question,v2,how to use strtok to strip \n and \r,,, +63c647f55f004bf4898cf82e,2023-03-14T22:17:44.428Z,General Question,v2,explain strncpy(),,, +63c647f55f004bf4898cf82e,2023-03-14T22:30:37.678Z,General Question,v2,how to strip '\n' of a string by using strtok,,, +63c647f55f004bf4898cf82e,2023-03-14T22:58:13.792Z,General Question,v2,1 is equal to true or false,,, +63c647f55f004bf4898cf82e,2023-03-15T09:09:49.813Z,Help Fix Code,v2,"split line ""\tgcc -Wall -g -std=gnu99 -o main linked_list.o main.o"" by "" "" and forms an array of strings","char **helper_build_action(char *line){ + char **result = malloc(MAXLINE * sizeof(char)); + if (result == NULL){ + perror(""Error occurred during malloc""); + exit(1); + } + char *tk = strtok(line, "" ""); + int i = 0; + tk = strtok(NULL, "" ""); + while(tk != NULL){ + result[i] = tk; + i += 1; + tk = strtok(NULL, "" ""); + } + result[i] = NULL; + return result; +}",, +63c647f55f004bf4898cf82e,2023-03-15T09:24:52.903Z,Help Fix Code,v2,"input: line = ""\tgcc -Wall -g -std=gnu99 -o main linked_list.o main.o"" +desired output = [""gcc"", ""-Wall"", ""-g"", ""-std=gnu99"", ""-o"", ""main"", ""linked_list.o"", ""main.o"", NULL]","char **helper_build_action(char *line){ + char **result = malloc(MAXLINE * sizeof(char)); + if (result == NULL){ + perror(""Error occurred during malloc""); + exit(1); + } + char *tk = strtok(line, ""\t""); + int i = 0; + tk = strtok(NULL, ""\t""); + while(tk != NULL){ + result[i] = strdup(tk); + i += 1; + tk = strtok(NULL, "" ""); + } + result[i] = NULL; + return result; +} +",, +63c647f55f004bf4898cf82e,2023-03-15T09:26:30.172Z,Help Fix Code,v2,"Intended Behavior: input: line = ""\tgcc -Wall -g -std=gnu99 -o main linked_list.o main.o"" +result = [""gcc"", ""-Wall"", ""-g"", ""-std=gnu99"", ""-o"", ""main"", ""linked_list.o"", ""main.o"", NULL]","char **helper_build_action(char *line){ + char **result = malloc(MAXLINE * sizeof(char)); + if (result == NULL){ + perror(""Error occurred during malloc""); + exit(1); + } + char *tk = strtok(line, ""\t""); + int i = 0; + tk = strtok(NULL, ""\t""); + while(tk != NULL){ + result[i] = strdup(tk); + i += 1; + tk = strtok(NULL, "" ""); + } + result[i] = NULL; + return result; +} +",, +63c647f55f004bf4898cf82e,2023-03-15T10:36:12.810Z,General Question,v2,what does execvp do,,, +63c647f55f004bf4898cf82e,2023-03-15T11:26:13.271Z,General Question,v2,how to call stat?,,, +63c647f55f004bf4898cf82e,2023-03-15T15:12:19.483Z,General Question,v2,what is the difference between perror and fprintf,,, +63c647f55f004bf4898cf82e,2023-03-15T15:28:32.546Z,General Question,v2,How to use access to determine file existence.,,, +63c647f55f004bf4898cf82e,2023-03-15T15:42:37.736Z,General Question,v2,how to initialize a variable taking true/false value,,, +63c647f55f004bf4898cf82e,2023-03-15T16:59:34.197Z,General Question,v2,what is st_mtim in struct stat?,,, +63c647f55f004bf4898cf82e,2023-03-15T18:41:30.616Z,General Question,v2,Explain WIFEXIT,,, +63c647f55f004bf4898cf82e,2023-03-15T18:56:51.445Z,General Question,v2,Should child process always use exit(0) at the end?,,, +63c647f55f004bf4898cf82e,2023-03-15T19:08:39.178Z,General Question,v2,is pid_t int?,,, +63c647f55f004bf4898cf82e,2023-03-15T19:23:18.052Z,General Question,v2,explain WIFEXITED,,, +63c647f55f004bf4898cf82e,2023-03-15T19:25:24.315Z,General Question,v2,explain WEXITSTATUS,,, +63c647f55f004bf4898cf82e,2023-03-15T19:25:24.647Z,General Question,v2,explain WEXITSTATUS,,, +63c647f55f004bf4898cf82e,2023-03-17T19:40:21.416Z,General Question,v2,How to generate a random int between 0 to 99,,, +63c647f55f004bf4898cf82e,2023-03-17T19:45:31.804Z,General Question,v2,what does it mean if a loop header is for (;;),,, +63c647f55f004bf4898cf82e,2023-03-17T19:47:28.121Z,Help Write Code,v2,,,,Generate a random integer from 0 to 100 +63c647f55f004bf4898cf82e,2023-03-17T20:03:48.107Z,General Question,v2,"if I add a signal handler to a file, where do I add it?",,, +63c647f55f004bf4898cf82e,2023-03-17T20:44:24.248Z,General Question,v2,"what does setitimer do +",,, +63c647f55f004bf4898cf82e,2023-04-05T20:05:56.675Z,General Question,v2,what is the input of malloc function,,, +63c647f55f004bf4898cf82e,2023-04-05T20:23:39.847Z,General Question,v2,explain strcat,,, +63c647f55f004bf4898cf82e,2023-04-05T20:28:31.540Z,General Question,v2,what will strncat do?,,, +63c647f55f004bf4898cf82e,2023-04-05T20:32:44.199Z,General Question,v2,"what is the strlen of ""\n""",,, +63c647f55f004bf4898cf82e,2023-04-05T20:37:38.981Z,General Question,v2,what does snprintf do,,, +63c647f55f004bf4898cf82e,2023-04-06T01:10:52.091Z,General Question,v2,"asctime(localtime()), what does the return string looks like",,, +63c647f55f004bf4898cf82e,2023-04-06T04:24:28.532Z,General Question,v2,do we need to catch error for free()?,,, +63c647f55f004bf4898cf82e,2023-04-06T07:00:55.885Z,General Question,v2,server program and client program communicate via what?,,, +63c647f65f004bf4898cf838,2023-01-20T17:47:56.538Z,Help Fix Code,v1,,"\usepackage[margins=2.5]{geometry} +",, +63c647f65f004bf4898cf838,2023-01-23T21:49:02.256Z,General Question,v1,"If I have float *x; and float y; how do I increase the amount stored in y by the amount in x? + + + + +",,, +63c647f65f004bf4898cf838,2023-01-23T21:53:49.733Z,Help Fix Code,v1,," + + + +int main(int argc, char **argv) { + float x = 2.2; + float *y; + y = &x +1 + + + +",add the value of x to the value of y, +63c647f65f004bf4898cf838,2023-01-30T21:45:11.994Z,Help Fix Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +int check_permissions(char *permission, char *file) { + for (int i = 0; i < 9; i++) { + if (permission[i] != file[i]) { + return 1; + } + } return 0; +} + + + + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + int loop = (argc == 2); + printf(""loop:%d"", loop); + + int check_size = strtol(argv[1], NULL, 10); + char perm[10]; + int size, count; + count = 0; + int cycle = 0; + scanf(""%*s %*s""); + while (scanf(""%s %*d %*s %*s %d %*s %*s %*s %*s"", perm, &size) == 2){ + printf(""cycle :%d"", cycle); cycle++; + if (perm[0] == '-') { + printf(""cycle :%d"", cycle); cycle++; + if (size > check_size){ + if (loop ==0){ + count++; + } else { + if (check_permissions(&perm[1], argv[2])==0){ + count++; + } + } + } + } + } + printf(""cycle :%d"", cycle); cycle++; + printf(""%d\n"", count); + return 0; +}","return a count of ls -l file sizes larger than value parameter provided. If second parameter is 9 element permissions, the count is of files larger than first parameter that also match permissions.", +63c647f65f004bf4898cf838,2023-01-30T21:51:20.234Z,Help Fix Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +int check_permissions(char *permission, char *file) { + for (int i = 0; i < 9; i++) { + if (permission[i] != file[i]) { + return 1; + } + } return 0; +} + + + + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + int loop = (argc == 3); + printf(""loop:%d"", loop); + + int check_size = strtol(argv[1], NULL, 10); + char perm[10]; + int size, count; + count = 0; + int cycle = 0; + scanf(""%*s %*s""); + while (scanf(""%s %*d %*s %*s %d %*s %*s %*s %*s"", perm, &size) == 2){ + printf(""cycle :%d"", cycle); cycle++; + if (perm[0] == '-') { + printf(""cycle :%d"", cycle); cycle++; + if (size > check_size){ + if (loop ==0){ + count++; + } else { + if (check_permissions(&perm[1], argv[2])==0){ + count++; + } + } + } + } + } + printf(""cycle :%d"", cycle); cycle++; + printf(""%d\n"", count); + return 0; +}",return count of files larger than given parameter, +63c647f65f004bf4898cf838,2023-02-03T22:18:04.327Z,Help Fix Code,v1,,"#include +#include +#include + +/* + Write a function named truncate() that takes a string s and a + non-negative integer n. If s has more than n characters (not including the + null terminator), the function should truncate s at n characters and + return the number of characters that were removed. If s has n or + fewer characters, s is unchanged and the function returns 0. For example, + if s is the string ""function"" and n is 3, then truncate() changes s to + the string ""fun"" and returns 5. +*/ +int truncate(char *s, int n) { + + int val_length = strlen(s); + + + if (val_length <= n){ + return 0; + } + s[n] = '\0'; + return val_length - n; + +} + + +int main(int argc, char **argv) { + /* Do not change the main function */ + if (argc != 3) { + fprintf(stderr, ""Usage: truncate number string\n""); + return 1; + } + int amt = strtol(argv[1], NULL, 10); + + char *target = argv[2]; + + int soln_val = truncate(target, amt); + printf(""%d %s\n"", soln_val, target); + + return 0; +}","it should return pow 3 when run with ./o powder 3 +", +63c647f95f004bf4898cf851,2023-01-23T19:47:31.213Z,General Question,v1,Assign a dynamically allocate char* array of length 4 to variable called last_names,,, +63c647f95f004bf4898cf851,2023-01-23T19:51:08.380Z,General Question,v1,How to write a for loop in c for an array,,, +63c647f95f004bf4898cf851,2023-01-23T19:58:31.200Z,General Question,v1,allocate a character array of length 20 for each element of the array pointed to by last_names,,, +63c647f95f004bf4898cf851,2023-01-23T20:01:55.140Z,Question from Code,v1,How do I allocate a character array of length 20 for each element of the array pointed to by last_names?,"#include +#include +int main() { + char **last_names; + // Assign a dynamically allocated char * array of length 4 to last_names. + // Then, allocate a character array of length 20 for each element of the array + // pointed to by last_names. + last_names = (char **) malloc(sizeof(char*)*4); + for (int i=0; i <= sizeof(last_names); ++i) + { + last_names[i] = (char *) malloc(20 * sizeof(char)); + } + return 0; +}",, +63c647f95f004bf4898cf851,2023-01-23T20:12:17.491Z,General Question,v1,allocate space on the heap for an array of 12 floats and assign it to the pointer monthly_rainfall,,, +63c647f95f004bf4898cf851,2023-01-23T20:13:05.936Z,General Question,v1,"rainfall has been dynamically allocated space for a floating point number and initialized in hidden code. +Add 7.6 to the amount currently stored in the space rainfall points to. +",,, +63c647f95f004bf4898cf851,2023-01-23T20:14:19.832Z,General Question,v1,"Add 7.6 to the amount currently stored in the space rainfall points to. +",,, +63c647f95f004bf4898cf851,2023-01-23T20:15:39.566Z,General Question,v1,Increase the amount currently stored in total_rain by the amount stored in the space pointed to by rainfall.,,, +63c647f95f004bf4898cf851,2023-01-23T20:18:06.765Z,General Question,v1,set the allocated_amount variable to the exact amount of space that should be allocated and pointed to by float *rainfall,,, +63c647f95f004bf4898cf851,2023-01-23T20:20:00.684Z,Question from Code,v1,Assign the amount in rain_today to the space rainfall points to.,"#include +#include + +int main(int argc, char **argv) { + float *rainfall; + float rain_today; + // rainfall has been dynamically allocated space for a floating point number. + // Both rainfall and rain_today have been initialized in hidden code. + // Assign the amount in rain_today to the space rainfall points to. + + return 0; +} +",, +63c647f95f004bf4898cf851,2023-01-23T20:25:31.830Z,Help Fix Code,v1,,"#include +#include + +int main(int argc, char **argv) { + float *rainfall; + float rain_today; + // rainfall has been dynamically allocated space for a floating point number. + // Both rainfall and rain_today have been initialized in hidden code. + // Assign the amount in rain_today to the space rainfall points to. + rain_today = *rainfall; + return 0; +} +",rain_today should be used., +63c647f95f004bf4898cf851,2023-01-23T20:31:32.366Z,General Question,v1,"/* Returns the sum of two integers in the string input_line. + + input_line is a string containing two integers separated by one + or more spaces. + + Assume that the string will not be more than 20 characters + long and that it contains two integers separated only by whitespace. + There may also be spaces before the first integer + and/or after the last, but there won't be any other characters + in the string. + + For example, get_sum("" 123 5 "") should return the integer 128, + and get_sum(""-14 30"") should return the integer 16. + + Hint: use strtol(). +*/",,, +63c647f95f004bf4898cf851,2023-01-23T21:33:06.583Z,General Question,v1,return the sum of two integers in the string input_line using strtol().,,, +63c647f95f004bf4898cf851,2023-01-23T21:34:25.773Z,Help Fix Code,v1,,"* Returns the sum of two integers in the string input_line. + + input_line is a string containing two integers separated by one + or more spaces. + + Assume that the string will not be more than 20 characters + long and that it contains two integers separated only by whitespace. + There may also be spaces before the first integer + and/or after the last, but there won't be any other characters + in the string. + + For example, get_sum("" 123 5 "") should return the integer 128, + and get_sum(""-14 30"") should return the integer 16. + + Hint: use strtol(). +*/ +int get_sum(const char *input_line) { +// Submitted code below + int a = strtol(input_line, &input_line, 10); + int b = strtol(input_line, &input_line, 10); + printf(""%d"", a + b); + return 0; +// Submitted code above +} + +// Submitted code below",, +63c647f95f004bf4898cf851,2023-01-23T21:36:37.332Z,General Question,v1,"Given three variables with hidden integer values, compare their values for uniqueness and print one of the following statements: +- ""none unique"" (if all variables share the same value) +- ""some unique"" (if exactly two variables share the same value) +- ""all unique"" (if there are no duplicates)",,, +63c647f95f004bf4898cf851,2023-01-23T22:12:55.480Z,General Question,v1,"Write a for loop that prints the integers from 1 to 10 with spaces separating the values. +",,, +63c647f95f004bf4898cf851,2023-01-25T21:04:05.798Z,Question from Code,v1,Why am I getting a segmentation fault?," + +#include +#include + +/* + * 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_seq, int count) { + *fib_seq = malloc(count * sizeof(int)); + + if (count == 0) { + return; + } + if (count > 0) { + (*fib_seq)[0] = 0; + } + if (count > 1) { + (*fib_seq)[1] = 1; + } + for (int i=2; i < count; i++) { + (*fib_seq)[i] = (*fib_seq)[i-2] + (*fib_seq)[i-1]; + } + return; +} + +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; +}",, +63c647f95f004bf4898cf851,2023-01-25T21:05:50.428Z,Help Fix Code,v1,," + +#include +#include + +/* + * 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_seq, int count) { + *fib_seq = malloc(count * sizeof(int)); + + if (count == 0) { + return; + } + if (count > 0) { + (*fib_seq)[0] = 0; + } + if (count > 1) { + (*fib_seq)[1] = 1; + } + for (int i=2; i < count; i++) { + (*fib_seq)[i] = (*fib_seq)[i-2] + (*fib_seq)[i-1]; + } + return; +} + +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; +}","no segmentation fault +", +63c647f95f004bf4898cf851,2023-01-25T21:43:24.126Z,Help Fix Code,v1,,"#include +#include + +/* + * 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 **sequence, int num){ + + *sequence = malloc(sizeof(int) * num); + int *sequence2 = *sequence; + + if (num > 0) { + sequence2[0] = 0; + if (num > 1) { + sequence2[1] = 1; + for (int i = 2; i < num; i++){ + sequence2[i] = sequence2[i - 1] + sequence2[i - 2]; + } + } + } + +} + + +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; +}",no segmentation error, +63c647f95f004bf4898cf851,2023-01-25T22:49:36.528Z,General Question,v1,"Define a string literal named tasty with the value ""doughnut"".",,, +63c647f95f004bf4898cf851,2023-01-25T22:55:02.957Z,General Question,v1,"Set the value of delicious (which has already been declared) to ""ice cream"". +",,, +63c647f95f004bf4898cf851,2023-01-25T22:57:12.367Z,General Question,v1,"The string sweet has been declared on the heap and has space for 20 characters. +Set the value of sweet to ""tiramisu"" without changing its address.",,, +63c647f95f004bf4898cf851,2023-01-25T22:59:44.674Z,General Question,v1,"declare a struct called best_friend and initialize the members of best_friend based on the following information. + +Name: Jane Doe +Phone Number: 377 177 377 +Address: 1337 University Avenue",,, +63c647f95f004bf4898cf851,2023-01-26T01:21:58.462Z,General Question,v1,"Define a string literal called season that has the value +""winter"". +",,, +63c647f95f004bf4898cf851,2023-01-26T01:25:29.535Z,General Question,v1,"Define a stack-allocated string called holiday that has space for 20 characters +including the null terminator. Set its initial value to ""Valentine's Day"". +",,, +63c647f95f004bf4898cf851,2023-01-26T01:26:55.050Z,General Question,v1,"Create a node struct with the following struct members: +- value, an integer +- next, a pointer to another node + +Typedef your structure as Node. +",,, +63c647f95f004bf4898cf851,2023-01-26T01:27:45.634Z,General Question,v1,"Write a loop which traverses a linked list starting at front and prints the value of each node in the list. + +Separate each value with a space.",,, +63c647f95f004bf4898cf851,2023-01-26T01:32:00.863Z,Question from Code,v1,"Write a loop which traverses a linked list starting at front and prints the value of each node in the list. + +Separate each value with a space.","typedef struct node { + int value; + struct node *next; +} Node; + +int main(int argc, char **argv) { + Node *front; // pointer to first node of the list +// Submitted code below + while (curr != NULL) { + printf(""%d \n"", curr->value); + curr = curr->next; + } +// Submitted code above + return 0; +} +// Submitted code below",, +63c647f95f004bf4898cf851,2023-01-26T01:32:37.598Z,Help Fix Code,v1,,"typedef struct node { + int value; + struct node *next; +} Node; + +int main(int argc, char **argv) { + Node *front; // pointer to first node of the list +// Submitted code below + while (curr != NULL) { + printf(""%d \n"", curr->value); + curr = curr->next; + } +// Submitted code above + return 0; +} +// Submitted code below","Write a loop which traverses a linked list starting at front and prints the value of each node in the list. + +Separate each value with a space.", +63c647f95f004bf4898cf851,2023-01-26T01:39:02.111Z,Help Fix Code,v1,,"#include +#include +#include + +int main() { + char delicious[30]; +// Submitted code below + +// Submitted code above + + /* hidden test code */ + + return 0; +} +// Submitted code below","Set the value of delicious (which has already been declared) to ""ice cream"".", +63c647f95f004bf4898cf851,2023-01-26T01:40:13.328Z,Help Fix Code,v1,,"#include +#include +#include + +int main() { + char delicious[30]; +// Submitted code below + delicious = ""ice cream""; +// Submitted code above + + /* hidden test code */ + + return 0; +} +// Submitted code below","Set the value of delicious (which has already been declared) to ""ice cream"". +", +63c647f95f004bf4898cf851,2023-01-26T01:41:41.472Z,Help Fix Code,v1,,"#include +#include +#include + +int main() { + char delicious[30]; +// Submitted code below + *delicious = strcpy(delicious, ""ice cream""); +// Submitted code above + + /* hidden test code */ + + return 0; +} +// Submitted code below","Set the value of delicious (which has already been declared) to ""ice cream"". +", +63c647f95f004bf4898cf851,2023-01-26T01:42:25.408Z,Help Fix Code,v1,,"#include +#include +#include + +int main() { + char delicious[30]; +// Submitted code below + delicious = strcpy(delicious, ""ice cream""); +// Submitted code above + + /* hidden test code */ + + return 0; +} +// Submitted code below","Set the value of delicious (which has already been declared) to ""ice cream"". +", +63c647f95f004bf4898cf851,2023-01-26T01:43:10.071Z,Help Fix Code,v1,,"#include +#include +#include + +int main() { + char delicious[30]; +// Submitted code below + delicious = strcpy(delicious, ""ice cream""); +// Submitted code above + + /* hidden test code */ + + return 0; +} +// Submitted code below","Set the value of delicious (which has already been declared) to ""ice cream"". +", +63c647f95f004bf4898cf851,2023-01-26T01:47:43.429Z,Help Fix Code,v1,,"#include +#include +#include + +struct contact { + char name[30]; + int phone_number; + char address[30]; +}; + +// update_contact goes here +// Submitted code below + +// Submitted code above + +int main(int argc, char **argv) { + struct contact friend; + // set initial values for friend + strcpy(friend.name, ""Jane Doe""); + friend.phone_number = 377177377; + strcpy(friend.address, ""3771 University Avenue""); + + char *name; + int number; + char *address; + // in hidden code set values for these 3 variables + + // call update_contact to change the values in friend + update_contact(&friend, name, number, address); + return 0; +}","Given the following program, write the function update_contact which takes a pointer to the struct contact, a new name, a new number, and a new address to change the struct’s members. Look at the function call for the order of arguments and appropriate types.", +63c647f95f004bf4898cf851,2023-01-26T01:56:49.710Z,Help Fix Code,v1,,"#include +#include +#include + +struct contact { + char name[30]; + int phone_number; + char address[30]; +}; + +// update_contact goes here +// Submitted code below +void update_contact(struct *contact friend, char name, int number, char address) { + strcpy(friend.name = name); + friend.number = number; + strcpy(friend.address = address); +} +// Submitted code above + +int main(int argc, char **argv) { + struct contact friend; + // set initial values for friend + strcpy(friend.name, ""Jane Doe""); + friend.phone_number = 377177377; + strcpy(friend.address, ""3771 University Avenue""); + + char *name; + int number; + char *address; + // in hidden code set values for these 3 variables + + // call update_contact to change the values in friend + update_contact(&friend, name, number, address); + return 0; +} +// Submitted code below","Given the following program, write the function update_contact which takes a pointer to the struct contact, a new name, a new number, and a new address to change the struct’s members. Look at the function call for the order of arguments and appropriate types. +", +63c647f95f004bf4898cf851,2023-01-26T01:59:52.494Z,Help Fix Code,v1,,"#include +#include +#include + +struct contact { + char name[30]; + int phone_number; + char address[30]; +}; + +// update_contact goes here +// Submitted code below +void update_contact(struct *contact friend, char *name, int *number, char *address) { + strcpy(friend.name = name); + friend.phone_number = number; + strcpy(friend.address = address); +} +// Submitted code above + +int main(int argc, char **argv) { + struct contact friend; + // set initial values for friend + strcpy(friend.name, ""Jane Doe""); + friend.phone_number = 377177377; + strcpy(friend.address, ""3771 University Avenue""); + + char *name; + int number; + char *address; + // in hidden code set values for these 3 variables + + // call update_contact to change the values in friend + update_contact(&friend, name, number, address); + return 0; +} +// Submitted code below","Given the following program, write the function update_contact which takes a pointer to the struct contact, a new name, a new number, and a new address to change the struct’s members. Look at the function call for the order of arguments and appropriate types. +", +63c647f95f004bf4898cf851,2023-01-26T02:10:14.124Z,Help Fix Code,v1,,"#include +#include +#include + +struct contact { + char name[30]; + int phone_number; + char address[30]; +}; + +// update_contact goes here +// Submitted code below +void update_contact(struct *contact friend, char *name, int number, char *address) { + strcpy(friend.name = name); + friend.phone_number = number; + strcpy(friend.address = address); +} +// Submitted code above + +int main(int argc, char **argv) { + struct contact friend; + // set initial values for friend + strcpy(friend.name, ""Jane Doe""); + friend.phone_number = 377177377; + strcpy(friend.address, ""3771 University Avenue""); + + char *name; + int number; + char *address; + // in hidden code set values for these 3 variables + + // call update_contact to change the values in friend + update_contact(&friend, name, number, address); + return 0; +}","write the function update_contact which takes a pointer to the struct contact, a new name, a new number, and a new address to change the struct’s members", +63c647f95f004bf4898cf851,2023-01-26T02:14:39.487Z,Help Fix Code,v1,,"#include +#include +#include + +struct contact { + char name[30]; + int phone_number; + char address[30]; +}; + +// update_contact goes here +// Submitted code below +void update_contact(struct *contact c, char new_name, int new_number, char new_address) { + strcpy(c.name = new_name); + c.phone_number = new_number; + strcpy(c.address = new_address); +} +// Submitted code above + +int main(int argc, char **argv) { + struct contact friend; + // set initial values for friend + strcpy(friend.name, ""Jane Doe""); + friend.phone_number = 377177377; + strcpy(friend.address, ""3771 University Avenue""); + + char *name; + int number; + char *address; + // in hidden code set values for these 3 variables + + // call update_contact to change the values in friend + update_contact(&friend, name, number, address); + return 0; +} +// Submitted code below","write the function update_contact which takes a pointer to the struct contact, a new name, a new number, and a new address to change the struct’s members. Look at the function call for the order of arguments and appropriate types. +", +63c647f95f004bf4898cf851,2023-01-26T02:18:12.884Z,Help Fix Code,v1,,"#include +#include +#include + +struct contact { + char name[30]; + int phone_number; + char address[30]; +}; + +// update_contact goes here +// Submitted code below +void update_contact(struct *contact c, char new_name, int new_number, char new_address) { + strcpy(c->name = new_name); + (*c).phone_number = new_number; + strcpy(c->address = new_address); +} +// Submitted code above + +int main(int argc, char **argv) { + struct contact friend; + // set initial values for friend + strcpy(friend.name, ""Jane Doe""); + friend.phone_number = 377177377; + strcpy(friend.address, ""3771 University Avenue""); + + char *name; + int number; + char *address; + // in hidden code set values for these 3 variables + + // call update_contact to change the values in friend + update_contact(&friend, name, number, address); + return 0; +} +// Submitted code below","Given the following program, write the function update_contact which takes a pointer to the struct contact, a new name, a new number, and a new address to change the struct’s members. Look at the function call for the order of arguments and appropriate types. +", +63c647f95f004bf4898cf851,2023-01-26T02:20:33.573Z,Help Fix Code,v1,,"#include +#include +#include + +struct contact { + char name[30]; + int phone_number; + char address[30]; +}; + +// update_contact goes here +// Submitted code below +void update_contact(struct contact c, char *new_name, int new_number, char *new_address) { + strcpy(c->name,new_name); + (*c).phone_number = new_number; + strcpy(c->address,new_address); +} +// Submitted code above + +int main(int argc, char **argv) { + struct contact friend; + // set initial values for friend + strcpy(friend.name, ""Jane Doe""); + friend.phone_number = 377177377; + strcpy(friend.address, ""3771 University Avenue""); + + char *name; + int number; + char *address; + // in hidden code set values for these 3 variables + + // call update_contact to change the values in friend + update_contact(&friend, name, number, address); + return 0; +} +// Submitted code below","Given the following program, write the function update_contact which takes a pointer to the struct contact, a new name, a new number, and a new address to change the struct’s members. Look at the function call for the order of arguments and appropriate types.", +63c647f95f004bf4898cf851,2023-02-13T17:01:17.977Z,Question from Code,v1,What will this declaration line become after the program has passed through the C pre-processor?,"#include +#define MAXNAME = 32; +#declaration +char name[MAXNAME]; + +int main() { + + #ifdef MACRO + printf(""%d\n"", MACRO); + #endif + return 0; +} + + +",, +63c647f95f004bf4898cf851,2023-02-13T17:12:54.370Z,General Question,v1,"Tell me about the C preprocessor +",,, +63c647fa5f004bf4898cf85b,2023-01-22T07:17:55.676Z,General Question,v1,"how can equality of two characters be verified? +",,, +63c647fa5f004bf4898cf85b,2023-01-22T07:19:35.126Z,General Question,v1,"how can a char be converted to an int? +",,, +63c647fa5f004bf4898cf85b,2023-01-22T07:20:30.544Z,Explain Code,v1,,int size = (int) argv[1];,, +63c647fa5f004bf4898cf85b,2023-02-14T22:53:53.964Z,General Question,v1,"How do I print the contents of a file line by line to standard output? +",,, +63c647fa5f004bf4898cf860,2023-01-27T00:24:58.743Z,Help Fix Code,v1,,"void fib(int** array, int n) { + *array = malloc(sizeof(int) * n); + int term1 = 0; + int term2 = 1; + int next; + if (n == 1) { + (*array)[0] = term1; + } else if (n == 2) { + (*array)[0] = term1; + (*array)[1] = term2; + } else { + (*array)[0] = term1; + (*array)[1] = term2; + for (int i = 2; i < n; i++) { + next = term1 + term2; + (*array[i]) = next; + term1 = term2; + term2 = next; + } + } +} + + +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; +}",store up to n fibonacci numbers in a dynamically allocated array and print each element, +63c647fa5f004bf4898cf860,2023-02-03T07:23:34.942Z,General Question,v1,"how do i truncate a string +",,, +63c647fa5f004bf4898cf860,2023-02-03T08:02:13.188Z,Question from Code,v1,what is wrong with my code?,"int strip_q_marks(char* s) { + int count = 0; + for (int i = strlen(s); i == 0; i--) { + if (s[i - 1] == '?' && s[i] == '\0') { + s[i - 1] = '\0'; + count++; + } + } + return count; +}",, +63c647fa5f004bf4898cf860,2023-02-03T20:11:59.396Z,General Question,v1,Use strcat to concatenate a space into a string,,, +63c647fa5f004bf4898cf860,2023-02-10T06:35:06.477Z,General Question,v1,how do I read an int from 4 bytes in a binary file,,, +63c647fa5f004bf4898cf860,2023-02-10T06:45:44.802Z,Help Fix Code,v1,,"void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { + fseek(image, 9, SEEK_SET); + fread(pixel_array_offset, 4, 1, image); + fseek(image, 17, SEEK_SET); + fread(width, 4, 1, image); + fseek(image, 21, SEEK_SET); + fread(height, 4, 1, image); +}",I want to read 4 byte integers from file metadata, +63c647fa5f004bf4898cf860,2023-02-10T06:48:46.385Z,Question from Code,v1,"I want to read integers from byte offsets 10-13, 18-21 and 22-25","void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { + fseek(image, 9, SEEK_SET); + fread(pixel_array_offset, 4, 1, image); + fseek(image, 17, SEEK_SET); + fread(width, 4, 1, image); + fseek(image, 21, SEEK_SET); + fread(height, 4, 1, image); +}",, +63c647fa5f004bf4898cf860,2023-02-10T07:13:05.794Z,General Question,v1,initialize multiple pointers using a for loop,,, +63c647fa5f004bf4898cf860,2023-02-14T08:01:25.711Z,Question from Code,v1,why is demo_user not defined?,"int main() { + struct user demo_user; + int result = create_user(""Dario"", &&demo_user); + printf(""%d\n"", result); + return 0; +}",, +63c647fa5f004bf4898cf860,2023-02-14T08:12:50.010Z,Question from Code,v1,why am i getting a segmentation fault?,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + while (curr->next != NULL) { + if (curr->name == name) { + return 1; + } + curr = curr->next; + } + + if (strlen(name) + 1 > 32) { + return 2; + } + + User new_user; + strcpy(new_user.name, name); + new_user.next = NULL; + curr->next = &new_user; + + return 0; +}",, +63c647fa5f004bf4898cf860,2023-02-14T08:29:07.341Z,Question from Code,v1,why am i getting a segmentation fault?,"int create_user(const char *name, User **user_ptr_add) { + User *curr = malloc(sizeof(User)); + curr = *user_ptr_add; + while (curr->next != NULL) { + if (curr->name == name) { + return 1; + } + curr = curr->next; + } + + if (strlen(name) + 1 > 32) { + return 2; + } + + User *new_user = malloc(sizeof(User)); + strncpy(new_user->name, name, 31); + new_user->next = NULL; + curr->next = new_user; + + return 0; +}",, +63c647fa5f004bf4898cf860,2023-02-14T08:34:55.057Z,Question from Code,v1,why am i getting a segmentation fault?,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + while (curr->next != NULL) { + if (curr->name == name) { + return 1; + } + curr = curr->next; + } + + if (strlen(name) + 1 > 32) { + return 2; + } + + User *new_user = malloc(sizeof(User)); + strncpy(new_user->name, name, 31); + new_user->next = NULL; + curr->next = new_user; + + return 0; +}",, +63c647fa5f004bf4898cf860,2023-02-14T18:14:51.708Z,General Question,v1,how do I insert an element into the first empty spot of an array?,,, +63c647fa5f004bf4898cf860,2023-02-15T03:09:52.992Z,Question from Code,v1,"why am i getting a segmentation fault +","int make_friends(const char *name1, const char *name2, User *head) { + if (strcmp(name1, name2) == 0) { + return 3; + } + + User *user1 = malloc(sizeof(User)); + User *user2 = malloc(sizeof(User)); + user1 = find_user(name1, head); + user2 = find_user(name2, head); + + if (user1 == NULL || user2 == NULL) { + return 4; + } + + int u1_friends_w_u2 = 0; + int u2_friends_w_u1 = 0; + + for (int i = 0; i < 10; i++) { + if (user1->friends[i] == user2) { + u1_friends_w_u2 = 1; + } + } + + for (int i = 0; i < 10; i++) { + if (user2->friends[i] == user1) { + u2_friends_w_u1 = 1; + } + } + + if (u1_friends_w_u2 == 1 && u2_friends_w_u1 == 1) { + return 1; + } + + // By now the check for if they are already friends has happened, so this should test not friends and if at max capcity + if (sizeof(user1->friends) == 10 || sizeof(user2->friends) == 10) { + return 2; + } + + for (int i = 0; i < 10; i++) { + if (user1->friends[i] == NULL) { + user1->friends[i] = user2; + break; + } + } + + for (int i = 0; i < 10; i++) { + if (user2->friends[i] == NULL) { + user2->friends[i] = user1; + break; + } + } + + return 0; +} +",, +63c647fa5f004bf4898cf860,2023-02-15T03:18:26.201Z,Question from Code,v1,why am i getting a segmentation fault,"int make_friends(const char *name1, const char *name2, User *head) { + if (strcmp(name1, name2) == 0) { + return 3; + } + + User *user1 = malloc(sizeof(User)); + User *user2 = malloc(sizeof(User)); + user1 = find_user(name1, head); + user2 = find_user(name2, head); + + if (user1 == NULL || user2 == NULL) { + return 4; + } + + int u1_friends_w_u2 = 0; + int u1_friend_counter = 0; + int u2_friends_w_u1 = 0; + int u2_friend_counter = 0; + + for (int i = 0; i < 10; i++) { + if (user1->friends[i] == user2) { + u1_friends_w_u2 = 1; + } else if (user1->friends[i] != NULL) { + u1_friend_counter++; + } + } + + for (int i = 0; i < 10; i++) { + if (user2->friends[i] == user1) { + u2_friends_w_u1 = 1; + } else if (user2->friends[i] != NULL) { + u2_friend_counter++; + } + } + + if (u1_friends_w_u2 == 1 && u2_friends_w_u1 == 1) { + return 1; + } + + // By now the check for if they are already friends has happened, so this should test not friends and if at max capcity + if (u1_friend_counter == 10 || u2_friend_counter == 10) { + return 2; + } + + for (int i = 0; i < 10; i++) { + if (user1->friends[i] == NULL) { + user1->friends[i] = user2; + break; + } + } + + for (int i = 0; i < 10; i++) { + if (user2->friends[i] == NULL) { + user2->friends[i] = user1; + break; + } + } + + return 0; +}",, +63c647fa5f004bf4898cf860,2023-02-15T03:46:40.447Z,Question from Code,v1,"why am i getting a segmentation fault +","int make_friends(const char *name1, const char *name2, User *head) { + if (strcmp(name1, name2) == 0) { + return 3; + } + + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + + if (user1 == NULL || user2 == NULL) { + return 4; + } + + int u1_friends_w_u2 = 0; + int u1_friend_counter = 0; + int u2_friends_w_u1 = 0; + int u2_friend_counter = 0; + + for (int i = 0; i < 10; i++) { + if (user1->friends[i] == user2) { + u1_friends_w_u2 = 1; + } else if (user1->friends[i] != NULL) { + u1_friend_counter++; + } + } + + for (int i = 0; i < 10; i++) { + if (user2->friends[i] == user1) { + u2_friends_w_u1 = 1; + } else if (user2->friends[i] != NULL) { + u2_friend_counter++; + } + } + + if (u1_friends_w_u2 == 1 && u2_friends_w_u1 == 1) { + return 1; + } + + // By now the check for if they are already friends has happened, so this should test not friends and if at max capcity + if (u1_friend_counter == 10 || u2_friend_counter == 10) { + return 2; + } + + for (int i = 0; i < 10; i++) { + if (user1->friends[i] == NULL) { + user1->friends[i] = user2; + break; + } + } + + for (int i = 0; i < 10; i++) { + if (user2->friends[i] == NULL) { + user2->friends[i] = user1; + break; + } + } + + return 0; +}",, +63c647fa5f004bf4898cf860,2023-02-15T05:55:17.095Z,General Question,v1,how to read ascii art ,,, +63c647fa5f004bf4898cf860,2023-02-15T07:26:03.965Z,General Question,v1,how to null terminate a string resulting from strncpy,,, +63c647fa5f004bf4898cf860,2023-02-15T07:40:32.315Z,General Question,v1,"how to use time function +",,, +63c647fa5f004bf4898cf860,2023-02-15T07:52:30.715Z,Question from Code,v1,why do i get a warning for this code,"strcpy(new_post->contents, contents);",, +63c647fa5f004bf4898cf860,2023-02-15T07:53:00.303Z,Question from Code,v1,"why do i get a maybe uninitialized warning +","strcpy(new_post->contents, contents);",, +63c647fa5f004bf4898cf860,2023-02-15T07:53:49.894Z,Question from Code,v1,how do i fix the maybe uninitialized warning,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + // Can't use friends function to check because there is no head passed in. We only need to check for friendship one way as it is symmetrical. + int auth_friends_w_target = 0; + + for (int i = 0; i < 10; i++) { + if (author->friends[i] == target) { + auth_friends_w_target = 1; + } + } + + if (!auth_friends_w_target) { + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + if (new_post == NULL) { + perror(""malloc""); + exit(-1); + } + // could have errors with null terminator + strncpy(new_post->author, author->name, 31); + strcpy(new_post->contents, contents); + + time_t current_time; + time_t non_readable_time = time(¤t_time); + + new_post->date = &non_readable_time; + return 0; +}",, +63c647fa5f004bf4898cf860,2023-02-15T18:16:01.874Z,General Question,v1,how do i malloc space for size 10 array,,, +63c647fb5f004bf4898cf865,2023-01-25T21:32:11.879Z,General Question,v1,"how to access one char of a string when we have the pointer to that string only +",,, +63c647fb5f004bf4898cf865,2023-01-25T21:34:35.584Z,Question from Code,v1,"how can i fix the error in line 3 +","int test(char *a, char*b){ + for(int i; i<9; i++){ + if(*b[i] != ""-"" && *b[i] != *a[i]){ + return 1; + } + + } + return 0; +} + +int main(int argc, char** argv){ + return test(argv[1], argv[2]); +}",, +63c647fb5f004bf4898cf865,2023-01-25T21:40:27.708Z,General Question,v1,how to pass command line arguements to another function one by one,,, +63c647fb5f004bf4898cf865,2023-01-28T22:01:16.676Z,General Question,v1,how to calculate the number of digits in an int,,, +63c647fb5f004bf4898cf865,2023-01-28T22:06:56.037Z,General Question,v1,how to perform integer division in c,,, +63c647fb5f004bf4898cf865,2023-01-28T22:07:17.894Z,General Question,v1,what is 9/2 in c,,, +63c647fb5f004bf4898cf865,2023-01-28T22:09:43.447Z,General Question,v1,how to get the number from i-th place in an integer,,, +63c647fb5f004bf4898cf865,2023-01-28T22:14:44.655Z,Question from Code,v1,what is the output of this code,"#include + +int main() { + int number = 12345; + int i; + for (i = 0; i < 5; i++) { + printf(""%d"", (number / (10^i)) % 10); + } + return 0; +}",, +63c647fb5f004bf4898cf865,2023-01-28T22:17:44.582Z,Question from Code,v1,what is the output when this code is run with arguements sin = 123456789 and sin_array is an integer array of 9 integers,"int populate_array(int sin, int *sin_array) { + int number_of_digits = 0; + while(sin != 0){ + sin = sin / 10; + number_of_digits ++; + } + + if(number_of_digits != 9){ + return 1; + } else { + for(int i = 0; i < 9; i++){ + sin_array[i] = (sin / (10^(8 - i))) % 10; + } + } +}",, +63c647fb5f004bf4898cf865,2023-01-28T22:18:16.805Z,Question from Code,v1,what is the output when this code is run with arguements sin = 012345678 and sin_array is an integer array of 9 integers,"int populate_array(int sin, int *sin_array) { + int number_of_digits = 0; + while(sin != 0){ + sin = sin / 10; + number_of_digits ++; + } + + if(number_of_digits != 9){ + return 1; + } else { + for(int i = 0; i < 9; i++){ + sin_array[i] = (sin / (10^(8 - i))) % 10; + } + } +}",, +63c647fb5f004bf4898cf865,2023-01-28T22:34:26.134Z,General Question,v1,how to get the i-th digit of a 2 digit number,,, +63c647fb5f004bf4898cf865,2023-01-28T22:37:40.421Z,Question from Code,v1,"what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 6}","int check_sin(int *sin_array) { + int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1} + int total = 0; + for(int i = 0; i<9; i++){ + int temp = sin_array[i] * mult_array[i] + if(temp / 10 == 0){ + total += temp; + } else { + int one = temp / 10; + int two = temp % 10; + total += (one * two); + } + } + if(total % 10 == 0){ + return 0; + } else { + return 1; + } +}",, +63c647fb5f004bf4898cf865,2023-01-28T22:38:24.961Z,Question from Code,v1,"what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 7}","int check_sin(int *sin_array) { + int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1} + int total = 0; + for(int i = 0; i<9; i++){ + int temp = sin_array[i] * mult_array[i] + if(temp / 10 == 0){ + total += temp; + } else { + int one = temp / 10; + int two = temp % 10; + total += (one * two); + } + } + if(total % 10 == 0){ + return 0; + } else { + return 1; + } +}",, +63c647fb5f004bf4898cf865,2023-01-28T22:39:18.245Z,Question from Code,v1,"what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 7} and the value of total","int check_sin(int *sin_array) { + int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1} + int total = 0; + for(int i = 0; i<9; i++){ + int temp = sin_array[i] * mult_array[i] + if(temp / 10 == 0){ + total += temp; + } else { + int one = temp / 10; + int two = temp % 10; + total += (one * two); + } + } + if(total % 10 == 0){ + return 0; + } else { + return 1; + } +}",, +63c647fb5f004bf4898cf865,2023-01-28T22:40:47.223Z,Question from Code,v1,"what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 6} and the value of total","int check_sin(int *sin_array) { + int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1} + int total = 0; + for(int i = 0; i<9; i++){ + int temp = sin_array[i] * mult_array[i] + if(temp / 10 == 0){ + total += temp; + } else { + int one = temp / 10; + int two = temp % 10; + total += (one * two); + } + } + if(total % 10 == 0){ + return 0; + } else { + return 1; + } +}",, +63c647fb5f004bf4898cf865,2023-01-28T22:42:51.630Z,Question from Code,v1,"what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 6} and the value of total","int check_sin(int *sin_array) { + int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1} + int total = 0; + for(int i = 0; i<9; i++){ + if((sin_array[i] * mult_array[i]) / 10 == 0){ + total += (sin_array[i] * mult_array[i]); + } else { + int one = (sin_array[i] * mult_array[i]) / 10; + int two = (sin_array[i] * mult_array[i]) % 10; + total += (one * two); + } + } + + if(total % 10 == 0){ + return 0; + } else { + return 1; + } +}",, +63c647fb5f004bf4898cf865,2023-01-28T22:45:51.260Z,Question from Code,v1,"what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 6} and the value of total","int check_sin(int *sin_array) { + int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1} + int total = 0; + for(int i = 0; i<9; i++){ + if((sin_array[i] * mult_array[i]) / 10 == 0){ + total += (sin_array[i] * mult_array[i]); + } else { + int one = (sin_array[i] * mult_array[i]) / 10; + int two = (sin_array[i] * mult_array[i]) % 10; + total += (one + two); + } + } + + if(total % 10 == 0){ + return 0; + } else { + return 1; + } +}",, +63c647fb5f004bf4898cf865,2023-01-28T22:47:10.377Z,Question from Code,v1,"what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 7} and the value of total","int check_sin(int *sin_array) { + int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1} + int total = 0; + for(int i = 0; i<9; i++){ + if((sin_array[i] * mult_array[i]) / 10 == 0){ + total += (sin_array[i] * mult_array[i]); + } else { + int one = (sin_array[i] * mult_array[i]) / 10; + int two = (sin_array[i] * mult_array[i]) % 10; + total += (one + two); + } + } + + if(total % 10 == 0){ + return 0; + } else { + return 1; + } +}",, +63c647fb5f004bf4898cf865,2023-01-28T22:49:31.646Z,Explain Code,v1,,"int check_sin(int *sin_array) { + int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1} + int total = 0; + for(int i = 0; i<9; i++){ + if((sin_array[i] * mult_array[i]) / 10 == 0){ + total += (sin_array[i] * mult_array[i]); + } else { + int one = (sin_array[i] * mult_array[i]) / 10; + int two = (sin_array[i] * mult_array[i]) % 10; + total += (one + two); + } + } + + if(total % 10 == 0){ + return 0; + } else { + return 1; + } +}",, +63c647fb5f004bf4898cf865,2023-01-28T22:59:33.475Z,Question from Code,v1,"what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 7} and the value of total","int check_sin(int *sin_array) { +check_sin takes an array of integers as an argument + int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1}; + int total = 0; + for(int i = 0; i<9; i++){ + if((sin_array[i] * mult_array[i]) / 10 == 0){ + total += (sin_array[i] * mult_array[i]); + } else { + int one = (sin_array[i] * mult_array[i]) / 10; + int two = (sin_array[i] * mult_array[i]) % 10; + total += (one + two); + } + } + + if(total % 10 == 0){ + return 0; + } else { + return 1; + } +}",, +63c647fb5f004bf4898cf865,2023-01-28T23:00:28.109Z,Question from Code,v1,"what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 7} and the value of total. can i track every step","int check_sin(int *sin_array) { +check_sin takes an array of integers as an argument + int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1}; + int total = 0; + for(int i = 0; i<9; i++){ + if((sin_array[i] * mult_array[i]) / 10 == 0){ + total += (sin_array[i] * mult_array[i]); + } else { + int one = (sin_array[i] * mult_array[i]) / 10; + int two = (sin_array[i] * mult_array[i]) % 10; + total += (one + two); + } + } + + if(total % 10 == 0){ + return 0; + } else { + return 1; + } +}",, +63c647fb5f004bf4898cf865,2023-01-28T23:01:41.994Z,Question from Code,v1,"what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 7} and the value of total","int check_sin(int *sin_array) { +check_sin takes an array of integers as an argument + int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1}; + int total = 0; + for(int i = 0; i<9; i++){ + if((sin_array[i] * mult_array[i]) < 10 == 0){ + total += (sin_array[i] * mult_array[i]); + } else { + int one = (sin_array[i] * mult_array[i]) / 10; + int two = (sin_array[i] * mult_array[i]) % 10; + total += (one + two); + } + } + + if(total % 10 == 0){ + return 0; + } else { + return 1; + } +}",, +63c647fb5f004bf4898cf865,2023-01-28T23:05:39.666Z,General Question,v1,how to initialize an array of integers in c,,, +63c647fb5f004bf4898cf865,2023-01-28T23:40:41.355Z,General Question,v1,what does populating an integer array mean,,, +63c647fb5f004bf4898cf865,2023-01-28T23:41:27.623Z,General Question,v1,how do i change a global array inside another function and retain the changes,,, +63c647fb5f004bf4898cf865,2023-01-29T02:16:45.289Z,General Question,v1,"how to differentiate between files and directories +",,, +63c647fb5f004bf4898cf865,2023-01-29T03:03:24.126Z,Question from Code,v1,how do i change sin_array initialisation or modification so the changes made to it in populate_array are global changes so i can use the modified sin_array in check_sin,"#include +#include + +// TODO: Implement populate_array +/* + * Convert a 9 digit int to a 9 element int array. + */ +int populate_array(int sin, int *sin_array) { + int number_of_digits = 0; + while(sin != 0){ + sin = sin / 10; + number_of_digits ++; + } + + if(number_of_digits != 9){ + return 1; + } else { + for(int i = 0; i < 9; i++){ + sin_array[i] = (sin / (10^(8 - i))) % 10; + } + return 0; + } +} + +// TODO: Implement check_sin +/* + * Return 0 if the given sin_array is a valid SIN, and 1 otherwise. + */ +int check_sin(int *sin_array) { + printf(""%d"", sin_array[0]); + int mult_array[9] = {1, 2, 1, 2, 1, 2, 1, 2, 1}; + int total = 0; + for(int i = 0; i<9; i++){ + if((sin_array[i] * mult_array[i]) / 10 == 0){ + total += (sin_array[i] * mult_array[i]); + printf(""%d\n"", sin_array[i]*mult_array[i]); + } else { + int one = (sin_array[i] * mult_array[i]) / 10; + int two = (sin_array[i] * mult_array[i]) % 10; + total += (one + two); + printf(""%d\n"", sin_array[i]*mult_array[i]); + } + } + printf(""%d\n"", total); + if(total % 10 == 0){ + return 0; + } else { + return 1; + } +} + +int populate_array(int, int *); +int check_sin(int *); + + +int main(int argc, char **argv) { + // TODO: Verify that command line arguments are valid. + + // TODO: Parse arguments and then call the two helpers in sin_helpers.c + // to verify the SIN given as a command line argument. + + if(argc != 2){ + return 2; + } else { + int sin_array[9]; + int is_nine = populate_array(atoi(argv[1]), sin_array); + if(is_nine == 1){ + printf(""%s"", ""Invalid SIN\n""); + return 1; + } + + if(check_sin(sin_array) == 0){ + printf(""%s"",""Valid SIN\n""); + return 0; + } else { + printf(""%s"",""Invalid SIN\n""); + return 1; + } + } + + +}",, +63c647fb5f004bf4898cf865,2023-01-29T22:21:41.389Z,General Question,v1,"what is the result of atoi(""012345"")",,, +63c647fb5f004bf4898cf865,2023-01-31T19:13:11.357Z,General Question,v1,how to make a function take a non negative int as one parameter,,, +63c647fb5f004bf4898cf865,2023-01-31T20:00:00.608Z,General Question,v1,how to check if a char is equal to a ?,,, +63c647fb5f004bf4898cf865,2023-01-31T22:06:30.496Z,Question from Code,v1,what is the value of greeting when the piece of code is run.,"char greeting[20]; +strncpy(greeting, ""Top of the morning to you"", sizeof(greeting) - 1);",, +63c647fb5f004bf4898cf865,2023-01-31T22:07:04.731Z,Question from Code,v1,what is the sizeof(greeting),"char greeting[20]; +strncpy(greeting, ""Top of the morning to you"", sizeof(greeting) - 1);",, +63c647fb5f004bf4898cf865,2023-01-31T22:07:31.681Z,Question from Code,v1,what is the value of greeting when the piece of code is run,"char greeting[20]; +strncpy(greeting, ""Top of the morning to you"", 19);",, +63c647fb5f004bf4898cf865,2023-02-08T07:11:41.645Z,General Question,v1,how to store something read by fread() in a variable,,, +63c647fb5f004bf4898cf865,2023-02-08T07:21:40.783Z,General Question,v1,how to initialise space for 10 structs at once,,, +63c647fb5f004bf4898cf865,2023-02-12T20:18:56.498Z,General Question,v1,how to find the length of a string in c,,, +63c647fb5f004bf4898cf865,2023-02-12T20:20:10.261Z,Question from Code,v1,what is sizeof(Kaavya),"char *name = ""Kaavya""",, +63c647fb5f004bf4898cf865,2023-02-12T20:20:42.831Z,Question from Code,v1,what is sizeof(name),"char *name = ""Kaavya""",, +63c647fb5f004bf4898cf865,2023-02-12T20:21:11.328Z,Question from Code,v1,what is sizeof(*name),"char *name = ""Kaavya""",, +63c647fb5f004bf4898cf865,2023-02-12T20:21:53.200Z,Question from Code,v1,what is the output of sizeof(*name),"char *name = ""Kaavya""",, +63c647fb5f004bf4898cf865,2023-02-12T20:22:28.603Z,General Question,v1,does something like strlen exist in c,,, +63c647fb5f004bf4898cf865,2023-02-12T20:22:52.971Z,Question from Code,v1,what is strlen(name),"char *name = ""Kaavya""",, +63c647fb5f004bf4898cf865,2023-02-12T20:26:25.539Z,General Question,v1,syntax of strcmp,,, +63c647fb5f004bf4898cf865,2023-02-12T20:28:34.831Z,General Question,v1,how to check if a current item is the last node of a linked list in c,,, +63c647fb5f004bf4898cf865,2023-02-12T21:04:03.595Z,General Question,v1,how to create an instance of am empty struct,,, +63c647fb5f004bf4898cf865,2023-02-12T21:05:30.037Z,General Question,v1,"syntax of strcpy +",,, +63c647fb5f004bf4898cf865,2023-02-13T00:01:14.280Z,General Question,v1,"how to cast datatypes in c +",,, +63c647fb5f004bf4898cf865,2023-02-13T00:04:08.509Z,General Question,v1,how to cast (const User *head) from a (const User *) to a (User *),,, +63c647fb5f004bf4898cf865,2023-02-13T00:19:33.750Z,Question from Code,v1,how do i return a pointer to the value in curr in the original linked list instead of returning the address of the local variable pointer curr,"User *find_user(const char *name, const User *head) { + User *user_head = (User *) head; + if(user_head == NULL){ + return NULL; + } else { + User curr = user_head[0]; + while(curr != NULL){ + if(strcmp(curr.name, name) == 0){ + return &curr; + } else { + curr = *(curr.next); + } + } + } + return NULL; +}",, +63c647fb5f004bf4898cf865,2023-02-13T00:25:23.705Z,General Question,v1,usage of -> in structs,,, +63c647fb5f004bf4898cf865,2023-02-13T01:22:07.133Z,Question from Code,v1,I want to return a pointer to the original User struct curr. How do i do that,"User *find_user(const char *name, const User *head) { + User *user_head = (User *) head; + if(user_head == NULL){ + return NULL; + } else { + User curr = user_head[0]; + while(curr.next != NULL){ + if(strcmp(curr.name, name) == 0){ + return curr; + } else { + curr = *(curr.next); + } + } + if(strcmp(curr.name, name) == 0){ + return curr; + } + + } + return NULL; +}",, +63c647fb5f004bf4898cf865,2023-02-13T01:22:59.923Z,Question from Code,v1,"I want to return a pointer to the original User struct curr. How do i do that. +I cannot use &curr as it gives me the error that i am returning a local variable","User *find_user(const char *name, const User *head) { + User *user_head = (User *) head; + if(user_head == NULL){ + return NULL; + } else { + User curr = user_head[0]; + while(curr.next != NULL){ + if(strcmp(curr.name, name) == 0){ + return curr; + } else { + curr = *(curr.next); + } + } + if(strcmp(curr.name, name) == 0){ + return curr; + } + + } + return NULL; +}",, +63c647fb5f004bf4898cf865,2023-02-13T01:23:39.509Z,Question from Code,v1,"I want to return a pointer to the original User struct curr. How do i do that. I cannot use &curr as it gives me the error that i am returning a local variable +How do I use malloc for this?","User *find_user(const char *name, const User *head) { + User *user_head = (User *) head; + if(user_head == NULL){ + return NULL; + } else { + User curr = user_head[0]; + while(curr.next != NULL){ + if(strcmp(curr.name, name) == 0){ + return curr; + } else { + curr = *(curr.next); + } + } + if(strcmp(curr.name, name) == 0){ + return curr; + } + + } + return NULL; +}",, +63c647fb5f004bf4898cf865,2023-02-13T01:26:18.026Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + User *user_head = (User *) head; + if(user_head == NULL){ + return NULL; + } else { + User curr = user_head[0]; + while(curr.next != NULL){ + if(strcmp(curr.name, name) == 0){ + return curr; + } else { + curr = *(curr.next); + } + } + if(strcmp(curr.name, name) == 0){ + return curr; + } + + } + return NULL; +}",i need to return a pointer to the user (the one in curr), +63c647fb5f004bf4898cf865,2023-02-13T01:27:07.927Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + User *user_head = (User *) head; + if(user_head == NULL){ + return NULL; + } else { + User curr = user_head[0]; + while(curr.next != NULL){ + if(strcmp(curr.name, name) == 0){ + return curr; + } else { + curr = *(curr.next); + } + } + if(strcmp(curr.name, name) == 0){ + return curr; + } + + } + return NULL; +}",i need to return a pointer to the user (the one in curr). cannot use &curr as I cannot return a local variable, +63c647fb5f004bf4898cf865,2023-02-13T01:59:19.963Z,General Question,v1,what is the syntax for calloc and an example,,, +63c647fb5f004bf4898cf865,2023-02-13T03:33:21.989Z,General Question,v1,is there an equivalent of count feature in C?,,, +63c647fb5f004bf4898cf865,2023-02-13T03:39:23.397Z,General Question,v1,how to initialise a boolean value,,, +63c647fb5f004bf4898cf865,2023-02-13T03:45:48.596Z,General Question,v1,"when u initialise an array of pointers of length 32, what are all elements by default set to",,, +63c647fb5f004bf4898cf865,2023-02-13T04:12:56.792Z,General Question,v1,how to printf a combination of strings and variables,,, +63c647fb5f004bf4898cf865,2023-02-13T04:19:23.768Z,General Question,v1,how to read a ascii file ,,, +63c647fb5f004bf4898cf865,2023-02-13T04:26:29.036Z,General Question,v1,equivalent of break in c,,, +63c647fb5f004bf4898cf865,2023-02-13T05:52:58.993Z,Question from Code,v1,what leads to a segmentation fault in make_post,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int make_post(const User *author, User *target, char *contents) { + if((author == NULL) || (target == NULL)){ + return 2; + } else { + int friends = 0; + for(int i = 0; i < MAX_FRIENDS; i++){ + if((author->friends)[i] == target){ + friends = 1; + } + } + + if(friends == 0){ + return 1; + } + + Post *new_post = calloc(1, sizeof(Post)); + strcpy(new_post->author, author->name); + strcpy(new_post->contents, contents); + *(new_post->date) = time(NULL); + + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; + } +}",, +63c647fb5f004bf4898cf865,2023-02-13T05:56:09.290Z,General Question,v1,how to set char *contents to char *contentt; ,,, +63c647fb5f004bf4898cf865,2023-02-13T06:00:48.855Z,Question from Code,v1,what leads to a segmentation fault in make_post,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int make_post(const User *author, User *target, char *contents) { + if((author == NULL) || (target == NULL)){ + return 2; + } else { + int friends = 0; + for(int i = 0; i < MAX_FRIENDS; i++){ + if((author->friends)[i] == target){ + friends = 1; + } + } + + if(friends == 0){ + return 1; + } + + Post *new_post = calloc(1, sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + *(new_post->date) = time(NULL); + + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; + } +}",, +63c647fb5f004bf4898cf865,2023-02-13T06:13:24.312Z,General Question,v1,what is time(NULL) output,,, +63c647fb5f004bf4898cf865,2023-02-13T06:15:11.068Z,Question from Code,v1,what will be the output of ctime(time_address),"time_t post_time = time(NULL); +time_t *time_address = &post_time; +",, +63c647fb5f004bf4898cf865,2023-02-13T06:28:45.993Z,Question from Code,v1,why is the date not being printed properly in print_user,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int make_post(const User *author, User *target, char *contents) { + if((author == NULL) || (target == NULL)){ + return 2; + } else { + int friends = 0; + for(int i = 0; i < MAX_FRIENDS; i++){ + if((author->friends)[i] == target){ + friends = 1; + } + } + if(friends == 0){ + return 1; + } + + Post *new_post = calloc(1, sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + time_t post_time = time(NULL); + new_post->date = &post_time; + + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; + } +} + +int print_user(const User *user) { + if(user == NULL){ + return 1; + } else { + if(user->profile_pic != NULL){ + ; + //display pp + } + printf(""%s%s\n"", ""Name: "", user->name); + printf(""%s\n"", ""------------------------------------------""); + printf(""%s\n"", ""Friends:""); + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user->friends)[i] != NULL){ + printf(""%s\n"", ((user->friends)[i])->name); + } + } + printf(""%s\n"", ""------------------------------------------""); + printf(""%s\n"", ""Posts:""); + if(user->first_post == NULL){ + ; + } else { + Post curr_post = (user->first_post)[0]; + while(curr_post.next != NULL){ + printf(""%s%s\n"", ""From: "", curr_post.author); + printf(""%s%s\n\n"", ""Date: "", ctime(curr_post.date)); + printf(""%s\n"", curr_post.contents); + printf(""\n%s\n\n"", ""===""); + curr_post = *(curr_post.next); + } + printf(""%s%s\n"", ""From: "", curr_post.author); + printf(""%s%s\n\n"", ""Date: "", ctime(curr_post.date)); + printf(""%s\n"", curr_post.contents); + + } + printf(""%s\n"", ""------------------------------------------""); + return 0; + } +}",, +63c647fb5f004bf4898cf865,2023-02-13T06:37:23.095Z,General Question,v1,"how to allocate space for time(NULL) +",,, +63c647fb5f004bf4898cf865,2023-02-13T07:11:34.653Z,General Question,v1,fscanf vs fgets,,, +63c647fb5f004bf4898cf865,2023-02-13T18:05:51.843Z,General Question,v1,do you have to allocate memory for individual attributes of structs as well to avoid memory leak,,, +63c647fb5f004bf4898cf865,2023-02-13T18:40:09.495Z,Question from Code,v1,how to allocate space for each item in struct,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User;",, +63c647fb5f004bf4898cf865,2023-02-13T18:41:11.373Z,Question from Code,v1,do i need to allocate space for every attribute separately even if i allocate space with sizeof(User),"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User;",, +63c647fb5f004bf4898cf865,2023-02-13T19:55:09.580Z,Question from Code,v1,WHERE IS MEMORY LEAKING,"#include ""friends.h"" +#include +#include +#include + +/* + * 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){ + return 2; + } + User *new_node = calloc(1, sizeof(User)); + + strcpy(new_node->name, name); + + if(user_ptr_add[0] == NULL){ + user_ptr_add[0] = new_node; + } else { + User *curr = user_ptr_add[0]; + while(curr->next != NULL){ + if(strcmp((*curr).name, name) == 0){ + return 1; + } else { + curr = curr->next; + } + } + if(strcmp((*curr).name, name) == 0){ + return 1; + } else { + curr->next = new_node; + } + } + 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 *user_head = (User *) head; + if(user_head == NULL){ + return NULL; + } else { + User *curr = user_head; + while(curr->next != NULL){ + if(strcmp((*curr).name, name) == 0){ + return curr; + } else { + curr = curr->next; + } + } + if(strcmp((*curr).name, name) == 0){ + return curr; + } + } + 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 *user_curr = (User *) curr; + printf(""%s\n"", ""User List""); + if(user_curr == NULL){ + ; + } else { + User curr_user = user_curr[0]; + while(curr_user.next != NULL){ + printf(""\t%s\n"", curr_user.name); + curr_user = *(curr_user.next); + } + printf(""\t%s\n"", curr_user.name); + } +} + + +/* + * 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) >= MAX_NAME){ + return 2; + } else if((fopen(filename, ""r"")) == NULL){ + return 1; + } else { + strcpy(user->profile_pic, filename); + return 0; + } +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int make_friends(const char *name1, const char *name2, User *head) { + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + if((find_user(name1, head) == NULL) || (find_user(name2, head) == NULL)){ + return 4; + } else if(strcmp(name1, name2) == 0){ + return 3; + } else { + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == user2){ + return 1; + } + } + + int not_max = 0; + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == NULL){ + not_max = 1; + } + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user2->friends)[i] == NULL){ + not_max = 1; + } + } + + if(not_max==0){ + return 2; + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == NULL){ + (user1->friends)[i] = user2; + break; + } + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user2->friends)[i] == NULL){ + (user2->friends)[i] = user1; + break; + } + } + + return 0; + } +} + + +/* + * 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; + } else { + if(user->profile_pic != NULL){ + FILE *pic_file; + char line[100]; + pic_file = fopen(user->profile_pic, ""r""); + if (pic_file != NULL){ + while (fgets(line, 100, pic_file) != NULL) { + printf(""%s"", line); + } + fclose(pic_file); + printf(""\n""); + } + + } + + printf(""%s%s\n"", ""Name: "", user->name); + printf(""%s\n"", ""------------------------------------------""); + printf(""%s\n"", ""Friends:""); + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user->friends)[i] != NULL){ + printf(""%s\n"", ((user->friends)[i])->name); + } + } + printf(""%s\n"", ""------------------------------------------""); + printf(""%s\n"", ""Posts:""); + if(user->first_post == NULL){ + ; + } else { + Post *curr_post = (user->first_post); + while(curr_post->next != NULL){ + printf(""%s%s\n"", ""From: "", curr_post->author); + printf(""%s%s\n"", ""Date: "", ctime(curr_post->date)); + printf(""%s\n"", curr_post->contents); + printf(""\n%s\n\n"", ""===""); + curr_post = curr_post->next; + } + printf(""%s%s\n"", ""From: "", curr_post->author); + printf(""%s%s\n"", ""Date: "", ctime(curr_post->date)); + printf(""%s\n"", curr_post->contents); + + } + printf(""%s\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; + } else { + int friends = 0; + for(int i = 0; i < MAX_FRIENDS; i++){ + if((author->friends)[i] == target){ + friends = 1; + } + } + if(friends == 0){ + return 1; + } + + Post *new_post = calloc(1, sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + time_t *post_time = malloc(sizeof(time_t)); + *post_time = time(NULL); + new_post->date = post_time; + + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; + } +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ +int delete_user(const char *name, User **user_ptr_del) { + if(find_user(name, user_ptr_del[0]) == NULL){ + return 1; + } else { + User *user_to_delete = find_user(name, user_ptr_del[0]); + + //remove as friend + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user_to_delete->friends)[i] != NULL){ + User *friend_user = find_user((user_to_delete->friends)[i]->name, user_ptr_del[0]); + for(int i = 0; i < MAX_FRIENDS; i++){ + if(((friend_user->friends)[i] != NULL) && (friend_user->friends)[i] == user_to_delete){ + friend_user->friends[i] = NULL; + } + } + } + } + + //delete the user from the linked list + User *curr = user_ptr_del[0]; + while((curr->next != NULL) && (curr->next) != user_to_delete){ + curr = curr->next; + } + + curr->next = user_to_delete->next; + + //free all memory related to the user + //free all time allocs in each post (User->Post->Time) + //free all allocs for each post (User->Post) + Post *curr_post = user_to_delete->first_post; + while(curr_post != NULL){ + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + free(user_to_delete); + return 0; + } +} + +#include +#include +#include +#include ""friends.h"" + +#define INPUT_BUFFER_SIZE 256 +#define INPUT_ARG_MAX_NUM 12 +#define DELIM "" \n"" + + +/* + * Print a formatted error message to stderr. + */ +void error(char *msg) { + fprintf(stderr, ""Error: %s\n"", msg); +} + +/* + * Read and process commands + * Return: -1 for quit command + * 0 otherwise + */ +int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) { + User *user_list = *user_list_ptr; + + if (cmd_argc <= 0) { + return 0; + } else if (strcmp(cmd_argv[0], ""quit"") == 0 && cmd_argc == 1) { + return -1; + } else if (strcmp(cmd_argv[0], ""add_user"") == 0 && cmd_argc == 2) { + switch (create_user(cmd_argv[1], user_list_ptr)) { + case 1: + error(""user by this name already exists""); + break; + case 2: + error(""username is too long""); + break; + } + } else if (strcmp(cmd_argv[0], ""list_users"") == 0 && cmd_argc == 1) { + list_users(user_list); + } else if (strcmp(cmd_argv[0], ""update_pic"") == 0 && cmd_argc == 3) { + User *user = find_user(cmd_argv[1], user_list); + if (user == NULL) { + error(""user not found""); + } else { + switch (update_pic(user, cmd_argv[2])) { + case 1: + error(""file not found""); + break; + case 2: + error(""filename too long""); + break; + } + } + } else if (strcmp(cmd_argv[0], ""delete_user"") == 0 && cmd_argc == 2) { + if (delete_user(cmd_argv[1], user_list_ptr) == 1) { + error(""user by this name does not exist""); + } + } else if (strcmp(cmd_argv[0], ""make_friends"") == 0 && cmd_argc == 3) { + switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) { + case 1: + error(""users are already friends""); + break; + case 2: + error(""at least one user you entered has the max number of friends""); + break; + case 3: + error(""you must enter two different users""); + break; + case 4: + error(""at least one user you entered does not exist""); + break; + } + } else if (strcmp(cmd_argv[0], ""post"") == 0 && cmd_argc >= 4) { + // first determine how long a string we need + int space_needed = 0; + for (int i = 3; i < cmd_argc; i++) { + space_needed += strlen(cmd_argv[i]) + 1; + } + + // allocate the space + char *contents = malloc(space_needed); + + // copy in the bits to make a single string + strcpy(contents, cmd_argv[3]); + for (int i = 4; i < cmd_argc; i++) { + strcat(contents, "" ""); + strcat(contents, cmd_argv[i]); + } + + User *author = find_user(cmd_argv[1], user_list); + User *target = find_user(cmd_argv[2], user_list); + switch (make_post(author, target, contents)) { + case 1: + error(""the users are not friends""); + break; + case 2: + error(""at least one user you entered does not exist""); + break; + } + } else if (strcmp(cmd_argv[0], ""profile"") == 0 && cmd_argc == 2) { + User *user = find_user(cmd_argv[1], user_list); + if (print_user(user) == 1) { + error(""user not found""); + } + } else { + error(""Incorrect syntax""); + } + return 0; +} + + +/* + * Tokenize the string stored in cmd. + * Return the number of tokens, and store the tokens in cmd_argv. + */ +int tokenize(char *cmd, char **cmd_argv) { + int cmd_argc = 0; + char *next_token = strtok(cmd, DELIM); + while (next_token != NULL) { + if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) { + error(""too many arguments!""); + cmd_argc = 0; + break; + } + cmd_argv[cmd_argc] = next_token; + cmd_argc++; + next_token = strtok(NULL, DELIM); + } + + return cmd_argc; +} + + +int main(int argc, char* argv[]) { + int batch_mode = (argc == 2); + char input[INPUT_BUFFER_SIZE]; + FILE *input_stream; + + // Create the heads of the empty data structure + User *user_list = NULL; + + if (batch_mode) { + input_stream = fopen(argv[1], ""r""); + if (input_stream == NULL) { + perror(""Error opening file""); + exit(1); + } + } else { + // interactive mode + input_stream = stdin; + } + + printf(""Welcome to FriendMe! (Local version)\nPlease type a command:\n> ""); + + while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) { + // only echo the line in batch mode since in interactive mode the user + // just typed the line + if (batch_mode) { + printf(""%s"", input); + } + + char *cmd_argv[INPUT_ARG_MAX_NUM]; + int cmd_argc = tokenize(input, cmd_argv); + + if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { + break; // can only reach if quit command was entered + } + + printf(""> ""); + } + + if (batch_mode) { + fclose(input_stream); + } + + // Delete all users. This should free all heap memory that was allocated + // during the run of the program. Uncomment after you've implemented + // deleting users. + printf(""Freeing all heap space.\n""); + while (user_list != NULL) { + delete_user(user_list->name, &user_list); + } + return 0; + }",, +63c647fb5f004bf4898cf865,2023-02-13T19:58:10.073Z,General Question,v1,"do i have to free memory only when i malloc or in other cases too? +",,, +63c647fb5f004bf4898cf865,2023-02-13T20:13:22.235Z,General Question,v1,how to free a linked list example,,, +63c647fb5f004bf4898cf865,2023-02-13T20:18:56.742Z,General Question,v1,how to free a calloc memory,,, +63c647fb5f004bf4898cf865,2023-02-13T20:19:32.738Z,Question from Code,v1,WHERE IS MEMORY LEAKING :(,"#include ""friends.h"" +#include +#include +#include + +/* + * 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){ + return 2; + } + User *new_node = calloc(1, sizeof(User)); + + strcpy(new_node->name, name); + + if(user_ptr_add[0] == NULL){ + user_ptr_add[0] = new_node; + } else { + User *curr = user_ptr_add[0]; + while(curr->next != NULL){ + if(strcmp((*curr).name, name) == 0){ + return 1; + } else { + curr = curr->next; + } + } + if(strcmp((*curr).name, name) == 0){ + return 1; + } else { + curr->next = new_node; + } + } + 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 *user_head = (User *) head; + if(user_head == NULL){ + return NULL; + } else { + User *curr = user_head; + while(curr->next != NULL){ + if(strcmp((*curr).name, name) == 0){ + return curr; + } else { + curr = curr->next; + } + } + if(strcmp((*curr).name, name) == 0){ + return curr; + } + } + 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 *user_curr = (User *) curr; + printf(""%s\n"", ""User List""); + if(user_curr == NULL){ + ; + } else { + User curr_user = user_curr[0]; + while(curr_user.next != NULL){ + printf(""\t%s\n"", curr_user.name); + curr_user = *(curr_user.next); + } + printf(""\t%s\n"", curr_user.name); + } +} + + +/* + * 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) >= MAX_NAME){ + return 2; + } else if((fopen(filename, ""r"")) == NULL){ + return 1; + } else { + strcpy(user->profile_pic, filename); + return 0; + } +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int make_friends(const char *name1, const char *name2, User *head) { + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + if((find_user(name1, head) == NULL) || (find_user(name2, head) == NULL)){ + return 4; + } else if(strcmp(name1, name2) == 0){ + return 3; + } else { + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == user2){ + return 1; + } + } + + int not_max = 0; + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == NULL){ + not_max = 1; + } + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user2->friends)[i] == NULL){ + not_max = 1; + } + } + + if(not_max==0){ + return 2; + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == NULL){ + (user1->friends)[i] = user2; + break; + } + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user2->friends)[i] == NULL){ + (user2->friends)[i] = user1; + break; + } + } + + return 0; + } +} + + +/* + * 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; + } else { + if(user->profile_pic != NULL){ + FILE *pic_file; + char line[100]; + pic_file = fopen(user->profile_pic, ""r""); + if (pic_file != NULL){ + while (fgets(line, 100, pic_file) != NULL) { + printf(""%s"", line); + } + fclose(pic_file); + printf(""\n""); + } + + } + + printf(""%s%s\n"", ""Name: "", user->name); + printf(""%s\n"", ""------------------------------------------""); + printf(""%s\n"", ""Friends:""); + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user->friends)[i] != NULL){ + printf(""%s\n"", ((user->friends)[i])->name); + } + } + printf(""%s\n"", ""------------------------------------------""); + printf(""%s\n"", ""Posts:""); + if(user->first_post == NULL){ + ; + } else { + Post *curr_post = (user->first_post); + while(curr_post->next != NULL){ + printf(""%s%s\n"", ""From: "", curr_post->author); + printf(""%s%s\n"", ""Date: "", ctime(curr_post->date)); + printf(""%s\n"", curr_post->contents); + printf(""\n%s\n\n"", ""===""); + curr_post = curr_post->next; + } + printf(""%s%s\n"", ""From: "", curr_post->author); + printf(""%s%s\n"", ""Date: "", ctime(curr_post->date)); + printf(""%s\n"", curr_post->contents); + + } + printf(""%s\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; + } else { + int friends = 0; + for(int i = 0; i < MAX_FRIENDS; i++){ + if((author->friends)[i] == target){ + friends = 1; + } + } + if(friends == 0){ + return 1; + } + + Post *new_post = calloc(1, sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + time_t *post_time = malloc(sizeof(time_t)); + *post_time = time(NULL); + new_post->date = post_time; + + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; + } +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ +int delete_user(const char *name, User **user_ptr_del) { + if(find_user(name, user_ptr_del[0]) == NULL){ + return 1; + } else { + User *user_to_delete = find_user(name, user_ptr_del[0]); + + //remove as friend + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user_to_delete->friends)[i] != NULL){ + User *friend_user = find_user((user_to_delete->friends)[i]->name, user_ptr_del[0]); + for(int i = 0; i < MAX_FRIENDS; i++){ + if(((friend_user->friends)[i] != NULL) && (friend_user->friends)[i] == user_to_delete){ + friend_user->friends[i] = NULL; + } + } + } + } + + //delete the user from the linked list + User *curr = user_ptr_del[0]; + while((curr->next != NULL) && (curr->next) != user_to_delete){ + curr = curr->next; + } + + curr->next = user_to_delete->next; + + //free all memory related to the user + free(user_to_delete->name); + free(user_to_delete->profile_pic); + + Post *curr_post = user_to_delete->first_post; + while(curr_post != NULL){ + free(author); + free(curr_post->contents); + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + free((user_to_delete->friends[i])[i]); + } + free(user_to_delete); + return 0; + } +}",, +63c647fb5f004bf4898cf865,2023-02-13T20:21:06.095Z,Question from Code,v1,WHERE IS MEMORY LEAKING,"#include ""friends.h"" +#include +#include +#include + +/* + * 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){ + return 2; + } + User *new_node = calloc(1, sizeof(User)); + + strcpy(new_node->name, name); + + if(user_ptr_add[0] == NULL){ + user_ptr_add[0] = new_node; + } else { + User *curr = user_ptr_add[0]; + while(curr->next != NULL){ + if(strcmp((*curr).name, name) == 0){ + return 1; + } else { + curr = curr->next; + } + } + if(strcmp((*curr).name, name) == 0){ + return 1; + } else { + curr->next = new_node; + } + } + 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 *user_head = (User *) head; + if(user_head == NULL){ + return NULL; + } else { + User *curr = user_head; + while(curr->next != NULL){ + if(strcmp((*curr).name, name) == 0){ + return curr; + } else { + curr = curr->next; + } + } + if(strcmp((*curr).name, name) == 0){ + return curr; + } + } + 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 *user_curr = (User *) curr; + printf(""%s\n"", ""User List""); + if(user_curr == NULL){ + ; + } else { + User curr_user = user_curr[0]; + while(curr_user.next != NULL){ + printf(""\t%s\n"", curr_user.name); + curr_user = *(curr_user.next); + } + printf(""\t%s\n"", curr_user.name); + } +} + + +/* + * 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) >= MAX_NAME){ + return 2; + } else if((fopen(filename, ""r"")) == NULL){ + return 1; + } else { + strcpy(user->profile_pic, filename); + return 0; + } +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int make_friends(const char *name1, const char *name2, User *head) { + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + if((find_user(name1, head) == NULL) || (find_user(name2, head) == NULL)){ + return 4; + } else if(strcmp(name1, name2) == 0){ + return 3; + } else { + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == user2){ + return 1; + } + } + + int not_max = 0; + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == NULL){ + not_max = 1; + } + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user2->friends)[i] == NULL){ + not_max = 1; + } + } + + if(not_max==0){ + return 2; + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == NULL){ + (user1->friends)[i] = user2; + break; + } + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user2->friends)[i] == NULL){ + (user2->friends)[i] = user1; + break; + } + } + + return 0; + } +} + + +/* + * 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; + } else { + if(user->profile_pic != NULL){ + FILE *pic_file; + char line[100]; + pic_file = fopen(user->profile_pic, ""r""); + if (pic_file != NULL){ + while (fgets(line, 100, pic_file) != NULL) { + printf(""%s"", line); + } + fclose(pic_file); + printf(""\n""); + } + + } + + printf(""%s%s\n"", ""Name: "", user->name); + printf(""%s\n"", ""------------------------------------------""); + printf(""%s\n"", ""Friends:""); + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user->friends)[i] != NULL){ + printf(""%s\n"", ((user->friends)[i])->name); + } + } + printf(""%s\n"", ""------------------------------------------""); + printf(""%s\n"", ""Posts:""); + if(user->first_post == NULL){ + ; + } else { + Post *curr_post = (user->first_post); + while(curr_post->next != NULL){ + printf(""%s%s\n"", ""From: "", curr_post->author); + printf(""%s%s\n"", ""Date: "", ctime(curr_post->date)); + printf(""%s\n"", curr_post->contents); + printf(""\n%s\n\n"", ""===""); + curr_post = curr_post->next; + } + printf(""%s%s\n"", ""From: "", curr_post->author); + printf(""%s%s\n"", ""Date: "", ctime(curr_post->date)); + printf(""%s\n"", curr_post->contents); + + } + printf(""%s\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; + } else { + int friends = 0; + for(int i = 0; i < MAX_FRIENDS; i++){ + if((author->friends)[i] == target){ + friends = 1; + } + } + if(friends == 0){ + return 1; + } + + Post *new_post = calloc(1, sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + time_t *post_time = malloc(sizeof(time_t)); + *post_time = time(NULL); + new_post->date = post_time; + + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; + } +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ +int delete_user(const char *name, User **user_ptr_del) { + if(find_user(name, user_ptr_del[0]) == NULL){ + return 1; + } else { + User *user_to_delete = find_user(name, user_ptr_del[0]); + + //remove as friend + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user_to_delete->friends)[i] != NULL){ + User *friend_user = find_user((user_to_delete->friends)[i]->name, user_ptr_del[0]); + for(int i = 0; i < MAX_FRIENDS; i++){ + if(((friend_user->friends)[i] != NULL) && (friend_user->friends)[i] == user_to_delete){ + friend_user->friends[i] = NULL; + } + } + } + } + + //delete the user from the linked list + User *curr = user_ptr_del[0]; + while((curr->next != NULL) && (curr->next) != user_to_delete){ + curr = curr->next; + } + + curr->next = user_to_delete->next; + + //free all memory related to the user + free(user_to_delete->name); + free(user_to_delete->profile_pic); + + Post *curr_post = user_to_delete->first_post; + while(curr_post != NULL){ + free(curr_post->author); + free(curr_post->contents); + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + free((user_to_delete->friends)[i]); + } + free(user_to_delete); + return 0; + } +}",, +63c647fb5f004bf4898cf865,2023-02-13T20:27:47.278Z,General Question,v1,how to fix invalid reads,,, +63c647fb5f004bf4898cf865,2023-02-13T20:43:22.396Z,General Question,v1,how does calloc work,,, +63c647fb5f004bf4898cf865,2023-02-13T21:03:38.114Z,Question from Code,v1,how do i allocate space for each of the attributes of user,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User;",, +63c647fb5f004bf4898cf865,2023-02-13T21:16:03.221Z,General Question,v1,how to allocate space for an array of pointers to ints,,, +63c647fb5f004bf4898cf865,2023-02-13T21:26:41.771Z,General Question,v1,syntax to calloc for a string,,, +63c647fb5f004bf4898cf865,2023-02-13T21:42:13.943Z,Question from Code,v1,what is wrong with the memory allocations,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +User *new_node = calloc(1, sizeof(User)); +new_node->name = calloc(1, sizeof(char) * MAX_NAME); +new_node->profile_pic = calloc(1, sizeof(char) * MAX_NAME); +new_node->first_post = calloc(1, sizeof(Post)); +new_node->friends = calloc(32, sizeof(User *)); +for(int i = 0; i < MAX_FRIENDS; i++){ + (new_node->friends)[i] = calloc(1, sizeof(User)); +} +new_node->next = calloc(1, sizeof(User));",, +63c647fb5f004bf4898cf865,2023-02-13T21:49:30.510Z,Question from Code,v1,how do i allocate space for this using calloc,char *a[32];,, +63c647fb5f004bf4898cf865,2023-02-14T07:37:50.078Z,Question from Code,v1,"do i need to close the file here? if yes, where all do i need to close the file","int update_pic(User *user, const char *filename) { + if(strlen(filename) >= MAX_NAME){ + return 2; + } else if((fopen(filename, ""r"")) == NULL){ + return 1; + } else { + strcpy(user->profile_pic, filename); + return 0; + } +}",, +63c647fb5f004bf4898cf865,2023-02-14T07:46:12.616Z,General Question,v1,how to set a string attribute of a struct to null,,, +63c647fb5f004bf4898cf865,2023-02-14T07:46:40.040Z,General Question,v1,"does """" mean NULL",,, +63c647fb5f004bf4898cf865,2023-02-14T07:49:46.543Z,General Question,v1,how to initialise an array of 32 NULL ,,, +63c647fb5f004bf4898cf865,2023-02-14T07:54:26.950Z,General Question,v1,how to set a string to NULL,,, +63c647fb5f004bf4898cf865,2023-02-14T07:55:36.717Z,Question from Code,v1,is a == NULL?,"char *a = '\0' +",, +63c647fb5f004bf4898cf865,2023-02-14T08:06:05.999Z,Question from Code,v1,how to close the file in this,"int update_pic(User *user, const char *filename) { + if(strlen(filename) >= MAX_NAME){ + return 2; + } else if((fopen(filename, ""r"")) == NULL){ + return 1; + } else { + strcpy(user->profile_pic, filename); + return 0; + } +}",, +63c647fb5f004bf4898cf865,2023-02-14T08:06:48.690Z,Help Fix Code,v1,,"int update_pic(User *user, const char *filename) { + if(strlen(filename) >= MAX_NAME){ + return 2; + } else if((fopen(filename, ""r"")) == NULL){ + return 1; + } else { + strcpy(user->profile_pic, filename); + return 0; + } +}",i want to close the file, +63c647fb5f004bf4898cf865,2023-02-14T08:07:20.833Z,General Question,v1,what is fclose(fp),,, +63c647fb5f004bf4898cf865,2023-02-14T08:24:39.505Z,Question from Code,v1,where is memory leaking,"#include ""friends.h"" +#include +#include +#include + +/* + * 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){ + return 2; + } + User *new_node = malloc(sizeof(User)); + strcpy(new_node->name, name); + + new_node->first_post = NULL; + + for(int i = 0; i < MAX_FRIENDS; i++){ + (new_node->friends)[i] = NULL; + } + + new_node->next = NULL; + + if(user_ptr_add[0] == NULL){ + user_ptr_add[0] = new_node; + } else { + User *curr = user_ptr_add[0]; + while(curr->next != NULL){ + if(strcmp((*curr).name, name) == 0){ + return 1; + free(new_node); + } else { + curr = curr->next; + } + } + if(strcmp((*curr).name, name) == 0){ + return 1; + free(new_node); + } else { + curr->next = new_node; + } + } + + 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 *user_head = (User *) head; + if(user_head == NULL){ + return NULL; + } else { + User *curr = user_head; + while(curr->next != NULL){ + if(strcmp((*curr).name, name) == 0){ + return curr; + } else { + curr = curr->next; + } + } + if(strcmp((*curr).name, name) == 0){ + return curr; + } + } + 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 *user_curr = (User *) curr; + printf(""%s\n"", ""User List""); + if(user_curr == NULL){ + ; + } else { + User curr_user = user_curr[0]; + while(curr_user.next != NULL){ + printf(""\t%s\n"", curr_user.name); + curr_user = *(curr_user.next); + } + printf(""\t%s\n"", curr_user.name); + } +} + + +/* + * 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) >= MAX_NAME){ + return 2; + } else { + FILE *pic = fopen(filename, ""r""); + if(pic == NULL){ + return 1; + } else { + fclose(pic); + strcpy(user->profile_pic, filename); + return 0; + } + } + +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int make_friends(const char *name1, const char *name2, User *head) { + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + if((find_user(name1, head) == NULL) || (find_user(name2, head) == NULL)){ + return 4; + } else if(strcmp(name1, name2) == 0){ + return 3; + } else { + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == user2){ + return 1; + } + } + + int not_max = 0; + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == NULL){ + not_max = 1; + } + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user2->friends)[i] == NULL){ + not_max = 1; + } + } + + if(not_max==0){ + return 2; + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == NULL){ + (user1->friends)[i] = user2; + break; + } + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user2->friends)[i] == NULL){ + (user2->friends)[i] = user1; + break; + } + } + + return 0; + } +} + + +/* + * 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; + } else { + if(user->profile_pic != NULL){ + FILE *pic_file; + char line[100]; + pic_file = fopen(user->profile_pic, ""r""); + if (pic_file != NULL){ + while (fgets(line, 100, pic_file) != NULL) { + printf(""%s"", line); + } + fclose(pic_file); + printf(""\n""); + } + + } + + printf(""%s%s\n"", ""Name: "", user->name); + printf(""%s\n"", ""------------------------------------------""); + printf(""%s\n"", ""Friends:""); + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user->friends)[i] != NULL){ + printf(""%s\n"", ((user->friends)[i])->name); + } + } + printf(""%s\n"", ""------------------------------------------""); + printf(""%s\n"", ""Posts:""); + if(user->first_post == NULL){ + ; + } else { + Post *curr_post = (user->first_post); + while(curr_post->next != NULL){ + printf(""%s%s\n"", ""From: "", curr_post->author); + printf(""%s%s\n"", ""Date: "", ctime(curr_post->date)); + printf(""%s\n"", curr_post->contents); + printf(""\n%s\n\n"", ""===""); + curr_post = curr_post->next; + } + printf(""%s%s\n"", ""From: "", curr_post->author); + printf(""%s%s\n"", ""Date: "", ctime(curr_post->date)); + printf(""%s\n"", curr_post->contents); + + } + printf(""%s\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; + } else { + int friends = 0; + for(int i = 0; i < MAX_FRIENDS; i++){ + if((author->friends)[i] == target){ + friends = 1; + } + } + if(friends == 0){ + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + time_t *post_time = malloc(sizeof(time_t)); + *post_time = time(NULL); + new_post->date = post_time; + + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; + } +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ +int delete_user(const char *name, User **user_ptr_del) { + if(find_user(name, user_ptr_del[0]) == NULL){ + return 1; + } else { + User *user_to_delete = find_user(name, user_ptr_del[0]); + + //remove as friend + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user_to_delete->friends)[i] != NULL){ + User *friend_user = find_user((user_to_delete->friends)[i]->name, user_ptr_del[0]); + for(int i = 0; i < MAX_FRIENDS; i++){ + if(((friend_user->friends)[i] != NULL) && (friend_user->friends)[i] == user_to_delete){ + friend_user->friends[i] = NULL; + } + } + } + } + + //delete the user from the linked list + User *curr = user_ptr_del[0]; + while((curr->next != NULL) && (curr->next) != user_to_delete){ + curr = curr->next; + } + + curr->next = user_to_delete->next; + + //free all memory related to the user + Post *curr_post = user_to_delete->first_post; + while(curr_post != NULL){ + free(curr_post->contents); + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + + free(user_to_delete); + return 0; + } +}",, +63c647fb5f004bf4898cf865,2023-02-15T05:50:48.176Z,General Question,v1,tons of valgrind error makes valgrind stuck and does not show me heap summary. what to do.,,, +63c647fb5f004bf4898cf865,2023-02-15T06:03:11.631Z,Help Fix Code,v1,,"#include ""friends.h"" +#include +#include +#include + +/* + * 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) { + //checking if given name cannot fit in name array + if(strlen(name) >= MAX_NAME){ + return 2; + } + User *new_node = malloc(sizeof(User)); + strcpy(new_node->name, name); + + new_node->first_post = NULL; + + for(int i = 0; i < MAX_FRIENDS; i++){ + (new_node->friends)[i] = NULL; + } + + new_node->next = NULL; + + if(user_ptr_add[0] == NULL){ + user_ptr_add[0] = new_node; + } else { + User *curr = user_ptr_add[0]; + while(curr->next != NULL){ + if(strcmp((*curr).name, name) == 0){ + return 1; + free(new_node); + } else { + curr = curr->next; + } + } + if(strcmp((*curr).name, name) == 0){ + return 1; + free(new_node); + } else { + curr->next = new_node; + } + } + + 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 *user_head = (User *) head; + if(user_head == NULL){ + return NULL; + } else { + User *curr = user_head; + while(curr->next != NULL){ + if(strcmp((*curr).name, name) == 0){ + return curr; + } else { + curr = curr->next; + } + } + if(strcmp((*curr).name, name) == 0){ + return curr; + } + } + 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 *user_curr = (User *) curr; + printf(""%s\n"", ""User List""); + if(user_curr == NULL){ + ; + } else { + User curr_user = user_curr[0]; + while(curr_user.next != NULL){ + printf(""\t%s\n"", curr_user.name); + curr_user = *(curr_user.next); + } + printf(""\t%s\n"", curr_user.name); + } +} + + +/* + * 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) >= MAX_NAME){ + return 2; + } else { + FILE *pic = fopen(filename, ""r""); + if(pic == NULL){ + return 1; + } else { + fclose(pic); + strcpy(user->profile_pic, filename); + return 0; + } + } + +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int make_friends(const char *name1, const char *name2, User *head) { + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + if((find_user(name1, head) == NULL) || (find_user(name2, head) == NULL)){ + return 4; + } else if(strcmp(name1, name2) == 0){ + return 3; + } else { + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == user2){ + return 1; + } + } + + int not_max_user1 = 0; + int not_max_user2 = 0; + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == NULL){ + not_max_user1 = 1; + } + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user2->friends)[i] == NULL){ + not_max_user2 = 1; + } + } + + if((not_max_user1==0) || (not_max_user2)==0 ){ + return 2; + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == NULL){ + (user1->friends)[i] = user2; + break; + } + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user2->friends)[i] == NULL){ + (user2->friends)[i] = user1; + break; + } + } + + return 0; + } +} + + +/* + * 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; + } else { + if(user->profile_pic != NULL){ + FILE *pic_file; + char line[100]; + pic_file = fopen(user->profile_pic, ""r""); + if (pic_file != NULL){ + while (fgets(line, 100, pic_file) != NULL) { + printf(""%s"", line); + } + fclose(pic_file); + printf(""\n""); + } + + } + + printf(""%s%s\n"", ""Name: "", user->name); + printf(""%s\n"", ""------------------------------------------""); + printf(""%s\n"", ""Friends:""); + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user->friends)[i] != NULL){ + printf(""%s\n"", ((user->friends)[i])->name); + } + } + printf(""%s\n"", ""------------------------------------------""); + printf(""%s\n"", ""Posts:""); + if(user->first_post == NULL){ + ; + } else { + Post *curr_post = (user->first_post); + while(curr_post->next != NULL){ + printf(""%s%s\n"", ""From: "", curr_post->author); + printf(""%s%s\n"", ""Date: "", ctime(curr_post->date)); + printf(""%s\n"", curr_post->contents); + printf(""\n%s\n\n"", ""===""); + curr_post = curr_post->next; + } + printf(""%s%s\n"", ""From: "", curr_post->author); + printf(""%s%s\n"", ""Date: "", ctime(curr_post->date)); + printf(""%s\n"", curr_post->contents); + + } + printf(""%s\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; + } else { + int friends = 0; + for(int i = 0; i < MAX_FRIENDS; i++){ + if((author->friends)[i] == target){ + friends = 1; + } + } + if(friends == 0){ + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + time_t *post_time = malloc(sizeof(time_t)); + *post_time = time(NULL); + new_post->date = post_time; + + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; + } +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ +int delete_user(const char *name, User **user_ptr_del) { + if(find_user(name, user_ptr_del[0]) == NULL){ + return 1; + } else { + User *user_to_delete = find_user(name, user_ptr_del[0]); + + //remove as friend + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user_to_delete->friends)[i] != NULL){ + User *friend_user = find_user((user_to_delete->friends)[i]->name, user_ptr_del[0]); + for(int i = 0; i < MAX_FRIENDS; i++){ + if(((friend_user->friends)[i] != NULL) && (friend_user->friends)[i] == user_to_delete){ + friend_user->friends[i] = NULL; + } + } + } + } + + //delete the user from the linked list + User *curr = user_ptr_del[0]; + if(curr->next == NULL){ + user_ptr_del[0] = NULL; + } else { + while((curr->next != NULL) && ((curr->next) != user_to_delete)){ + curr = curr->next; + } + + curr->next = user_to_delete->next; + } + + //free all memory related to the user + Post *curr_post = user_to_delete->first_post; + while(curr_post != NULL){ + free(curr_post->contents); + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + + free(user_to_delete); + return 0; + } +}",need to fix multiple invalid read erros, +63c647fb5f004bf4898cf865,2023-02-15T06:04:26.232Z,Question from Code,v1,how to fix multiple invalid read erros,"#include ""friends.h"" +#include +#include +#include + +/* + * 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) { + //checking if given name cannot fit in name array + if(strlen(name) >= MAX_NAME){ + return 2; + } + User *new_node = malloc(sizeof(User)); + strcpy(new_node->name, name); + + new_node->first_post = NULL; + + for(int i = 0; i < MAX_FRIENDS; i++){ + (new_node->friends)[i] = NULL; + } + + new_node->next = NULL; + + if(user_ptr_add[0] == NULL){ + user_ptr_add[0] = new_node; + } else { + User *curr = user_ptr_add[0]; + while(curr->next != NULL){ + if(strcmp((*curr).name, name) == 0){ + return 1; + free(new_node); + } else { + curr = curr->next; + } + } + if(strcmp((*curr).name, name) == 0){ + return 1; + free(new_node); + } else { + curr->next = new_node; + } + } + + 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 *user_head = (User *) head; + if(user_head == NULL){ + return NULL; + } else { + User *curr = user_head; + while(curr->next != NULL){ + if(strcmp((*curr).name, name) == 0){ + return curr; + } else { + curr = curr->next; + } + } + if(strcmp((*curr).name, name) == 0){ + return curr; + } + } + 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 *user_curr = (User *) curr; + printf(""%s\n"", ""User List""); + if(user_curr == NULL){ + ; + } else { + User curr_user = user_curr[0]; + while(curr_user.next != NULL){ + printf(""\t%s\n"", curr_user.name); + curr_user = *(curr_user.next); + } + printf(""\t%s\n"", curr_user.name); + } +} + + +/* + * 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) >= MAX_NAME){ + return 2; + } else { + FILE *pic = fopen(filename, ""r""); + if(pic == NULL){ + return 1; + } else { + fclose(pic); + strcpy(user->profile_pic, filename); + return 0; + } + } + +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int make_friends(const char *name1, const char *name2, User *head) { + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + if((find_user(name1, head) == NULL) || (find_user(name2, head) == NULL)){ + return 4; + } else if(strcmp(name1, name2) == 0){ + return 3; + } else { + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == user2){ + return 1; + } + } + + int not_max_user1 = 0; + int not_max_user2 = 0; + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == NULL){ + not_max_user1 = 1; + } + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user2->friends)[i] == NULL){ + not_max_user2 = 1; + } + } + + if((not_max_user1==0) || (not_max_user2)==0 ){ + return 2; + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user1->friends)[i] == NULL){ + (user1->friends)[i] = user2; + break; + } + } + + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user2->friends)[i] == NULL){ + (user2->friends)[i] = user1; + break; + } + } + + return 0; + } +} + + +/* + * 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; + } else { + if(user->profile_pic != NULL){ + FILE *pic_file; + char line[100]; + pic_file = fopen(user->profile_pic, ""r""); + if (pic_file != NULL){ + while (fgets(line, 100, pic_file) != NULL) { + printf(""%s"", line); + } + fclose(pic_file); + printf(""\n""); + } + + } + + printf(""%s%s\n"", ""Name: "", user->name); + printf(""%s\n"", ""------------------------------------------""); + printf(""%s\n"", ""Friends:""); + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user->friends)[i] != NULL){ + printf(""%s\n"", ((user->friends)[i])->name); + } + } + printf(""%s\n"", ""------------------------------------------""); + printf(""%s\n"", ""Posts:""); + if(user->first_post == NULL){ + ; + } else { + Post *curr_post = (user->first_post); + while(curr_post->next != NULL){ + printf(""%s%s\n"", ""From: "", curr_post->author); + printf(""%s%s\n"", ""Date: "", ctime(curr_post->date)); + printf(""%s\n"", curr_post->contents); + printf(""\n%s\n\n"", ""===""); + curr_post = curr_post->next; + } + printf(""%s%s\n"", ""From: "", curr_post->author); + printf(""%s%s\n"", ""Date: "", ctime(curr_post->date)); + printf(""%s\n"", curr_post->contents); + + } + printf(""%s\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; + } else { + int friends = 0; + for(int i = 0; i < MAX_FRIENDS; i++){ + if((author->friends)[i] == target){ + friends = 1; + } + } + if(friends == 0){ + return 1; + } + + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + new_post->contents = contents; + time_t *post_time = malloc(sizeof(time_t)); + *post_time = time(NULL); + new_post->date = post_time; + + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; + } +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ +int delete_user(const char *name, User **user_ptr_del) { + if(find_user(name, user_ptr_del[0]) == NULL){ + return 1; + } else { + User *user_to_delete = find_user(name, user_ptr_del[0]); + + //remove as friend + for(int i = 0; i < MAX_FRIENDS; i++){ + if((user_to_delete->friends)[i] != NULL){ + User *friend_user = find_user((user_to_delete->friends)[i]->name, user_ptr_del[0]); + for(int i = 0; i < MAX_FRIENDS; i++){ + if(((friend_user->friends)[i] != NULL) && (friend_user->friends)[i] == user_to_delete){ + friend_user->friends[i] = NULL; + } + } + } + } + + //delete the user from the linked list + User *curr = user_ptr_del[0]; + if(curr->next == NULL){ + user_ptr_del[0] = NULL; + } else { + while((curr->next != NULL) && ((curr->next) != user_to_delete)){ + curr = curr->next; + } + + curr->next = user_to_delete->next; + } + + //free all memory related to the user + Post *curr_post = user_to_delete->first_post; + while(curr_post != NULL){ + free(curr_post->contents); + free(curr_post->date); + free(curr_post); + curr_post = curr_post->next; + } + + free(user_to_delete); + return 0; + } +}",, +63c647fb5f004bf4898cf865,2023-02-15T06:53:27.359Z,General Question,v1,how to use gdb to debug,,, +63c647fb5f004bf4898cf865,2023-02-15T17:36:02.025Z,General Question,v1,"when i intialise char *profile, what is its default value set to",,, +63c647fb5f004bf4898cf865,2023-02-15T20:39:40.750Z,General Question,v1,how to check if malloc fails,,, +63c647fb5f004bf4898cf865,2023-02-15T22:18:42.626Z,General Question,v1,what is memset,,, +63c647fb5f004bf4898cf865,2023-02-16T19:21:12.396Z,General Question,v1,"c_helper.py:125: in _t + stdout, stderr, returncode = self._run_exec(args=args, input_=input_, timeout=timeout) +c_helper.py:85: in _run_exec + return _exec([os.path.join(""."", self.executable_name)] + (args or []), **kwargs) +c_helper.py:471: in _exec + stdout, stderr = proc.communicate(timeout=timeout, input=input_) +/local/packages/python-3.10/lib/python3.10/subprocess.py:1152: in communicate + stdout, stderr = self._communicate(input, endtime, timeout) +/local/packages/python-3.10/lib/python3.10/subprocess.py:2041: in _communicate + stdout = self._translate_newlines(stdout, +/local/packages/python-3.10/lib/python3.10/subprocess.py:1029: in _translate_newlines + data = data.decode(encoding, errors) +E UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfd in position 4: invalid start byte + +WHAT IS THIS ERROR?",,, +63c647fb5f004bf4898cf865,2023-02-17T20:49:07.750Z,General Question,v1,"strncpy(char *dest, ""ananyajain"", 7) output?",,, +63c647fb5f004bf4898cf865,2023-02-24T04:22:09.710Z,Question from Code,v1,what is the output of the code,"#include +#include +// Initialize two parallel lists. +void initialize(int *a1, int *a2, int n) { +for (int i = 0; i < n; i++) { +a1[i] = i; +a2[i] = i; +} +} +int main() { +int numbers1[3]; +int *numbers2 = malloc(sizeof(int) * 3); +initialize(numbers1, numbers2, 3); +for (int i = 0; i < 3; i++) { +printf(""%d %d\n"", +numbers1[i], numbers2[i]); +} +free(numbers2); +return 0; +}",, +63c647fb5f004bf4898cf865,2023-02-25T01:12:08.982Z,General Question,v1,"what exactly is the second parameter of strtol +",,, +63c647fb5f004bf4898cf865,2023-02-25T01:20:27.479Z,Question from Code,v1,what is strlen(a),"char *a = ""Hello I""",, +63c647fb5f004bf4898cf865,2023-02-25T01:21:28.920Z,General Question,v1,"is strlen(a) 7 or 8 or 9 +",,, +63c647fb5f004bf4898cf865,2023-02-25T01:21:55.847Z,Question from Code,v1,is strlen(a) 7 or 8 or 9,"char *a = ""Hello I""",, +63c647fb5f004bf4898cf865,2023-02-25T01:22:23.216Z,General Question,v1,"what is strlen(a) when char *a = ""Hello I""",,, +63c647fb5f004bf4898cf865,2023-02-28T13:42:36.004Z,Question from Code,v1,what will be the output,"char *a = ""Hello Bruh"" +char *b = strchr(a, ' ') +printf(""%s"", b)",, +63c647fb5f004bf4898cf865,2023-02-28T13:43:12.003Z,Question from Code,v1,output,"char *a = ""Hello Bruh"" +char *b = strchr(a, ' ') - 1 +printf(""%s"", b)",, +63c647fb5f004bf4898cf865,2023-02-28T13:43:40.890Z,Question from Code,v1,output,"char *a = ""Hello Bruh"" +char *b = strchr(a, ' ') - 2 +printf(""%s"", b)",, +63c647fb5f004bf4898cf865,2023-02-28T13:57:12.514Z,General Question,v1,"how does grep work +",,, +63c647fb5f004bf4898cf865,2023-02-28T13:58:07.770Z,General Question,v1,how does cut work,,, +63c647fb5f004bf4898cf865,2023-03-08T20:59:08.860Z,General Question,v1,my child calls a different program using execl which has different exit values. how do i get the exit value the program exited with back to my child,,, +63c647fb5f004bf4898cf865,2023-03-09T17:39:55.883Z,General Question,v2,"how to write ""swim"" to stdin +",,, +63c647fb5f004bf4898cf865,2023-03-09T17:45:14.359Z,General Question,v2,a program called validate is expecting 2 inputs from stdin. my function calls validate using execl. how do i pass the variables i need to as stdin to it.,,, +63c647fb5f004bf4898cf865,2023-03-09T19:54:51.918Z,General Question,v2,what is the difference between exit(0) and return 0,,, +63c647fb5f004bf4898cf865,2023-03-11T00:46:58.582Z,General Question,v2,how does strtok work,,, +63c647fb5f004bf4898cf865,2023-03-11T01:23:26.533Z,General Question,v2,"Let a string be s = ""main : linked_list.o main.o"". What is the output of strtok(s, "" "")",,, +63c647fb5f004bf4898cf865,2023-03-11T01:35:30.566Z,General Question,v2,how to check is a string starts with \t,,, +63c647fb5f004bf4898cf865,2023-03-11T01:37:30.028Z,Question from Code,v2,what is the value of b,"a = ""\tHello World"" b = strncmp(a, ""\t"", 1); +",, +63c647fb5f004bf4898cf865,2023-03-11T06:30:58.170Z,Question from Code,v2,what is the value of token,"char *a = ""\tHello Boss"" char *token = strtok(a, "" "") +",, +63c647fb5f004bf4898cf865,2023-03-11T07:02:24.286Z,General Question,v2,what does strncmp returning 3 mean,,, +63c647fb5f004bf4898cf865,2023-03-11T18:36:25.133Z,General Question,v2,"how to find if a char is in a string +",,, +63c647fb5f004bf4898cf865,2023-03-13T17:16:17.258Z,Question from Code,v2,what is the value of a[0],"char *a = ""Hello I am Kaavya"";",, +63c647fb5f004bf4898cf865,2023-03-13T17:19:27.142Z,General Question,v2,how does stat work?,,, +63c647fb5f004bf4898cf865,2023-03-13T20:54:27.091Z,General Question,v2,how to use access to check if a file exists or not,,, +63c647fb5f004bf4898cf865,2023-03-13T22:23:42.855Z,General Question,v2,how to check if a string is NULL,,, +63c647fb5f004bf4898cf865,2023-03-14T06:59:19.696Z,Question from Code,v2,"what is return value of args_to_string({""Hi"" , ""Kaavya"" , NULL}, a, 3","char *args_to_string(char **args, char *buffer, int size) { + buffer[0] = '\0'; + int i = 0; + while (args[i] != NULL) { + strncat(buffer, args[i], size - strlen(buffer)); + strncat(buffer, "" "", size - strlen(buffer)); + i++; + } + return buffer; +} + +char *a = malloc(256); +",, +63c647fb5f004bf4898cf865,2023-03-14T07:00:09.491Z,Question from Code,v2,"what is return value of args_to_string({""Hi"" , ""Kaavya"" , NULL}, a, 256)","char *args_to_string(char **args, char *buffer, int size) { + buffer[0] = '\0'; + int i = 0; + while (args[i] != NULL) { + strncat(buffer, args[i], size - strlen(buffer)); + strncat(buffer, "" "", size - strlen(buffer)); + i++; + } + return buffer; +} + +char *a = malloc(256); +",, +63c647fb5f004bf4898cf865,2023-03-14T07:07:45.074Z,General Question,v2,"how to update the updated time of a file +",,, +63c647fb5f004bf4898cf865,2023-03-14T07:45:21.157Z,General Question,v2,Can you use variables declared in the main stack directly in a function and change its value globally (variable of type int),,, +63c647fb5f004bf4898cf865,2023-03-15T05:54:19.022Z,General Question,v2,how to mention the start and end numbers for random(),,, +63c647fb5f004bf4898cf865,2023-03-17T02:07:41.161Z,General Question,v2,how to use random() to generate numbers between 0 to 99 inclusive in C,,, +63c647fb5f004bf4898cf865,2023-03-17T02:08:26.512Z,Help Write Code,v2,,,,how to use random() to generate numbers between 0 to 99 inclusive in C +63c647fb5f004bf4898cf865,2023-03-21T23:19:14.843Z,Question from Code,v2,what is a[2],"char *a = ""Hello""",, +63c647fb5f004bf4898cf865,2023-03-26T01:23:35.472Z,Question from Code,v2,why am i getting a \n in my msg,"int read_from(int client_index, struct sockname *users) { + int fd = users[client_index].sock_fd; + char buf[BUF_SIZE + 1]; + + int num_read = read(fd, &buf, BUF_SIZE); + buf[num_read] = '\0'; + + if (users[client_index].username == NULL) { + if (num_read == 0) { + users[client_index].sock_fd = -1; + return fd; + } + users[client_index].username = malloc(num_read + 1); + strncpy(users[client_index].username, buf, num_read + 1); + return 0; + } + + if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) { + users[client_index].sock_fd = -1; + return fd; + } + + char *msg = malloc(num_read + 3 + strlen(users[client_index].username)); + sprintf(msg, ""%s: %s"", users[client_index].username, buf); + for (int i = 0; i < MAX_CONNECTIONS; i++) { + if (users[i].sock_fd != -1) { + write(users[i].sock_fd, msg, strlen(msg)); + } + } + free(msg); + return 0; +}",, +63c647fb5f004bf4898cf865,2023-03-27T04:40:39.790Z,General Question,v2,"area=`expr $width * $height` +why is this incorrect",,, +63c647fb5f004bf4898cf865,2023-04-04T22:59:31.345Z,Question from Code,v2,does the given code work,"char *a; +a = ""Hello"";",, +63c647fb5f004bf4898cf86a,2023-01-25T01:44:10.416Z,General Question,v1,How do I reassign a string in C,,, +63c647fb5f004bf4898cf86a,2023-01-25T02:15:37.567Z,General Question,v1,How do I read a file through stdin in C,,, +63c647fb5f004bf4898cf86a,2023-01-25T02:16:54.510Z,General Question,v1,How do I use scanf to read a redirected file,,, +63c647fb5f004bf4898cf86a,2023-01-25T15:05:23.024Z,General Question,v1,How do I use a function in one C file in another C file,,, +63c647fb5f004bf4898cf86a,2023-01-25T15:22:01.497Z,General Question,v1,How to convert char to int,,, +63c647fc5f004bf4898cf874,2023-03-07T15:40:50.302Z,General Question,v1,"split string on spaces +",,, +63c647fc5f004bf4898cf874,2023-03-07T15:49:59.570Z,General Question,v1,how do i read a file line by line ,,, +63c647fc5f004bf4898cf874,2023-03-07T16:11:49.425Z,General Question,v1,read character by character in a string ,,, +63c647fc5f004bf4898cf874,2023-03-07T16:15:41.136Z,General Question,v1,does fgets overwrite the string array ,,, +63c647fc5f004bf4898cf874,2023-03-07T16:44:26.728Z,General Question,v1,dictionaries in C,,, +63c647fc5f004bf4898cf874,2023-03-07T17:14:22.595Z,General Question,v1,"count number of spaces in string +",,, +63c647fc5f004bf4898cf874,2023-03-07T20:50:40.799Z,General Question,v1,how to read the last string in a string delimeted by a delimeter using strtok,,, +63c647fc5f004bf4898cf874,2023-03-07T21:30:51.805Z,Question from Code,v1,"strtok doesnt work as expected, last string in line cannot be obtained +","//get the target name + targets = strtok(line, "" ""); + printf(""%s \n"", targets); + + //create the Rule datatype + if (curr_rule == NULL){ + curr_rule = malloc(sizeof(Rule)); + head = curr_rule; + }else{ + Rule * new_rule = malloc(sizeof(Rule)); + (curr_rule -> next_rule) = new_rule; + curr_rule = new_rule; + } + curr_rule -> next_rule = NULL; + + //set the name properly + (curr_rule -> target) = malloc(sizeof(char) * (strlen(targets) + 1)); + strncpy(curr_rule -> target, targets, sizeof(char) * (strlen(targets) + 1)); + (curr_rule -> target)[strlen(curr_rule -> target)] = '\0'; + + //link the dependencies + targets = strtok(NULL, "" ""); + while (targets != NULL){ + printf(""%s \n"", targets); + targets = strtok(NULL, "" ""); + if (targets != NULL){ + link_dependencies(targets, curr_dependency, head); + } + }",, +63c647fc5f004bf4898cf874,2023-03-07T21:31:42.917Z,Explain Code,v1,,"//get the target name + targets = strtok(line, "" ""); + printf(""%s \n"", targets); + + //create the Rule datatype + if (curr_rule == NULL){ + curr_rule = malloc(sizeof(Rule)); + head = curr_rule; + }else{ + Rule * new_rule = malloc(sizeof(Rule)); + (curr_rule -> next_rule) = new_rule; + curr_rule = new_rule; + } + curr_rule -> next_rule = NULL; + + //set the name properly + (curr_rule -> target) = malloc(sizeof(char) * (strlen(targets) + 1)); + strncpy(curr_rule -> target, targets, sizeof(char) * (strlen(targets) + 1)); + (curr_rule -> target)[strlen(curr_rule -> target)] = '\0'; + + //link the dependencies + targets = strtok(NULL, "" ""); + while (targets != NULL){ + printf(""%s \n"", targets); + targets = strtok(NULL, "" ""); + if (targets != NULL){ + link_dependencies(targets, curr_dependency, head); + } + }",, +63c647fc5f004bf4898cf874,2023-03-08T17:48:54.125Z,General Question,v1,"skip the first character in a string literal +",,, +63c647fc5f004bf4898cf874,2023-03-09T17:41:38.147Z,General Question,v2,print a char** array ,,, +63c647fc5f004bf4898cf874,2023-03-09T17:53:26.927Z,General Question,v2,how does recursion work in C?,,, +63c647fc5f004bf4898cf874,2023-03-09T18:09:05.366Z,General Question,v2,how do i make my function wait on a recursive call without using fork in C?,,, +63c647fc5f004bf4898cf874,2023-03-14T22:36:12.789Z,General Question,v2,"how do i use random in C? it says it need RAND_MAX, how do i set it myself?",,, +63c647fc5f004bf4898cf874,2023-03-14T23:05:10.130Z,Question from Code,v2,not reading binary file correctly. giving me random values. ," + +#include +#include +#include +#include +#include + +#define MESSAGE ""%ld reads were done in %ld seconds.\n"" + +long num_reads, seconds; + +int main(int argc, char **argv) { + if (argc != 3) { + fprintf(stderr, ""Usage: time_reads s filename\n""); + exit(1); + } + seconds = strtol(argv[1], NULL, 10); + + FILE *fp; + if ((fp = fopen(argv[2], ""r"")) == NULL) { + perror(""fopen""); + exit(1); + } + + int rand_idx; + int value_at_idx; + for (int i = 0; i < 100; i++) { + rand_idx = (int)(((double)random() / RAND_MAX) * 99); + fseek(fp, rand_idx, SEEK_SET); + fread(&value_at_idx, sizeof(int), 1, fp); + printf(""%i\n"", value_at_idx); + } + return 1; +} +",, +63c647fc5f004bf4898cf874,2023-03-27T16:33:22.584Z,General Question,v2,"I am unable to use asprintf, eventhough i have defined _GNU_SOURCE and imported stdio.h",,, +63c647fd5f004bf4898cf879,2023-01-31T19:42:37.938Z,General Question,v1,Is the function strcpy safe?,,, +63c647fd5f004bf4898cf879,2023-01-31T19:45:48.601Z,General Question,v1,How to copy a string to an array of char in C safely?,,, +63c647ff5f004bf4898cf888,2023-01-23T22:55:59.232Z,Help Fix Code,v1,,"hile (scanf("" %c %s %*d %*s %*s %d %*s %*d %*s %*s"", &file_type, permissions, &file_size) != EOF) {",, +63c647ff5f004bf4898cf888,2023-01-23T22:56:43.380Z,Help Fix Code,v1,,"while (scanf("" %c %s %*d %*s %*s %d %*s %*d %*s %*s"", &file_type, permissions, &file_size) != EOF) {scanf("" %s %d "", temp_s, &temp_i); + + while (scanf("" %c %s %*d %*s %*s %d %*s %*d %*s %*s "", &file_type, permissions, &file_size) != EOF) { + if (file_type == '-' && file_size > cutoff_size) { + if (argc == 2){ + count_so_far++; + } else { + if (check_permissions(permissions, argv[2]) == 0) { + count_so_far++; + } + } + } + }",, +63c647ff5f004bf4898cf888,2023-01-23T22:57:20.101Z,Help Fix Code,v1,," + while (scanf("" %c %s %*d %*s %*s %d %*s %*d %*s %*s "", &file_type, permissions, &file_size) != EOF) { + if (file_type == '-' && file_size > cutoff_size) { + if (argc == 2){ + count_so_far++; + } else { + if (check_permissions(permissions, argv[2]) == 0) { + count_so_far++; + } + } + } + }",, +63c647ff5f004bf4898cf888,2023-01-23T22:57:51.510Z,Question from Code,v1,"why should I put a space after the last %*s +"," + while (scanf("" %c %s %*d %*s %*s %d %*s %*d %*s %*s "", &file_type, permissions, &file_size) != EOF) { + if (file_type == '-' && file_size > cutoff_size) { + if (argc == 2){ + count_so_far++; + } else { + if (check_permissions(permissions, argv[2]) == 0) { + count_so_far++; + } + } + } + }",, +63c647ff5f004bf4898cf88d,2023-01-17T23:44:24.128Z,Explain Code,v1,,"int age[5] = {4, 70, 52, 18, 16}; + int *p = age;",, +63c648025f004bf4898cf89c,2023-01-17T22:01:24.007Z,Question from Code,v1,Why is phone used instead of &phone?,"char phone[11]; +scanf(""%s %d"", phone, &index);",, +63c648025f004bf4898cf89c,2023-01-17T22:03:09.890Z,Help Fix Code,v1,,"char phone[11]; +scanf(""%s %d"", &phone);",get string input and store it in phone., +63c648055f004bf4898cf8bf,2023-01-23T03:55:47.480Z,General Question,v1,"why is system(""ls -l"") of type int?",,, +63c648055f004bf4898cf8bf,2023-01-23T04:38:30.199Z,General Question,v1,does scanf read multiple lines?,,, +63c648055f004bf4898cf8bf,2023-01-23T04:47:59.292Z,Question from Code,v1,how do I only print certain part of stdin?,"int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + if (argc == 2) { + char line[32]; + while (fgets(line, 32, stdin) != NULL) { + printf(""%s"", line); + } + } + + return 0; +}",, +63c648055f004bf4898cf8bf,2023-01-23T04:48:43.044Z,Question from Code,v1,how do i parse stdin without strtok?,"int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + if (argc == 2) { + char line[32]; + while (fgets(line, 32, stdin) != NULL) { + printf(""%s"", line); + } + } + + return 0; +}",, +63c648055f004bf4898cf8bf,2023-01-23T04:49:13.072Z,Question from Code,v1,how do i parse stdin with strtol?,"int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + if (argc == 2) { + char line[32]; + while (fgets(line, 32, stdin) != NULL) { + printf(""%s"", line); + } + } + + return 0; +}",, +63c648055f004bf4898cf8bf,2023-01-23T04:52:42.686Z,Question from Code,v1,inputs of scanf?,"int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + if (argc == 2) { + char line[32]; + while (fgets(line, 32, stdin) != NULL) { + printf(""%s"", line); + } + } + + return 0; +}",, +63c648055f004bf4898cf8bf,2023-01-23T04:53:44.548Z,General Question,v1,"if my stdin is ""hello yo 3 191 so what"" what are my scanf arguments?",,, +63c648055f004bf4898cf8bf,2023-01-23T04:55:17.495Z,General Question,v1,"if my stdin is ""-rwxrwxrwx 1 kimluci2 kimluci2 763 Jan 22 18:05 Makefile"" what are my scanf arguments? ",,, +63c648055f004bf4898cf8bf,2023-01-23T05:10:17.348Z,General Question,v1,how do i skip the first line of stdin when scanf?,,, +63c648055f004bf4898cf8bf,2023-01-23T06:06:30.169Z,General Question,v1,what is the type of %c%10s,,, +63c648055f004bf4898cf8bf,2023-01-23T06:10:45.967Z,General Question,v1,how do i initialize a variable of %c%10s,,, +63c648055f004bf4898cf8bf,2023-01-23T06:12:41.383Z,General Question,v1,is %c variable declared as char,,, +63c648055f004bf4898cf8bf,2023-01-23T06:27:52.509Z,Question from Code,v1,what does EOF do,"int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + if (argc == 2) { + int total = 0; + char dir; + char perms[10]; + int size; + int param1 = strtol(argv[1], NULL, 10); + while (scanf(""%c%10s %*d %*s %*s %d %*s %*d %*s %*s\n"", &dir, perms, &size) != EOF) { + if (size > param1) { + total += 1; + } + } + printf(""%d\n"", total); + } + return 0; +} +",, +63c648055f004bf4898cf8bf,2023-01-23T06:28:37.760Z,Question from Code,v1,what does == 3 in the while loop do,"int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + if (argc == 2) { + int total = 0; + char dir; + char perms[10]; + int size; + int param1 = strtol(argv[1], NULL, 10); + while (scanf(""%c%10s %*d %*s %*s %d %*s %*d %*s %*s\n"", &dir, perms, &size) == 3) { + if (size > param1) { + total += 1; + } + } + printf(""%d\n"", total); + } + return 0; +} +",, +63c648055f004bf4898cf8bf,2023-01-23T06:34:25.907Z,General Question,v1,how do i make scanf ignore the first line?,,, +63c648055f004bf4898cf8bf,2023-01-23T06:35:03.922Z,Question from Code,v1,how do i make scanf skip a specific line?,"int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + if (argc == 2) { + int total = 0; + char dir; + char perms[10]; + int size; + int param1 = strtol(argv[1], NULL, 10); + while (scanf(""%c%10s %*d %*s %*s %d %*s %*d %*s %*s\n"", &dir, perms, &size) == 3) { + if (size > param1) { + total += 1; + } + } + printf(""%d\n"", total); + } + return 0; +} +",, +63c648055f004bf4898cf8bf,2023-01-23T06:37:45.311Z,General Question,v1,%*[^\n] what does ths do,,, +63c648055f004bf4898cf8bf,2023-01-23T06:44:42.098Z,Question from Code,v1,how do i ONLY skip the first line from stdin??,"int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + if (argc == 2) { + int total = 0; + char dir; + char perms[10]; + int size; + int param1 = strtol(argv[1], NULL, 10); + while (scanf(""%*[^\n] %c%10s %*d %*s %*s %d %*s %*d %*s %*s\n"", &dir, perms, &size) != EOF) { + if (size > param1) { + total += 1; + } + } + printf(""%d\n"", total); + } + + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + + // if (arg == 3) { + // char test[32]; + // scanf(""%s"", test); + // int result = check_permissions(); + // printf(""%d"", result); + // } + + return 0; +}",, +63c648055f004bf4898cf8bf,2023-01-23T07:14:18.140Z,Question from Code,v1,"why is (dir == ""-"") giving me errors?","int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + + + if (argc == 2) { + int total = 0; + char dir; + char perms[10]; + int size; + int param1 = strtol(argv[1], NULL, 10); + scanf(""%*[^\n]\n""); + while (scanf(""%c%10s %*d %*s %*s %d %*s %*d %*s %*s\n"", &dir, perms, &size) != EOF) { + if ((size > param1) && (dir == ""-"")) { + printf(""%c\n"", dir); + total += 1; + } + } + printf(""%d\n"", total); + } +return 0;",, +63c648055f004bf4898cf8bf,2023-01-23T17:08:26.133Z,General Question,v1,how to check a given integer is 9 digits using % 10 or / 10,,, +63c648055f004bf4898cf8bf,2023-01-23T17:12:11.923Z,Question from Code,v1,what does this return,"int help = 100000000 % 10; +printf(""%d"", help);",, +63c648055f004bf4898cf8bf,2023-01-23T17:12:47.748Z,Question from Code,v1,what does this return,"int help = 100923999 % 10; +printf(""%d"", help);",, +63c648055f004bf4898cf8bf,2023-01-23T17:13:38.461Z,General Question,v1,how do i check that a given integer is 9 digits?,,, +63c648055f004bf4898cf8bf,2023-01-23T17:14:02.648Z,General Question,v1,how do i check that a given integer is 9 digits with math operations?,,, +63c648055f004bf4898cf8bf,2023-01-23T17:14:54.890Z,General Question,v1,how do i use % 10 and / 10 to calculate the digits?,,, +63c648055f004bf4898cf8bf,2023-01-23T18:09:34.323Z,General Question,v1,how do i populate an array with a given integer?,,, +63c648055f004bf4898cf8bf,2023-01-23T18:11:11.614Z,General Question,v1,"how do i separate an integer 123 to array [1,2,3]",,, +63c648055f004bf4898cf8bf,2023-01-23T18:15:13.754Z,General Question,v1,how to check how many digits an integer has ,,, +63c648055f004bf4898cf8bf,2023-01-23T18:21:32.385Z,General Question,v1,if i % 2 == 1,,, +63c648055f004bf4898cf8bf,2023-01-23T18:24:48.819Z,Question from Code,v1,what does this print,printf(2 / 10);,, +63c648055f004bf4898cf8bf,2023-01-23T18:25:28.308Z,Question from Code,v1,waht does this print,"int i = 2/10; +printf(""%d"", i);",, +63c648055f004bf4898cf8bf,2023-01-23T18:25:47.965Z,Question from Code,v1,what does this print,"int i = 12/10; +printf(""%d"", i);",, +63c648055f004bf4898cf8bf,2023-01-23T18:31:46.467Z,General Question,v1,how to check if integer is divisible by 10,,, +63c648055f004bf4898cf8bf,2023-01-24T20:20:28.342Z,General Question,v1,how do i get the length of all argv,,, +63c648055f004bf4898cf8bf,2023-01-24T20:22:54.782Z,Explain Code,v1,,"int c = 6/2; +printf(""%d"", c);",, +63c648055f004bf4898cf8bf,2023-01-24T20:23:13.959Z,Question from Code,v1,what does this print?,"int c = 6/2; +printf(""%d"", c);",, +63c648055f004bf4898cf8bf,2023-01-24T20:43:45.840Z,General Question,v1,how do i pass in an array of argv elements,,, +63c648055f004bf4898cf8bf,2023-01-24T20:51:19.802Z,General Question,v1,how do i get all argv and pass it in an array to helper method,,, +63c648055f004bf4898cf8bf,2023-01-24T21:12:35.686Z,General Question,v1,how do i build an array from argv inputs?,,, +63c648055f004bf4898cf8bf,2023-01-24T21:15:38.019Z,General Question,v1,how do you loop through the argv elements,,, +63c648055f004bf4898cf8bf,2023-01-24T21:26:26.786Z,Question from Code,v1,what does this print,"int c = 3/2; +printf(""%d"", c);",, +63c648055f004bf4898cf8bf,2023-01-24T21:40:20.234Z,General Question,v1,how do you do ceiling?,,, +63c648055f004bf4898cf8bf,2023-01-24T21:40:48.446Z,General Question,v1,how do you do ceiling without importing libraries??????,,, +63c648055f004bf4898cf8bf,2023-01-24T21:45:35.946Z,Question from Code,v1,how do i make this return 2,"int c = 3/2; +printf(""%d"", c);",, +63c648055f004bf4898cf8bf,2023-01-24T21:46:07.804Z,Question from Code,v1,what does this return,"int c = 3/2.0; +printf(""%d"", c);",, +63c648055f004bf4898cf8bf,2023-01-24T21:47:22.857Z,Question from Code,v1,what does this return?,"int c; +c = (int) 3/2; +printf(""%d"", c);",, +63c648055f004bf4898cf8bf,2023-01-24T21:51:15.465Z,Question from Code,v1,what doe sthis return,"int c = (3 / 2) + ((3 % 2) != 0); +printf(""%d"", c);",, +63c648055f004bf4898cf8bf,2023-01-24T21:51:36.523Z,Question from Code,v1,what does this return?,"int c = (4 / 2) + ((4 % 2) != 0); +printf(""%d"", c);",, +63c648055f004bf4898cf8bf,2023-01-24T21:53:02.648Z,Question from Code,v1,rwjat does this reutn,"int c = int((4 / 2) + ((4 % 2) != 0)); +printf(""%d"", c);",, +63c648055f004bf4898cf8bf,2023-01-24T21:53:29.300Z,Question from Code,v1,waht does this reutnr?,"int c = (4 / 2) + ((4 % 2) != 0); +printf(""%d"", c);",, +63c648055f004bf4898cf8bf,2023-01-26T02:40:57.190Z,Question from Code,v1,what does this print?,"int c; +printf(""%d"", c+1);",, +63c648055f004bf4898cf8bf,2023-01-26T02:41:21.683Z,Question from Code,v1,what is the value of c+1?,"int c; +printf(""%d"", c+1);",, +63c648055f004bf4898cf8bf,2023-01-26T02:42:05.633Z,Question from Code,v1,what is c=+1?,"int c; +printf(""%d"", c=+1);",, +63c648055f004bf4898cf8bf,2023-01-26T02:43:38.469Z,Question from Code,v1,what is the value of c?,"int c; +c+=1;",, +63c648055f004bf4898cf8bf,2023-01-26T02:44:26.170Z,Question from Code,v1,what is the value of c?,"int c=0; +c+=1;",, +63c648055f004bf4898cf8bf,2023-02-01T00:21:59.604Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 3) { + printf(""Invalid\n""); + } + else { + if (s1 == s2) { + printf(""Same\n""); + } + else { + printf(""Different\n""); + } + } + return 0; +}",print Same if the 2 arguments are the same., +63c648055f004bf4898cf8bf,2023-02-01T00:23:29.435Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 3) { + printf(""Invalid\n""); + } + else { + if (argv[1] == argv[2]) { + printf(""Same\n""); + } + else { + printf(""Different\n""); + } + } + return 0; +}",compare argv[1] to argv[2] without using strcmp, +63c648055f004bf4898cf8bf,2023-02-01T02:38:37.983Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 3) { + fprintf(stderr, ""Usage: greeting message name\n""); + return 1; + } + char greeting[20]; + char *name = argv[2]; + + // Your code goes here + char *hi = argv[1]; + strncpy(greeting, hi, sizeof(greeting) - 1); + strncat(greeting, ' ', sizeof(greeting) - strlen(greeting) - 1); + strncat(greeting, name, sizeof(greeting) - strlen(greeting) - 1); + + + printf(""%s\n"", greeting); + return 0; +}",how do i concatenate a space after copying hi to greeting?, +63c648055f004bf4898cf8bf,2023-02-01T02:41:41.619Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + if (argc != 3) { + fprintf(stderr, ""Usage: greeting message name\n""); + return 1; + } + char greeting[20]; + char *name = argv[2]; + + // Your code goes here + char *hi = argv[1]; + strncpy(greeting, hi, sizeof(greeting) - 1); + strncat(greeting, "" "", sizeof(greeting) - strlen(greeting) - 1); + strncat(greeting, name, sizeof(greeting) - strlen(greeting) - 1); + + + printf(""%s\n"", greeting); + return 0; +} +",how do i make sure greeting is properly null terminated after strncpy call?, +63c648055f004bf4898cf8bf,2023-02-01T03:21:03.009Z,General Question,v1,how do you remove the last few question marks from a given string?,,, +63c648055f004bf4898cf8bf,2023-02-01T03:32:37.341Z,Help Fix Code,v1,,"int strip_q_marks(char *s) { + char *p; + p = strchr(s, '\0'); + if (s[p-s-1] != ""?"") { + return 0; + } + else { + printf(""%d\n"", p-s-1); + } +}","if p-s-1 != ""?"" return 0, else return the value", +63c648055f004bf4898cf8bf,2023-02-01T03:48:10.306Z,Help Fix Code,v1,,"int truncate(char *s, int n) { + int p = strlen(s); + if (p <= n) { + return 0; + } + else { + strncpy(s, s, n); + int q = strlen(s); + return p - q; + } +}",change s to be s of length at most n, +63c648055f004bf4898cf8bf,2023-02-01T03:51:53.085Z,Help Fix Code,v1,,"int truncate(char *s, int n) { + int p = strlen(s); + if (p <= n) { + return 0; + } + else { + strncpy(s, s, n); + s[n+1] = '\0'; + int q = strlen(s); + return p - q; + } +}",modify s to a string with max character of n in line 7., +63c648055f004bf4898cf8bf,2023-02-07T23:55:21.603Z,General Question,v1,Does fseek in c take in bits or bytes for offset?,,, +63c648055f004bf4898cf8bf,2023-02-08T00:00:36.056Z,General Question,v1,does fread take in bytes as element size?,,, +63c648055f004bf4898cf8bf,2023-02-08T00:06:32.721Z,General Question,v1,When you fseek with pointer by 0 distance with respect to end of file what value do you print?,,, +63c648055f004bf4898cf8bf,2023-02-08T00:07:43.882Z,Question from Code,v1,how many characters does this have?,"""Someone over there is calling you. we are going for work. take care of yourself.""",, +63c648055f004bf4898cf8bf,2023-02-08T02:10:16.478Z,Help Fix Code,v1,,"void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { + int pointer; + pointer = fseek(image, 10, SEEK_SET); + pixel_array_offset = fread(&pointer, 4, 1, image); + pointer = fseek(image, 18, SEEK_SET); + width = fread(&pointer, 4, 1, image); + pointer = fseek(image, 22, SEEK_SET); + height = fread(&pointer, 4, 1, image); +}","Why do i get this error? + +assignment to ‘int *’ from ‘size_t’ {aka ‘long unsigned int’} makes pointer from integer without a cast [-Wint-conversion] + 13 | pixel_array_offset = fread(&pointer, 4, 1, image);", +63c648055f004bf4898cf8bf,2023-02-08T02:14:49.296Z,Help Fix Code,v1,,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **m = malloc(sizeof(struct pixel*) * height); + for (int i = 0; i < height; i++) { + m[i] = malloc(sizeof(struct pixel) * width); + } + + fseek(image, pixel_array_offset, SEEK_SET); + fread(image, sizeof(width), height, m); + + // for (int j = 0; j < height; j++) { + // for (int k = 0; k < width; k++) { + // m[j][k] = image[pixel_array_offset]; + // } + // } + + return m; +}",Why is it not reading on line 8?, +63c648055f004bf4898cf8bf,2023-02-08T02:16:35.429Z,Help Fix Code,v1,,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **m = malloc(sizeof(struct pixel*) * height); + for (int i = 0; i < height; i++) { + m[i] = malloc(sizeof(struct pixel) * width); + } + + fseek(image, pixel_array_offset, SEEK_SET); + fread(image, sizeof(width), height, m); + + // for (int j = 0; j < height; j++) { + // for (int k = 0; k < width; k++) { + // m[j][k] = image[pixel_array_offset]; + // } + // } + + return m; +}","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.", +63c648055f004bf4898cf8bf,2023-02-08T02:30:02.875Z,Help Fix Code,v1,," for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + free(pixels[i][j]); + } + } + free(pixels);",why does this not free the memory i used for pixels?, +63c648055f004bf4898cf8bf,2023-02-12T01:12:22.825Z,Question from Code,v1,"why would you pass in User **user_ptr_add here +","/* + * 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) { + User *curr = user_ptr_add; + while (curr->next != NULL) { + curr = curr->next + } + return -1; +}",, +63c648055f004bf4898cf8bf,2023-02-12T01:13:05.745Z,Question from Code,v1,why is user_ptr_add passed in as a pointer to a pointer,"int create_user(const char *name, User **user_ptr_add) { + User *curr = user_ptr_add; + while (curr->next != NULL) { + curr = curr->next + } + return -1; +}",, +63c648055f004bf4898cf8bf,2023-02-12T01:13:53.332Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *curr = user_ptr_add; + while (curr->next != NULL) { + curr = curr->next + } + return -1; +}",traverse through the Users until it reaches the end, +63c648055f004bf4898cf8bf,2023-02-12T01:18:06.204Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, ""name""); + User *curr = *user_ptr_add; + while (curr->next != NULL) { + curr = curr->next; + } + return -1; +} +",how do i check if the given name cannot fit in the 'name' array (don't forget about the null terminator), +63c648055f004bf4898cf8bf,2023-02-12T01:18:33.413Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + User *curr = *user_ptr_add; + while (curr->next != NULL) { + curr = curr->next; + } + return -1; +} +", how do i check if the given name cannot fit in the 'name' array (don't forget about the null terminator), +63c648055f004bf4898cf8bf,2023-02-12T01:19:17.142Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + User *curr = *user_ptr_add; + while (curr->next != NULL) { + curr = curr->next; + } + return -1; +} +",how do i check that the given name cannot fit in the 'name' array by adding a check for the length of the name?, +63c648055f004bf4898cf8bf,2023-02-12T01:25:16.927Z,Explain Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) > MAX_NAME - 1) { + return 2; + } + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + User *curr = *user_ptr_add; + while (curr->next != NULL) { + if (curr->name == name) { + return 1; + } + curr = curr->next; + } + curr->next = new_user; + return 0; +}",, +63c648055f004bf4898cf8bf,2023-02-12T01:30:33.398Z,Question from Code,v1,why am i getting a segmentation fault with this code,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) > MAX_NAME - 1) { + return 2; + } + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + User *curr = *user_ptr_add; + while (curr->next != NULL) { + if (curr->name == name) { + return 1; + } + curr = curr->next; + } + curr->next = new_user; + return 0; +}",, +63c648055f004bf4898cf8bf,2023-02-12T01:32:30.054Z,Question from Code,v1,how do i allocate space for new_user->name,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) > MAX_NAME - 1) { + return 2; + } + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + User *curr = *user_ptr_add; + while (curr->next != NULL) { + if (curr->name == name) { + return 1; + } + curr = curr->next; + } + curr->next = new_user; + return 0; +}",, +63c648055f004bf4898cf8bf,2023-02-12T01:35:54.118Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) > MAX_NAME - 1) { + return 2; + } + User *new_user = malloc(sizeof(User)); + *new_user->name = malloc(sizeof(char) * MAX_NAME); + strcpy(new_user->name, name); + new_user->next = NULL; + User *curr = *user_ptr_add; + while (curr->next != NULL) { + if (curr->name == name) { + return 1; + } + curr = curr->next; + } + curr->next = new_user; + return 0; +} +",why am i getting a segmentation fault, +63c648055f004bf4898cf8bf,2023-02-12T01:37:55.584Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) > MAX_NAME - 1) { + return 2; + } + User *new_user = malloc(sizeof(User)); + new_user->name = malloc(sizeof(char) * MAX_NAME); + strcpy(new_user->name, name); + new_user->next = NULL; + User *curr = *user_ptr_add; + while (*curr->next != NULL) { + if *curr->name == name) { + return 1; + } + *curr = curr->next; + } + *curr->next = new_user; + return 0; +} +",not get segmentation fault, +63c648055f004bf4898cf8bf,2023-02-12T01:40:08.325Z,Question from Code,v1,how do i not get a segmentation fault with this...,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) > MAX_NAME - 1) { + return 2; + } + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + User *curr = *user_ptr_add; + while (curr->next != NULL) { + if (curr->name == name) { + return 1; + } + curr = curr->next; + } + curr->next = new_user; + return 0; +}",, +63c648055f004bf4898cf8bf,2023-02-12T01:44:03.595Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) > MAX_NAME - 1) { + return 2; + } + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + User *curr = *user_ptr_add; + if (curr == NULL) { + curr = new_user; + } else { + while (curr->next != NULL) { + if (curr->name == name) { + return 1; + } + curr = curr->next; + } + curr->next = new_user; + } + return 0; +}","/* + * 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) + */", +63c648055f004bf4898cf8bf,2023-02-12T01:47:16.214Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) > MAX_NAME - 1) { + return 2; + } + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + User *curr = *user_ptr_add; + if (curr == NULL) { + *user_ptr_add = new_user; + } else { + while (curr->next != NULL) { + if (strcmp(curr->name, name) != 0) { + return 1; + } else { + curr = curr->next; + }} + curr->next = new_user; + } + return 0; +}",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), +63c648055f004bf4898cf8bf,2023-02-12T01:50:29.722Z,General Question,v1,what happens when you make a struct with members and don't initialize all its contents?,,, +63c648055f004bf4898cf8bf,2023-02-12T03:08:13.233Z,General Question,v1,"how do you do ""pass"" in c",,, +63c648055f004bf4898cf8bf,2023-02-12T03:16:08.637Z,Question from Code,v1,is this the right way to return 1 if the file does not exist or cannot be opened?," +int update_pic(User *user, const char *filename) { + // file cannot be opened + FILE *file = fopen(filename, ""rb""); + if (file == NULL) { + return 2; + } + return -1; +} +",, +63c648055f004bf4898cf8bf,2023-02-12T03:16:40.271Z,Question from Code,v1,does this return 2 if the file does not exist or cannot be opened?," +int update_pic(User *user, const char *filename) { + // file cannot be opened + FILE *file = fopen(filename, ""rb""); + if (file == NULL) { + return 2; + } + return -1; +} +",, +63c648055f004bf4898cf8bf,2023-02-12T03:27:38.117Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + // check if name cannot fit array + if (strlen(name) > MAX_NAME - 1) { + return 2; + } + // allocate space for new user and make new user + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + User *curr = *user_ptr_add; + // if there are no users, make new_user the first of linked list + if (curr == NULL) { + *user_ptr_add = new_user; + } + // else, traverse until the end of the list and add new_user to the end + else { + while (curr->next != NULL) { + if (strcmp(curr->name, name) == 0) { + return 1; + } else { + curr = curr->next; + } + } curr->next = new_user; + } + return 0; +} +",return 1 if a user by this name already exists in this list, +63c648055f004bf4898cf8bf,2023-02-12T04:17:32.693Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + User *curr1 = head; + User *curr2 = head; + User *store1 = NULL; + User *store2 = NULL; + while (curr1 != NULL) { + if (strcmp(curr1->name, name1) == 0) { + store1 = curr1; + } + else { + curr1 = curr1->next; + } + } + while (curr2 != NULL) { + if (strcmp(curr2->name, name2) == 0) { + store2 = curr2; + } + else { + curr2 = curr2->next; + } + } + if (store1 == NULL || store2 == NULL) { + return 4; + } + else if (store1 == store2) { + return 3; + } + else if (store1->friends[10] != NULL || store2->friends[10] != NULL) { + return 2; + } + else { + + } + +// * - 0 on success. +// * - 1 if the two users are already friends. + return 0; +}",how do i return 1 if the two users are already friends?, +63c648055f004bf4898cf8bf,2023-02-12T04:35:27.738Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + User *curr1 = head; + User *curr2 = head; + User *store1 = NULL; + User *store2 = NULL; + while (curr1 != NULL) { + if (strcmp(curr1->name, name1) == 0) { + store1 = curr1; + } + else { + curr1 = curr1->next; + } + } + while (curr2 != NULL) { + if (strcmp(curr2->name, name2) == 0) { + store2 = curr2; + } + else { + curr2 = curr2->next; + } + } + if (store1 == NULL || store2 == NULL) { + return 4; + } + else if (store1 == store2) { + return 3; + } + else if (store1->friends[MAX_FRIENDS] != NULL || store2->friends[MAX_FRIENDS] != NULL) { + return 2; + } + else { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (store1->friends[i] == store2) { + return 1; + } + else if (store1->friends[i] != NULL) { + store1 = store1->next; + } + else { + store1->friends[i] = store2; + for (int j = 0; j < MAX_FRIENDS; j++) { + if (store2->friends[j] != NULL) { + store2 = store2->next; + } + else { + store2->friends[j] = store1; + } + } + } + } + } + return 0; +}","/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */", +63c648055f004bf4898cf8bf,2023-02-12T04:37:28.617Z,Explain Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + User *curr1 = head; + User *curr2 = head; + User *store1 = NULL; + User *store2 = NULL; + while (curr1 != NULL) { + if (strcmp(curr1->name, name1) == 0) { + store1 = curr1; + } + else { + curr1 = curr1->next; + } + } + while (curr2 != NULL) { + if (strcmp(curr2->name, name2) == 0) { + store2 = curr2; + } + else { + curr2 = curr2->next; + } + } + if (store1 == NULL || store2 == NULL) { + return 4; + } + else if (store1 == store2) { + return 3; + } + else if (store1->friends[MAX_FRIENDS] != NULL || store2->friends[MAX_FRIENDS] != NULL) { + return 2; + } + else { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (store1->friends[i] == store2) { + return 1; + } + else if (store1->friends[i] != NULL) { + store1 = store1->next; + } + else { + store1->friends[i] = store2; + for (int j = 0; j < MAX_FRIENDS; j++) { + if (store2->friends[j] != NULL) { + store2 = store2->next; + } + else { + store2->friends[j] = store1; + } + } + } + } + } + return 0; +}",, +63c648055f004bf4898cf8bf,2023-02-12T04:38:34.825Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + User *curr1 = head; + User *curr2 = head; + User *store1 = NULL; + User *store2 = NULL; + while (curr1 != NULL) { + if (strcmp(curr1->name, name1) == 0) { + store1 = curr1; + } + else { + curr1 = curr1->next; + } + } + while (curr2 != NULL) { + if (strcmp(curr2->name, name2) == 0) { + store2 = curr2; + } + else { + curr2 = curr2->next; + } + } + if (store1 == NULL || store2 == NULL) { + return 4; + } + else if (store1 == store2) { + return 3; + } + else if (store1->friends[MAX_FRIENDS-1] != NULL || store2->friends[MAX_FRIENDS-1] != NULL) { + return 2; + } + else { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (store1->friends[i] == store2) { + return 1; + } + else if (store1->friends[i] != NULL) { + store1 = store1->next; + } + else { + store1->friends[i] = store2; + for (int j = 0; j < MAX_FRIENDS; j++) { + if (store2->friends[j] != NULL) { + store2 = store2->next; + } + else { + store2->friends[j] = store1; + } + } + } + } + } + return 0; +}","/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */", +63c648055f004bf4898cf8bf,2023-02-12T04:48:20.592Z,General Question,v1,how do you write file to standard output?,,, +63c648055f004bf4898cf8bf,2023-02-12T05:36:13.203Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + User *curr1 = head; + User *curr2 = head; + while (curr1 != NULL && strcmp(curr1->name, name1) != 0) { + curr1 = curr1->next; + } + while (curr2 != NULL && strcmp(curr2->name, name2) != 0) { + curr2 = curr2->next; + } + if (curr1 == NULL || curr2 == NULL) { + return 4; + } + else if (curr1 == curr2) { + return 3; + } + else if (curr1->friends[MAX_FRIENDS-1] != NULL || curr2->friends[MAX_FRIENDS-1] != NULL) { + return 2; + } + else { + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr1->friends[i] == curr2) { + return 1; + } + else if (curr1->friends[i] == NULL) { + curr1->friends[i] = curr2; + for (int j = 0; j < MAX_FRIENDS; j++) { + if (curr2->friends[j] == NULL) { + curr2->friends[j] = curr1; + } + else { + curr2 = curr2->next; + } + } + } + else { + curr1 = curr1->next; + } + } + } + return 0; +}",make 2 valid users friends if they are not already friends, +63c648055f004bf4898cf8bf,2023-02-12T08:36:51.126Z,General Question,v1,"gives a nice human-readable representation of the date and time of each post in the format, ""Friday Jan 27 23:01:00 2023"". Use the time and ctime functions to find the current time and convert a time value to a string, respectively. ",,, +63c648055f004bf4898cf8bf,2023-02-12T08:39:38.692Z,Question from Code,v1,how do i use ctime to convert this into a string?,time_t current_time = time(NULL);,, +63c648055f004bf4898cf8bf,2023-02-12T08:49:34.825Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if (author->friends[i] == target) { + Post *new_post = malloc(sizeof(Post)); + strncpy(new_post->author, author->name, MAX_NAME); + new_post->author[MAX_NAME-1] = '\0'; + strcpy(new_post->contents, contents); + // time function to get the current time and store it in a variable + // Then, use the ctime function to convert the time value to a string. + time_t current_time = time(NULL); + // char* time_string = ctime(¤t_time); + new_post->date = current_time; + new_post->next = target->first_post; + target->first_post = new_post; + return 0; + } + } + return 1; +}","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", +63c648055f004bf4898cf8bf,2023-02-12T08:51:16.212Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + if (author == NULL || target == NULL) { + return 2; + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if (author->friends[i] == target) { + Post *new_post = malloc(sizeof(Post)); + strncpy(new_post->author, author->name, MAX_NAME); + new_post->author[MAX_NAME-1] = '\0'; + new_post->contents = contents; + // time function to get the current time and store it in a variable + // Then, use the ctime function to convert the time value to a string. + time_t current_time = time(NULL); + // char* time_string = ctime(¤t_time); + new_post->date = current_time; + new_post->next = target->first_post; + target->first_post = new_post; + return 0; + } + } + return 1; +}","error: assignment to ‘time_t *’ {aka ‘long int *’} from ‘time_t’ {aka ‘long int’} makes pointer from integer without a cast [-Werror=int- +conversion] + 230 | new_post->date = current_time;", +63c648055f004bf4898cf8bf,2023-02-13T06:13:38.791Z,Question from Code,v1,"What is the line to compile it into an executable named a.out that it will print the value 5 to stdout? (You do not need to include the options to display warnings, build the symbol table for the debugger or select the C standard.)","#include +int main() { + + #ifdef MACRO + printf(""%d\n"", MACRO); + #endif + return 0; +}",, +63c648055f004bf4898cf8bf,2023-02-13T06:14:46.588Z,Explain Code,v1,,"#define MAXNAME = 32; + +and then the declaration + +char name[MAXNAME]; +",, +63c648055f004bf4898cf8bf,2023-02-13T06:15:00.894Z,Question from Code,v1,"in the program. What will this declaration line become after the program has passed +through the C pre-processor? +","#define MAXNAME = 32; + +and then the declaration + +char name[MAXNAME]; +",, +63c648055f004bf4898cf8bf,2023-02-13T06:17:21.767Z,Question from Code,v1,What will be the output of the following program fragment?,"#define SUPERVISOR(regular) regular + 5 +int main() { + int regular_pay = 20; + int hours_worked = 10; + printf(""pay is %d\n"", (hours_worked * SUPERVISOR(regular_pay))); + // rest omitted +}",, +63c648055f004bf4898cf8bf,2023-02-13T06:27:07.139Z,Question from Code,v1,What is the line to compile it into an executable named a.out that it will print the value 5 to stdout? ,"#include +int main() { + + #ifdef MACRO + printf(""%d\n"", MACRO); + #endif + return 0; +}",, +63c648055f004bf4898cf8bf,2023-02-14T23:23:48.285Z,General Question,v1,how to you hard code strncpy?,,, +63c648055f004bf4898cf8bf,2023-02-14T23:28:03.100Z,Question from Code,v1,what does this print?,"src = ""abcdefghijklmno"" +strncpy(dest, src, 10); +dest[9] = '\0'; +printf(""%s"", dest);",, +63c648055f004bf4898cf8bf,2023-02-14T23:28:42.039Z,Question from Code,v1,does dest have a null terminator,"src = ""abcdefghijklmno"" +strncpy(dest, src, 10); +dest[9] = '\0'; +printf(""%s"", dest);",, +63c648055f004bf4898cf8bf,2023-02-14T23:29:09.821Z,Question from Code,v1,what does this print,"src = ""abcdefghijklmno"" +strncpy(dest, src, 10); +dest[7] = '\0'; +printf(""%s"", dest);",, +63c648055f004bf4898cf8bf,2023-02-14T23:47:28.191Z,Explain Code,v1,,"#include + +#define SIZE 4 +#define OVERFLOW 4 + +int main() { + int index = 0; + int i; + int before[SIZE] = {10, 10, 10, 10}; + int a[SIZE] = {0, 0, 0, 0}; + int after[SIZE] = {10, 10, 10, 10}; + + printf(""Address of the variables:\n""); + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &after[%d]\n"", (unsigned long) &after[index], index); + } + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &a[%d]\n"", (unsigned long) &a[index], index); + } + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &before[%d]\n"", (unsigned long) &before[index], index); + } + printf(""%lx -> &i\n"", (unsigned long)&i); + printf(""%lx -> &index\n"", (unsigned long)&index); + printf(""\n""); + + + printf(""Initial values:\n""); + printf(""i = %d\n"", i); + printf(""before = {%d, %d, %d, %d}\n"", before[0], before[1], before[2], before[3]); + printf(""a = {%d, %d, %d, %d}\n"", a[0], a[1], a[2], a[3]); + printf(""after = {%d, %d, %d, %d}\n"", after[0], after[1], after[2], after[3]); + printf(""\n""); + + + for (i = 0; i < OVERFLOW; i++) { + a[i] = i * 10; + printf(""i = %d\n"", i); + printf(""before = {%d, %d, %d, %d}\n"", before[0], before[1], before[2], before[3]); + printf(""a = {%d, %d, %d, %d}\n"", a[0], a[1], a[2], a[3]); + printf(""after = {%d, %d, %d, %d}\n"", after[0], after[1], after[2], after[3]); + } + + return 0; +} +",, +63c648055f004bf4898cf8bf,2023-02-14T23:52:08.406Z,Question from Code,v1,what does this print?,"#include + +#define SIZE 4 +#define OVERFLOW 4 + +int main() { + int index = 0; + int i; + int before[SIZE] = {10, 10, 10, 10}; + int a[SIZE] = {0, 0, 0, 0}; + int after[SIZE] = {10, 10, 10, 10}; + + printf(""Address of the variables:\n""); + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &after[%d]\n"", (unsigned long) &after[index], index); + } + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &a[%d]\n"", (unsigned long) &a[index], index); + } + for (index = 0; index < SIZE; index++) { + printf(""%lx -> &before[%d]\n"", (unsigned long) &before[index], index); + } + printf(""%lx -> &i\n"", (unsigned long)&i); + printf(""%lx -> &index\n"", (unsigned long)&index); + printf(""\n""); + + + printf(""Initial values:\n""); + printf(""i = %d\n"", i); + printf(""before = {%d, %d, %d, %d}\n"", before[0], before[1], before[2], before[3]); + printf(""a = {%d, %d, %d, %d}\n"", a[0], a[1], a[2], a[3]); + printf(""after = {%d, %d, %d, %d}\n"", after[0], after[1], after[2], after[3]); + printf(""\n""); + + + for (i = 0; i < OVERFLOW; i++) { + a[i] = i * 10; + printf(""i = %d\n"", i); + printf(""before = {%d, %d, %d, %d}\n"", before[0], before[1], before[2], before[3]); + printf(""a = {%d, %d, %d, %d}\n"", a[0], a[1], a[2], a[3]); + printf(""after = {%d, %d, %d, %d}\n"", after[0], after[1], after[2], after[3]); + } + + return 0; +} +",, +63c648055f004bf4898cf8bf,2023-02-15T19:30:18.827Z,General Question,v1,"if i set the middle of a char array to '\0', would the values after it become null values as well ",,, +63c648055f004bf4898cf8bf,2023-02-15T19:31:44.031Z,General Question,v1,"if i have char *s with size 10 and s[6] is originally 'a', then make s[5]='\0', what would s[6] be",,, +63c648055f004bf4898cf8bf,2023-03-02T16:42:03.915Z,Question from Code,v1,What are all possible combinations that the output can be,"#include +#include +#include + +int main() { + int ret; + + printf(""A\n""); + + ret = fork(); + + printf(""B\n""); + + if (ret < 0) { + perror(""fork""); + exit(1); + } else if (ret == 0) { + printf(""C\n""); + } else { + printf(""D\n""); + } + + printf(""E\n""); + return 0; +} +",, +63c648055f004bf4898cf8bf,2023-03-02T16:42:37.070Z,Question from Code,v1,"can the output be something other than A B D E B C E +","#include +#include +#include + +int main() { + int ret; + + printf(""A\n""); + + ret = fork(); + + printf(""B\n""); + + if (ret < 0) { + perror(""fork""); + exit(1); + } else if (ret == 0) { + printf(""C\n""); + } else { + printf(""D\n""); + } + + printf(""E\n""); + return 0; +} +",, +63c648055f004bf4898cf8bf,2023-03-02T17:37:46.241Z,Question from Code,v1,how do i modify the program so that each process creates exactly one a new process?,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +",, +63c648055f004bf4898cf8bf,2023-03-02T17:38:51.445Z,Help Fix Code,v1,,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +",modify the program so that each process creates exactly one a new process., +63c648055f004bf4898cf8bf,2023-03-02T17:40:25.491Z,Help Fix Code,v1,,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0) { + perror(""fork""); + exit(1); + } + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +} +",how do you add an if statement that checks if the process is a child, +63c648055f004bf4898cf8bf,2023-03-12T21:32:26.994Z,General Question,v2,how do you check if a line from fgets starts with a tab?,,, +63c648055f004bf4898cf8bf,2023-03-12T21:36:30.056Z,Question from Code,v2,what is line[0] from this?,"char line[50]; +line = "" hello"";",, +63c648055f004bf4898cf8bf,2023-03-12T21:37:15.871Z,Question from Code,v2, what is line[0] from this?,"char line[50]; +strcpy(line, "" hello"");",, +63c648055f004bf4898cf8bf,2023-03-12T21:38:19.819Z,Question from Code,v2,what does this return,"char line[50]; +strcpy(line, "" hello""); +if (strcmp(line[0], ""\t"") == 0) { + printf(""yay""); +}",, +63c648055f004bf4898cf8bf,2023-03-12T21:39:18.681Z,Question from Code,v2,what does this return,"char line[50]; +strcpy(line, "" hello""); +if (line[0] == ""\t"") { + printf(""yay""); +}",, +63c648055f004bf4898cf8bf,2023-03-12T21:39:49.962Z,Question from Code,v2,what does this return,"char line[50]; +strcpy(line, "" hello""); +if (line[0] == \t) { + printf(""yay""); +}",, +63c648055f004bf4898cf8bf,2023-03-12T22:24:14.703Z,Explain Code,v2,,"char *args_to_string(char **args, char *buffer, int size) { + buffer[0] = '\0'; + int i = 0; + while (args[i] != NULL) { + strncat(buffer, args[i], size - strlen(buffer)); + strncat(buffer, "" "", size - strlen(buffer)); + i++; + } + return buffer; +}",, +63c6480a5f004bf4898cf8ec,2023-02-10T13:19:10.261Z,General Question,v1,explain how fseek works,,, +63c6480a5f004bf4898cf8ec,2023-02-10T19:58:14.882Z,General Question,v1,is there a difference between exit(0) and return 0 from main in C,,, +63c6480a5f004bf4898cf8ec,2023-02-10T20:00:27.960Z,General Question,v1,how do i free malloced memory,,, +63c6480a5f004bf4898cf8ec,2023-03-10T19:51:58.564Z,General Question,v2,"explain the C dup2 function +",,, +63c6480c5f004bf4898cf8fb,2023-01-31T15:51:12.017Z,Question from Code,v1,what does this print,"char **build_month_list(){ + // char *month1 = malloc(sizeof(char) * 8); + // char *month2 = malloc(sizeof(char) * 9); + // char *month3 = malloc(sizeof(char) * 6); + + // strcpy(month1, ""January""); + // strcpy(month2, ""February""); + // strcpy(month3, ""March""); + + char month1[] = ""January""; + char month2[] = ""Fenruary""; + char month3[] = ""March""; + + char *month_list[3] = {month1, month2, month3}; + + return &month_list; + } + + + + char **months = build_month_list(); + for(int i = 0; i < 3; i++) { + printf(""%s "", months[i]); + } + printf(""\n""); + + for(int i = 0; i < 3; i++) { + months[i][3] = '\0'; + printf(""%s "", months[i]); + } + printf(""\n"");",, +63c6480c5f004bf4898cf8fb,2023-02-02T15:16:34.076Z,Question from Code,v1,Have I initialized p1 correctly,"#define MAX_AREA_SIZE 16 +#include +#include +#include + +struct faculty { + char *name; + char area[MAX_AREA_SIZE]; + int num_students; +}; + + + + + + + +int main() { + // Declare a struct faculty named p1. + struct faculty p1; + + + // Initialize p1 to represent Professor Roger Grosse, whose research area + // is ML (Machine Learning). He is supervising 11 graduate students. + strcpy(p1.name, &""Roger Grose""); + strcpy(p1.area, ""Machine Learning""); + p1.num_students = 11; +}",, +63c6480c5f004bf4898cf8fb,2023-02-02T15:21:36.177Z,Question from Code,v1,"Have I initialized p1 correctly +","#define MAX_AREA_SIZE 16 +#include +#include +#include + +struct faculty { + char *name; + char area[MAX_AREA_SIZE]; + int num_students; +}; + + + + + + + +int main() { + // Declare a struct faculty named p1. + struct faculty p1; + + + // Initialize p1 to represent Professor Roger Grosse, whose research area + // is ML (Machine Learning). He is supervising 11 graduate students. + char *prof = malloc(sizeof(char) * 12); + prof = ""Roger Grosse""; + p1.name = prof; + strcpy(p1.area, ""Machine Learning""); + p1.num_students = 11; +}",, +63c6480c5f004bf4898cf8fb,2023-02-02T15:23:11.333Z,Question from Code,v1,Have I initialized p1 correctly,"#define MAX_AREA_SIZE 16 +#include +#include +#include + +struct faculty { + char *name; + char area[MAX_AREA_SIZE]; + int num_students; +}; + + + + + + + +int main() { + // Declare a struct faculty named p1. + struct faculty p1; + + + // Initialize p1 to represent Professor Roger Grosse, whose research area + // is ML (Machine Learning). He is supervising 11 graduate students. + char *prof = malloc(sizeof(char) * 12); + strpy(prof, ""Roger Grosse""); + p1.name = prof; + strcpy(p1.area, ""Machine Learning""); + p1.num_students = 11; +}",, +63c6480c5f004bf4898cf8fb,2023-02-03T02:41:13.146Z,Question from Code,v1,Why does my truncate function not change s,"#include +#include +#include + +/* + Write a function named truncate() that takes a string s and a + non-negative integer n. If s has more than n characters (not including the + null terminator), the function should truncate s at n characters and + return the number of characters that were removed. If s has n or + fewer characters, s is unchanged and the function returns 0. For example, + if s is the string ""function"" and n is 3, then truncate() changes s to + the string ""fun"" and returns 5. +*/ + +int truncate(char *s, int n){ + // char *temp = s; + int length = strlen(s); + if (length <= n){ + return 0; + } else{ + s = malloc(sizeof(char) * n); + s[n] = ""\0""; + return length - n; + } +} + + +int main(int argc, char **argv) { + /* Do not change the main function */ + if (argc != 3) { + fprintf(stderr, ""Usage: truncate number string\n""); + return 1; + } + int amt = strtol(argv[1], NULL, 10); + + char *target = argv[2]; + + int soln_val = truncate(target, amt); + printf(""%d %s\n"", soln_val, target); + + return 0; +}",, +63c6480c5f004bf4898cf8fb,2023-02-03T02:43:39.994Z,Question from Code,v1,How do I change the value of s ,"#include +#include +#include + +/* + Write a function named truncate() that takes a string s and a + non-negative integer n. If s has more than n characters (not including the + null terminator), the function should truncate s at n characters and + return the number of characters that were removed. If s has n or + fewer characters, s is unchanged and the function returns 0. For example, + if s is the string ""function"" and n is 3, then truncate() changes s to + the string ""fun"" and returns 5. +*/ + +int truncate(char *s, int n){ + // char *temp = s; + int length = strlen(s); + if (length <= n){ + return 0; + } else{ + // s = malloc(sizeof(char) * n); + s[n] = ""\0""; + return length - n; + } +} + + +int main(int argc, char **argv) { + /* Do not change the main function */ + if (argc != 3) { + fprintf(stderr, ""Usage: truncate number string\n""); + return 1; + } + int amt = strtol(argv[1], NULL, 10); + + char *target = argv[2]; + + int soln_val = truncate(target, amt); + printf(""%d %s\n"", soln_val, target); + + return 0; +}",, +63c6480c5f004bf4898cf8fb,2023-02-03T02:51:45.702Z,Question from Code,v1,How do I create the function truncate from the information in lines 6 to 12,"#include +#include +#include + +/* + Write a function named truncate() that takes a string s and a + non-negative integer n. If s has more than n characters (not including the + null terminator), the function should truncate s at n characters and + return the number of characters that were removed. If s has n or + fewer characters, s is unchanged and the function returns 0. For example, + if s is the string ""function"" and n is 3, then truncate() changes s to + the string ""fun"" and returns 5. +*/ + +int truncate(char *s, int n){ + // char *temp = s; + int length = strlen(s); + if (length <= n){ + return 0; + } else{ + // s = malloc(sizeof(char) * n); + s[n] = ""\0""; + return length - n; + } +} + + +int main(int argc, char **argv) { + /* Do not change the main function */ + if (argc != 3) { + fprintf(stderr, ""Usage: truncate number string\n""); + return 1; + } + int amt = strtol(argv[1], NULL, 10); + + char *target = argv[2]; + + int soln_val = truncate(target, amt); + printf(""%d %s\n"", soln_val, target); + + return 0; +}",, +63c6480c5f004bf4898cf8fb,2023-02-03T02:52:46.568Z,Question from Code,v1,How do I change s in truncate to a different string,"#include +#include +#include + +/* + Write a function named truncate() that takes a string s and a + non-negative integer n. If s has more than n characters (not including the + null terminator), the function should truncate s at n characters and + return the number of characters that were removed. If s has n or + fewer characters, s is unchanged and the function returns 0. For example, + if s is the string ""function"" and n is 3, then truncate() changes s to + the string ""fun"" and returns 5. +*/ + +int truncate(char *s, int n){ + // char *temp = s; + int length = strlen(s); + if (length <= n){ + return 0; + } else{ + // s = malloc(sizeof(char) * n); + s[n] = ""\0""; + return length - n; + } +} + + +int main(int argc, char **argv) { + /* Do not change the main function */ + if (argc != 3) { + fprintf(stderr, ""Usage: truncate number string\n""); + return 1; + } + int amt = strtol(argv[1], NULL, 10); + + char *target = argv[2]; + + int soln_val = truncate(target, amt); + printf(""%d %s\n"", soln_val, target); + + return 0; +}",, +63c6480c5f004bf4898cf8fb,2023-02-03T02:53:46.594Z,Help Fix Code,v1,,"#include +#include +#include + +/* + Write a function named truncate() that takes a string s and a + non-negative integer n. If s has more than n characters (not including the + null terminator), the function should truncate s at n characters and + return the number of characters that were removed. If s has n or + fewer characters, s is unchanged and the function returns 0. For example, + if s is the string ""function"" and n is 3, then truncate() changes s to + the string ""fun"" and returns 5. +*/ + +int truncate(char *s, int n){ + // char *temp = s; + int length = strlen(s); + if (length <= n){ + return 0; + } else{ + // s = malloc(sizeof(char) * n); + s[n] = ""\0""; + return length - n; + } +} + + +int main(int argc, char **argv) { + /* Do not change the main function */ + if (argc != 3) { + fprintf(stderr, ""Usage: truncate number string\n""); + return 1; + } + int amt = strtol(argv[1], NULL, 10); + + char *target = argv[2]; + + int soln_val = truncate(target, amt); + printf(""%d %s\n"", soln_val, target); + + return 0; +}","Write a function named truncate() that takes a string s and a + non-negative integer n. If s has more than n characters (not including the + null terminator), the function should truncate s at n characters and + return the number of characters that were removed. If s has n or + fewer characters, s is unchanged and the function returns 0. For example, + if s is the string ""function"" and n is 3, then truncate() changes s to + the string ""fun"" and returns 5.", +63c6480c5f004bf4898cf8fb,2023-02-09T06:02:16.610Z,Question from Code,v1,why do I get a segmentation fault in this program,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= 32){ + return 2; + } + + User *curr = *user_ptr_add; + while(curr->next != NULL){ + if (strcmp(name, curr->name) == 0){ + return 1; + } + curr = curr->next; + } + + User new_person; + strcpy(new_person.name, name); + curr->next = &new_person; + return 0; + +}",, +63c6480c5f004bf4898cf8fb,2023-02-09T06:15:40.485Z,Question from Code,v1,Why am I receiving a segmentation fault,"int create_user(const char *name, User **user_ptr_add) { + if (strlen(name) >= 32){ + return 2; + } + + User *curr = *user_ptr_add; + while(curr->next != NULL){ + if (strcmp(name, curr->name) == 0){ + return 1; + } + curr = curr->next; + } + + User *new_person; + new_person = malloc(sizeof(User)); + strcpy(new_person->name, name); + curr->next = new_person; + return 0; + +}",, +63c6480c5f004bf4898cf8fb,2023-02-10T19:14:21.392Z,Question from Code,v1,What is the error in my code,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel *values = malloc(sizeof(struct pixel*) * height); + + for(int i = 0; i < height; i++){ + struct pixel* values[i] = malloc(sizeof(unsigned char) * 3); + } + + fseek(image, pixel_array_offset, SEEK_SET); + for(int i = 0; i < height; i++){ + for(int j = 0; j < 3; j++){ + unsigned char val; + fread(val, sizeof(unsigned char), 1, image); + values[i][j] = val; + } + } + return &values[0]; +}",, +63c6480c5f004bf4898cf8fb,2023-02-11T18:50:37.176Z,Question from Code,v1,What is wrong with my code,"int make_friends(const char *name1, const char *name2, User *head) { + User user1 = NULL; + User user2 = NULL; + while(head != NULL){ + if (strcmp(name1, head->name) == 0){ + user1 = *head; + } else if (strcmp(name2, head->name) == 0){ + user2 = *head; + } else{ + head = head->next; + } + } + + if ((user1 == NULL) || (user2 == NULL)){ + return 4; + } + + if (strcmp(name1, name2) == 0){ + return 3; + } + + if ((strlen(user1.friends) == 10) || (strlen(user2.friends) == 10)){ + return 2; + } + + for(int i = 0; i < strlen(user1.friends); i++){ + if (strcmp(name2, user1.friends[i]->name) == 0){ + for(int j = 0; j < strlen(user2.friends); j++){ + if (strcmp(name1, user2.friends[j]->name) == 0){ + return 1; + } + } + } + } + + user1.friends[strlen(user1.friends)] = &user2; + user2.friends[strlen(user2.friends)] = &user1; + return 0; + + + +}",, +63c6480c5f004bf4898cf8fb,2023-02-11T19:18:47.862Z,Question from Code,v1,What's wrong with my code,"int make_friends(const char *name1, const char *name2, User *head) { + User *user1 = NULL; + User *user2 = NULL; + while(head != NULL){ + if (strcmp(name1, head->name) == 0){ + user1 = head; + } else if (strcmp(name2, head->name) == 0){ + user2 = head; + } else{ + head = head->next; + } + } + + if ((user1 == NULL) || (user2 == NULL)){ + return 4; + } + + if (strcmp(name1, name2) == 0){ + return 3; + } + + if ((strlen(user1.friends) == 10) || (strlen(user2.friends) == 10)){ + return 2; + } + + for(int i = 0; i < strlen(user1.friends); i++){ + if (strcmp(name2, user1.friends[i]->name) == 0){ + for(int j = 0; j < strlen(user2.friends); j++){ + if (strcmp(name1, user2.friends[j]->name) == 0){ + return 1; + } + } + } + } + + user1.friends[strlen(user1.friends)] = &user2; + user2.friends[strlen(user2.friends)] = &user1; + return 0; + + + +}",, +63c6480c5f004bf4898cf8fb,2023-02-11T19:22:00.554Z,Question from Code,v1,What's wrong with my code,"int make_friends(const char *name1, const char *name2, User *head) { + User *user1 = NULL; + User *user2 = NULL; + while(head != NULL){ + if (strcmp(name1, head->name) == 0){ + user1 = head; + } else if (strcmp(name2, head->name) == 0){ + user2 = head; + } else{ + head = head->next; + } + } + + if ((user1 == NULL) || (user2 == NULL)){ + return 4; + } + + if (strcmp(name1, name2) == 0){ + return 3; + } + + if ((strlen(user1.friends) == 10) || (strlen(user2.friends) == 10)){ + return 2; + } + + for(int i = 0; i < strlen(user1.friends); i++){ + if (strcmp(name2, user1.friends[i]->name) == 0){ + for(int j = 0; j < strlen(user2.friends); j++){ + if (strcmp(name1, user2.friends[j]->name) == 0){ + return 1; + } + } + } + } + + user1.friends[strlen(user1.friends)] = user2; + user2.friends[strlen(user2.friends)] = user1; + return 0; + + + +}",, +63c6480c5f004bf4898cf8fb,2023-02-13T22:12:22.292Z,Question from Code,v1,What does my code print,"#define SUPERVISOR(regular) regular + 5 +int main() { + int regular_pay = 20; + int hours_worked = 10; + printf(""pay is %d\n"", (hours_worked * SUPERVISOR(regular_pay))); +}",, +63c6480c5f004bf4898cf8fb,2023-02-15T00:06:05.191Z,Question from Code,v1,Why is it that the date stored in now is not correct?,"int make_post(const User *author, User *target, char *contents) { + if ((author == NULL) || (target == NULL)){ + return 2; + } + + int author_length = 0; + int target_length = 0; + for (int i = 0; i < 10; i++){ + if (author->friends[i] != NULL){ + author_length = author_length + 1; + } + } + for (int i = 0; i < 10; i++){ + if (author->friends[i] != NULL){ + target_length = target_length + 1; + } + } + int are_friends = 0; // are_friends = 1 for no and 0 for yes + for (int i = 0; i < author_length; i++){ + if (strcmp(target->name, author->friends[i]->name) == 0){ + for (int j = 0; j < target_length; j++){ + if (strcmp(author->name, target->friends[i]->name) == 0){ + are_friends = 1; + } + } + } + } + if (are_friends == 0){ + return 1; + } + + Post *new_first_post = malloc(sizeof(Post)); + strcpy(new_first_post->author, author->name); + new_first_post->contents = contents; + time_t now = time(NULL); + new_first_post->date = &now; + new_first_post->next = target->first_post; + target->first_post = new_first_post; + return 0; +} +",, +63c6480c5f004bf4898cf8fb,2023-02-15T08:37:50.817Z,Question from Code,v1,Why do I get a segmentation fault,"int delete_user(const char *name, User **user_ptr_del) { + User *curr = *user_ptr_del; + User *prev = NULL; + + while((strcmp(name, curr->name) != 0) || (curr != NULL)){ + prev = curr; + curr = curr->next; + } + + if (curr == NULL){ + return 1; + } + + if (prev == NULL){ + curr->next = NULL; + } else{ + prev->next = curr->next; + curr->next = NULL; + } + + int length = 0; + for(int i = 0; i < 10; i++){ + if (curr->friends[i] != NULL){ + length = length + 1; + } + } + + for(int i = 0; i < length; i++){ + int length2 = 0; + for (int a = 0; a < 10; a++){ + if(curr->friends[i]->friends[a] != NULL){ + length2 = length2 + 1; + } + } + for(int j = 0; j < length2; j++){ + if(strcmp(name, curr->friends[i]->friends[j]->name) == 0){ + for (int x = j; x < length2 - 1; x++){ + curr->friends[i]->friends[x] = curr->friends[i]->friends[x + 1]; + } + curr->friends[i]->friends[length2 - 1] = NULL; + } + } + } + + for(int i = 0; i < 10; i++){ + // if (curr->friends[i] == NULL){ + // free(curr->friends[i]); + // } + free(curr->friends[i]); + } + + // Post *curr3 = curr->first_post; + while(curr->first_post != NULL){ + free(curr->first_post->author); + free(curr->first_post->contents); + free(curr->first_post->date); + curr->first_post = curr->first_post->next; + } + curr->first_post->next = NULL; + + free(curr->name); + free(curr->profile_pic); + free(curr->friends); + free(curr->first_post); + free(curr); + return 0;",, +63c6480c5f004bf4898cf8fb,2023-02-15T08:39:51.023Z,Question from Code,v1,Why am I getting a segmentation fault,"int delete_user(const char *name, User **user_ptr_del) { + User *curr = *user_ptr_del; + User *prev = NULL; + + while((strcmp(name, curr->name) != 0) || (curr != NULL)){ + prev = curr; + curr = curr->next; + } + + if (curr == NULL){ + return 1; + } + + if (prev == NULL){ + curr->next = NULL; + } else{ + prev->next = curr->next; + curr->next = NULL; + } + + int length = 0; + for(int i = 0; i < 10; i++){ + if (curr->friends[i] != NULL){ + length = length + 1; + } + } + + for(int i = 0; i < length; i++){ + int length2 = 0; + for (int a = 0; a < 10; a++){ + if(curr->friends[i]->friends[a] != NULL){ + length2 = length2 + 1; + } + } + for(int j = 0; j < length2; j++){ + if(strcmp(name, curr->friends[i]->friends[j]->name) == 0){ + for (int x = j; x < length2 - 1; x++){ + curr->friends[i]->friends[x] = curr->friends[i]->friends[x + 1]; + } + curr->friends[i]->friends[length2 - 1] = NULL; + } + } + } + + for(int i = 0; i < 10; i++){ + if (curr->friends[i] != NULL){ + free(curr->friends[i]); + } + } + + // Post *curr3 = curr->first_post; + while(curr->first_post != NULL){ + free(curr->first_post->author); + free(curr->first_post->contents); + free(curr->first_post->date); + curr->first_post = curr->first_post->next; + } + curr->first_post->next = NULL; + + free(curr->name); + free(curr->profile_pic); + free(curr->friends); + free(curr->first_post); + free(curr); + return 0; +}",, +63c6480c5f004bf4898cf8fb,2023-02-15T09:09:51.577Z,Question from Code,v1,Why do I get an invalid pointer,"int delete_user(const char *name, User **user_ptr_del) { + User *curr = *user_ptr_del; + User *prev = NULL; + + while ((curr != NULL) && (strcmp(name, curr->name) != 0)) { + prev = curr; + curr = curr->next; + } + + if (curr == NULL){ + return 1; + } + + if (prev == NULL) { + *user_ptr_del = curr->next; + } else { + prev->next = curr->next; + curr->next = NULL; + } + + int length = 0; + for(int i = 0; (i < 10) && (curr->friends[i] != NULL); i++){ + length = length + 1; + } + + for(int i = 0; (i < length) && (curr->friends[i] != NULL); i++){ + int length2 = 0; + for (int a = 0; (a < 10) && (curr->friends[i]->friends[a] != NULL); a++){ + length2 = length2 + 1; + } + for(int j = 0; j < length2; j++){ + if(strcmp(name, curr->friends[i]->friends[j]->name) == 0){ + for (int x = j; x < length2 - 1; x++){ + curr->friends[i]->friends[x] = curr->friends[i]->friends[x + 1]; + } + curr->friends[i]->friends[length2 - 1] = NULL; + } + } + } + + printf(""good""); + + Post *curr3 = curr->first_post; + while (curr3 != NULL) { + free(curr3->author); + free(curr3->contents); + free(curr3->date); + Post *next_post = curr3->next; + free(curr3); + curr3 = next_post; + } + + printf(""good""); + + free(curr->name); + free(curr->profile_pic); + for (int i = 0; i < 10; i++) { + if (curr->friends[i] != NULL) { + free(curr->friends[i]); + } + } + free(curr); + return 0;",, +63c6480c5f004bf4898cf8fb,2023-02-15T09:10:43.205Z,Question from Code,v1,Am I demallocing correctly,"int delete_user(const char *name, User **user_ptr_del) { + User *curr = *user_ptr_del; + User *prev = NULL; + + while ((curr != NULL) && (strcmp(name, curr->name) != 0)) { + prev = curr; + curr = curr->next; + } + + if (curr == NULL){ + return 1; + } + + if (prev == NULL) { + *user_ptr_del = curr->next; + } else { + prev->next = curr->next; + curr->next = NULL; + } + + int length = 0; + for(int i = 0; (i < 10) && (curr->friends[i] != NULL); i++){ + length = length + 1; + } + + for(int i = 0; (i < length) && (curr->friends[i] != NULL); i++){ + int length2 = 0; + for (int a = 0; (a < 10) && (curr->friends[i]->friends[a] != NULL); a++){ + length2 = length2 + 1; + } + for(int j = 0; j < length2; j++){ + if(strcmp(name, curr->friends[i]->friends[j]->name) == 0){ + for (int x = j; x < length2 - 1; x++){ + curr->friends[i]->friends[x] = curr->friends[i]->friends[x + 1]; + } + curr->friends[i]->friends[length2 - 1] = NULL; + } + } + } + + printf(""good""); + + Post *curr3 = curr->first_post; + while (curr3 != NULL) { + free(curr3->author); + free(curr3->contents); + free(curr3->date); + Post *next_post = curr3->next; + free(curr3); + curr3 = next_post; + } + + printf(""good""); + + free(curr->name); + free(curr->profile_pic); + for (int i = 0; i < 10; i++) { + if (curr->friends[i] != NULL) { + free(curr->friends[i]); + } + } + free(curr); + return 0;",, +63c6480c5f004bf4898cf8fb,2023-03-03T17:31:29.137Z,Question from Code,v1,Why am I getting a warning when I try to wait for the child process to finish (line 24),"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + int pid = getpid(); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0){ + perror(""fork""); + exit(1); + } else if (n == 0){ + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + exit(0); + } else{ + wait(NULL); + } + } + + if (getpid() == pid){ + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), iterations); + } + + return 0; +}",, +63c6480c5f004bf4898cf8fb,2023-03-03T17:32:24.997Z,Question from Code,v1,what should I put in the parameter of wait so that the child process finishes first (line 24),"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + int pid = getpid(); + + for (int i = 0; i < iterations; i++) { + int n = fork(); + if (n < 0){ + perror(""fork""); + exit(1); + } else if (n == 0){ + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + exit(0); + } else{ + wait(NULL); + } + } + + if (getpid() == pid){ + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), iterations); + } + + return 0; +}",, +63c648105f004bf4898cf919,2023-03-15T15:47:31.463Z,Question from Code,v2,Why am I getting a segmentation fault?,"Rule *parse_file(FILE *fp) { + Rule *result; + Rule *prev_rule; + + char line[MAXLINE]; + fgets(&(line[0]), MAXLINE, fp); + + Rule *head_rule = malloc(sizeof(Rule)); + result = head_rule; + prev_rule = head_rule; + + char *token = strtok(line, "":""); + head_rule->target = malloc(strlen(token)); + strcpy(head_rule->target, token); + return result; +}",, +63c648105f004bf4898cf919,2023-03-15T15:56:54.199Z,Question from Code,v2,Why does this produce a segmentation fault?,"Rule *parse_file(FILE *fp) { + Rule *result; + Rule *prev_rule; + + char line[MAXLINE]; + fgets(&(line[0]), MAXLINE, fp); + + Rule *head_rule = malloc(sizeof(Rule)); + result = head_rule; + prev_rule = head_rule; + + char *token = strtok(&(line[0]), "":""); + head_rule->target = malloc(strlen(token)); + strcpy(head_rule->target, token); + return result; +}",, +63c648105f004bf4898cf919,2023-03-15T15:59:34.455Z,Question from Code,v2,Why is there a segmentation fault?,"Rule *parse_file(FILE *fp) { + Rule *result; + Rule *prev_rule; + + char line[MAXLINE]; + fgets(&(line[0]), MAXLINE, fp); + + Rule *head_rule = malloc(sizeof(Rule)); + result = head_rule; + prev_rule = head_rule; + + char *token = strtok(&(line[0]), "":""); + if (head_rule != NULL) { + head_rule->target = malloc(strlen(token)); + strcpy(head_rule->target, token); + } + return result; +}",, +63c648105f004bf4898cf919,2023-03-15T16:03:07.329Z,Question from Code,v2,Why am I still getting a segmentation fault?,"Rule *parse_file(FILE *fp) { + Rule *result; + Rule *prev_rule; + + char line[MAXLINE]; + fgets(&(line[0]), MAXLINE, fp); + + Rule *head_rule = malloc(sizeof(Rule)); + result = head_rule; + prev_rule = head_rule; + + char *token = strtok(&(line[0]), "":""); + if (head_rule != NULL && head_rule->target != NULL) { + head_rule->target = malloc(strlen(token)); + strcpy(head_rule->target, token); + } + return result; +}",, +63c648105f004bf4898cf919,2023-03-15T16:37:58.823Z,Question from Code,v2,What is causing the seg fault?,"Rule *parse_file(FILE *fp) { + Rule *result; + Rule *prev_rule; + + char line[MAXLINE]; + fgets(&(line[0]), MAXLINE, fp); + + Rule *head_rule = malloc(sizeof(Rule)); + result = head_rule; + prev_rule = head_rule; + + char *token = strtok(&(line[0]), "":""); + if (head_rule != NULL && head_rule->target != NULL) { + head_rule->target = malloc(strlen(token)); + strcpy(head_rule->target, token); + } + return result; +}",, +63c648105f004bf4898cf919,2023-03-15T16:38:55.058Z,Question from Code,v2,Why does strtok produce a seg fault?,"Rule *parse_file(FILE *fp) { + Rule *result; + Rule *prev_rule; + + char line[MAXLINE]; + fgets(&(line[0]), MAXLINE, fp); + + Rule *head_rule = malloc(sizeof(Rule)); + result = head_rule; + prev_rule = head_rule; + + char *token = strtok(&(line[0]), "":""); + if (head_rule != NULL && head_rule->target != NULL) { + head_rule->target = malloc(strlen(token)); + strcpy(head_rule->target, token); + } + return result; +}",, +63c648105f004bf4898cf919,2023-03-15T16:42:03.821Z,Question from Code,v2,Why does line 12 give a seg fault?,"Rule *parse_file(FILE *fp) { + Rule *result; + Rule *prev_rule; + + char line[MAXLINE]; + fgets(&(line[0]), MAXLINE, fp); + + Rule *head_rule = malloc(sizeof(Rule)); + result = head_rule; + prev_rule = head_rule; + + char *token = strtok(line, "":""); + if (head_rule != NULL && head_rule->target != NULL) { + head_rule->target = malloc(strlen(token)); + strcpy(head_rule->target, token); + } + return result; +}",, +63c648105f004bf4898cf919,2023-03-15T16:42:58.783Z,Question from Code,v2,Why does strtok give a seg fault?,"Rule *parse_file(FILE *fp) { + Rule *result; + Rule *prev_rule; + + char line[MAXLINE]; + fgets(&(line[0]), MAXLINE, fp); + + Rule *head_rule = malloc(sizeof(Rule)); + result = head_rule; + prev_rule = head_rule; + + char *token = strtok(line, "":""); + if (head_rule != NULL && head_rule->target != NULL) { + head_rule->target = malloc(strlen(token)); + strcpy(head_rule->target, token); + } + return result; +}",, +63c648105f004bf4898cf919,2023-03-15T16:47:18.826Z,Question from Code,v2,Why does line 24 produce a seg fault?,"Rule *parse_file(FILE *fp) { + Rule *result; + Rule *prev_rule; + + char line[MAXLINE]; + fgets(&(line[0]), MAXLINE, fp); + + Rule *head_rule = malloc(sizeof(Rule)); + result = head_rule; + prev_rule = head_rule; + + char *token = strtok(line, "":""); + if (head_rule != NULL && head_rule->target != NULL) { + head_rule->target = malloc(strlen(token)); + strcpy(head_rule->target, token); + } + fgets(line, MAXLINE, fp); + while (&(line[0]) != NULL) { + if (is_comment_or_empty(line) == 0 && line[0] != '\t') { + token = strtok(&(line[0]), "":""); + + Rule *new_rule = add_rule(prev_rule); + head_rule->target = malloc(sizeof(token)); + strcpy(new_rule->target, token); + + prev_rule->next_rule = new_rule; + prev_rule = new_rule; + } + fgets(line, MAXLINE, fp); + token = strtok(&(line[0]), "" ""); + } + return result; +}",, +63c648105f004bf4898cf919,2023-03-15T16:51:04.994Z,Question from Code,v2,Why does strcpy in line 24 cause a seg fault?,"Rule *parse_file(FILE *fp) { + Rule *result; + Rule *prev_rule; + + char line[MAXLINE]; + fgets(&(line[0]), MAXLINE, fp); + + Rule *head_rule = malloc(sizeof(Rule)); + result = head_rule; + prev_rule = head_rule; + + char *token = strtok(line, "":""); + if (head_rule != NULL && head_rule->target != NULL) { + head_rule->target = malloc(strlen(token)); + strcpy(head_rule->target, token); + } + fgets(line, MAXLINE, fp); + while (&(line[0]) != NULL) { + if (is_comment_or_empty(line) == 0 && line[0] != '\t') { + token = strtok(&(line[0]), "":""); + + Rule *new_rule = add_rule(prev_rule); + head_rule->target = malloc(sizeof(token)); + strcpy(new_rule->target, token); + + prev_rule->next_rule = new_rule; + prev_rule = new_rule; + } + fgets(line, MAXLINE, fp); + token = strtok(&(line[0]), "" ""); + } + return result; +}",, +63c648105f004bf4898cf919,2023-03-15T17:00:33.628Z,Question from Code,v2,Why does line 24 give a seg fault?,"Rule *parse_file(FILE *fp) { + Rule *result; + Rule *prev_rule; + + char line[MAXLINE]; + fgets(&(line[0]), MAXLINE, fp); + + Rule *head_rule = malloc(sizeof(Rule)); + result = head_rule; + prev_rule = head_rule; + + char *token = strtok(line, "":""); + if (head_rule != NULL && head_rule->target != NULL) { + head_rule->target = malloc(strlen(token)); + strcpy(head_rule->target, token); + } + fgets(line, MAXLINE, fp); + while (&(line[0]) != NULL) { + if (is_comment_or_empty(line) == 0 && line[0] != '\t') { + token = strtok(&(line[0]), "":""); + + Rule *new_rule = add_rule(prev_rule); + head_rule->target = malloc(strlen(token) + 1); + strcpy(new_rule->target, token); + + prev_rule->next_rule = new_rule; + prev_rule = new_rule; + } + fgets(line, MAXLINE, fp); + token = strtok(&(line[0]), "" ""); + } + return result; +}",, +63c648105f004bf4898cf919,2023-03-15T17:01:44.910Z,Question from Code,v2,Why does line 24 give a seg fault?,"Rule *parse_file(FILE *fp) { + Rule *result; + Rule *prev_rule; + + char line[MAXLINE]; + fgets(&(line[0]), MAXLINE, fp); + + Rule *head_rule = malloc(sizeof(Rule)); + result = head_rule; + prev_rule = head_rule; + + char *token = strtok(line, "":""); + if (head_rule != NULL && head_rule->target != NULL) { + head_rule->target = malloc(strlen(token)); + strcpy(head_rule->target, token); + } + fgets(line, MAXLINE, fp); + while (&(line[0]) != NULL) { + if (is_comment_or_empty(line) == 0 && line[0] != '\t') { + token = strtok(&(line[0]), "":""); + + Rule *new_rule = add_rule(prev_rule); + if (head_rule != NULL) { + head_rule->target = malloc(strlen(token) + 1); + strcpy(new_rule->target, token); + } + + prev_rule->next_rule = new_rule; + prev_rule = new_rule; + } + fgets(line, MAXLINE, fp); + token = strtok(&(line[0]), "" ""); + } + return result; +}",, +63c648115f004bf4898cf928,2023-02-08T19:38:24.412Z,General Question,v1,how to initialize the pointer allocated to point to heap-allocated space for an entire row of pixel data.,,, +63c648115f004bf4898cf928,2023-02-08T19:39:16.902Z,Question from Code,v1,how to initialize the pointer allocated to point to heap-allocated space for an entire row of pixel data.,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **array = malloc (sizeof(struct pixel *) * height); + + +}",, +63c648115f004bf4898cf928,2023-02-08T19:43:21.201Z,Help Fix Code,v1,,"void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { + + + fseek(image, 10, SEEK_SET); + error = fread(pixel_array_offset,4,1,image); + fseek(image, 18, SEEK_SET); + error = fread(width,4,1,image); + fseek(image, 22, SEEK_SET); + error = fread(height,4,1,image); +}","byte 10 - 13 should be the value for pixel_array_offset, byte 18 - 21 should be the value for width and byte 22-25 should be the height.", +63c648115f004bf4898cf928,2023-02-08T19:45:07.186Z,Help Fix Code,v1,,"void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { + + + fseek(image, 10, SEEK_SET); + error = fread(pixel_array_offset,4,1,image); + fseek(image, 18, SEEK_SET); + error = fread(width,4,1,image); + fseek(image, 22, SEEK_SET); + error = fread(height,4,1,image); +}","pixel_array_offset should assign the value in byte 10 - 13 of the file, byte 18 - 21 should be the value for width should be assigned the value of byte 18 - 21 of the file, height should be assign the value of byte 22-25 of the file", +63c648125f004bf4898cf932,2023-01-28T15:56:39.718Z,Help Fix Code,v1,,"char cmd_permission[10]; +cmd_permission = argv[2];",assign arg[v] to cmd_permission, +63c648125f004bf4898cf932,2023-01-28T15:57:40.282Z,General Question,v1,how to assign string to char array,,, +63c648125f004bf4898cf932,2023-01-28T16:04:18.432Z,General Question,v1,assign string to char array without strcpy(),,, +63c648125f004bf4898cf932,2023-02-10T00:09:14.977Z,Help Fix Code,v1,,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel *pixel_arr[height]; +}",initialize an array with height elements, +63c648125f004bf4898cf932,2023-02-12T20:03:17.699Z,General Question,v1,how to use gdb with multiple files,,, +63c648125f004bf4898cf932,2023-02-12T20:03:59.650Z,General Question,v1,how to use gdb,,, +63c648125f004bf4898cf932,2023-02-12T20:05:55.861Z,General Question,v1,how to run next line in gdb,,, +63c648125f004bf4898cf932,2023-02-12T22:27:33.276Z,General Question,v1,what does fopen() return if file does not exist?,,, +63c648125f004bf4898cf932,2023-02-12T22:34:00.817Z,General Question,v1,how to go back in gdp?,,, +63c648125f004bf4898cf932,2023-02-13T00:40:02.155Z,General Question,v1,can i pass a User * type into a const User * type?,,, +63c648125f004bf4898cf932,2023-02-13T02:20:24.893Z,General Question,v1,can you negative index an array?,,, +63c648125f004bf4898cf932,2023-02-13T02:26:22.241Z,General Question,v1,can you initialise an array of 0 elements?,,, +63c648125f004bf4898cf932,2023-02-13T02:32:39.532Z,General Question,v1,how can i set all elements of an array to NULL?,,, +63c648125f004bf4898cf932,2023-02-13T15:45:43.838Z,General Question,v1,how to run until breakpoint in gdb?,,, +63c648125f004bf4898cf932,2023-02-13T16:58:34.436Z,General Question,v1,can i set a string to NULL?,,, +63c648125f004bf4898cf932,2023-02-13T17:20:41.606Z,General Question,v1,ho to print contents of a text file with fopen?,,, +63c648125f004bf4898cf932,2023-02-13T17:32:46.109Z,General Question,v1,do you have to fclose on a NULL stream?,,, +63c648125f004bf4898cf932,2023-02-13T17:38:02.878Z,General Question,v1,fgets return value?,,, +63c648125f004bf4898cf932,2023-02-13T18:23:14.417Z,General Question,v1,how to define char array in function parameter?,,, +63c648125f004bf4898cf932,2023-02-13T18:48:19.484Z,General Question,v1,how to use ctime?,,, +63c648125f004bf4898cf932,2023-02-15T14:13:03.156Z,Help Fix Code,v1,,*curr_time = time(NULL);,assign the pointer value to time(NULL), +63c648125f004bf4898cf932,2023-02-15T14:14:24.613Z,Help Fix Code,v1,,"time_t *curr_time = malloc(sizeof(time_t)); +curr_time = time(NULL);",assign the pointer curr_time to the address of time(NULL), +63c648125f004bf4898cf932,2023-03-01T23:16:49.762Z,General Question,v1,does fgets() read empty lines?,,, +63c648125f004bf4898cf932,2023-03-10T22:09:55.786Z,General Question,v2,what is the purpose of NULL passed to execl()?,,, +63c648125f004bf4898cf932,2023-03-10T22:37:05.485Z,General Question,v2,how to write multiple values to a pipe?,,, +63c648125f004bf4898cf932,2023-03-11T18:43:14.770Z,General Question,v2,how can i use strtok() to split on spaces and tabs?,,, +63c648125f004bf4898cf932,2023-03-11T18:49:18.814Z,General Question,v2,how to use strtok() to find comments?,,, +63c648125f004bf4898cf932,2023-03-12T01:13:54.786Z,General Question,v2,can i index a char *pointer?,,, +63c648125f004bf4898cf932,2023-03-12T01:33:20.122Z,General Question,v2,does fgets() read \n character for blank lines?,,, +63c648125f004bf4898cf932,2023-03-12T01:52:16.766Z,General Question,v2,how can i tell if a string starts with a \t tab character?,,, +63c648125f004bf4898cf932,2023-03-12T14:09:32.905Z,General Question,v2,can you compare characters with == ?,,, +63c648125f004bf4898cf932,2023-03-12T19:53:27.868Z,General Question,v2,can i set a struct attribute to a read-only string in a helper function?,,, +63c648125f004bf4898cf932,2023-03-13T01:42:18.355Z,General Question,v2,when should i malloc a string vs just assign it in a helper function?,,, +63c648125f004bf4898cf932,2023-03-13T18:26:20.727Z,General Question,v2,"when passing a struct to a function to be modified, should i pass a pointer to struct or a pointer to pointer to struct?",,, +63c648125f004bf4898cf932,2023-03-13T18:40:09.445Z,General Question,v2,how to call strtok repeatedly on the same line?,,, +63c648125f004bf4898cf932,2023-03-14T02:22:21.090Z,General Question,v2,is there a function to count the number of words in a string?,,, +63c648125f004bf4898cf932,2023-03-14T02:42:42.654Z,General Question,v2,can you initailize an array of size 0?,,, +63c648125f004bf4898cf932,2023-03-14T03:28:15.439Z,General Question,v2,what is returned if strtok() is called on a whitespace line?,,, +63c648125f004bf4898cf932,2023-03-14T03:43:32.687Z,General Question,v2,how to strip a string of specific characters?,,, +63c648125f004bf4898cf932,2023-03-14T03:47:37.625Z,General Question,v2,"does strtok remove tabs when passed "" ""?",,, +63c648125f004bf4898cf932,2023-03-14T03:49:21.068Z,General Question,v2,what's the \r character?,,, +63c648125f004bf4898cf932,2023-03-14T05:06:17.661Z,General Question,v2,what happens if you call strtok() with NULL?,,, +63c648125f004bf4898cf932,2023-03-14T14:42:21.844Z,General Question,v2,how to strip \n character with gets?,,, +63c648125f004bf4898cf932,2023-03-14T14:43:04.270Z,General Question,v2,how to strip \n character with fgets?,,, +63c648125f004bf4898cf932,2023-03-14T15:14:31.866Z,General Question,v2,when does strtok end?,,, +63c648125f004bf4898cf932,2023-03-14T18:38:27.601Z,General Question,v2,does strncpy copy the null terminator?,,, +63c648125f004bf4898cf932,2023-03-14T18:42:13.607Z,General Question,v2,how to edit value of a dynamically allocated string?,,, +63c648125f004bf4898cf932,2023-03-14T23:30:07.098Z,General Question,v2,how to get the size of an array?,,, +63c648125f004bf4898cf932,2023-03-14T23:46:57.591Z,General Question,v2,what type does malloc(sizeof(char *)) return?,,, +63c648125f004bf4898cf932,2023-03-15T00:32:10.411Z,General Question,v2,can i check if a variables been declaared or not?,,, +63c648125f004bf4898cf932,2023-03-15T04:06:28.172Z,General Question,v2,how to use strchr to replace a character with another?,,, +63c648125f004bf4898cf932,2023-03-15T04:08:42.976Z,Help Fix Code,v2,replace \r character with \0,"if ((newline_index = strchr(curr_line, '\r')) != NULL) { + *newline_index = '\0'; +}",, +63c648125f004bf4898cf932,2023-03-15T04:31:41.976Z,General Question,v2,how to clear a character array?,,, +63c648125f004bf4898cf932,2023-03-15T13:21:26.656Z,General Question,v2,can you set a character array to NULL?,,, +63c648125f004bf4898cf932,2023-03-17T16:12:21.803Z,General Question,v2,how to write ineteger to a binary file?,,, +63c648125f004bf4898cf932,2023-03-28T03:15:51.557Z,General Question,v2,can i use % string formating in a write() call?,,, +63c648125f004bf4898cf932,2023-03-28T22:15:12.275Z,General Question,v2,what is the first argument passed to select()?,,, +63c648125f004bf4898cf932,2023-04-06T13:46:40.893Z,General Question,v2,how to check if a pointer points to NULL?,,, +63c648125f004bf4898cf932,2023-04-06T13:51:53.191Z,General Question,v2,how to malloc an int for a struct,,, +63c648125f004bf4898cf932,2023-04-06T14:01:16.077Z,Question from Code,v2,will this code safely allocate an int on the heap?,"int *ptr = malloc(sizeof(int)); +int num = 42; +*ptr = num;",, +63c648125f004bf4898cf932,2023-04-06T15:42:02.506Z,General Question,v2,how to use write to write to a buffer string?,,, +63c648125f004bf4898cf932,2023-04-06T17:47:35.856Z,General Question,v2,when do you have to close a fd?,,, +63c648125f004bf4898cf932,2023-04-06T18:17:54.004Z,Question from Code,v2,does this properly set buf to an empty string?,"char buf[5]; +buf = {'/0'};",, +63c648135f004bf4898cf937,2023-01-31T03:50:51.950Z,Question from Code,v1,why is there a compilation error,"void update_contact(struct *contact friend, char *new_name, int new_number, char *new_address) { + strcpy(friend.name, new_name); + friend.phone_number = new_number; + strcpy(friend.address, new_address); +}",, +63c648135f004bf4898cf937,2023-01-31T03:54:12.879Z,Help Fix Code,v1,,"void update_contact(struct *contact friend, char *new_name, int new_number, char *new_address) { + strcpy(friend.name, new_name); + friend.phone_number = new_number; + strcpy(friend.address, new_address); +}","update struct contact +", +63c648135f004bf4898cf937,2023-01-31T04:37:29.685Z,Question from Code,v1,why is there a compilation error,"void update_contact(struct *contact friend, int new_name, int new_number, char *new_address) { + strcpy(friend->name, new_name); + friend->phone_number = new_number; + strcpy(friend->address, new_address); +}",, +63c648135f004bf4898cf937,2023-01-31T04:41:18.233Z,Question from Code,v1,compilation erroR?,"void update_contact(struct *contact friend, char* new_name, int new_number, char *new_address) { + strcpy(friend->name, new_name); + friend->phone_number = new_number; + strcpy(friend->address, new_address); +}",, +63c648135f004bf4898cf937,2023-04-21T20:30:16.524Z,Question from Code,v2,"Why is it that if I sigint at position A, the process is terminated? I didn't call quit in the handler function","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; +}",, +63c648135f004bf4898cf937,2023-04-21T20:40:56.378Z,General Question,v2,"A signal handling function that you write must always have the same signature.Why is this true? +",,, +63c648165f004bf4898cf950,2023-02-12T21:37:00.055Z,Help Fix Code,v1,,"#include ""friends.h"" +#include +#include +#include + +const int MAX_PFP = 40; +/* + * 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) { + User *new = malloc(sizeof(User)); + User *curr = *user_ptr_add; + User *prev = NULL; + if (new < 0){ + return -1; + } + + if(strlen(name) >= MAX_NAME){ + return 2; + } + + printf(""%s\n"", name); + while(curr != NULL){ + if(strcmp(curr->name, name) == 0){ + return 1; + } + prev = curr; + curr = curr->next; + } + + strncpy(new->name, name, MAX_NAME); + printf(""%s\n"", new->name); + prev->next = new; + return 0; +}"," 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.", +63c648165f004bf4898cf950,2023-02-13T19:42:33.882Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + User* curr = *user_ptr_del; + User* prev = NULL; + int found = 0; + // iterate through the list + while (curr != NULL){ + // check if the curr is pointing to the user to be deleted + if(strcmp(curr->name, name) == 0){ + // check if the user to be deleted is the first user + if (curr == *user_ptr_del){ + *user_ptr_del = curr->next; + } + else{ + prev->next = curr->next; + } + found = 1; + free(curr); + break; + } + // check if curr is friends with the user to be deleted + else{ + for (int i = 0; i < MAX_FRIENDS; i++){ + if (curr->friends[i] == NULL){ + break; + } + if (strcmp(curr->friends[i]->name, name) == 0){ + curr->friends[i] = NULL; + } + } + curr = curr->next; + } + + } + if (found == 0){ + return 1; + } + 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 + */", +63c648165f004bf4898cf950,2023-02-13T20:10:47.507Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + // check if users exist + if (author == NULL || target == NULL){ + return 2; + } + + // check if they are friends + int isFriend = 0; + for (int i = 0; i < MAX_FRIENDS; i++){ + if (author->friends[i] == target){ + isFriend = 1; + break; + } + } + + if (isFriend == 0){ + return 1; + } + + // create new post + Post* newPost = malloc(sizeof(Post)); + newPost->contents = contents; + strncpy(newPost->author, author->name, MAX_NAME); + time_t now; + time(&now); + newPost->date = &now; + + // insert the new post to the front of the list for the target user + newPost->next = target->first_post; + target->first_post = newPost; + return 0; + +} +",Set the time of the post to the current time using time(), +63c648165f004bf4898cf950,2023-02-13T20:13:01.385Z,Help Fix Code,v1,,"int make_post(const User *author, User *target, char *contents) { + // check if users exist + if (author == NULL || target == NULL){ + return 2; + } + + // check if they are friends + int isFriend = 0; + for (int i = 0; i < MAX_FRIENDS; i++){ + if (author->friends[i] == target){ + isFriend = 1; + break; + } + } + + if (isFriend == 0){ + return 1; + } + + // create new post + Post* newPost = malloc(sizeof(Post)); + newPost->contents = contents; + strncpy(newPost->author, author->name, MAX_NAME); + time_t now; + time(&now); + newPost->date = &now; + + // insert the new post to the front of the list for the target user + newPost->next = target->first_post; + target->first_post = newPost; + 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 + */", +63c648165f004bf4898cf950,2023-02-13T20:24:40.738Z,Help Fix Code,v1,,"int print_user(const User *user) { + if (user == NULL){ + return 1; + } + + printf(""------------------------------------------\n""); + printf(""Posts:\n""); + + // print posts + Post* currPost = user->first_post; + while (currPost != NULL){ + printf(""From: %s\n"", currPost->author); + printf(""Date: %s\n"", ctime(currPost->date)); + printf(""%s\n"", currPost->contents); + currPost = currPost->next; + return 0; +} +",Print user profile and any associated posts. However the time is not printed properly., +63c648165f004bf4898cf950,2023-02-13T20:28:54.100Z,Question from Code,v1,the time is not printed properly from line 71,"int make_post(const User *author, User *target, char *contents) { + // check if users exist + if (author == NULL || target == NULL){ + return 2; + } + + // check if they are friends + int isFriend = 0; + for (int i = 0; i < MAX_FRIENDS; i++){ + if (author->friends[i] == target){ + isFriend = 1; + break; + } + } + + if (isFriend == 0){ + return 1; + } + + // create new post + Post* newPost = malloc(sizeof(Post)); + newPost->contents = contents; + strncpy(newPost->author, author->name, MAX_NAME); + + // allocate and set time + newPost->date = (time_t*)malloc(sizeof(time_t)); + time_t now = time(NULL); + time(&now); + + newPost->date = &now; + //printf(""%s\n"", ctime(newPost->date)); + // insert the new post to the front of the list for the target user + newPost->next = target->first_post; + target->first_post = newPost; + return 0; + +} + +int print_user(const User *user) { + if (user == NULL){ + return 1; + } + + // print profile pic + char currChar; + FILE *fp = fopen(user->profile_pic, ""r""); + if (fp != NULL){ + while((fscanf(fp, ""%c"", &currChar)) != EOF){ + printf(""%c"", currChar); + } + printf(""\n""); + fclose(fp); + } + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + //print friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++){ + if (user->friends[i] != NULL){ + printf(""%s\n"", user->friends[i]->name); + } + } + printf(""------------------------------------------\n""); + printf(""Posts:\n""); + + // print posts + Post* currPost = user->first_post; + while (currPost != NULL){ + printf(""From: %s\n"", currPost->author); + printf(""Date: %s\n"", ctime(currPost->date)); + printf(""%s\n"", currPost->contents); + currPost = currPost->next; + + // padding prints + if (currPost != NULL){ + printf(""\n""); + printf(""===\n""); + printf(""\n""); + } + } + printf(""------------------------------------------\n""); + + return 0; +} +",, +63c648165f004bf4898cf950,2023-02-14T22:11:05.039Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + User* curr1 = find_user(name1, head); + User* curr2 = find_user(name2, head); + + // check if user exists + if (curr1 == NULL || curr2 == NULL){ + return 4; + } + + // check if the same user is passed in twice + if (curr1 == curr2){ + return 3; + } + + + // check for number of friends for each user + int count1 = 0; + for(int i = 0; i < MAX_FRIENDS; i++){ + if (curr1->friends[i] != NULL){ + count1 += 1; + + // check if the two users are already friends + if (curr1->friends[i] == curr2) + { + return 1; + } + } + } + int count2 = 0; + for(int i = 0; ifriends[i] != NULL){ + count2 += 1; + + // check if the two users are already friends + if (curr2->friends[i] == curr1) + { + return 1; + } + } + } + + if (count1 >= MAX_FRIENDS || count2 >= MAX_FRIENDS){ + return 2; + } + + + + int i1 = 0; + int i2 = 0; + + while (i1 < MAX_FRIENDS || i2 < MAX_FRIENDS){ + if (curr1->friends[i1] != NULL){ + i1++; + } + else{ + curr1->friends[i1] = curr2; + } + + if (curr2->friends[i2] != NULL){ + i2++; + } + else{ + curr2->friends[i2] = curr1; + } + + if (curr1->friends[i1] != NULL && curr2->friends[i2] != NULL){ + break; + } + } + return 0; +}","/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */", +63c648165f004bf4898cf950,2023-03-06T03:53:30.260Z,Help Fix Code,v1,,"Rule *parse_file(FILE *fp) { + char buffer[MAXLINE+1]; + Rule *curr_rule = NULL; + RuleLL *head = malloc(sizeof(RuleLL)); + + + + // Read each line until the EOF + while(fgets(buffer, MAXLINE, fp) != NULL) { + if (!is_comment_or_empty(buffer)) { + // Target line + if(buffer[0] != '\t') { + char delim[] = "" ""; + + // Find if the rule already exists in head + char* token = strtok(buffer, delim); + curr_rule = find_rule(head, token); + + + // Else if the current Target is not in the head_rule, create a new rule + if (curr_rule == NULL){ + curr_rule = malloc(sizeof(Rule)); + curr_rule->actions = NULL; + curr_rule->target = malloc(strlen(token) + 1); + strcpy(curr_rule->target, token); + insert_rule(head, curr_rule); + } + + // Skip the colon + token = strtok(NULL, delim); + token = strtok(NULL, delim); + + // Add dependencies to the current rule + while(token != NULL) { + Dependency *new_dep = malloc(sizeof(Dependency)); + // Find the rule that matches the dependency in head_rule + Rule *dep_rule = find_rule(head, token); + + // If the dependency rule is found + if (dep_rule != NULL) { + new_dep->rule = dep_rule; + } + // Else, then create a new rule for the dependency + else{ + new_dep->rule = malloc(sizeof(Rule)); + // if it has trailing \n character, don't include it + /* if (token[strlen(token)-1] == '\n') { + token[strlen(token)-1] = '\0'; + } */ + if (token[strlen(token)-1] == '\n') { + new_dep->rule->target = malloc(strlen(token)); + strcpy(new_dep->rule->target, token - 1); + new_dep->rule->target[strlen(token)-1] = '\0'; + } + else{ + new_dep->rule->target = malloc(strlen(token) + 1); + strcpy(new_dep->rule->target, token); + } + insert_rule(head, new_dep->rule); + } + + // Add the dependency to the current rule + insert_dependency(curr_rule, new_dep); + token = strtok(NULL, delim); + } + + } + // Action line + else { + //printf(""%s"", buffer); + int num_tokens = 0; + char delim[] = "" ""; + char* token = strtok(buffer, delim); + while(token != NULL) { + num_tokens++; + token = strtok(NULL, delim); + } + + // Make new token + char *new_token = strtok(buffer, delim); + Action *new_act = malloc(sizeof(Action)); + new_act->args = malloc(sizeof(char*)*(num_tokens+1)); + int i = 0; + while(new_token != NULL) { + if(new_token[0] == '\t'){ + new_token = new_token + 1; + } + new_act->args[i] = malloc(strlen(new_token) + 1); + strcpy(new_act->args[i], new_token); + printf(""%s\n"", new_token); + i++; + new_token = strtok(NULL, delim); + } + new_act->args[i] = NULL; + + // Add the action to the current rule + insert_action(curr_rule, new_act); + } + } + } + if (head) { + return head->rule; + } + return NULL; +} + +","When inserting new dependency, if it has trailing '\n' it shouldn't include it as shown in line 50. However, it fails for some cases to filter out '\n' character.", +63c648165f004bf4898cf950,2023-03-06T04:07:18.831Z,Help Fix Code,v1,,"int num_tokens = 0; +char delim[] = "" ""; +char* token = strtok(buffer, delim); +while(token != NULL) { + num_tokens++; + token = strtok(NULL, delim); +} + +// Make new token +char *new_token = strtok(buffer, delim); +Action *new_act = malloc(sizeof(Action)); +new_act->args = malloc(sizeof(char*)*(num_tokens+1)); +int i = 0; +while(new_token != NULL) { + if(new_token[0] == '\t'){ + new_token = new_token + 1; + } + new_act->args[i] = malloc(strlen(new_token) + 1); + strcpy(new_act->args[i], new_token); + //printf(""%s\n"", new_token); + i++; + new_token = strtok(NULL, delim); +} +new_act->args[i] = NULL; + +// Add the action to the current rule +insert_action(curr_rule, new_act);",each string token is not being iterated properly, +63c648165f004bf4898cf950,2023-03-08T04:11:10.378Z,Help Fix Code,v1,,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + + int fd[2]; + if(pipe(fd) == -1) { + perror(""pipe""); + exit(1); + } + int read_end = fd[0]; + int write_end = fd[1]; + pid_t pid = fork(); + + //child process + if (pid == 0){ + wait(0); + close(write_end); + dup2(read_end, STDIN_FILENO); + close(read_end); + execl(""./validate"", ""validate"", NULL); + } + // parent process + else if (pid > 0){ + close(read_end); + write(write_end, user_id, strlen(user_id)); + write(write_end, password, strlen(password)); + close(write_end); + int status; + wait(&status); + + if (WIFEXITED(status)){ + int exit_status = WEXITSTATUS(status); + if (exit_status == 0){ + printf(""%s\n"", SUCCESS); + } + else if (exit_status == 1){ + printf(""%s\n"", INVALID); + } + else if (exit_status == 2){ + printf(""%s\n"", NO_USER); + } + } + else{ + perror(""wait""); + exit(1); + } + + } + else{ + perror(""fork""); + exit(1); + } + + + + return 0; +} +","in line 50, when validate needs to read from stdin, but it can't read from it. ", +63c648165f004bf4898cf950,2023-03-08T04:12:22.730Z,Help Fix Code,v1,,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + + int fd[2]; + if(pipe(fd) == -1) { + perror(""pipe""); + exit(1); + } + int read_end = fd[0]; + int write_end = fd[1]; + pid_t pid = fork(); + + //child process + if (pid == 0){ + wait(0); + close(write_end); + dup2(read_end, STDIN_FILENO); + close(read_end); + execl(""./validate"", ""validate"", NULL); + } + // parent process + else if (pid > 0){ + close(read_end); + write(write_end, user_id, strlen(user_id)); + write(write_end, password, strlen(password)); + close(write_end); + int status; + wait(&status); + + if (WIFEXITED(status)){ + int exit_status = WEXITSTATUS(status); + if (exit_status == 0){ + printf(""%s\n"", SUCCESS); + } + else if (exit_status == 1){ + printf(""%s\n"", INVALID); + } + else if (exit_status == 2){ + printf(""%s\n"", NO_USER); + } + } + else{ + perror(""wait""); + exit(1); + } + + } + else{ + perror(""fork""); + exit(1); + } + + + + return 0; +} +","in line 50, the program run from execl cannot read from stdin.", +63c648165f004bf4898cf950,2023-03-08T04:13:14.704Z,Help Fix Code,v1,,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + + int fd[2]; + if(pipe(fd) == -1) { + perror(""pipe""); + exit(1); + } + int read_end = fd[0]; + int write_end = fd[1]; + pid_t pid = fork(); + + //child process + if (pid == 0){ + close(write_end); + dup2(read_end, STDIN_FILENO); + close(read_end); + execl(""./validate"", ""validate"", NULL); + } + // parent process + else if (pid > 0){ + close(read_end); + write(write_end, user_id, strlen(user_id)); + write(write_end, password, strlen(password)); + close(write_end); + int status; + wait(&status); + + if (WIFEXITED(status)){ + int exit_status = WEXITSTATUS(status); + if (exit_status == 0){ + printf(""%s\n"", SUCCESS); + } + else if (exit_status == 1){ + printf(""%s\n"", INVALID); + } + else if (exit_status == 2){ + printf(""%s\n"", NO_USER); + } + } + else{ + perror(""wait""); + exit(1); + } + + } + else{ + perror(""fork""); + exit(1); + } + + + + return 0; +} +",the program run from excl in line 49 cannot read from stdin., +63c648165f004bf4898cf950,2023-03-11T01:06:37.746Z,Help Fix Code,v2,"The program works, but curr_act prints twice in line 65-70. ","void evaluate_part2(Rule *head, Rule *rule){ + // 1. Update each of the dependencies. In other words, recursively evaluate each dependency rule. + Dependency *curr_dep = rule->dependencies; + while (curr_dep != NULL) { + evaluate_part2(head, curr_dep->rule); + curr_dep = curr_dep->next_dep; + } + + // 2. Compare the last modified time for each of the dependencies to the target. + + // If the target does not have any dependencies, execute the rule's actions + + while (curr_dep2 != NULL) { + struct stat dep_stat; + int dep_exist = stat(curr_dep2->rule->target, &dep_stat); + // 3. If the target does not exist, or at least one of the dependencies refers to a file with a + // last modified time more recent than the target, execute the rule's actions. + struct timespec dep_time = dep_stat.st_mtim; + + if (target_exist != 0 || dep_exist != 0 || dep_time.tv_sec > target_time.tv_sec || \ + (dep_time.tv_sec == target_time.tv_sec && dep_time.tv_nsec > target_time.tv_nsec)) { + + Action *curr_act = rule->actions; + while (curr_act != NULL) { + int visited = 0; + // Execute the rule's actions + pid_t pid = fork(); + if (pid == 0) { + + // Print the current action line + int i = 0; + if (visited == 0) { + while (curr_act->args[i] != NULL) { + printf(""%s "", curr_act->args[i]); + i++; + } + printf(""\n""); + visited = 1; + } + + execvp(curr_act->args[0], curr_act->args); + fprintf(stderr, ""Error: execvp failed\n""); + exit(1); + } + } else { + int status; + waitpid(pid, &status, 0); + if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { + fprintf(stderr, ""Error: child process failed\n""); + exit(1); + } + } + curr_act = curr_act->next_act; + } + } + curr_dep2 = curr_dep2->next_dep; + } + +}",, +63c648165f004bf4898cf950,2023-03-11T01:31:18.548Z,Help Fix Code,v2,it segments error at line 13,"typedef struct rl{ + Rule *rule; + struct rl *next_node; + int visited; +} RuleLL; + +// ==================== Helper Functions ==================== + +// Check if the rule is already in the rule linked list +int is_in_ruleLL(RuleLL *head, Rule *rule) { + RuleLL *curr = head; + while (curr != NULL) { + if (curr->rule == rule) { + return 1; + } + curr = curr->next_node; + } + return 0; +} +",, +63c648165f004bf4898cf950,2023-03-29T20:51:15.090Z,Help Fix Code,v2,function returns dynamically allocated string that contains the list of users stored in linked list curr. I get the error that list is used unitialized,"char* list_users(const User *curr) { + // First loop until curr is NULL to find the total number of chars to allocate + int list_len = 0; + while (curr != NULL) { + list_len += strlen(curr->name) + 2; // +2 for tab and new line + curr = curr->next; + } + list_len += 1; // +1 for null terminator + list_len += 10; // +10 for ""User List\n"" + + char *list = malloc(list_len * sizeof(char)); + + strcat(list, ""User List\n""); + while (curr != NULL) { + strcat(list, ""\t""); + strcat(list, curr->name); + strcat(list, ""\n""); + curr = curr->next; + } + + return list; +}",, +63c648165f004bf4898cf955,2023-03-12T01:36:43.498Z,Help Fix Code,v2,This code recursively evaluates rules by comparing the most recent modification time.,"void evaluate_rule(Rule *rule, struct stat *parent_stat){ + // given some conditions, execute action of the rule (compares dependencies) + struct stat rule_stat; + stat(rule->target, &rule_stat); + + Dependency *cur_dep = rule->dependencies; + if(cur_dep != NULL){ + // recursively call on dependency rules + while(cur_dep != NULL){ + evaluate_rule(cur_dep->rule, &rule_stat); + cur_dep = cur_dep->next_dep; + } + } + printf(""%s %s"", ""Cur rule: "", rule->target); + if(rule->dependencies == NULL){ + execute_action(rule->target, rule->actions); + } + // retrieve modified time + if(&rule_stat.st_mtim == NULL){ + printf(""%s\n"", "" has null mod time""); + } else { + printf(""%s %ld\n"", "", mod_time: "", rule_stat.st_mtim.tv_sec); + } + if(parent_stat != NULL){ + if(rule_stat.st_mtim.tv_sec > parent_stat->st_mtim.tv_sec){ + execute_action(rule->target, rule->actions); + } else if(rule_stat.st_mtim.tv_sec == parent_stat->st_mtim.tv_sec){ + if(rule_stat.st_mtim.tv_nsec > parent_stat->st_mtim.tv_nsec){ + execute_action(rule->target, rule->actions); + } + } + } else { + execute_action(rule->target, rule->actions); + } +}",, +63c648165f004bf4898cf955,2023-03-12T01:41:14.486Z,Help Write Code,v2,,,,"I want to write code that recursively calls the function through a linked list structure with each node being its own linked list. For each function call, I want to call a helper function: evaluate(Node)." +63c648165f004bf4898cf955,2023-03-12T01:45:01.101Z,Help Write Code,v2,,,,Write function: Evaluate. This function traverses a linked list with linked list nodes and recursively calls the function. The base case prints the value at the node. +63c648195f004bf4898cf969,2023-01-17T19:23:51.650Z,General Question,v1,How do I print an emoji to stdout?,,, +63c648195f004bf4898cf969,2023-01-17T19:26:27.004Z,Help Write Code,v1,,,,Implement a max-heap data structure +63c648195f004bf4898cf969,2023-01-20T03:17:46.969Z,General Question,v1,How do you read a string from stdin using scanf?,,, +63c648195f004bf4898cf969,2023-01-20T03:27:58.442Z,General Question,v1,What does %10s do?,,, +63c648195f004bf4898cf969,2023-01-28T23:08:03.550Z,General Question,v1,What is the type for a pointer to a struct?,,, +63c648195f004bf4898cf969,2023-03-13T04:25:02.227Z,Help Write Code,v2,,,,"I want to take a string like ""main : dep1 dep2"" and split it into the two strings ""main"" and ""dep1 dep2""" +63c648195f004bf4898cf969,2023-03-13T04:26:37.376Z,General Question,v2,How can you use strtok to handle multiple delimiter characters?,,, +63c648195f004bf4898cf969,2023-03-13T04:29:15.839Z,Explain Code,v2,,"#include +#include +#include + +int main() { + char str_to_delim[] = ""name : dep1 dep2""; + strtok(str_to_delim, "" :""); + return 0; +}",, +63c6481a5f004bf4898cf973,2023-01-27T20:59:09.162Z,General Question,v1,how do I get the size of an array that's passed into the main function,,, +63c6481a5f004bf4898cf973,2023-01-27T21:35:08.142Z,General Question,v1,how do you get the size of **argv,,, +63c6481a5f004bf4898cf973,2023-01-27T21:36:38.574Z,General Question,v1,How do you get the size of **argv that's passed into main,,, +63c6481a5f004bf4898cf973,2023-01-27T22:22:56.851Z,Help Fix Code,v1,,"#include +#include + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + int **two_array = malloc(sizeof(int*) * 2); + if(length % 2 == 1){ + two_array[0] = malloc(sizeof(int) * ((length / 2) + 1)); + } + else{ + two_array[0] = malloc(sizeof(int) * (length / 2)); + } + two_array[1] = malloc(sizeof(int) * (length / 2)); + int odd = 0; + int even = 0; + for(int i = 0; i < length; i++){ + if(i % 2 == 0){ + two_array[0][odd] = s[i]; + odd++; + } + else{ + two_array[0][even] = s[i]; + even++; + } + } + return two_array; +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int *full_array = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++){ + full_array[i] = strtol(strs[i+ 1], NULL, 10); + printf(""%d"", full_array[i]); + } + + return full_array; + +} + + +int main(int argc, char **argv) { + /* Replace the comments in the next two lines with the appropriate + arguments. Do not add any additional lines of code to the main + function or make other changes. + */ + int *full_array = build_array(argv, argc - 1); + int **result = split_array(full_array, argc - 1); + + printf(""Original array:\n""); + for (int i = 0; i < argc - 1; i++) { + printf(""%d "", full_array[i]); + } + printf(""\n""); + + printf(""result[0]:\n""); + for (int i = 0; i < argc / 2; i++) { + printf(""%d "", result[0][i]); + } + printf(""\n""); + + printf(""result[1]:\n""); + for (int i = 0; i < (argc - 1) / 2; i++) { + printf(""%d "", result[1][i]); + } + printf(""\n""); + free(full_array); + free(result[0]); + free(result[1]); + free(result); + return 0; +} +","Main is supposed to take a list of strings that are numbers. Then *build_array is supposed to convert the strings into numbers and return the array of numbers. Then **split_array is supposed to take the array made in *build_array, then create a 2d array where the first index is the odd indices of the int array, and the second index is the even indices of the int array.", +63c6481a5f004bf4898cf973,2023-01-27T22:27:24.494Z,Help Fix Code,v1,,"#include +#include + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + int **two_array = malloc(sizeof(int*) * 2); + if(length % 2 == 1){ + two_array[0] = malloc(sizeof(int) * ((length / 2) + 1)); + } + else{ + two_array[1] = malloc(sizeof(int) * (length / 2)); + } + two_array[1] = malloc(sizeof(int) * (length / 2)); + int odd = 0; + int even = 0; + for(int i = 0; i < length; i++){ + if(i % 2 == 0){ + two_array[0][odd] = s[i]; + odd++; + } + else{ + two_array[1][even] = s[i]; + even++; + } + } + return two_array; +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int *full_array = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++){ + full_array[i] = strtol(strs[i+ 1], NULL, 10); + printf(""%d"", full_array[i]); + } + + return full_array; + +} + + +int main(int argc, char **argv) { + /* Replace the comments in the next two lines with the appropriate + arguments. Do not add any additional lines of code to the main + function or make other changes. + */ + int *full_array = build_array(argv, argc - 1); + int **result = split_array(full_array, argc - 1); + + printf(""Original array:\n""); + for (int i = 0; i < argc - 1; i++) { + printf(""%d "", full_array[i]); + } + printf(""\n""); + + printf(""result[0]:\n""); + for (int i = 0; i < argc / 2; i++) { + printf(""%d "", result[0][i]); + } + printf(""\n""); + + printf(""result[1]:\n""); + for (int i = 0; i < (argc - 1) / 2; i++) { + printf(""%d "", result[1][i]); + } + printf(""\n""); + free(full_array); + free(result[0]); + free(result[1]); + free(result); + return 0; +} +","Main is supposed to take a list of strings that are numbers. Then *build_array is supposed to convert the strings into numbers and return the array of numbers. Then **split_array is supposed to take the array made in *build_array, then create a 2d array where the first index is the odd indices of the int array, and the second index is the even indices of the int array.", +63c6481a5f004bf4898cf973,2023-01-29T22:22:47.106Z,General Question,v1,How do I use command line from within a program,,, +63c6481a5f004bf4898cf973,2023-01-29T22:25:46.391Z,General Question,v1,"What is the variable type of system(""ls -l"")",,, +63c6481a5f004bf4898cf973,2023-01-29T22:32:25.202Z,General Question,v1,"Do I use char** to hold the information of system(""ls -l"")",,, +63c6481a5f004bf4898cf973,2023-01-29T23:43:11.233Z,General Question,v1,Do I call scanf for each line that directed to it from the shell,,, +63c6481a5f004bf4898cf973,2023-01-30T00:56:45.998Z,Help Fix Code,v1,,"#include +#include + +int main(){ + char s[10]; + char idk[31]; + char username[31]; + char groupname[31]; + int size; + char month[3]; + char date[3]; + char time[6]; + char filename[31]; + scanf(""%10s %31s %31s %31s %d %3s %3s %6s %31s"", s, idk, username, groupname, &size, month, date, time, filename); + printf(""%d\n"", size); + return 0; +}",Supposed to take in a single line of input with 9 different variables and print the 5th one, +63c6481a5f004bf4898cf973,2023-01-30T00:59:38.249Z,Help Fix Code,v1,,"#include +#include + +int main(){ + char s[10]; + char idk[31]; + char username[31]; + char groupname[31]; + int size; + char month[3]; + char date[3]; + char time[6]; + char filename[31]; + scanf(""%9s %30s %30s %30s %d %2s %2s %5s %30s"", s, idk, username, groupname, &size, month, date, time, filename); + printf(""%d\n"", size); + return 0; +}","Supposed to take in a single line of input with 9 different variables and print the 5th one +", +63c6481a5f004bf4898cf973,2023-01-30T01:02:29.248Z,Help Fix Code,v1,,"#include +#include + +int main(){ + char s[10]; + char idk[31]; + char username[31]; + char groupname[31]; + int size; + char month[3]; + char date[3]; + char time[6]; + char filename[31]; + scanf(""%9s %30s %30s %30s %d %2s %2s %5s %30s"", s, idk, username, groupname, &size, month, date, time, filename); + printf(""%d\n"", size); + return 0; +}",Supposed to take in a single line of input with 9 different variables and print the 5th one (which is an integer), +63c6481a5f004bf4898cf973,2023-01-30T01:05:46.429Z,Question from Code,v1,Why is it printing a random number instead of the number I inputted,"#include +#include + +int main(){ + char s[10]; + char idk[31]; + char username[31]; + char groupname[31]; + int size; + char month[3]; + char date[3]; + char time[6]; + char filename[31]; + scanf(""%9s %30s %30s %30s %d %2s %2s %5s %30s"", s, idk, username, groupname, &size, month, date, time, filename); + printf(""%d\n"", size); + return 0; +}",, +63c6481a5f004bf4898cf973,2023-01-30T01:10:01.612Z,Question from Code,v1,Why is printf printing a random number when scanf sets the number before the printf function,"#include +#include + +int main(){ + char s[10]; + char idk[31]; + char username[31]; + char groupname[31]; + int size; + char month[3]; + char date[3]; + char time[6]; + char filename[31]; + + scanf(""%9s %30s %30s %30s %d %2s %2s %5s %30s"", s, idk, username, groupname, &size, month, date, time, filename); + + printf(""%d\n"", size); + + return 0; +}",, +63c6481a5f004bf4898cf973,2023-01-30T01:15:23.674Z,Question from Code,v1,"Why is printf printing a 0 when scanf should change size before the printf function +","#include +#include + +int main(){ + char s[10]; + char idk[31]; + char username[31]; + char groupname[31]; + int size; + char month[3]; + char date[3]; + char time[6]; + char filename[31]; + + size = 0; + + scanf(""%9s %30s %30s %30s %d %2s %2s %5s %30s"", s, idk, username, groupname, &size, month, date, time, filename); + + printf(""%d\n"", size); + + return 0; +}",, +63c6481a5f004bf4898cf973,2023-01-30T01:47:10.802Z,General Question,v1,"If we are inputting a file into a program from shell, can we tell how many lines are in the file",,, +63c6481a5f004bf4898cf973,2023-01-30T18:22:48.660Z,General Question,v1,How do I take a subsection of a string,,, +63c6481a5f004bf4898cf973,2023-01-30T18:30:55.907Z,General Question,v1,Can I split an input into two variables if there is no space between them,,, +63c6481a5f004bf4898cf973,2023-01-30T18:47:33.989Z,General Question,v1,Can I split the input of a string into two by using %c first and then %8s,,, +63c6481a5f004bf4898cf973,2023-01-30T18:50:11.329Z,Help Fix Code,v1,,"#include +#include + +int main(){ + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char groupname[31]; + int size; + + size = 0; + + scanf(""%c %8s %30s %30s %30s %d"", directory, permissions, idk, username, groupname, &size); + + printf(""%c %8s %d \n"", directory, permissions, size); + + return 0; +}","Code is supposed to take 4 strings and an int. The first string will be split into two with directory taking the first character of the string and then permissions taking the other 9 characters. The printf will print directory, permissions and size to make sure the first string was split properly and the integer was also taken correctly.", +63c6481a5f004bf4898cf973,2023-01-30T18:53:38.209Z,General Question,v1,Does char name[10] specify a string that's 10 characters long or 9 characters long,,, +63c6481a5f004bf4898cf973,2023-01-30T18:56:56.397Z,Help Fix Code,v1,,"#include +#include + +int main(){ + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char groupname[31]; + int size; + + size = 0; + + scanf(""%c %9s %31s %31s %31s %d"", &directory, permissions, idk, username, groupname, &size); + + printf(""%c %9s %d \n"", directory, permissions, size); + + return 0; +}","Code is supposed to take 4 strings and an int. The first string will be split into two with directory taking the first character of the string and then permissions taking the other 9 characters. The printf will print directory, permissions and size to make sure the first string was split properly and the integer was also taken correctly.", +63c6481a5f004bf4898cf973,2023-01-30T18:59:32.086Z,Question from Code,v1,"Why do I get the message ""zsh: abort ./testing"" after the proper output when the input is ""-rwx------ 1 reid staff 1734 Jun 22 14:52 prog""","#include +#include + +int main(){ + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char groupname[31]; + int size; + + size = 0; + + scanf(""%c %9s %31s %31s %31s %d"", &directory, permissions, idk, username, groupname, &size); + + printf(""%c %9s %d \n"", directory, permissions, size); + + return 0; +}",, +63c6481a5f004bf4898cf973,2023-01-30T19:02:26.637Z,Question from Code,v1,"Why do I get the message: +""- rwx------ 1734 +zsh: abort ./testing"" +When the input is ""-rwx------ 1 reid staff 1734 Jun 22 14:52 prog""","#include +#include + +int main(){ + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char groupname[31]; + int size; + + size = 0; + + scanf(""%c %9s %31s %31s %31s %d"", &directory, permissions, idk, username, groupname, &size); + + printf(""%c %9s %d \n"", directory, permissions, size); + + return 0; +}",, +63c6481a5f004bf4898cf973,2023-01-30T19:11:44.091Z,Help Fix Code,v1,,"#include +#include + +int main(){ + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char groupname[31]; + int size; + char month[3]; + int date; + char time[5]; + char filename[31]; + + size = 0; + + scanf("" %c %9s %31s %31s %31s %d %3s %d %5s %31s"", &directory, permissions, idk, username, groupname, &size, month, &date, time, filename); + + printf(""%c %9s %31s %31s %d %3s %d %5s %31s \n"", directory, permissions, idk, username, groupname, size, month, date, time, filename); + + return 0; +}",Code is supposed to take 7 strings and 2 ints. The first string will be split into two with directory taking the first character of the string and then permissions taking the other 9 characters. The printf will print the variables., +63c6481a5f004bf4898cf973,2023-01-30T19:17:17.587Z,Question from Code,v1,"Why does this input ""-rwx------ 1 reid staff 1734 Jun 22 14:52 prog"", give this output ""- rwx------ 1 reid staff 1536 Jun 0 14:52 prog +zsh: abort ./testing""","#include +#include + +int main(){ + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char group_name[31]; + int size; + char month[3]; + int date; + char time[5]; + char filename[31]; + + size = 0; + + scanf("" %c %9s %31s %31s %31s %d %3s %d %5s %31s"", &directory, permissions, idk, username, group_name, &size, month, &date, time, filename); + + printf(""%c %9s %31s %31s %31s %d %3s %d %5s %31s \n"", directory, permissions, idk, username, group_name, size, month, date, time, filename); + + return 0; +}",, +63c6481a5f004bf4898cf973,2023-01-30T19:23:34.436Z,Question from Code,v1,"Why does the input of ""-rwx------ 1 reid staff 1734 Jun 22 14:52 prog"" give the correct characters and strings in the printf but wrong ints","#include +#include + +int main(){ + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char group_name[31]; + int size; + char month[3]; + int date; + char time[5]; + char filename[31]; + + size = 0; + + scanf(""%c %s %s %s %s %d %s %d %s %s"", &directory, permissions, idk, username, group_name, &size, month, &date, time, filename); + + printf(""%c %s %s %s %s %d %s %d %s %s \n"", directory, permissions, idk, username, group_name, size, month, date, time, filename); + + return 0; +}",, +63c6481a5f004bf4898cf973,2023-01-30T19:25:35.123Z,Question from Code,v1,"Why does the input of ""-rwx------ 1 reid staff 1734 Jun 22 14:52 prog"" give the correct characters and strings in the printf but size = 1536 and date = 0","#include +#include + +int main(){ + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char group_name[31]; + int size; + char month[3]; + int date; + char time[5]; + char filename[31]; + + size = 0; + + scanf(""%c %s %s %s %s %d %s %d %s %s"", &directory, permissions, idk, username, group_name, &size, month, &date, time, filename); + + printf(""%c %s %s %s %s %d %s %d %s %s \n"", directory, permissions, idk, username, group_name, size, month, date, time, filename); + + return 0; +}",, +63c6481a5f004bf4898cf973,2023-01-30T19:34:57.835Z,Help Fix Code,v1,,"#include +#include + +int main(){ + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char group_name[31]; + char s_size[31]; + char month[3]; + char date[2]; + char time[5]; + char filename[31]; + + scanf(""%c %s %s %s %s %s %s %s %s %s"", &directory, permissions, idk, username, group_name, s_size, month, date, time, filename); + + int size = strtol(s_size, NULL, 10); + + printf(""%c %s %s %s %s %d %s %s %s %s \n"", directory, permissions, idk, username, group_name, size, month, date, time, filename); + + return 0; +}",Code is supposed to take 9 strings. The first string will be split into two with directory taking the first character of the string and then permissions taking the other 9 characters. Then s_size will be converted into an integer. Then all the variables except s_size are printed, +63c6481a5f004bf4898cf973,2023-01-30T19:36:46.024Z,Help Fix Code,v1,,"#include +#include + +int main(){ + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char group_name[31]; + char s_size[31]; + char month[3]; + char date[2]; + char time[5]; + char filename[31]; + + scanf(""%c %s %s %s %s %s %s %s %s %s"", &directory, permissions, idk, username, group_name, s_size, month, date, time, filename); + + int size = strtol(s_size, NULL, 10); + + printf(""%c %s %s %s %s %d %s %s %s %s \n"", directory, permissions, idk, username, group_name, size, month, date, time, filename); + + return 0; +}",Code is supposed to take 9 strings. The first string is 10 characters long and will be split into two with directory taking the first character of the string and then permissions taking the other 9 characters. Then s_size will be converted into an integer. Then all the variables except s_size are printed, +63c6481a5f004bf4898cf973,2023-01-30T19:41:10.183Z,Question from Code,v1,"Why does the input of ""-rwx------ 1 reid staff 1734 Jun 22 14:52 prog"" give the error of ""zsh: abort""",-rwx------ 1 reid staff 1734 Jun 22 14:52 prog,, +63c6481a5f004bf4898cf973,2023-01-30T19:42:44.676Z,Question from Code,v1,"Why does the input of ""-rwx------ 1 reid staff 1734 Jun 22 14:52 prog"" give the error of ""zsh: abort""","#include +#include + +int main(){ + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char group_name[31]; + char s_size[31]; + char month[31]; + char date[31]; + char time[31]; + char filename[31]; + + scanf("" %c %s %s %s %s %s %s %s %s %s"", &directory, permissions, idk, username, group_name, s_size, month, date, time, filename); + + int size = strtol(s_size, NULL, 10); + + printf("" %c %s %s %s %s %d %s %s %s %s \n"", directory, permissions, idk, username, group_name, size, month, date, time, filename); + + return 0; +}",, +63c6481a5f004bf4898cf973,2023-01-30T19:44:23.110Z,Question from Code,v1,"Why does the input of ""-rwx------ 1 reid staff 1734 Jun 22 14:52 prog"" give the error of ""zsh: abort"" +","#include +#include + +int main(){ + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char group_name[31]; + char s_size[31]; + char month[31]; + char date[31]; + char time[31]; + char filename[31]; + + scanf(""%c%s %s %s %s %s %s %s %s %s"", &directory, permissions, idk, username, group_name, s_size, month, date, time, filename); + + int size = strtol(s_size, NULL, 10); + + printf(""%c %s %s %s %s %d %s %s %s %s \n"", directory, permissions, idk, username, group_name, size, month, date, time, filename); + + return 0; +}",, +63c6481a5f004bf4898cf973,2023-01-30T19:45:44.235Z,Question from Code,v1,"How do I get the input of ""-rwx------ 1 reid staff 1734 Jun 22 14:52 prog"" to not give the error of ""zsh: abort""","#include +#include + +int main(){ + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char group_name[31]; + char s_size[31]; + char month[31]; + char date[31]; + char time[31]; + char filename[31]; + + scanf(""%c%s %s %s %s %s %s %s %s %s"", &directory, permissions, idk, username, group_name, s_size, month, date, time, filename); + + int size = strtol(s_size, NULL, 10); + + printf(""%c %s %s %s %s %d %s %s %s %s \n"", directory, permissions, idk, username, group_name, size, month, date, time, filename); + + return 0; +}",, +63c6481a5f004bf4898cf973,2023-01-30T19:46:56.539Z,Question from Code,v1,"How do I get the input of ""-rwx------ 1 reid staff 1734 Jun 22 14:52 prog"" to not give the error of ""zsh: abort""","#include +#include + +int main(){ + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char group_name[31]; + char s_size[31]; + char month[31]; + char date[31]; + char time[31]; + char filename[31]; + + scanf(""%c %s %s %s %s %s %s %s %s %s"", &directory, permissions, idk, username, group_name, s_size, month, date, time, filename); + + int size = strtol(s_size, NULL, 10); + + printf(""%c %s %s %s %s %d %s %s %s %s \n"", directory, permissions, idk, username, group_name, size, month, date, time, filename); + + return 0; +}",, +63c6481a5f004bf4898cf973,2023-01-30T19:53:43.186Z,Question from Code,v1,"Why does the input of ""-rwx------ 1 reid staff 1734 Jun 22 14:52 prog"", give correct print statement, but still give an error after","#include +#include + +int main(){ + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char group_name[31]; + char s_size[31]; + char month[31]; + char date[31]; + char time[31]; + char filename[31]; + + scanf(""%c %s %s %s %s %s %s %s %s %s"", &directory, permissions, idk, username, group_name, s_size, month, date, time, filename); + + int size = strtol(s_size, NULL, 10); + + printf(""%c %s %s %s %s %d %s %s %s %s \n"", directory, permissions, idk, username, group_name, size, month, date, time, filename); + + return 0; +}",, +63c6481a5f004bf4898cf973,2023-01-30T19:58:56.093Z,Help Fix Code,v1,,"#include +#include + +int main(){ + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char group_name[31]; + char s_size[31]; + char month[31]; + char date[31]; + char time[31]; + char filename[31]; + + scanf(""%c %s %s %s %s %s %s %s %s %s"", &directory, permissions, idk, username, group_name, s_size, month, date, time, filename); + + int size = strtol(s_size, NULL, 10); + + printf(""%c %s %s %s %s %d %s %s %s %s \n"", directory, permissions, idk, username, group_name, size, month, date, time, filename); + + return 0; +}",Code is supposed to take 9 strings. The first string is 10 characters long and will be split into two with directory taking the first character of the string and then permissions taking the other 9 characters. Then s_size will be converted into an integer. Then all the variables except s_size are printed, +63c6481a5f004bf4898cf973,2023-01-30T21:56:34.360Z,Help Fix Code,v1,,"#include +#include + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + int min_size = strtol(argv[1], NULL, 10); + + int num_filtered_files = 0; + + int eof = 1; + int first = 0; + + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char group_name[31]; + char s_size[31]; + char month[31]; + char date[31]; + char time[31]; + char filename[31]; + + int size = 1; + + while(0 < eof){ + if(first == 0){ + eof = scanf(""%c%s%s%s%s%s%s%s%s%s"", &directory, permissions, idk, username, group_name, s_size, month, date, time, filename); + first = 1; + } + else{ + eof = scanf(""%c%s%s%s%s%s%s%s%s%s"", &directory, permissions, idk, username, group_name, s_size, month, date, time, filename); + } + if(eof == 0){ + break; + } + if (directory == '-'){ + if(min_size < size){ + num_filtered_files = num_filtered_files + 1; + } + } + + } + + printf(""%d \n"", num_filtered_files); + + return 0; +} +","The main takes in a number and potentially a string in argv. +min_size is supposed to be the number given in argv. +Then, a ""ls -l"" of the current directory is given as the input. +The first line is supposed to be skipped as it does not match the form of the listed files. +The size of the file is first taken as a string, and then converted to an int. +If the size of the file is greater than min_size, the num_filtered_files increases by 1. +Finally num_filtered_files is printed.", +63c6481a5f004bf4898cf973,2023-01-30T22:01:09.214Z,Help Fix Code,v1,,"#include +#include + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + int min_size = strtol(argv[1], NULL, 10); + + int num_filtered_files = 0; + + int eof = 1; + int first = 0; + + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char group_name[31]; + char s_size[31]; + char month[31]; + char date[31]; + char time[31]; + char filename[31]; + + int size; + + while(0 < eof){ + if(first == 0){ + eof = scanf(""%c%s%s%s%s%s%s%s%s%s"", &directory, permissions, idk, username, group_name, s_size, month, date, time, filename); + first = 1; + } + else{ + eof = scanf(""%c%s%s%s%s%s%s%s%s%s"", &directory, permissions, idk, username, group_name, s_size, month, date, time, filename); + } + if(eof == 0){ + break; + } + size = strtol(s_size, NULL, 10); + if (directory == '-'){ + if(min_size < size){ + num_filtered_files = num_filtered_files + 1; + } + } + + } + + printf(""%d \n"", num_filtered_files); + + return 0; +} +","The main takes in a number and potentially a string in argv. min_size is supposed to be the number given in argv. Then, a ""ls -l"" of the current directory is given as the input. The first line is supposed to be skipped as it does not match the form of the listed files. The size of the file is first taken as a string, and then converted to an int. If the size of the file is greater than min_size, the num_filtered_files increases by 1. Finally num_filtered_files is printed.", +63c6481a5f004bf4898cf973,2023-01-30T22:05:20.548Z,Help Fix Code,v1,,"#include +#include + + +int check_permissions(char *given, char *needed){ + int same = 0; + for(int i = 0; i < 9; i++){ + if(needed[i] != '-'){ + if(needed[i] != given[i]){ + same = 1; + break; + } + } + } + return same; +} + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + int min_size = strtol(argv[1], NULL, 10); + + int num_filtered_files = 0; + + int eof = 1; + int first = 0; + + char directory; + char permissions[9]; + char idk[31]; + char username[31]; + char group_name[31]; + char s_size[31]; + char month[31]; + char date[31]; + char time[31]; + char filename[31]; + int size; + + while(0 < eof){ + if(first == 0){ + char s1[10]; + char s2[10]; + eof = scanf(""%s %s"", s1, s2); + first = 1; + } + else{ + eof = scanf(""%c%s%s%s%s%s%s%s%s%s"", &directory, permissions, idk, username, group_name, s_size, month, date, time, filename); + } + if(eof == 0){ + break; + } + size = strtol(s_size, NULL, 10); + if (directory == '-'){ + if(argc == 3 && min_size < size && check_permissions(permissions, argv[2]) == 0){ + num_filtered_files ++; + } + else if(min_size < size){ + num_filtered_files = num_filtered_files + 1; + } + + } + + } + + printf(""%d \n"", num_filtered_files); + + return 0; +} +","The main takes in a number and potentially a string in argv. min_size is supposed to be the number given in argv. Then, a ""ls -l"" of the current directory is given as the input. The first line is supposed to be skipped as it does not match the form of the listed files. The size of the file is first taken as a string, and then converted to an int. If the size of the file is greater than min_size, the num_filtered_files increases by 1. Finally num_filtered_files is printed.", +63c6481a5f004bf4898cf973,2023-01-30T23:26:23.905Z,Help Write Code,v1,,,,"given an int, and an empty int array, convert each digit into a single place in the int array" +63c6481a5f004bf4898cf973,2023-01-30T23:28:26.708Z,General Question,v1,How do I use modulo to get the individual digits of an int,,, +63c6481a5f004bf4898cf973,2023-01-30T23:36:44.843Z,General Question,v1,"If I give a helper function an empty array, how do I keep the changes to the array in main without returning the array",,, +63c6481a5f004bf4898cf973,2023-01-30T23:38:52.673Z,General Question,v1,"If I give the address of an int array to helper function, what should the argument for the helper function be.",,, +63c6481a5f004bf4898cf973,2023-01-31T00:35:03.710Z,General Question,v1,"Is it possible to keep the value of an int array that is edited in a helper function if the helper function's argument is also int array, and it doesn't return the array?",,, +63c6481a5f004bf4898cf973,2023-01-31T00:49:01.841Z,Help Fix Code,v1,,"#include +#include + +int t_populate_array(int sin, int *sin_array) { + for (int i = 8; -1 < i; i--){ + sin_array[i] = sin % 10; + if(sin == 0){ + return 1; + } + sin = sin / 10; + } + return 0; +} + +int main() { + int sin_arrayy[9]; + int sin = 999999999; + int valid = t_populate_array(sin, &sin_arrayy); + for (int i = 0; i < 9; i++){ + printf(""%d \n"", sin_arrayy[i]); + } + printf(""Valid: %d \n"", valid); + + return 0; +}","Populate the empty integer array in a helper function, and return whether the given int was 9 digits long. +In the main function print the digits in the now populated array and whether the given number was 9 digits long", +63c6481a5f004bf4898cf973,2023-01-31T00:50:16.246Z,Help Fix Code,v1,,"#include +#include + +int t_populate_array(int sin, int *sin_array) { + for (int i = 8; -1 < i; i--){ + sin_array[i] = sin % 10; + if(sin == 0){ + return 1; + } + sin = sin / 10; + } + return 0; +} + +int main() { + int sin_arrayy[9]; + int sin = 999999999; + int valid = t_populate_array(sin, sin_arrayy); + for (int i = 0; i < 9; i++){ + printf(""%d \n"", sin_arrayy[i]); + } + printf(""Valid: %d \n"", valid); + + return 0; +}","Populate the empty integer array in a helper function, and return whether the given int was 9 digits long. In the main function print the digits in the now populated array and whether the given number was 9 digits long by either printing 0 or 1", +63c6481a5f004bf4898cf973,2023-01-31T01:06:56.793Z,General Question,v1,Can you keep the value of a variable in a helper function without malloc or returning the variable,,, +63c6481a5f004bf4898cf973,2023-01-31T01:14:49.100Z,General Question,v1,How do I set the address of an int array to be an already declared integer,,, +63c6481a5f004bf4898cf973,2023-02-03T18:50:15.337Z,Help Fix Code,v1,,"struct node{ + int value; + struct node *next; +};",Create a basic linked list class., +63c6481a5f004bf4898cf973,2023-02-03T18:51:25.257Z,Help Fix Code,v1,,"struct node{ + int value; + struct node *next; +};",Create a basic class. This class is supposed to act like a linked list., +63c6481a5f004bf4898cf973,2023-02-03T19:01:33.451Z,Help Fix Code,v1,,"typedef struct node{ + int value; + Node *next; +};","Create a basic class. This class is supposed to act like a linked list. +", +63c6481a5f004bf4898cf973,2023-02-03T19:49:56.065Z,General Question,v1,"In a linked list, why is the value of the next node, a pointer instead of the actual value of the node",,, +63c6481a5f004bf4898cf973,2023-02-03T19:58:19.277Z,General Question,v1,"If node *next is a pointer to a node, what is the value of next.",,, +63c6481a5f004bf4898cf973,2023-02-03T19:58:54.367Z,General Question,v1,"If node *two is a pointer to a node, what is the value of two",,, +63c6481a5f004bf4898cf973,2023-02-03T21:19:07.615Z,General Question,v1,Are structs classes?,,, +63c6481a5f004bf4898cf973,2023-02-03T21:28:45.106Z,General Question,v1,"If you pass an array to a function, do you need to use malloc to keep the changes to the array",,, +63c6481a5f004bf4898cf973,2023-02-03T21:31:52.062Z,General Question,v1,"To keep changes to variables made in a function, do you pass a pointer to the variable",,, +63c6481a5f004bf4898cf973,2023-02-03T23:00:22.050Z,General Question,v1,How do I compare strings in,,, +63c6481a5f004bf4898cf973,2023-02-03T23:05:26.787Z,General Question,v1,"If I take a string as an argument in the function, can I use strlen on it",,, +63c6481a5f004bf4898cf973,2023-02-03T23:45:17.440Z,General Question,v1,"If a function is given a string, can you directly edit the string in the function or do you need to use malloc",,, +63c6481a5f004bf4898cf973,2023-02-04T00:20:22.976Z,General Question,v1,Does strcpy also copy the null terminator,,, +63c6481a5f004bf4898cf973,2023-02-04T00:39:24.555Z,General Question,v1,Can I use strcat with a string and a character,,, +63c6481a5f004bf4898cf973,2023-02-14T00:33:36.738Z,General Question,v1,Can you use memset to set characters to null pointer,,, +63c6481a5f004bf4898cf973,2023-02-14T00:43:42.177Z,General Question,v1,How do I use malloc on a char * given to the function. How do I then assign characters to that char *.,,, +63c6481a5f004bf4898cf973,2023-02-14T00:55:13.333Z,General Question,v1,"If I'm given a char * in a function, how do I make the changes stay outsides the function",,, +63c6481a5f004bf4898cf973,2023-02-14T00:58:40.846Z,General Question,v1,"If the parameter to my function is char *, how do I make changes to the variable that will stay in main.",,, +63c6481a5f004bf4898cf973,2023-02-14T01:07:25.998Z,Help Fix Code,v1,,"char *copy(char *dest, const char *src, int capacity) { + dest = malloc(sizeof(char) * capacity); + memset(dest, '\0', capacity); + for(int i = 0; i < (capacity - 1); i++){ + dest[i] = src[i]; + } + return dest; +}","char *copy copies the string in *src to *dest, but it makes sure that *dest ends with a '\0'. capacity is supposed to be the maximum amount of characters copied to *dest, including the '\0' at the end.", +63c6481a5f004bf4898cf973,2023-02-14T01:08:29.695Z,Help Fix Code,v1,,"char *copy(char *dest, const char *src, int capacity) { + dest = malloc(sizeof(char) * capacity); + memset(dest, '\0', capacity); + dest[capacity] = '\0'; + for(int i = 0; i < (capacity - 1); i++){ + dest[i] = src[i]; + } + return dest; +}","char *copy copies the string in *src to *dest, but it makes sure that *dest ends with a '\0'. capacity is supposed to be the maximum amount of characters copied to *dest, including the '\0' at the end. +", +63c6481a5f004bf4898cf973,2023-02-14T01:09:03.999Z,Question from Code,v1,Why don't my changes to dest remain in main?,"char *copy(char *dest, const char *src, int capacity) { + dest = malloc(sizeof(char) * capacity); + memset(dest, '\0', capacity); + for(int i = 0; i < (capacity - 1); i++){ + dest[i] = src[i]; + } + return dest; +}",, +63c6481a5f004bf4898cf973,2023-02-14T01:09:57.539Z,Question from Code,v1,How do I make changes to dest remain in main,"char *copy(char *dest, const char *src, int capacity) { + dest = malloc(sizeof(char) * capacity); + memset(dest, '\0', capacity); + for(int i = 0; i < (capacity - 1); i++){ + dest[i] = src[i]; + } + return dest; +}",, +63c6481a5f004bf4898cf973,2023-02-14T01:21:24.171Z,General Question,v1,Do you need to use malloc when changing the values of an array in a function,,, +63c6481a5f004bf4898cf973,2023-02-14T18:54:50.942Z,General Question,v1,Can you change the value of a macro from the command line?,,, +63c6481a5f004bf4898cf973,2023-02-14T19:16:38.931Z,Explain Code,v1,,"#include +#include +#include +#include ""friends.h"" + +#define INPUT_BUFFER_SIZE 256 +#define INPUT_ARG_MAX_NUM 12 +#define DELIM "" \n"" + + +/* + * Print a formatted error message to stderr. + */ +void error(char *msg) { + fprintf(stderr, ""Error: %s\n"", msg); +} + +/* + * Read and process commands + * Return: -1 for quit command + * 0 otherwise + */ +int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) { + User *user_list = *user_list_ptr; + + if (cmd_argc <= 0) { + return 0; + } else if (strcmp(cmd_argv[0], ""quit"") == 0 && cmd_argc == 1) { + return -1; + } else if (strcmp(cmd_argv[0], ""add_user"") == 0 && cmd_argc == 2) { + switch (create_user(cmd_argv[1], user_list_ptr)) { + case 1: + error(""user by this name already exists""); + break; + case 2: + error(""username is too long""); + break; + } + } else if (strcmp(cmd_argv[0], ""list_users"") == 0 && cmd_argc == 1) { + list_users(user_list); + } else if (strcmp(cmd_argv[0], ""update_pic"") == 0 && cmd_argc == 3) { + User *user = find_user(cmd_argv[1], user_list); + if (user == NULL) { + error(""user not found""); + } else { + switch (update_pic(user, cmd_argv[2])) { + case 1: + error(""file not found""); + break; + case 2: + error(""filename too long""); + break; + } + } + } else if (strcmp(cmd_argv[0], ""delete_user"") == 0 && cmd_argc == 2) { + if (delete_user(cmd_argv[1], user_list_ptr) == 1) { + error(""user by this name does not exist""); + } + } else if (strcmp(cmd_argv[0], ""make_friends"") == 0 && cmd_argc == 3) { + switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) { + case 1: + error(""users are already friends""); + break; + case 2: + error(""at least one user you entered has the max number of friends""); + break; + case 3: + error(""you must enter two different users""); + break; + case 4: + error(""at least one user you entered does not exist""); + break; + } + } else if (strcmp(cmd_argv[0], ""post"") == 0 && cmd_argc >= 4) { + // first determine how long a string we need + int space_needed = 0; + for (int i = 3; i < cmd_argc; i++) { + space_needed += strlen(cmd_argv[i]) + 1; + } + + // allocate the space + char *contents = malloc(space_needed); + + // copy in the bits to make a single string + strcpy(contents, cmd_argv[3]); + for (int i = 4; i < cmd_argc; i++) { + strcat(contents, "" ""); + strcat(contents, cmd_argv[i]); + } + + User *author = find_user(cmd_argv[1], user_list); + User *target = find_user(cmd_argv[2], user_list); + switch (make_post(author, target, contents)) { + case 1: + error(""the users are not friends""); + break; + case 2: + error(""at least one user you entered does not exist""); + break; + } + } else if (strcmp(cmd_argv[0], ""profile"") == 0 && cmd_argc == 2) { + User *user = find_user(cmd_argv[1], user_list); + if (print_user(user) == 1) { + error(""user not found""); + } + } else { + error(""Incorrect syntax""); + } + return 0; +} + + +/* + * Tokenize the string stored in cmd. + * Return the number of tokens, and store the tokens in cmd_argv. + */ +int tokenize(char *cmd, char **cmd_argv) { + int cmd_argc = 0; + char *next_token = strtok(cmd, DELIM); + while (next_token != NULL) { + if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) { + error(""too many arguments!""); + cmd_argc = 0; + break; + } + cmd_argv[cmd_argc] = next_token; + cmd_argc++; + next_token = strtok(NULL, DELIM); + } + + return cmd_argc; +} + + +int main(int argc, char* argv[]) { + int batch_mode = (argc == 2); + char input[INPUT_BUFFER_SIZE]; + FILE *input_stream; + + // Create the heads of the empty data structure + User *user_list = NULL; + + if (batch_mode) { + input_stream = fopen(argv[1], ""r""); + if (input_stream == NULL) { + perror(""Error opening file""); + exit(1); + } + } else { + // interactive mode + input_stream = stdin; + } + + printf(""Welcome to FriendMe! (Local version)\nPlease type a command:\n> ""); + + while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) { + // only echo the line in batch mode since in interactive mode the user + // just typed the line + if (batch_mode) { + printf(""%s"", input); + } + + char *cmd_argv[INPUT_ARG_MAX_NUM]; + int cmd_argc = tokenize(input, cmd_argv); + + if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { + break; // can only reach if quit command was entered + } + + printf(""> ""); + } + + if (batch_mode) { + fclose(input_stream); + } + + // Delete all users. This should free all heap memory that was allocated + // during the run of the program. Uncomment after you've implemented + // deleting users. + /*printf(""Freeing all heap space.\n""); + while (user_list != NULL) { + delete_user(user_list->name, &user_list); + }*/ + return 0; + } +",, +63c6481a5f004bf4898cf973,2023-02-15T02:25:42.973Z,General Question,v1,How do I ask for equivalence to null,,, +63c6481a5f004bf4898cf973,2023-02-15T02:30:35.114Z,General Question,v1,Does strlen include the null pointer,,, +63c6481a5f004bf4898cf973,2023-02-15T02:33:46.917Z,General Question,v1,"If a function is given a **struct, is the actual struct: *struct +",,, +63c6481a5f004bf4898cf973,2023-02-15T16:23:09.164Z,General Question,v1,"If I'm creating a struct, if I don't initialize the values, are they automatically set to null",,, +63c6481a5f004bf4898cf973,2023-02-15T16:50:07.294Z,General Question,v1,"If my struct has a variable that is a list of structs, can I set that value to NULL",,, +63c6481a5f004bf4898cf973,2023-02-15T16:54:21.652Z,General Question,v1,What is the else if statement in C called,,, +63c6481a5f004bf4898cf973,2023-02-15T16:56:42.602Z,General Question,v1,How do I compare strings,,, +63c6481a5f004bf4898cf973,2023-02-15T16:59:26.723Z,General Question,v1,"Let User be a struct. If I wanted a function to return a pointer to a User, would the return statement be *user_name or **user_name",,, +63c6481a5f004bf4898cf973,2023-02-15T17:09:35.623Z,General Question,v1,"Let char *name1 be a string. If I want a new char *name2 to be equal to name1, do I do char *name2 = *name1 or char *name2 = name1",,, +63c6481a5f004bf4898cf973,2023-02-15T17:14:45.856Z,General Question,v1,What does open return if a file doesn't exist,,, +63c6481a5f004bf4898cf973,2023-02-15T18:00:18.268Z,General Question,v1,"If an object has a list of structs, how do I find the size of this list.",,, +63c6481a5f004bf4898cf973,2023-02-15T18:03:20.962Z,General Question,v1,is an int the same as an unsigned integer,,, +63c6481a5f004bf4898cf973,2023-02-15T18:11:18.211Z,General Question,v1,Can I use index numbers to iterate through a list of structs,,, +63c6481a5f004bf4898cf973,2023-02-15T18:18:50.782Z,General Question,v1,"If User is a struct that has the variable: struct user *friends[MAX_FRIENDS], +If new_user is an empty User, would I be able to do new_user->friends[32]; to initialize the array",,, +63c6481a5f004bf4898cf973,2023-02-15T18:27:02.021Z,General Question,v1,"If a macro is defined in a different file, but the same folder, can that macro value be accessed from a different file.",,, +63c6481a5f004bf4898cf973,2023-02-15T18:30:10.553Z,General Question,v1,"If *name is a string, do I do strlen(*name) or strlen(name)",,, +63c6481a5f004bf4898cf973,2023-02-15T18:45:49.306Z,General Question,v1,"If I have a struct User, and User has a variable: struct user *friends[MAX_FRIENDS], would I have to malloc the individual index position when I'm placing a User in them",,, +63c6481a5f004bf4898cf973,2023-02-15T18:59:07.172Z,General Question,v1,Is a pointer to a NULL == NULL,,, +63c6481a5f004bf4898cf973,2023-02-15T19:32:46.458Z,General Question,v1,How do I print the contents f a file using fopen,,, +63c6481a5f004bf4898cf973,2023-02-15T21:13:29.570Z,General Question,v1,"If my struct has another struct as a variable, do I have to use malloc for that variable",,, +63c6481a5f004bf4898cf973,2023-02-15T21:15:26.467Z,General Question,v1,"If I have struct user, and a variable for User is: struct post *first_post;, if I'm initializing a new User called new_user, would I have to use malloc on new_user->first_post;",,, +63c6481a5f004bf4898cf973,2023-02-15T21:26:22.649Z,General Question,v1,Should the tail of a linked list use malloc on it's next variable,,, +63c6481a5f004bf4898cf973,2023-02-15T21:51:36.173Z,General Question,v1,How do I use time_t to record the time,,, +63c6481a5f004bf4898cf973,2023-02-15T22:01:07.599Z,General Question,v1,If I'm creating a function to free heap memory. Should I give the function the pointer to the variable or just the variable,,, +63c6481a5f004bf4898cf973,2023-02-15T22:07:00.966Z,General Question,v1,What is the function that frees hap memory,,, +63c6481a5f004bf4898cf973,2023-02-15T22:09:03.773Z,Question from Code,v1,Am I freeing the right memory,"int remove_post(Post **head_post){ + Post *head = *head_post; + free(head); +}",, +63c6481a5f004bf4898cf973,2023-02-15T22:10:00.484Z,Question from Code,v1,Am I freeing the right memory,"int remove_post(struct post **head_post){ + free(**head_post); +}",, +63c6481a5f004bf4898cf973,2023-02-15T22:11:00.135Z,Question from Code,v1,Am I freeing the right memory,"int remove_post(struct post **head_post){ + struct post *head = *head_post; + free(head); +}",, +63c6481a5f004bf4898cf973,2023-02-15T22:11:59.093Z,Question from Code,v1,What memory am I freeing,"int remove_post(struct post **head_post){ + free(*head); +}",, +63c6481a5f004bf4898cf973,2023-02-15T22:12:23.004Z,Question from Code,v1,"What memory am I freeing +","int remove_post(struct post **head_post){ + free(*head_post); +}",, +63c6481a5f004bf4898cf973,2023-02-15T22:12:45.127Z,Question from Code,v1,Am I freeing the right memory,"int remove_post(struct post **head_post){ + free(*head_post); +}",, +63c6481a5f004bf4898cf973,2023-02-15T22:14:01.627Z,Question from Code,v1,"If I'm treeing to free the object, am I freeing the right memory","int remove_post(struct post **post){ + free(*post); +}",, +63c6481a5f004bf4898cf973,2023-02-15T22:22:05.173Z,General Question,v1,Is this valid: &(head->next),,, +63c6481a5f004bf4898cf973,2023-02-15T22:52:20.478Z,General Question,v1,"If I have a struct user with the variable time_t *date, do I free free(&(head->date)) or free(head->date);",,, +63c6481a5f004bf4898cf973,2023-02-15T23:07:15.660Z,General Question,v1,How do I set a char array after it's been initialized,,, +63c6481a5f004bf4898cf973,2023-02-15T23:17:59.973Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int create_user(const char *name, User **user_ptr_add) { + User *new_user = malloc(sizeof(User)); + new_user->name = malloc(sizeof(char) * MAX_NAME); + strcpy(new_user->name, name); + new_user->profile_pic = malloc(sizeof(char) * MAX_NAME); + new_user->profile_pic[0] = '\0'; + new_user->first_post = malloc(sizeof(Post)); + new_user->first_post = NULL; + new_user->friends = malloc(sizeof(User) * MAX_FRIENDS); +}",Create a struct user, +63c6481a5f004bf4898cf973,2023-02-15T23:32:21.087Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + if(head == NULL){ + return NULL; + } + User *curr = *head; + if(strcmp(curr->name, name) == 0){ + return *curr; + } + while(curr->next != NULL) + curr = curr->next; + if(strcmp(curr->name, name) == 0){ + return *curr; + } + return NULL; +}",Traverse through a linked structure to find a specific User, +63c6481a5f004bf4898cf973,2023-02-15T23:54:32.249Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int create_user(const char *name, User **user_ptr_add) { + if(find_user(name, *user_ptr_add) != NULL){ + return 1; + } + if(MAX_NAME < strlen(name) + 1){ + return 2; + } + else{ + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->profile_pic[0] = '\0'; + new_user->first_post = malloc(sizeof(Post)); + new_user->first_post = NULL; + if(*user_ptr_add == NULL){ + new_user->next = *user_ptr_add; + **user_ptr_add = *new_user; + } + else{ + User *curr = *user_ptr_add; + while(curr->next != NULL){ + curr = curr->next; + } + new_user->next = curr->next; + curr->next = new_user; + } + return 0; + } + return -1; +}",Add a new user to the end of the linked structure, +63c6481a5f004bf4898cf973,2023-02-16T00:02:09.917Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int make_post(const User *author, User *target, char *contents){ + if(author == NULL || target == NULL){ + return 2; + } + + int not_detected = 1; + for(int i = 0; i < MAX_FRIENDS; i++){ + if(strcmp(author->friends[i]->name, target->name) == 0){ + not_detected = 0; + break; + } + } + + if(not_detected == 1){ + return not_detected; + } + else{ + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + strcpy(new_post->contents, contents); + new_post->date = malloc(sizeof(time_t)); + *new_post->date = time(NULL); + new_post->next = target->first_post; + target->first_post = new_post; + return 0; + } + + return -1; +}",Create a post and set it to target's first_post, +63c6481a5f004bf4898cf973,2023-02-16T00:05:52.291Z,General Question,v1,How do I make scanf ignore spaces,,, +63c6481a5f004bf4898cf973,2023-02-16T00:12:24.012Z,General Question,v1,How do I make scanf ignore spaces but not \n,,, +63c6481a5f004bf4898cf973,2023-02-16T00:20:01.653Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int delete_user(const char *name, User **user_ptr_del) { + printf(""1""); + int no_exist = 1; + User *head = *user_ptr_del; + User *b_head = *user_ptr_del; + while(head != NULL){ + printf(""1""); + if(strcmp(head->name, name) == 0){ + no_exist = 0; + break; + } + b_head = head; + head = head->next; + printf(""1""); + } + + if(no_exist == 1){ + return 1; + } + else{ + remove_post(&(head->first_post)); + for(int i = 0; i < MAX_FRIENDS; i++){ + if(head->friends[i] != NULL){ + remove_friend(head->name, &(head->friends[i])); + } + } + if(strcmp(b_head->name, head->name) == 0){ + *user_ptr_del = (head->next); + free(head); + return 0; + } + else{ + b_head->next = head->next; + free(head); + return 0; + } + } + return -1; +}",Delete a User from the linked structure, +63c6481a5f004bf4898cf973,2023-02-16T00:20:55.543Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int delete_user(const char *name, User **user_ptr_del) { + int no_exist = 1; + User *head = *user_ptr_del; + User *b_head = *user_ptr_del; + while(head != NULL){ + if(strcmp(head->name, name) == 0){ + no_exist = 0; + break; + } + b_head = head; + head = head->next; + } + + if(no_exist == 1){ + return 1; + } + else{ + remove_post(&(head->first_post)); + for(int i = 0; i < MAX_FRIENDS; i++){ + if(head->friends[i] != NULL){ + remove_friend(head->name, &(head->friends[i])); + } + } + if(strcmp(b_head->name, head->name) == 0){ + *user_ptr_del = (head->next); + free(head); + return 0; + } + else{ + b_head->next = head->next; + free(head); + return 0; + } + } + return -1; +}",Delete a User from the linked structure, +63c6481a5f004bf4898cf973,2023-02-16T01:43:43.990Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int remove_post(Post **head_post){ + Post *head = *head_post; + if(head->next == NULL){ + printf(""1""); + free(head->next); + } + else{ + remove_post(&(head->next)); + } + free(head->date); + free(*head_post); + return 0; +}",Delete Posts in a linked structure recursively, +63c6481a5f004bf4898cf973,2023-02-16T01:44:22.286Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int remove_post(Post **head_post){ + Post *head = *head_post; + if(head->next == NULL){ + free(head->next); + } + else{ + remove_post(&(head->next)); + } + free(head->date); + free(*head_post); + return 0; +}",Delete Posts in a linked structure recursively, +63c6481a5f004bf4898cf973,2023-02-16T01:46:42.208Z,Question from Code,v1,Why does this code cause a segmentation fault,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int remove_post(Post **head_post){ + Post *head = *head_post; + if(head->next == NULL){ + free(head->next); + } + else{ + remove_post(&(head->next)); + } + free(head->date); + free(*head_post); + return 0; +}",, +63c6481a5f004bf4898cf973,2023-02-16T01:57:59.498Z,Question from Code,v1,Why is this code creating a segmentation fault,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +int make_post(const User *author, User *target, char *contents){ + if(author == NULL || target == NULL){ + return 2; + } + + int not_detected = 1; + for(int i = 0; i < MAX_FRIENDS; i++){ + if(strcmp(author->friends[i]->name, target->name) == 0){ + not_detected = 0; + break; + } + } + + if(not_detected == 1){ + return not_detected; + } + else{ + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post->author, author->name); + strcpy(new_post->contents, contents); + new_post->date = malloc(sizeof(time_t)); + *new_post->date = time(NULL); + new_post->next = target->first_post; + target->first_post = new_post; + return 0; + } + + return -1; +} +",, +63c6481a5f004bf4898cf973,2023-02-17T19:51:01.826Z,General Question,v1,What is the minimum value strcpy work with,,, +63c6481a5f004bf4898cf973,2023-02-22T21:53:19.784Z,General Question,v1,Does strcpy work on a char * that's been malloced,,, +63c6481a5f004bf4898cf973,2023-02-24T20:25:06.859Z,General Question,v1,"After declaring a char *, how do I fill it.",,, +63c6481a5f004bf4898cf973,2023-02-24T20:30:46.587Z,General Question,v1,Can you add a variable to the heap after having initialized it,,, +63c6481a5f004bf4898cf973,2023-02-24T20:36:40.912Z,General Question,v1,"If I declare a char[15], does that mean 16 bytes are allocated to the char * as it has 15 bytes of characters as well as one byte for the null terminator?",,, +63c6481a5f004bf4898cf973,2023-02-26T20:48:30.317Z,General Question,v1,"Can you put '\n' as a character in a string +",,, +63c6481a5f004bf4898cf973,2023-02-27T20:47:12.638Z,General Question,v1,"would the output of strlen(""five"") be four",,, +63c6481a5f004bf4898cf973,2023-02-27T21:27:51.236Z,General Question,v1,Can you malloc something when it's value has already been assigned,,, +63c6481a5f004bf4898cf973,2023-02-27T21:36:07.309Z,General Question,v1,"If you have a struct with a char *, and you've used malloc on that char, when freeing the memory, do you free(&thing->name) or free(thing->name) where thing is the variable name of the struct and name is the char * in the struct.",,, +63c6481a5f004bf4898cf973,2023-02-28T18:25:46.139Z,General Question,v1,"If I declare a char array like this: char name[x]; +Would x be equivalent to the number of bits the array can hold",,, +63c6481a5f004bf4898cf973,2023-02-28T19:12:38.543Z,General Question,v1,"If I have int array[3] = {1, 2, 3}; +would the value of array be the pointer to the first int?",,, +63c6481a5f004bf4898cf973,2023-03-11T22:06:19.671Z,General Question,v2,How do child processes return information to the parent process,,, +63c6481a5f004bf4898cf973,2023-03-13T17:37:54.368Z,General Question,v2,Can you create a 2d array by using to square brackets,,, +63c6481a5f004bf4898cf973,2023-03-13T17:39:14.402Z,General Question,v2,Is argc one more than the amount of inputs,,, +63c6481a5f004bf4898cf973,2023-03-13T17:56:41.909Z,General Question,v2,"If I'm using pipes pipes, and I want to read an int, do I give the address of the int",,, +63c6481a5f004bf4898cf973,2023-03-13T18:09:46.260Z,General Question,v2,"If I use pipes to read an integer, how do I use that integer from fd",,, +63c6481a5f004bf4898cf973,2023-03-13T23:39:10.852Z,General Question,v2,"If I'm reading a file, how do I make sure I read it line by line",,, +63c6481a5f004bf4898cf973,2023-03-13T23:40:58.779Z,General Question,v2,Do a lines of a file end with '\n' or '\0',,, +63c6481a5f004bf4898cf973,2023-03-13T23:52:19.126Z,General Question,v2,"If I have a FILE *fp, do I use fopen(fp, ""r"") to open the file for reading",,, +63c6481a5f004bf4898cf973,2023-03-14T00:19:54.489Z,General Question,v2,"If I use fgets, and \n is the last index position of the string, does fgets replace \n with \0",,, +63c6481a5f004bf4898cf973,2023-03-17T19:51:43.718Z,General Question,v2,Can you add index positions to an array,,, +63c6481a5f004bf4898cf973,2023-03-17T20:25:05.432Z,General Question,v2,How do I use FSEEK to reset my file position to the top of the file,,, +63c6481a5f004bf4898cf973,2023-03-17T20:36:04.304Z,General Question,v2,"If I have a struct rule, does this allow me to fill the array properly: +struct rule rules_to_use[10]; +struct rule *new_rule = malloc(sizeof(struct rule)); +rules_to_use[0] = new_rule;",,, +63c6481a5f004bf4898cf973,2023-03-17T20:42:09.068Z,General Question,v2,How do I make sure I'm assigning a read-only string to a member of a struct so that I don't use more memory than is assigned to the struct,,, +63c6481a5f004bf4898cf973,2023-03-17T20:49:07.883Z,General Question,v2,"If I use strcpy, will the destination string will have different memory address than the source string?",,, +63c6481a5f004bf4898cf973,2023-03-17T20:52:34.226Z,General Question,v2,"If I do char *name = ""bob""; will *name have the memory address of the read only memory of bob, meaning it's size will only be the size of a pointer?",,, +63c6481a5f004bf4898cf973,2023-03-17T20:55:23.496Z,General Question,v2,"char *rule_names[max_rules]; +Is this an array of pointers to strings?",,, +63c6481a5f004bf4898cf973,2023-03-18T00:18:58.309Z,General Question,v2,How do I use FSEEK to go up one line a file without knowing the size of the line,,, +63c6481a5f004bf4898cf973,2023-03-18T04:30:30.614Z,General Question,v2,Can I declare a pointer first and then use malloc in a separate line on the pointer,,, +63c6481a5f004bf4898cf973,2023-04-04T19:12:04.162Z,General Question,v2,"If I have code that is accepting input from a socket, and will stop when it detects ""\r\n"" using the function strstr, how do I print ""\r\n"" from input",,, +63c6481a5f004bf4898cf973,2023-04-25T17:28:46.922Z,General Question,v2,"char *flights[3]; +Is this equivalent to a char **flights = malloc(3* sizeof(char *));",,, +63c6481a5f004bf4898cf973,2023-04-25T19:18:58.442Z,General Question,v2,"When trying to send a sigaction signal to a process from shell (assume it is SIGUSR1), is it: +kill -USR1 pid +or +kill -SIGUSR1 pid +",,, +63c6481a5f004bf4898cf973,2023-04-25T19:34:35.752Z,General Question,v2,is it chmod 555 file.txt or chmod file.txt 555,,, +63c6481a5f004bf4898cf973,2023-04-25T19:40:39.138Z,General Question,v2,"What is : +cp ../*.in . +do",,, +63c6481a5f004bf4898cf973,2023-04-25T20:26:40.072Z,General Question,v2,"if I do printf(""%d"", x); will it print the bit value of x",,, +63c6481a5f004bf4898cf973,2023-04-25T20:53:14.758Z,General Question,v2,"if you have char *name1 and char *name2; +To set the address of name1 to name2, do you do name1 = name2;",,, +63c6481a5f004bf4898cf973,2023-04-25T20:55:50.210Z,General Question,v2,"if you have char *name1 and char *name2. To set the address of name1 to the address of name2, do you do name1 = name2;",,, +63c6481a5f004bf4898cf973,2023-04-25T21:48:53.281Z,General Question,v2,What does r+ do with fopen,,, +63c6481a5f004bf4898cf978,2023-01-18T00:13:44.889Z,General Question,v1,"I have this error with C program: phone.c:6:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[10]’ [-Wformat=] + 6 | scanf(""%s %d"", &str, &num); + | ~^ ~~~~ + | | | + | char * char (*)[10] + +how do i fix it? +",,, +63c6481a5f004bf4898cf978,2023-01-18T00:23:54.961Z,General Question,v1,"why wont this work? +int main(int argc, char **argv) { + char str[10]; + int num; + scanf(""%s %d"", str, &num); + + if (num == -1) { + printf(""%s"", str); + } else if (0 <= num && num < 10) { + printf(""%s"", *(&str+num)); + } else { + printf(""ERROR""); + } +}",,, +63c6481a5f004bf4898cf978,2023-01-23T15:04:36.960Z,General Question,v1,how do you make an array of an array of strings in C,,, +63c6481a5f004bf4898cf978,2023-01-23T15:33:48.088Z,General Question,v1,How to convert a string to an integer,,, +63c6481a5f004bf4898cf978,2023-02-28T03:41:36.060Z,General Question,v1,How do I make a dynamically sized array in C,,, +63c6481a5f004bf4898cf978,2023-02-28T03:42:02.541Z,General Question,v1,how to make an arraylist in C like python and how to append to it,,, +63c6481c5f004bf4898cf987,2023-01-18T23:55:38.328Z,General Question,v1,what type is scanf output,,, +63c6481c5f004bf4898cf987,2023-01-22T21:21:57.932Z,General Question,v1,"is the output of ls -l a string +",,, +63c6481c5f004bf4898cf987,2023-01-22T21:26:14.498Z,General Question,v1,how do you print an initialized integer,,, +63c6481c5f004bf4898cf987,2023-01-22T21:27:50.354Z,General Question,v1,how do you skip a line in the input stream,,, +63c6481c5f004bf4898cf987,2023-01-22T21:29:18.828Z,General Question,v1,how do i compile a file,,, +63c6481c5f004bf4898cf987,2023-01-22T21:32:49.190Z,General Question,v1,how do i put ls into standard input of a executable,,, +63c6481c5f004bf4898cf987,2023-01-22T21:36:31.599Z,General Question,v1,can you alternate between fgets and fscan for the same file,,, +63c6481c5f004bf4898cf987,2023-01-22T22:24:25.124Z,General Question,v1,how do you remove the first element from a string,,, +63c6481c5f004bf4898cf987,2023-01-23T00:19:34.601Z,General Question,v1,what is pipe in c,,, +63c6481c5f004bf4898cf987,2023-01-24T00:12:43.296Z,General Question,v1,how to compile two programs at once,,, +63c6481c5f004bf4898cf987,2023-01-24T23:14:46.366Z,Help Fix Code,v1,,"void fib(int **fib_sequence, int count){ + *fib_sequence = malloc(sizeof(int) * count); + + if(count >= 1){ + *fib_sequence[0] = 0; + } + printf(""HI\n""); + + if(count >= 2){ + *fib_sequence[1] = 1; + int i; + for(i = 2; i < count; i++){ + *fib_sequence[i] = *fib_sequence[i - 1] + *fib_sequence[i - 2]; + } + } +}",, +63c6481c5f004bf4898cf987,2023-01-24T23:19:20.495Z,Question from Code,v1,why am i getting a segmentation error,"void fib(int **fib_sequence, int count){ + *fib_sequence = malloc(sizeof(int) * count); + + if(count >= 1){ + *fib_sequence[0] = 0; + } + printf(""HI\n""); + + if(count >= 2){ + *fib_sequence[1] = 1; + int i; + for(i = 2; i < count; i++){ + *fib_sequence[i] = *fib_sequence[i - 1] + *fib_sequence[i - 2]; + } + } +}",, +63c6481c5f004bf4898cf987,2023-02-01T20:39:07.452Z,General Question,v1,how to compare strings,,, +63c6481c5f004bf4898cf987,2023-02-01T20:47:27.271Z,General Question,v1,does strlen include the null terminator,,, +63c6481c5f004bf4898cf987,2023-02-01T20:53:53.368Z,General Question,v1,how to trim a string,,, +63c6481c5f004bf4898cf987,2023-02-01T20:54:35.044Z,General Question,v1,how to get a sub string,,, +63c6481c5f004bf4898cf987,2023-02-01T22:36:47.518Z,General Question,v1,how to use strncat,,, +63c6481c5f004bf4898cf987,2023-02-02T02:25:43.028Z,General Question,v1,is the struct stored in the stack,,, +63c6481c5f004bf4898cf987,2023-02-02T02:28:43.117Z,General Question,v1,does strlen count the null terminator,,, +63c6481c5f004bf4898cf987,2023-02-02T02:38:55.080Z,Question from Code,v1,why am i getting an error,"int create_user(const char *name, User **user_ptr_add) { + if(strlen(name) > 33){ + return -2; + } + + while(*user_ptr_add != NULL){ + if (strcmp((**user_ptr_add).name, name) == 0){ + return -1; + } + *user_ptr_add = user_ptr_add->next; + } + + struct user add; + strcpy(add.name, name); + + user_ptr_add->next = &add; + return -1; +}",, +63c6481c5f004bf4898cf987,2023-02-02T02:49:08.368Z,Question from Code,v1,why am i getting a segmentation fault,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int create_user(const char *name, User **user_ptr_add) { + if(strlen(name) > 33){ + return -2; + } + + while(user_ptr_add != NULL){ + if (strcmp((**user_ptr_add).name, name) == 0){ + printf(""%s"", (**user_ptr_add).name); + return -1; + } + *user_ptr_add = (**user_ptr_add).next; + } + + User add; + strcpy(add.name, name); + + (**user_ptr_add).next = &add; + return -1; +}",, +63c6481c5f004bf4898cf987,2023-02-02T02:51:25.325Z,Question from Code,v1,why am i getting a segmentation fault,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int create_user(const char *name, User **user_ptr_add) { + if(strlen(name) > 33){ + return -2; + } + + while(user_ptr_add != NULL){ + if (strcmp((**user_ptr_add).name, name) == 0){ + printf(""%s"", (**user_ptr_add).name); + return -1; + } + *user_ptr_add = (**user_ptr_add).next; + } + + User add; + strcpy(add.name, name); + + (*user_ptr_add)->next = &add; + return -1; +}",, +63c6481c5f004bf4898cf987,2023-02-02T02:53:30.176Z,Question from Code,v1,why am i getting a segmentation fault,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int create_user(const char *name, User **user_ptr_add) { + if(strlen(name) > 33){ + return -2; + } + + while(user_ptr_add != NULL){ + if (strcmp((**user_ptr_add).name, name) == 0){ + printf(""%s"", (**user_ptr_add).name); + return -1; + } + user_ptr_add = &(*user_ptr_add)->next; + } + + User add; + strcpy(add.name, name); + + (*user_ptr_add)->next = &add; + return -1; +}",, +63c6481c5f004bf4898cf987,2023-02-02T02:55:56.404Z,Question from Code,v1,why am i getting a segmentation fault,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int create_user(const char *name, User **user_ptr_add) { + if(strlen(name) > 33){ + return -2; + } + + while(user_ptr_add != NULL){ + if (strcmp((**user_ptr_add).name, name) == 0){ + return -1; + } + user_ptr_add = &((**user_ptr_add).next); + } + + User add; + strcpy(add.name, name); + + (**user_ptr_add).next = &add; + return -1; +}",, +63c6481c5f004bf4898cf987,2023-02-02T03:07:18.280Z,Question from Code,v1,why am i getting a segmentation fault,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int create_user(const char *name, User **user_ptr_add) { + if(strlen(name) > 33){ + return -2; + } + + while((*user_ptr_add)->next != NULL){ + if (strcmp((**user_ptr_add).name, name) == 0){ + return -1; + } + *user_ptr_add = (*user_ptr_add)->next; + } + + User *add = malloc(sizeof(User)); + strcpy(add->name, name); + + (*user_ptr_add)->next = add; + return -1; +}",, +63c6481c5f004bf4898cf987,2023-02-02T03:08:06.715Z,Question from Code,v1,what line is causing a segmentation fault,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int create_user(const char *name, User **user_ptr_add) { + if(strlen(name) > 33){ + return -2; + } + + while((*user_ptr_add)->next != NULL){ + if (strcmp((**user_ptr_add).name, name) == 0){ + return -1; + } + *user_ptr_add = (*user_ptr_add)->next; + } + + User *add = malloc(sizeof(User)); + strcpy(add->name, name); + + (*user_ptr_add)->next = add; + return -1; +}",, +63c6481c5f004bf4898cf987,2023-02-02T03:09:06.707Z,Question from Code,v1,how do i fix my segmentation fault,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int create_user(const char *name, User **user_ptr_add) { + if(strlen(name) > 33){ + return -2; + } + + while((*user_ptr_add)->next != NULL){ + if (strcmp((**user_ptr_add).name, name) == 0){ + return -1; + } + *user_ptr_add = (*user_ptr_add)->next; + } + + User *add = malloc(sizeof(User)); + strcpy(add->name, name); + + (*user_ptr_add)->next = add; + return -1; +}",, +63c6481c5f004bf4898cf987,2023-02-02T03:10:27.624Z,Question from Code,v1,what line is causing a segmentation fault,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int create_user(const char *name, User **user_ptr_add) { + if(strlen(name) > 33){ + return -2; + } + + while((*user_ptr_add)->next != NULL && *user_ptr_add != NULL){ + if (strcmp((**user_ptr_add).name, name) == 0){ + return -1; + } + *user_ptr_add = (*user_ptr_add)->next; + } + + User *add = malloc(sizeof(User)); + strcpy(add->name, name); + + (*user_ptr_add)->next = add; + return -1; +}",, +63c6481c5f004bf4898cf987,2023-02-02T03:12:59.701Z,Question from Code,v1,what line is causing a segmentation fault and how can i fix it,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int create_user(const char *name, User **user_ptr_add) { + if(strlen(name) > 33){ + return -2; + } + + while((*user_ptr_add)->next != NULL && *user_ptr_add != NULL){ + if (strcmp((**user_ptr_add).name, name) == 0){ + return -1; + } + *user_ptr_add = (*user_ptr_add)->next; + } + + User *add = malloc(sizeof(User)); + strcpy(add->name, name); + + if((*user_ptr_add) == NULL){ + user_ptr_add = &add; + } + else{ + (*user_ptr_add)->next = add; + } + return 0; +}",, +63c6481c5f004bf4898cf987,2023-02-02T17:15:33.382Z,General Question,v1,how to use fopen to see if a file exists,,, +63c6481c5f004bf4898cf987,2023-02-02T20:24:31.871Z,General Question,v1,how to print an ascii file,,, +63c6481c5f004bf4898cf987,2023-02-02T20:48:20.230Z,Question from Code,v1,why is there an infinite loop,"User *find_user(const char *name, const User *head) { + User *curr = (User *)head; + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return curr; + } + } + return NULL; +}",, +63c6481c5f004bf4898cf987,2023-02-03T20:03:09.458Z,General Question,v1,what does the time function do,,, +63c6481c5f004bf4898cf987,2023-02-05T01:04:02.208Z,Help Fix Code,v1,,"typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int make_post(const User *author, User *target, char *contents) { + int i; + for(i = 0; i < MAX_FRIENDS; i++){ + if((target->friends)[i] == author){ + Post *pst = malloc(sizeof(Post)); + strncpy(pst->author, author->name, MAX_NAME); + pst->contents = contents; + time_t now; + time(&now); + pst->date = &now; + + pst->next = target->first_post; + target->first_post = pst; + } + } + return -1; +} + +int print_user(const User *user) { + if (user == NULL){ + return 1; + } + + FILE *f = fopen(user->profile_pic, ""r""); + if(f != NULL){ + char c = fgetc(f); + while(c != EOF){ + printf(""%c"", c); + c = fgetc(f); + } + fclose(f); + } + + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + printf(""Friends:\n""); + + int i; + for(i = 0; i < MAX_FRIENDS; i++){ + if(user->friends[i] != NULL){ + printf(""%s\n"", user->friends[i]->name); + } + else{ + break; + } + } + + printf(""------------------------------------------\n""); + printf(""Posts:\n""); + + Post *curr_post = user->first_post; + while(curr_post != NULL){ + printf(""From: %s\n"", curr_post->author); + printf(""%s\n"", ctime(curr_post->date)); + printf(""\n""); + printf(""%s\n"", curr_post->contents); + printf(""\n""); + curr_post = curr_post->next; + if(curr_post != NULL){ + printf(""===""); + printf(""\n""); + } + } + + printf(""------------------------------------------\n""); + return 0; +}",why is there a segmentation fault, +63c6481c5f004bf4898cf987,2023-02-05T02:33:57.526Z,Help Fix Code,v1,,"typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int make_post(const User *author, User *target, char *contents) { + int i; + for(i = 0; i < MAX_FRIENDS; i++){ + if((target->friends)[i] == author){ + Post *pst = malloc(sizeof(Post)); + strncpy(pst->author, author->name, MAX_NAME); + pst->contents = contents; + time_t now; + time(&now); + pst->date = &now; + + pst->next = target->first_post; + target->first_post = pst; + } + } + return -1; +} +int create_user(const char *name, User **user_ptr_add) { + if(strlen(name) > MAX_NAME){ + return 2; + } + + User *add = malloc(sizeof(User)); + strcpy(add->name, name); + + if(*user_ptr_add == NULL){ + *user_ptr_add = add; + return 0; + } + + User *curr = *user_ptr_add; + + while(curr->next != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + if (strcmp(curr->name, name) != 0){ + curr->next = add; + } + else{ + return -1; + } + + return 0; +} + +int delete_user(const char *name, User **user_ptr_del) { + User *target = *user_ptr_del; + while(target != NULL){ + if (strcmp(target->name, name) == 0){ + int i; + for(i = 0; i < MAX_FRIENDS; i++){ + User *curr_friend = target->friends[i]; + int deleted = 0; + int j; + for(j = 0; j < (MAX_FRIENDS - 1); j++){ + if(curr_friend->friends[j] == NULL){ + break; + } + + if (curr_friend->friends[j] == target || deleted == 1){ + deleted = 1; + curr_friend->friends[j] = curr_friend->friends[j + 1]; + } + } + curr_friend->friends[MAX_FRIENDS - 1] = NULL; + } + Post *curr_post = target->first_post; + while(curr_post != NULL){ + free(curr_post->date); + } + + free(target); + return 0; + } + target = target->next; + } + return 1; +}",why is there a segmentation fault, +63c6481c5f004bf4898cf987,2023-02-05T02:36:51.706Z,Question from Code,v1,why is there a segmentation fault,"typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +int make_post(const User *author, User *target, char *contents) { + int i; + for(i = 0; i < MAX_FRIENDS; i++){ + if((target->friends)[i] == author){ + Post *pst = malloc(sizeof(Post)); + strncpy(pst->author, author->name, MAX_NAME); + pst->contents = contents; + time_t now; + time(&now); + pst->date = &now; + + pst->next = target->first_post; + target->first_post = pst; + } + } + return -1; +} +int create_user(const char *name, User **user_ptr_add) { + if(strlen(name) > MAX_NAME){ + return 2; + } + + User *add = malloc(sizeof(User)); + strcpy(add->name, name); + + if(*user_ptr_add == NULL){ + *user_ptr_add = add; + return 0; + } + + User *curr = *user_ptr_add; + + while(curr->next != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + if (strcmp(curr->name, name) != 0){ + curr->next = add; + } + else{ + return -1; + } + + return 0; +} + +int delete_user(const char *name, User **user_ptr_del) { + User *target = *user_ptr_del; + while(target != NULL){ + if (strcmp(target->name, name) == 0){ + int i; + for(i = 0; i < MAX_FRIENDS; i++){ + User *curr_friend = target->friends[i]; + int deleted = 0; + int j; + for(j = 0; j < (MAX_FRIENDS - 1); j++){ + if(curr_friend->friends[j] == NULL){ + break; + } + + if (curr_friend->friends[j] == target || deleted == 1){ + deleted = 1; + curr_friend->friends[j] = curr_friend->friends[j + 1]; + } + } + curr_friend->friends[MAX_FRIENDS - 1] = NULL; + } + Post *curr_post = target->first_post; + while(curr_post != NULL){ + free(curr_post->contents); + free(curr_post->date); + } + + free(target); + return 0; + } + target = target->next; + } + return 1; +}",, +63c6481c5f004bf4898cf987,2023-03-12T20:45:21.323Z,General Question,v2,how do u use st_mtim,,, +63c6481d5f004bf4898cf991,2023-01-19T03:20:34.165Z,General Question,v1,Write a sample code for tim Sort using python,,, +63c6481e5f004bf4898cf99b,2023-01-20T19:25:29.855Z,General Question,v1,"in c, how can i restrict scanf() to only read the first 10 characters inputed",,, +63c6481e5f004bf4898cf99b,2023-01-20T19:27:05.980Z,General Question,v1,what does char phone[11] mean?,,, +63c6481e5f004bf4898cf99b,2023-01-27T00:49:51.605Z,Question from Code,v1,is this an array or string,char file[31];,, +63c6481e5f004bf4898cf99b,2023-01-27T00:51:06.079Z,Question from Code,v1,is this valid in c?,"char file; +file = '1234';",, +63c6481e5f004bf4898cf99b,2023-01-27T00:51:29.880Z,Help Fix Code,v1,,"char file; +file = '1234';",file should be a string, +63c6481e5f004bf4898cf99b,2023-01-29T01:37:19.216Z,General Question,v1,"in c, how can we read input from stand input in function main?",,, +63c6481e5f004bf4898cf99b,2023-02-03T02:09:50.874Z,General Question,v1,how can i concatenate a space to a string,,, +63c6481e5f004bf4898cf99b,2023-02-10T20:18:07.258Z,General Question,v1,how can i test if the pixels of a 24 bitmap is correctly printed out?,,, +63c6481e5f004bf4898cf99b,2023-02-10T20:23:27.369Z,General Question,v1,Any advices for aceing CSC209?,,, +63c6481e5f004bf4898cf99b,2023-02-10T20:24:33.763Z,General Question,v1,"any recommendation for vs code studio c language configuration? +",,, +63c6481e5f004bf4898cf99b,2023-02-10T21:06:41.793Z,General Question,v1,"'gcc' is not recognized as an internal or external command, +operable program or batch file.",,, +63c6481e5f004bf4898cf99b,2023-02-13T01:02:11.326Z,General Question,v1,how to use fprint to print time_t object,,, +63c6481e5f004bf4898cf99b,2023-02-13T01:15:17.738Z,General Question,v1,How can i print a file to standard output using fopen and fprintf,,, +63c6481e5f004bf4898cf99b,2023-02-13T01:40:11.655Z,Question from Code,v1,"imagining we have a name1 and we call user1 = find_user(name1, hearder).given that this function returns NULL in this case, if i want to check user1 is not NULL in an if statement, how should i write it","/* + * 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) { + // cast a (const User *) to a (User *) + User *current = (User *) head; + + // Use a while loop chekc if there exists an user with this name in the list given + while (current != NULL){ + if(strcmp(current->name, name)){ + return current; + } + current = current -> next; + } + + return NULL; +}",, +63c6481e5f004bf4898cf99b,2023-02-13T01:40:49.692Z,General Question,v1,why do we use user1 insteand of *user1,,, +63c6481e5f004bf4898cf99b,2023-02-13T01:41:24.932Z,Question from Code,v1,why we use user1 instead of *user1,"/* + * 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) { + // cast a (const User *) to a (User *) + User *current = (User *) head; + + // Use a while loop chekc if there exists an user with this name in the list given + while (current != NULL){ + if(strcmp(current->name, name)){ + return current; + } + current = current -> next; + } + + return NULL; +}",, +63c6481e5f004bf4898cf99b,2023-02-13T01:42:38.315Z,General Question,v1,why a pointer can be NULL,,, +63c6481e5f004bf4898cf99b,2023-02-13T02:04:05.590Z,Question from Code,v1,"given that i have this function header, can i call this function with an argument of type User * instead of const User*?","User *find_user(const char *name, const User *head) {",, +63c6481e5f004bf4898cf99b,2023-02-13T02:04:41.208Z,Question from Code,v1,what do you mean by const quanlifier is not part of the type?,"User *find_user(const char *name, const User *head) {",, +63c6481e5f004bf4898cf99b,2023-02-13T02:09:12.160Z,Question from Code,v1,How can I check if there is no empty place in friends?,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; +",, +63c6481e5f004bf4898cf99b,2023-02-13T03:24:15.399Z,General Question,v1,how can i make a time_t object of current time,,, +63c6481e5f004bf4898cf99b,2023-02-13T06:26:35.668Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +new_user->next = malloc(sizeof(User)); +","what is wrong with malloc here +", +63c6481e5f004bf4898cf99b,2023-02-13T06:28:29.807Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +new_user->next = malloc(sizeof(User*)); +",the error says invalid conversion from void* to user*, +63c6481e5f004bf4898cf99b,2023-02-13T06:29:28.114Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +new_user->next = malloc(sizeof(User)); +","avoid the error invalid conversion from void* to user* +and successfully allocate memory to this", +63c6481e5f004bf4898cf99b,2023-02-13T06:36:14.777Z,Help Fix Code,v1,," +int create_user(const char *name, User **user_ptr_add) { + User *new_user; + new_user = malloc(sizeof(User)); + User *current = *user_ptr_add; + + // 2 if the name cannot fit in the name array (e.g. > MAX_NAME) + if (sizeof(name) > 31){ + return 2; + } + + // When this loop stops, current is the last pointer to User in user_ptr_add + while(current != NULL){ + // return 1 user already exists in this list + if(strcmp(current->name, name) == 0){ + return 1; + } + current = current->next; + } + + // copy the name to new_user.name + strncpy(new_user->name, name, 32); + new_user->name[strlen(new_user->name)] = '\0'; + + // initialize this user's profile_pic to a null terminator + // (the first char in profile_pic) + new_user->profile_pic[0] = '\0'; + + // initialize other attributes + new_user->first_post = NULL; + for (int i = 0; i < MAX_FRIENDS; i++) { + new_user->friends[i] = malloc(sizeof(User)); //Allocate space of 1 User + new_user->friends[i] = NULL; + } + new_user->next = malloc(sizeof(User)); //Allocate space of 1 User + new_user->next = NULL; + + // Add this new_user to the tail of user_prtr_add + current->next = new_user; + + // return 0 if successful + return 0; + + // Still need to free memory afterwards (!!!) + // Maybe everthing is freed through delete user +}","/* + * 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) + */", +63c6481e5f004bf4898cf99b,2023-02-13T06:43:54.376Z,Question from Code,v1,Do i need to actually use malloc for new_user->friends and new_user->next?," +int create_user(const char *name, User **user_ptr_add) { + User *new_user; + new_user = malloc(sizeof(User)); + User *current = *user_ptr_add; + + // 2 if the name cannot fit in the name array (e.g. > MAX_NAME) + if (sizeof(name) > 31){ + return 2; + } + + // When this loop stops, current is the last pointer to User in user_ptr_add + while(current != NULL){ + // return 1 user already exists in this list + if(strcmp(current->name, name) == 0){ + return 1; + } + current = current->next; + } + + // copy the name to new_user.name + strncpy(new_user->name, name, 32); + new_user->name[strlen(new_user->name)] = '\0'; + + // initialize this user's profile_pic to a null terminator + // (the first char in profile_pic) + new_user->profile_pic[0] = '\0'; + + // initialize other attributes + new_user->first_post = NULL; + for (int i = 0; i < MAX_FRIENDS; i++) { + new_user->friends[i] = malloc(sizeof(User)); //Allocate space of 1 User + new_user->friends[i] = NULL; + } + new_user->next = malloc(sizeof(User)); //Allocate space of 1 User + new_user->next = NULL; + + // Add this new_user to the tail of user_prtr_add + current->next = new_user; + + // return 0 if successful + return 0; + + // Still need to free memory afterwards (!!!) + // Maybe everthing is freed through delete user +}",, +63c6481e5f004bf4898cf99b,2023-02-13T06:44:32.177Z,Question from Code,v1,Do i need to use malloc for new_user->friends and new_user->next which are attributes for new_user?," +int create_user(const char *name, User **user_ptr_add) { + User *new_user; + new_user = malloc(sizeof(User)); + User *current = *user_ptr_add; + + // 2 if the name cannot fit in the name array (e.g. > MAX_NAME) + if (sizeof(name) > 31){ + return 2; + } + + // When this loop stops, current is the last pointer to User in user_ptr_add + while(current != NULL){ + // return 1 user already exists in this list + if(strcmp(current->name, name) == 0){ + return 1; + } + current = current->next; + } + + // copy the name to new_user.name + strncpy(new_user->name, name, 32); + new_user->name[strlen(new_user->name)] = '\0'; + + // initialize this user's profile_pic to a null terminator + // (the first char in profile_pic) + new_user->profile_pic[0] = '\0'; + + // initialize other attributes + new_user->first_post = NULL; + for (int i = 0; i < MAX_FRIENDS; i++) { + new_user->friends[i] = malloc(sizeof(User)); //Allocate space of 1 User + new_user->friends[i] = NULL; + } + new_user->next = malloc(sizeof(User)); //Allocate space of 1 User + new_user->next = NULL; + + // Add this new_user to the tail of user_prtr_add + current->next = new_user; + + // return 0 if successful + return 0; + + // Still need to free memory afterwards (!!!) + // Maybe everthing is freed through delete user +}",, +63c6481e5f004bf4898cf99b,2023-02-13T06:45:46.177Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *new_user; + new_user = malloc(sizeof(User)); + new_user->next = NULL; + new_user->first_post = NULL; + User *current = *user_ptr_add; + + // retrun 2 if the name cannot fit in the name array (e.g. > MAX_NAME) + if (sizeof(name) > 31){ + return 2; + } + + // When this loop stops, current is the last pointer to User in user_ptr_add + while(current != NULL){ + // return 1 user already exists in this list + if(strcmp(current->name, name) == 0){ + return 1; + } + current = current->next; + } + + // copy the name to new_user.name + strncpy(new_user->name, name, 32); + new_user->name[strlen(new_user->name)] = '\0'; + + // initialize this user's profile_pic to a null terminator + // (the first char in profile_pic) + new_user->profile_pic[0] = '\0'; + + // initialize other attributes + new_user->friends = malloc(sizeof(User) * 10); + for (int i = 0; i < MAX_FRIENDS; i++) { + new_user->friends[i] = NULL; //Allocate space of 1 User + } + new_user->next = malloc(sizeof(User)); //Allocate space of 1 User + new_user->next = NULL; + + // Add this new_user to the tail of user_prtr_add + current->next = new_user; + + // return 0 if successful + return 0; +}","/* + * 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) + */", +63c6481e5f004bf4898cf99b,2023-02-13T19:51:36.357Z,General Question,v1,"given that i want to print to standardoutput, whats the difference between printf and fprintf",,, +63c6481e5f004bf4898cf99b,2023-02-13T19:54:03.030Z,Question from Code,v1,What the difference between these lines? which one should i use for reporting error,"fprintf(stderr, ""Error: could not open file or file does not exist""); +printf(""Error: could not open file or file does not exist"");",, +63c6481e5f004bf4898cf99b,2023-02-13T21:44:32.783Z,Question from Code,v1,what is wrong with this code,"User *find_user(const char *name, const User *head) { + // cast a (const User *) to a (User *) + User *current = (User *) head; + + if (current == NULL) + { + return NULL; + }else if (strcmp(current->name, name)) + { + return current; + }else{ + if (current->next != NULL) + { + find_user(name, current->next); + } + } +}",, +63c6481e5f004bf4898cf99b,2023-02-13T21:45:22.883Z,Help Fix Code,v1,," +User *find_user(const char *name, const User *head) { +}","/* + * 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. + */", +63c6481e5f004bf4898cf99b,2023-02-13T21:52:27.386Z,Help Fix Code,v1,,"User *find_user(const char *name, const User *head) { + // cast a (const User *) to a (User *) + User *current = (User *) head; + + if (current == NULL) + { + return NULL; + } + + while (current != NULL && current->next != NULL) + { + if(strcmp(current->name, name) == 0){ + return current; + } + current = current->next; + } + + return NULL; + +}",/* * 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. */, +63c6481e5f004bf4898cf99b,2023-02-14T01:00:28.281Z,Question from Code,v1,"gievn that current is assigned as in line 3. In the while loop, when I iterate using current. When i assign current in line 10, is there any changes made to user_ptr_add?"," int create_user(const char *name, User **user_ptr_add) { + User *new_user = malloc(sizeof(User)); + User *current = *user_ptr_add; + + while(current != NULL && current->next != NULL){ + // return 1 user already exists in this list + if(strcmp(current->name, name) == 0){ + return 1; + } + current = current->next; + }",, +63c6481e5f004bf4898cf99b,2023-02-14T01:01:16.609Z,Help Fix Code,v1,," int create_user(const char *name, User **user_ptr_add) { + User *new_user = malloc(sizeof(User)); + User *current = *user_ptr_add; + + while(current != NULL && current->next != NULL){ + // return 1 user already exists in this list + if(strcmp(current->name, name) == 0){ + return 1; + } + current = current->next; + }","gievn that current is assigned as in line 3. In the while loop, when I iterate using current. When i assign current in line 10, I want this change to this object also cause changes to user_ptr_add, what should i do", +63c6481e5f004bf4898cf99b,2023-02-14T01:02:16.066Z,Help Fix Code,v1,," int create_user(const char *name, User **user_ptr_add) { + User *new_user = malloc(sizeof(User)); + User *current = *user_ptr_add; + + while(current != NULL && current->next != NULL){ + // return 1 user already exists in this list + if(strcmp(current->name, name) == 0){ + return 1; + } + current = current->next; + } + + current->next = new_user;","gievn that current is assigned as in line 3. In the while loop, when I iterate using current. When i assign current in line 13, I want this change to this object also cause changes to user_ptr_add, what should i do", +63c6481e5f004bf4898cf99b,2023-02-14T07:02:46.296Z,General Question,v1,"Program received signal SIGSEGV, Segmentation fault. +__GI_localtime (t=0x63eb26af) at ./time/localtime.c:54 + +i get this error, how can i solve it",,, +63c6481e5f004bf4898cf99b,2023-02-14T07:06:02.664Z,Question from Code,v1,"I have this struct post, when I allocate memory as in line 8, does that means I've also allocated memory to new_post->contents and date?","typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + + Post *new_post = malloc(sizeof(Post));",, +63c6481e5f004bf4898cf99b,2023-02-14T07:07:05.485Z,Question from Code,v1,"I have this struct user, when I allocate memory as in line 8, does that means I've also allocated memory to user->first_post and etc?","typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + + User *user = malloc(sizeof(User));",, +63c6481e5f004bf4898cf99b,2023-02-14T07:07:48.827Z,Question from Code,v1,how can i properly allocate memory to user object and all of its attributes?,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + + User *user = malloc(sizeof(User));",, +63c6481e5f004bf4898cf99b,2023-02-14T07:23:16.568Z,Help Fix Code,v1,,"void list_posts(const Post *curr) { + // cast a (const Post *) to a (Post *) + Post *current = (Post *) curr; + + while (current != NULL){ + // print to standard output + printf(""From: %s\n"", current->author); + printf(""Date: %s\n"", ctime(current->date)); + printf(""%s\n"", current->contents); + + if (current->next != NULL) + { + printf(""\n""); + printf(""===\n""); + printf(""\n""); + } + + current = current->next; + } + +}","this code is causing segmentation fault with ./time/localtime.c: No such file or directory. +how can i fix it?", +63c6481e5f004bf4898cf99b,2023-02-14T07:24:03.685Z,Help Fix Code,v1,,"typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +void list_posts(const Post *curr) { + // cast a (const Post *) to a (Post *) + Post *current = (Post *) curr; + + while (current != NULL){ + // print to standard output + printf(""From: %s\n"", current->author); + printf(""Date: %s\n"", ctime(current->date)); + printf(""%s\n"", current->contents); + + if (current->next != NULL) + { + printf(""\n""); + printf(""===\n""); + printf(""\n""); + } + + current = current->next; + } + +}", this code is causing segmentation fault with ./time/localtime.c: No such file or directory. how can i fix it?, +63c6481e5f004bf4898cf99b,2023-02-14T21:46:30.473Z,Help Fix Code,v1,,"typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + + + + +int delete_user(const char *name, User **user_ptr_del) { + // the first user in this list + User *current_user = *user_ptr_del; + User *prev = NULL; + User *found_user = NULL; + + while (current_user != NULL) + { + if (strcmp(name, current_user->name) == 0) + { + found_user = current_user; + break; + } + prev = current_user; + current_user = current_user->next; + } + + // return 1 of a user with this name does not exist + if (found_user == NULL) + { + return 1; + }else{ + // make the next of prev be the next of found_user + prev->next = found_user->next; + } + + // free user's posts list + // free_users_post_list(found_user->first_post); + + // free each friend + for (int i = 0; i < MAX_FRIENDS; i++) { + if (found_user->friends[i] == NULL) { + break; + } + // remove the deleted user from friend list of this friend + for (int j = 0; j < MAX_FRIENDS; j++) { + // free post from this user in friends' post lists + free_post_from_author(name, found_user->friends[i]); + if (strcmp((char *)(found_user->friends)[i]->friends[j]->name, name) == 0) + { + found_user->friends[i]->friends[j - 1]->next = found_user->friends[i]->friends[j + 1]; + } + } + } + free(found_user->friends); + + // free the user + free(found_user); + + // return 0 on success + return 0; +}",in the function delete_user strcmp in line 57 is causing segmentation fault, +63c6481e5f004bf4898cf99b,2023-02-17T00:09:07.074Z,Help Fix Code,v1,,"char *copy(char *dest, const char *src, int capacity) { + int i; + for (i = 0; i < capacity && src[i] != '\0'; i++){ + dest[i] = src[i]; + } + + for (int j = i; j < capacity; j++) + { + dest[i] = '\0'; + } + // explicitly make dest null-terminated when there is no null terminator in + // the first 'capacity' chars in src. + dest[sizeof(dest) - 1] = '\0'; + + return dest; +}","/* Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset. + */", +63c6481e5f004bf4898cf99b,2023-03-13T06:21:02.635Z,Help Fix Code,v2,"in line 11, buffer_line should be assigend to ""main : linked_list.o main.o"". Suppose we the ngoes into line 18. in line 28, the buffer_line in parse_target_dependency should be ""main : linked_list.o main.o"", but it turns out to be ""main"" which is wrong","/* 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) { + // Implement this function and remove the stubbed return statement below. + char buffer_line[MAXLINE]; + Rule* first_rule_ptr = NULL; + Rule *current_rule = NULL; + + while (fgets(buffer_line, MAXLINE, fp) != NULL){ + // buffer_line[MAXLINE - 1] = '\0'; + // If the line is a comment line + if (is_comment_or_empty(buffer_line)){ + continue; + } + // If the line is a target line + else if (buffer_line[0] != '\t'){ + char buffer_copy[MAXLINE]; + strcpy(buffer_copy, buffer_line); + + // Parse target and dependencies + if (first_rule_ptr == NULL){ + char *token = strtok(buffer_line, "" ""); + first_rule_ptr = init_rule(token); + current_rule = first_rule_ptr; + } + Rule *this_rule = parse_target_dependency(buffer_copy, first_rule_ptr); + + Action *first_action; + Action *current_action = NULL; + // Parse Actions + while (fgets(buffer_line, MAXLINE, fp) != NULL && buffer_line[0] == '\t') + { + Action *new_action = parse_action_line(buffer_line); + // If the linked list of action nodes is empty + if (current_action == NULL) + { + first_action = new_action; + current_action = new_action; + }else{ + current_action->next_act = new_action; + current_action = current_action->next_act; + } + } + this_rule->actions = first_action; + current_rule->next_rule = this_rule; + current_rule = current_rule->next_rule; + } + } + + return first_rule_ptr; +}",, +63c6481e5f004bf4898cf99b,2023-03-13T06:57:44.878Z,Help Fix Code,v2,"given that MAXLINE = 256. fgets on line 7 is reading too much than expected. for a line ""main : linked_list.o main.o\n"" it should only read ""main : linked_list.o main.o"" but its giving garbage after that","Rule *parse_file(FILE *fp) { + // Implement this function and remove the stubbed return statement below. + char buffer_line[MAXLINE]; + Rule* first_rule_ptr = NULL; + Rule *current_rule = NULL; + //fgets(buffer_line, MAXLINE, fp) + while (fgets(buffer_line, MAXLINE, fp)){ + .....",, +63c6481e5f004bf4898cf99b,2023-03-13T20:06:02.518Z,Help Fix Code,v2,"the line parameter given will be a string start with a tab. I want to store every word (splited by space) except the tab to a char ** +","Action *parse_action_line(char line[MAXLINE]){ + int max_strings = MAXLINE/2; + char **args_array = malloc(max_strings * sizeof(char *)); + char *delim = ""\t""; + + // Gives the string before ""\t"" which is empty. + char *token = strtok(line, delim); + // Gives the string before "" "" and after ""\t"" + token = strtok(NULL, "" ""); + int i = 0; + + while (token != NULL) + { + args_array[i] = strdup(token); + token = strtok(NULL, "" ""); + i++; + } + // Last element in Action->args is NULL. + args_array[i] = NULL; + + Action *new_action = malloc(sizeof(Action)); + new_action->args = args_array; + new_action->next_act = NULL; + + return new_action; +}",, +63c6481e5f004bf4898cf9a0,2023-02-06T19:09:11.620Z,General Question,v1,how to check malloc fail,,, +63c6481e5f004bf4898cf9a0,2023-02-06T19:15:57.910Z,General Question,v1,how to check two strings are equal,,, +63c6481e5f004bf4898cf9a0,2023-02-06T19:18:27.791Z,General Question,v1,default value of declared variable,,, +63c6481e5f004bf4898cf9a0,2023-02-06T19:20:01.377Z,General Question,v1,how to check for empty array,,, +63c6481e5f004bf4898cf9a0,2023-02-06T19:24:49.002Z,General Question,v1,how to check if reach end of array,,, +63c6481e5f004bf4898cf9a0,2023-02-06T19:31:08.927Z,General Question,v1,explain how linked list is implemented in c,,, +63c6481e5f004bf4898cf9a0,2023-02-08T22:26:02.566Z,General Question,v1,is const char always a proper string,,, +63c6481e5f004bf4898cf9a0,2023-02-08T22:26:32.939Z,General Question,v1,is const char* always a proper string,,, +63c6481e5f004bf4898cf9a0,2023-02-08T22:27:23.213Z,General Question,v1,can I only use strlen on proper string,,, +63c6481e5f004bf4898cf9a0,2023-02-08T22:45:05.554Z,General Question,v1,how to check two strings are equal,,, +63c6481e5f004bf4898cf9a0,2023-02-09T17:48:45.170Z,General Question,v1,tell me about struct pixel,,, +63c6481e5f004bf4898cf9a0,2023-02-09T17:55:59.896Z,General Question,v1,is attribute of struct a pointer,,, +63c6481e5f004bf4898cf9a0,2023-02-09T18:30:09.515Z,General Question,v1,how to fix malloc(): corrupted top size,,, +63c6481e5f004bf4898cf9a0,2023-02-09T18:31:26.911Z,General Question,v1,"sample code to implement 2D array with malloc +",,, +63c6481e5f004bf4898cf9a0,2023-02-09T18:33:12.401Z,Question from Code,v1,does the code properly implement a 2D array for struct pixel,"struct pixel **pixel_array = malloc(height*sizeof(struct pixel *)); + if (pixel_array == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int i = 0; i < height; i++){ + pixel_array[i] = malloc(width*sizeof(struct pixel)); + if (pixel_array[i] == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + }",, +63c6481e5f004bf4898cf9a0,2023-02-09T18:34:57.788Z,Question from Code,v1,does the code properly implement a 2D array for struct pixel and malloc space ,"struct pixel *pixel_array = malloc(height*sizeof(struct pixel *)); +if (pixel_array == NULL){ + perror(""Unable to malloc space""); + exit(1); +} +for(int i = 0; i < height; i++){ + pixel_array[i] = malloc(width*sizeof(struct pixel)); + if (pixel_array[i] == NULL){ + perror(""Unable to malloc space""); + exit(1); + } +}",, +63c6481e5f004bf4898cf9a0,2023-02-10T06:02:34.286Z,General Question,v1,how to make 2d array of string using malloc,,, +63c6481e5f004bf4898cf9a0,2023-02-10T06:08:34.772Z,Help Fix Code,v1,,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **pixel_array = malloc(height*sizeof(struct pixel *)); + if (pixel_array == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int i = 0; i < height; i++){ + pixel_array[i] = malloc(width*sizeof(struct pixel)); + if (pixel_array[i] == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int j = 0; j < width; j++){ + if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){ + perror(""Unable to navigate in image file""); + exit(1); + } + if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + } + } + return pixel_array; +}",malloc space for 2d array of struct pixel, +63c6481e5f004bf4898cf9a0,2023-02-10T06:14:01.003Z,Question from Code,v1,how should I allocate space for this code,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **pixel_array = malloc(height*sizeof(struct pixel *)); + if (pixel_array == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int i = 0; i < height; i++){ + pixel_array[i] = malloc(width*sizeof(struct pixel)); + if (pixel_array[i] == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int j = 0; j < width; j++){ + if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){ + perror(""Unable to navigate in image file""); + exit(1); + } + if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + } + } + return pixel_array; +}",, +63c6481e5f004bf4898cf9a0,2023-02-10T06:18:16.098Z,Help Fix Code,v1,,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **pixel_array = (struct pixel **) malloc(height*sizeof(struct pixel *)); + if (pixel_array == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int i = 0; i < height; i++){ + pixel_array[i] = (struct pixel *) malloc(width*sizeof(struct pixel)); + if (pixel_array[i] == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int j = 0; j < width; j++){ + if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){ + perror(""Unable to navigate in image file""); + exit(1); + } + if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + } + } + return pixel_array; +}",malloc space for 2d array of struct pixel, +63c6481e5f004bf4898cf9a0,2023-02-10T06:20:51.058Z,Explain Code,v1,,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **pixel_array = (struct pixel **) malloc(height*sizeof(struct pixel *)); + if (pixel_array == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int i = 0; i < height; i++){ + pixel_array[i] = (struct pixel *) malloc(width*sizeof(struct pixel)); + if (pixel_array[i] == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int j = 0; j < width; j++){ + if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){ + perror(""Unable to navigate in image file""); + exit(1); + } + if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + } + } + return pixel_array; +}",, +63c6481e5f004bf4898cf9a0,2023-02-10T06:32:13.034Z,Question from Code,v1,how to fix malloc(): corrupted top size in this code,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **pixel_array = (struct pixel **) malloc(height*sizeof(struct pixel *)); + if (pixel_array == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int i = 0; i < height; i++){ + pixel_array[i] = (struct pixel *) malloc(width*sizeof(struct pixel)); + if (pixel_array[i] == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int j = 0; j < width; j++){ + if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){ + perror(""Unable to navigate in image file""); + exit(1); + } + if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + } + } + return pixel_array; +}",, +63c6481e5f004bf4898cf9a0,2023-02-10T06:43:53.237Z,Question from Code,v1,how to fix malloc(): corrupted top size in this code,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **pixel_array = (struct pixel **) malloc(height*sizeof(struct pixel *)); + if (pixel_array == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int i = 0; i < height; i++){ + pixel_array[i] = (struct pixel *) malloc(width*3); + if (pixel_array[i] == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int j = 0; j < width; j++){ + if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){ + perror(""Unable to navigate in image file""); + exit(1); + } + if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + } + } + return pixel_array; +}",, +63c6481e5f004bf4898cf9a0,2023-02-10T06:45:03.705Z,Question from Code,v1,how to fix malloc(): corrupted top size in this code,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **pixel_array = malloc(height*sizeof(struct pixel *)); + if (pixel_array == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int i = 0; i < height; i++){ + pixel_array[i] = malloc(width*3); + if (pixel_array[i] == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int j = 0; j < width; j++){ + if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){ + perror(""Unable to navigate in image file""); + exit(1); + } + if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + } + } + return pixel_array; +}",, +63c6481e5f004bf4898cf9a0,2023-02-10T06:49:51.704Z,General Question,v1,write example code of 2d array of struct pixel,,, +63c6481e5f004bf4898cf9a0,2023-02-10T06:55:54.106Z,Question from Code,v1,does the code properly malloc space for 2d array of struct pixel,"struct pixel *pixel_array = malloc(height*width*3); + if (pixel_array == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int i = 0; i < height; i++){ + for(int j = 0; j < width; j++){ + if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){ + perror(""Unable to navigate in image file""); + exit(1); + } + if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + } + } + return pixel_array;",, +63c6481e5f004bf4898cf9a0,2023-02-10T07:13:05.666Z,Question from Code,v1,is there memory leak,"#include +#include + +struct pixel { + unsigned char blue; + unsigned char green; + unsigned char red; +}; + +void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height); +struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height); +void print_pixel(struct pixel p); + +struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **pixel_array = malloc(height*sizeof(struct pixel *)); + if (pixel_array == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int i = 0; i < height; i++){ + pixel_array[i] = malloc(width*3); + if (pixel_array[i] == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int j = 0; j < width; j++){ + if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){ + perror(""Unable to navigate in image file""); + exit(1); + } + if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + } + } + return pixel_array; +} + +void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { + if (sizeof(int) != 4){ + perror(""Size of integer in this machine does not match file detail requirement""); + exit(1); + } + if (fseek(image, 10, SEEK_SET) != 0){ + perror(""Unable to navigate in image file""); + exit(1); + } + if (fread(pixel_array_offset, sizeof(int), 1, image) != 1){ + perror(""Error when reading array starting point data""); + exit(1); + } + if (fseek(image, 18, SEEK_SET) != 0){ + perror(""Unable to navigate in image file""); + exit(1); + } + if (fread(width, sizeof(int), 1, image) != 1){ + perror(""Error when reading width data""); + exit(1); + } + if (fseek(image, 22, SEEK_SET) != 0){ + perror(""Unable to navigate in image file""); + exit(1); + } + if (fread(height, sizeof(int), 1, image) != 1){ + perror(""Error when reading height data""); + exit(1); + } +} + +void print_pixel(struct pixel p) { + printf(""(%u, %u, %u)\n"", p.blue, p.green, p.red); +} + +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; + read_bitmap_metadata(image, &pixel_array_offset, &width, &height); + + // Print out metadata. + printf(""Pixel array offset: %d\n"", pixel_array_offset); + printf(""Width: %d\n"", width); + printf(""Height: %d\n"", height); + + // Read in the pixel data + struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height); + + // Print out some pixels from each of the image's corners. + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 5; j++) { + print_pixel(pixels[i][j]); + print_pixel(pixels[i][width - 1 - j]); + print_pixel(pixels[height - 1 - i][j]); + print_pixel(pixels[height - 1 - i][width - 1 - j]); + } + } + + // Clean up: you need to do this! + if (fclose(image) != 0){ + perror(""Unable to close image""); + return 1; + } + for (int i = 0; i < width; i++){ + free(pixels[i]); + } + free(pixels); + return 0; +} +",, +63c6481e5f004bf4898cf9a0,2023-02-10T07:40:30.733Z,Question from Code,v1,is there memory leak," +struct pixel { + unsigned char blue; + unsigned char green; + unsigned char red; +} +struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height); +struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **pixel_array = malloc(height*sizeof(struct pixel *)); + if (pixel_array == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int i = 0; i < height; i++){ + pixel_array[i] = malloc(width*3); + if (pixel_array[i] == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int j = 0; j < width; j++){ + if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){ + perror(""Unable to navigate in image file""); + exit(1); + } + if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + } + } + return pixel_array; +} +int main{ + FILE *image = fopen(argv[1], ""rb""); + int pixel_array_offset = 54; + int width = 200; + int height = 200; + struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height); + for (int i = 0; i < width; i++){ + free(pixels[i]); + } + free(pixels); +}",, +63c6481e5f004bf4898cf9a0,2023-02-10T07:43:21.062Z,Help Fix Code,v1,," +struct pixel { + unsigned char blue; + unsigned char green; + unsigned char red; +} +struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height); +struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **pixel_array = malloc(height*sizeof(struct pixel *)); + if (pixel_array == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int i = 0; i < height; i++){ + pixel_array[i] = malloc(width*3); + if (pixel_array[i] == NULL){ + perror(""Unable to malloc space""); + exit(1); + } + for(int j = 0; j < width; j++){ + if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){ + perror(""Unable to navigate in image file""); + exit(1); + } + if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){ + perror(""Error when reading pixel data""); + exit(1); + } + } + } + return pixel_array; +} +int main{ + FILE *image = fopen(argv[1], ""rb""); + int pixel_array_offset = 54; + int width = 200; + int height = 200; + struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height); + for (int i = 0; i < width; i++){ + free(pixels[i]); + } + free(pixels); +}",free memory allocated to pixels, +63c6481e5f004bf4898cf9a0,2023-02-12T01:08:44.954Z,General Question,v1,how to force printf to print to standard out,,, +63c6481e5f004bf4898cf9a0,2023-02-12T01:09:20.871Z,Question from Code,v1,does the code print to standard output,"printf(stdout, ""hi"");",, +63c6481e5f004bf4898cf9a0,2023-02-12T01:09:46.035Z,Question from Code,v1,does the code print to standard output,"printf(""stdout"", ""hi"");",, +63c6481e5f004bf4898cf9a0,2023-02-12T06:36:38.480Z,General Question,v1,write example code for fopen then fclose,,, +63c6481e5f004bf4898cf9a0,2023-02-12T20:24:47.968Z,Question from Code,v1,is k an array of int pointers or array of int,int *k[10];,, +63c6481e5f004bf4898cf9a0,2023-02-14T03:40:16.049Z,General Question,v1,how to use fgets,,, +63c6481e5f004bf4898cf9a0,2023-02-14T04:55:01.308Z,General Question,v1,how to use time function,,, +63c6481e5f004bf4898cf9a0,2023-02-14T07:55:30.056Z,General Question,v1,when does segmentation fault occur,,, +63c6481e5f004bf4898cf9a0,2023-02-14T08:00:02.040Z,Question from Code,v1,identify segmentation fault,"int make_friends(const char *name1, const char *name2, User *head) { + User *user1; + User *user2; + User *curr = head; + while((curr != NULL) && ((user1 == NULL) || (user2 == NULL))){ + if(strcmp(curr->name, name1) == 0){ + user1 = curr; + } + if(strcmp(head->name, name2) == 0){ + user2 = curr; + } + curr = curr->next; + } + if ((user1 == NULL) || (user2 == NULL)) + { + return 4; + } + if (strcmp(name1, name2) == 0){ + return 3; + } + int index1 = 0; + int index2 = 0; + while((index1 < MAX_FRIENDS) && (user1->friends[index1] != NULL)){ + if (user1->friends[index1] == user2) + { + return 1; + } + index1 = index1 + 1; + } + while((index2 < MAX_FRIENDS) && (user2->friends[index2] != NULL)){ + if (user2->friends[index2] == user1) + { + return 1; + } + index2 = index2 + 1; + } + if ((index1 == MAX_FRIENDS) || (index2 == MAX_FRIENDS)) + { + return 2; + } + else{ + user1->friends[index1] = user2; + user2->friends[index2] = user1; + return 0; + } + + return -1; +}",, +63c6481e5f004bf4898cf9a0,2023-02-14T20:32:19.483Z,Question from Code,v1,what is uninitialized,"int delete(User *delete_target){ + // remove from other friends + int i; + int j; + i = 0; + while((i < MAX_FRIENDS) && (delete_target->friends[i] != NULL)){ + User *friend = delete_target->friends[i]; + j = 0; + while ((j < MAX_FRIENDS) && (strcmp(friend->friends[j]->name, delete_target->name) != 0)) + { + j = j + 1; + } + if (j == MAX_FRIENDS) + { + perror(""mutrual friendship status not maintained""); + return 1; + } + + for (int k = j + 1 ; k < MAX_FRIENDS; k++) + { + friend->friends[k-1] = friend->friends[k]; + } + friend->friends[MAX_FRIENDS-1] = NULL; + i = i + 1; + } + // free posts user received + Post *curr_post = delete_target->first_post; + Post *curr_pt; + while(curr_post != NULL){ + curr_pt = curr_post; + curr_post = curr_post->next; + free(curr_pt->contents); + free(curr_pt->date); + free(curr_pt); + } + // free user + free(delete_target); + return 0; +}",, +63c6481e5f004bf4898cf9a0,2023-02-14T20:33:20.221Z,Question from Code,v1,what variables are uninitialized before use,"int delete(User *delete_target){ + // remove from other friends + int i; + int j; + i = 0; + while((i < MAX_FRIENDS) && (delete_target->friends[i] != NULL)){ + User *friend = delete_target->friends[i]; + j = 0; + while ((j < MAX_FRIENDS) && (strcmp(friend->friends[j]->name, delete_target->name) != 0)) + { + j = j + 1; + } + if (j == MAX_FRIENDS) + { + perror(""mutrual friendship status not maintained""); + return 1; + } + + for (int k = j + 1 ; k < MAX_FRIENDS; k++) + { + friend->friends[k-1] = friend->friends[k]; + } + friend->friends[MAX_FRIENDS-1] = NULL; + i = i + 1; + } + // free posts user received + Post *curr_post = delete_target->first_post; + Post *curr_pt; + while(curr_post != NULL){ + curr_pt = curr_post; + curr_post = curr_post->next; + free(curr_pt->contents); + free(curr_pt->date); + free(curr_pt); + } + // free user + free(delete_target); + return 0; +}",, +63c6481e5f004bf4898cf9a0,2023-03-08T06:25:59.912Z,General Question,v1,how to read a line from a file,,, +63c6481e5f004bf4898cf9a0,2023-03-08T07:40:55.325Z,General Question,v1,write me sample code for strtok,,, +63c6481e5f004bf4898cf9a0,2023-03-08T08:32:26.742Z,General Question,v1,does malloc on struct containing pointers set pointer to null by default,,, +63c6481e5f004bf4898cf9a0,2023-03-09T03:39:05.430Z,Question from Code,v1,is there segmentation fault,"#include +#include +#include + +#include ""pmake.h"" + +/****************************************************************************** + * Newly defined helper functions + *****************************************************************************/ + +/* Makes a new Rule struct + */ +Rule *make_rule(Rule **head, char *line); + +/* Link dependency and make new rules if nessecary + * note: assume no circular dependency + */ +Dependency *link_dependency(Rule **head, char *token); + +/* Add action to rule + */ +void make_action(Rule *rule, char *line); + +Rule *make_rule(Rule **head, char *line) +{ + const char delim[3] = ""\t ""; + char *token; + token = strtok(line, delim); + int len = strlen(token); + + // check if target is in a rule already (due to dependency making the rule beforehand) + Rule *curr; + if (*head == NULL) + { + *head = calloc(1, sizeof(Rule)); + curr = *head; + } + else + { + curr = *head; + while (curr->next_rule != NULL) + { + if (strcmp(curr->target, token) == 0) + { + break; + } + curr = curr->next_rule; + } + if (strcmp(curr->target, token) != 0) + { + curr->next_rule = calloc(1, sizeof(Rule)); + curr = curr->next_rule; + } + } + + // assumed correct format, first two tokens are target and ':' + curr->target = malloc(sizeof(char) * (len + 1)); + strncpy(curr->target, token, len); + curr->target[len] = '\0'; + token = strtok(NULL, delim); + if (strcmp(token, "":"") != 0) + { + fprintf(stderr, ""assumption not fulfilled""); + exit(1); + } + Dependency *depend_curr = curr->dependencies; + token = strtok(NULL, delim); + while (token != NULL) + { + if (curr->dependencies == NULL) + { + depend_curr = link_dependency(head, token); + curr->dependencies = depend_curr; + } + else + { + while (depend_curr->next_dep != NULL) + { + depend_curr = depend_curr->next_dep; + } + + depend_curr->next_dep = link_dependency(head, token); + depend_curr = depend_curr->next_dep; + } + } + + return curr; +} + +void make_action(Rule *rule, char *line) +{ + Action *new_action = calloc(1, sizeof(Action)); + const char delim[3] = ""\t ""; + int count = 0; + char *token; + char line_count[MAXLINE]; + strncpy(line_count, line, MAXLINE); + token = strtok(line_count, delim); + + while (token != NULL) + { + count++; + token = strtok(NULL, delim); + } + if (count != 0) + { + new_action->args = malloc(sizeof(char *) * (count + 1)); + token = strtok(line, delim); + int len = strlen(token); + new_action->args[0] = malloc(sizeof(char) * (len + 1)); + strncpy(new_action->args[0], token, len); + (new_action->args[0])[len] = '\0'; + for (int i = 1; i < count - 1; i++) + { + token = strtok(line, delim); + len = strlen(token); + new_action->args[i] = malloc(sizeof(char) * (len + 1)); + strncpy(new_action->args[i], token, len); + (new_action->args[i])[len] = '\0'; + } + new_action->args[count] = NULL; + } + + if (rule->actions == NULL) + { + rule->actions = new_action; + } + else + { + Action *curr = rule->actions; + while (curr->next_act != NULL) + { + curr = curr->next_act; + } + curr->next_act = new_action; + } +} + +Dependency *link_dependency(Rule **head, char *token) +{ + Rule *curr = *head; + Dependency *depend = calloc(1, sizeof(Dependency)); + // link_dependency called implies at least one rule is stored/linked in head + while (curr->next_rule != NULL) + { + if (strcmp(curr->target, token) == 0) + { + break; + } + curr = curr->next_rule; + } + if (strcmp(curr->target, token) != 0) + { + char sim_line[MAXLINE]; + // assuming correct format, each target has max length MAXLINE-4 + strncat(sim_line, token, (MAXLINE-4)); + strcat(sim_line, "" : ""); + sim_line[MAXLINE - 1] = '\0'; + // assume every entry is unique + curr = make_rule(head, sim_line); + } + depend->rule = curr; + return depend; +} + +/* 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; + Rule *curr = NULL; + + while (fgets(line, MAXLINE, fp) != NULL) + { + if (is_comment_or_empty(line)) + { + continue; + } + // assumed at least one rule exists and is at beginnint, + // so the if part will at least execute once before the else part meaning curr is well defined + if (line[0] != '\t') + { + curr = make_rule(&head, line); + } + else{ + make_action(curr, line); + } + } + return head; +} + +/****************************************************************************** + * These helper functions are provided for you. Do not modify them. + *****************************************************************************/ +/* Print the list of actions */ +void print_actions(Action *act) +{ + while (act != NULL) + { + if (act->args == NULL) + { + fprintf(stderr, ""ERROR: action with NULL args\n""); + act = act->next_act; + continue; + } + printf(""\t""); + + int i = 0; + while (act->args[i] != NULL) + { + printf(""%s "", act->args[i]); + i++; + } + printf(""\n""); + act = act->next_act; + } +} + +/* Print the list of rules to stdout in makefile format. If the output + of print_rules is saved to a file, it should be possible to use it to + run make correctly. + */ +void print_rules(Rule *rules) +{ + Rule *cur = rules; + + while (cur != NULL) + { + if (cur->dependencies || cur->actions) + { + // Print target + printf(""%s : "", cur->target); + + // Print dependencies + Dependency *dep = cur->dependencies; + while (dep != NULL) + { + if (dep->rule->target == NULL) + { + fprintf(stderr, ""ERROR: dependency with NULL rule\n""); + } + printf(""%s "", dep->rule->target); + dep = dep->next_dep; + } + printf(""\n""); + + // Print actions + print_actions(cur->actions); + } + cur = cur->next_rule; + } +} + +/* Return 1 if the line is a comment line, as defined on the assignment handout. + Return 0 otherwise. + */ +int is_comment_or_empty(const char *line) +{ + for (int i = 0; i < strlen(line); i++) + { + if (line[i] == '#') + { + return 1; + } + if (line[i] != '\t' && line[i] != ' ') + { + return 0; + } + } + return 1; +} + +/* Convert an array of args to a single space-separated string in buffer. + Returns buffer. Note that memory for args and buffer should be allocted + by the caller. + */ +char *args_to_string(char **args, char *buffer, int size) +{ + buffer[0] = '\0'; + int i = 0; + while (args[i] != NULL) + { + strncat(buffer, args[i], size - strlen(buffer)); + strncat(buffer, "" "", size - strlen(buffer)); + i++; + } + return buffer; +} +",, +63c648205f004bf4898cf9aa,2023-01-28T15:43:41.854Z,Help Fix Code,v1,,"#include +#include +/* + * Convert a 9 digit int to a 9 element int array. + */ +int populate_array(int sin, int *sin_array) { + if ((sin / 1000000000 == 0) | (sin / 1000000000 >= 10)){ + return 1; //sin num is not 9 digit + } + else{ + sin_array = malloc(sizeof(int) * 9); + for (int i = 1; i < 10; i++){ + sin_array[i - 1] = sin % (10^i); + sin = sin / 10; + } + return 0; + } +}",Convert a 9 digit int to a 9 element int array., +63c648225f004bf4898cf9be,2023-01-19T04:29:52.445Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + // Read in the command-line arguments and convert the strings to doubles + double principal = strtod(argv[1], NULL); + double rate = strtod(argv[2], NULL); + + // Call invest to make you more money + invest(&principal, rate); + + printf(""%.2f\n"", principal); + return 0; +} + +void invest(double *principal, double rate){ + *principal = *principal + rate; +}",, +63c648225f004bf4898cf9be,2023-01-19T04:31:09.268Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + // Read in the command-line arguments and convert the strings to doubles + double principal = strtod(argv[1], NULL); + double rate = strtod(argv[2], NULL); + + // Call invest to make you more money + invest(&principal, rate); + + printf(""%.2f\n"", principal); + return 0; +} + +void invest(double *principal, double rate){ + *principal = *principal * rate; +}",, +63c648225f004bf4898cf9be,2023-01-23T21:00:42.031Z,Explain Code,v1,," char* min; + scanf(""%s"", min); + int min_size = strtol(min, NULL, 10); + printf(""%d"", min_size);",, +63c648225f004bf4898cf9be,2023-01-23T22:57:41.334Z,Explain Code,v1,,"#include +int main() { + char string[10] = ""hello""; + printf(""%s"", &string[1]); + return 0; +}",, +63c648225f004bf4898cf9be,2023-01-26T15:56:47.764Z,Explain Code,v1,," int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6}; + + + // sum the result + int sum = 0; + + for (int i = 0; i < 9; i++){ + sum += sin_array[i]; + } + + if (sum % 10 != 0){ + return 1; + } + + return 0;",, +63c648225f004bf4898cf9be,2023-01-26T15:57:56.833Z,Help Fix Code,v1,," int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6}; + + + // sum the result + int sum = 0; + + for (int i = 0; i < 9; i++){ + sum += sin_array[i]; + } + + if (sum % 10 != 0){ + return 1; + } + + return 0;",sum the integers in the sum_array array, +63c648225f004bf4898cf9be,2023-01-26T15:59:33.967Z,Help Fix Code,v1,," int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6}; + + + // sum the result + int sum = 0; + + for (int i = 0; i < 9; i++){ + sum += sin_array[i]; + } + + if (sum % 10 != 0){ + return 1; + } + + return 0;",sum the integers in the sin_array array, +63c648225f004bf4898cf9be,2023-01-26T16:12:07.704Z,Explain Code,v1,,"#include +int main() { + int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6}; + + if (sin_array[0] == 0){ + return 1; + } + + // make the result array + int result[9]; + + for (int i = 0; i < 9; i++){ + + if (i % 2 == 0){ + result[i] = sin_array[i]; + } + + else { + + int product = sin_array[i] * 2; + + // sum the digits if it is a two-digit number + if (product > 9){ + + result[i] = 1 + (product % 10); + } + + else { + + result[i] = product; + } + } + } + + printf(""%d"", result[0]); + printf(""%d"", result[1]); + printf(""%d"", result[2]); + printf(""%d"", result[3]); + printf(""%d"", result[4]); + printf(""%d"", result[5]); + printf(""%d"", result[6]); + printf(""%d"", result[7]); + printf(""%d"", result[8]); + + // sum the result + int sum; + + for (int i = 0; i < 9; i++){ + sum += result[i]; + } + + if (sum % 10 != 0){ + return 1; + } + + return 0; + + +}",, +63c648225f004bf4898cf9be,2023-01-26T16:18:34.795Z,Help Fix Code,v1,,"#include +int main() { + int sin_array[9] = {0, 0, 0, 6, 2, 0, 7, 1, 6}; + + if (sin_array[0] == 0){ + return 1; + } + + + // sum the result + + + +}",return 1 if the first element in the array is 0, +63c648225f004bf4898cf9be,2023-01-26T16:20:05.188Z,Help Fix Code,v1,,"#include +int main() { + int sin_array[9] = {0, 0, 0, 6, 2, 0, 7, 1, 6}; + + if (sin_array[0] == 0){ + return 1; + } + + + // sum the result + return 0; + + +}",if the first element in sin_array is 0 return 1, +63c648225f004bf4898cf9be,2023-01-26T16:22:33.737Z,Help Fix Code,v1,,"#include +int main() { + int sin_array[9] = {0, 0, 0, 6, 2, 0, 7, 1, 6}; + + if (sin_array[0] == 0){ + return 1; + } + + return 0; + + +}",if the first element in sin_array is 0 return 1, +63c648225f004bf4898cf9be,2023-01-26T16:23:45.349Z,Help Fix Code,v1,,"#include +int main() { + int sin_array[9] = {0, 0, 0, 6, 2, 0, 7, 1, 6}; + + if (sin_array[0] == 0){ + printf(""hello""); + } + + return 0; + + +}","if the first element in sin_array is 0 print ""hello""", +63c648225f004bf4898cf9be,2023-01-26T16:33:59.685Z,Explain Code,v1,,"#include +int main() { + int sin_array[9] = {0, 0, 0, 6, 2, 0, 7, 1, 6}; + + if (sin_array[0] == 0){ + return 1; + } + + // make the result array + int result[9]; + + for (int i = 0; i < 9; i++){ + + if (i % 2 == 0){ + result[i] = sin_array[i]; + } + + else { + + int product = sin_array[i] * 2; + + // sum the digits if it is a two-digit number + if (product > 9){ + + result[i] = 1 + (product % 10); + } + + else { + + result[i] = product; + } + } + } + + // sum the result + + int sum = 0; + + for (int i = 0; i < 9; i++){ + sum += result[i]; + } + + if (sum % 10 != 0){ + return 1; + } + + return 0; + +} +",, +63c648225f004bf4898cf9be,2023-01-26T16:35:13.906Z,Help Fix Code,v1,,"#include +int main() { + int sin_array[9] = {0, 0, 0, 6, 2, 0, 7, 1, 6}; + + if (sin_array[0] == 0){ + return 1; + } + + // make the result array + int result[9]; + + for (int i = 0; i < 9; i++){ + + if (i % 2 == 0){ + result[i] = sin_array[i]; + } + + else { + + int product = sin_array[i] * 2; + + // sum the digits if it is a two-digit number + if (product > 9){ + + result[i] = 1 + (product % 10); + } + + else { + + result[i] = product; + } + } + } + + // sum the result + + int sum = 0; + + for (int i = 0; i < 9; i++){ + sum += result[i]; + } + + if (sum % 10 != 0){ + return 1; + } + + return 0; + +} +","the code above checks if a social insurance number is valid. it does this by first checking if the first digit is 0. if it is, the function returns 1, indicating that the SIN is invalid. if the first digit is not 0, the function proceeds to multiply every other digit by 2 and sum the digits of the resulting number if it is a two-digit number. the function then sums the result array and checks if the sum is divisible by 10. if it is, the function returns 0, indicating that the SIN is valid. if it is not, the function returns 1, indicating that the SIN is invalid.", +63c648225f004bf4898cf9be,2023-01-26T18:10:07.524Z,Help Fix Code,v1,,"#include +int main() { + int arr[3] = {0, 0, 0}; + + if (arr[0] == 0){ + return 1; + } + return 0; +} +","return 1 if integer at arr[0] is 0, return 1 otherwise", +63c648225f004bf4898cf9be,2023-01-26T18:26:37.624Z,Help Fix Code,v1,,"#include +int main() { + int sin_array[9]; + sin_array = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + + return 0; +} +",make an array with 9 elements. return 0, +63c648225f004bf4898cf9be,2023-01-26T18:27:21.241Z,Help Fix Code,v1,,"#include +int main() { + int sin_array[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + + return 0; +} +",initialize sin_array with 9 elements. return 0, +63c648225f004bf4898cf9be,2023-01-26T18:33:15.808Z,Help Fix Code,v1,,"#include +int main() { + + if (11 % 10 != 0){ + return 1; + } + + return 0; + +} +","return 1 if the remainder of 11 / 10 is not 0, return 0 otherwise", +63c648225f004bf4898cf9be,2023-01-27T03:13:12.474Z,Explain Code,v1,,"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; +}",, +63c648235f004bf4898cf9c3,2023-01-24T01:50:02.530Z,Question from Code,v1,Why do I get Abort trap: 6?,"#include +#include + +int check_permissions(char *file_perm, char *required_perm) { + // first is permissions of file, second is permissions required + for (int i = 0; i < 9; i++) { + // if the required perm char is -, don't care about the file perm char + if (required_perm[i] != '-' && file_perm[i] != required_perm[i]) { + return 1; + } + } + return 0; +} + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + int cutoff_size = strtol(argv[1], NULL, 10); + char file_perms[11]; + int file_size; + char file_name[32]; + + // scan and ignore the first line that says total: ... + scanf(""%*s %*d""); + int count = 0; + while (scanf(""%s %*d %*s %*s %d %*s %*s %*s %s"", file_perms, + &file_size, file_name) == 3) { + // note argv[2] points to the array of required perms + if (file_size > cutoff_size && + (argc == 2 || check_permissions(&file_perms[1], argv[2]) == 0)) { + count++; + } + } + printf(""%d\n"", count); + + return 0; +} +",, +63c648245f004bf4898cf9d2,2023-02-14T02:15:25.077Z,Question from Code,v1,What will be the output of the following program fragment?,"#define SUPERVISOR(regular) regular + 5 +int main() { + int regular_pay = 20; + int hours_worked = 10; + printf(""pay is %d\n"", (hours_worked * SUPERVISOR(regular_pay))); + // rest omitted +}",, +63c648245f004bf4898cf9d2,2023-02-14T16:50:49.744Z,General Question,v1,Find bugs and memory leaks in these functions,,, +63c648255f004bf4898cf9dc,2023-01-24T02:38:30.706Z,Explain Code,v1,,"void fib(int **sequence_pt, int n) { + sequence_pt = malloc(sizeof(int) * n); +}",, +63c648255f004bf4898cf9dc,2023-01-24T02:50:07.447Z,Question from Code,v1,line 9 causes segmentation error,"void fib(int **sequence_pt, int n) { + int num = 1; + int prev = 0; + int new_num; + + *sequence_pt = malloc(sizeof(int) * n); + *sequence_pt[0] = 0; // n >= 1 by assumption + for (int i = 1; i < n; i++) { + *sequence_pt[i] = num; + new_num = num + prev; + prev = num; + num = new_num; + } +}",, +63c648255f004bf4898cf9dc,2023-01-24T03:07:48.655Z,General Question,v1,why is argv of type char **,,, +63c648255f004bf4898cf9dc,2023-01-30T22:58:42.839Z,General Question,v1,variable-sized object may not be initialized,,, +63c648255f004bf4898cf9dc,2023-02-06T18:29:09.571Z,General Question,v1,what special cases do i need to ocnsider for linked list in C,,, +63c648275f004bf4898cf9eb,2023-01-27T02:38:48.734Z,General Question,v1,"how to use valgrind +",,, +63c648275f004bf4898cf9eb,2023-01-27T02:42:07.632Z,Question from Code,v1,"what does this code do +","for (int i = 0; i < 5; i++) { + printf(""%d\n"", i); +}",, +63c648275f004bf4898cf9eb,2023-01-27T02:59:34.133Z,General Question,v1,"what arguments to you pass strtol +",,, +63c648275f004bf4898cf9eb,2023-02-04T21:26:34.049Z,General Question,v1,"what does the function strtok() do +",,, +63c648275f004bf4898cf9eb,2023-02-04T21:29:58.505Z,Question from Code,v1,"What happens when the first argument to strtok is NULL +","strtok(NULL, '\n') ",, +63c648275f004bf4898cf9eb,2023-02-04T21:40:33.057Z,General Question,v1,"what's the specification for fgets +",,, +63c648275f004bf4898cf9eb,2023-02-04T21:45:58.985Z,General Question,v1,does stdin get closed at the end of file execution ,,, +63c648275f004bf4898cf9eb,2023-02-09T16:42:51.570Z,General Question,v1,"how does fseek work +",,, +63c648275f004bf4898cf9eb,2023-02-14T01:13:00.833Z,General Question,v1,"how can I get a readable format from time_t +",,, +63c648285f004bf4898cf9f0,2023-02-12T21:52:10.881Z,Question from Code,v1,am I creating a local variable with line 19 that is not updating the list of users,"int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + exit(-1); + } + // set the name of the new user + strcpy(new_user->name, name); + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User *latest_user = *user_ptr_add; + // No current users + if (latest_user == NULL){ + latest_user = new_user; + printf(""successfully added user""); // TESTING + return 0; + } + // find the latest_user added + while(latest_user->next != NULL){ + if (latest_user->name == new_user->name){ // another user already has this name + return 1; + } + latest_user = latest_user->next; + } + // found the last user + if(latest_user->name == new_user->name){ + return 1; + } + latest_user->next = new_user; + + printf(""successfully added""); // TESTING + + return 0; +}",, +63c648285f004bf4898cf9f0,2023-02-12T21:53:08.892Z,Question from Code,v1,"how do I update the pointer to the user list instead of creating a local variable +","int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + exit(-1); + } + // set the name of the new user + strcpy(new_user->name, name); + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User *latest_user = *user_ptr_add; + // No current users + if (latest_user == NULL){ + latest_user = new_user; + printf(""successfully added user""); // TESTING + return 0; + } + // find the latest_user added + while(latest_user->next != NULL){ + if (latest_user->name == new_user->name){ // another user already has this name + return 1; + } + latest_user = latest_user->next; + } + // found the last user + if(latest_user->name == new_user->name){ + return 1; + } + latest_user->next = new_user; + + printf(""successfully added""); // TESTING + + return 0; +}",, +63c648285f004bf4898cf9f0,2023-02-12T21:54:00.871Z,Question from Code,v1,"how do I create a local alias to the user_ptr_list to prevent having to dereference +","int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + exit(-1); + } + // set the name of the new user + strcpy(new_user->name, name); + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User *latest_user = *user_ptr_add; + // No current users + if (latest_user == NULL){ + latest_user = new_user; + printf(""successfully added user""); // TESTING + return 0; + } + // find the latest_user added + while(latest_user->next != NULL){ + if (latest_user->name == new_user->name){ // another user already has this name + return 1; + } + latest_user = latest_user->next; + } + // found the last user + if(latest_user->name == new_user->name){ + return 1; + } + latest_user->next = new_user; + + printf(""successfully added""); // TESTING + + return 0; +}",, +63c648285f004bf4898cf9f0,2023-02-12T22:09:36.869Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + exit(-1); + } + // set the name of the new user + strcpy(new_user->name, name); + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User *latest_user = *user_ptr_add; + // No current users + if (latest_user == NULL){ + latest_user = new_user; + printf(""successfully added user""); // TESTING + return 0; + } + // find the latest_user added + while(latest_user->next != NULL){ + if (latest_user->name == new_user->name){ // another user already has this name + return 1; + } + latest_user = latest_user->next; + } + // found the last user + if(latest_user->name == new_user->name){ + return 1; + } + latest_user->next = new_user; + + printf(""successfully added""); // TESTING + + return 0; +}","be able to iterate the user_ptr list and add a new user at the end of the list +", +63c648285f004bf4898cf9f0,2023-02-12T22:15:33.095Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + exit(-1); + } + // set the name of the new user + strcpy(new_user->name, name); + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User *latest_user = *user_ptr_add; + // No current users + if (latest_user == NULL){ + latest_user = new_user; + printf(""successfully added user\n""); // TESTING + return 0; + } + // find the latest_user added + while(latest_user->next != NULL){ + if (strcmp(latest_user->name, new_user->name) == 0){ // another user already has this name + return 1; + } + latest_user = latest_user->next; + } + // found the last user + if(strcmp(latest_user->name, new_user->name) == 0){ + return 1; + } + latest_user->next = new_user; + + printf(""successfully added\n""); // TESTING + + return 0; +}",add users to the end of user_ptr_list, +63c648285f004bf4898cf9f0,2023-02-12T22:27:42.606Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + exit(-1); + } + // set the name of the new user + strcpy(new_user->name, name); + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User **latest_user_ptr = *user_ptr_add; + User *latest_user = *latest_user_ptr; + // No current users + if (latest_user == NULL){ + latest_user = new_user; + printf(""successfully added user\n""); // TESTING + return 0; + } + // find the latest_user added + while(latest_user->next != NULL){ + if (strcmp(latest_user->name, new_user->name) == 0){ // another user already has this name + return 1; + } + latest_user = latest_user->next; + } + // found the last user + if(strcmp(latest_user->name, new_user->name) == 0){ + return 1; + } + latest_user->next = new_user; + + printf(""successfully added\n""); // TESTING + + return 0; +}",add the new user to the end of user_ptr_add, +63c648285f004bf4898cf9f0,2023-02-12T22:29:20.244Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + exit(-1); + } + // set the name of the new user + strcpy(new_user->name, name); + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User **latest_user_ptr = user_ptr_add; + User *latest_user = *latest_user_ptr; + // No current users + if (latest_user == NULL){ + latest_user = new_user; + printf(""successfully added user\n""); // TESTING + return 0; + } + // find the latest_user added + while(latest_user->next != NULL){ + if (strcmp(latest_user->name, new_user->name) == 0){ // another user already has this name + return 1; + } + latest_user = latest_user->next; + } + // found the last user + if(strcmp(latest_user->name, new_user->name) == 0){ + return 1; + } + latest_user->next = new_user; + + printf(""successfully added\n""); // TESTING + + return 0; +}",add the new user to the end of user_ptr_add, +63c648285f004bf4898cf9f0,2023-02-13T02:57:17.702Z,General Question,v1,do I need to malloc character arrays within structs?,,, +63c648285f004bf4898cf9f0,2023-02-13T02:58:04.249Z,General Question,v1,"If I assign a struct on the heap, will I need to malloc character arrays within the struct as well ?",,, +63c648285f004bf4898cf9f0,2023-02-13T20:17:31.928Z,General Question,v1,"how do I iterate in a while loop, is it i++ or i+=1",,, +63c648285f004bf4898cf9f0,2023-02-15T03:12:09.821Z,General Question,v1,how do I deallocate memory in a linked list,,, +63c648285f004bf4898cf9f0,2023-02-15T03:55:43.657Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User **latest_user = user_ptr_add; + + // No current users + if ((*latest_user) == NULL){ + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user) = new_user; + printf(""successfully added user\n""); // TESTING + return 0; + } + + // find the latest_user added + while((*latest_user)->next != NULL){ + if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name + return 1; + } + (*latest_user) = (*latest_user)->next; + } + // found the last user + if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name + return 1; + } + // create the new user and add to the list + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user)->next = new_user; + + printf(""successfully added\n""); // TESTING + + return 0; +}",add new users at the end of the list user_ptr_add, +63c648285f004bf4898cf9f0,2023-02-15T04:09:35.518Z,Question from Code,v1,is line 9 aliasing. Does changing *latest_user also change user_ptr_add,"int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User **latest_user = user_ptr_add; + + // No current users + if ((*latest_user) == NULL){ + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user) = new_user; + printf(""successfully added user\n""); // TESTING + return 0; + } + + // find the latest_user added + while((*latest_user)->next != NULL){ + if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name + return 1; + } + (*latest_user) = (*latest_user)->next; + } + // found the last user + if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name + return 1; + } + // create the new user and add to the list + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user)->next = new_user; + + printf(""successfully added\n""); // TESTING + + return 0; +}",, +63c648285f004bf4898cf9f0,2023-02-15T05:22:36.383Z,Question from Code,v1,is user_ptr_add changed at the end of the code,"int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User **latest_user = user_ptr_add; + + // No current users + if ((*latest_user) == NULL){ + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user) = new_user; + printf(""successfully added user\n""); // TESTING + return 0; + } + + // find the latest_user added + while((*latest_user)->next != NULL){ + if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name + return 1; + } + (*latest_user) = (*latest_user)->next; + } + // found the last user + if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name + return 1; + } + // create the new user and add to the list + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user)->next = new_user; + + printf(""successfully added\n""); // TESTING + + return 0; +}",, +63c648285f004bf4898cf9f0,2023-02-15T05:23:15.727Z,Question from Code,v1,how is user_ptr_add changed ,"int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User **latest_user = user_ptr_add; + + // No current users + if ((*latest_user) == NULL){ + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user) = new_user; + printf(""successfully added user\n""); // TESTING + return 0; + } + + // find the latest_user added + while((*latest_user)->next != NULL){ + if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name + return 1; + } + (*latest_user) = (*latest_user)->next; + } + // found the last user + if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name + return 1; + } + // create the new user and add to the list + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user)->next = new_user; + + printf(""successfully added\n""); // TESTING + + return 0; +}",, +63c648285f004bf4898cf9f0,2023-02-15T05:24:40.810Z,Question from Code,v1,what does user_ptr_add point to by the end of the code? Is it the original value of user_ptr_add,"int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User **latest_user = user_ptr_add; + + // No current users + if ((*latest_user) == NULL){ + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user) = new_user; + printf(""successfully added user\n""); // TESTING + return 0; + } + + // find the latest_user added + while((*latest_user)->next != NULL){ + if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name + return 1; + } + (*latest_user) = (*latest_user)->next; + } + // found the last user + if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name + return 1; + } + // create the new user and add to the list + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user)->next = new_user; + + printf(""successfully added\n""); // TESTING + + return 0; +}",, +63c648285f004bf4898cf9f0,2023-02-15T05:25:25.263Z,Question from Code,v1,why does user_ptr_add point to the last user in the code,"int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User **latest_user = user_ptr_add; + + // No current users + if ((*latest_user) == NULL){ + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user) = new_user; + printf(""successfully added user\n""); // TESTING + return 0; + } + + // find the latest_user added + while((*latest_user)->next != NULL){ + if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name + return 1; + } + (*latest_user) = (*latest_user)->next; + } + // found the last user + if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name + return 1; + } + // create the new user and add to the list + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user)->next = new_user; + + printf(""successfully added\n""); // TESTING + + return 0; +}",, +63c648285f004bf4898cf9f0,2023-02-15T05:26:42.893Z,Question from Code,v1,what does user_ptr_add point to at the end of the code,"int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User **latest_user = user_ptr_add; + + // No current users + if ((*latest_user) == NULL){ + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user) = new_user; + printf(""successfully added user\n""); // TESTING + return 0; + } + + // find the latest_user added + while((*latest_user)->next != NULL){ + if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name + return 1; + } + (*latest_user) = (*latest_user)->next; + } + // found the last user + if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name + return 1; + } + // create the new user and add to the list + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user)->next = new_user; + + printf(""successfully added\n""); // TESTING + + return 0; +}",, +63c648285f004bf4898cf9f0,2023-02-15T05:28:56.503Z,Question from Code,v1,have user_ptr_add point to the start of the list after adding the new user to the end of the linked list,"int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User **latest_user = user_ptr_add; + + // No current users + if ((*latest_user) == NULL){ + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user) = new_user; + printf(""successfully added user\n""); // TESTING + return 0; + } + + // find the latest_user added + while((*latest_user)->next != NULL){ + if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name + return 1; + } + (*latest_user) = (*latest_user)->next; + } + // found the last user + if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name + return 1; + } + // create the new user and add to the list + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user)->next = new_user; + + printf(""successfully added\n""); // TESTING + + return 0; +}",, +63c648285f004bf4898cf9f0,2023-02-15T06:13:34.906Z,Question from Code,v1,is User **latest_user = user_ptr_add; an alias,"int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User **latest_user = user_ptr_add; + + // No current users + if ((*latest_user) == NULL){ + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user) = new_user; + printf(""successfully added user\n""); // TESTING + return 0; + } + + // find the latest_user added + while((*latest_user)->next != NULL){ + if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name + return 1; + } + (*latest_user) = (*latest_user)->next; + } + // found the last user + if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name + return 1; + } + // create the new user and add to the list + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user)->next = new_user; + + printf(""successfully added\n""); // TESTING + + return 0; +}",, +63c648285f004bf4898cf9f0,2023-02-15T06:14:10.320Z,Question from Code,v1,is line 9 aliasing,"int create_user(const char *name, User **user_ptr_add) { + // check if the name is longer than the length limit. + if (strlen(name) > (MAX_NAME - 1)){ + return 2; + } + + // find the position to add the new user with name name while checking to make sure no other + // user has that name. + User **latest_user = user_ptr_add; + + // No current users + if ((*latest_user) == NULL){ + // create new user using calloc - will initialise all the structs internal elements to NULL + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user) = new_user; + printf(""successfully added user\n""); // TESTING + return 0; + } + + // find the latest_user added + while((*latest_user)->next != NULL){ + if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name + return 1; + } + (*latest_user) = (*latest_user)->next; + } + // found the last user + if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name + return 1; + } + // create the new user and add to the list + User *new_user = calloc(1, sizeof(User)); + if (new_user == NULL) { + // calloc failed + perror(""create_user calloc""); + return -1; + } + // set the name of the new user + strncpy(new_user->name, name, MAX_NAME); + new_user->name[MAX_NAME - 1] = '\0'; + + (*latest_user)->next = new_user; + + printf(""successfully added\n""); // TESTING + + return 0; +}",, +63c648285f004bf4898cf9f0,2023-02-15T06:49:49.431Z,Help Fix Code,v1,,"Post *temp = (user_to_del->first_post)->next; + free((user_to_del->first_post)->contents); + free((user_to_del->first_post)->date); + free(user_to_del->first_post); + user_to_del->first_post = temp;",deallocate dynamically allocated memory for struct first_post and date and contents in the linked list with first struct first_post, +63c648285f004bf4898cf9f0,2023-02-15T21:09:56.390Z,Question from Code,v1,why is there an infinite loop in my code,"int delete_user(const char *name, User **user_ptr_del) { + // find the user + User *user_to_del = find_user(name, *user_ptr_del); + if (user_to_del == NULL){ + // user not found + return 1; + } + printf(""hello1""); // TESTING + // fflush(stdout); + + // user exists + // loop through and delete the user from their friend's list + User *curr_user = *user_ptr_del; + + // go through each user and delete the user from friend list + while (curr_user != NULL){ + printf(""hello2""); // TESTING + // fflush(stdout); // TESTING + int i; + for (i=0; i < MAX_FRIENDS; i++){ + if ((curr_user->friends)[i] == user_to_del){ + printf(""%d"", i); // TESTING + // found the user to delete in friend's list, shift the position of other friends in the list + int j; + for (j=i; j < (MAX_FRIENDS - 1); j++){ + (curr_user->friends)[j] = (curr_user->friends)[j + 1]; + printf(""%d"", j); // TESTING + } + // shift the last friend - must be NULL + (curr_user->friends)[MAX_FRIENDS - 1] = NULL; + } + } + curr_user = curr_user->next; + } + + // deallocate memory associated with user's posts + Post *post = user_to_del->first_post; + while(post != NULL){ + user_to_del->first_post = post->next; + free(post->date); + free(post->contents); + free(post); + post = user_to_del->first_post; + } + + // find the user and delete + curr_user = *user_ptr_del; + if (curr_user == user_to_del){ + // user to delete is the first user + *user_ptr_del = user_to_del->next; + free(user_to_del); + return 0; + } + while (curr_user->next != NULL){ + if (curr_user->next == user_to_del){ + // found user to delete + curr_user->next = user_to_del->next; + // free memory for user + free(user_to_del); + return 0; + } + } + return 0; +}",, +63c648285f004bf4898cf9f0,2023-02-15T21:27:53.512Z,Question from Code,v1,"why does line 24 have no effect +","int delete_user(const char *name, User **user_ptr_del) { + // find the user + User *user_to_del = find_user(name, *user_ptr_del); + if (user_to_del == NULL){ + // user not found + return 1; + } + printf(""hello1""); // TESTING + // fflush(stdout); + + // user exists + // loop through and delete the user from their friend's list + User *curr_user = *user_ptr_del; + + // go through each user and delete the user from friend list + while (curr_user != NULL){ + printf(""hello2""); // TESTING + // fflush(stdout); // TESTING + int i; + for (i=0; i < MAX_FRIENDS; i++){ + if ((curr_user->friends)[i] == user_to_del){ + printf(""i : %d"", i); // TESTING + // found the user to delete in friend's list, shift the position of other friends in the list + for (i; i < (MAX_FRIENDS - 1); i++){ + (curr_user->friends)[i] = (curr_user->friends)[i + 1]; + printf(""j : %d"", i); // TESTING + } + // shift the last friend - must be NULL + (curr_user->friends)[MAX_FRIENDS - 1] = NULL; + } + } + curr_user = curr_user->next; + } + + // deallocate memory associated with user's posts + Post *post = user_to_del->first_post; + while(post != NULL){ + user_to_del->first_post = post->next; + free(post->date); + free(post->contents); + free(post); + post = user_to_del->first_post; + } + + // find the user and delete + curr_user = *user_ptr_del; + if (curr_user == user_to_del){ + // user to delete is the first user + *user_ptr_del = user_to_del->next; + free(user_to_del); + return 0; + } + while (curr_user->next != NULL){ + if (curr_user->next == user_to_del){ + // found user to delete + curr_user->next = user_to_del->next; + // free memory for user + free(user_to_del); + return 0; + } + } + return 0; +} +",, +63c648285f004bf4898cf9f0,2023-02-15T21:35:29.721Z,Question from Code,v1,is the code stuck anywhere," while (curr_user != NULL){ + printf(""hello2""); // TESTING + // fflush(stdout); // TESTING + int i; + for (i=0; i < MAX_FRIENDS; i++){ + if ((curr_user->friends)[i] == user_to_del){ + printf(""i : %d"", i); // TESTING + // found the user to delete in friend's list, shift the position of other friends in the list + int j = i; + for (j = i; j < (MAX_FRIENDS - 1); j++){ + (curr_user->friends)[j] = (curr_user->friends)[j + 1]; + printf(""j : %d"", j); // TESTING + } + // shift the last friend - must be NULL + (curr_user->friends)[MAX_FRIENDS - 1] = NULL; + i = MAX_FRIENDS ; // breaks the for_loop because already deleted user from friend list + } + } + curr_user = curr_user->next; + }",, +63c648285f004bf4898cf9f0,2023-02-15T21:37:06.791Z,Question from Code,v1,is curr_user updated in the while_loop," while (curr_user != NULL){ + printf(""hello2""); // TESTING + // fflush(stdout); // TESTING + int i; + for (i=0; i < MAX_FRIENDS; i++){ + if ((curr_user->friends)[i] == user_to_del){ + printf(""i : %d"", i); // TESTING + // found the user to delete in friend's list, shift the position of other friends in the list + int j = i; + for (j = i; j < (MAX_FRIENDS - 1); j++){ + (curr_user->friends)[j] = (curr_user->friends)[j + 1]; + printf(""j : %d"", j); // TESTING + } + // shift the last friend - must be NULL + (curr_user->friends)[MAX_FRIENDS - 1] = NULL; + i = MAX_FRIENDS ; // breaks the for_loop because already deleted user from friend list + } + } + curr_user = curr_user->next; + }",, +63c648285f004bf4898cf9f0,2023-03-15T21:23:11.684Z,General Question,v2,how do I convert a newline character in a string to a null terminator,,, +63c648285f004bf4898cf9f0,2023-03-16T03:23:37.686Z,General Question,v2,How do I use strtok,,, +63c648285f004bf4898cf9f0,2023-03-16T16:11:42.943Z,General Question,v2,how do I use if else if I have more code to execute regardless of which of the if or else statement is executed,,, +63c648285f004bf4898cf9f0,2023-03-24T21:36:21.383Z,General Question,v2,how do I point to a substring at position 5 of a string,,, +63c648285f004bf4898cf9f0,2023-04-06T03:18:03.994Z,General Question,v2,how do I use asprintf in a loop,,, +63c648295f004bf4898cf9fa,2023-01-19T01:26:59.149Z,Explain Code,v1,,"char phone[11]; + scanf(""%s"", phone);",, +63c648295f004bf4898cf9fa,2023-01-19T01:29:15.572Z,Explain Code,v1,,"char phone[11]; + scanf(""%s"", phone); + printf(""%s"", phone);",, +63c648295f004bf4898cf9fa,2023-01-19T01:30:35.405Z,General Question,v1,How does printf work with a pointer to a single character,,, +63c648295f004bf4898cf9fa,2023-01-19T01:47:28.391Z,General Question,v1,what does scanf return?,,, +63c648295f004bf4898cf9fa,2023-01-19T01:47:56.327Z,General Question,v1,What does scanf return when input stream is closed?,,, +63c648295f004bf4898cf9fa,2023-01-19T01:48:19.617Z,General Question,v1,What type is EOF,,, +63c648295f004bf4898cf9fa,2023-01-19T01:49:39.743Z,Explain Code,v1,,"scanf(""%d"", &num) != EOF",, +63c648295f004bf4898cf9fa,2023-01-23T04:14:28.100Z,Help Fix Code,v1,,"char[9] perms; +int file_size; +int acc = 0; +char[32] file_name; +scanf(""%s %*d %*s %*s %d %*s %*s %*s %*s %s"", perms, &file_size, file_name);","this code should compile, if it is in a function.", +63c648295f004bf4898cf9fa,2023-01-25T02:43:13.450Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + int *int_array = malloc(sizeof(int) * size); + for(int i = 0; i < 4; i++){ + int_array[i] = strtol(strs[i], NULL, 10); + } + return int_array; +} +","**strs is an array filled with number as strings. convert them all into ints, put them into an int array, and return the int array", +63c648295f004bf4898cf9fa,2023-01-25T02:44:12.916Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) { + int *int_array = malloc(sizeof(int) * size); + for(int i = 0; i < size; i++){ + int_array[i] = strtol(strs[i], NULL, 10); + } + return int_array; +} +","return a pointer to an array of ints, which is strs all converted to ints", +63c648295f004bf4898cf9fa,2023-02-08T03:56:12.010Z,Help Fix Code,v1,,"/* + * Read in the location of the pixel array, the image width, and the image + * height in the given bitmap file. + */ +void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { + fseek(image, 10, SEEK_SET); + fread(pixel_array_offset, sizeof(int), 1, image); + fseek(image, 18, SEEK_SET); + fread(width, sizeof(int), 1, SEEK_SET); + fseek(image, 22, SEEK_SET); + fread(height, sizeof(int), 1, SEEK_SET); +}","set pixel_array_offset, width, and height to what was in the file", +63c648295f004bf4898cf9fa,2023-02-08T03:56:57.807Z,Explain Code,v1,,"void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { + fseek(image, 10, SEEK_SET); + fread(pixel_array_offset, sizeof(int), 1, image); + fseek(image, 18, SEEK_SET); + fread(width, sizeof(int), 1, SEEK_SET); + fseek(image, 22, SEEK_SET); + fread(height, sizeof(int), 1, SEEK_SET); +}",, +63c648295f004bf4898cf9fa,2023-02-16T23:08:49.454Z,Explain Code,v1,,"Action *anode = malloc(sizeof(Action)); +// add values to anode; code omitted + +// put the action node into the rule +Action **ap = &(cur_rule->actions); // pointer to pointer of actions +while(*ap != NULL){ + *ap = (*ap)->next_act; +} +*ap = anode;",, +63c648295f004bf4898cf9fa,2023-03-03T02:33:40.787Z,General Question,v1,Should I use strncpy or strncat if I don't know the size of the string I'm copying or concatenating to?,,, +63c648295f004bf4898cf9fa,2023-03-05T00:31:00.666Z,General Question,v1,How do I check if a file exists in current working directory?,,, +63c648295f004bf4898cf9fa,2023-03-09T00:04:20.148Z,General Question,v1,What is the read() command?,,, +63c6482c5f004bf4898cfa18,2023-01-30T04:37:09.299Z,General Question,v1,How do I check permissions in a C file,,, +63c6482c5f004bf4898cfa18,2023-01-30T07:47:08.396Z,General Question,v1,"write me a for loop that loops from 0 to 9 +",,, +63c6482c5f004bf4898cfa18,2023-01-30T08:07:47.925Z,General Question,v1,"if I want to include helper functions from another file into the main file, what do I do",,, +63c6482c5f004bf4898cfa18,2023-01-30T08:17:39.102Z,Help Fix Code,v1,,"#include +#include +#include ""sin_helpers.c"" + +int populate_array(int, int *); +int check_sin(int *); + + +int main(int argc, char **argv) { + // TODO: Verify that command line arguments are valid. + if (!(argc == 2)) { + return 2; + } + // TODO: Parse arguments and then call the two helpers in sin_helpers.c + // to verify the SIN given as a command line argument. + int socialInsuranceNum = strtol(argv[1], NULL, 10); + int numArray[9]; + int ans; + populate_array(socialInsuranceNum, numArray); + ans = check_sin(numArray); + printf(""%d"", ans); + return ans; +} +",My code is supposed to print if the SIN is valid but it states there are multiple definitions for check_sin, +63c6482c5f004bf4898cfa18,2023-02-09T06:30:01.427Z,Help Fix Code,v1,," +struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **ptr = malloc(height*sizeof(struct pixel)); + for (int i = 0; i < height; i++) { + ptr[i] = malloc(width * sizeof(struct pixel)); + } + + fseek(image, pixel_array_offset, SEEK_SET); + + for (int i = 0; i < height; i++) { + fread(ptr[i], sizeof(struct pixel), width, image); + } + return ptr; +}","/* + * 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. + */", +63c6482c5f004bf4898cfa18,2023-02-14T23:48:49.690Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + User *current_user = *user_ptr_add; + while (current_user != NULL) { + if (strcmp(current_user->name, name) == 0) { + return 1; + } + current_user = current_user->next; + } + if (strlen(name) + 1 > MAX_NAME) { + return 2; + } + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + } else { + current_user = *user_ptr_add; + while (current_user->next != NULL) { + current_user = current_user->next; + } + current_user->next = new_user; + } + + // Return 0 to indicate success + return 0; +}","/* + * 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) + */", +63c6482c5f004bf4898cfa18,2023-02-15T00:25:50.131Z,Help Fix Code,v1,," +int update_pic(User *user, const char *filename) { + if (strlen(filename) > MAX_NAME) { + return 2; // Filename too long + } + FILE *file = fopen(filename, ""r""); + if (file == NULL) { + return 1; // File does not exist or cannot be opened + } + fclose(file); + + // Update the user's profile picture filename + strcpy(user->profile_pic, filename); + + return 0; // Success + return -1; +}","/* + * 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. + */", +63c6482c5f004bf4898cfa18,2023-02-15T03:09:38.881Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + if(find_user(name1, (const User *) head) == NULL || find_user(name2, (const User *) head) == NULL){ + return 4; + } + if(strcmp(name1, name2) == 0){ + return 3; + } + User *user1 = find_user(name1, (const User *) head); + User *user2 = find_user(name2, (const User *) head); + for(int i = 0; i < sizeof(user1->friends); i++){ + if(strcmp(user1->friends[i]->name, name2) == 0){ + return 1; + } + } + for(int i = 0; i < sizeof(user2->friends); i++){ + if(strcmp(user2->friends[i]->name, name1) == 0){ + return 1; + } + } + if (sizeof(user1->friends) >= MAX_FRIENDS || sizeof(user2->friends) >= MAX_FRIENDS){ + return 2; + } + for(int i = 0; i < sizeof(user1->friends); i++){ + if(user1->friends[i] == NULL){ + user1->friends[i] = user2; + } + } + for(int i = 0; i < sizeof(user2->friends); i++){ + if(user2->friends[i] == NULL){ + user2->friends[i] = user1; + } + } + + + return 0; +}","/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */", +63c6482c5f004bf4898cfa18,2023-02-15T03:18:56.305Z,Help Fix Code,v1,,"int make_friends(const char *name1, const char *name2, User *head) { + if(find_user(name1, (const User *) head) == NULL || find_user(name2, (const User *) head) == NULL){ + return 4; + } + if(strcmp(name1, name2) == 0){ + return 3; + } + User *user1 = find_user(name1, (const User *) head); + User *user2 = find_user(name2, (const User *) head); + for(int i = 0; i < sizeof(user1->friends); i++){ + if(strcmp(user1->friends[i]->name, name2) == 0){ + return 1; + } + } + for(int i = 0; i < sizeof(user2->friends); i++){ + if(strcmp(user2->friends[i]->name, name1) == 0){ + return 1; + } + } + if (sizeof(user1->friends) >= MAX_FRIENDS || sizeof(user2->friends) >= MAX_FRIENDS){ + return 2; + } + for(int i = 0; i < MAX_FRIENDS; i++){ + if(user1->friends[i] == NULL){ + user1->friends[i] = user2; + } + } + for(int i = 0; i < MAX_FRIENDS; i++){ + if(user2->friends[i] == NULL){ + user2->friends[i] = user1; + } + } + + + return 0; +}","/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */", +63c6482c5f004bf4898cfa18,2023-02-15T03:48:34.262Z,Help Fix Code,v1,,"for(int i = 0; i < MAX_FRIENDS; i++){ + if(strcmp(user1->friends[i]->name, name2) == 0){ + return 1; + } + }",This snippet is supposed to compare the names of the users and return 1 if they are the same, +63c6482c5f004bf4898cfa18,2023-02-15T06:55:07.102Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + int exists = 0; + User *current_user = *user_ptr_del; + while (current_user != NULL) { + if (strcmp(current_user->name, name) == 0) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if(current_user->friends[i] != NULL){ + //Look through targets list of friends' list of friends and delete target from them + for (int a = 0; a < MAX_FRIENDS; a++) { + if(current_user->friends[i]->friends[a] != NULL && strcmp(current_user->friends[i]->friends[a]->name, name) == 0){ + current_user->friends[i]->friends[a] = NULL; + } + } + } + } + exists = 1; + } + current_user = current_user->next; + } + if (exists == 1){ + // Free memory associated with user + free(current_user->name); + free(current_user->profile_pic); + for (int i = 0; i < MAX_FRIENDS; i++) { + free(current_user->friends[i]); + } + free(current_user->friends); + + Post*currentPost = current_user->first_post; + while (currentPost != NULL){ + free(currentPost->author); + free(currentPost->date); + free(currentPost->contents); + Post*nextPost = currentPost->next; + free(currentPost); + currentPost = nextPost; + } + + free(current_user); + 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.", +63c6482c5f004bf4898cfa18,2023-02-15T07:17:16.972Z,Help Fix Code,v1,,"for (int i = 0; i < MAX_FRIENDS; i++) { + free(current_user->friends[i]); + }","Code is supposed to free each element of the friends array but the error is double free detected in tcache 2 +", +63c6482c5f004bf4898cfa18,2023-02-17T00:37:15.441Z,Help Fix Code,v1,,"char *copy(char *dest, const char *src, int capacity) { + for(int i = 0; i < capacity-1; i++){ + if (src[i] != NULL){ + memset(dest[i], src[i], 1); + } + else{ + memset(dest[i], '\0', 1); + } + } + memset(dest[capacity-1], '\0', 1); + return dest; +}","Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset.", +63c6482e5f004bf4898cfa2c,2023-01-17T20:07:46.351Z,General Question,v1,What are double pointers for,,, +63c6482e5f004bf4898cfa2c,2023-01-17T20:35:16.508Z,General Question,v1,why can I access an array index further than the size of the array,,, +63c6482e5f004bf4898cfa2c,2023-01-18T18:59:37.582Z,General Question,v1,how to know when the input stream ends with scans,,, +63c6482e5f004bf4898cfa2c,2023-01-18T19:01:54.577Z,General Question,v1,how do I check the end of input stream from the terminal with scanf,,, +63c6482f5f004bf4898cfa3b,2023-02-28T01:43:42.658Z,Question from Code,v1,"what happens when I try to call strtol on ""42""","#include +#include +#include +#include +int main(int argc, char **argv) { +char *next = NULL; +errno = 0; +long longi = strtol(argv[1], &next, 0); +int i = longi; +printf(""longi: %ld\n"",longi); +printf(""i: %d\n"",i); +printf(""next is |%s|\n"",next); +if(errno != 0) { +perror(""strtol""); +} +return 0; +}",, +63c6482f5f004bf4898cfa3b,2023-02-28T01:45:21.115Z,Question from Code,v1,what happens when base is 0 for strtol,"#include +#include +#include +#include +int main(int argc, char **argv) { +char *next = NULL; +errno = 0; +long longi = strtol(argv[1], &next, 0); +int i = longi; +printf(""longi: %ld\n"",longi); +printf(""i: %d\n"",i); +printf(""next is |%s|\n"",next); +if(errno != 0) { +perror(""strtol""); +} +return 0; +}",, +63c6482f5f004bf4898cfa3b,2023-02-28T01:46:38.931Z,Explain Code,v1,,"#include +#include +#include +#include +int main(int argc, char **argv) { +char *next = NULL; +errno = 0; +long longi = strtol(argv[1], &next, 0); +int i = longi; +printf(""longi: %ld\n"",longi); +printf(""i: %d\n"",i); +printf(""next is |%s|\n"",next); +if(errno != 0) { +perror(""strtol""); +} +return 0; +}",, +63c648305f004bf4898cfa40,2023-01-20T14:41:27.424Z,Explain Code,v1,,"#include +#include + +int main() { + char phone[11]; + int i; + + scanf(""%s %d"", phone, &i); + printf(""%s %d"", phone, i); + + if (i == -1) { + printf(""%s\n"", phone); + } else if (i > -1 && i < 10) { + printf(""%c\n"", phone[i]); + } else { + printf(""ERROR\n""); + return 1; + } + + return 0; +} +",, +63c648305f004bf4898cfa40,2023-01-20T14:48:02.898Z,General Question,v1,"what scanf return +",,, +63c648305f004bf4898cfa40,2023-01-20T14:54:52.155Z,Help Fix Code,v1,,"#include +#include + +int main() { + char phone[11]; + int t; + int i; + // repeatedly reads integers until standard input is closed + while (t < 10) { + scanf(""%s"", &phone[t]); + } + return 0; +}",, +63c648305f004bf4898cfa40,2023-01-20T14:56:56.744Z,Help Fix Code,v1,,"#include +#include + +int main() { + char phone[11]; + int t; + int i; + // repeatedly reads integers until standard input is closed + while (t < 10) { + scanf(""%s"", phone[t]); + } + return 0; +}",, +63c648305f004bf4898cfa40,2023-01-20T15:00:58.620Z,Help Fix Code,v1,,"#include +#include + +int main() { + char phone[11]; + int t; + int i; + // repeatedly reads integers until standard input is closed + while (t < 10) { + t += scanf(""%s"", &phone[t]); + } + return 0; +}",, +63c648305f004bf4898cfa40,2023-01-20T15:04:16.699Z,Help Fix Code,v1,,"#include +#include + +int main() { + char phone[11]; + int t = 0; + int i; + // repeatedly reads integers until standard input is closed + while (t < 10) { + t += scanf(""%s"", &phone[t]); + } + return 0; +}",, +63c648305f004bf4898cfa40,2023-01-20T15:06:06.332Z,Explain Code,v1,,"#include +#include + +int main() { + char phone[11]; + int t = 0; + int i; + // repeatedly reads integers until standard input is closed + while (t < 10) { + t += scanf(""%s"", &phone[t]); + } + return 0; +} +",, +63c648305f004bf4898cfa40,2023-01-20T15:08:56.910Z,General Question,v1,what return value of scanf indicates that the standard input is closed,,, +63c648305f004bf4898cfa40,2023-01-20T15:21:12.265Z,Explain Code,v1,,"#include +#include + + +int main() { + char phone[11]; + int t = 0; + int i; + scanf(""%s"", phone); + + // repeatedly reads integers until standard input is closed + while (scanf(""%d"", &i) != EOF) { + if (i == -1) { + // prints the phone number + printf(""%s\n"", phone); + } else if (i > -1 && i < 10) { + // prints the character at that index in the phone number + printf(""%c\n"", phone[i]); + } else { + // prints an error message + printf(""ERROR\n""); + return 1; + } + } + return 0; +} +",, +63c648305f004bf4898cfa40,2023-01-20T20:17:16.250Z,General Question,v1,what %i represent in scanf() in c?,,, +63c648305f004bf4898cfa40,2023-01-25T00:05:08.405Z,Explain Code,v1,," +/* Write your solution here */ +void fib(int **fseq, int n) { + *fseq = malloc(sizeof(int) * n); + fseq[0] = 0; + for (int i = 1; i < n; i++) { + if (i == 1) { + fseq[1] = 1;} + else { + fseq[i] = fseq[i-2] + fseq[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; +}",, +63c648305f004bf4898cfa40,2023-01-25T00:52:37.874Z,General Question,v1,how do you use malloc()?,,, +63c648305f004bf4898cfa40,2023-01-25T01:37:04.770Z,General Question,v1,how do we convert a float into a integer?,,, +63c648305f004bf4898cfa40,2023-01-25T01:38:33.282Z,Explain Code,v1,,"float x = 1.5; +printf(""%d"", (int)x);",, +63c648305f004bf4898cfa40,2023-01-25T01:58:24.718Z,Help Fix Code,v1,,"#include +#include + + +/* + * Sample usage: + * $ gcc -Wall -std=gnu99 -g -o split_array split_array.c + * $ ./split_array 1 2 3 + * Original array: + * 1 2 3 + * result[0]: + * 1 3 + * result[1]: + * 2 + */ + + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ + +int **split_array(const int *s, int length) { + int **ptr; + int half; + half = length / 2; + + ptr[0] = malloc(sizeof(int) * half); + ptr[1] = malloc(sizeof(int) * (half+1)); + + for (int i = 0; i < length; i++) { + if (i == (length - 1) && length % 2 == 1) { + (ptr[0])[i] = s[1+i]; + } + (ptr[0])[i] = s[1+i]; + (ptr[1])[i] = s[2+i]; + } + return ptr; +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int *p = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) { + p[0] = strtol(strs[1+i], NULL, 10); + } + return p; +} + + +int main(int argc, char **argv) { + /* Replace the comments in the next two lines with the appropriate + arguments. Do not add any additional lines of code to the main + function or make other changes. + */ + int *full_array = build_array(argv, argc - 1); + int **result = split_array(full_array, argc - 1); + + printf(""Original array:\n""); + for (int i = 0; i < argc - 1; i++) { + printf(""%d "", full_array[i]); + } + printf(""\n""); + + printf(""result[0]:\n""); + for (int i = 0; i < argc / 2; i++) { + printf(""%d "", result[0][i]); + } + printf(""\n""); + + printf(""result[1]:\n""); + for (int i = 0; i < (argc - 1) / 2; i++) { + printf(""%d "", result[1][i]); + } + printf(""\n""); + free(full_array); + free(result[0]); + free(result[1]); + free(result); + return 0; +} +",, +63c648305f004bf4898cfa40,2023-01-26T23:48:04.668Z,Explain Code,v1,,"!(n = strtol(argv[1], NULL, 10))",, +63c648305f004bf4898cfa40,2023-01-27T02:46:14.673Z,General Question,v1,how to read standard input?,,, +63c648305f004bf4898cfa40,2023-01-27T03:10:32.695Z,Explain Code,v1,,"#include + +int main() { + char str1[20] = ""Hello""; + char str2[20] = ""World""; + printf(""str1: %s"", str1); + printf(""str2: %s"", str2); + strncpy(str1, str2, 3); + printf(""str1: %s"", str1); + printf(""str2: %s"", str2); + + return 0; +} +",, +63c648305f004bf4898cfa40,2023-01-27T15:52:42.258Z,General Question,v1,what scanf can do?,,, +63c648305f004bf4898cfa40,2023-01-27T16:32:41.137Z,Help Fix Code,v1,,"#include +#include + + +/* + * Sample usage: + * $ gcc -Wall -std=gnu99 -g -o count_large count_large.c + * $ count_large 1000 + * 0 + * $ ./fibo 2 + * 0 1 + */ + +// TODO: Implement a helper named check_permissions that matches the prototype below. +int check_permissions(char *perm, char *req) { + char p[3]; + p[0] = req[0]; + p[1] = req[1]; + p[2] = req[2]; + + for (int i ; i < 3; i++) { + // handle r + if (p[0] == perm[i*3]) {p[0] = '-';} + else if (i < 2) {p[0] = req[(i+1)*3];} + // handle w + if (p[1] == perm[i*3 + 1]) {p[1] = '-';} + else if (i < 2) {p[0] = req[(i+1)*3 + 1];} + // handle x + if (p[2] == perm[i*3 + 2]) {p[2] = '-';} + else if (i < 2) {p[0] = req[(i+1)*2 + 2];} + // check if permissions are all meet + if (p[0] == '-' && p[1] == '-' && p[2] == '-') { + return 0; + } + } + return 1; +}; + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + int count = 0; + int n, di, size; + char req[9], p[10], user[31], group[31], m[31], d[31], y[31], f[31]; + + // TODO: Process command line arguments. + n = strtol(argv[1], NULL, 10); + if (argc == 3) {req = argv[2];} + + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + scanf(""%s"", &f); + + while (scanf(""%s %d %s %s %d %s %s %s %s"", + p, &di, user, group, &size, m, d, y, f) != EOF) { + if (argc == 3 && !check_permissions(p+1, req) && n < size ) { + count++; + } else if (n < size){ + count++; + } + } + printf('%d/n', count); + + return 0; +}",a C program called count_large. The program will inspect the output from the ls -l command and print the number of files larger than a specified size., +63c648305f004bf4898cfa40,2023-01-27T16:36:20.223Z,Explain Code,v1,,"int check_permissions(char *perm, char *req) { + char p[3]; + p[0] = req[0]; + p[1] = req[1]; + p[2] = req[2]; + + for (int i ; i < 3; i++) { + // handle r + if (p[0] == perm[i*3]) {p[0] = '-';} + else if (i < 2) {p[0] = req[(i+1)*3];} + // handle w + if (p[1] == perm[i*3 + 1]) {p[1] = '-';} + else if (i < 2) {p[0] = req[(i+1)*3 + 1];} + // handle x + if (p[2] == perm[i*3 + 2]) {p[2] = '-';} + else if (i < 2) {p[0] = req[(i+1)*2 + 2];} + // check if permissions are all meet + if (p[0] == '-' && p[1] == '-' && p[2] == '-') { + return 0; + } + } + return 1; +};",, +63c648305f004bf4898cfa40,2023-01-27T16:52:40.464Z,Explain Code,v1,,"int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + int count = 0; + int n, di, size; + char req[9], p[10], user[31], group[31], m[31], d[31], y[31], f[31]; + + // TODO: Process command line arguments. + n = strtol(argv[1], NULL, 10); + + if (argc == 3) {req = argv[2];} +}",, +63c648305f004bf4898cfa40,2023-01-27T21:32:22.700Z,Explain Code,v1,,"int populate_array(int sin, int *sin_array) { + int a = sin; + for (int i=0; i<9;i++) { + sin_array[i] = sin % 10; + a = (a - sin % 10)/10; + } + return 0; +}",, +63c648305f004bf4898cfa40,2023-01-28T02:16:20.829Z,Question from Code,v1,does the string holiday has '\0' at the end?,"char holiday[20] = ""Valentine's Day"";",, +63c648305f004bf4898cfa40,2023-01-28T02:17:39.290Z,Question from Code,v1,does alphabet has '\0' at the end?,"char alphabet[] = ""abcdef"";",, +63c648305f004bf4898cfa40,2023-03-03T16:43:57.942Z,Explain Code,v1,,"#include +#include +#include + + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, ""Usage: forkloop \n""); + exit(1); + } + + int iterations = strtol(argv[1], NULL, 10); + int r; + for (int i = 0; i < iterations; i++) { + if ((r = fork()) < 0) { + perror(""fork""); + exit(1); + } else if (r == 0) { + printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + exit(0); + } + + // printf(""ppid = %d, pid = %d, i = %d\n"", getppid(), getpid(), i); + } + + return 0; +}",, +63c648335f004bf4898cfa5e,2023-01-17T23:58:14.126Z,General Question,v1,"how would I restrict a standard input to a string with a limited number of characters in C +",,, +63c648335f004bf4898cfa5e,2023-01-18T00:02:55.039Z,General Question,v1,what is the format specifier for integers in scanf?,,, +63c648335f004bf4898cfa5e,2023-01-18T00:38:30.643Z,General Question,v1,where can I find the man page for scanf?,,, +63c648335f004bf4898cfa5e,2023-01-24T00:12:05.112Z,General Question,v1,"does C allow comparison of two chars using > or ? ,,, +63c648335f004bf4898cfa5e,2023-01-24T07:23:20.850Z,General Question,v1,why does an unused variable warning appear after compilation?,,, +63c648335f004bf4898cfa5e,2023-01-24T07:34:42.046Z,General Question,v1,"given arrays as inputs to a function in the form of pointers, how should I access the variable at a specific index? ",,, +63c648335f004bf4898cfa5e,2023-01-24T16:47:23.205Z,General Question,v1,does dividing two ints round or floor the result in C? ,,, +63c648335f004bf4898cfa5e,2023-01-24T23:16:29.841Z,General Question,v1,how can a .c file use functions from another .c file in the same directory? ,,, +63c648335f004bf4898cfa5e,2023-01-25T22:54:14.539Z,General Question,v1,can I get the size of an array in C? ,,, +63c648335f004bf4898cfa5e,2023-02-01T00:21:48.459Z,General Question,v1,"where can I find the man page for malloc? +",,, +63c648335f004bf4898cfa5e,2023-02-02T20:27:51.042Z,General Question,v1,is int a struct in C? ,,, +63c648335f004bf4898cfa5e,2023-02-02T20:28:54.222Z,General Question,v1,"where can I find the man page for typedef? +",,, +63c648335f004bf4898cfa5e,2023-02-02T20:32:42.031Z,General Question,v1,can typedef be used on primitives? ,,, +63c648335f004bf4898cfa5e,2023-02-14T00:47:23.923Z,General Question,v1,"do structs get initialized to NULL by default? +",,, +63c648335f004bf4898cfa5e,2023-02-14T00:48:16.793Z,General Question,v1,does C have a function to check the type of a variable?,,, +63c648335f004bf4898cfa5e,2023-02-14T06:35:04.088Z,General Question,v1,does strncpy's _Count parameter correspond to the number of characters before adding a null terminator or including the null terminator? ,,, +63c648335f004bf4898cfa5e,2023-02-14T06:49:07.202Z,General Question,v1,"what does fopen return if the file does not exist? +",,, +63c648335f004bf4898cfa5e,2023-02-14T06:58:20.718Z,General Question,v1,do I still need to use fclose if fopen returns NULL?,,, +63c648335f004bf4898cfa5e,2023-02-14T07:28:08.463Z,General Question,v1,what happens with strstr if the second argument is longer than the first argument? ,,, +63c648335f004bf4898cfa5e,2023-02-14T09:07:14.567Z,General Question,v1,what can I use to open and then print the contents of an ASCII file? ,,, +63c648335f004bf4898cfa5e,2023-02-14T09:10:40.951Z,General Question,v1,does the format specifier for a character include whitespace and newline characters? ,,, +63c648335f004bf4898cfa5e,2023-02-14T09:11:56.922Z,General Question,v1,what mode needs to be specified to read an ASCII file? ,,, +63c648335f004bf4898cfa5e,2023-02-14T09:13:56.357Z,General Question,v1,what does the %s format specifier match? ,,, +63c648335f004bf4898cfa5e,2023-02-14T09:19:03.091Z,General Question,v1,what does fscanf return? ,,, +63c648335f004bf4898cfa5e,2023-02-14T09:22:33.453Z,General Question,v1,is there a way for scanf to also read the whitespace and newlines from a file? ,,, +63c648335f004bf4898cfa5e,2023-02-14T09:30:28.918Z,General Question,v1,"what does the format specifier %s match in printf? +",,, +63c648335f004bf4898cfa5e,2023-02-14T09:40:51.820Z,General Question,v1,how are time_t values printed? ,,, +63c648335f004bf4898cfa5e,2023-02-14T09:42:17.793Z,General Question,v1,please provide an example and the printed result of setting the value of a time_t variable and calling printf using the %s format specifier. ,,, +63c648335f004bf4898cfa5e,2023-02-14T09:43:37.301Z,General Question,v1,"generate a detailed documentation of `time_t now; time(&now); printf(""%s"", ctime(&now));` with usage examples and explanations",,, +63c648335f004bf4898cfa5e,2023-02-14T09:54:55.411Z,General Question,v1,"does the keyword ""time"" denote the current time? ",,, +63c648335f004bf4898cfa5e,2023-02-14T16:38:12.597Z,General Question,v1,does C have a data type for booleans? ,,, +63c648335f004bf4898cfa5e,2023-02-14T16:48:35.714Z,General Question,v1,how would I initialize a variable to the current time? ,,, +63c648335f004bf4898cfa5e,2023-02-14T17:25:24.226Z,General Question,v1,"When an if statement contains an &&, is the left hand side of that && always evaluated first? +",,, +63c648335f004bf4898cfa5e,2023-02-14T19:17:08.167Z,General Question,v1,"when malloc allocates memory for a struct, does it allocate memory for all of the variables in the struct? ",,, +63c648335f004bf4898cfa5e,2023-02-14T19:44:50.073Z,General Question,v1,how would I obtain a line from a file up to a newline character and send that value to a string using fscanf? ,,, +63c648335f004bf4898cfa5e,2023-02-15T01:32:29.140Z,General Question,v1,"does the exit() function terminate the program? +",,, +63c648335f004bf4898cfa5e,2023-02-15T01:35:27.639Z,General Question,v1,is there a format character that matches a string of unknown length? ,,, +63c648335f004bf4898cfa5e,2023-02-15T20:12:06.136Z,General Question,v1,does strncpy add the null terminator at the end of the string it's copying or when it hits the provided argument for number of characters,,, +63c648335f004bf4898cfa5e,2023-02-15T20:30:37.635Z,General Question,v1,does fgets need to copy to a pointer to heap allocated memory?,,, +63c648335f004bf4898cfa5e,2023-02-16T20:00:04.691Z,General Question,v1,what is the difference between size_t and int? ,,, +63c648335f004bf4898cfa5e,2023-02-28T04:00:53.350Z,General Question,v1,are unassigned chars treated as null terminators? ,,, +63c648335f004bf4898cfa5e,2023-03-07T20:43:14.382Z,General Question,v1,what does wait return if it fails?,,, +63c648335f004bf4898cfa5e,2023-03-13T20:52:31.936Z,General Question,v2,"where can I find the man page for strtok? +",,, +63c648335f004bf4898cfa5e,2023-03-13T20:56:40.631Z,General Question,v2,what can I use to read through a file line by line?,,, +63c648335f004bf4898cfa5e,2023-03-13T21:12:13.408Z,General Question,v2,how do I initialize an array of n elements in c? ,,, +63c648335f004bf4898cfa5e,2023-03-13T22:21:33.374Z,General Question,v2,what is the tab character in c? ,,, +63c648335f004bf4898cfa5e,2023-03-13T22:22:49.072Z,General Question,v2,is it safe to declare and assign a variable inside of a loop? ,,, +63c648335f004bf4898cfa5e,2023-03-13T22:39:24.915Z,General Question,v2,does C have do while loops? ,,, +63c648335f004bf4898cfa5e,2023-03-14T01:07:34.230Z,General Question,v2,how can I remove \n and \r characters from a string? ,,, +63c648335f004bf4898cfa5e,2023-03-14T01:18:08.720Z,General Question,v2,how can I remove a newline character from the end of a string? ,,, +63c648335f004bf4898cfa5e,2023-03-14T01:20:40.271Z,General Question,v2,does fgets stop when it reaches a newline? ,,, +63c648335f004bf4898cfa5e,2023-03-14T01:39:07.541Z,General Question,v2,what does malloc return if it fails?,,, +63c648335f004bf4898cfa5e,2023-03-14T20:48:16.590Z,General Question,v2,how can I obtain the contents of a string after a certain index? ,,, +63c648335f004bf4898cfa5e,2023-03-14T20:49:24.216Z,General Question,v2,does strtok return the content of the string before or after the delimiter?,,, +63c648335f004bf4898cfa5e,2023-03-14T22:35:46.904Z,General Question,v2,how do I declare an array of strings in C?,,, +63c648335f004bf4898cfa5e,2023-03-14T22:43:56.110Z,General Question,v2,does strtok automatically stop at null pointers?,,, +63c648335f004bf4898cfa5e,2023-03-14T23:40:37.478Z,General Question,v2,is there a type signature that represents any given pointer?,,, +63c648335f004bf4898cfa5e,2023-03-15T00:43:55.849Z,General Question,v2,what does strtok return if it never reads the delimiter? ,,, +63c648335f004bf4898cfa5e,2023-03-15T00:45:07.267Z,General Question,v2,does fgets include the \n?,,, +63c648335f004bf4898cfa5e,2023-03-15T00:49:23.990Z,General Question,v2,is there a way to replace the value of a character in a read-only string? ,,, +63c648335f004bf4898cfa5e,2023-03-15T00:51:34.074Z,General Question,v2,what does strcspn do?,,, +63c648335f004bf4898cfa5e,2023-03-15T12:40:52.704Z,General Question,v2,"does stat require that the input statbuf is allocated with memory, or is it sufficient to simply declare a buffer pointer?",,, +63c648335f004bf4898cfa5e,2023-03-15T13:07:22.122Z,General Question,v2,how can I compare two st_mtime structs?,,, +63c648335f004bf4898cfa5e,2023-03-15T13:55:54.555Z,General Question,v2,"if my program runs into an error, should I use perror or fprintf to stderr?",,, +63c648335f004bf4898cfa5e,2023-03-15T14:31:17.460Z,General Question,v2,does memory need to be de-allocated from a child process that has terminated and been waited for? ,,, +63c648335f004bf4898cfa5e,2023-03-15T15:08:52.225Z,General Question,v2,can I use strncat in one function to change the contents of an array that was passed in from a different function? ,,, +63c648335f004bf4898cfa5e,2023-03-15T15:13:55.290Z,General Question,v2,Is there any way to directly find the size taken up by a pointer/array in memory?,,, +63c648335f004bf4898cfa5e,2023-03-15T15:32:40.255Z,General Question,v2,"what does fork take as an input? +",,, +63c648335f004bf4898cfa5e,2023-03-15T15:44:57.834Z,General Question,v2,could you please provide an example of using execvp to run a string of shell commands in the current directory?,,, +63c648335f004bf4898cfa5e,2023-03-15T16:28:20.743Z,General Question,v2,is execvp a system call? ,,, +63c648335f004bf4898cfa5e,2023-03-15T16:37:46.366Z,General Question,v2,what should be done if WIFEXITED returns false? ,,, +63c648335f004bf4898cfa5e,2023-03-15T17:15:03.048Z,General Question,v2,what character represents a string in printf? ,,, +63c648335f004bf4898cfa5e,2023-03-15T18:58:43.827Z,General Question,v2,how can I copy the values of object stored in one pointer to a struct to another pointer to the same struct? ,,, +63c648335f004bf4898cfa5e,2023-03-15T20:20:56.691Z,General Question,v2,how do I make an instance of struct timespec with value 0?,,, +63c648335f004bf4898cfa5e,2023-03-15T20:29:51.047Z,General Question,v2,does dereferencing a null pointer produce segmentation fault?,,, +63c648335f004bf4898cfa5e,2023-03-22T00:45:42.212Z,General Question,v2,how can I confirm whether or not a certain index of an array is empty,,, +63c648335f004bf4898cfa5e,2023-03-22T01:06:03.073Z,General Question,v2,how does memmove work? ,,, +63c648335f004bf4898cfa5e,2023-03-22T06:28:21.656Z,General Question,v2,how can I tell if memory has been allocated for a variable? ,,, +63c648335f004bf4898cfa5e,2023-03-24T18:53:49.225Z,General Question,v2,what inputs are taken by the write system call? ,,, +63c648335f004bf4898cfa5e,2023-03-24T18:55:24.549Z,General Question,v2,does strcat replace a null terminator at the end of the destination? ,,, +63c648335f004bf4898cfa5e,2023-03-24T19:31:10.770Z,General Question,v2,"if I have an array of chars named charr which ends in a null terminator (and only has the terminator at its last index), is sizeof(charr) - 1 the same as strlen(charr)?",,, +63c648335f004bf4898cfa5e,2023-03-24T19:52:27.387Z,General Question,v2,what does FD_SET do? ,,, +63c648335f004bf4898cfa5e,2023-03-24T19:58:29.778Z,General Question,v2,what is the fd for stdin?,,, +63c648335f004bf4898cfa5e,2023-03-25T02:27:19.511Z,General Question,v2,does strcpy add a newline to a string?,,, +63c648335f004bf4898cfa5e,2023-03-25T02:28:18.527Z,General Question,v2,does read terminate on network newlines?,,, +63c648335f004bf4898cfa5e,2023-03-25T02:32:15.493Z,General Question,v2,does wsl automatically add network newlines when the enter key is pressed? ,,, +63c648335f004bf4898cfa5e,2023-03-25T02:38:03.505Z,General Question,v2,does wsl automatically add newlines when the enter key is pressed,,, +63c648335f004bf4898cfa5e,2023-03-25T02:41:14.218Z,General Question,v2,does pressing enter on a line add a newline? ,,, +63c648335f004bf4898cfa5e,2023-04-06T07:58:23.030Z,General Question,v2,does strcat work with dynamically allocated strings?,,, +63c648335f004bf4898cfa5e,2023-04-06T08:01:08.994Z,General Question,v2,can I create an array with a variable value for its size? ,,, +63c648335f004bf4898cfa5e,2023-04-06T08:22:46.143Z,General Question,v2,can read only strings be used at source inputs for strcat?,,, +63c648335f004bf4898cfa5e,2023-04-06T10:36:17.635Z,General Question,v2,what is a reasonable maximum size for a buffer?,,, +63c648335f004bf4898cfa5e,2023-04-25T20:21:04.946Z,General Question,v2,whats the difference between open and fopen? ,,, +63c648355f004bf4898cfa72,2023-02-11T04:31:45.386Z,General Question,v1,"HOW do I find the number of items in an array +",,, +63c648365f004bf4898cfa7c,2023-01-27T13:52:30.532Z,Explain Code,v1,,"void fib(int **fib_sequence, int count) { + fib_sequence = malloc(sizeof(int*) * count); + + for (int i = 0; i max_size){ + num_large += 1; + } + + } +",, +63c648395f004bf4898cfa95,2023-01-28T21:14:25.445Z,Question from Code,v1,why does this give a warning,"strtol(argv, Null, 10); +",, +63c648395f004bf4898cfa95,2023-01-28T21:14:51.322Z,Question from Code,v1,why does this give a warning about argv?,"strtol(argv, NULL, 10); +",, +63c648395f004bf4898cfa95,2023-01-28T21:17:37.777Z,Question from Code,v1,"suppose we run this code with ""345"" as argv[1]. why does it print ""3450""?"," int max_size = strtol(argv[1], NULL, 10); + printf(""%d"", max_size); + int num_large = 0; +",, +63c648395f004bf4898cfa95,2023-01-28T23:15:26.479Z,Question from Code,v1,why doesn't this compile,"int main(int argc, char** argv){ + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + printf(""line19\n""); + + // initialize program variables + int max_size = (int)strtol(argv[1], NULL, 10); + int num_large = 0; + int num_large_permissions = 0; + int total; + int done; + + // initialize req_permissions + char *req_permissions = NULL; + if(argv[2] != NULL){ + for (int i = 0; i < 9; i++){ + req_permissions[i] = argv[2][i]; + } + } + + // discard total data + done = scanf(""%*s %d %*c"", &total); + printf(""line38\n""); + printf(""done %d"", done); + + // iterate over lines + while (done == 1){ + + printf(""initializing size, permissions); + int size; + char *permissions = NULL; + printf(""about to scan""); + done = scanf(""%*c %s %*s %*s %*s %d %*s %*s %*s %*s"", permissions, &size); + printf(""while loop\n""); + if ((done == 1) && (size > max_size)){ + num_large += 1; + if((argc == 2) && (check_permissions(req_permissions, permissions) == 0)){ + num_large -= 1; + } + } + + } + + printf(""%d"", num_large); + return 0; + +} +",, +63c648395f004bf4898cfa95,2023-01-28T23:16:40.919Z,Question from Code,v1,why doesn't this compile?,"#include +#include + +// check_permissions +int check_permissions(char *req_permissions, char *permissions){ + for(int i = 0; i < 9; i++){ + if((req_permissions[i] != 13) && (permissions[i] != req_permissions[i])){return 0;} + } + return 1; +} + +// main method +int main(int argc, char** argv){ + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + printf(""line19\n""); + + // initialize program variables + int max_size = (int)strtol(argv[1], NULL, 10); + int num_large = 0; + int num_large_permissions = 0; + int total; + int done; + + // initialize req_permissions + char *req_permissions = NULL; + if(argv[2] != NULL){ + for (int i = 0; i < 9; i++){ + req_permissions[i] = argv[2][i]; + } + } + + // discard total data + done = scanf(""%*s %d %*c"", &total); + printf(""line38\n""); + printf(""done %d"", done); + + // iterate over lines + while (done == 1){ + + printf(""initializing size, permissions); + int size; + char *permissions = NULL; + printf(""about to scan""); + done = scanf(""%*c %s %*s %*s %*s %d %*s %*s %*s %*s"", permissions, &size); + printf(""while loop\n""); + if ((done == 1) && (size > max_size)){ + num_large += 1; + if((argc == 2) && (check_permissions(req_permissions, permissions) == 0)){ + num_large -= 1; + } + } + + } + + printf(""%d"", num_large); + return 0; + +} +",, +63c648395f004bf4898cfa95,2023-01-30T20:12:46.891Z,Question from Code,v1,don't lines 2 and 3 not actually change s1?,"char s1[22] = ""Cat got your tongue?""; +char s2[2] = ""\0""; +strncat(s1, s2, 1); +printf(""%s"", s1);",, +63c648395f004bf4898cfa95,2023-02-12T22:05:29.034Z,Help Fix Code,v1,, if((User *new_user = malloc(sizeof(User))) == NULL){return 0;},"initialize a User struct on the heap and pass the address to new_user. If there is no memory available, return 0.", +63c6483a5f004bf4898cfa9a,2023-04-25T19:55:34.222Z,General Question,v2,multiple process can use the same port to listen for connections,,, +63c6483b5f004bf4898cfaa9,2023-01-27T05:21:08.693Z,Help Fix Code,v1,,"void fib(int **seq, int n){ + int *sequence = malloc(sizeof(int*) * n); + *seq = sequence; + + *seq[0] = 0; + *seq[1] = 1; + int i = 2; + while (i<=n){ + *seq[i] =*seq[i-2] + *seq[i-1]; + + } +}",update seq, +63c6483b5f004bf4898cfaa9,2023-01-30T23:04:54.140Z,Help Fix Code,v1,,"int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + // + + int limit; + limit = strtol(argv[1], NULL, 10); + + + + char *permission[9]; + // scanf(""%10s"", permission); + permission = &argv[2]; + + char* files; + scanf(""%32s"", files); + + + + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + + + int return_value = 0; + scanf(""%s"", files); + while (files[0] != 'E' ){ + if (files[0] != 'd' ){ + char *leftover; + int size_file = strtol(files, &leftover, 10); + char *file_permission[9]; + file_permission = &files[1]; + if (check_permissions(*file_permission,*permission) != 1 && size_file < limit ){ + return_value ++; + } + } + scanf(""%s"", files); + } + printf(""%d"", return_value); + return 0; +}",input permission given from command arguments, +63c6483b5f004bf4898cfaa9,2023-01-30T23:09:35.285Z,Help Fix Code,v1,,"int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + // + + int limit; + limit = strtol(argv[1], NULL, 10); + + + + char *permission[9]; + // scanf(""%10s"", permission); + permission = argv[2]; + + char* files[32]; + scanf(""%32s"", *files); + + + + // TODO: Call check_permissions as part of your solution to count the files to + // compute and print the correct value. + + + int return_value = 0; + scanf(""%s"", *files); + while (files[0] != 'E' ){ + if (files[0] != 'd' ){ + char *leftover; + int size_file = strtol(files, &leftover, 10); + char *file_permission[9]; + file_permission = files[1]; + if (check_permissions(*file_permission,*permission) != 1 && size_file < limit ){ + return_value ++; + } + } + scanf(""%s"", *files); + } + printf(""%d"", return_value); + return 0; +}",assign permission based on command line input, +63c6483b5f004bf4898cfaa9,2023-03-17T21:36:10.663Z,General Question,v2,write to file integers 1 to 100 in binary form,,, +63c6483b5f004bf4898cfaa9,2023-03-17T21:58:37.521Z,General Question,v2,random(100),,, +63c6483b5f004bf4898cfaa9,2023-03-17T22:04:56.298Z,General Question,v2,seek to a random location in the file,,, +63c6483b5f004bf4898cfaa9,2023-03-17T22:06:17.635Z,Help Write Code,v2,,,,seek to a random location in the file +63c6483b5f004bf4898cfaa9,2023-03-17T22:07:26.850Z,General Question,v2,seek to a random location in the file,,, +63c6483b5f004bf4898cfaa9,2023-03-17T22:11:00.894Z,General Question,v2,seek to a random location in the file and print its value,,, +63c6483b5f004bf4898cfaa9,2023-03-17T22:12:50.637Z,General Question,v2,"Now, use sigactionLinks to an external site. to add a signal handler to your program. Start with something simple that just prints a message to standard out and exits (with termination code 0) when it receives a SIGPROF. Then, to test it, run your program and use kill to send it a SIGPROF signal from the shell in another terminal window. (Check the man page for killLinks to an external site. to see how to get a listing of signals and their numbers.) + +",,, +63c6483b5f004bf4898cfaa9,2023-03-17T22:14:34.716Z,General Question,v2,SIGPROF() usage,,, +63c6483b5f004bf4898cfaa9,2023-03-17T22:17:11.823Z,General Question,v2,sigaction() usage,,, +63c6483b5f004bf4898cfaa9,2023-03-17T22:19:01.060Z,General Question,v2,"Now, use sigactionLinks to an external site. to add a signal handler to your program. Start with something simple that just prints a message to standard out and exits (with termination code 0) when it receives a SIGPROF.",,, +63c6483b5f004bf4898cfaa9,2023-03-17T22:24:40.663Z,General Question,v2,set field sa_handler of sa to signal_handler,,, +63c6483b5f004bf4898cfaa9,2023-03-31T21:03:42.603Z,Help Write Code,v2,,,,test malloc fail or not +63c6483b5f004bf4898cfaa9,2023-03-31T21:16:28.573Z,Help Fix Code,v2,access the username attribute of users[fd] which is the fd th user in a list of sturcts," char* echo_msg = ""%s:%s"", users[fd].username, buf;",, +63c6483b5f004bf4898cfaa9,2023-03-31T21:22:18.503Z,Help Write Code,v2,,,,"concatenate username, and message with "":"" in the middle and sent it through a socket" +63c6483c5f004bf4898cfaae,2023-01-20T19:15:34.729Z,General Question,v1,how does scans() work?,,, +63c6483c5f004bf4898cfaae,2023-01-20T19:16:36.094Z,General Question,v1,how could I end scanf() when it is at the end of the file?,,, +63c6483c5f004bf4898cfaae,2023-01-20T19:18:15.782Z,Explain Code,v1,,"int main(int argc, char **argv) { + // Read in the command-line arguments and convert the strings to doubles + double principal = strtod(argv[1], NULL); + double rate = strtod(argv[2], NULL); + + // Call invest to make you more money + invest(&principal, rate); + + printf(""%.2f\n"", principal); + return 0; +} +",, +63c6483c5f004bf4898cfaae,2023-01-26T20:07:37.861Z,Help Fix Code,v1,,"void fib(int **sequence, int n){ + // allocate space + int **sequence = malloc(sizeof(int *)); + int sequence[0] = malloc(sizeof(int) * n); + /* set the first two elements */ + sequence[0][0] = 0; + sequence[0][1] = 1; + /* the rest of the sequence */ + for (int i = 2; i < n; i++){ + sequence[0][i] = sequence[0][i-2] + sequence[0][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; +} +","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.", +63c6483c5f004bf4898cfaae,2023-02-10T22:39:15.331Z,General Question,v1,how to check my code pointing to image files?,,, +63c6483c5f004bf4898cfaae,2023-02-14T17:56:23.250Z,General Question,v1,How to cast a type to another in c?,,, +63c6483c5f004bf4898cfaae,2023-02-14T18:47:22.919Z,General Question,v1,expected ‘const char * restrict’ but argument is of type ‘char’,,, +63c6483c5f004bf4898cfaae,2023-02-14T18:51:23.722Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + + // Check whether the given name could fit in the 'name' array + if (strlen(name) + 1 >= MAX_NAME) { + return 2; + } + + // Check if the given name already exists in the list + // Move to the next item in the linked list by 'next' + while ((**user_ptr_add).next != NULL) { + if ((**user_ptr_add).name == name) { + return 1; + } + } + + // Insert the user if the given name is valid + // Copy the name + strncpy((**user_ptr_add).name, name, MAX_NAME - 1); + // Initiate the other elements + (**user_ptr_add).first_post = NULL; + // For the linked list of friends set the first element to NULL + *(**user_ptr_add).friends = NULL; + (**user_ptr_add).next = NULL; + // Return the code if successfully run + return 0; +} +",create a user with type of struct User, +63c6483c5f004bf4898cfaae,2023-03-03T20:33:14.828Z,General Question,v1,how to prevent a child process creating new processes,,, +63c6483c5f004bf4898cfaae,2023-03-03T20:56:59.420Z,General Question,v1,how to use makefile to compile a specific file,,, +63c6483c5f004bf4898cfaae,2023-03-03T22:16:19.669Z,General Question,v1,"how to end a void function +",,, +63c6483c5f004bf4898cfaae,2023-03-03T22:27:58.174Z,General Question,v1,How do we create child process from child process,,, +63c6483c5f004bf4898cfaae,2023-03-10T22:10:05.208Z,Help Fix Code,v2,check if username or password is too long,"// If username is too long + fgets(user_id, MAXLINE, stdin); + if(strlen(user_id) > MAX_PASSWORD + 1) { + printf(NO_USER); + exit(1); + } + + // If password is too long + fgets(password, MAXLINE, stdin); + if(strlen(password) > MAX_PASSWORD + 1) { + printf(NO_USER); + exit(1); + } +",, +63c6483c5f004bf4898cfaae,2023-03-17T19:11:16.557Z,General Question,v2,how to write random numbers to a file,,, +63c6483c5f004bf4898cfaae,2023-03-17T19:25:41.437Z,General Question,v2,how to use -vtu4 flag in od to display contents of a binary file?,,, +63c6483c5f004bf4898cfaae,2023-03-24T19:08:50.075Z,General Question,v2,Could you explain the parameters in read() function?,,, +63c6483c5f004bf4898cfaae,2023-03-24T19:47:02.133Z,General Question,v2,How does memmove() work?,,, +63c6483c5f004bf4898cfaae,2023-03-28T17:51:22.221Z,General Question,v2,How to write a message from stdin to socket?,,, +63c6483c5f004bf4898cfaae,2023-03-28T18:02:08.662Z,General Question,v2,how does a server broadcast messages to its connected clients?,,, +63c6483c5f004bf4898cfaae,2023-03-31T21:52:53.095Z,Help Fix Code,v2,Monitor stdin and the socket using select to avoid blocking on either one.,"fd_set read_fds; + FD_ZERO(&read_fds); + FD_SET(sock_fd, &read_fds); + FD_SET(STDIN_FILENO, &read_fds); + + + // Read input from the user and send it to the server. Echo any output + // received from the server. + while (1) { + +// Read from server + if (FD_ISSET(sock_fd, &read_fds)) { + num_read = read(sock_fd, buf, BUF_SIZE); + } +// Read from stdin + if (FD_ISSET(STDIN_FILENO, &read_fds)) { + num_read = read(STDIN_FILENO, buf, BUF_SIZE); + } + + if (num_read == 0) { + break; + } + buf[num_read] = '\0'; + + /* + * We should really send ""\r\n"" too, so the server can identify partial + * reads, but you are not required to handle partial reads in this lab. + */ + + + if (write(sock_fd, buf, num_read) != num_read) { + perror(""client: write""); + close(sock_fd); + exit(1); + } + + num_read = read(sock_fd, buf, sizeof(buf) - 1); + if (num_read == 0) { + break; + } + buf[num_read] = '\0'; + printf(""[Server] %s"", buf); + } + + close(sock_fd); + return 0; +",, +63c6483c5f004bf4898cfab3,2023-01-18T16:12:04.913Z,General Question,v1,"c_helper.py:148: in _t + self.assertEqual(stdout, expected_stdout) +E AssertionError: '125' != '1\n6\n4161234567\n3' +E - 125 +E + 1 +E 6 +E 4161234567 +E 3 + +what is wrong with the code?",,, +63c6483c5f004bf4898cfab3,2023-01-18T16:18:51.771Z,Help Fix Code,v1,,"#include + + + + + + +int main() { + char str1[11]; + int num; + while(scanf(""%s\n"", str1)==1){ + scanf(""%d"", &num); + if (num==-1){ + printf(""%s\n"", str1); + + }else if (num >=0 && num <=9){ + printf(""%c\n"", str1[num]); + + }else if (num < -1 || num >9){ + printf(""ERROR\n""); + + } + } + return 0; + + +} ", keep asking user for input until input stream is closed, +63c6483c5f004bf4898cfab3,2023-01-18T16:29:28.002Z,Question from Code,v1,"c_helper.py:148: in _t + self.assertEqual(stdout, expected_stdout) +E AssertionError: '1\n2\n5' != '1\n6\n4161234567\n3' +E 1 +E - 2 +E - 5+ 6 +E + 4161234567 +E + 3 + +How to get this test to pass?","#include + + + + + + +int main() { + char str1[11]; + int num; + while(scanf(""%s\n"", str1)==1){ + scanf(""%d"", &num); + if (num==-1){ + printf(""%s\n"", str1); + + }else if (num >=0 && num <=9){ + printf(""%c\n"", str1[num]); + + }else if (num < -1 || num >9){ + printf(""ERROR\n""); + + } + } + return 0; + + +} ",, +63c6483c5f004bf4898cfab3,2023-01-18T16:30:48.853Z,Question from Code,v1,"c_helper.py:148: in _t self.assertEqual(stdout, expected_stdout) E AssertionError: '1\n2\n5' != '1\n6\n4161234567\n3' E 1 E - 2 E - 5+ 6 E + 4161234567 E + 3 + +Why is my code returning the wrong output?","#include + + + + + + +int main() { + char str1[11]; + int num; + while(scanf(""%s\n"", str1)==1){ + scanf(""%d"", &num); + if (num==-1){ + printf(""%s\n"", str1); + + }else if (num >=0 && num <=9){ + printf(""%c\n"", str1[num]); + + }else if (num < -1 || num >9){ + printf(""ERROR\n""); + + } + } + return 0; + + +} ",, +63c6483c5f004bf4898cfab3,2023-01-18T16:36:32.631Z,Help Fix Code,v1,,"#include + + + + + + +int main() { + char str1[11]; + int num; + while(scanf(""%s"", str1)==1){ + scanf(""%d"", &num); + if (num==-1){ + printf(""%s\n"", str1); + + }else if (num >=0 && num <=9){ + printf(""%c\n"", str1[num]); + + }else if (num < -1 || num >9){ + printf(""ERROR\n""); + + } + } + return 0; + + +} ","c_helper.py:148: in _t self.assertEqual(stdout, expected_stdout) E AssertionError: '1\n2\n5' != '1\n6\n4161234567\n3' E 1 E - 2 E - 5+ 6 E + 4161234567 E + 3 Why is my code returning the wrong output?", +63c6483c5f004bf4898cfab3,2023-02-05T21:13:46.862Z,General Question,v1,"why am I getting a segmentation fault? +if (strcmp(curr->name,name)==0){ + prev->next=curr->next; + + //remove user from friend lists + for(int i=0; ifriends[i]; + if(f!=NULL){ + int j =0; + while(f->friends[j]!=NULL && strcmp(f->friends[j]->name,name)!=0){ + j+=1; + } + if (strcmp(f->friends[j]->name,name)==0){ + //shift friends forward to get rid of this user + // while(jfriends[j]=f->friends[j+1]; + // j+=1; + // } + + } + + } + } + free(curr); + return 0; +",,, +63c6483c5f004bf4898cfab3,2023-02-05T21:19:29.218Z,Help Fix Code,v1,," int delete_user(const char *name, User **user_ptr_del) { + User *curr = *user_ptr_del; + User *prev=NULL; + //find the user + while(curr!=NULL && strcmp(curr->name,name)!=0){ + prev=curr; + curr=curr->next; + } + //remove the user +if (strcmp(curr->name,name)==0){ + prev->next=curr->next; + + //remove user from friend lists + for(int i=0; ifriends[i]; + if(f!=NULL){ + int j =0; + while(f->friends[j]!=NULL && strcmp(f->friends[j]->name,name)!=0){ + j+=1; + } + if (strcmp(f->friends[j]->name,name)==0){ + //shift friends forward to get rid of this user + while(jfriends[j]=f->friends[j+1]; + j+=1; + } + + } + + } + } + free(curr); + return 0; +} +return 1; + }",remove the user with name from the user_ptr_del, +63c6483c5f004bf4898cfab3,2023-02-05T21:34:18.558Z,Help Fix Code,v1,," int delete_user(const char *name, User **user_ptr_del) { + User *curr = *user_ptr_del; + User *prev=NULL; + //find the user + while(curr!=NULL && strcmp(curr->name,name)!=0){ + prev=curr; + curr=curr->next; + } + //remove the user +if (strcmp(curr->name,name)==0){ + prev->next=curr->next; + + //remove user from friend lists + for(int i=0; ifriends[i]; + if(f!=NULL){ + int j =0; + while(f->friends[j]!=NULL && strcmp(f->friends[j]->name,name)!=0){ + j+=1; + } + if (strcmp(f->friends[j]->name,name)==0){ + //shift friends forward to get rid of this user + while(jfriends[j]=f->friends[j+1]; + j+=1; + } + + } + + } + } + free(curr); + return 0; +} +return 1; + }",The code is supposed to remove the user with name from the list by setting pointer of the previous user before the user with name to the user after the user with name. It also need to delete all of the user with name from the users friends lists., +63c6483c5f004bf4898cfab3,2023-02-05T21:48:58.969Z,Help Fix Code,v1,," if (strcmp(curr->name,name)==0){ + + //remove user from friend lists + for(int i=0; ifriends[i]; + if(f!=NULL){ + int j =0; + while(f->friends[j]!=NULL && strcmp(f->friends[j]->name,name)!=0){ + j+=1; + } + if (strcmp(f->friends[j]->name,name)==0){ + //shift friends forward to get rid of this user + while(jfriends[j]=f->friends[j+1]; + j+=1; + } + + } + + } + } + prev->next=curr->next; + free(curr); + return 0; +",why is there a seg fault occurring?, +63c6483c5f004bf4898cfab3,2023-02-05T22:17:06.797Z,Help Fix Code,v1,,"User *curr = *user_ptr_del; + User *prev=NULL; + //find the user + while(curr!=NULL && strcmp(curr->name,name)!=0){ + prev=curr; + curr=curr->next; + } + //remove the user + if (strcmp(curr->name,name)==0){ + + //remove user from friend lists + for(int i=0; ifriends[i]; + if(f!=NULL){ + for(int j=0;jfriends[i]; + if(f!=NULL){ + for(int j=0;jfriends[j]!=NULL){ + } + }",why is there a set fault?, +63c6483c5f004bf4898cfab3,2023-02-12T16:28:54.706Z,Question from Code,v1,"What will this declaration line become after the program has passed +through the C pre-processor?","suppose we have the following line at the top of our program + +#define MAXNAME = 32; + +and then the declaration + +char name[MAXNAME]; + +in the program. ",, +63c6483c5f004bf4898cfab3,2023-02-24T01:24:31.478Z,General Question,v1,How to parse file with fgets and fscanf?,,, +63c6483c5f004bf4898cfab3,2023-02-25T03:15:18.971Z,Help Write Code,v1,,,,"how to write a function that will parse a Makefile and separate lines into actions, target, dependencies into linked list structures?" +63c6483c5f004bf4898cfab3,2023-03-08T15:12:02.381Z,Question from Code,v1,what does tt.tv_sec refer to when struct stat st refers to a file?,"struct timespec tt; +struct stat st; + +tt.tv_sec=st.st_mtime;",, +63c6483c5f004bf4898cfab3,2023-03-09T17:00:55.825Z,Help Fix Code,v2,Why is this giving an error error: member reference base type '__darwin_time_t' (aka 'long') is not a structure or union,"#include +#include +#include +#include +#include +#include +#include + +int main(){ + + + +struct stat sd; +//struct timespec td; + +char *path=malloc(9); +strncpy(path, ""file.txt"",8); +path[8]='\0'; +stat(path, &sd); +//td.tv_sec=sd.st_mtime; +printf(""%lu.%lu\n"",sd.st_mtime, sd.st_mtime.tv_nsec); +return 0; +}",, +63c6483e5f004bf4898cfac7,2023-01-18T20:09:39.816Z,Explain Code,v1,,"int main(int argc, char **argv) { + int size = argc - 1; + int *score_card[size]; + + for (int i = 0; i < size; i++) { + // NOTE: We haven't covered malloc yet, so don't worry about this line. + score_card[i] = malloc(sizeof(int)); + *(score_card[i]) = strtol(argv[i + 1], NULL, 10); + } + + printf(""Sum: %d\n"", sum_card(score_card, size)); + + return 0; +}",, +63c6483e5f004bf4898cfac7,2023-01-18T20:37:37.240Z,General Question,v1,What determines the return for scanf()?,,, +63c6483e5f004bf4898cfac7,2023-01-29T19:01:54.730Z,General Question,v1,Why is strcpy() unsafe?,,, +63c6483f5f004bf4898cfacc,2023-01-18T01:27:49.083Z,General Question,v1,what is wrong with my code,,, +63c6483f5f004bf4898cfacc,2023-01-18T01:29:17.226Z,Help Fix Code,v1,,"int main() { + // These variables have been assigned hidden values: + int secret; + int *another_secret; + + // Declare a variable named secret_pt and make it point to secret: + int *secret_pt = &secret; + // Add the value pointed to by another_secret to the + // value pointed to by secret_pt. + // Do not change the address assigned to secret_pt, and don't explicitly set secret. + secret_pt = secret_pt + *another_secret; + return 0; +}","Add the value pointed to by another_secret to the +value pointed to by secret_pt. Do not change the address assigned to secret_pt, and don't explicitly set secret.", +63c6483f5f004bf4898cfacc,2023-01-18T01:56:03.306Z,Explain Code,v1,,"int main() { + + int i = 81; + int *pt = &i; + int **pt_ptr = &pt; + + int *r= *pt_ptr; + int k = *r; + + // We don't actually need the intermediate value r. + // We can dereference pt_ptr twice like this. + int k1 = **pt_ptr; + + // We can even have triple pointers. + int ***pt_ptr_ptr = &pt_ptr; + int k2 = ***pt_ptr_ptr; + return 0; +}",, +63c6483f5f004bf4898cfacc,2023-01-18T01:57:15.800Z,General Question,v1,"on line 7 why does *r = *pt_ptr and not **pt_ptr +",,, +63c6483f5f004bf4898cfacc,2023-01-18T02:06:04.148Z,Explain Code,v1,," int x = 4; + int *y = &x; + int **z = &y; + + int q;",, +63c6483f5f004bf4898cfacc,2023-01-19T20:09:27.913Z,Explain Code,v1,,"#include +#include + +int main() { + int allocated_amount; + + // Set the value of allocated_amount: + + + return 0; +}",, +63c6483f5f004bf4898cfacc,2023-01-19T20:10:18.944Z,General Question,v1,How to set the allocated_amount variable to the exact amount of space that should be allocated?,,, +63c6483f5f004bf4898cfacc,2023-01-19T20:11:09.151Z,Help Fix Code,v1,,"#include +#include + +int main() { + int allocated_amount; + + // Set the value of allocated_amount: + allocated_amount = malloc(sizeof(int)); + + return 0; +}",why doesn't this work?, +63c6483f5f004bf4898cfacc,2023-01-19T20:11:34.808Z,Help Fix Code,v1,,"#include +#include + +int main() { + int allocated_amount; + + // Set the value of allocated_amount: + *allocated_amount = malloc(sizeof(int)); + + return 0; +}",, +63c6483f5f004bf4898cfacc,2023-01-19T20:13:11.658Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + float *rainfall; + float rain_today; + // rainfall has been dynamically allocated space for a floating point number. + // Both rainfall and rain_today have been initialized in hidden code. + // Assign the amount in rain_today to the space rainfall points to. + + return 0; +}",, +63c648415f004bf4898cfadb,2023-01-19T23:05:12.030Z,General Question,v1,How to check when standard input is closed using scanf?,,, +63c648415f004bf4898cfadb,2023-01-27T11:43:28.585Z,Question from Code,v1,Why does this code give a segmentation fault?,"#include +#include + +/* + * 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_ptr, int n){ + *fib_ptr = malloc(sizeof (int) * n); + if (n == 1){ + *fib_ptr[0] = 0; + } + else if (n == 2){ + *fib_ptr[0] = 0; + *fib_ptr[1] = 1; + } + else{ + *fib_ptr[0] = 0; + *fib_ptr[1] = 1; + for (int i = 2; i < n; i++){ + *fib_ptr[i] = *fib_ptr[i - 2] + *fib_ptr[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; +} + +",, +63c648415f004bf4898cfadb,2023-01-27T18:03:29.109Z,General Question,v1,How to use strtol?,,, +63c648415f004bf4898cfadb,2023-01-27T18:06:53.191Z,General Question,v1,How to use argc and argv?,,, +63c648415f004bf4898cfadb,2023-01-27T18:09:56.401Z,General Question,v1,How to get the program arguments without the program name using argv?,,, +63c648415f004bf4898cfadb,2023-02-05T23:32:38.973Z,General Question,v1,what information does ls -l give?,,, +63c648415f004bf4898cfadb,2023-03-06T00:58:05.355Z,General Question,v1,"how to use fseek? + +",,, +63c648415f004bf4898cfae0,2023-02-08T23:06:22.048Z,General Question,v1,what does fseek do?,,, +63c648445f004bf4898cfaf9,2023-02-28T03:19:04.303Z,Question from Code,v1,Is this a string literal or a character array?,"char *colour = ""Blue"";",, +63c648445f004bf4898cfaf9,2023-02-28T03:20:03.796Z,Question from Code,v1,Is this a string literal or a character array?,"char colour[] = ""Blue"";",, +63c648445f004bf4898cfaf9,2023-02-28T15:16:11.560Z,General Question,v1,what is the difference between char[] and char *?,,, +63c648445f004bf4898cfaf9,2023-02-28T15:24:55.507Z,General Question,v1,"When using gdb, will the execution stop before or after a breakpoint?",,, +63c648445f004bf4898cfaf9,2023-03-10T03:15:36.978Z,General Question,v2,why do file descriptors never have to be initialized?,,, +63c648445f004bf4898cfaf9,2023-03-10T04:22:36.823Z,Help Write Code,v2,,,,show me how to write a switch statement in c +63c648445f004bf4898cfaf9,2023-03-14T22:32:52.766Z,General Question,v2,what does strtok return?,,, +63c648445f004bf4898cfaf9,2023-03-17T15:39:23.564Z,General Question,v2,How do I pass a timespec struct to a function?,,, +63c648445f004bf4898cfaf9,2023-03-17T16:13:01.161Z,General Question,v2,how do I generate random numbers from 0 to 99 with random()?,,, +63c648445f004bf4898cfaf9,2023-03-17T17:24:25.375Z,General Question,v2,How do I error check fwrite?,,, +63c648445f004bf4898cfaf9,2023-04-03T13:46:31.227Z,General Question,v2,how can I use asprintf?,,, +63c648445f004bf4898cfaf9,2023-04-03T14:19:04.957Z,General Question,v2,how to use snprintf?,,, +63c648445f004bf4898cfaf9,2023-04-03T20:32:05.223Z,General Question,v2,does snprintf always add a null terminating character?,,, +63c648445f004bf4898cfaf9,2023-04-04T23:23:48.438Z,General Question,v2,"Once a file descriptor is closed, can it be reused?",,, +63c648445f004bf4898cfaf9,2023-04-04T23:28:51.241Z,Question from Code,v2,Does this declare two pointers to integers or a pointer and an int?,"int *a, b;",, +63c648445f004bf4898cfaf9,2023-04-04T23:33:12.250Z,General Question,v2,what happens if free() is called on NULL?,,, +63c648445f004bf4898cfaf9,2023-04-05T03:03:56.675Z,General Question,v2,"when select is called, do disconnected clients show up in the modified set?",,, +63c648445f004bf4898cfaf9,2023-04-05T13:35:10.706Z,General Question,v2,"if a write call is cut off, could the client be disconnected?",,, +63c648445f004bf4898cfaf9,2023-04-06T02:27:21.828Z,General Question,v2,What are the ways to know if a client has been disconnected?,,, +63c648445f004bf4898cfaf9,2023-04-06T15:06:17.357Z,General Question,v2,What should a server do if a write call to a client does not write the expected number of bytes?,,, +63c648445f004bf4898cfaf9,2023-04-12T14:40:16.225Z,General Question,v2,does strcpy check that s2 is null terminated?,,, +63c648445f004bf4898cfaf9,2023-04-12T14:42:59.098Z,General Question,v2,"for stcpy, what if s1 is not null terminated?",,, +63c648445f004bf4898cfaf9,2023-04-12T14:56:18.773Z,Question from Code,v2,what if there is no room for the null-terminator in s1 after adding s2?,"strcat(s1, s2, n);",, +63c648445f004bf4898cfaf9,2023-04-12T14:56:54.535Z,Question from Code,v2,what if there is no room for the null-terminator in s1 after adding s2?,"strncat(s1, s2, n);",, +63c648445f004bf4898cfaf9,2023-04-22T20:27:53.405Z,General Question,v2,how does sigprocmask know which sigaction struct to mask signals for?,,, +63c648445f004bf4898cfaf9,2023-04-22T21:28:30.980Z,General Question,v2,why do signal handlers print to standard error instead of standard out?,,, +63c648445f004bf4898cfaf9,2023-04-25T02:08:28.505Z,General Question,v2,a server may read and write to multiple clients on the same socket,,, +63c648445f004bf4898cfaf9,2023-04-25T02:14:59.923Z,General Question,v2,what is sigchild signal,,, +63c648445f004bf4898cfaf9,2023-04-25T02:28:54.866Z,General Question,v2,what is calloc?,,, +63c648445f004bf4898cfaf9,2023-04-25T02:30:03.727Z,General Question,v2,what is the difference between runtime and compile time errors,,, +63c648455f004bf4898cfafe,2023-01-18T00:55:58.967Z,Help Fix Code,v1,,"#include +#include + +/* + * Write a small program that takes 1 standard inputs, a phone number and repeatedly asks for a a number + * the program will stop asking for an input when standard input stream is closed + * if the number is between 0 and 9 print the corresponding index + * if the number is -1 print the whole number + * if the number is less then -1 or greater then 9 print error and return 1 + */ + +int checkNum(char phone[], int num){ + if (num < -1 || num > 9){ + printf(""ERROR\n""); + return 1; + +} + if (num == -1){ + for (int i = 0; i < 10; i++) { + printf(""%c"", phone[i]); +} + printf(""\n""); + return 0; +} + else { + for (int i = 0; i < 10; i++) { + if (i == num){ + printf(""%c\n"", phone[i]); + +} +} + return 0; +} + +} + + +int main() { + char phone[11]; + int num = 0; + int funcRes = 0; + + int res = scanf(""%s %d"", phone, &num); + + funcRes = checkNum(phone, num); + + if (funcRes == 1){ + return 1; +} + +while(res != EOF){ + res = scanf(""%d"", &num); + + if (res == EOF){ + break; +} + + funcRes = checkNum(phone, num); + + if (funcRes == 1){ + return 1; +} + + +} + return 0; +} + +",, +63c648455f004bf4898cfafe,2023-01-18T00:56:44.378Z,Explain Code,v1,,"#include +#include + +/* + * Write a small program that takes 1 standard inputs, a phone number and repeatedly asks for a a number + * the program will stop asking for an input when standard input stream is closed + * if the number is between 0 and 9 print the corresponding index + * if the number is -1 print the whole number + * if the number is less then -1 or greater then 9 print error and return 1 + */ + +int checkNum(char phone[], int num){ + if (num < -1 || num > 9){ + printf(""ERROR\n""); + return 1; + +} + if (num == -1){ + for (int i = 0; i < 10; i++) { + printf(""%c"", phone[i]); +} + printf(""\n""); + return 0; +} + else { + for (int i = 0; i < 10; i++) { + if (i == num){ + printf(""%c\n"", phone[i]); + +} +} + return 0; +} + +} + + +int main() { + char phone[11]; + int num = 0; + int funcRes = 0; + + int res = scanf(""%s %d"", phone, &num); + + funcRes = checkNum(phone, num); + + if (funcRes == 1){ + return 1; +} + +while(res != EOF){ + res = scanf(""%d"", &num); + + if (res == EOF){ + break; +} + + funcRes = checkNum(phone, num); + + if (funcRes == 1){ + return 1; +} + + +} + return 0; +} + +",, +63c648455f004bf4898cfafe,2023-01-18T00:57:23.643Z,Explain Code,v1,,"#include +#include + +/* + * Write a small program that takes 1 standard inputs, a phone number and repeatedly asks for a a number + * the program will stop asking for an input when standard input stream is closed + * if the number is between 0 and 9 print the corresponding index + * if the number is -1 print the whole number + * if the number is less then -1 or greater then 9 print error and return 1 + */ + +int checkNum(char phone[], int num){ + if (num < -1 || num > 9){ + printf(""ERROR\n""); + return 1; + +} + if (num == -1){ + for (int i = 0; i < 10; i++) { + printf(""%c"", phone[i]); +} + printf(""\n""); + return 0; +} + else { + for (int i = 0; i < 10; i++) { + if (i == num){ + printf(""%c\n"", phone[i]); + +} +} + return 0; +} + +} + + +int main() { + char phone[11]; + int num = 0; + int funcRes = 0; + + int res = scanf(""%s %d"", phone, &num); + + funcRes = checkNum(phone, num); + + if (funcRes == 1){ + return 1; +} + +while(res != EOF){ + res = scanf(""%d"", &num); + + if (res == EOF){ + break; +} + + funcRes = checkNum(phone, num); + + if (funcRes == 1){ + return 1; +} + + +} + return 0; +} + +",, +63c648455f004bf4898cfafe,2023-01-18T00:57:49.221Z,General Question,v1,when will scanf return EOF,,, +63c648455f004bf4898cfafe,2023-01-18T00:58:30.126Z,General Question,v1,how do I know when standard input is closed,,, +63c648455f004bf4898cfafe,2023-01-18T00:59:09.774Z,Question from Code,v1,why does my code have an infinite loop?,"#include +#include + +/* + * Write a small program that takes 1 standard inputs, a phone number and repeatedly asks for a a number + * the program will stop asking for an input when standard input stream is closed + * if the number is between 0 and 9 print the corresponding index + * if the number is -1 print the whole number + * if the number is less then -1 or greater then 9 print error and return 1 + */ + +int checkNum(char phone[], int num){ + if (num < -1 || num > 9){ + printf(""ERROR\n""); + return 1; + +} + if (num == -1){ + for (int i = 0; i < 10; i++) { + printf(""%c"", phone[i]); +} + printf(""\n""); + return 0; +} + else { + for (int i = 0; i < 10; i++) { + if (i == num){ + printf(""%c\n"", phone[i]); + +} +} + return 0; +} + +} + + +int main() { + char phone[11]; + int num = 0; + int funcRes = 0; + + int res = scanf(""%s %d"", phone, &num); + + funcRes = checkNum(phone, num); + + if (funcRes == 1){ + return 1; +} + +while(res != EOF){ + scanf(""%d"", &num); + + if (res == EOF){ + break; +} + + funcRes = checkNum(phone, num); + + if (funcRes == 1){ + return 1; +} + + +} + return 0; +} + +",, +63c648455f004bf4898cfafe,2023-01-18T01:03:14.948Z,Help Write Code,v1,,,,print each character of a phone number from a standard input +63c648455f004bf4898cfafe,2023-01-21T21:36:45.650Z,Help Fix Code,v1,,"int check_sin(int *sin_array) { + + int luhn_order[9] = {1,2,1,2,1,2,1,2,1}; + int sum = 0; + int temp; + + for(int i = 0; i < 9; i ++){ + + temp = sin_array[i] * luhn_order[i]; + + if (temp < 10){ + sum += temp; + printf(""%d\n"", temp); + } + else { + printf(""%d\n"", temp); + sum += temp % 10; + temp /= 10; + printf(""%d\n"", temp); + sum += temp % 10; + } + + } + + if(sum % 10 == 0){ + return 0; + } + + return 1; +}",to multiply each element in sin_array by each element in luhn_order and sum up all the numbers, +63c648455f004bf4898cfafe,2023-02-15T03:04:49.289Z,Help Fix Code,v1,,"/* Write the copy function to perform exactly as strncpy does, with one + exception: your copy function will guarantee that dest is always + null-terminated. Capacity is expected to be the number of bytes of + memory allocated to dest. + You shoud read the man page to learn how strncpy works. + + NOTE: You must write this function without using any string functions. + The only function that should depend on string.h is memset. + */ + +char *copy(char *dest, const char *src, int capacity) { + + for(int i = 0; i < capacity; i++){ + dest[i] = src[i]; + } + dest[capacity] = '\0'; + return dest; +}",to copy a string into destination from source, +63c648455f004bf4898cfafe,2023-02-15T03:07:10.593Z,Explain Code,v1,," for(int i = 0; i < capacity; i++){ + + if (src[i] == '\0') + { + dest[i] = '\0'; + } + else{ + dest[i] = src[i]; + } + } + dest[capacity] = '\0'; + return dest;",, +63c648455f004bf4898cfafe,2023-02-15T03:09:52.470Z,Help Fix Code,v1,," for(int i = 0; i < capacity; i++){ + + if (src[i] == '\0') + { + dest[i] = '\0'; + } + else{ + dest[i] = src[i]; + } + } + dest[capacity] = '\0'; + return dest;",copy a string from src to dest, +63c648455f004bf4898cfafe,2023-02-15T03:21:46.429Z,Help Fix Code,v1,," for(int i = 0; i < capacity; i++){ + + if (src[i] == '\0') + { + dest[i] = '\0'; + break; + } + else{ + dest[i] = src[i]; + } + } + return dest;",copy a string from src to dest, +63c648455f004bf4898cfafe,2023-03-14T03:16:32.214Z,Question from Code,v2,I am unable to access target_stat.st how do I access it,"struct stat target_stat, dep_stat; +time_t t1, t2; + +if (stat(target, &target_stat) == -1 || stat(dependency, &dep_stat) == -1) { + return 1; +} +t1 = target_stat.st_mtime; + +if (target_stat.st_mtime.tv_sec < dep_stat.st_mtimespec.tv_sec || + (target_stat.st_mtimespec.tv_sec == dep_stat.st_mtimespec.tv_sec && target_stat.st_mtimespec.tv_nsec < dep_stat.st_mtimespec.tv_nsec)) { + return 1; +} else { + return 0; +}",, +63c648465f004bf4898cfb08,2023-01-27T15:52:08.990Z,Explain Code,v1,,"int main(int argc, char **argv) { + + if (argc == 1) { + printf(""Usage: %s NUM\n"", argv[0]); + return 1; + } + + // Convert the first argument to an integer. + int n = strtol(argv[1], NULL, 10); + + for (int i = 0; i < n; i++) { + printf(""%d"", i); + } + return 0; +} ",, +63c648465f004bf4898cfb08,2023-01-27T15:53:10.942Z,Question from Code,v1,"what do the arguments to strtol() in line 9 represent? +","int main(int argc, char **argv) { + + if (argc == 1) { + printf(""Usage: %s NUM\n"", argv[0]); + return 1; + } + + // Convert the first argument to an integer. + int n = strtol(argv[1], NULL, 10); + + for (int i = 0; i < n; i++) { + printf(""%d"", i); + } + return 0; +} ",, +63c648475f004bf4898cfb0d,2023-01-17T20:09:21.264Z,General Question,v1,How do I declare a for loop,,, +63c648475f004bf4898cfb0d,2023-01-17T20:56:30.115Z,Help Write Code,v1,,,,"Add one +then multiply by 5" +63c648475f004bf4898cfb0d,2023-01-19T05:55:53.281Z,General Question,v1,"scanf syntax in c +",,, +63c648475f004bf4898cfb0d,2023-01-19T06:01:38.722Z,General Question,v1,generate a detailed documentation of `scanf()` with usage examples and explanations,,, +63c648475f004bf4898cfb0d,2023-01-19T06:07:58.787Z,Question from Code,v1,Why would this return an errpr?,"printf(""$c"", phone[num]);",, +63c648475f004bf4898cfb0d,2023-01-24T06:45:22.072Z,Question from Code,v1,"What does this code do? +","#include +#include +int main() { + char **last_names; + // Assign a dynamically allocated char * array of length 4 to last_names. + // Then, allocate a character array of length 20 for each element of the array + // pointed to by last_names. + last_names = malloc(4*sizeof(char)); + last_names[0] = malloc(20*sizeof(char)); + last_names[1] = malloc(20*sizeof(char)); + last_names[2] = malloc(20*sizeof(char)); + last_names[3] = malloc(20*sizeof(char)); + return 0; +}",, +63c648475f004bf4898cfb0d,2023-01-24T06:47:04.846Z,Help Fix Code,v1,,"#include +#include +int main() { + char **last_names; + // Assign a dynamically allocated char * array of length 4 to last_names. + // Then, allocate a character array of length 20 for each element of the array + // pointed to by last_names. + return 0; +}"," // Assign a dynamically allocated char * array of length 4 to last_names. + // Then, allocate a character array of length 20 for each element of the array + // pointed to by last_names.", +63c648475f004bf4898cfb0d,2023-01-24T06:47:56.566Z,Help Fix Code,v1,,"#include +#include +int main() { + char **last_names; + // Assign a dynamically allocated char * array of length 4 to last_names. + // Then, allocate a character array of length 20 for each element of the array + // pointed to by last_names. + return 0; +}",, +63c648475f004bf4898cfb0d,2023-01-24T06:48:49.206Z,Help Write Code,v1,,,,"Assign a dynamically allocated char * array of length 4 to last_names. + Then, allocate a character array of length 20 for each element of the array pointed to by last_names." +63c648475f004bf4898cfb0d,2023-01-25T02:32:59.641Z,Help Fix Code,v1,,"void fib(int **fib_sequence, int count) { + + *fib_sequence = malloc(5 * sizeof(int)); + + //set the first value (n always >=1) + *fib_sequence[0] = 0; + + //set the second value if count is >=2 + if (count >= 2) { + *fib_sequence[1] = 1; + } + + + //loop on the remaining elements needed. + for(int i = 2;i < count; i++){ + + *fib_sequence[i] = *fib_sequence[i-1]+*fib_sequence[i-2]; + + } + +}",Set fib_sequence to refer to an array of the Fibonacci sequence., +63c648475f004bf4898cfb0d,2023-01-25T02:53:12.475Z,General Question,v1,"array declaration syntax +",,, +63c648475f004bf4898cfb0d,2023-01-25T02:53:41.783Z,Question from Code,v1,Why does this give an error?,"int **split_array(const int *s, int length) { + + int even[length]; + +}",, +63c648475f004bf4898cfb0d,2023-02-02T19:43:02.493Z,General Question,v1,"How do i print a char +",,, +63c648475f004bf4898cfb0d,2023-02-13T08:22:30.344Z,Question from Code,v1,What will the code look like after preprocessor,"#define MAXNAME = 32; + +and then the declaration + +char name[MAXNAME];",, +63c648475f004bf4898cfb0d,2023-03-15T05:30:24.790Z,Help Fix Code,v2,set each dependency->rule equal to a rule.,"void create_dep(char *line, Rule *rule){ + + Dependency *dependency; + + strtok(line, "" \n""); //returns the target name + strtok(NULL, "" \n""); //returns the colon + + char *curr_tok = strtok(NULL, "" \n""); //returns the first dependency name OR NULL if none + + while(curr_tok != NULL){ + + dependency = malloc(sizeof(Dependency)); + + dependency->next_dep = NULL; + dependency->rule = initialize_rule(curr_tok); + curr_tok = strtok(NULL, "" \n""); + dependency = dependency->next_dep; + + }",, +63c648475f004bf4898cfb0d,2023-03-15T05:32:23.830Z,Question from Code,v2,Does this set the dependency linked list to be a rule in the heap or is it a local variable.,"void create_dep(char *line, Rule *rule) { + Dependency *dependency; + + strtok(line, "" \n""); // returns the target name + strtok(NULL, "" \n""); // returns the colon + + char *curr_tok = strtok(NULL, "" \n""); // returns the first dependency name OR NULL if none + + while (curr_tok != NULL) { + dependency = malloc(sizeof(Dependency)); + + dependency->next_dep = NULL; + dependency->rule = initialize_rule(curr_tok); + curr_tok = strtok(NULL, "" \n""); + dependency = dependency->next_dep; + }",, +63c648475f004bf4898cfb0d,2023-03-15T05:36:51.389Z,Question from Code,v2,Does this set the dependency->next to a pointer in the heap or is it local?,"typedef struct dep_node { + struct rule_node *rule; + struct dep_node *next_dep; +} Dependency; + +void create_dep(char *line, Rule *rule) { + Dependency *dependency; + + strtok(line, "" \n""); // returns the target name + strtok(NULL, "" \n""); // returns the colon + + char *curr_tok = strtok(NULL, "" \n""); // returns the first dependency name OR NULL if none + + while (curr_tok != NULL) { + dependency = malloc(sizeof(Dependency)); + + dependency->next_dep = NULL; + dependency->rule = initialize_rule(curr_tok); + curr_tok = strtok(NULL, "" \n""); + dependency = dependency->next_dep; + }",, +63c648475f004bf4898cfb0d,2023-03-15T05:51:12.168Z,Question from Code,v2,Does this set the dependency->next to a pointer in the heap or is it local?,"typedef struct dep_node { + struct rule_node *rule; + struct dep_node *next_dep; +} Dependency; + +void create_dep(char *line, Rule *rule) { + Dependency *front = NULL; + Dependency *curr_dep; + Dependency *last_dep; + + strtok(line, "" \n""); // returns the target name + strtok(NULL, "" \n""); // returns the colon + + char *curr_tok = strtok(NULL, "" \n""); // returns the first dependency name OR NULL if none + + if (curr_tok != NULL) { + front->rule = initialize_rule(curr_tok); + last_dep = front; + front->next_dep = NULL; + } + + curr_tok = strtok(NULL, "" \n""); + + while (curr_tok != NULL) { // loop through all the dependencies. + + curr_dep = malloc(sizeof(Dependency)); + + // initialize the new dependency values + curr_dep->next_dep = NULL; + curr_dep->rule = initialize_rule(curr_tok); + + // set the last dependencies next_dep to be the newly created one and move last dependency over + last_dep->next_dep = curr_dep; + last_dep = curr_dep; + + // move to next token + curr_tok = strtok(NULL, "" \n""); + } + + rule->dependencies = front; +}",, +63c648475f004bf4898cfb0d,2023-03-15T05:52:58.921Z,Explain Code,v2,,"typedef struct dep_node { + struct rule_node *rule; + struct dep_node *next_dep; +} Dependency; + +void create_dep(char *line, Rule *rule) { + Dependency *front = NULL; + Dependency *curr_dep; + Dependency *last_dep; + + strtok(line, "" \n""); // returns the target name + strtok(NULL, "" \n""); // returns the colon + + char *curr_tok = strtok(NULL, "" \n""); // returns the first dependency name OR NULL if none + + if (curr_tok != NULL) { + front->rule = initialize_rule(curr_tok); + last_dep = front; + front->next_dep = NULL; + } + + curr_tok = strtok(NULL, "" \n""); + + while (curr_tok != NULL) { // loop through all the dependencies. + + curr_dep = malloc(sizeof(Dependency)); + + // initialize the new dependency values + curr_dep->next_dep = NULL; + curr_dep->rule = initialize_rule(curr_tok); + + // set the last dependencies next_dep to be the newly created one and move last dependency over + last_dep->next_dep = curr_dep; + last_dep = curr_dep; + + // move to next token + curr_tok = strtok(NULL, "" \n""); + } + + rule->dependencies = front; +}",, +63c648475f004bf4898cfb0d,2023-03-15T08:56:59.304Z,General Question,v2,how can i count the number of tokens in a string,,, +63c648475f004bf4898cfb0d,2023-04-05T05:45:55.253Z,Help Fix Code,v2,"Gets input from fd file descriptor and returns a single string regardless of the input size from the fd. +The string ends when there is a newline.","int find_network_newline(const char *buf, int n) { + + //loop through n characters + for(int i = 0;i 0) { + //add the amount of bytes just read. + inbuf += nbytes; + printf(""%d, %c\n"", inbuf, buf[inbuf-1]); + //Determine if we have gained a full line. + if ((where = find_network_newline(buf, inbuf)) > 0) { + // where is now the index into buf immediately after + // the first network newline + //now we must terminate the buf. + buf[where-1] = '\0'; + printf(""HELLO %s\n"", buf); + return NULL; + + } + // Step 5: update after and room, in preparation for the next read. + after = &buf[inbuf]; //inbuf+1 not needed because inbuf is size not index** + room = size-inbuf; + printf(""%d \n"", room); + //double buf size* + //we must do dynamic allocation within this function for that to work + //dont forget to free the old memory + //we will use memset + if(room <= 0){ + size = 2*size; + buf = realloc(buf, size); + room = size - inbuf; + } + + } + printf(""oops, %s"", buf); + //no end of line?? + return NULL; + +}",, +63c648475f004bf4898cfb0d,2023-04-05T05:49:24.341Z,Help Fix Code,v2,The function should return a string which dynamically changes size according to input from fd. It ends when it receives a newline.,"/* +* returns a pointer to a string of the client's (from this fd) entire input. (up to \r\n). +*/ +char *read_input(int fd){ + + int size = 2; + + int nbytes; + // Receive messages + char *buf = malloc(size); + int inbuf = 0; // How many bytes currently in buffer? + int room = size; // How many bytes remaining in buffer? + char *after = buf; // Pointer to position after the data in buf + int where; + + while ((nbytes = read(fd, after, room)) > 0) { + //add the amount of bytes just read. + inbuf += nbytes; + printf(""%d, %c\n"", inbuf, buf[inbuf-1]); + //Determine if we have gained a full line. + if ((where = find_network_newline(buf, inbuf)) > 0) { + // where is now the index into buf immediately after + // the first network newline + //now we must terminate the buf. + buf[where-1] = '\0'; + printf(""HELLO %s\n"", buf); + return NULL; + + } + // Step 5: update after and room, in preparation for the next read. + after = &buf[inbuf]; //inbuf+1 not needed because inbuf is size not index** + room = size-inbuf; + printf(""%d \n"", room); + //double buf size* + //we must do dynamic allocation within this function for that to work + //dont forget to free the old memory + //we will use memset + if(room <= 0){ + size = 2*size; + buf = realloc(buf, size); + room = size - inbuf; + } + + } + printf(""oops, %s"", buf); + //no end of line?? + return NULL; + +}",, +63c648495f004bf4898cfb21,2023-01-25T18:14:41.825Z,Question from Code,v1,"When is memory allocated for variables? Where is memory allocated for each variable? For each variable, when is the memory deallocated?","void fun(int **i) { + *i = malloc(sizeof(int)*7); +} +int main() { + int *i; + fun(&i); + free(i); +}",, +63c648495f004bf4898cfb21,2023-01-25T18:18:36.355Z,General Question,v1,What are then ways I can ensure a value persists beyond the function it is called in?,,, +63c648495f004bf4898cfb21,2023-01-25T23:10:35.674Z,Help Fix Code,v1,,"#include +#include + +int fib(int **sequence, int n){ + int i; //index + int j = 0; // term n-1 + int k = 1; // term n + *sequence = malloc(n * sizeof(int)); + for (i = 0; i < n; i++){ + *sequence[i] = k; + k += j; + j = k - j; + } + return 0; +} + +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; +} +",d, +63c648495f004bf4898cfb21,2023-01-25T23:12:45.741Z,Question from Code,v1,why is there a segmentation fault?,"#include +#include + +int fib(int **sequence, int n){ + int i; //index + int j = 0; // term n-1 + int k = 1; // term n + *sequence = malloc(n * sizeof(int)); + for (i = 0; i < n; i++){ + *sequence[i] = k; + k += j; + j = k - j; + } + return 0; +} + +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; +} +",, +63c648495f004bf4898cfb21,2023-01-30T21:13:44.042Z,Question from Code,v1,"what syntax error did i make? +","struct node{ + int value; + struct node *next; +}",, +63c648495f004bf4898cfb21,2023-01-30T21:16:23.780Z,Help Write Code,v1,,,,where should i put the semicolon i am missing +63c648495f004bf4898cfb21,2023-02-10T22:03:48.605Z,Question from Code,v1,"am i creating my structs correctly from lines 14-28? +","struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **ptr = malloc(sizeof(struct pixel*) * height); + if (ptr == NULL){ + fprintf(stderr, ""Error: Failed Malloc()""); + } + + for (int i = 0; i < height; i++){ + ptr[i] = malloc(3 * width); //sizeof(struct pixel) == 3 + if (ptr[i] == NULL){ + fprintf(stderr, ""Error: Failed Malloc()""); + } + } + + int error = 0; + fseek(image, pixel_array_offset, SEEK_SET); + for (int i = 0; i < height; i++){ + for (int j = 0; j < width; j++){ + error = 0; + struct pixel p; + error += fread(p.blue, 1, 1, image); + error += fread(p.green, 1, 1, image); + error += fread(p.red, 1, 1, image); + ptr[i][j] = p; + if (error != 3){ + fprintf(stderr, ""Error: Failed fread.""); + } + } + } + + return ptr; +}",, +63c648495f004bf4898cfb21,2023-02-10T22:04:28.468Z,Question from Code,v1,is line 19 creating a new struct every time?,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **ptr = malloc(sizeof(struct pixel*) * height); + if (ptr == NULL){ + fprintf(stderr, ""Error: Failed Malloc()""); + } + + for (int i = 0; i < height; i++){ + ptr[i] = malloc(3 * width); //sizeof(struct pixel) == 3 + if (ptr[i] == NULL){ + fprintf(stderr, ""Error: Failed Malloc()""); + } + } + + int error = 0; + fseek(image, pixel_array_offset, SEEK_SET); + for (int i = 0; i < height; i++){ + for (int j = 0; j < width; j++){ + error = 0; + struct pixel p; + error += fread(p.blue, 1, 1, image); + error += fread(p.green, 1, 1, image); + error += fread(p.red, 1, 1, image); + ptr[i][j] = p; + if (error != 3){ + fprintf(stderr, ""Error: Failed fread.""); + } + } + } + + return ptr; +}",, +63c648495f004bf4898cfb21,2023-02-10T22:09:58.468Z,Question from Code,v1,"why am I getting a warning that says ""passing argument 1 of 'fread' makes pointer from integer without a cast""? +","struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel **ptr = malloc(sizeof(struct pixel*) * height); + if (ptr == NULL){ + fprintf(stderr, ""Error: Failed Malloc()""); + } + + for (int i = 0; i < height; i++){ + ptr[i] = malloc(3 * width); //sizeof(struct pixel) == 3 + if (ptr[i] == NULL){ + fprintf(stderr, ""Error: Failed Malloc()""); + } + } + + int error = 0; + fseek(image, pixel_array_offset, SEEK_SET); + for (int i = 0; i < height; i++){ + for (int j = 0; j < width; j++){ + error = 0; + struct pixel p; + error += fread(p.blue, 1, 1, image); + error += fread(p.green, 1, 1, image); + error += fread(p.red, 1, 1, image); + ptr[i][j] = p; + if (error != 3){ + fprintf(stderr, ""Error: Failed fread.""); + } + } + } + + return ptr; +}",, +63c648495f004bf4898cfb21,2023-02-14T00:35:47.590Z,General Question,v1,how do i create a struct on the heap?,,, +63c648495f004bf4898cfb21,2023-02-23T15:26:34.120Z,Question from Code,v1,did i use malloc correctly here?,"#include +#include +int main(){ + int **p = malloc(sizeof(int**)); + int x; + *p = &x; + printf(""%d"", **p); +} +",, +63c648495f004bf4898cfb21,2023-02-23T15:29:34.093Z,General Question,v1,"how do i create a dynamically allocated pointer to a pointer to an integer? +",,, +63c648495f004bf4898cfb21,2023-03-12T20:22:20.572Z,General Question,v2,how do i figure out what my computer uses for a line separator?,,, +63c648495f004bf4898cfb21,2023-03-13T15:52:37.443Z,General Question,v2,"how do I check for if a file exists in a directory with C code? +",,, +63c648495f004bf4898cfb21,2023-03-13T18:34:13.138Z,General Question,v2,how do I print an array of strings separated by spaces?,,, +63c648495f004bf4898cfb21,2023-03-13T23:04:56.718Z,General Question,v2,how do I compare 2 struct timespec?,,, +63c648495f004bf4898cfb21,2023-03-14T00:15:46.442Z,General Question,v2,"if target is type struct stat, what type is target.st_mtime?",,, +63c648495f004bf4898cfb21,2023-03-15T22:20:30.143Z,General Question,v2,how can i convert an int to a string?,,, +63c648495f004bf4898cfb21,2023-03-15T22:34:31.282Z,General Question,v2,"does fwrite set perror? +",,, +63c648495f004bf4898cfb21,2023-03-15T23:17:35.603Z,General Question,v2,how do i write integers to a binary file?,,, +63c648495f004bf4898cfb21,2023-04-02T00:12:46.180Z,General Question,v2,how do i type cast char * to char [],,, +63c648495f004bf4898cfb21,2023-04-02T00:14:58.524Z,General Question,v2,how do i malloc a char []?,,, +63c648495f004bf4898cfb21,2023-04-03T20:19:11.568Z,General Question,v2,how do I insert at the beginning of a struct linked list?,,, +63c6484a5f004bf4898cfb26,2023-01-24T00:19:53.241Z,Explain Code,v1,,"#include +#include + +/* + * 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 */ + +int* fib(int** fib_sequence, int count) { + *fib_sequence = malloc(sizeof(int) * count); + for (int i = 0; i < count; i++) { + fib_sequence[i] = malloc(sizeof(int)); + if (i <= 1) { + fib_sequence[i] = 0; + } else { + *fib_sequence[i] = *fib_sequence[i-1] + *fib_sequence[i-2]; + } + } + return *fib_sequence; +} + +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; +} +",, +63c6484a5f004bf4898cfb26,2023-01-24T00:23:58.420Z,Help Fix Code,v1,,"#include +#include + +/* + * 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 */ + +int* fib(int** fib_sequence, int count) { + *fib_sequence = malloc(sizeof(int) * count); + for (int i = 0; i < count; i++) { + fib_sequence[i] = malloc(sizeof(int)); + if (i <= 1) { + fib_sequence[i] = 0; + } else { + *fib_sequence[i] = *fib_sequence[i-1] + *fib_sequence[i-2]; + } + } + return *fib_sequence; +} + +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; +} +",give a list of the first n fibonacci numbers, +63c6484a5f004bf4898cfb26,2023-01-24T00:31:08.148Z,Help Fix Code,v1,,"#include +#include + +/* + * 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 */ + +int* fib(int** fib_sequence, int count) { + *fib_sequence = malloc(sizeof(int*) * count); + for (int i = 0; i < count; i++) { + fib_sequence[i] = malloc(sizeof(int)); + if (i <= 1) { + *fib_sequence[i] = 0; + } else { + *fib_sequence[i] = *fib_sequence[i-1] + *fib_sequence[i-2]; + } + } + return *fib_sequence; +} + +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; +} +",, +63c6484b5f004bf4898cfb30,2023-02-09T00:06:28.757Z,Help Fix Code,v1,,"#include +#include +#include ""bitmap.h"" + + +/* + * 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) { + + // initialize heap-allocated array + struct pixel** array = malloc(height * sizeof(struct pixel*)); + + // set initial pointer position in image file + fseek(image, pixel_array_offset, SEEK_SET); + + for (int i = 0; i < height; i++) { + struct pixel* row = malloc(width * 3); + array[i] = row; + + for (int j = 0; j < width; j++) { + fread(row[j].blue, 1, 1, image); + fseek(image, 1, SEEK_CUR); + + fread(row[j].green, 1, 1, image); + fseek(image, 1, SEEK_CUR); + + fread(row[j].red, 1, 1, image); + fseek(image, 1, SEEK_CUR); + } + } + return array; +} + +","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. + */", +63c6484b5f004bf4898cfb35,2023-01-17T20:49:48.408Z,Question from Code,v1,what is the type of x?,x = 1;,, +63c6484b5f004bf4898cfb35,2023-01-17T20:59:56.994Z,Question from Code,v1,What are the types of the arguments of the function invest?," + +/* + * NOTE: don't change the main function! + * Sample usage: + * $ gcc -Wall -std=gnu99 -g -o invest invest.c + * $ ./invest 10000 1.05 + * 10500.00 + */ +int main(int argc, char **argv) { + // Read in the command-line arguments and convert the strings to doubles + double principal = strtod(argv[1], NULL); + double rate = strtod(argv[2], NULL); + + // Call invest to make you more money + invest(&principal, rate); + + printf(""%.2f\n"", principal); + return 0; +}",, +63c6484b5f004bf4898cfb35,2023-01-18T00:49:22.194Z,Question from Code,v1,what is the type of test?,int *test[2];,, +63c6484b5f004bf4898cfb35,2023-01-18T02:44:04.960Z,General Question,v1,what are the arguments for scanf?,,, +63c6484b5f004bf4898cfb35,2023-01-18T02:45:00.420Z,General Question,v1,"What does each of the format specifiers %s, %d, %f, %c mean?",,, +63c6484b5f004bf4898cfb35,2023-01-18T03:01:04.392Z,General Question,v1,How do I hold a string of 10 characters from scanf to a variable?,,, +63c6484b5f004bf4898cfb35,2023-01-18T03:02:46.442Z,Help Fix Code,v1,,"int main() { + char phone[11]; + + scanf(""%s"", phone); +}","save the 10 inputted characters from scanf to the array of phone, each spot of the array is one character", +63c6484b5f004bf4898cfb35,2023-01-18T03:03:48.318Z,Help Fix Code,v1,,"int main() { + char phone[11]; + + scanf(""%10+s"", phone); +}","save the 10 inputted characters from scanf to the array of phone, each spot of the array is one character", +63c6484b5f004bf4898cfb35,2023-01-18T03:04:12.487Z,Help Fix Code,v1,,"int main() { + char phone[11]; + + scanf(""%10s"", phone); +}","save the 10 inputted characters from scanf to the array of phone, each spot of the array is one character", +63c6484b5f004bf4898cfb35,2023-01-18T03:10:09.485Z,Help Fix Code,v1,,"int main() { + char phone[11]; + int x; + + scanf(""%10s %d"", phone, &x); + + if (x == -1) { + printf(""%s"", phone); + return 0; + } + else if (x >= 0 && x <= 9) { + printf('%c', *(phone + x)); + return 0; + } + else { + printf(""ERROR""); + return 1; + } +}","print the xth element of phone when x is between 0 and 9, inclusive", +63c6484b5f004bf4898cfb35,2023-01-18T03:16:26.817Z,General Question,v1,"what does it mean to have ""determine the return value of scanf when the input stream is closed""",,, +63c6484b5f004bf4898cfb35,2023-01-18T03:20:09.552Z,General Question,v1,generate a detailed documentation of `EOF` with usage examples and explanations,,, +63c6484b5f004bf4898cfb35,2023-01-18T03:30:06.841Z,General Question,v1,How do we redirect the input to come from a file?,,, +63c6484b5f004bf4898cfb35,2023-01-18T03:32:21.961Z,Help Fix Code,v1,,"int main() { + char phone[11]; + scanf(""%10s"", phone); + int x; + int error = 0; + + scanf(""%d"", &x); + + while (x != EOF) { + if (x == -1) { + printf(""%s\n"", phone); + } + else if (x >= 0 && x <= 9) { + printf(""%c\n"", *(phone + x)); + } + else { + printf(""ERROR\n""); + error = 1; + } + + scanf(""%d"", &x); + } + + return error;",exit the while loop once the input stream is closed, +63c6484b5f004bf4898cfb35,2023-01-21T04:26:26.003Z,Question from Code,v1,what is the type of argv[1],"int main(int argc, char **argv)",, +63c6484b5f004bf4898cfb35,2023-01-21T04:43:28.009Z,General Question,v1,what is the difference between %s and %3s in scanf,,, +63c6484b5f004bf4898cfb35,2023-01-21T04:44:01.185Z,General Question,v1,does %s specifier have a character length limit?,,, +63c6484b5f004bf4898cfb35,2023-01-21T04:44:51.179Z,General Question,v1,"what does scanf(""%1s%9s"") mean",,, +63c6484b5f004bf4898cfb35,2023-01-21T04:46:13.187Z,Question from Code,v1,"I only want to save the %d input to a variable, but skip the %s input. How can I do this?","scanf(""%s %d"")",, +63c6484b5f004bf4898cfb35,2023-01-21T04:47:00.065Z,General Question,v1,"how can I use `scanf(""%s %d"", dummy, &num)` ? do I need to initialize dummy before I call scanf?",,, +63c6484b5f004bf4898cfb35,2023-01-21T04:48:10.669Z,General Question,v1,what does each column of ls -l mean?,,, +63c6484b5f004bf4898cfb35,2023-01-21T05:05:19.532Z,General Question,v1,how to check if a string is empty or not?,,, +63c6484b5f004bf4898cfb35,2023-01-21T05:06:41.639Z,Help Fix Code,v1,,"while (scanf(""%1s%9s%1s %d %s %s %d %3s %d %5s %31s"", skip_str, file_permissions, skip_str, &skip_int, skip_str, skip_str, &file_size, skip_str, &skip_int, skip_str, skip_str, file_name) != EOF) { + if (input_permissions[0] != '\0') { + if (!check_permissions(file_permissions, input_permissions) && file_size > cutoff) { + count++; + } + } + else { + if (file_size > cutoff) { + count++; + } + } + }",Store each the specifier to the variable, +63c6484b5f004bf4898cfb35,2023-01-21T05:16:50.894Z,Help Fix Code,v1,,"char test[2]; + +if (test[0] != '\0')",check if test is an empty string, +63c6484b5f004bf4898cfb35,2023-01-21T05:22:39.146Z,Help Fix Code,v1,,"int main() { + char test[3]; + + if (test[0] == '\0') { + printf(""Hello""); + } + + return 0; +}",print Hello if the string test is empty, +63c6484b5f004bf4898cfb35,2023-01-21T05:24:27.063Z,Question from Code,v1,what value does test[0] hold after declaring," + char test[3];",, +63c6484b5f004bf4898cfb35,2023-01-21T05:24:55.501Z,General Question,v1,what value can I use to compare with an undefined value,,, +63c6484b5f004bf4898cfb35,2023-01-21T05:25:52.066Z,Help Fix Code,v1,,"int main() { + char test[3]; + + if (test == NULL) { + printf(""Hello""); + } + + return 0; +}",print Hello if the string test is empty, +63c6484b5f004bf4898cfb35,2023-01-27T09:15:17.831Z,Question from Code,v1,what is the length of s1 after line 4 is executed?,"char s1[21]; +char s2[50] = ""got your tongue?""; +char s3[10] = ""Cat""; +strncpy(s1, s2, sizeof(s1) - 1); +strncat(s1, s3, sizeof(s1) - strlen(s1) - 1); +printf(""%s"", s1);",, +63c6484b5f004bf4898cfb35,2023-01-27T09:45:34.426Z,General Question,v1,what does typedef mean?,,, +63c6484b5f004bf4898cfb35,2023-01-27T09:52:22.504Z,Question from Code,v1,I have a segmentation fault. Where is that?,"typedef struct node { + int value; + struct node *next; +} Node; + +int main(int argc, char **argv) { + Node *front; // pointer to first node of the list + printf(""%d "", front->value); + Node *next = front->next; + while (next != NULL) { + printf(""%d "", next->value); + next = next->next; + } + return 0; +}",, +63c6484b5f004bf4898cfb35,2023-01-27T09:53:43.134Z,Question from Code,v1,I have a segmentation fault. Where is that,"typedef struct node { + int value; + struct node *next; +} Node; + +int main(int argc, char **argv) { + Node *front; + front->value = 1; + front->next = NULL; + printf(""%d "", front->value); + Node *next = front->next; + while (next != NULL) { + printf(""%d "", next->value); + next = next->next; + } + return 0; +}",, +63c6484b5f004bf4898cfb35,2023-02-02T05:40:26.782Z,General Question,v1,does strcat add a null terminator to the end?,,, +63c6484b5f004bf4898cfb35,2023-02-02T05:41:57.526Z,Question from Code,v1,what happens if n > strlen(s2)? will there be an error?,"strncat(s1, s2, n)",, +63c6484b5f004bf4898cfb35,2023-02-02T05:44:17.171Z,Question from Code,v1,will there be an error?,"strncat(s1, s2, 0)",, +63c6484b5f004bf4898cfb35,2023-02-02T05:45:13.118Z,Question from Code,v1,why is there an illegal instruction?," +int main(int argc, char **argv) { + if (argc != 3) { + fprintf(stderr, ""Usage: greeting message name\n""); + return 1; + } + char greeting[20]; + char *name = argv[2]; + + // Your code goes here + strncpy(greeting, argv[1], 19); + strncat(greeting, "" "", 19 - strlen(greeting)); + + printf(""%s\n"", greeting); + return 0; +}",, +63c6484b5f004bf4898cfb35,2023-02-02T05:46:16.676Z,Help Fix Code,v1,," +int main(int argc, char **argv) { + if (argc != 3) { + fprintf(stderr, ""Usage: greeting message name\n""); + return 1; + } + char greeting[20]; + char *name = argv[2]; + + // Your code goes here + strncpy(greeting, argv[1], 19); + strncat(greeting, "" "", 19 - strlen(greeting)); + + printf(""%s\n"", greeting); + return 0; +}",fix illegal instruction, +63c6484c5f004bf4898cfb3a,2023-02-11T22:32:33.847Z,Help Fix Code,v1,,"struct Node { + int value; + struct Node *next; +};",make a node struct, +63c6484c5f004bf4898cfb3a,2023-02-12T18:04:26.766Z,Question from Code,v1,why am i getting a segmentation fault?,"int create_user(const char *name, User **user_ptr_add) { + + // if username is too long, return 2 + if (strlen(name) >= MAX_NAME) { + return 2; // note: we use >= instead of > since strlen does not include the null terminator in its calculation + } + + User *existing_curr = *user_ptr_add; // setting current element at the start of the user linked list + // iterating through user list to see if the provided nickname already exists + while (existing_curr != NULL) { + if (strcmp(name, existing_curr->name) == 0) { + return 1; // if any user in the linked list has the same name we return 1 + } + } + + User *user = malloc(sizeof(user)); // initializing the user object + if (user == NULL) { + return -1; // malloc failed, terminate the program by returning -1 + } + + // SETTING THE USER OBJECT ATTRIBUTES + + strcpy(user->name, name); + strcpy(user->profile_pic, """"); + user->first_post = NULL; + // for loop to iterate through the friends array to set all its elements to null + int friend_index; + for (friend_index = 0; friend_index < MAX_FRIENDS; friend_index++) { + (user->friends)[friend_index] = NULL; + } + user->next = NULL; + + // ADDING THE USER TO THE LIST + + User *creation_curr = *user_ptr_add; // resetting the current element at the start of the user linked list + + if (creation_curr == NULL) { + *user_ptr_add = user; // if the list is empty, make the user object the first element + } + + while(creation_curr->next != NULL) { + creation_curr = creation_curr->next; // iterate through the list until we find the first empty position + } + creation_curr->next = user; // place the user object into this first empty position + + return 0; + +}",, +63c6484c5f004bf4898cfb3a,2023-02-12T18:08:45.175Z,Question from Code,v1,why am i getting a segmentation fault?,"int create_user(const char *name, User **user_ptr_add) { + + // if username is too long, return 2 + if (strlen(name) >= MAX_NAME) { + return 2; // note: we use >= instead of > since strlen does not include the null terminator in its calculation + } + + User *existing_curr = *user_ptr_add; // setting current element at the start of the user linked list + // iterating through user list to see if the provided nickname already exists + while (existing_curr != NULL) { + if (strcmp(name, existing_curr->name) == 0) { + return 1; // if any user in the linked list has the same name we return 1 + } + } + + User *user = malloc(sizeof(User)); // initializing the user object + if (user == NULL) { + return -1; // malloc failed, terminate the program by returning -1 + } + + // SETTING THE USER OBJECT ATTRIBUTES + + strcpy(user->name, name); + strcpy(user->profile_pic, """"); + user->first_post = NULL; + // for loop to iterate through the friends array to set all its elements to null + int friend_index; + for (friend_index = 0; friend_index < MAX_FRIENDS; friend_index++) { + (user->friends)[friend_index] = NULL; + } + user->next = NULL; + + // ADDING THE USER TO THE LIST + + User *creation_curr = *user_ptr_add; // resetting the current element at the start of the user linked list + + if (creation_curr == NULL) { + *user_ptr_add = user; // if the list is empty, make the user object the first element + } + + while(creation_curr->next != NULL) { + creation_curr = creation_curr->next; // iterate through the list until we find the first empty position + } + creation_curr->next = user; // place the user object into this first empty position + + return 0; + +}",, +63c6484c5f004bf4898cfb3a,2023-02-12T18:12:03.150Z,Question from Code,v1,why is this function crashing after running it twice,"int create_user(const char *name, User **user_ptr_add) { + + // if username is too long, return 2 + if (strlen(name) >= MAX_NAME) { + return 2; // note: we use >= instead of > since strlen does not include the null terminator in its calculation + } + + User *existing_curr = *user_ptr_add; // setting current element at the start of the user linked list + // iterating through user list to see if the provided nickname already exists + while (existing_curr != NULL) { + if (strcmp(name, existing_curr->name) == 0) { + return 1; // if any user in the linked list has the same name we return 1 + } + } + + User *user = malloc(sizeof(User)); // initializing the user object + if (user == NULL) { + return -1; // malloc failed, terminate the program by returning -1 + } + + // SETTING THE USER OBJECT ATTRIBUTES + + strcpy(user->name, name); + strcpy(user->profile_pic, """"); + user->first_post = NULL; + // for loop to iterate through the friends array to set all its elements to null + int friend_index; + for (friend_index = 0; friend_index < MAX_FRIENDS; friend_index++) { + (user->friends)[friend_index] = NULL; + } + user->next = NULL; + + // ADDING THE USER TO THE LIST + + User *creation_curr = *user_ptr_add; // resetting the current element at the start of the user linked list + + if (creation_curr == NULL) { + *user_ptr_add = user; // if the list is empty, make the user object the first element + } + + while(creation_curr->next != NULL) { + creation_curr = creation_curr->next; // iterate through the list until we find the first empty position + } + creation_curr->next = user; // place the user object into this first empty position + + return 0; + +}",, +63c6484c5f004bf4898cfb3a,2023-02-12T18:16:25.709Z,Help Fix Code,v1,,"int create_user(const char *name, User **user_ptr_add) { + + // if username is too long, return 2 + if (strlen(name) >= MAX_NAME) { + return 2; // note: we use >= instead of > since strlen does not include the null terminator in its calculation + } + + User *existing_curr = *user_ptr_add; // setting current element at the start of the user linked list + // iterating through user list to see if the provided nickname already exists + while (existing_curr != NULL) { + if (strcmp(name, existing_curr->name) == 0) { + return 1; // if any user in the linked list has the same name we return 1 + } + } + + User *user = malloc(sizeof(User)); // initializing the user object + if (user == NULL) { + return -1; // malloc failed, terminate the program by returning -1 + } + + // SETTING THE USER OBJECT ATTRIBUTES + + strcpy(user->name, name); + strcpy(user->profile_pic, """"); + user->first_post = NULL; + // for loop to iterate through the friends array to set all its elements to null + int friend_index; + for (friend_index = 0; friend_index < MAX_FRIENDS; friend_index++) { + (user->friends)[friend_index] = NULL; + } + user->next = NULL; + + // ADDING THE USER TO THE LIST + + User *creation_curr = *user_ptr_add; // resetting the current element at the start of the user linked list + + if (creation_curr == NULL) { + *user_ptr_add = user; // if the list is empty, make the user object the first element + } + + while(creation_curr->next != NULL) { + creation_curr = creation_curr->next; // iterate through the list until we find the first empty position + } + creation_curr->next = user; // place the user object into this first empty position + + return 0; + +}","this code is intended to take in a username and a pointer to a linked list. then create a user object check if the username exists in a linked list. If it does not exist in the linked list, create a User object with the username parameter and add it to the end of the linked list parameter", +63c6484c5f004bf4898cfb3a,2023-02-13T01:14:59.778Z,Question from Code,v1,why am i getting a segmentation fault?,"int print_user(const User *user) { + if (user == NULL) { + return 1; // returns 1 if user pointer is null + } + + FILE *profile_picture; + profile_picture = fopen(user->profile_pic, ""r""); + Post *user_posts_curr = user -> first_post; // saves the head of the user's posts linked list + + if (profile_picture != NULL) { + char buffer[100]; + while (fgets(buffer, 100, profile_picture) != NULL) { + printf(""%s"", buffer); // printing chunks of profile picture ascii one by one + } + fclose(profile_picture); + } + + printf(""\nName: %s"" + ""\n------------------------------------------"" + ""\nFriends:\n"", + user->name); + + // looping through friends list to print names + int friend_index; + for (friend_index = 0; friend_index < MAX_FRIENDS; friend_index++) { + if ((user->friends)[friend_index] == NULL) { + continue; // if we come across an empty index in the friends list, skip index and do not print + } + printf(""%s\n"", ((user->friends)[friend_index]->name)); + } + + printf(""------------------------------------------"" + ""\nPosts\n""); + // looping through the user's posts + while (user_posts_curr->next != NULL) { + printf(""From: %s"" + ""\nDate: %s"" + ""\n%s"" + ""\n\n===\n\n"", + user_posts_curr->author, ctime(user_posts_curr->date), user_posts_curr->contents); + // we print each line with a equal barrier ""==="" as long as there is a next post to divide + user_posts_curr = user_posts_curr->next; + } + printf(""From: %s"" + ""\nDate: %s"" + ""\n%s"" + ""\n------------------------------------------"", + user_posts_curr->author, ctime(user_posts_curr->date), user_posts_curr->contents); + // when there is no next post, as seen on sample_output, we close the section off with a full barrier, not ""==="" + + return 0; +}",, +63c6484c5f004bf4898cfb3a,2023-02-13T01:19:10.974Z,Question from Code,v1,why am i getting a segmentation fault?,"int print_user(const User *user) { + if (user == NULL) { + return 1; // returns 1 if user pointer is null + } + + FILE *profile_picture; + profile_picture = fopen(user->profile_pic, ""r""); + Post *user_posts_curr = user -> first_post; // saves the head of the user's posts linked list + + if (profile_picture != NULL) { + char buffer[100]; + while (fgets(buffer, 100, profile_picture) != NULL) { + printf(""%s"", buffer); // printing chunks of profile picture ascii one by one + } + fclose(profile_picture); + } + + printf(""\nName: %s"" + ""\n------------------------------------------"" + ""\nFriends:\n"", + user->name); + + // looping through friends list to print names + int friend_index; + for (friend_index = 0; friend_index < MAX_FRIENDS; friend_index++) { + if ((user->friends)[friend_index] == NULL) { + continue; // if we come across an empty index in the friends list, skip index and do not print + } + printf(""%s\n"", ((user->friends)[friend_index]->name)); + } + + printf(""------------------------------------------"" + ""\nPosts\n""); + // looping through the user's posts + while (user_posts_curr->next != NULL) { + printf(""From: %s"" + ""\nDate: %s"" + ""\n%s"" + ""\n\n===\n\n"", + user_posts_curr->author, ctime(user_posts_curr->date), user_posts_curr->contents); + // we print each line with an equal barrier ""==="" as long as there is a next post to divide + user_posts_curr = user_posts_curr->next; + } + if (user_posts_curr != NULL) { + printf(""From: %s"" + ""\nDate: %s"" + ""\n%s"" + ""\n------------------------------------------"", + user_posts_curr->author, ctime(user_posts_curr->date), user_posts_curr->contents); + // when there is no next post, as seen on sample_output, we close the section off with a full barrier, not ""==="" + } + + return 0; +}",, +63c6484c5f004bf4898cfb3a,2023-02-13T01:23:41.958Z,Question from Code,v1,Why am I getting a segmentation fault?,"int make_friends(const char *name1, const char *name2, User *head) { + // points the user objects with the provided usernames if they exist, NULL otherwise + User *first_user = find_user(name1, head); + User *second_user = find_user(name2, head); + + if ((first_user == NULL) || (second_user == NULL)) { + return 4; // if either user was not found through our search, return 4 + } + + if (strcmp(name1, name2) == 0) { + return 3; // return 3 if the usernames are the same (same user was provided twice) + } + + // the location of a free space in either user's friends list, set to -1 if no free spaces are available + int first_user_empty_index = first_empty_friends_index(first_user); + int second_user_empty_index = first_empty_friends_index(second_user); + + if (first_user_empty_index == -1 || second_user_empty_index == -1) { + return 2; // if either index is -1, there are no empty places in one of their friends lists so return 2 + } + + if (check_friends_helper(first_user, second_user) == 0) { + return 1; // return 1 if the users are already friends + } + + // setting the values at the friends list indexes to the other user in each user object + (first_user->friends)[first_user_empty_index] = first_user; + (second_user->friends)[second_user_empty_index] = second_user; + + return 0; +}",, +63c6484c5f004bf4898cfb3a,2023-02-13T01:25:39.539Z,Question from Code,v1,Why am I getting a segmentation fault?,"/* + * Returns whether the provided pointers to users are in each other's friends lists + * + * Return: + * - 0 if they are in each other's friends lists. + * - 1 if they are not in each other's friends lists. + */ +int check_friends_helper(const User *first_user, const User *second_user) { + // iterating through both friends lists to see if they are already friends + int already_friends_index; + for (already_friends_index = 0; already_friends_index < MAX_FRIENDS; already_friends_index++) { + if ((strcmp(first_user->friends[already_friends_index]->name, second_user->name) == 0) || + (strcmp(second_user->friends[already_friends_index]->name, first_user->name) == 0)) { + return 0; // return 1 if the users are already friends + } + } + return 1; +} + +/* + * Returns the first empty index in the friends list of the user that passed in + * + * Return: + * - first empty index if such an index exists + * - -1 if the friends list has not empty spots + */ +int first_empty_friends_index(const User *user) { + // iterating through both friends lists to see if they are already friends and if both friends have available spots + // in their friends lists + int friend_array_index; + for (friend_array_index = 0; friend_array_index < MAX_FRIENDS; friend_array_index++) { + if (user->friends[friend_array_index] == NULL) { + return friend_array_index; + } + } + return -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) { + // points the user objects with the provided usernames if they exist, NULL otherwise + User *first_user = find_user(name1, head); + User *second_user = find_user(name2, head); + + if ((first_user == NULL) || (second_user == NULL)) { + return 4; // if either user was not found through our search, return 4 + } + + if (strcmp(name1, name2) == 0) { + return 3; // return 3 if the usernames are the same (same user was provided twice) + } + + // the location of a free space in either user's friends list, set to -1 if no free spaces are available + int first_user_empty_index = first_empty_friends_index(first_user); + int second_user_empty_index = first_empty_friends_index(second_user); + + if (first_user_empty_index == -1 || second_user_empty_index == -1) { + return 2; // if either index is -1, there are no empty places in one of their friends lists so return 2 + } + + if (check_friends_helper(first_user, second_user) == 0) { + return 1; // return 1 if the users are already friends + } + + // setting the values at the friends list indexes to the other user in each user object + (first_user->friends)[first_user_empty_index] = first_user; + (second_user->friends)[second_user_empty_index] = second_user; + + return 0; +}",, +63c6484c5f004bf4898cfb3a,2023-02-13T01:28:18.747Z,Question from Code,v1,why am i getting a segmentation fault in the make_friends function?,"/* + * Returns whether the provided pointers to users are in each other's friends lists + * + * Return: + * - 0 if they are in each other's friends lists. + * - 1 if they are not in each other's friends lists. + */ +int check_friends_helper(const User *first_user, const User *second_user) { + // iterating through both friends lists to see if they are already friends + int already_friends_index; + for (already_friends_index = 0; already_friends_index < MAX_FRIENDS; already_friends_index++) { + if ((strcmp(first_user->friends[already_friends_index]->name, second_user->name) == 0) || + (strcmp(second_user->friends[already_friends_index]->name, first_user->name) == 0)) { + return 1; // return 1 if the users are already friends + } + } + return 0; +} + +/* + * Returns the first empty index in the friends list of the user that passed in + * + * Return: + * - first empty index if such an index exists + * - -1 if the friends list has not empty spots + */ +int first_empty_friends_index(const User *user) { + // iterating through both friends lists to see if they are already friends and if both friends have available spots + // in their friends lists + int friend_array_index; + for (friend_array_index = 0; friend_array_index < MAX_FRIENDS; friend_array_index++) { + if (user->friends[friend_array_index] == NULL) { + return friend_array_index; + } + } + return -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) { + // points the user objects with the provided usernames if they exist, NULL otherwise + User *first_user = find_user(name1, head); + User *second_user = find_user(name2, head); + + if ((first_user == NULL) || (second_user == NULL)) { + return 4; // if either user was not found through our search, return 4 + } + + if (strcmp(name1, name2) == 0) { + return 3; // return 3 if the usernames are the same (same user was provided twice) + } + + // the location of a free space in either user's friends list, set to -1 if no free spaces are available + int first_user_empty_index = first_empty_friends_index(first_user); + int second_user_empty_index = first_empty_friends_index(second_user); + + if (first_user_empty_index == -1 || second_user_empty_index == -1) { + return 2; // if either index is -1, there are no empty places in one of their friends lists so return 2 + } + + if (check_friends_helper(first_user, second_user) == 1) { + return 1; // return 1 if the users are already friends + } + + // setting the values at the friends list indexes to the other user in each user object + (first_user->friends)[first_user_empty_index] = first_user; + (second_user->friends)[second_user_empty_index] = second_user; + + return 0; +}",, +63c6484c5f004bf4898cfb3a,2023-02-13T01:30:48.137Z,Help Fix Code,v1,,"/* + * Returns whether the provided pointers to users are in each other's friends lists + * + * Return: + * - 0 if they are in each other's friends lists. + * - 1 if they are not in each other's friends lists. + */ +int check_friends_helper(const User *first_user, const User *second_user) { + // iterating through both friends lists to see if they are already friends + int already_friends_index; + for (already_friends_index = 0; already_friends_index < MAX_FRIENDS; already_friends_index++) { + if ((strcmp(first_user->friends[already_friends_index]->name, second_user->name) == 0) || + (strcmp(second_user->friends[already_friends_index]->name, first_user->name) == 0)) { + return 1; // return 1 if the users are already friends + } + } + return 0; +} + +/* + * Returns the first empty index in the friends list of the user that passed in + * + * Return: + * - first empty index if such an index exists + * - -1 if the friends list has not empty spots + */ +int first_empty_friends_index(const User *user) { + // iterating through both friends lists to see if they are already friends and if both friends have available spots + // in their friends lists + int friend_array_index; + for (friend_array_index = 0; friend_array_index < MAX_FRIENDS; friend_array_index++) { + if (user->friends[friend_array_index] == NULL) { + return friend_array_index; + } + } + return -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) { + // points the user objects with the provided usernames if they exist, NULL otherwise + User *first_user = find_user(name1, head); + User *second_user = find_user(name2, head); + + if ((first_user == NULL) || (second_user == NULL)) { + return 4; // if either user was not found through our search, return 4 + } + + if (strcmp(name1, name2) == 0) { + return 3; // return 3 if the usernames are the same (same user was provided twice) + } + + // the location of a free space in either user's friends list, set to -1 if no free spaces are available + int first_user_empty_index = first_empty_friends_index(first_user); + int second_user_empty_index = first_empty_friends_index(second_user); + + if (first_user_empty_index == -1 || second_user_empty_index == -1) { + return 2; // if either index is -1, there are no empty places in one of their friends lists so return 2 + } + + if (check_friends_helper(first_user, second_user) == 1) { + return 1; // return 1 if the users are already friends + } + + // setting the values at the friends list indexes to the other user in each user object + (first_user->friends)[first_user_empty_index] = first_user; + (second_user->friends)[second_user_empty_index] = second_user; + + return 0; +}",add two user objects to each other's friends attribute whihc is a linked list, +63c6484c5f004bf4898cfb3a,2023-02-13T01:34:40.598Z,Help Fix Code,v1,,"/* + * Returns whether the provided pointers to users are in each other's friends lists + * + * Return: + * - 0 if they are in each other's friends lists. + * - 1 if they are not in each other's friends lists. + */ +int check_friends_helper(const User *first_user, const User *second_user) { + // iterating through both friends lists to see if they are already friends + int already_friends_index; + for (already_friends_index = 0; already_friends_index < MAX_FRIENDS; already_friends_index++) { + if ((strcmp(first_user->friends[already_friends_index]->name, second_user->name) == 0) || + (strcmp(second_user->friends[already_friends_index]->name, first_user->name) == 0)) { + return 1; // return 1 if the users are already friends + } + } + return 0; +} + +/* + * Returns the first empty index in the friends list of the user that passed in + * + * Return: + * - first empty index if such an index exists + * - -1 if the friends list has not empty spots + */ +int first_empty_friends_index(const User *user) { + // iterating through both friends lists to see if they are already friends and if both friends have available spots + // in their friends lists + int friend_array_index; + for (friend_array_index = 0; friend_array_index < MAX_FRIENDS; friend_array_index++) { + if (user->friends[friend_array_index] == NULL) { + return friend_array_index; + } + } + return -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) { + // points the user objects with the provided usernames if they exist, NULL otherwise + User *first_user = find_user(name1, head); + User *second_user = find_user(name2, head); + + if ((first_user == NULL) || (second_user == NULL)) { + return 4; // if either user was not found through our search, return 4 + } + + if (strcmp(name1, name2) == 0) { + return 3; // return 3 if the usernames are the same (same user was provided twice) + } + + // the location of a free space in either user's friends list, set to -1 if no free spaces are available + int first_user_empty_index = first_empty_friends_index(first_user); + int second_user_empty_index = first_empty_friends_index(second_user); + + if (first_user_empty_index == -1 || second_user_empty_index == -1) { + return 2; // if either index is -1, there are no empty places in one of their friends lists so return 2 + } + + if (check_friends_helper(first_user, second_user) == 1) { + return 1; // return 1 if the users are already friends + } + + // setting the values at the friends list indexes to the other user in each user object + (first_user->friends)[first_user_empty_index] = second_user; + (second_user->friends)[second_user_empty_index] = first_user; + + return 0; +}",add two user objects to each other's friends attribute which is a linked list, +63c6484c5f004bf4898cfb3a,2023-02-13T01:37:42.414Z,Help Fix Code,v1,,"/* + * Returns whether the provided pointers to users are in each other's friends lists + * + * Return: + * - 1 if they are in each other's friends lists. + * - 0 if they are not in each other's friends lists. + */ +int check_friends_helper(const User *first_user, const User *second_user) { + // iterating through both friends lists to see if they are already friends + int already_friends_index; + for (already_friends_index = 0; already_friends_index < MAX_FRIENDS; already_friends_index++) { + if ((strcmp(first_user->friends[already_friends_index]->name, second_user->name) == 0) || + (strcmp(second_user->friends[already_friends_index]->name, first_user->name) == 0)) { + return 1; // return 1 if the users are already friends + } + } + return 0; +} + +/* + * Returns the first empty index in the friends list of the user that passed in + * + * Return: + * - first empty index if such an index exists + * - -1 if the friends list has not empty spots + */ +int first_empty_friends_index(const User *user) { + // iterating through both friends lists to see if they are already friends and if both friends have available spots + // in their friends lists + int friend_array_index; + for (friend_array_index = 0; friend_array_index < MAX_FRIENDS; friend_array_index++) { + if (user->friends[friend_array_index] == NULL) { + return friend_array_index; + } + } + return -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) { + // points the user objects with the provided usernames if they exist, NULL otherwise + User *first_user = find_user(name1, head); + User *second_user = find_user(name2, head); + + if ((first_user == NULL) || (second_user == NULL)) { + return 4; // if either user was not found through our search, return 4 + } + + if (strcmp(name1, name2) == 0) { + return 3; // return 3 if the usernames are the same (same user was provided twice) + } + + // the location of a free space in either user's friends list, set to -1 if no free spaces are available + int first_user_empty_index = first_empty_friends_index(first_user); + int second_user_empty_index = first_empty_friends_index(second_user); + + if (first_user_empty_index == -1 || second_user_empty_index == -1) { + return 2; // if either index is -1, there are no empty places in one of their friends lists so return 2 + } + + if (check_friends_helper(first_user, second_user) == 1) { + return 1; // return 1 if the users are already friends + } + + // setting the values at the friends list indexes to the other user in each user object + (first_user->friends)[first_user_empty_index] = second_user; + (second_user->friends)[second_user_empty_index] = first_user; + + return 0; +}",add two user objects to each other's friends attribute which is a linked list, +63c6484c5f004bf4898cfb3a,2023-02-13T01:38:22.211Z,Question from Code,v1,Why am I getting a segmentation fault?,"/* + * Returns whether the provided pointers to users are in each other's friends lists + * + * Return: + * - 1 if they are in each other's friends lists. + * - 0 if they are not in each other's friends lists. + */ +int check_friends_helper(const User *first_user, const User *second_user) { + // iterating through both friends lists to see if they are already friends + int already_friends_index; + for (already_friends_index = 0; already_friends_index < MAX_FRIENDS; already_friends_index++) { + if ((strcmp(first_user->friends[already_friends_index]->name, second_user->name) == 0) || + (strcmp(second_user->friends[already_friends_index]->name, first_user->name) == 0)) { + return 1; // return 1 if the users are already friends + } + } + return 0; +} + +/* + * Returns the first empty index in the friends list of the user that passed in + * + * Return: + * - first empty index if such an index exists + * - -1 if the friends list has not empty spots + */ +int first_empty_friends_index(const User *user) { + // iterating through both friends lists to see if they are already friends and if both friends have available spots + // in their friends lists + int friend_array_index; + for (friend_array_index = 0; friend_array_index < MAX_FRIENDS; friend_array_index++) { + if (user->friends[friend_array_index] == NULL) { + return friend_array_index; + } + } + return -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) { + // points the user objects with the provided usernames if they exist, NULL otherwise + User *first_user = find_user(name1, head); + User *second_user = find_user(name2, head); + + if ((first_user == NULL) || (second_user == NULL)) { + return 4; // if either user was not found through our search, return 4 + } + + if (strcmp(name1, name2) == 0) { + return 3; // return 3 if the usernames are the same (same user was provided twice) + } + + // the location of a free space in either user's friends list, set to -1 if no free spaces are available + int first_user_empty_index = first_empty_friends_index(first_user); + int second_user_empty_index = first_empty_friends_index(second_user); + + if (first_user_empty_index == -1 || second_user_empty_index == -1) { + return 2; // if either index is -1, there are no empty places in one of their friends lists so return 2 + } + + if (check_friends_helper(first_user, second_user) == 1) { + return 1; // return 1 if the users are already friends + } + + // setting the values at the friends list indexes to the other user in each user object + (first_user->friends)[first_user_empty_index] = second_user; + (second_user->friends)[second_user_empty_index] = first_user; + + return 0; +}",, +63c6484c5f004bf4898cfb3a,2023-02-13T03:31:17.880Z,Question from Code,v1,Why am I getting segmentation faults?,"/* + * Returns the first empty index in the friends list of the user that passed in + * + * Return: + * - first empty index if such an index exists + * - -1 if the friends list has not empty spots + */ +int first_empty_friends_index(const User *user) { + // iterating through both friends lists to see if they are already friends and if both friends have available spots + // in their friends lists + int friend_array_index; + for (friend_array_index = 0; friend_array_index < MAX_FRIENDS; friend_array_index++) { + if ((user->friends)[friend_array_index] == NULL) { + return friend_array_index; + } + } + return -1; +} + +/* + * Returns the first empty index in the friends list of the user that passed in + * + * Return: + * - the index of the second user in the first user's friends list + * - -1 if the second user is not in the first user's friends list + */ +int find_friend_index(const User *first_user, const User *second_user) { + // iterating through first user's friends lists to see they are friends with the second user + int friend_index; + for (friend_index = 0; friend_index < MAX_FRIENDS; friend_index++) { + if ((first_user->friends)[friend_index] != NULL) { + if (strcmp(((first_user->friends)[friend_index])->name, second_user->name) == 0) { + return friend_index; // return 1 if the users are already friends + } + } + } + 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 *deleted_user = find_user(name, *user_ptr_del); + if (deleted_user == NULL) { + return 1; + } + + User *curr_user = *user_ptr_del; + User *prev_user = NULL; + + while (curr_user != NULL) { + if (strcmp(name, curr_user->name) == 0) { + // deleting the user from the linked list and freeing up the allocated memory + if (prev_user != NULL) { + // if curr has a previous element, make its previous point to its next + prev_user->next = (curr_user->next)->next; + curr_user = prev_user->next; + } else { + // if curr is the element at the head, make its next element the new head + *user_ptr_del = curr_user->next; + curr_user = *user_ptr_del; + } + + Post *curr_post = curr_user->first_post; + // deleting and freeing the user's posts in memory + while (curr_post != NULL) { + Post *post_copy = curr_post; + curr_post = curr_post->next; + free(post_copy->contents); + free(post_copy->date); + free(post_copy); + } + } else { + int friend_index = find_friend_index(deleted_user, curr_user); + if (friend_index != -1) { // checks if the users are friends + (curr_user->friends)[friend_index] = NULL; + // sets the index old the deleted user in the user's friends list to NULL + } + } + curr_user = curr_user->next; + } + + free(deleted_user); + + return 0; +} +",, +63c6484e5f004bf4898cfb4e,2023-01-19T22:18:05.264Z,Help Fix Code,v1,,"#include + +int main() +{ + char phone[11]; + int code = scanf(""%s"", phone); + int isError = 0; + + while (code != 0) + { + int num; + scanf(""%d"", &num); + if (num == -1) + { + printf(""%s \n"", phone); + } + else if ((0 <= num) && (num <= 9)) + { + printf(""%c \n"", phone[num]); + } + else + { + printf(""ERROR \n""); + isError = 1; + } + } + return isError; +}",keep taking in inputs until standard output is closed, +63c6484e5f004bf4898cfb4e,2023-01-19T22:20:17.126Z,Help Fix Code,v1,,"#include + +int main() +{ + char phone[11]; + int code = scanf(""%s"", phone); + int isError = 0; + + while (code != EOF) + { + int num; + scanf(""%d"", &num); + if (num == -1) + { + printf(""%s \n"", phone); + } + else if ((0 <= num) && (num <= 9)) + { + printf(""%c \n"", phone[num]); + } + else + { + printf(""ERROR \n""); + isError = 1; + } + } + return isError; +}","keep taking in input until standard output is closed +", +63c6484e5f004bf4898cfb4e,2023-01-19T22:52:34.910Z,Help Fix Code,v1,,"#include + +int main() +{ + char phone[11]; + int isError = 0; + + while (scanf(""%s"", phone) != EOF) + { + int num; + scanf(""%d"", &num); + if (num == -1) + { + printf(""%s \n"", phone); + } + else if ((0 <= num) && (num <= 9)) + { + printf(""%c \n"", phone[num]); + } + else + { + printf(""ERROR \n""); + isError = 1; + } + } + return isError; +}",get input once every time until standard output closes, +63c6484e5f004bf4898cfb4e,2023-01-22T19:57:04.209Z,Help Fix Code,v1,,"int main() { + char **last_names; + // Assign a dynamically allocated char * array of length 4 to last_names. + // Then, allocate a character array of length 20 for each element of the array + // pointed to by last_names. + char *array = malloc(sizeof(char) * 4); + last_names = &array; + char *array2 = malloc(sizeof(char) * 20); + return 0 +}","Assign a dynamically allocated char * array of length 4 to last_names. Then, allocate a character array of length 20 for each element of the array pointed to by last_names.", +63c6484e5f004bf4898cfb4e,2023-01-22T20:00:10.983Z,Help Fix Code,v1,,"int main() { + char **last_names; + // Assign a dynamically allocated char * array of length 4 to last_names. + // Then, allocate a character array of length 20 for each element of the array + // pointed to by last_names. + char *array = malloc(sizeof(char*) * 4); + last_names = &array; + for(int i = 0; i < 4; i++){ + char *array2 = malloc(sizeof(char) * 20); + last_names[i] = array2; + } +}","Assign a dynamically allocated char * array of length 4 to last_names. Then, allocate a character array of length 20 for each element of the array pointed to by last_names.", +63c6484e5f004bf4898cfb4e,2023-01-22T20:01:59.277Z,Help Fix Code,v1,,"int main() { + char **last_names; + // Assign a dynamically allocated char * array of length 4 to last_names. + // Then, allocate a character array of length 20 for each element of the array + // pointed to by last_names. + char *array = malloc(sizeof(char*) * 4); + last_names = array; + for(int i = 0; i < 4; i++){ + char array2 = malloc(sizeof(char) * 20); + last_names[i] = array2; + } +}","Assign a dynamically allocated char * array of length 4 to last_names. Then, allocate a character array of length 20 for each element of the array pointed to by last_names.", +63c6484e5f004bf4898cfb4e,2023-01-22T20:22:42.724Z,Help Fix Code,v1,,"int get_sum(const char *input_line) { + char *last; + int first = strtol(input_line, &last, 10); + return first + *last; +}",Return the sum of two integers in the string input_line, +63c6484e5f004bf4898cfb4e,2023-01-25T00:39:27.991Z,Help Fix Code,v1,,"void fib(int **arr, int n) +{ + if (n == 1) + { + *arr[0] = 0; + } + else if (n == 2) + { + *arr[0] = 0; + *arr[1] = 1; + } + else + { + *arr[0] = 0; + *arr[1] = 1; + for (int i = 0; i < n - 2; i++) + { + *arr[i + 2] = *arr[i - 2] + *arr[i - 1]; + } + } +}",Define a function void fib(...) below. This function takes parameter n and generates the first n values in the Fibonacci sequence. 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., +63c6484e5f004bf4898cfb4e,2023-01-25T00:41:30.498Z,Help Fix Code,v1,,"void fib(int **arr, int n) +{ + if (n == 1) + { + *arr[0] = 0; + } + else if (n == 2) + { + *arr[0] = 0; + *arr[1] = 1; + } + else + { + *arr[0] = 0; + *arr[1] = 1; + for (int i = 2; i < n - 2; i++) + { + *arr[i] = *arr[i - 2] + *arr[i - 1]; + } + } +}",Define a function void fib(...) below. This function takes parameter n and generates the first n values in the Fibonacci sequence. 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., +63c6484e5f004bf4898cfb4e,2023-01-25T00:45:07.853Z,Help Fix Code,v1,,"void fib(int **arr, int n) +{ + *arr = malloc(sizeof(int) * n); + if (n == 1) + { + *arr[0] = 0; + } + else if (n == 2) + { + *arr[0] = 0; + *arr[1] = 1; + } + else + { + *arr[0] = 0; + *arr[1] = 1; + for (int i = 2; i < n - 2; i++) + { + *arr[i] = *arr[i - 2] + *arr[i - 1]; + } + } +}",Define a function void fib(...) below. This function takes parameter n and generates the first n values in the Fibonacci sequence. 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., +63c6484e5f004bf4898cfb4e,2023-01-25T00:48:00.164Z,Help Write Code,v1,,,,Define a function void fib(...) below. This function takes parameter n and generates the first n values in the Fibonacci sequence. 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. +63c6484e5f004bf4898cfb4e,2023-01-25T02:30:46.865Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) +{ + int *nums = malloc(sizeof(int) * size); + for (int i = 1; i < size; i++) + { + nums[i - 1] = strtol(strs[i], NULL, 10); + } + return nums",Return a pointer to an array of ints with size elements. strs is an array of strings where each element is the string representation of an integer. size is the size of the array, +63c6484e5f004bf4898cfb4e,2023-01-25T02:31:32.976Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) +{ + int *nums = malloc(sizeof(int) * size); + for (int i = 1; i < size; i++) + { + nums[i - 1] = strtol(strs[i], NULL, 10); + } + return nums: +}",Return a pointer to an array of ints with size elements. strs is an array of strings where each element is the string representation of an integer. size is the size of the array, +63c6484e5f004bf4898cfb4e,2023-01-25T02:31:57.442Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) +{ + int *nums = malloc(sizeof(int) * size); + for (int i = 1; i < size; i++) + { + nums[i - 1] = strtol(strs[i], NULL, 10); + } + return nums; +}",, +63c6484e5f004bf4898cfb4e,2023-01-25T02:32:26.470Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) +{ + int *nums = malloc(sizeof(int) * size); + for (int i = 1; i < size; i++) + { + nums[i - 1] = strtol(strs[i], NULL, 10); + } + return nums; +}",Return a pointer to an array of ints with size elements. strs is an array of strings where each element is the string representation of an integer. size is the size of the array, +63c6484e5f004bf4898cfb4e,2023-01-25T02:34:17.545Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) +{ + int *nums = malloc(sizeof(int*) * size); + for (int i = 1; i < size; i++) + { + nums[i - 1] = strtol(strs[i], NULL, 10); + } + return nums; +}",Return a pointer to an array of ints with size elements. strs is an array of strings where each element is the string representation of an integer. size is the size of the array, +63c6484e5f004bf4898cfb4e,2023-01-25T17:17:26.329Z,Help Fix Code,v1,,"int *build_array(char **strs, int size) +{ + int *nums = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) + { + nums[i] = strtol(strs[i + 1], NULL, 10); + } + return nums; +}",Return a pointer to an array of ints with size elements.strs is an array of strings where each element is the string representation of an integer. size is the size of the array, +63c6484e5f004bf4898cfb4e,2023-02-03T03:55:16.623Z,Help Fix Code,v1,,"int truncate(char *s, int n) +{ + char new_str[n + 1]; + strncpy(new_str, s, n - 1); + new_str[n] = '\0'; + if (strcmp(new_str, s) == 0) + { + int value = strlen(s) - n; + s = &new_str; + return value; + } + return 0; +}","Write a function named truncate() that takes a string s and a non-negative integer n. If s has more than n characters (not including the null terminator), the function should truncate s at n characters and return the number of characters that were removed. If s has n or fewer characters, s is unchanged and the function returns 0.", +63c6484e5f004bf4898cfb4e,2023-02-03T03:56:46.494Z,Help Write Code,v1,,,,"Write a function named truncate() that takes a string s and a non-negative integer n. If s has more than n characters (not including the null terminator), the function should truncate s at n characters and return the number of characters that were removed. If s has n or fewer characters, s is unchanged and the function returns 0." +63c6484e5f004bf4898cfb4e,2023-02-03T03:57:39.863Z,General Question,v1,how to check if the length of the string is greater than n,,, +63c6484e5f004bf4898cfb4e,2023-02-03T03:58:39.716Z,Help Fix Code,v1,,"int truncate(char *s, int n) +{ + if (strlen(s) > n) + { + s[n] = '\0'; + return strlen(s) - n; + } + return 0; +} +","Write a function named truncate() that takes a string s and a non-negative integer n. If s has more than n characters (not including the null terminator), the function should truncate s at n characters and return the number of characters that were removed. If s has n or fewer characters, s is unchanged and the function returns 0.", +63c6484e5f004bf4898cfb4e,2023-02-03T04:09:44.175Z,Help Write Code,v1,,,,remove trailing ? in a string +63c6484e5f004bf4898cfb4e,2023-02-03T04:18:43.529Z,Help Fix Code,v1,,"int strip_q_marks(char *s) +{ + int i = strlen(s); + char q = '?'; + while (strcmp(&s[i], &q) == 0) + { + i--; + } + int value = strlen(s) - i; + strncpy(s, s, i - 1); + s[i] = '\0'; + return value; +}",modify the string to remove any trailing question marks and return the number of question marks that were removed., +63c6484e5f004bf4898cfb4e,2023-02-14T19:34:26.634Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) +{ + return retry_delete(name, user_ptr_del); +} + +void remove_posts(User *curr) +{ + Post *curr_post = (Post *)curr->first_post; + while (curr_post != NULL) + { + curr->first_post = curr_post->next; + free(curr_post->date); + free(curr_post->contents); + free(curr_post); + curr_post = curr->first_post; + } +} + +void remove_from_friends(User *curr, const char *name, User *user_list) +{ + for (int i = 0; i < MAX_FRIENDS; i++) + { + if (curr->friends[i] != NULL && strcmp(curr->friends[i]->name, name) == 0) // if user is a friend of someone else, delete + { + for (int j = i; j < MAX_FRIENDS - 1; j++) + { + if (curr->friends[j + 1] != NULL) + { + curr->friends[j] = curr->friends[j + 1]; // delete and shift everything up + } + else + { + curr->friends[j] = NULL; + // make the last element NULL + } + } + } + } +} + +int retry_delete(const char *name, User **user_ptr_del) +{ + User *user_to_del = find_user(name, *user_ptr_del); + if (user_to_del == NULL) + { + // user was not found + return 1; + } + remove_posts(user_to_del); + remove_from_friends(user_to_del, name, *user_ptr_del); + if (*user_ptr_del == user_to_del) + { + *user_ptr_del = user_to_del->next; + free(user_to_del); + } + else + { + User *prev = *user_ptr_del; + while (prev->next != user_to_del) + { + prev = prev->next; + } + prev->next = user_to_del->next; + free(user_to_del); + } + return 0; +}",delete user for friend's friend array, +63c648515f004bf4898cfb67,2023-02-06T21:41:50.914Z,Explain Code,v1,,"#include +#include + +int main() +{ + while(1) + fork(); + return 0; +}",, +63c648515f004bf4898cfb67,2023-02-10T21:26:09.616Z,Help Fix Code,v1,,"struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel** pixels = malloc(sizeof(struct pixel*) * height); + fseek(image, pixel_array_offset, SEEK_SET); + + for(struct pixel** i = pixels; i < sizeof(struct pixel*) * height + pixels; i += sizeof(struct pixel*)) { + *i = malloc(sizeof(struct pixel) * width); + + unsigned char red, green, blue = 0; + for(struct pixel* j = *i; j < sizeof(struct pixel) * width + *i; j += sizeof(struct pixel)) { + fread(&blue, sizeof(unsigned char), 1, image); + fseek(image, 1, SEEK_CUR); + + fread(&green, sizeof(unsigned char), 1, image); + fseek(image, 1, SEEK_CUR); + + fread(&red, sizeof(unsigned char), 1, image); + fseek(image, 1, SEEK_CUR); + + j->blue = blue; + j->green = green; + j->red = red; + } + } + + return pixels; +}","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. + */", +63c648515f004bf4898cfb6c,2023-01-23T16:20:31.371Z,General Question,v1,write a fibonacci sequence with n elements and save it in array,,, +63c648515f004bf4898cfb6c,2023-01-23T16:26:54.601Z,Explain Code,v1,,"int fib = malloc(sizeof(int) * n)); + +int i = 0 + +for (i = 0; i < n; i++){ + if (i == 1 || i == 2){ + fib[i] = 1; + } + else { + fib[i] = fib[i - 1] + fib[i - 2]; + } +} +",, +63c648525f004bf4898cfb76,2023-01-29T01:35:33.441Z,Question from Code,v1,why am I getting a segmentation fault?,"/* + * Convert a 9 digit int to a 9 element int array. + */ +int populate_array(int sin, int *sin_array) { + if (sin <= 99999999) { + return 1; + } + + int temp_sin = sin; + for (int i = 8; i >= 0; i++) { + sin_array[i] = temp_sin % 10; + temp_sin = temp_sin / 10; + } + return 0; +} + +/* + * Return 0 if the given sin_array is a valid SIN, and 1 otherwise. + */ +int check_sin(int *sin_array) { + int luhn_array[9] = {1, 2, 1, 2, 1, 2, 1, 2, 1}; + int sum = 0; + for (int i = 0; i < 9; i++) { + int product = luhn_array[i] * sin_array[i]; + if (product > 9) { + product = product / 10 + product % 10; + } + sum += product; + } + if (sum % 10 == 0) { + return 0; + } + return 1; +} +",, +63c648525f004bf4898cfb76,2023-02-15T01:12:52.019Z,General Question,v1,how do you use booleans?,,, +63c648525f004bf4898cfb76,2023-02-15T01:16:43.983Z,General Question,v1,how do you use time_t?,,, +63c648525f004bf4898cfb76,2023-03-10T02:18:55.765Z,Question from Code,v1,Why is this code not compiling?,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + char user_id[MAXLINE]; + char password[MAXLINE]; + int status; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + // TODO + + // check length + + // create pipe + int fd[2]; + if (pipe(fd) == -1) { + perror(""pipe""); + exit(1); + } + + // fork + + int result = fork(); + if (result < 0) { + perror(""fork""); + exit(1); + } + + // if child execl, close write, use dup2 to change stdin to read end of pipe + if (result == 0) { + // dup2 + if (close(fd[1]) != 0) { + perror(""pipe""); + exit(1); + } + } else { + if (close(fd[0]) != 0) { + perror(""pipe""); + exit(1); + } + } + + // if parent wait + wait(&status); + + if (WIFEXITED(status)) { + int s = WEXITSTATUS(status); + if (s == 0) { + printf(SUCCESS); + } elif (s == 2) { + printf(INVALID); + } elif (s == 3) { + printf(NO_USER); + } + } + + return 0; +} +",, +63c648525f004bf4898cfb76,2023-03-14T17:48:19.178Z,General Question,v2,how can I use strtok to seperate by a space?,,, +63c648525f004bf4898cfb76,2023-03-14T17:59:28.936Z,Question from Code,v2,Why am I getting compilation errors,"#include +#include +#include + +#include ""pmake.h"" + +Rule *parse_file(FILE *fp) { + Rule *first_rule = NULL; + Rule *curr_rule = NULL; + char line[MAXLINE]; + + // Read and process each line of the given file. + while (fgets(line, MAXLINE, fp) != NULL) { + if (is_comment_or_empty(line) == 0) { + if (line[0] == '\t') { // line is an action + + } else { // line is a target + char *token; + + // Get the target for the rule + token = strtok(line, "" ""); + + // TODO check to see if there is a rule by that target already + // otherwise make a new rule + Rule *new_rule = find_rule(first_rule, token); + + // Rule *new_rule = malloc(sizeof(Rule)); + new_rule->target = malloc(strlen(token) + 1); + strcpy(new_rule->target, token); + + // Get the dependencies + new_rule->dependencies = NULL; // Initialize to NULL + strtok(NULL, "" ""); // Skip the ':' + while ((token = strtok(NULL, "" "")) != NULL) { + // search for an existing rule by target token, otherwise + // make a new one that is empty except for the target and insert it + // after the current node + } + + // Add new rule to the end of the linked list + if (first_rule == NULL) { + first_rule = new_rule; + } else { + // change to add the new rule + curr_rule->next_rule = new_rule; + } + curr_rule = new_rule; + } + } + } + return first_rule; +} + +Rule *find_rule(Rule *head, char *target) { + Rule *curr_rule = head; + while (curr_rule != NULL && strcmp(curr_rule->target, target) != 0) { + curr_rule = curr_rule->next_rule; + } + return curr_rule; +} + +void print_actions(Action *act) { + while (act != NULL) { + if (act->args == NULL) { + fprintf(stderr, ""ERROR: action with NULL args\n""); + act = act->next_act; + continue; + } + printf(""\t""); + + int i = 0; + while (act->args[i] != NULL) { + printf(""%s "", act->args[i]); + i++; + } + printf(""\n""); + act = act->next_act; + } +} + +void print_rules(Rule *rules) { + Rule *cur = rules; + + while (cur != NULL) { + if (cur->dependencies || cur->actions) { + // Print target + printf(""%s : "", cur->target); + + // Print dependencies + Dependency *dep = cur->dependencies; + while (dep != NULL) { + if (dep->rule->target == NULL) { + fprintf(stderr, ""ERROR: dependency with NULL rule\n""); + } + printf(""%s "", dep->rule->target); + dep = dep->next_dep; + } + printf(""\n""); + + // Print actions + print_actions(cur->actions); + } + cur = cur->next_rule; + } +} + +int is_comment_or_empty(const char *line) { + for (int i = 0; i < strlen(line); i++) { + if (line[i] == '#') { + return 1; + } + if (line[i] != '\t' && line[i] != ' ') { + return 0; + } + } + return 1; +} + +char *args_to_string(char **args, char *buffer, int size) { + buffer[0] = '\0'; + int i = 0; + while (args[i] != NULL) { + strncat(buffer, args[i], size - strlen(buffer)); + strncat(buffer, "" "", size - strlen(buffer)); + i++; + } + return buffer; +} +",, +63c648525f004bf4898cfb76,2023-03-14T18:07:56.554Z,General Question,v2,"What does ""implicit declaration of function"" mean",,, +63c648525f004bf4898cfb76,2023-03-14T19:42:34.690Z,General Question,v2,is there a function that returns the number of tokens in a string,,, +63c648525f004bf4898cfb76,2023-03-15T01:59:53.415Z,General Question,v2,what is an example of execvp,,, +63c648525f004bf4898cfb76,2023-03-15T02:50:29.235Z,General Question,v2,how do you use stat?,,, +63c648525f004bf4898cfb76,2023-03-15T03:13:30.896Z,General Question,v2,can a struct be returned from a function?,,, +63c648525f004bf4898cfb76,2023-03-17T15:26:22.887Z,General Question,v2,how do you generate random numbers in a range,,, +63c648525f004bf4898cfb76,2023-04-06T19:15:34.794Z,General Question,v2,can I set default values in a struct,,, +63c648545f004bf4898cfb85,2023-02-10T16:34:53.504Z,Help Fix Code,v1,,"void list_users(const User *curr) { + // WHAT IF CURR IS NULL, DO WE HAVE TO ACCOUNT FOR THAT CASE?? + + // case where curr is no null + while (curr != NULL) { + printf(""%s\n"",curr->name); + curr = curr->next; + } +}","rint the usernames of all users in the list starting at curr. + * Names should be printed to standard output, one per line.", +63c648545f004bf4898cfb85,2023-02-10T17:28:49.980Z,General Question,v1,difference between printf and fprintf,,, +63c648545f004bf4898cfb85,2023-02-10T18:47:24.237Z,General Question,v1,how to write to a file,,, +63c648545f004bf4898cfb85,2023-02-10T20:04:18.534Z,General Question,v1,what is the return value when fread successfully reads a file.,,, +63c648545f004bf4898cfb85,2023-02-10T20:40:09.945Z,General Question,v1,WHAT IS THE RETURN VALUE OF fseek,,, +63c648545f004bf4898cfb85,2023-02-10T20:43:54.276Z,General Question,v1,give me an example of using fseek,,, +63c648545f004bf4898cfb85,2023-02-10T21:56:15.108Z,General Question,v1,"what does this mean ""First, allocate space for m `struct pixel *` values""",,, +63c648555f004bf4898cfb8f,2023-01-27T19:09:16.963Z,General Question,v1,what is a fibonacci sequence,,, +63c648555f004bf4898cfb8f,2023-01-27T19:26:15.176Z,General Question,v1,can you set the value of a pointer in main by a function that is not in main?,,, +63c648555f004bf4898cfb8f,2023-01-27T19:35:16.764Z,General Question,v1,when do we use ** instead of * using pointers,,, +63c648555f004bf4898cfb8f,2023-01-27T19:37:58.706Z,General Question,v1,how to set the value of an array malloc,,, +63c648555f004bf4898cfb8f,2023-01-27T19:38:31.247Z,General Question,v1,how to set the value of the third item in an array malloc,,, +63c648555f004bf4898cfb8f,2023-01-27T19:39:24.970Z,General Question,v1,how to return a void function in c ,,, +63c648555f004bf4898cfb8f,2023-01-27T19:57:30.340Z,General Question,v1,how to convert str to integer in c ,,, +63c648555f004bf4898cfb8f,2023-01-27T20:07:10.482Z,General Question,v1,how to make nested arrays stored in heap in c,,, +63c648585f004bf4898cfba8,2023-01-18T21:35:40.109Z,Question from Code,v1,why is my output delayed by one iteration?,"#include +#include + +int main() { + + char phone[11]; + scanf(""%s\n"", phone); + + int num; + scanf(""%d\n"", &num); + while (scanf("" %d\n"", &num) == 1) { + if (num == -1) { + printf(""%s\n"", phone); + } else if (0 <= num && num <= 9) { + printf(""%c\n"", phone[num]); + } else { + printf(""ERROR\n""); + } + } + return 0; +}",, +63c648585f004bf4898cfba8,2023-01-18T21:37:44.784Z,Question from Code,v1,why is my output delayed by one iteration,"#include +#include + +int main() { + + char phone[11]; + scanf(""%s\n"", phone); + + int num; + scanf(""%d"", &num); + while (scanf(""%d"", &num) == 1) { + if (num == -1) { + printf(""%s\n"", phone); + } else if (0 <= num && num <= 9) { + printf(""%c\n"", phone[num]); + } else { + printf(""ERROR\n""); + } + } + return 0; +}",, +63c648585f004bf4898cfba8,2023-01-23T21:02:02.445Z,Question from Code,v1,why is result[1] containing the wrong output,"#include +#include + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + int *arr1, *arr2; + int **arr_pt = malloc(sizeof(int*) * 2); + + int size = (int)sizeof(int) * (int)(length / 2); + + if (length % 2 == 0) { + arr1 = malloc(size); + arr2 = malloc(size); + printf(""%d\n"", size); + } else { + arr1 = malloc(size + 1); + arr2 = malloc(size); + } + + for (int i = 0; i < size; i++) { + //printf(""%d%d\n"", s[i], s[i+1]); + arr1[i] = s[2 * i]; + arr2[i] = s[2 * i + 1]; + } + + for (int i = 0; i < size; i++) { + printf(""%d%d\n"", arr1[i], arr2[i]); + } + arr_pt[0] = arr1; + arr_pt[1] = arr2; + + return arr_pt; +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int *arr = malloc(sizeof(int) * size - 1); + for (int i = 1; i < size; i++) { + int j = strtol(strs[i], NULL, 10); + arr[i-1] = j; + } + return arr; +} + + +int main(int argc, char **argv) { + /* Replace the comments in the next two lines with the appropriate + arguments. Do not add any additional lines of code to the main + function or make other changes. + */ + int *full_array = build_array(argv, argc); + int **result = split_array(full_array, argc - 1); + + printf(""Original array:\n""); + for (int i = 0; i < argc - 1; i++) { + printf(""%d "", full_array[i]); + } + printf(""\n""); + + printf(""result[0]:\n""); + for (int i = 0; i < argc / 2; i++) { + printf(""%d "", result[0][i]); + } + printf(""\n""); + + printf(""result[1]:\n""); + for (int i = 0; i < (argc - 1) / 2; i++) { + printf(""%d "", result[1][i]); + } + printf(""\n""); + free(full_array); + free(result[0]); + free(result[1]); + free(result); + return 0; +}",, +63c648585f004bf4898cfba8,2023-01-23T21:04:01.733Z,Question from Code,v1,Why am I getting an abort error message,"#include +#include + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + int *arr1, *arr2; + int **arr_pt = malloc(sizeof(int*) * 2); + + int size = (int)sizeof(int) * (int)(length / 2); + + if (length % 2 == 0) { + arr1 = malloc(size); + arr2 = malloc(size); + printf(""%d\n"", size); + } else { + arr1 = malloc(size + 1); + arr2 = malloc(size); + } + + for (int i = 0; i < size; i++) { + //printf(""%d%d\n"", s[i], s[i+1]); + arr1[i] = s[2 * i]; + if (2 * i + 1) { + arr2[i] = s[2 * i + 1]; + } + } + + for (int i = 0; i < size; i++) { + printf(""%d%d\n"", arr1[i], arr2[i]); + } + arr_pt[0] = arr1; + arr_pt[1] = arr2; + + return arr_pt; +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int *arr = malloc(sizeof(int) * size - 1); + for (int i = 1; i < size; i++) { + int j = strtol(strs[i], NULL, 10); + arr[i-1] = j; + } + return arr; +} + + +int main(int argc, char **argv) { + /* Replace the comments in the next two lines with the appropriate + arguments. Do not add any additional lines of code to the main + function or make other changes. + */ + int *full_array = build_array(argv, argc); + int **result = split_array(full_array, argc - 1); + + printf(""Original array:\n""); + for (int i = 0; i < argc - 1; i++) { + printf(""%d "", full_array[i]); + } + printf(""\n""); + + printf(""result[0]:\n""); + for (int i = 0; i < argc / 2; i++) { + printf(""%d "", result[0][i]); + } + printf(""\n""); + + printf(""result[1]:\n""); + for (int i = 0; i < (argc - 1) / 2; i++) { + printf(""%d "", result[1][i]); + } + printf(""\n""); + free(full_array); + free(result[0]); + free(result[1]); + free(result); + return 0; +}",, +63c648585f004bf4898cfba8,2023-01-23T21:05:18.984Z,Question from Code,v1,How can I change the helper functions to stop getting the abort error message?,"#include +#include + +/* Return a pointer to an array of two dynamically allocated arrays of ints. + The first array contains the elements of the input array s that are + at even indices. The second array contains the elements of the input + array s that are at odd indices. + + Do not allocate any more memory than necessary. You are not permitted + to include math.h. You can do the math with modulo arithmetic and integer + division. +*/ +int **split_array(const int *s, int length) { + int *arr1, *arr2; + int **arr_pt = malloc(sizeof(int*) * 2); + + int size = (int)sizeof(int) * (int)(length / 2); + + if (length % 2 == 0) { + arr1 = malloc(size); + arr2 = malloc(size); + printf(""%d\n"", size); + } else { + arr1 = malloc(size + 1); + arr2 = malloc(size); + } + + for (int i = 0; i < size; i++) { + //printf(""%d%d\n"", s[i], s[i+1]); + arr1[i] = s[2 * i]; + if (2 * i + 1) { + arr2[i] = s[2 * i + 1]; + } + } + + for (int i = 0; i < size; i++) { + printf(""%d%d\n"", arr1[i], arr2[i]); + } + arr_pt[0] = arr1; + arr_pt[1] = arr2; + + return arr_pt; +} + +/* Return a pointer to an array of ints with size elements. + - strs is an array of strings where each element is the string + representation of an integer. + - size is the size of the array + */ + +int *build_array(char **strs, int size) { + int *arr = malloc(sizeof(int) * size - 1); + for (int i = 1; i < size; i++) { + int j = strtol(strs[i], NULL, 10); + arr[i-1] = j; + } + return arr; +} + + +int main(int argc, char **argv) { + /* Replace the comments in the next two lines with the appropriate + arguments. Do not add any additional lines of code to the main + function or make other changes. + */ + int *full_array = build_array(argv, argc); + int **result = split_array(full_array, argc - 1); + + printf(""Original array:\n""); + for (int i = 0; i < argc - 1; i++) { + printf(""%d "", full_array[i]); + } + printf(""\n""); + + printf(""result[0]:\n""); + for (int i = 0; i < argc / 2; i++) { + printf(""%d "", result[0][i]); + } + printf(""\n""); + + printf(""result[1]:\n""); + for (int i = 0; i < (argc - 1) / 2; i++) { + printf(""%d "", result[1][i]); + } + printf(""\n""); + free(full_array); + free(result[0]); + free(result[1]); + free(result); + return 0; +}",, +63c648585f004bf4898cfba8,2023-02-01T20:12:42.424Z,General Question,v1,How to write code to remove trailing periods,,, +63c648585f004bf4898cfba8,2023-02-01T20:34:40.250Z,General Question,v1,"does strlen in c count the number of question marks +",,, +63c648585f004bf4898cfba8,2023-02-01T20:35:14.756Z,General Question,v1,why is strlen() not counting the number of question marks,,, +63c648585f004bf4898cfba8,2023-02-08T20:52:33.138Z,Question from Code,v1,Why am I getting a segmentation fault from this section of the code?,"#include +#include +#include ""bitmap.h"" + + +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; + read_bitmap_metadata(image, &pixel_array_offset, &width, &height); + + // Print out metadata. + printf(""Pixel array offset: %d\n"", pixel_array_offset); + printf(""Width: %d\n"", width); + printf(""Height: %d\n"", height); + + // Read in the pixel data + struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height); + + // Print out some pixels from each of the image's corners. + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 5; j++) { + print_pixel(pixels[i][j]); + print_pixel(pixels[i][width - 1 - j]); + print_pixel(pixels[height - 1 - i][j]); + print_pixel(pixels[height - 1 - i][width - 1 - j]); + } + } + + // Clean up: you need to do this! + + return 0; +} + +struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { + struct pixel *arr[height]; + + for (int i = 0; i < height; i++) { + arr[i] = malloc(sizeof(struct pixel) * width); + fseek(image, pixel_array_offset + sizeof(struct pixel) * i, SEEK_SET); + fread(arr[i], sizeof(struct pixel), width, image); + } + + return arr; +}",, +63c648585f004bf4898cfba8,2023-02-14T05:27:52.510Z,Question from Code,v1,Why does deleting a user then adding a user not update properly on friend's list,"#include ""friends.h"" +#include +#include +#include + +/* + * Check if two users are friends + * + * Return: + * - 0 if the users are friends + * - 1 if the users are not friends +*/ +int check_friends(const User *user1, User *user2) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if(user1 -> friends[i] != NULL) { + if (strcmp(user1 -> friends[i] -> name, user2 -> name) == 0) { + return 0; + } + } + } + return 1; +} + +/* + * 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) { + // Check length of name + if (strlen(name) > MAX_NAME - 1) { + return 2; + } + + // Create Space for the user & Initialize Variables + User *new_user = malloc(sizeof(User)); + strcpy(new_user -> name, name); + strcpy(new_user -> profile_pic, """"); + new_user -> first_post = NULL; + for (int i = 0; i < MAX_FRIENDS; i++) { + new_user -> friends[i] = NULL; + } + new_user -> next = NULL; + + + // First User to be created + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + // User(s) already exist + User *curr = *user_ptr_add; + + // printf(""Linked List Excluding Last: ""); + + while (curr -> next != NULL) { + //printf(""%s "", curr -> name); + if (strcmp(curr -> name, name) == 0) { + return 1; + } + curr = curr -> next; + } + + // Check for the final node + if (strcmp(curr -> name, name) == 0) { + return 1; + } + + curr -> next = new_user; + + //printf(""\n%s %s\n"", curr -> name, new_user -> 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) { + // Check to make sure at least one User exists + if ((User *) head == NULL) { + return NULL; + } + + // Find a User with the same name, and return if it exists + User *curr = (User *) head; + while (curr != NULL) { + if (strcmp(curr -> name, 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) { + // Check to make sure at least one User exists + if ((User *) curr == NULL) { + } else { + User *current = (User *) curr; + + // Loop through every User + while (current != NULL) { + printf(""User: %s\n"", current -> name); + printf(""Profile Pic: %s\n"", current -> profile_pic); + printf(""Friends: ""); + for (int i = 0; i < MAX_FRIENDS; i++) { + if (current -> friends[i] != NULL) { + printf(""%s "", current -> friends[i] -> name); + } + } + printf(""\n\n""); + 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) { + // Check filename length + if (strlen(filename) > MAX_NAME - 1) { + return 2; + } + + // Check file existance and workings + FILE *pic = fopen(filename, ""r""); + if (pic == NULL) { + return 1; + } + fclose(pic); + + // Change user profile picture + strncpy(user -> profile_pic, filename, MAX_NAME - 1); + return 0; +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int make_friends(const char *name1, const char *name2, User *head) { + User *person1 = find_user(name1, head); + User *person2 = find_user(name2, head); + + // Check to make sure both users exist + if (person1 == NULL || person2 == NULL) { + return 4; + } + + // Check for the same user passed in twice + if (strcmp(name1, name2) == 0) { + return 3; + } + + int open1 = MAX_FRIENDS; + int open2 = MAX_FRIENDS; + for (int i = MAX_FRIENDS - 1; i >= 0; i--) { + // Check to see if users are already friends + if(person1 -> friends[i] != NULL) { + if (strcmp(person1 -> friends[i] -> name, name2) == 0) { + return 1; + } + } + + // Find first open index of each user's friend list + if (person1 -> friends[i] == NULL) { + open1 = i; + } + if (person2 -> friends[i] == NULL) { + open2 = i; + } + + // Check to ensure there is room in both user's friend lists + if (open1 == MAX_FRIENDS || open2 == MAX_FRIENDS) { + return 2; + } + } + + // Add to list + person1 -> friends[open1] = person2; + person2 -> friends[open2] = person1; + return 0; +} + + +/* + * 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 *pic = fopen(user -> profile_pic, ""r""); + if (pic == NULL) { + } else { + char line_info[100]; + while(fgets(line_info, 100, pic) != NULL) { + printf(""%s\n"", line_info); + } + fclose(pic); + } + + // Print Name + printf(""Name: %s\n"", user -> name); + printf(""------------------------------------------\n""); + + // Print Friends List + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user -> friends[i] != NULL) { + printf(""%s\n"", user -> friends[i] -> name); + } + } + printf(""------------------------------------------\n""); + + // Print Posts + printf(""Posts:\n""); + struct post *curr; + if (user -> first_post != NULL) { + curr = user -> first_post; + } + + while (curr != NULL) { + printf(""From: %s\n"", curr -> author); + printf(""Date: %s\n"", ctime(curr -> date)); + printf(""%s\n"", curr -> contents); + + curr = curr -> next; + + if (curr != NULL) { + printf(""\n===\n\n""); + } + } + + printf(""------------------------------------------\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) { + // Check both exists + if (author == NULL || target == NULL) { + return 2; + } + + // Check users are friends + int friend = check_friends(author, target); + if (friend == 1) { + return 1; + } + + // Create Post + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post -> author, author -> name); + time_t *curtime = malloc(sizeof(time_t)); + time(curtime); + new_post -> date = curtime; + new_post -> contents = contents; + + // Link Post to Post Linked List + if (target -> first_post == NULL) { + target -> first_post = new_post; + } else { + new_post -> next = target ->first_post; + target -> first_post = new_post; + } + + return 0; +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ +int delete_user(const char *name, User **user_ptr_del) { + // Check to ensure user with name exists + if (*user_ptr_del == NULL) { + return 1; + } + + // Check to see if first user is to be deleted + User *user = find_user(name, *user_ptr_del); + if (user == NULL) { + return 1; + } + + User *prev = NULL; + User *curr = *user_ptr_del; + + if (strcmp((*user_ptr_del) -> name, name) == 0) { // Check to see if first user is to be deleted + } else { // Check the rest of the users + while (prev == NULL) { + if (strcmp((curr -> next) -> name, name) == 0) { + prev = curr; + } + curr = curr -> next; + } + } + + // Delete user from friend's list + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = NULL; + if (user -> friends[i] != NULL) { + friend = user -> friends[i]; + + for (int j = 0; j < MAX_FRIENDS; j++) { + if (friend -> friends[i] == user) { + friend -> friends[i] = NULL; + } + } + } + } + + // Free all dynamically allocated memory + Post *curr_post = user -> first_post; + while (curr_post != NULL) { + Post *temp = curr_post; + curr_post = curr_post -> next; + free(temp -> contents); + free(temp -> date); + free(temp); + } + + if (prev == NULL) { + *user_ptr_del = curr -> next; + } else { + prev -> next = curr -> next; + } + free(curr); + + return 0; +} +",, +63c648585f004bf4898cfba8,2023-02-14T05:34:34.491Z,Help Fix Code,v1,,"#include ""friends.h"" +#include +#include +#include + +/* + * Check if two users are friends + * + * Return: + * - 0 if the users are friends + * - 1 if the users are not friends +*/ +int check_friends(const User *user1, User *user2) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if(user1 -> friends[i] != NULL) { + if (strcmp(user1 -> friends[i] -> name, user2 -> name) == 0) { + return 0; + } + } + } + return 1; +} + +/* + * 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) { + // Check length of name + if (strlen(name) > MAX_NAME - 1) { + return 2; + } + + // Create Space for the user & Initialize Variables + User *new_user = malloc(sizeof(User)); + strcpy(new_user -> name, name); + strcpy(new_user -> profile_pic, """"); + new_user -> first_post = NULL; + for (int i = 0; i < MAX_FRIENDS; i++) { + new_user -> friends[i] = NULL; + } + new_user -> next = NULL; + + + // First User to be created + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + // User(s) already exist + User *curr = *user_ptr_add; + + // printf(""Linked List Excluding Last: ""); + + while (curr -> next != NULL) { + //printf(""%s "", curr -> name); + if (strcmp(curr -> name, name) == 0) { + return 1; + } + curr = curr -> next; + } + + // Check for the final node + if (strcmp(curr -> name, name) == 0) { + return 1; + } + + curr -> next = new_user; + + //printf(""\n%s %s\n"", curr -> name, new_user -> 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) { + // Check to make sure at least one User exists + if ((User *) head == NULL) { + return NULL; + } + + // Find a User with the same name, and return if it exists + User *curr = (User *) head; + while (curr != NULL) { + if (strcmp(curr -> name, 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) { + // Check to make sure at least one User exists + if ((User *) curr == NULL) { + } else { + User *current = (User *) curr; + + // Loop through every User + while (current != NULL) { + printf(""User: %s\n"", current -> name); + printf(""Profile Pic: %s\n"", current -> profile_pic); + printf(""Friends: ""); + for (int i = 0; i < MAX_FRIENDS; i++) { + if (current -> friends[i] != NULL) { + printf(""%s "", current -> friends[i] -> name); + } + } + printf(""\n\n""); + 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) { + // Check filename length + if (strlen(filename) > MAX_NAME - 1) { + return 2; + } + + // Check file existance and workings + FILE *pic = fopen(filename, ""r""); + if (pic == NULL) { + return 1; + } + fclose(pic); + + // Change user profile picture + strncpy(user -> profile_pic, filename, MAX_NAME - 1); + return 0; +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int make_friends(const char *name1, const char *name2, User *head) { + User *person1 = find_user(name1, head); + User *person2 = find_user(name2, head); + + // Check to make sure both users exist + if (person1 == NULL || person2 == NULL) { + return 4; + } + + // Check for the same user passed in twice + if (strcmp(name1, name2) == 0) { + return 3; + } + + int open1 = MAX_FRIENDS; + int open2 = MAX_FRIENDS; + for (int i = MAX_FRIENDS - 1; i >= 0; i--) { + // Check to see if users are already friends + if(person1 -> friends[i] != NULL) { + if (strcmp(person1 -> friends[i] -> name, name2) == 0) { + return 1; + } + } + + // Find first open index of each user's friend list + if (person1 -> friends[i] == NULL) { + open1 = i; + } + if (person2 -> friends[i] == NULL) { + open2 = i; + } + + // Check to ensure there is room in both user's friend lists + if (open1 == MAX_FRIENDS || open2 == MAX_FRIENDS) { + return 2; + } + } + + // Add to list + person1 -> friends[open1] = person2; + person2 -> friends[open2] = person1; + return 0; +} + + +/* + * 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 *pic = fopen(user -> profile_pic, ""r""); + if (pic == NULL) { + } else { + char line_info[100]; + while(fgets(line_info, 100, pic) != NULL) { + printf(""%s\n"", line_info); + } + fclose(pic); + } + + // Print Name + printf(""Name: %s\n"", user -> name); + printf(""------------------------------------------\n""); + + // Print Friends List + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user -> friends[i] != NULL) { + printf(""%s\n"", user -> friends[i] -> name); + } + } + printf(""------------------------------------------\n""); + + // Print Posts + printf(""Posts:\n""); + struct post *curr; + if (user -> first_post != NULL) { + curr = user -> first_post; + } + + while (curr != NULL) { + printf(""From: %s\n"", curr -> author); + printf(""Date: %s\n"", ctime(curr -> date)); + printf(""%s\n"", curr -> contents); + + curr = curr -> next; + + if (curr != NULL) { + printf(""\n===\n\n""); + } + } + + printf(""------------------------------------------\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) { + // Check both exists + if (author == NULL || target == NULL) { + return 2; + } + + // Check users are friends + int friend = check_friends(author, target); + if (friend == 1) { + return 1; + } + + // Create Post + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post -> author, author -> name); + time_t *curtime = malloc(sizeof(time_t)); + time(curtime); + new_post -> date = curtime; + new_post -> contents = contents; + + // Link Post to Post Linked List + if (target -> first_post == NULL) { + target -> first_post = new_post; + } else { + new_post -> next = target ->first_post; + target -> first_post = new_post; + } + + return 0; +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ +int delete_user(const char *name, User **user_ptr_del) { + // Check to ensure user with name exists + if (*user_ptr_del == NULL) { + return 1; + } + + // Check to see if first user is to be deleted + User *user = find_user(name, *user_ptr_del); + if (user == NULL) { + return 1; + } + + User *prev = NULL; + User *curr = *user_ptr_del; + + if (strcmp((*user_ptr_del) -> name, name) == 0) { // Check to see if first user is to be deleted + } else { // Check the rest of the users + while (prev == NULL) { + if (strcmp((curr -> next) -> name, name) == 0) { + prev = curr; + } + curr = curr -> next; + } + } + + // Delete user from friend's list + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = NULL; + if (user -> friends[i] != NULL) { + friend = user -> friends[i]; + + for (int j = 0; j < MAX_FRIENDS; j++) { + if (friend -> friends[i] == user) { + friend -> friends[i] = NULL; + } + } + } + } + + // Free all dynamically allocated memory + Post *curr_post = user -> first_post; + while (curr_post != NULL) { + Post *temp = curr_post; + curr_post = curr_post -> next; + free(temp -> contents); + free(temp -> date); + free(temp); + } + + if (prev == NULL) { + *user_ptr_del = curr -> next; + } else { + prev -> next = curr -> next; + } + free(curr); + + return 0; +} +","Delete User fails to delete user from friend's friend list. When a user with the same name is created afterwards, the user reappears on their friend's list", +63c648585f004bf4898cfba8,2023-02-14T05:36:30.732Z,Question from Code,v1,Why does the for loop not work in deleting user from friend's list.,"#include ""friends.h"" +#include +#include +#include + +/* + * Check if two users are friends + * + * Return: + * - 0 if the users are friends + * - 1 if the users are not friends +*/ +int check_friends(const User *user1, User *user2) { + for (int i = 0; i < MAX_FRIENDS; i++) { + if(user1 -> friends[i] != NULL) { + if (strcmp(user1 -> friends[i] -> name, user2 -> name) == 0) { + return 0; + } + } + } + return 1; +} + +/* + * 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) { + // Check length of name + if (strlen(name) > MAX_NAME - 1) { + return 2; + } + + // Create Space for the user & Initialize Variables + User *new_user = malloc(sizeof(User)); + strcpy(new_user -> name, name); + strcpy(new_user -> profile_pic, """"); + new_user -> first_post = NULL; + for (int i = 0; i < MAX_FRIENDS; i++) { + new_user -> friends[i] = NULL; + } + new_user -> next = NULL; + + + // First User to be created + if (*user_ptr_add == NULL) { + *user_ptr_add = new_user; + return 0; + } + + // User(s) already exist + User *curr = *user_ptr_add; + + // printf(""Linked List Excluding Last: ""); + + while (curr -> next != NULL) { + //printf(""%s "", curr -> name); + if (strcmp(curr -> name, name) == 0) { + return 1; + } + curr = curr -> next; + } + + // Check for the final node + if (strcmp(curr -> name, name) == 0) { + return 1; + } + + curr -> next = new_user; + + //printf(""\n%s %s\n"", curr -> name, new_user -> 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) { + // Check to make sure at least one User exists + if ((User *) head == NULL) { + return NULL; + } + + // Find a User with the same name, and return if it exists + User *curr = (User *) head; + while (curr != NULL) { + if (strcmp(curr -> name, 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) { + // Check to make sure at least one User exists + if ((User *) curr == NULL) { + } else { + User *current = (User *) curr; + + // Loop through every User + while (current != NULL) { + printf(""User: %s\n"", current -> name); + printf(""Profile Pic: %s\n"", current -> profile_pic); + printf(""Friends: ""); + for (int i = 0; i < MAX_FRIENDS; i++) { + if (current -> friends[i] != NULL) { + printf(""%s "", current -> friends[i] -> name); + } + } + printf(""\n\n""); + 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) { + // Check filename length + if (strlen(filename) > MAX_NAME - 1) { + return 2; + } + + // Check file existance and workings + FILE *pic = fopen(filename, ""r""); + if (pic == NULL) { + return 1; + } + fclose(pic); + + // Change user profile picture + strncpy(user -> profile_pic, filename, MAX_NAME - 1); + return 0; +} + + +/* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ +int make_friends(const char *name1, const char *name2, User *head) { + User *person1 = find_user(name1, head); + User *person2 = find_user(name2, head); + + // Check to make sure both users exist + if (person1 == NULL || person2 == NULL) { + return 4; + } + + // Check for the same user passed in twice + if (strcmp(name1, name2) == 0) { + return 3; + } + + int open1 = MAX_FRIENDS; + int open2 = MAX_FRIENDS; + for (int i = MAX_FRIENDS - 1; i >= 0; i--) { + // Check to see if users are already friends + if(person1 -> friends[i] != NULL) { + if (strcmp(person1 -> friends[i] -> name, name2) == 0) { + return 1; + } + } + + // Find first open index of each user's friend list + if (person1 -> friends[i] == NULL) { + open1 = i; + } + if (person2 -> friends[i] == NULL) { + open2 = i; + } + + // Check to ensure there is room in both user's friend lists + if (open1 == MAX_FRIENDS || open2 == MAX_FRIENDS) { + return 2; + } + } + + // Add to list + person1 -> friends[open1] = person2; + person2 -> friends[open2] = person1; + return 0; +} + + +/* + * 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 *pic = fopen(user -> profile_pic, ""r""); + if (pic == NULL) { + } else { + char line_info[100]; + while(fgets(line_info, 100, pic) != NULL) { + printf(""%s\n"", line_info); + } + fclose(pic); + } + + // Print Name + printf(""Name: %s\n"", user -> name); + printf(""------------------------------------------\n""); + + // Print Friends List + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user -> friends[i] != NULL) { + printf(""%s\n"", user -> friends[i] -> name); + } + } + printf(""------------------------------------------\n""); + + // Print Posts + printf(""Posts:\n""); + struct post *curr; + if (user -> first_post != NULL) { + curr = user -> first_post; + } + + while (curr != NULL) { + printf(""From: %s\n"", curr -> author); + printf(""Date: %s\n"", ctime(curr -> date)); + printf(""%s\n"", curr -> contents); + + curr = curr -> next; + + if (curr != NULL) { + printf(""\n===\n\n""); + } + } + + printf(""------------------------------------------\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) { + // Check both exists + if (author == NULL || target == NULL) { + return 2; + } + + // Check users are friends + int friend = check_friends(author, target); + if (friend == 1) { + return 1; + } + + // Create Post + Post *new_post = malloc(sizeof(Post)); + strcpy(new_post -> author, author -> name); + time_t *curtime = malloc(sizeof(time_t)); + time(curtime); + new_post -> date = curtime; + new_post -> contents = contents; + + // Link Post to Post Linked List + if (target -> first_post == NULL) { + target -> first_post = new_post; + } else { + new_post -> next = target ->first_post; + target -> first_post = new_post; + } + + return 0; +} + + +/* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ +int delete_user(const char *name, User **user_ptr_del) { + // Check to ensure user with name exists + if (*user_ptr_del == NULL) { + return 1; + } + + // Check to see if first user is to be deleted + User *user = find_user(name, *user_ptr_del); + if (user == NULL) { + return 1; + } + + User *prev = NULL; + User *curr = *user_ptr_del; + + if (strcmp((*user_ptr_del) -> name, name) == 0) { // Check to see if first user is to be deleted + } else { // Check the rest of the users + while (prev == NULL) { + if (strcmp((curr -> next) -> name, name) == 0) { + prev = curr; + } + curr = curr -> next; + } + } + + // Delete user from friend's list + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = NULL; + if (user -> friends[i] != NULL) { + friend = user -> friends[i]; + + for (int j = 0; j < MAX_FRIENDS; j++) { + if (friend -> friends[i] == user) { + friend -> friends[i] = NULL; + } + } + } + } + + // Free all dynamically allocated memory + Post *curr_post = user -> first_post; + while (curr_post != NULL) { + Post *temp = curr_post; + curr_post = curr_post -> next; + free(temp -> contents); + free(temp -> date); + free(temp); + } + + if (prev == NULL) { + *user_ptr_del = curr -> next; + } else { + prev -> next = curr -> next; + } + free(curr); + + return 0; +} +",, +63c648585f004bf4898cfba8,2023-03-10T04:27:08.094Z,Question from Code,v2,why is token[1] an integer and not the first character of the string,"char *token = strtok(line, "" ""); + + // Check if line is a target line + + while (token != NULL) { + printf(""%d%s|\n"", token[1], token); + + token = strtok(NULL, "" ""); + }",, +63c648585f004bf4898cfba8,2023-03-10T04:28:03.732Z,Question from Code,v2,why is token[0] and integer and not the first character of the string,"char *token = strtok(line, "" ""); + + // Check if line is a target line + + while (token != NULL) { + printf(""%d%s|\n"", token[0], token); + + token = strtok(NULL, "" ""); + }",, +63c648585f004bf4898cfba8,2023-03-10T04:29:00.933Z,General Question,v2,how do i get the first character of a token,,, +63c648585f004bf4898cfba8,2023-03-10T04:31:19.567Z,General Question,v2,how to dereference a token to get the first character,,, +63c648585f004bf4898cfba8,2023-03-10T16:43:35.580Z,Question from Code,v2,why is line 47 causing a segmentation fault,"#include +#include +#include + +#include ""pmake.h"" + +#include + +/* 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) { + // File is already opened for reading + char line[MAXLINE]; + + + while (fgets(line, MAXLINE, fp)) { + //Check if line is only white space and skip + int all_white = 1; + for (int i = 0; i < strlen(line); i++) { + if (!isspace(line[i])) { + all_white = 0; + } + } + if (all_white) { + continue; + } + + // Check if line is a comment and skip + if (is_comment_or_empty(line)) { + continue; + } + + char str[] = "" ""; + char *token = strtok(line, str); + + printf(""|%s|"", token); + + char first = *token; + // Check if line is a target line + if (isalpha(first)) { + printf(""Target: ""); + Rule *curr_rule; + + // First argument must be a target + curr_rule -> target = token; + + // Second argument is a colon + + // Subsequent arguments are dependencies if any exists + while (token != NULL) { + printf(""%s "", token); + + token = strtok(NULL, str); + } + } + + // Check if line is an action line + if (first == '\t') { + printf(""Action: ""); + + while (token != NULL) { + printf(""%s "", token); + + token = strtok(NULL, str); + } + } + + printf(""---------------NEXT LINE-------------\n""); + + // printf(""%s"", line); + } + return 0; +} + +/* Return 1 if the line is a comment line, as defined on the assignment handout. + Return 0 otherwise. + */ +int is_comment_or_empty(const char *line) { + for (int i = 0; i < strlen(line); i++){ + if (line[i] == '#') { + return 1; + } + if (line[i] != '\t' && line[i] != ' ') { + return 0; + } + } + return 1; +} +",, +63c648585f004bf4898cfba8,2023-03-10T17:07:14.881Z,Help Write Code,v2,,,,how to get last character of token given by strtok in c +63c648585f004bf4898cfba8,2023-03-11T01:54:29.822Z,Question from Code,v2,why is line 2 causing a segmentation fault,"char *token = strtok(line, str); +char first = *token;",, +63c648585f004bf4898cfba8,2023-03-11T04:29:16.311Z,Question from Code,v2,why is space not incrementing whenever there is a space in line?,"int space = 1; +for (int i = 0; line[i] != '\0'; i++) { + if (line[i] == ' ') { + space++; + } +}",, +63c648585f004bf4898cfba8,2023-03-11T04:34:28.000Z,General Question,v2,how to check number of spaces in sentence with spaces,,, +63c648585f004bf4898cfba8,2023-03-11T05:03:49.766Z,Question from Code,v2,Why is the malloc data not saved,"char **arr = malloc(sizeof(char *) * space); +malloc_check(arr); + +// Fill array with values +int index = 0; +while (token != NULL) { + arr[index] = malloc(sizeof(char) * (strlen(token) + 1)); + malloc_check(arr[index]); + strcpy(arr[index], token); + + index++; + token = strtok(NULL, str); +} + +// Create and initialize new Action node +Action *new_act = malloc(sizeof(Action)); +malloc_check(new_act); + +new_act->args = arr; +new_act->next_act = NULL; + +// Link Action and Rules +Action *last_act = select_last_act(line_first_rule->actions); +if (last_act == NULL) { + line_first_rule->actions = new_act; +} else { + last_act->next_act = new_act; +}",, +63c648585f004bf4898cfba8,2023-03-11T19:01:54.707Z,General Question,v2,How do I use stat in c,,, +63c6485f5f004bf4898cfbe4,2023-02-02T15:14:26.663Z,General Question,v1,"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)",,, +63c6485f5f004bf4898cfbe4,2023-02-02T15:16:58.632Z,Explain Code,v1,,"/* + * 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) { + return -1; +}",, +63c6485f5f004bf4898cfbe4,2023-02-08T16:26:51.636Z,Question from Code,v1,"In this code, I have to store the author, contents, and date of a user's post. I use the time() function to get the time, but every time I test it, it gives me a date from the future. What is wrong with how I called time? I need it to give me the current time.","int make_post(const User *author, User *target, char *contents) { + if (!author || !target) { + return 2; + } + + int areFriends = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + areFriends = 1; + break; + } + } + + if (!areFriends) { + return 1; + } + + Post *newPost = (Post*)malloc(sizeof(Post)); + strcpy(newPost->author, author->name); + newPost->contents = contents; + time_t now; + time(&now); + newPost->date = &now; + newPost->next = target->first_post; + target->first_post = newPost; + + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-08T16:34:24.761Z,Question from Code,v1,"This method is supposed to store the author, content, and date of a post. When I run it, I get a future date instead of the current date. If I replace newPost->date = ""&now"" with newPost->date = ""now,"" I get a warning that I should use ""&"" before the now. What should I do to fix it?","int make_post(const User *author, User *target, char *contents) { + if (!author || !target) { + return 2; + } + + int areFriends = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + areFriends = 1; + break; + } + } + + if (!areFriends) { + return 1; + } + + Post *newPost = (Post*)malloc(sizeof(Post)); + strcpy(newPost->author, author->name); + newPost->contents = contents; + time_t now; + time(&now); + newPost->date = &now; + newPost->next = target->first_post; + target->first_post = newPost; + + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-08T16:35:44.708Z,Question from Code,v1,"This method is supposed to store the author, content, and date of a post. When I run it, I get a future date instead of the current date. If I replace newPost->date = ""&now"" with newPost->date = ""now,"" I get a warning that I should use ""&"" before the now. What should I do to fix it?","int make_post(const User *author, User *target, char *contents) { + if (!author || !target) { + return 2; + } + + int areFriends = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + areFriends = 1; + break; + } + } + + if (!areFriends) { + return 1; + } + + Post *newPost = (Post*)malloc(sizeof(Post)); + strcpy(newPost->author, author->name); + newPost->contents = contents; + time_t now; + newPost->date = now; + newPost->next = target->first_post; + target->first_post = newPost; + + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-08T16:39:36.939Z,Question from Code,v1,"This method is supposed to store the author, content, and date of a post. When I run it, I get a future or past date instead of the current date. If I replace newPost->date = ""now"" with newPost->date = ""&now,"" I get a warning that I should use ""&"" before the now. What should I do to fix it?","int make_post(const User *author, User *target, char *contents) { + if (!author || !target) { + return 2; + } + + int areFriends = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + areFriends = 1; + break; + } + } + + if (!areFriends) { + return 1; + } + + Post *newPost = (Post*)malloc(sizeof(Post)); + strcpy(newPost->author, author->name); + newPost->contents = contents; + time_t *now = (time_t*)malloc(sizeof(time_t)); + newPost->date = &now; + newPost->next = target->first_post; + target->first_post = newPost; + + return 0; + +}",, +63c6485f5f004bf4898cfbe4,2023-02-08T16:41:12.068Z,Question from Code,v1,"This method is supposed to store the author, content, and date of a post. When I run it, I get a future date instead of the current date. If I replace newPost->date = ""&now"" with newPost->date = ""now,"" I get a warning that I should use ""&"" before the now. If I replace newPost->date = ""now"" with newPost->date = ""&now,"" I get a warning that I remove the ""&"" before the now. What should I do to fix it?","int make_post(const User *author, User *target, char *contents) { + if (!author || !target) { + return 2; + } + + int areFriends = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + areFriends = 1; + break; + } + } + + if (!areFriends) { + return 1; + } + + Post *newPost = (Post*)malloc(sizeof(Post)); + strcpy(newPost->author, author->name); + newPost->contents = contents; + time_t *now = (time_t*)malloc(sizeof(time_t)); + newPost->date = now; + newPost->next = target->first_post; + target->first_post = newPost; + + return 0; + +}",, +63c6485f5f004bf4898cfbe4,2023-02-08T16:55:54.873Z,Question from Code,v1,"This method is supposed to store the author, content, and date of a post. When I run it, I get a future date instead of the current date. If I replace newPost->date = ""&now"" with newPost->date = ""now,"" I get a warning that I should use ""&"" before the now. If I replace newPost->date = ""now"" with newPost->date = ""&now,"" I get a warning that I remove the ""&"" before the now. I am not using a string literal to store the date either. What should I do to fix it?","int make_post(const User *author, User *target, char *contents) { + if (!author || !target) { + return 2; + } + + int areFriends = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + areFriends = 1; + break; + } + } + + if (!areFriends) { + return 1; + } + + Post *newPost = (Post*)malloc(sizeof(Post)); + strcpy(newPost->author, author->name); + newPost->contents = contents; + time_t *now = (time_t*)malloc(sizeof(time_t)); + newPost->date = now; + newPost->next = target->first_post; + target->first_post = newPost; + + return 0; + +}",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:29:09.733Z,Question from Code,v1,do I have any possible memory leaks in my code?,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + + //first, check if user with same name exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // reset the current pointer + curr = *user_ptr_add; + if (curr == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + + return 0; +} ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:32:43.216Z,Question from Code,v1,"how can I fix the memory leak where curr +is not NULL +and curr->next +is NULL +. In this case, new_user +is never freed?","int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + + //first, check if user with same name exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // reset the current pointer + curr = *user_ptr_add; + if (curr == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + + return 0; +} ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:35:00.577Z,Question from Code,v1,do i have any possible memory leaks in my code?,"User *find_user(const char *name, const User *head) { + // initialize the head of the linked list + const User *curr = head; + + while (curr != NULL) { + // if match is found, return the user + if (strcmp(curr->name, name) == 0){ + return (User *)curr; + } + curr = curr->next; + } + // end of the list was reached without finding user + return NULL; +} ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:35:52.901Z,Question from Code,v1,do i have any possible memory leaks in my code?,"void list_users(const User *curr) { + // formatted like the sample output + printf(""User List\n""); + while (curr != NULL) { + // indent + printf("" ""); + // name + printf(""%s\n"", curr->name); + curr = curr->next; + } +} ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:36:35.658Z,Question from Code,v1,do i have any possible memory leaks in my code?,"int update_pic(User *user, const char *filename) { + // check if the file name is too long + if (strlen(filename) >= sizeof(user->profile_pic) - 1){ + return 2; + } + + // declare FILE variable like in PCRS prep (week 3) + FILE *file; + file = fopen(filename, ""r""); + // check if failed to be opened + if (file == NULL){ + return 1; + } + + // can use unsafe strcpy since the filename can fit + // close the file that opened successfully + strcpy(user->profile_pic, filename); + fclose(file); + return 0; +} ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:37:42.324Z,Question from Code,v1, do i have any possible memory leaks in my code?,"int make_friends(const char *name1, const char *name2, User *head) { + // find the users + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + //check if at least one user doesn't exist + if (!user1 || !user2){ + return 4; + } + + // check if the users are equal + else if (user1 == user2){ + return 3; + } + + // initialize the number of friends each user has + int user1_friends = 0; + int user2_friends = 0; + + // loop counts the number of friends each user has to see if they + // exceed the maximum number of friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] != NULL) { + user1_friends++; + } + if (user2->friends[i] != NULL) { + user2_friends++; + } + } + if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { + return 2; + } + + // checks if the users are already friends + for (int j = 0; j < MAX_FRIENDS; j++) { + if (user1->friends[j] == user2) { + return 1; + } + } + + // otherwise, adds the users to each of their firends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == NULL) { + user1->friends[i] = user2; + break; + } + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user2->friends[i] == NULL) { + user2->friends[i] = user1; + break; + } + } + return 0; +} ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:39:26.432Z,Question from Code,v1,do i have any possible memory leaks in my code?,"int make_friends(const char *name1, const char *name2, User *head) { + // find the users + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + //check if at least one user doesn't exist + if (!user1 || !user2){ + return 4; + } + + // check if the users are equal + else if (user1 == user2){ + return 3; + } + + // initialize the number of friends each user has + int user1_friends = 0; + int user2_friends = 0; + + // loop counts the number of friends each user has to see if they + // exceed the maximum number of friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] != NULL) { + user1_friends++; + } + if (user2->friends[i] != NULL) { + user2_friends++; + } + } + if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { + return 2; + } + + // checks if the users are already friends + for (int j = 0; j < MAX_FRIENDS; j++) { + if (user1->friends[j] == user2) { + return 1; + } + } + + // otherwise, adds the users to each of their firends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == NULL) { + user1->friends[i] = user2; + break; + } + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user2->friends[i] == NULL) { + user2->friends[i] = user1; + break; + } + } + return 0; +} + +User *find_user(const char *name, const User *head) { + // initialize the head of the linked list + const User *curr = head; + + while (curr != NULL) { + // if match is found, return the user + if (strcmp(curr->name, name) == 0){ + return (User *)curr; + } + curr = curr->next; + } + // end of the list was reached without finding user + return NULL; +} ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:41:09.825Z,Question from Code,v1,"how do i fix the memory leak in find_user +where I am returning a pointer to a local variable?","int make_friends(const char *name1, const char *name2, User *head) { + // find the users + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + //check if at least one user doesn't exist + if (!user1 || !user2){ + return 4; + } + + // check if the users are equal + else if (user1 == user2){ + return 3; + } + + // initialize the number of friends each user has + int user1_friends = 0; + int user2_friends = 0; + + // loop counts the number of friends each user has to see if they + // exceed the maximum number of friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] != NULL) { + user1_friends++; + } + if (user2->friends[i] != NULL) { + user2_friends++; + } + } + if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { + return 2; + } + + // checks if the users are already friends + for (int j = 0; j < MAX_FRIENDS; j++) { + if (user1->friends[j] == user2) { + return 1; + } + } + + // otherwise, adds the users to each of their firends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == NULL) { + user1->friends[i] = user2; + break; + } + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user2->friends[i] == NULL) { + user2->friends[i] = user1; + break; + } + } + return 0; +} + +User *find_user(const char *name, const User *head) { + // initialize the head of the linked list + const User *curr = head; + + while (curr != NULL) { + // if match is found, return the user + if (strcmp(curr->name, name) == 0){ + return (User *)curr; + } + curr = curr->next; + } + // end of the list was reached without finding user + return NULL; +} ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:42:34.428Z,Question from Code,v1,"how to fix the memory leak by allocating memory for a new user and copying the data from the local user to the new user. +","int make_friends(const char *name1, const char *name2, User *head) { + // find the users + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + //check if at least one user doesn't exist + if (!user1 || !user2){ + return 4; + } + + // check if the users are equal + else if (user1 == user2){ + return 3; + } + + // initialize the number of friends each user has + int user1_friends = 0; + int user2_friends = 0; + + // loop counts the number of friends each user has to see if they + // exceed the maximum number of friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] != NULL) { + user1_friends++; + } + if (user2->friends[i] != NULL) { + user2_friends++; + } + } + if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { + return 2; + } + + // checks if the users are already friends + for (int j = 0; j < MAX_FRIENDS; j++) { + if (user1->friends[j] == user2) { + return 1; + } + } + + // otherwise, adds the users to each of their firends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == NULL) { + user1->friends[i] = user2; + break; + } + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user2->friends[i] == NULL) { + user2->friends[i] = user1; + break; + } + } + return 0; +} + +User *find_user(const char *name, const User *head) { + // initialize the head of the linked list + const User *curr = head; + + while (curr != NULL) { + // if match is found, return the user + if (strcmp(curr->name, name) == 0){ + return (User *)curr; + } + curr = curr->next; + } + // end of the list was reached without finding user + return NULL; +} ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:45:39.361Z,Question from Code,v1,do I have any possible memory leaks?,"int update_pic(User *user, const char *filename) { + // check if the file name is too long + if (strlen(filename) >= sizeof(user->profile_pic) - 1){ + return 2; + } + + // declare FILE variable like in PCRS prep (week 3) + FILE *file; + file = fopen(filename, ""r""); + // check if failed to be opened + if (file == NULL){ + return 1; + } + + // can use unsafe strcpy since the filename can fit + // close the file that opened successfully + strcpy(user->profile_pic, filename); + fclose(file); + return 0; +} ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:47:37.864Z,Question from Code,v1,DOES THIS FIX ANY MEMORY LEAKS?,"int update_pic(User *user, const char *filename) { + // check if the file name is too long + if (strlen(filename) >= sizeof(user->profile_pic) - 1){ + return 2; + } + + // declare FILE variable like in PCRS prep (week 3) + FILE *file; + file = fopen(filename, ""r""); + // check if failed to be opened + if (file == NULL){ + return 1; + } + + // can use unsafe strcpy since the filename can fit + // close the file that opened successfully + strcpy(user->profile_pic, filename); + if (file != NULL) { + fclose(file); + } + + return 0; +} ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:48:26.294Z,Question from Code,v1,how can i fix the memory leak?,"int update_pic(User *user, const char *filename) { + // check if the file name is too long + if (strlen(filename) >= sizeof(user->profile_pic) - 1){ + return 2; + } + + // declare FILE variable like in PCRS prep (week 3) + FILE *file; + file = fopen(filename, ""r""); + // check if failed to be opened + if (file == NULL){ + return 1; + } + + // can use unsafe strcpy since the filename can fit + // close the file that opened successfully + strcpy(user->profile_pic, filename); + fclose(file); + + return 0; +} ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:49:05.680Z,Question from Code,v1,how can I free the file pointer?,"int update_pic(User *user, const char *filename) { + // check if the file name is too long + if (strlen(filename) >= sizeof(user->profile_pic) - 1){ + return 2; + } + + // declare FILE variable like in PCRS prep (week 3) + FILE *file; + file = fopen(filename, ""r""); + // check if failed to be opened + if (file == NULL){ + return 1; + } + + // can use unsafe strcpy since the filename can fit + // close the file that opened successfully + strcpy(user->profile_pic, filename); + fclose(file); + + return 0; +} ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:50:18.112Z,Question from Code,v1,do I have any memory leaks?,"int update_pic(User *user, const char *filename) { + // check if the file name is too long + if (strlen(filename) >= sizeof(user->profile_pic) - 1){ + return 2; + } + + // declare FILE variable like in PCRS prep (week 3) + FILE *file; + file = fopen(filename, ""r""); + // check if failed to be opened + if (file == NULL){ + return 1; + } + + // can use unsafe strcpy since the filename can fit + // close the file that opened successfully + strcpy(user->profile_pic, filename); + fclose(file); + + return 0; +} ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:51:04.918Z,Question from Code,v1,do I have any memory leaks?,"/* + * 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) { + // find the users + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + //check if at least one user doesn't exist + if (!user1 || !user2){ + return 4; + } + + // check if the users are equal + else if (user1 == user2){ + return 3; + } + + // initialize the number of friends each user has + int user1_friends = 0; + int user2_friends = 0; + + // loop counts the number of friends each user has to see if they + // exceed the maximum number of friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] != NULL) { + user1_friends++; + } + if (user2->friends[i] != NULL) { + user2_friends++; + } + } + if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { + return 2; + } + + // checks if the users are already friends + for (int j = 0; j < MAX_FRIENDS; j++) { + if (user1->friends[j] == user2) { + return 1; + } + } + + // otherwise, adds the users to each of their firends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == NULL) { + user1->friends[i] = user2; + break; + } + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user2->friends[i] == NULL) { + user2->friends[i] = user1; + break; + } + } + return 0; +} ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:52:28.173Z,Question from Code,v1,where and how should i free memory to fix the memory leaks?,"/* + * 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) { + // find the users + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + //check if at least one user doesn't exist + if (!user1 || !user2){ + return 4; + } + + // check if the users are equal + else if (user1 == user2){ + return 3; + } + + // initialize the number of friends each user has + int user1_friends = 0; + int user2_friends = 0; + + // loop counts the number of friends each user has to see if they + // exceed the maximum number of friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] != NULL) { + user1_friends++; + } + if (user2->friends[i] != NULL) { + user2_friends++; + } + } + if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { + return 2; + } + + // checks if the users are already friends + for (int j = 0; j < MAX_FRIENDS; j++) { + if (user1->friends[j] == user2) { + return 1; + } + } + + // otherwise, adds the users to each of their firends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == NULL) { + user1->friends[i] = user2; + break; + } + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user2->friends[i] == NULL) { + user2->friends[i] = user1; + break; + } + } + return 0; +} ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:53:42.289Z,Question from Code,v1,do i have any memory leaks?," +/* + * 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) { + // check if user is NULL + if (user == NULL) { + return 1; + } + + // check if there is a profile pic associated with the user + if (strlen(user->profile_pic) != 0){ + // open the profile pic + FILE *fptr; + fptr = fopen(user->profile_pic, ""r""); + if (fptr == NULL){ + printf(""Cannot open file \n""); + } + + // Read contents from file and print them + char pic = fgetc(fptr); + while (pic != EOF){ + printf (""%c"", pic); + pic = fgetc(fptr); + } + printf(""\n""); + }// print name + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // print each friend in order in which they became friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = user->friends[i]; + if (friend != NULL) { + printf(""%s\n"", friend->name); + } + } + printf(""------------------------------------------\n""); + + printf(""Posts:\n""); + Post *current_post = user->first_post; + while (current_post != NULL) { + printf(""From: %s\n"", current_post->author); + // convert time_t to a string + printf(""Date: %s"", ctime(current_post->date)); + // checks if post is the last to see if ""==="" is needed + if (current_post->next == NULL){ + printf(""\n%s\n"", current_post->contents); + } + else { + printf(""\n%s\n\n===\n\n"", current_post->contents); + } + current_post = current_post->next; + } + printf(""------------------------------------------\n""); + return 0; +} + ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:54:32.796Z,Question from Code,v1,how do i free memory to fix memory leaks?," +/* + * 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) { + // check if user is NULL + if (user == NULL) { + return 1; + } + + // check if there is a profile pic associated with the user + if (strlen(user->profile_pic) != 0){ + // open the profile pic + FILE *fptr; + fptr = fopen(user->profile_pic, ""r""); + if (fptr == NULL){ + printf(""Cannot open file \n""); + } + + // Read contents from file and print them + char pic = fgetc(fptr); + while (pic != EOF){ + printf (""%c"", pic); + pic = fgetc(fptr); + } + printf(""\n""); + }// print name + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // print each friend in order in which they became friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = user->friends[i]; + if (friend != NULL) { + printf(""%s\n"", friend->name); + } + } + printf(""------------------------------------------\n""); + + printf(""Posts:\n""); + Post *current_post = user->first_post; + while (current_post != NULL) { + printf(""From: %s\n"", current_post->author); + // convert time_t to a string + printf(""Date: %s"", ctime(current_post->date)); + // checks if post is the last to see if ""==="" is needed + if (current_post->next == NULL){ + printf(""\n%s\n"", current_post->contents); + } + else { + printf(""\n%s\n\n===\n\n"", current_post->contents); + } + current_post = current_post->next; + } + printf(""------------------------------------------\n""); + return 0; +} + ",, +63c6485f5f004bf4898cfbe4,2023-02-09T18:55:47.812Z,Help Write Code,v1,,,,"/* + * 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. + */ Avoid memory leaks." +63c6485f5f004bf4898cfbe4,2023-02-10T16:14:05.953Z,Question from Code,v1,where do i have memory leaks and how can I fix them exactly?," #include ""friends.h"" + #include + #include + #include + void remove_friend(User *user, const char *friend_name); + /* + * 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) { + User *curr = *user_ptr_add; + + //first, check if user with same name exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // reset the current pointer + curr = *user_ptr_add; + if (curr == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + + 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) { + // initialize the head of the linked list + const User *curr = head; + + while (curr != NULL) { + // if match is found, return the user + if (strcmp(curr->name, name) == 0){ + return (User *)curr; + } + curr = curr->next; + } + // end of the list was reached without finding user + 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) { + // formatted like the sample output + printf(""User List\n""); + while (curr != NULL) { + // indent + printf("" ""); + // name + printf(""%s\n"", curr->name); + curr = curr->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) { + // check if the file name is too long + if (strlen(filename) >= sizeof(user->profile_pic) - 1){ + return 2; + } + + // declare FILE variable like in PCRS prep (week 3) + FILE *file; + file = fopen(filename, ""r""); + // check if failed to be opened + if (file == NULL){ + return 1; + } + + // can use unsafe strcpy since the filename can fit + // close the file that opened successfully + strcpy(user->profile_pic, filename); + fclose(file); + return 0; + } + + + /* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ + int make_friends(const char *name1, const char *name2, User *head) { + // find the users + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + //check if at least one user doesn't exist + if (!user1 || !user2){ + return 4; + } + + // check if the users are equal + else if (user1 == user2){ + return 3; + } + + // initialize the number of friends each user has + int user1_friends = 0; + int user2_friends = 0; + + // loop counts the number of friends each user has to see if they + // exceed the maximum number of friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] != NULL) { + user1_friends++; + } + if (user2->friends[i] != NULL) { + user2_friends++; + } + } + if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { + return 2; + } + + // checks if the users are already friends + for (int j = 0; j < MAX_FRIENDS; j++) { + if (user1->friends[j] == user2) { + return 1; + } + } + + // otherwise, adds the users to each of their firends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == NULL) { + user1->friends[i] = user2; + break; + } + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user2->friends[i] == NULL) { + user2->friends[i] = user1; + break; + } + } + return 0; + } + + + /* + * 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) { + // check if user is NULL + if (user == NULL) { + return 1; + } + + // check if there is a profile pic associated with the user + if (strlen(user->profile_pic) != 0){ + // open the profile pic + FILE *fptr; + fptr = fopen(user->profile_pic, ""r""); + if (fptr == NULL){ + printf(""Cannot open file \n""); + } + + // Read contents from file and print them + char pic = fgetc(fptr); + while (pic != EOF){ + printf (""%c"", pic); + pic = fgetc(fptr); + } + printf(""\n""); + }// print name + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // print each friend in order in which they became friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = user->friends[i]; + if (friend != NULL) { + printf(""%s\n"", friend->name); + } + } + printf(""------------------------------------------\n""); + + printf(""Posts:\n""); + Post *current_post = user->first_post; + while (current_post != NULL) { + printf(""From: %s\n"", current_post->author); + // convert time_t to a string + printf(""Date: %s"", ctime(current_post->date)); + // checks if post is the last to see if ""==="" is needed + if (current_post->next == NULL){ + printf(""\n%s\n"", current_post->contents); + } + else { + printf(""\n%s\n\n===\n\n"", current_post->contents); + } + current_post = current_post->next; + } + printf(""------------------------------------------\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 || !target) { + return 2; + } + + // numerical Boolean to make predicate logic easy + int areFriends = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + areFriends = 1; + break; + } + } + + if (!areFriends) { + return 1; + } + // allocate space for a new post + Post *newPost = (Post*)malloc(sizeof(Post)); + strcpy(newPost->author, author->name); + newPost->contents = contents; + // allocate space for the time + time_t *now = (time_t*)malloc(sizeof(time_t)); + time(now); + newPost->date = now; + newPost->next = target->first_post; + target->first_post = newPost; + + return 0; + } + + + /* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ + int delete_user(const char *name, User **user_ptr_del) { + User *curr = *user_ptr_del; + User *prev = NULL; + + while (curr != NULL) { + // found the user to delete + if (strcmp(curr->name, name) == 0) { + // update pointers to skip over this user + if (prev != NULL) { + prev->next = curr->next; + } else { + *user_ptr_del = curr->next; + } + + // remove this user from their friends' lists of friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + remove_friend(curr->friends[i], curr->name); + } + } + + // free memory associated with this user + free(curr); + + return 0; + } + prev = curr; + curr = curr->next; + } + + // user not found + return 1; + } + + void remove_friend(User *user, const char *friend_name) { + int i = 0; + while (i < MAX_FRIENDS && user->friends[i] != NULL) { + if (strcmp(user->friends[i]->name, friend_name) == 0) { + // Shift the rest of the elements in the friends array to fill the gap + for (int j = i; j < MAX_FRIENDS - 1; j++) { + user->friends[j] = user->friends[j + 1]; + } + // Mark the last element in the array as NULL + user->friends[MAX_FRIENDS - 1] = NULL; + break; + } + i++; + } + } ",, +63c6485f5f004bf4898cfbe4,2023-02-10T16:48:19.157Z,Question from Code,v1,where exactly do i add free(newPost->date) and free(newPost)? what lines?,"#include ""friends.h"" + #include + #include + #include + void remove_friend(User *user, const char *friend_name); + /* + * 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) { + User *curr = *user_ptr_add; + + //first, check if user with same name exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // reset the current pointer + curr = *user_ptr_add; + if (curr == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + + 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) { + // initialize the head of the linked list + const User *curr = head; + + while (curr != NULL) { + // if match is found, return the user + if (strcmp(curr->name, name) == 0){ + return (User *)curr; + } + curr = curr->next; + } + // end of the list was reached without finding user + 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) { + // formatted like the sample output + printf(""User List\n""); + while (curr != NULL) { + // indent + printf("" ""); + // name + printf(""%s\n"", curr->name); + curr = curr->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) { + // check if the file name is too long + if (strlen(filename) >= sizeof(user->profile_pic) - 1){ + return 2; + } + + // declare FILE variable like in PCRS prep (week 3) + FILE *file; + file = fopen(filename, ""r""); + // check if failed to be opened + if (file == NULL){ + return 1; + } + + // can use unsafe strcpy since the filename can fit + // close the file that opened successfully + strcpy(user->profile_pic, filename); + fclose(file); + return 0; + } + + + /* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ + int make_friends(const char *name1, const char *name2, User *head) { + // find the users + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + //check if at least one user doesn't exist + if (!user1 || !user2){ + return 4; + } + + // check if the users are equal + else if (user1 == user2){ + return 3; + } + + // initialize the number of friends each user has + int user1_friends = 0; + int user2_friends = 0; + + // loop counts the number of friends each user has to see if they + // exceed the maximum number of friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] != NULL) { + user1_friends++; + } + if (user2->friends[i] != NULL) { + user2_friends++; + } + } + if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { + return 2; + } + + // checks if the users are already friends + for (int j = 0; j < MAX_FRIENDS; j++) { + if (user1->friends[j] == user2) { + return 1; + } + } + + // otherwise, adds the users to each of their firends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == NULL) { + user1->friends[i] = user2; + break; + } + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user2->friends[i] == NULL) { + user2->friends[i] = user1; + break; + } + } + return 0; + } + + + /* + * 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) { + // check if user is NULL + if (user == NULL) { + return 1; + } + + // check if there is a profile pic associated with the user + if (strlen(user->profile_pic) != 0){ + // open the profile pic + FILE *fptr; + fptr = fopen(user->profile_pic, ""r""); + if (fptr == NULL){ + printf(""Cannot open file \n""); + } + + // Read contents from file and print them + char pic = fgetc(fptr); + while (pic != EOF){ + printf (""%c"", pic); + pic = fgetc(fptr); + } + printf(""\n""); + }// print name + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // print each friend in order in which they became friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = user->friends[i]; + if (friend != NULL) { + printf(""%s\n"", friend->name); + } + } + printf(""------------------------------------------\n""); + + printf(""Posts:\n""); + Post *current_post = user->first_post; + while (current_post != NULL) { + printf(""From: %s\n"", current_post->author); + // convert time_t to a string + printf(""Date: %s"", ctime(current_post->date)); + // checks if post is the last to see if ""==="" is needed + if (current_post->next == NULL){ + printf(""\n%s\n"", current_post->contents); + } + else { + printf(""\n%s\n\n===\n\n"", current_post->contents); + } + current_post = current_post->next; + } + printf(""------------------------------------------\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 || !target) { + return 2; + } + + // numerical Boolean to make predicate logic easy + int areFriends = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + areFriends = 1; + break; + } + } + + if (!areFriends) { + return 1; + } + // allocate space for a new post + Post *newPost = (Post*)malloc(sizeof(Post)); + strcpy(newPost->author, author->name); + newPost->contents = contents; + // allocate space for the time + time_t *now = (time_t*)malloc(sizeof(time_t)); + time(now); + newPost->date = now; + newPost->next = target->first_post; + target->first_post = newPost; + + return 0; + } + + + /* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ + int delete_user(const char *name, User **user_ptr_del) { + User *curr = *user_ptr_del; + User *prev = NULL; + + while (curr != NULL) { + // found the user to delete + if (strcmp(curr->name, name) == 0) { + // update pointers to skip over this user + if (prev != NULL) { + prev->next = curr->next; + } else { + *user_ptr_del = curr->next; + } + + // remove this user from their friends' lists of friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + remove_friend(curr->friends[i], curr->name); + } + } + + // free memory associated with this user + free(curr); + + return 0; + } + prev = curr; + curr = curr->next; + } + + // user not found + return 1; + } + + void remove_friend(User *user, const char *friend_name) { + int i = 0; + while (i < MAX_FRIENDS && user->friends[i] != NULL) { + if (strcmp(user->friends[i]->name, friend_name) == 0) { + // Shift the rest of the elements in the friends array to fill the gap + for (int j = i; j < MAX_FRIENDS - 1; j++) { + user->friends[j] = user->friends[j + 1]; + } + // Mark the last element in the array as NULL + user->friends[MAX_FRIENDS - 1] = NULL; + break; + } + i++; + } + } ",, +63c6485f5f004bf4898cfbe4,2023-02-10T16:54:10.395Z,Question from Code,v1,where are the memory leaks coming from?,"#include ""friends.h"" + #include + #include + #include + void remove_friend(User *user, const char *friend_name); + /* + * 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) { + User *curr = *user_ptr_add; + + //first, check if user with same name exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // reset the current pointer + curr = *user_ptr_add; + if (curr == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + + 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) { + // initialize the head of the linked list + const User *curr = head; + + while (curr != NULL) { + // if match is found, return the user + if (strcmp(curr->name, name) == 0){ + return (User *)curr; + } + curr = curr->next; + } + // end of the list was reached without finding user + 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) { + // formatted like the sample output + printf(""User List\n""); + while (curr != NULL) { + // indent + printf("" ""); + // name + printf(""%s\n"", curr->name); + curr = curr->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) { + // check if the file name is too long + if (strlen(filename) >= sizeof(user->profile_pic) - 1){ + return 2; + } + + // declare FILE variable like in PCRS prep (week 3) + FILE *file; + file = fopen(filename, ""r""); + // check if failed to be opened + if (file == NULL){ + return 1; + } + + // can use unsafe strcpy since the filename can fit + // close the file that opened successfully + strcpy(user->profile_pic, filename); + fclose(file); + return 0; + } + + + /* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ + int make_friends(const char *name1, const char *name2, User *head) { + // find the users + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + //check if at least one user doesn't exist + if (!user1 || !user2){ + return 4; + } + + // check if the users are equal + else if (user1 == user2){ + return 3; + } + + // initialize the number of friends each user has + int user1_friends = 0; + int user2_friends = 0; + + // loop counts the number of friends each user has to see if they + // exceed the maximum number of friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] != NULL) { + user1_friends++; + } + if (user2->friends[i] != NULL) { + user2_friends++; + } + } + if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { + return 2; + } + + // checks if the users are already friends + for (int j = 0; j < MAX_FRIENDS; j++) { + if (user1->friends[j] == user2) { + return 1; + } + } + + // otherwise, adds the users to each of their firends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == NULL) { + user1->friends[i] = user2; + break; + } + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user2->friends[i] == NULL) { + user2->friends[i] = user1; + break; + } + } + return 0; + } + + + /* + * 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) { + // check if user is NULL + if (user == NULL) { + return 1; + } + + // check if there is a profile pic associated with the user + if (strlen(user->profile_pic) != 0){ + // open the profile pic + FILE *fptr; + fptr = fopen(user->profile_pic, ""r""); + if (fptr == NULL){ + printf(""Cannot open file \n""); + } + + // Read contents from file and print them + char pic = fgetc(fptr); + while (pic != EOF){ + printf (""%c"", pic); + pic = fgetc(fptr); + } + printf(""\n""); + }// print name + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // print each friend in order in which they became friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = user->friends[i]; + if (friend != NULL) { + printf(""%s\n"", friend->name); + } + } + printf(""------------------------------------------\n""); + + printf(""Posts:\n""); + Post *current_post = user->first_post; + while (current_post != NULL) { + printf(""From: %s\n"", current_post->author); + // convert time_t to a string + printf(""Date: %s"", ctime(current_post->date)); + // checks if post is the last to see if ""==="" is needed + if (current_post->next == NULL){ + printf(""\n%s\n"", current_post->contents); + } + else { + printf(""\n%s\n\n===\n\n"", current_post->contents); + } + current_post = current_post->next; + } + printf(""------------------------------------------\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 || !target) { + return 2; + } + + // numerical Boolean to make predicate logic easy + int areFriends = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + areFriends = 1; + break; + } + } + + if (!areFriends) { + return 1; + } + // allocate space for a new post + Post *newPost = (Post*)malloc(sizeof(Post)); + strcpy(newPost->author, author->name); + newPost->contents = contents; + // allocate space for the time + time_t *now = (time_t*)malloc(sizeof(time_t)); + time(now); + newPost->date = now; + newPost->next = target->first_post; + target->first_post = newPost; + + return 0; + } + + + /* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ + int delete_user(const char *name, User **user_ptr_del) { + User *curr = *user_ptr_del; + User *prev = NULL; + + while (curr != NULL) { + // found the user to delete + if (strcmp(curr->name, name) == 0) { + // update pointers to skip over this user + if (prev != NULL) { + prev->next = curr->next; + } else { + *user_ptr_del = curr->next; + } + + // remove this user from their friends' lists of friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + remove_friend(curr->friends[i], curr->name); + } + } + + // free memory associated with this user + free(curr); + + return 0; + } + prev = curr; + curr = curr->next; + } + + // user not found + return 1; + } + + void remove_friend(User *user, const char *friend_name) { + int i = 0; + while (i < MAX_FRIENDS && user->friends[i] != NULL) { + if (strcmp(user->friends[i]->name, friend_name) == 0) { + // Shift the rest of the elements in the friends array to fill the gap + for (int j = i; j < MAX_FRIENDS - 1; j++) { + user->friends[j] = user->friends[j + 1]; + } + // Mark the last element in the array as NULL + user->friends[MAX_FRIENDS - 1] = NULL; + break; + } + i++; + } + } ",, +63c6485f5f004bf4898cfbe4,2023-02-10T16:56:53.476Z,Question from Code,v1,where are the memory leaks?,"#include ""friends.h"" + #include + #include + #include + void remove_friend(User *user, const char *friend_name); + /* + * 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) { + User *curr = *user_ptr_add; + + //first, check if user with same name exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // reset the current pointer + curr = *user_ptr_add; + if (curr == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + + 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) { + // initialize the head of the linked list + const User *curr = head; + + while (curr != NULL) { + // if match is found, return the user + if (strcmp(curr->name, name) == 0){ + return (User *)curr; + } + curr = curr->next; + } + // end of the list was reached without finding user + 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) { + // formatted like the sample output + printf(""User List\n""); + while (curr != NULL) { + // indent + printf("" ""); + // name + printf(""%s\n"", curr->name); + curr = curr->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) { + // check if the file name is too long + if (strlen(filename) >= sizeof(user->profile_pic) - 1){ + return 2; + } + + // declare FILE variable like in PCRS prep (week 3) + FILE *file; + file = fopen(filename, ""r""); + // check if failed to be opened + if (file == NULL){ + return 1; + } + + // can use unsafe strcpy since the filename can fit + // close the file that opened successfully + strcpy(user->profile_pic, filename); + fclose(file); + free(file); + return 0; + } + + + /* + * Make two users friends with each other. This is symmetric - a pointer to + * each user must be stored in the 'friends' array of the other. + * + * New friends must be added in the first empty spot in the 'friends' array. + * + * Return: + * - 0 on success. + * - 1 if the two users are already friends. + * - 2 if the users are not already friends, but at least one already has + * MAX_FRIENDS friends. + * - 3 if the same user is passed in twice. + * - 4 if at least one user does not exist. + * + * Do not modify either user if the result is a failure. + * NOTE: If multiple errors apply, return the *largest* error code that applies. + */ + int make_friends(const char *name1, const char *name2, User *head) { + // find the users + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + //check if at least one user doesn't exist + if (!user1 || !user2){ + return 4; + } + + // check if the users are equal + else if (user1 == user2){ + return 3; + } + + // initialize the number of friends each user has + int user1_friends = 0; + int user2_friends = 0; + + // loop counts the number of friends each user has to see if they + // exceed the maximum number of friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] != NULL) { + user1_friends++; + } + if (user2->friends[i] != NULL) { + user2_friends++; + } + } + if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { + return 2; + } + + // checks if the users are already friends + for (int j = 0; j < MAX_FRIENDS; j++) { + if (user1->friends[j] == user2) { + return 1; + } + } + + // otherwise, adds the users to each of their firends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == NULL) { + user1->friends[i] = user2; + break; + } + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user2->friends[i] == NULL) { + user2->friends[i] = user1; + break; + } + } + return 0; + } + + + /* + * 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) { + // check if user is NULL + if (user == NULL) { + return 1; + } + + // check if there is a profile pic associated with the user + if (strlen(user->profile_pic) != 0){ + // open the profile pic + FILE *fptr; + fptr = fopen(user->profile_pic, ""r""); + if (fptr == NULL){ + printf(""Cannot open file \n""); + } + + // Read contents from file and print them + char pic = fgetc(fptr); + while (pic != EOF){ + printf (""%c"", pic); + pic = fgetc(fptr); + } + printf(""\n""); + }// print name + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // print each friend in order in which they became friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = user->friends[i]; + if (friend != NULL) { + printf(""%s\n"", friend->name); + } + } + printf(""------------------------------------------\n""); + + printf(""Posts:\n""); + Post *current_post = user->first_post; + while (current_post != NULL) { + printf(""From: %s\n"", current_post->author); + // convert time_t to a string + printf(""Date: %s"", ctime(current_post->date)); + // checks if post is the last to see if ""==="" is needed + if (current_post->next == NULL){ + printf(""\n%s\n"", current_post->contents); + } + else { + printf(""\n%s\n\n===\n\n"", current_post->contents); + } + current_post = current_post->next; + } + printf(""------------------------------------------\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 || !target) { + return 2; + } + + // numerical Boolean to make predicate logic easy + int areFriends = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + areFriends = 1; + break; + } + } + + if (!areFriends) { + return 1; + } + // allocate space for a new post + Post *newPost = (Post*)malloc(sizeof(Post)); + strcpy(newPost->author, author->name); + newPost->contents = contents; + // allocate space for the time + time_t *now = (time_t*)malloc(sizeof(time_t)); + time(now); + newPost->date = now; + free(now); + newPost->next = target->first_post; + target->first_post = newPost; + free(newPost); + + return 0; + } + + + /* + * From the list pointed to by *user_ptr_del, delete the user + * with the given name. + * Remove the deleted user from any lists of friends. + * + * Return: + * - 0 on success. + * - 1 if a user with this name does not exist. + */ + int delete_user(const char *name, User **user_ptr_del) { + User *curr = *user_ptr_del; + User *prev = NULL; + + while (curr != NULL) { + // found the user to delete + if (strcmp(curr->name, name) == 0) { + // update pointers to skip over this user + if (prev != NULL) { + prev->next = curr->next; + } else { + *user_ptr_del = curr->next; + } + + // remove this user from their friends' lists of friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (curr->friends[i] != NULL) { + remove_friend(curr->friends[i], curr->name); + } + } + + // free memory associated with this user + free(curr); + + return 0; + } + prev = curr; + curr = curr->next; + } + + // user not found + return 1; + } + + void remove_friend(User *user, const char *friend_name) { + int i = 0; + while (i < MAX_FRIENDS && user->friends[i] != NULL) { + if (strcmp(user->friends[i]->name, friend_name) == 0) { + // Shift the rest of the elements in the friends array to fill the gap + for (int j = i; j < MAX_FRIENDS - 1; j++) { + user->friends[j] = user->friends[j + 1]; + } + // Mark the last element in the array as NULL + user->friends[MAX_FRIENDS - 1] = NULL; + break; + } + i++; + } + } ",, +63c6485f5f004bf4898cfbe4,2023-02-10T17:41:26.663Z,Question from Code,v1,what is causing the memory leak?,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + + //first, check if user with same name exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // reset the current pointer + curr = *user_ptr_add; + if (curr == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-10T17:47:17.308Z,Question from Code,v1,where should I free new_user? what line?,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + + //first, check if user with same name exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // reset the current pointer + curr = *user_ptr_add; + if (curr == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-10T17:50:35.877Z,Question from Code,v1,where is the memory leak?,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + + //first, check if user with same name exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // reset the current pointer + curr = *user_ptr_add; + if (curr == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + free(new_user); + + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-10T17:51:15.862Z,Question from Code,v1,how should i free the memory allocated for new_user,"int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + + //first, check if user with same name exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // reset the current pointer + curr = *user_ptr_add; + if (curr == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + free(new_user); + + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-10T17:52:20.916Z,Question from Code,v1,"how should i fix the following issues: (1) the memory allocated for new_user is freed before it is added to the list, so the memory is lost, and (2) the memory allocated for new_user is not freed when the function returns.","int create_user(const char *name, User **user_ptr_add) { + User *curr = *user_ptr_add; + + //first, check if user with same name exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // reset the current pointer + curr = *user_ptr_add; + if (curr == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + free(new_user); + + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T00:54:32.481Z,Question from Code,v1,where is the memory leak?,"/* + * 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) { + User *curr = *user_ptr_add; + + //first, check if user with same name exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // reset the current pointer + curr = *user_ptr_add; + if (curr == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + + free(new_user); + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T00:55:15.586Z,Question from Code,v1,how does the free(new_user) call free the memory allocated by malloc before the new user is added to the list.,"/* + * 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) { + User *curr = *user_ptr_add; + + //first, check if user with same name exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // reset the current pointer + curr = *user_ptr_add; + if (curr == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + + free(new_user); + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T01:04:32.851Z,Question from Code,v1,what are the possible reasons that I might have a memory leak?,"/* + * 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) { + User *curr = *user_ptr_add; + + //first, check if user with same name exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // reset the current pointer + curr = *user_ptr_add; + if (curr == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T01:05:42.136Z,Question from Code,v1,how can i fix if malloc fails?,"/* + * 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) { + User *curr = *user_ptr_add; + + //first, check if user with same name exists + while(curr != NULL){ + if (strcmp(curr->name, name) == 0){ + return 1; + } + curr = curr->next; + } + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + // reset the current pointer + curr = *user_ptr_add; + if (curr == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr->next != NULL){ + curr = curr->next; + } + curr->next = new_user; + } + + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T01:21:31.722Z,Question from Code,v1,do i have memory leak?,"/* + * 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) { + // initialize the head of the linked list + const User *curr = head; + + while (curr != NULL) { + // if match is found, return the user + if (strcmp(curr->name, name) == 0){ + return (User *)curr; + } + curr = curr->next; + } + // end of the list was reached without finding user + return NULL; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T01:22:06.787Z,Question from Code,v1,do i have memory leak?,"/* + * 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) { + // check if the file name is too long + if (strlen(filename) >= sizeof(user->profile_pic) - 1){ + return 2; + } + + // declare FILE variable like in PCRS prep (week 3) + FILE *file; + file = fopen(filename, ""r""); + // check if failed to be opened + if (file == NULL){ + return 1; + } + + // can use unsafe strcpy since the filename can fit + // close the file that opened successfully + strcpy(user->profile_pic, filename); + fclose(file); + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T01:23:01.243Z,Question from Code,v1,do i have memory leak?,"/* + * 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) { + // initialize the head of the linked list + const User *curr = head; + + while (curr != NULL) { + // if match is found, return the user + if (strcmp(curr->name, name) == 0){ + return (User *)curr; + } + curr = curr->next; + } + // end of the list was reached without finding user + return 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) { + // find the users + User *user1 = find_user(name1, head); + User *user2 = find_user(name2, head); + //check if at least one user doesn't exist + if (!user1 || !user2){ + return 4; + } + + // check if the users are equal + else if (user1 == user2){ + return 3; + } + + // initialize the number of friends each user has + int user1_friends = 0; + int user2_friends = 0; + + // loop counts the number of friends each user has to see if they + // exceed the maximum number of friends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] != NULL) { + user1_friends++; + } + if (user2->friends[i] != NULL) { + user2_friends++; + } + } + if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { + return 2; + } + + // checks if the users are already friends + for (int j = 0; j < MAX_FRIENDS; j++) { + if (user1->friends[j] == user2) { + return 1; + } + } + + // otherwise, adds the users to each of their firends + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user1->friends[i] == NULL) { + user1->friends[i] = user2; + break; + } + } + for (int i = 0; i < MAX_FRIENDS; i++) { + if (user2->friends[i] == NULL) { + user2->friends[i] = user1; + break; + } + } + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T01:23:59.631Z,Question from Code,v1,do i have memory leak?,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +/* + * 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) { + // check if user is NULL + if (user == NULL) { + return 1; + } + + // check if there is a profile pic associated with the user + if (strlen(user->profile_pic) != 0){ + //open the profile pic + FILE *fptr; + fptr = fopen(user->profile_pic, ""r""); + if (fptr == NULL){ + printf(""Cannot open file \n""); + } + + // Read contents from file and print them + else { + char pic = fgetc(fptr); + while (pic != EOF){ + printf (""%c"", pic); + pic = fgetc(fptr); + } + } + fclose(fptr); + printf(""\n""); + } + // print name + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // print each friend in order in which they became friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = user->friends[i]; + if (friend != NULL) { + printf(""%s\n"", friend->name); + } + } + printf(""------------------------------------------\n""); + + printf(""Posts:\n""); + Post *current_post = user->first_post; + while (current_post != NULL) { + printf(""From: %s\n"", current_post->author); + // convert time_t to a string + printf(""Date: %s"", ctime(current_post->date)); + // checks if post is the last to see if ""==="" is needed + if (current_post->next == NULL){ + printf(""\n%s\n"", current_post->contents); + } + else { + printf(""\n%s\n\n===\n\n"", current_post->contents); + } + current_post = current_post->next; + } + printf(""------------------------------------------\n""); + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T01:25:17.079Z,Question from Code,v1,do i have memory leak?,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +/* + * 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) { + // check if user is NULL + if (user == NULL) { + return 1; + } + + // check if there is a profile pic associated with the user + if (strlen(user->profile_pic) != 0){ + //open the profile pic + FILE *fptr; + fptr = fopen(user->profile_pic, ""r""); + if (fptr == NULL){ + printf(""Cannot open file \n""); + } + + // Read contents from file and print them + else { + char pic = fgetc(fptr); + while (pic != EOF){ + printf (""%c"", pic); + pic = fgetc(fptr); + } + } + fclose(fptr); + printf(""\n""); + } + // print name + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // print each friend in order in which they became friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = user->friends[i]; + if (friend != NULL) { + printf(""%s\n"", friend->name); + } + } + printf(""------------------------------------------\n""); + + printf(""Posts:\n""); + Post *current_post = user->first_post; + while (current_post != NULL) { + printf(""From: %s\n"", current_post->author); + // convert time_t to a string + printf(""Date: %s"", ctime(current_post->date)); + // checks if post is the last to see if ""==="" is needed + if (current_post->next == NULL){ + printf(""\n%s\n"", current_post->contents); + } + else { + printf(""\n%s\n\n===\n\n"", current_post->contents); + } + current_post = current_post->next; + } + printf(""------------------------------------------\n""); + free(current_post->contents); + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T01:25:45.994Z,Question from Code,v1,did i free current_post->contents?,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +/* + * 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) { + // check if user is NULL + if (user == NULL) { + return 1; + } + + // check if there is a profile pic associated with the user + if (strlen(user->profile_pic) != 0){ + //open the profile pic + FILE *fptr; + fptr = fopen(user->profile_pic, ""r""); + if (fptr == NULL){ + printf(""Cannot open file \n""); + } + + // Read contents from file and print them + else { + char pic = fgetc(fptr); + while (pic != EOF){ + printf (""%c"", pic); + pic = fgetc(fptr); + } + } + fclose(fptr); + printf(""\n""); + } + // print name + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // print each friend in order in which they became friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = user->friends[i]; + if (friend != NULL) { + printf(""%s\n"", friend->name); + } + } + printf(""------------------------------------------\n""); + + printf(""Posts:\n""); + Post *current_post = user->first_post; + while (current_post != NULL) { + printf(""From: %s\n"", current_post->author); + // convert time_t to a string + printf(""Date: %s"", ctime(current_post->date)); + // checks if post is the last to see if ""==="" is needed + if (current_post->next == NULL){ + printf(""\n%s\n"", current_post->contents); + } + else { + printf(""\n%s\n\n===\n\n"", current_post->contents); + } + current_post = current_post->next; + } + printf(""------------------------------------------\n""); + free(current_post->contents); + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T01:26:04.010Z,Question from Code,v1,how do i free current_post->contents?,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +/* + * 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) { + // check if user is NULL + if (user == NULL) { + return 1; + } + + // check if there is a profile pic associated with the user + if (strlen(user->profile_pic) != 0){ + //open the profile pic + FILE *fptr; + fptr = fopen(user->profile_pic, ""r""); + if (fptr == NULL){ + printf(""Cannot open file \n""); + } + + // Read contents from file and print them + else { + char pic = fgetc(fptr); + while (pic != EOF){ + printf (""%c"", pic); + pic = fgetc(fptr); + } + } + fclose(fptr); + printf(""\n""); + } + // print name + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // print each friend in order in which they became friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = user->friends[i]; + if (friend != NULL) { + printf(""%s\n"", friend->name); + } + } + printf(""------------------------------------------\n""); + + printf(""Posts:\n""); + Post *current_post = user->first_post; + while (current_post != NULL) { + printf(""From: %s\n"", current_post->author); + // convert time_t to a string + printf(""Date: %s"", ctime(current_post->date)); + // checks if post is the last to see if ""==="" is needed + if (current_post->next == NULL){ + printf(""\n%s\n"", current_post->contents); + } + else { + printf(""\n%s\n\n===\n\n"", current_post->contents); + } + current_post = current_post->next; + } + printf(""------------------------------------------\n""); + free(current_post->contents); + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T01:26:31.785Z,Question from Code,v1,what method do i call to free current_post->contents and where?,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +/* + * 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) { + // check if user is NULL + if (user == NULL) { + return 1; + } + + // check if there is a profile pic associated with the user + if (strlen(user->profile_pic) != 0){ + //open the profile pic + FILE *fptr; + fptr = fopen(user->profile_pic, ""r""); + if (fptr == NULL){ + printf(""Cannot open file \n""); + } + + // Read contents from file and print them + else { + char pic = fgetc(fptr); + while (pic != EOF){ + printf (""%c"", pic); + pic = fgetc(fptr); + } + } + fclose(fptr); + printf(""\n""); + } + // print name + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // print each friend in order in which they became friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = user->friends[i]; + if (friend != NULL) { + printf(""%s\n"", friend->name); + } + } + printf(""------------------------------------------\n""); + + printf(""Posts:\n""); + Post *current_post = user->first_post; + while (current_post != NULL) { + printf(""From: %s\n"", current_post->author); + // convert time_t to a string + printf(""Date: %s"", ctime(current_post->date)); + // checks if post is the last to see if ""==="" is needed + if (current_post->next == NULL){ + printf(""\n%s\n"", current_post->contents); + } + else { + printf(""\n%s\n\n===\n\n"", current_post->contents); + } + current_post = current_post->next; + } + printf(""------------------------------------------\n""); + free(current_post->contents); + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T01:27:34.348Z,Question from Code,v1,where are my memory leaks?,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +/* + * 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) { + // check if user is NULL + if (user == NULL) { + return 1; + } + + // check if there is a profile pic associated with the user + if (strlen(user->profile_pic) != 0){ + //open the profile pic + FILE *fptr; + fptr = fopen(user->profile_pic, ""r""); + if (fptr == NULL){ + printf(""Cannot open file \n""); + } + + // Read contents from file and print them + else { + char pic = fgetc(fptr); + while (pic != EOF){ + printf (""%c"", pic); + pic = fgetc(fptr); + } + } + fclose(fptr); + printf(""\n""); + } + // print name + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // print each friend in order in which they became friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = user->friends[i]; + if (friend != NULL) { + printf(""%s\n"", friend->name); + } + } + printf(""------------------------------------------\n""); + + printf(""Posts:\n""); + Post *current_post = user->first_post; + while (current_post != NULL) { + printf(""From: %s\n"", current_post->author); + // convert time_t to a string + printf(""Date: %s"", ctime(current_post->date)); + // checks if post is the last to see if ""==="" is needed + if (current_post->next == NULL){ + printf(""\n%s\n"", current_post->contents); + } + else { + printf(""\n%s\n\n===\n\n"", current_post->contents); + free(current_post->contents); + } + current_post = current_post->next; + } + printf(""------------------------------------------\n""); + free(user); + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T01:28:35.641Z,Question from Code,v1,do i have memory leaks?,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +/* + * 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) { + // check if user is NULL + if (user == NULL) { + return 1; + } + + // check if there is a profile pic associated with the user + if (strlen(user->profile_pic) != 0){ + //open the profile pic + FILE *fptr; + fptr = fopen(user->profile_pic, ""r""); + if (fptr == NULL){ + printf(""Cannot open file \n""); + } + + // Read contents from file and print them + else { + char pic = fgetc(fptr); + while (pic != EOF){ + printf (""%c"", pic); + pic = fgetc(fptr); + } + } + fclose(fptr); + printf(""\n""); + } + // print name + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // print each friend in order in which they became friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = user->friends[i]; + if (friend != NULL) { + printf(""%s\n"", friend->name); + } + } + printf(""------------------------------------------\n""); + + printf(""Posts:\n""); + Post *current_post = user->first_post; + while (current_post != NULL) { + printf(""From: %s\n"", current_post->author); + // convert time_t to a string + printf(""Date: %s"", ctime(current_post->date)); + // checks if post is the last to see if ""==="" is needed + if (current_post->next == NULL){ + printf(""\n%s\n"", current_post->contents); + free(current_post->contents); + } + else { + printf(""\n%s\n\n===\n\n"", current_post->contents); + free(current_post->contents); + } + current_post = current_post->next; + } + printf(""------------------------------------------\n""); + free(user); + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T01:29:16.062Z,Question from Code,v1,how to correct the memory leaks?,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +/* + * 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) { + // check if user is NULL + if (user == NULL) { + return 1; + } + + // check if there is a profile pic associated with the user + if (strlen(user->profile_pic) != 0){ + //open the profile pic + FILE *fptr; + fptr = fopen(user->profile_pic, ""r""); + if (fptr == NULL){ + printf(""Cannot open file \n""); + } + + // Read contents from file and print them + else { + char pic = fgetc(fptr); + while (pic != EOF){ + printf (""%c"", pic); + pic = fgetc(fptr); + } + } + fclose(fptr); + printf(""\n""); + } + // print name + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // print each friend in order in which they became friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = user->friends[i]; + if (friend != NULL) { + printf(""%s\n"", friend->name); + } + } + printf(""------------------------------------------\n""); + + printf(""Posts:\n""); + Post *current_post = user->first_post; + while (current_post != NULL) { + printf(""From: %s\n"", current_post->author); + // convert time_t to a string + printf(""Date: %s"", ctime(current_post->date)); + // checks if post is the last to see if ""==="" is needed + if (current_post->next == NULL){ + printf(""\n%s\n"", current_post->contents); + free(current_post->contents); + } + else { + printf(""\n%s\n\n===\n\n"", current_post->contents); + free(current_post->contents); + } + current_post = current_post->next; + } + printf(""------------------------------------------\n""); + free(user); + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T01:30:53.364Z,Question from Code,v1,do i still have memory leak?,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +/* + * 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) { + // check if user is NULL + if (user == NULL) { + return 1; + } + + // check if there is a profile pic associated with the user + if (strlen(user->profile_pic) != 0){ + //open the profile pic + FILE *fptr; + fptr = fopen(user->profile_pic, ""r""); + if (fptr == NULL){ + printf(""Cannot open file \n""); + } + + // Read contents from file and print them + else { + char pic = fgetc(fptr); + while (pic != EOF){ + printf (""%c"", pic); + pic = fgetc(fptr); + } + } + fclose(fptr); + printf(""\n""); + } + // print name + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // print each friend in order in which they became friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = user->friends[i]; + if (friend != NULL) { + printf(""%s\n"", friend->name); + } + } + printf(""------------------------------------------\n""); + + printf(""Posts:\n""); + Post *current_post = user->first_post; + while (current_post != NULL) { + printf(""From: %s\n"", current_post->author); + // convert time_t to a string + printf(""Date: %s"", ctime(current_post->date)); + // checks if post is the last to see if ""==="" is needed + if (current_post->next == NULL){ + printf(""\n%s\n"", current_post->contents); + } + else { + printf(""\n%s\n\n===\n\n"", current_post->contents); + } + current_post = current_post->next; + } + printf(""------------------------------------------\n""); + free(user->profile_pic); + free(user->friends); + free(user->first_post); + free(current_post->contents); + free(user); + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T01:31:47.266Z,Question from Code,v1,do i have memory leak?,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +/* + * 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) { + // check if user is NULL + if (user == NULL) { + return 1; + } + + // check if there is a profile pic associated with the user + if (strlen(user->profile_pic) != 0){ + //open the profile pic + FILE *fptr; + fptr = fopen(user->profile_pic, ""r""); + if (fptr == NULL){ + printf(""Cannot open file \n""); + } + + // Read contents from file and print them + else { + char pic = fgetc(fptr); + while (pic != EOF){ + printf (""%c"", pic); + pic = fgetc(fptr); + } + } + fclose(fptr); + printf(""\n""); + } + // print name + printf(""Name: %s\n"", user->name); + printf(""------------------------------------------\n""); + + // print each friend in order in which they became friends + printf(""Friends:\n""); + for (int i = 0; i < MAX_FRIENDS; i++) { + User *friend = user->friends[i]; + if (friend != NULL) { + printf(""%s\n"", friend->name); + } + } + printf(""------------------------------------------\n""); + + printf(""Posts:\n""); + Post *current_post = user->first_post; + while (current_post != NULL) { + printf(""From: %s\n"", current_post->author); + // convert time_t to a string + printf(""Date: %s"", ctime(current_post->date)); + // checks if post is the last to see if ""==="" is needed + if (current_post->next == NULL){ + printf(""\n%s\n"", current_post->contents); + free(current_post->contents); + } + else { + printf(""\n%s\n\n===\n\n"", current_post->contents); + free(current_post->contents); + } + current_post = current_post->next; + } + printf(""------------------------------------------\n""); + free(user->profile_pic); + free(user->friends); + free(user->first_post); + + return 0; +}",, +63c6485f5f004bf4898cfbe4,2023-02-11T01:42:51.363Z,Help Fix Code,v1,,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +/* + * 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) { + User *curr1 = *user_ptr_add; + + //first, check if user with same name exists + while(curr1 != NULL){ + if (strcmp(curr1->name, name) == 0){ + return 1; + } + curr1 = curr1->next; + } + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + User *curr2 = *user_ptr_add; + if (curr2 == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr2->next != NULL){ + curr2 = curr2->next; + } + curr2->next = new_user; + } + + return 0; +}",this function is not supposed to have any memory leaks but it does, +63c6485f5f004bf4898cfbe4,2023-02-11T01:47:15.563Z,Help Fix Code,v1,,"#define MAX_NAME 32 // Max username and profile_pic filename lengths +#define MAX_FRIENDS 10 // Max number of friends a user can have + +typedef struct user { + char name[MAX_NAME]; + char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. + struct post *first_post; + struct user *friends[MAX_FRIENDS]; + struct user *next; +} User; + +typedef struct post { + char author[MAX_NAME]; + char *contents; + time_t *date; + struct post *next; +} Post; + +/* + * 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) { + User *curr1 = *user_ptr_add; + + //first, check if user with same name exists + while(curr1 != NULL){ + if (strcmp(curr1->name, name) == 0){ + return 1; + } + curr1 = curr1->next; + } + free(curr1); + + // next, check if the name cannot fit in the 'name' array + // the '-1' reserves space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = (User *)malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + User *curr2 = *user_ptr_add; + if (curr2 == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr2->next != NULL){ + curr2 = curr2->next; + } + curr2->next = new_user; + } + + return 0; +}",this function is not supposed to have any memory leaks but it does, +63c6485f5f004bf4898cfbe4,2023-02-12T03:45:03.614Z,Question from Code,v1,I need to terminate the program using a non-zero status if malloc returns NULL. Using status 3 gives me a segmentation fault. what do I do?,"/* + * 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) { + User *curr1 = *user_ptr_add; + + //check if user with same name exists + while(curr1 != NULL){ + if (strcmp(curr1->name, name) == 0){ + return 1; + } + curr1 = curr1->next; + } + + // check if the name cannot fit in the 'name' array + // use -1 to save space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = malloc(sizeof(User)); + if (new_user == NULL){ + perror(""Cannot allocate space for user""); + exit(3); + } + + strcpy(new_user->name, name); + new_user->next = NULL; + + User *curr2 = *user_ptr_add; + if (curr2 == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr2->next != NULL){ + curr2 = curr2->next; + } + } + + curr2->next = new_user; + for (int i = 0; i < MAX_FRIENDS; i++) { + new_user->friends[i] = NULL; + } + for (int i = 0; i < MAX_FRIENDS; i++) { + new_user->profile_pic[i] = '\0'; + } + new_user->first_post = NULL; + return 0; +} +",, +63c6485f5f004bf4898cfbe4,2023-02-12T04:33:28.312Z,Question from Code,v1,where is my memory leak?,"/* + * 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 || !target) { + return 2; + } + + // numerical Boolean to make predicate logic easy + int friend = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + friend = 1; + break; + } + } + + if (!friend) { + return 1; + } + // allocate space for a new post + Post *newPost = (Post*)malloc(sizeof(Post)); + // allocate space for the time + time_t *now = (time_t*)malloc(sizeof(time_t)); + if (newPost != NULL && now != NULL) { + strcpy(newPost->author, author->name); + newPost->contents = contents; + time(now); + newPost->date = now;; + newPost->next = target->first_post; + target->first_post = newPost; + + return 0; + } + + else { + perror(""Cannot allocate space""); + exit(3); + } +} +",, +63c6485f5f004bf4898cfbe4,2023-02-12T04:43:41.767Z,Question from Code,v1,contents doesn't need to be freed. what is causing the memory leak?,"/* + * 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 || !target) { + return 2; + } + + // numerical Boolean to make predicate logic easy + int friend = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + friend = 1; + break; + } + } + + if (!friend) { + return 1; + } + // allocate space for a new post + Post *newPost = (Post*)malloc(sizeof(Post)); + // allocate space for the time + time_t *now = (time_t*)malloc(sizeof(time_t)); + if (newPost != NULL && now != NULL) { + strcpy(newPost->author, author->name); + newPost->contents = contents; + time(now); + newPost->date = now;; + newPost->next = target->first_post; + target->first_post = newPost; + + return 0; + } + + else { + perror(""Cannot allocate space""); + exit(3); + } +} +",, +63c6485f5f004bf4898cfbe4,2023-02-12T04:44:10.098Z,Question from Code,v1,contents doesn't need to be freed. what is causing the memory leak?,"/* + * 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 || !target) { + return 2; + } + + // numerical Boolean to make predicate logic easy + int friend = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + friend = 1; + break; + } + } + + if (!friend) { + return 1; + } + // allocate space for a new post + Post *newPost = (Post*)malloc(sizeof(Post)); + // allocate space for the time + time_t *now = (time_t*)malloc(sizeof(time_t)); + if (newPost != NULL && now != NULL) { + strcpy(newPost->author, author->name); + newPost->contents = contents; + time(now); + newPost->date = now;; + newPost->next = target->first_post; + target->first_post = newPost; + free(now); + + return 0; + } + + else { + perror(""Cannot allocate space""); + exit(3); + } +} +",, +63c6485f5f004bf4898cfbe4,2023-02-12T18:30:13.983Z,Question from Code,v1,do i have a memory leak?,"int create_user(const char *name, User **user_ptr_add) { + User *curr1 = *user_ptr_add; + + //check if user with same name exists + while(curr1 != NULL){ + if (strcmp(curr1->name, name) == 0){ + return 1; + } + curr1 = curr1->next; + } + + // check if the name cannot fit in the 'name' array + // use -1 to save space for the null terminator + if (strlen(name) >= MAX_NAME - 1){ + return 2; + } + + // create a new user and add it to the end of the list + User *new_user = malloc(sizeof(User)); + strcpy(new_user->name, name); + new_user->next = NULL; + + User *curr2 = *user_ptr_add; + if (new_user != NULL) { + if (curr2 == NULL) { + *user_ptr_add = new_user; + } + else { + while (curr2->next != NULL){ + curr2 = curr2->next; + } + curr2->next = new_user; + } + + for (int i = 0; i < MAX_FRIENDS; i++) { + new_user->friends[i] = NULL; + } + for (int i = 0; i < MAX_FRIENDS; i++) { + new_user->profile_pic[i] = '\0'; + } + new_user->first_post = NULL; + return 0; + } + + else { + perror(""Cannot allocate space""); + exit(3); + } +}",, +63c6485f5f004bf4898cfbe4,2023-02-12T18:31:31.025Z,Help Write Code,v1,,,,"/* + * 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 + */" +63c6485f5f004bf4898cfbe4,2023-02-12T18:39:11.302Z,Help Fix Code,v1,,"/* + * 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 || !target) { + return 2; + } + + int friend = 0; + for (int i = 0; i < MAX_FRIENDS; i++) { + if (target->friends[i] == author) { + friend = 1; + break; + } + } + + if (friend == 0) { + return 1; + } + // allocate space for a new post + Post *new_post = malloc(sizeof(Post)); + // allocate space for the time + time_t *now = malloc(sizeof(time_t)); + if (new_post != NULL) { + strcpy(new_post->author, author->name); + new_post->contents = contents; + time(now); + new_post->date = now;; + new_post->next = target->first_post; + target->first_post = new_post; + + return 0; + } + + else { + free(new_post); + free(now); + perror(""Cannot allocate space""); + exit(3); + } +}",valgrind tells me that line 32 is causing memory leak. what can I do to fix this?, +63c6485f5f004bf4898cfbe4,2023-02-23T23:51:53.568Z,Help Write Code,v1,,,,"/* 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. + */ +Notice that there is one struct rule_node for each rule in the original makefile and an additional struct rule_node for each dependency that is not also a target. You must follow this design. You are are also required to have the head of the list be the first rule in the original makefile. However, depending on how you do your parsing, the struct rule_node elements may come in a different order in your linked list of rules. You will see that the actions are stored in a struct action_node that has a member args. This array has the format required by the second parameter to execvp. Use the man page for execvp to understand this format. In particular, notice that the first element is the executable name, subsequent elements are the arguments for that executable, and these are followed by an extra NULL pointer element. + +Rule *parse_file(FILE *fp) { + // Implement this function and remove the stubbed return statement below. + return NULL; +} +" +63c6485f5f004bf4898cfbe4,2023-02-24T19:57:33.697Z,Help Fix Code,v1,,"/* 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(strtok(line, "" \t\n"")); + + // Parse the dependencies and create a new Dependency node for each of them + Dependency *dep_head = NULL, *dep_curr = NULL; + char *dep_str = strtok(NULL, "" \t\n""); + while (dep_str != NULL) { + 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 (dep_curr == NULL) { + dep_head = dep; + } else { + dep_curr->next_dep = dep; + } + dep_curr = dep; + dep_str = strtok(NULL, "" \t\n""); + } + rule->dependencies = dep_head; + + // 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; +}","when i run this to make a makefile, the first letter of every target is missing and there are two colons instead of one.", +63c6485f5f004bf4898cfbe4,2023-02-24T20:08:25.846Z,Help Fix Code,v1,,"/* 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(strtok(line, "" \t\n"")); + + // Parse the dependencies and create a new Dependency node for each of them + Dependency *dep_head = NULL, *dep_curr = NULL; + char *dep_str = strtok(NULL, "" \t\n""); + while (dep_str != NULL) { + 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 (dep_curr == NULL) { + dep_head = dep; + } else { + dep_curr->next_dep = dep; + } + dep_curr = dep; + dep_str = strtok(NULL, "" \t\n""); + } + rule->dependencies = dep_head; + + // 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 first letter in the target is missing and there are two colons showing up after the target instead of one. i did not use strtok(line, "" \t\n"") + 1 or strtok(NULL, "" \t\n"") + 1", +63c6485f5f004bf4898cfbe4,2023-02-24T20:10:40.614Z,Help Fix Code,v1,,"/* 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(strtok(line, "" \t\n"")); + + // Parse the dependencies and create a new Dependency node for each of them + Dependency *dep_head = NULL, *dep_curr = NULL; + char *dep_str = strtok(NULL, "" \t\n""); + while (dep_str != NULL) { + 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 (dep_curr == NULL) { + dep_head = dep; + } else { + dep_curr->next_dep = dep; + } + dep_curr = dep; + dep_str = strtok(NULL, "" \t\n""); + } + rule->dependencies = dep_head; + + // 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 first letter in the target is not supposed to be missing and there is supposed to be one colon not two colons showing up after the target , +63c6485f5f004bf4898cfbe4,2023-02-24T20:11:33.317Z,Question from Code,v1,how do i changing strtok() to parse the target until a colon.,"/* 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(strtok(line, "" \t\n"")); + + // Parse the dependencies and create a new Dependency node for each of them + Dependency *dep_head = NULL, *dep_curr = NULL; + char *dep_str = strtok(NULL, "" \t\n""); + while (dep_str != NULL) { + 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 (dep_curr == NULL) { + dep_head = dep; + } else { + dep_curr->next_dep = dep; + } + dep_curr = dep; + dep_str = strtok(NULL, "" \t\n""); + } + rule->dependencies = dep_head; + + // 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; +} + +",, +63c6485f5f004bf4898cfbe4,2023-02-25T00:32:29.390Z,Question from Code,v1,"why am i getting a segmentation fault +","#include +#include +#include + +#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; +} +",, +63c6485f5f004bf4898cfbe4,2023-02-25T00:33:14.712Z,Question from Code,v1,why am i getting a segmentation fault? cur_rule and cur_action variables are initialized to NULL,"#include +#include +#include + +#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; +} +",, +63c6485f5f004bf4898cfbe4,2023-02-25T00:34:49.220Z,Question from Code,v1,how do i fix the seg fault?,"#include +#include +#include + +#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; +} +",, +63c6485f5f004bf4898cfbe4,2023-02-25T00:35:51.405Z,Question from Code,v1,how do i initialize cur_rule to NULL before the first iteration of the loop.,"#include +#include +#include + +#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; +} +",, +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 +#include +#include + +#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; +} +",, +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; +",, +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; +} +",, +63c6485f5f004bf4898cfbe4,2023-03-04T00:02:16.700Z,Help Fix Code,v1,,"/* 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? ", +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; +} +",, +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; +} +",, +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; +}",, +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; +}",, +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.,,, +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; +} +",, +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; +} +",, +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; +} +",, +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; +} +",, +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; +} +",, +63c6485f5f004bf4898cfbe4,2023-04-19T22:04:52.383Z,Explain Code,v2,,"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; +}",, +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);",, +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);",, +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);",, +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);",, +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);",, +63c648625f004bf4898cfbfd,2023-01-26T19:40:11.489Z,Explain Code,v1,,"char **c; +*c = char[2];",, +63c648625f004bf4898cfbfd,2023-01-26T23:04:19.750Z,Question from Code,v1,"what is &i? +","int *i; +",, +63c648625f004bf4898cfbfd,2023-01-27T23:55:20.088Z,Explain Code,v1,,"char **data; +scanf(ls -l, data);",, +63c648625f004bf4898cfbfd,2023-01-27T23:58:02.632Z,Help Write Code,v1,,,,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,,, +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; +} + ",, +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; +} + ",, +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; +} ",, +63c648635f004bf4898cfc07,2023-02-10T19:51:30.244Z,Help Fix Code,v1,,"/* + * 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, +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; +} ",, +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; +} ",, +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; +} + + ",, +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; +} ",, +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; +} + ",, +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; +} + ",, +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; +} + ",, +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; + } +} + ",, +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; + } +} + ",, +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; +}",, +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; +}",, +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; +} +",, +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; +}",, +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; +}",, +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; +}",, +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; +}",, +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; +}",, +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; +}",, +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; +}",, +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; +}",, +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; +}",, +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; +}",, +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; +} +",, +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; +} +",, +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; +}",, +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; +} +",, +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; +} +",, +63c648635f004bf4898cfc07,2023-02-11T19:34:30.747Z,Help Write Code,v1,,,,"* + * 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,,,,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,,, +63c648635f004bf4898cfc07,2023-02-11T19:36:23.797Z,General Question,v1,how to read from an ASCII file not knowing the size of the file,,, +63c648635f004bf4898cfc07,2023-02-11T20:37:04.941Z,Help Write Code,v1,,,,"/* + * 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 ,,, +63c648635f004bf4898cfc07,2023-02-11T22:02:39.998Z,General Question,v1,how to add an element to the front of a non-empty linked struct,,, +63c648635f004bf4898cfc07,2023-02-11T22:09:29.320Z,General Question,v1,"give me the code to get the current time in c +",,, +63c648635f004bf4898cfc07,2023-02-11T22:11:14.040Z,Help Write Code,v1,,,,how to get the current time in c +63c648635f004bf4898cfc07,2023-02-11T22:13:12.588Z,Help Write Code,v1,,,,how to get the current time using ctime and time +63c648635f004bf4898cfc07,2023-02-11T23:46:19.387Z,Help Write Code,v1,,,,"/* + * 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. + */ ",, +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; +} ",, +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; +} ",, +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; +} ",, +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; +}",, +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; +}",, +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; +} ",, +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; +} ",, +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; +} ",, +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; +}",, +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; +}",, +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; +}",, +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; +}",, +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; +}",, +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; +}",, +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?,,, +63c648635f004bf4898cfc07,2023-02-24T21:26:23.308Z,General Question,v1,when should i use a single and double pointer as parameter?,,, +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?,,, +63c648635f004bf4898cfc07,2023-02-24T21:28:35.309Z,General Question,v1,does malloc modify the original pointer?,,, +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; + +} +",, +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; +} +",, +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; +} +",, +63c648635f004bf4898cfc07,2023-02-24T21:30:52.136Z,General Question,v1,i am confused on when i should use a double pointer as parameter,,, +63c648635f004bf4898cfc07,2023-02-24T21:31:27.437Z,General Question,v1,"is passing a double pointer as parameter only used for nested structures? +",,, +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?",,, +63c648635f004bf4898cfc07,2023-02-24T21:38:07.303Z,General Question,v1,where should i start for assignment 3,,, +63c648635f004bf4898cfc07,2023-03-10T01:21:34.992Z,General Question,v2,"for assignment 3, how do i initialize the structs in parse file?",,, +63c648635f004bf4898cfc07,2023-03-10T01:23:03.738Z,General Question,v2,can you give me the psuedo code for parse file in A3?,,, +63c648635f004bf4898cfc07,2023-03-10T01:23:45.849Z,General Question,v2,how do you use strtok and fgets to parse thorough a makefile?,,, +63c648635f004bf4898cfc07,2023-03-10T17:52:42.159Z,General Question,v2,"how to assign a token from strtok() to char *? +",,, +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) { + ",, +63c648635f004bf4898cfc07,2023-03-10T19:35:30.486Z,General Question,v2,"What is \t? +",,, +63c648635f004bf4898cfc07,2023-03-10T19:35:57.777Z,General Question,v2,how to use isspace?,,, +63c648635f004bf4898cfc07,2023-03-10T22:52:18.657Z,General Question,v2,how to traverse through strtok token?,,, +63c648635f004bf4898cfc07,2023-03-11T00:11:34.479Z,General Question,v2,"how to know if strdup is working? i cant print the string returned by strdup? +",,, +63c648635f004bf4898cfc07,2023-03-11T00:20:02.997Z,Question from Code,v2,Why can't I print the head->target?,"head = malloc(sizeof(Rule)); +head->target = strdup(token); +printf(""%s\n"", head->target);",, +63c648635f004bf4898cfc07,2023-03-11T00:21:14.735Z,Question from Code,v2,why is this not printing the head->target?,"while (fgets(line, MAXLINE, fp) != NULL) { + line[MAXLINE - 1] = '\n'; + + if (is_comment_or_empty(line) != 1) { + char *token; + token = strtok(line, "" ""); + + while (token != NULL) { + if (token[0] != '\t' && token[0] != '\n') { + if (head == NULL) { + head = malloc(sizeof(Rule)); + head->target = strdup(token); + head->dependencies = NULL; + head->actions = NULL; + head->next_rule = NULL; + } + } + token = strtok(NULL, "" ""); + } + } +} +printf(""%s\n"", head->target); +}",, +63c648635f004bf4898cfc07,2023-03-11T00:30:11.386Z,General Question,v2,Why does the token print a bunch of blank lines at the beginning?,,, +63c648635f004bf4898cfc07,2023-03-11T04:06:43.490Z,Question from Code,v2,why does this while loop never end?,"while (token != NULL) { + if (token[0] != '\t' && (isspace(token[0]) == 0) && (strcmp(token, "":"") != 0)) { + if (head == NULL) { + r = malloc(sizeof(Rule)); + r->target = strdup(token); + r->dependencies = NULL; + r->actions = NULL; + r->next_rule = NULL; + head = r; + curr = head; + } + + if (curr->dependencies == NULL) { + dep = malloc(sizeof(Dependency)); + + if (strcmp(curr->target, token) != 0) { + r = malloc(sizeof(Rule)); + r->target = strdup(token); + r->dependencies = NULL; + r->actions = NULL; + r->next_rule = NULL; + dep->next_dep = NULL; + dep->rule = r; + printf(""%s\n"", dep->rule->target); + curr->dependencies = dep; + } + + } else { + while (curr->dependencies->next_dep != NULL) { + curr->dependencies = curr->dependencies->next_dep; + } + + r = malloc(sizeof(Rule)); + r->target = strdup(token); + r->actions = NULL; + r->next_rule = NULL; + dep->next_dep = NULL; + dep->rule = r; + curr->dependencies->next_dep = dep; + printf(""%s\n"", curr->dependencies->next_dep->rule->target); + } + } + + token = strtok(NULL, "" ""); +} +}",, +63c648635f004bf4898cfc07,2023-03-11T04:09:05.928Z,Question from Code,v2,is this the correct way to loop to the last element?,"while (curr->dependencies->next_dep != NULL) { + curr->dependencies = curr->dependencies->next_dep; +} +",, +63c648635f004bf4898cfc07,2023-03-11T04:10:47.719Z,Question from Code,v2,why does this give me segmentation fault?,"while (curr->dependencies != NULL) { + curr->dependencies = curr->dependencies->next_dep; +} + +r = malloc(sizeof(Rule)); +r->target = strdup(token); +r->actions = NULL; +r->next_rule = NULL; +dep->next_dep = NULL; +dep->rule = r; +curr->dependencies = dep; +printf(""%s\n"", curr->dependencies->next_dep->rule->target); +} +}",, +63c648635f004bf4898cfc07,2023-03-11T04:11:39.138Z,Question from Code,v2,why is this giving me segmentation fault?,"if (curr->dependencies == NULL) { + dep = malloc(sizeof(Dependency)); + + if (strcmp(curr->target, token) != 0) { + r = malloc(sizeof(Rule)); + r->target = strdup(token); + r->dependencies = NULL; + r->actions = NULL; + r->next_rule = NULL; + dep->next_dep = NULL; + dep->rule = r; + printf(""%s\n"", dep->rule->target); + curr->dependencies = dep; + } + +} else { + while (curr->dependencies != NULL) { + curr->dependencies = curr->dependencies->next_dep; + } + + r = malloc(sizeof(Rule)); + r->target = strdup(token); + r->actions = NULL; + r->next_rule = NULL; + dep->next_dep = NULL; + dep->rule = r; + curr->dependencies = dep; + printf(""%s\n"", curr->dependencies->next_dep->rule->target); +} +}",, +63c648635f004bf4898cfc07,2023-03-11T04:33:26.682Z,Question from Code,v2,why is this printing false twice?,"while (fgets(line, MAXLINE, fp) != NULL) { + line[MAXLINE - 1] = '\0'; + + int i = MAXLINE - 1; + while (isspace(line[i]) != 0) { + line[i] = '\0'; + i--; + } + + if (is_comment_or_empty(line) != 1) { + char *token; + token = strtok(line, "" ""); + + while (token != NULL) { + if (token[0] != '\t' && (isspace(token[0]) == 0) && (strcmp(token, "":"") != 0)) { + if (head == NULL) { + printf(""true\n""); + r = malloc(sizeof(Rule)); + r->target = strdup(token); + r->dependencies = NULL; + r->actions = NULL; + r->next_rule = NULL; + head = r; + curr = head; + } + + if (curr->dependencies == NULL) { + printf(""false\n""); + dep = malloc(sizeof(Dependency)); + + if (strcmp(curr->target, token) != 0) { + printf(""nani?\n""); + r = malloc(sizeof(Rule)); + r->target = strdup(token); + r->dependencies = NULL; + r->actions = NULL; + r->next_rule = NULL; + dep->next_dep = NULL; + dep->rule = r; + curr->dependencies = dep; + printf(""%s\n"", curr->dependencies->rule->target); + if (curr->dependencies == NULL) { + printf(""what?\n""); + } + } + + } else { + } + } + + token = strtok(NULL, "" ""); + } + } +}",, +63c648635f004bf4898cfc07,2023-03-11T04:45:31.260Z,Question from Code,v2,does this loop to the second element when there is only one element in the linked list?,"while (curr->dependencies != NULL) { + curr->dependencies = curr->dependencies->next_dep; +}",, +63c648635f004bf4898cfc07,2023-03-11T16:04:11.004Z,Question from Code,v2,is this causing a segmentation fault?,"while (curr->dependencies->next_dep != NULL) { + curr->dependencies = curr->dependencies->next_dep; +} + +curr->dependencies->next_dep = dep;",, +63c648635f004bf4898cfc07,2023-03-11T16:05:31.355Z,Question from Code,v2,will this cause segmentation fault?," +while (curr->dependencies->next_dep != NULL) { + curr->dependencies = curr->dependencies->next_dep; +} + +if (curr->dependencies != NULL) { + curr->dependencies->next_dep = dep; +}",, +63c648635f004bf4898cfc07,2023-03-11T18:43:32.087Z,Question from Code,v2,why is this giving me segmentation fault?," +if (token[0] == '\t') { + printf(""true\n""); + printf(""%s\n"", token); + + token = strtok(NULL, "" ""); + + a = malloc(sizeof(Action)); + char** arg = NULL; + int i = 0; + while (token != NULL) { + arg[i] = strdup(token); + token = strtok(NULL, "" ""); + i++; + }",, +63c648635f004bf4898cfc07,2023-03-11T18:45:13.784Z,Question from Code,v2,will this give me segmentation fault?," +if (token[0] == '\t') { + printf(""true\n""); + printf(""%s\n"", token); + + token = strtok(NULL, "" ""); + + a = malloc(sizeof(Action)); + char** arg = a->args; + int i = 0; + while (token != NULL) { + arg[i] = strdup(token); + token = strtok(NULL, "" ""); + i++; + }",, +63c648635f004bf4898cfc07,2023-03-11T18:47:31.879Z,Question from Code,v2,why is this giving me segmentation fault?," + +if (token[0] == '\t') { + printf(""true\n""); + printf(""%s\n"", token); + + a = malloc(sizeof(Action)); + char** arg = a->args; + int i = 0; + while (token != NULL) { + arg[i] = strdup(token); + token = strtok(NULL, "" ""); + i++; + }",, +63c648635f004bf4898cfc07,2023-03-11T18:51:19.770Z,Question from Code,v2,do i need to malloc for args?,"typedef struct action_node { + char **args; + struct action_node *next_act; +} Action;",, +63c648635f004bf4898cfc07,2023-03-11T18:57:43.986Z,Question from Code,v2,how would you malloc for args without knowing how many char * you will be storing?,"typedef struct action_node { + char **args; + struct action_node *next_act; +} Action;",, +63c648635f004bf4898cfc07,2023-03-11T19:08:40.822Z,Question from Code,v2,why is this giving me segmentation ,"char *token; +token = strtok(line, "" ""); + +if (token != NULL && token[0] == '\t') { + a = malloc(sizeof(Action)); + char **arg = malloc(sizeof(char *) * MAXLINE); + int i = 0; + + while (token != NULL) { + arg[i] = strdup(token); + token = strtok(NULL, "" ""); + i++; + } + a->args = arg; + a->next_act = NULL; + int j = 0; + + while (arg != NULL) { + printf(""%s\n"", arg[j]); + j++; + } +}",, +63c648635f004bf4898cfc07,2023-03-11T20:02:46.946Z,Question from Code,v2,why is this giving me segmentation fault?,"while (curr->dependencies->next_dep != NULL) { + curr->dependencies = curr->dependencies->next_dep; +} + +curr->dependencies->next_dep->rule = r2; + +printf(""%s\n"", curr->dependencies->rule->target);",, +63c648635f004bf4898cfc07,2023-03-11T20:15:22.865Z,Question from Code,v2,why is this giving me segmentation fault?,"Rule *curr2 = head; +Rule *r = NULL; + +while (curr2 != NULL) { + if (curr2->target != NULL && strcmp(curr2->target, token) == 0) { + r = curr2; + } + curr2 = curr2->next_rule; +} +",, +63c648635f004bf4898cfc07,2023-03-11T20:45:32.364Z,Question from Code,v2,does this loop to the last element?,"while (curr->next_rule != NULL) { + curr->next_rule; +} +",, +63c648635f004bf4898cfc07,2023-03-11T20:46:03.580Z,Question from Code,v2,does this loop to the last element?,"while (curr->next_rule != NULL) { + curr = curr->next_rule; +}",, +63c648635f004bf4898cfc07,2023-03-11T20:53:18.279Z,Question from Code,v2,why is this giving me infinite while loop,"while (curr->next_rule != NULL) { + curr = curr->next_rule; +}",, +63c648635f004bf4898cfc07,2023-03-11T20:55:08.440Z,General Question,v2,does this loop to the last element?,,, +63c648635f004bf4898cfc07,2023-03-11T20:55:25.302Z,Question from Code,v2,does this loop to the last element?,"while (curr != NULL) { + curr = curr->next_rule; +}",, +63c648635f004bf4898cfc07,2023-03-11T20:56:11.112Z,Question from Code,v2,if i want to assign the curr->next value at the very last element will this loop take me to the right element to append at the end?,"while (curr != NULL) { + curr = curr->next_rule; +}",, +63c648635f004bf4898cfc07,2023-03-11T21:03:38.151Z,Question from Code,v2,why is this causing infinite while loop,"Rule *curr2 = head; +Rule *r2 = NULL; + +while (curr2 != NULL) { + if (strcmp(curr2->target, token) == 0) { + printf(""yay\n""); + r2 = curr2; + } + curr2 = curr2->next_rule; +} + +if (r2 == NULL) { + r2 = malloc(sizeof(Rule)); + r2->target = strdup(token); + r2->dependencies = NULL; + r2->actions = a; + r2->next_rule = NULL; +} + +while (curr->next_rule != NULL) { + printf(""%s\n"", curr->target); + curr = curr->next_rule; +} + +curr->next_rule = r2;",, +63c648635f004bf4898cfc07,2023-03-12T05:48:27.368Z,Help Write Code,v2,,,,to append to the last node of the linked list how should i do it? +63c648635f004bf4898cfc07,2023-03-12T05:52:43.254Z,Question from Code,v2,why is this giving me infinite while loop?,"while (fgets(line, MAXLINE, fp) != NULL) { + line[MAXLINE - 1] = '\0'; + + int i = MAXLINE - 1; + while (isspace(line[i]) != 0) { + line[i] = '\0'; + i--; + } + + if (is_comment_or_empty(line) != 1) { + char *token; + token = strtok(line, "" ""); + + Rule *r2 = NULL; + Rule *curr2 = head; + + while (token != NULL) { + while (curr2 != NULL) { + if (strcmp(curr2->target, token) == 0) { + r2 = curr2; + } + curr2 = curr2->next_rule; + } + + if (r2 == NULL) { + r = malloc(sizeof(Rule)); + r->target = strdup(token); + r->actions = NULL; + r->dependencies = NULL; + r->next_rule = NULL; + } + + if (head == NULL) { + head = r; + } else { + Rule *curr = head; + while (curr->next_rule != NULL) { + curr = curr->next_rule; + } + curr->next_rule = r; + } + + token = strtok(NULL, "" ""); + } + } +} +",, +63c648635f004bf4898cfc07,2023-03-12T06:13:56.602Z,Help Fix Code,v2,why is tis giving me infinite while loop?," if(is_comment_or_empty(line) != 1){ + // printf(""%s\n"", line[0]); + + char *token; + token = strtok(line, "" ""); + + while(token != NULL){ + if((isspace(token[0]) == 0 )&& (strcmp(token, "":"") != 0) && (token[0] != '\t')){ + Rule *r = NULL; + Rule *curr2 = head; + // Check if rule already exists + while(curr2 != NULL){ + printf(""%s\n"", curr2->target); + if(strcmp(curr2->target, token) == 0){ + r = curr2; + break; + } + curr2 = curr2->next_rule; + } + + // Rule doesn't exists + if(r == NULL){ + r = malloc(sizeof(Rule)); + r->target = strdup(token); + r->actions = NULL; + r->dependencies = NULL; + r->next_rule = NULL; + } + + // Now append to head + if(head == NULL){ + head = r; + }else{ + Rule *curr = head; + while(curr-> next_rule!= NULL){ + curr = curr->next_rule; + } + curr->next_rule = r; + } + } + token = strtok(NULL, "" ""); + } + + } + } + return head; +}",, +63c648635f004bf4898cfc07,2023-03-12T06:16:03.749Z,Help Fix Code,v2,"make rules nodes like a make file, check if the rule node already exists, if it doesnt don't add a new one. If it doesnt create a new rule node and add to the end of the linked list","/* 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) { + + //Initialize variables + char line[MAXLINE]; + Rule *head = NULL; + Rule *r; + // Dependency *dep; + // Action *a; + + // Read the first line + + while(fgets(line, MAXLINE, fp) != NULL){ + + line[MAXLINE - 1] = '\0'; + + int i = MAXLINE - 1; + while(isspace(line[i]) != 0){ + line[i] = '\0'; + i --; + } + + if(is_comment_or_empty(line) != 1){ + // printf(""%s\n"", line[0]); + + char *token; + token = strtok(line, "" ""); + + while(token != NULL){ + if((isspace(token[0]) == 0 )&& (strcmp(token, "":"") != 0) && (token[0] != '\t')){ + Rule *r = NULL; + Rule *curr2 = head; + // Check if rule already exists + while(curr2 != NULL){ + printf(""%s\n"", curr2->target); + if(strcmp(curr2->target, token) == 0){ + r = curr2; + break; + } + curr2 = curr2->next_rule; + } + + // Rule doesn't exists + if(r == NULL){ + r = malloc(sizeof(Rule)); + r->target = strdup(token); + r->actions = NULL; + r->dependencies = NULL; + r->next_rule = NULL; + } + + // Now append to head + if(head == NULL){ + head = r; + }else{ + Rule *curr = head; + while(curr-> next_rule!= NULL){ + curr = curr->next_rule; + } + curr->next_rule = r; + } + } + token = strtok(NULL, "" ""); + } + + } + } + return head; +}",, +63c648635f004bf4898cfc07,2023-03-12T06:20:42.193Z,Question from Code,v2,why is this code giving finite while loop?," +Rule *parse_file(FILE *fp) { + char line[MAXLINE]; + Rule *head = NULL; + Rule *r; + + while (fgets(line, MAXLINE, fp) != NULL) { + line[MAXLINE - 1] = '\0'; + + int i = MAXLINE - 1; + while (isspace(line[i]) != 0) { + line[i] = '\0'; + i--; + } + + if (is_comment_or_empty(line) != 1) { + char *token; + token = strtok(line, "" ""); + + while (token != NULL) { + if ((isspace(token[0]) == 0) && (strcmp(token, "":"") != 0) && (token[0] != '\t')) { + Rule *r = NULL; + Rule *curr2 = head; + while (curr2 != NULL) { + printf(""%s\n"", curr2->target); + if (strcmp(curr2->target, token) == 0) { + r = curr2; + break; + } + curr2 = curr2->next_rule; + } + + if (r == NULL) { + r = malloc(sizeof(Rule)); + r->target = strdup(token); + r->actions = NULL; + r->dependencies = NULL; + r->next_rule = NULL; + } + + if (head == NULL) { + head = r; + } else { + Rule *curr = head; + while (curr->next_rule != NULL) { + curr = curr->next_rule; + } + curr->next_rule = r; + } + } + token = strtok(NULL, "" ""); + } + } + } + return head; +}",, +63c648635f004bf4898cfc07,2023-03-12T07:00:23.881Z,Question from Code,v2,does this sucessfully set all the trailing spaces to null?,"int i = MAXLINE; +while (isspace(line[i]) != 0) { + line[i] = '\0'; + i--; +}",, +63c648635f004bf4898cfc07,2023-03-13T22:45:26.229Z,Help Write Code,v2,,,,"/* Evaluate the rule in rules corresponding to the given target. + If target is NULL, evaluate the first rule instead. + If pflag is 0, evaluate each dependency in sequence. + If pflag is 1, then evaluate each dependency in parallel (by creating one + new process per dependency). In this case, the parent process will wait until + all child processes have terminated before checking dependency modified times + to decide whether to execute the actions. + */" +63c648635f004bf4898cfc07,2023-03-14T04:37:08.211Z,Question from Code,v2,why is this recursion stopping after it executes action?,"void evaluate_dep(char *target, Rule *rule) { + Rule *r = find_rule(target, rule, 0); + + if (r == NULL) { + } + + if (r != NULL) { + Dependency *dep = r->dependencies; + + if (dep == NULL) { + execute_action(r->actions); + } + + if (dep != NULL) { + struct stat t; + + if (stat(target, &t) != 0) { + printf(""f\n""); + execute_action(r->actions); + } else { + int res = 2; + + res = compare_time(r->target, dep->rule->target); + + printf(""%d\n"", res); + if (res == 0) { + execute_action(r->actions); + } + } + + while (dep != NULL) { + evaluate_dep(dep->rule->target, rule); + + struct stat t; + + if (stat(target, &t) != 0) { + printf(""f\n""); + execute_action(r->actions); + + } else { + int res = 2; + + res = compare_time(r->target, dep->rule->target); + + printf(""%d\n"", res); + if (res == 0) { + execute_action(r->actions); + } + } + + dep = dep->next_dep; + } + } + } +}",, +63c648635f004bf4898cfc07,2023-03-14T04:52:12.712Z,Question from Code,v2,why does this recursion stop after executing one action only?,"void evaluate_dep(char *target, Rule *rule) { + Rule *r = find_rule(target, rule, 0); + + if (r == NULL) { + } + + if (r != NULL) { + Dependency *dep = r->dependencies; + + if (dep == NULL) { + execute_action(r->actions); + } + + if (dep != NULL) { + struct stat t; + + if (stat(target, &t) != 0) { + printf(""f\n""); + execute_action(r->actions); + } else { + int res = 2; + + res = compare_time(r->target, dep->rule->target); + + printf(""%d\n"", res); + if (res == 0) { + execute_action(r->actions); + } + } + + while (dep != NULL) { + evaluate_dep(dep->rule->target, rule); + + struct stat t; + + if (stat(target, &t) != 0) { + printf(""f\n""); + execute_action(r->actions); + + } else { + int res = 2; + + res = compare_time(r->target, dep->rule->target); + + printf(""%d\n"", res); + if (res == 0) { + execute_action(r->actions); + } + } + + dep = dep->next_dep; + } + } + } +}",, +63c648635f004bf4898cfc07,2023-03-14T04:56:33.898Z,Question from Code,v2,why is this recursion not working?,"void evaluate_dep(char *target, Rule *rule) { + Rule *r = find_rule(target, rule, 0); + + if (r == NULL) { + } + + if (r != NULL) { + Dependency *dep = r->dependencies; + + if (dep == NULL) { + execute_action(r->actions); + } + + if (dep != NULL) { + struct stat t; + + if (stat(target, &t) != 0) { + printf(""f\n""); + execute_action(r->actions); + } else { + int res = 2; + + res = compare_time(r->target, dep->rule->target); + + printf(""%d\n"", res); + if (res == 0) { + execute_action(r->actions); + } + } + + while (dep != NULL) { + evaluate_dep(dep->rule->target, rule); + + struct stat t; + + if (stat(target, &t) != 0) { + printf(""f\n""); + execute_action(r->actions); + + } else { + int res = 2; + + res = compare_time(r->target, dep->rule->target); + + printf(""%d\n"", res); + if (res == 0) { + execute_action(r->actions); + } + } + + dep = dep->next_dep; + } + } + } +}",, +63c648635f004bf4898cfc07,2023-03-14T05:00:33.480Z,Question from Code,v2,why does this code stop right after doing one action?,"void evaluate_dep(char *target, Rule *rule) { + Rule *r = find_rule(target, rule, 0); + + if (r == NULL) { + } + + if (r != NULL) { + Dependency *dep = r->dependencies; + + if (dep == NULL) { + execute_action(r->actions); + } + + if (dep != NULL) { + struct stat t; + + if (stat(target, &t) != 0) { + printf(""f\n""); + execute_action(r->actions); + } else { + int res = 2; + + res = compare_time(r->target, dep->rule->target); + + printf(""%d\n"", res); + if (res == 0) { + execute_action(r->actions); + } + } + + while (dep != NULL) { + evaluate_dep(dep->rule->target, rule); + + struct stat t; + + if (stat(target, &t) != 0) { + printf(""f\n""); + execute_action(r->actions); + + } else { + int res = 2; + + res = compare_time(r->target, dep->rule->target); + + printf(""%d\n"", res); + if (res == 0) { + execute_action(r->actions); + } + } + + dep = dep->next_dep; + } + } + } +}",, +63c648635f004bf4898cfc07,2023-03-14T05:01:34.414Z,Question from Code,v2,why is this recursion stopping right after one action is executed?,"void evaluate_dep(char *target, Rule *rule) { + Rule *r = find_rule(target, rule, 0); + + if (r == NULL) { + } + + if (r != NULL) { + Dependency *dep = r->dependencies; + + if (dep == NULL) { + execute_action(r->actions); + } + + while (dep != NULL) { + evaluate_dep(dep->rule->target, rule); + + struct stat t; + + if (stat(target, &t) != 0) { + printf(""f\n""); + execute_action(r->actions); + + } else { + int res = 2; + + res = compare_time(r->target, dep->rule->target); + + printf(""%d\n"", res); + if (res == 0) { + execute_action(r->actions); + } + } + + dep = dep->next_dep; + } + } +} +",, +63c648635f004bf4898cfc07,2023-03-14T05:07:41.371Z,Question from Code,v2,Why is this only executing one action then stopping the loop?,"void execute_action(Action *a) { + Action *curr = a; + char buffer[MAXLINE]; + + while (curr != NULL) { + args_to_string(curr->args, buffer, sizeof(buffer)); + printf(""%s\n"", buffer); + curr = curr->next_act; + } + + Action *curr2 = a; + + while (curr2 != NULL) { + execvp(curr2->args[0], curr2->args); + curr2 = curr2->next_act; + } +} + +void evaluate_dep(char *target, Rule *rule) { + Rule *r = find_rule(target, rule, 0); + + if (r == NULL) { + } + + if (r != NULL) { + Dependency *dep = r->dependencies; + + if (dep == NULL) { + execute_action(r->actions); + } + + while (dep != NULL) { + evaluate_dep(dep->rule->target, rule); + + struct stat t; + + if (stat(target, &t) != 0) { + printf(""f\n""); + execute_action(r->actions); + + } else { + int res = 2; + + res = compare_time(r->target, dep->rule->target); + + printf(""%d\n"", res); + if (res == 0) { + execute_action(r->actions); + } + } + + dep = dep->next_dep; + } + } +} +",, +63c648635f004bf4898cfc07,2023-03-14T20:33:18.947Z,Question from Code,v2,how come this can't execute multiple actions?,"void execute_action(Action *a) { + Action *curr = a; + char buffer[MAXLINE]; + + while (curr != NULL) { + args_to_string(curr->args, buffer, sizeof(buffer)); + printf(""%s\n"", buffer); + curr = curr->next_act; + } + + Action *curr2 = a; + + while (curr2 != NULL) { + execvp(curr2->args[0], curr2->args); + curr2 = curr2->next_act; + } +}",, +63c648635f004bf4898cfc07,2023-03-14T21:19:28.115Z,Question from Code,v2,why is this wait not working,"void execute_action(Action *a) { + Action *curr = a; + char buffer[MAXLINE]; + + while (curr != NULL) { + args_to_string(curr->args, buffer, sizeof(buffer)); + printf(""%s\n"", buffer); + curr = curr->next_act; + } + + Action *curr2 = a; + + while (curr2 != NULL) { + int n = fork(); + if (n == 0) { + execvp(curr2->args[0], curr2->args); + } else { + int status; + if (!(WIFEXITED(status))) { + perror(""wait""); + exit(1); + } + } + curr2 = curr2->next_act; + } +}",, +63c648635f004bf4898cfc07,2023-03-14T21:22:32.732Z,Question from Code,v2,why is this executing the actions multiple times?,"void execute_action(Action *a) { + Action *curr = a; + char buffer[MAXLINE]; + + while (curr != NULL) { + args_to_string(curr->args, buffer, sizeof(buffer)); + printf(""%s\n"", buffer); + curr = curr->next_act; + } + + Action *curr2 = a; + + while (curr2 != NULL) { + int n = fork(); + if (n == 0) { + execvp(curr2->args[0], curr2->args); + exit(0); + } else { + int status; + wait(&status); + if (!(WIFEXITED(status))) { + perror(""wait""); + exit(1); + } + } + curr2 = curr2->next_act; + } +}",, +63c648635f004bf4898cfc07,2023-03-14T21:27:07.084Z,Question from Code,v2,"is this working properly +","void execute_action(Action *a) { + Action *curr = a; + char buffer[MAXLINE]; + + while (curr != NULL) { + args_to_string(curr->args, buffer, sizeof(buffer)); + printf(""%s\n"", buffer); + curr = curr->next_act; + } + + Action *curr2 = a; + + while (curr2 != NULL) { + int n = fork(); + if (n == 0) { + execvp(curr2->args[0], curr2->args); + exit(0); + } else { + int status; + wait(&status); + if (!(WIFEXITED(status))) { + perror(""wait""); + exit(1); + } + } + curr2 = curr2->next_act; + } +} +",, +63c648635f004bf4898cfc07,2023-03-14T21:35:52.780Z,Question from Code,v2,is this doimg parallel properly?,"void evaluate_parallel(Rule *r, Rule *rule) { + int num_child = 0; + + if (r != NULL) { + Dependency *dep = r->dependencies; + + if (dep == NULL) { + execute_action(r->actions); + } + + while (dep != NULL) { + int n = fork(); + + if (n == 0) { + evaluate_parallel(dep->rule, rule); + exit(0); + } else { + num_child += 1; + + struct stat t; + + if (stat(r->target, &t) != 0) { + execute_action(r->actions); + } else { + int res = 2; + res = compare_time(r->target, dep->rule->target); + if (res == 0) { + execute_action(r->actions); + } + } + } + dep = dep->next_dep; + } + } + + for (int i = 0; i < num_child; i++) { + int status; + wait(&status); + if (!(WIFEXITED(status))) { + perror(""wait""); + exit(1); + } + } +}",, +63c648635f004bf4898cfc07,2023-03-14T21:38:49.802Z,Question from Code,v2,how to make this run in parallel,"void evaluate_parallel(Rule *r, Rule *rule) { + int num_child = 0; + + if (r != NULL) { + Dependency *dep = r->dependencies; + + if (dep == NULL) { + execute_action(r->actions); + } + + while (dep != NULL) { + int n = fork(); + + if (n == 0) { + evaluate_parallel(dep->rule, rule); + exit(0); + } else { + num_child += 1; + + struct stat t; + + if (stat(r->target, &t) != 0) { + execute_action(r->actions); + } else { + int res = 2; + res = compare_time(r->target, dep->rule->target); + if (res == 0) { + execute_action(r->actions); + } + } + } + dep = dep->next_dep; + } + } + + for (int i = 0; i < num_child; i++) { + int status; + wait(&status); + if (!(WIFEXITED(status))) { + perror(""wait""); + exit(1); + } + } +}",, +63c648635f004bf4898cfc07,2023-03-25T23:20:07.558Z,General Question,v2,Why is my lab11 saying chat_client connection refused?,,, +63c648635f004bf4898cfc07,2023-03-25T23:20:27.624Z,General Question,v2,what are the causes of connect refused on a server?,,, +63c648635f004bf4898cfc07,2023-03-25T23:21:50.567Z,Question from Code,v2,why is this causing connecting errors?,"nt accept_connection(int fd, struct sockname *users) { + int user_index = 0; + while (user_index < MAX_CONNECTIONS && users[user_index].sock_fd != -1) { + user_index++; + } + + if (user_index == MAX_CONNECTIONS) { + fprintf(stderr, ""server: max concurrent connections\n""); + return -1; + } + + int client_fd = accept(fd, NULL, NULL); + if (client_fd < 0) { + perror(""server: accept""); + close(fd); + exit(1); + } + + char name[BUF_SIZE + 1]; + int num_read; + + if ((num_read = read(client_fd, &name, BUF_SIZE)) == -1) { + perror(""read""); + exit(1); + } + + name[num_read] = '\0'; + + users[user_index].sock_fd = client_fd; + + if ((users[user_index].username = malloc(strlen(name) * sizeof(char))) == NULL) { + perror(""malloc""); + exit(1); + } + + strcpy(users[user_index].username, name); + + return client_fd; +}",, +63c648635f004bf4898cfc07,2023-04-03T01:50:17.057Z,Question from Code,v2,what is causing this error?,"savannapan@Savannas-MacBook-Pro a4 % make +gcc -DPORT=\59208 -g -std=gnu99 -Wall -Werror -o friend_server friend_server.c +Undefined symbols for architecture x86_64: + ""_accept_connection"", referenced from: + _main in friend_server-0cc6cb.o + ""_init_server_addr"", referenced from: + _main in friend_server-0cc6cb.o + ""_set_up_server_socket"", referenced from: + _main in friend_server-0cc6cb.o +ld: symbol(s) not found for architecture x86_64 +clang: error: linker command failed with exit code 1 (use -v to see invocation) +make: *** [friend_server] Error 1",, +63c648635f004bf4898cfc07,2023-04-03T02:00:20.544Z,Question from Code,v2,what may this causing when i have already included the #define _GNU_SOURCE,"friends.c : 193 : 5 : error : implicit declaration of function ‘asprintf’; +did you mean ‘vsprintf’ ? [-Werror = implicit - function - declaration] 193 | asprintf(&time, ""%s"", asctime(localtime(post->date))); +| ^~~~~~~~ | vsprintf",, +63c648635f004bf4898cfc07,2023-04-03T02:08:46.253Z,Question from Code,v2,is asprintf not supported on ssh?,"friends.c : 193 : 5 : error : implicit declaration of function ‘asprintf’; +did you mean ‘vsprintf’ ? [-Werror = implicit - function - declaration] 193 | asprintf(&time, ""%s"", asctime(localtime(post->date))); +| ^~~~~~~~ | vsprintf",, +63c648635f004bf4898cfc07,2023-04-03T02:10:30.188Z,General Question,v2,what is the difference between asprintf and vsprrintf?,,, +63c648635f004bf4898cfc07,2023-04-03T02:26:19.662Z,Question from Code,v2,why is printing random garbage?,"------------------------------------------ +> list_users +�[�Z�david +karen +michelle +bogdan +> profile david +�[�Z�Name: david +------------------------------------------ +Friends: +bogdan",, +63c648635f004bf4898cfc07,2023-04-03T02:27:28.376Z,Question from Code,v2,why is this printing garbage sometimes on ssh?,"char *list_users(const User *curr) { + int size = 0; + + const User *curr2 = (User *)curr; + while (curr2->next != NULL) { + size += strlen(curr2->name) + 2; + curr2 = curr2->next; + } + size += strlen(curr2->name); + + char *buffer = malloc(sizeof(char) * (size + 1)); + if (buffer == NULL) { + free(buffer); + exit(1); + } + + User *curr3 = (User *)curr; + while (curr3->next != NULL) { + strcat(buffer, curr3->name); + strcat(buffer, ""\r\n""); + curr3 = curr3->next; + } + + strcat(buffer, curr3->name); + + strcat(buffer, ""\0""); + strcat(buffer, ""\r\n""); + printf(""%s"", buffer); + + return buffer; +} +",, +63c648635f004bf4898cfc07,2023-04-03T03:14:53.064Z,Help Write Code,v2,,,,how to approach this? +63c648635f004bf4898cfc07,2023-04-03T03:15:18.502Z,Help Write Code,v2,,,,please write code from this description? +63c648635f004bf4898cfc07,2023-04-03T03:15:48.726Z,Question from Code,v2,please write pseudo code with this description?,"Write the friend_server.c code so that you can have one client writing friends commands to the server. When you run the executable it should not take any command-line arguments. You will want some pieces from friendme.c (either your own solution or the new one we have provided), but you should remove sections of code that are no longer needed. Take advantage of example socket code that you have been given (including source code from the PCRS videos, lectures, and labs.) + +When your client connects to the server, ask for and store their name. Then allow them to enter commands. Of course the commands aren't very interesting at this point without a second user for them to befriend or message. But before adding a second user, you should sort out how to handle partial reads. + +Calling read on a socket is not guaranteed to return a full line typed in by the client, so you will need to keep track of how much is read each time and check to see if you have received a network newline which indicates the end of a command.",, +63c648635f004bf4898cfc07,2023-04-03T14:49:56.714Z,Question from Code,v2,why is this not printing the what is your username?,"int main() { + setbuf(stdout, NULL); + fprintf(stdout, ""Welcome to FriendMe! (Local version)\nPlease type a command:\n> ""); + + struct sockaddr_in *self = init_server_addr(PORT); + int listenfd = set_up_server_socket(self, 5); + + while (1) { + int fd = accept_connection(listenfd); + if (fd < 0) { + continue; + } + printf(""What is your usename?""); + + close(fd); + } + + free(self); + close(listenfd); + return 0; +}",, +63c648635f004bf4898cfc07,2023-04-03T15:04:09.720Z,Question from Code,v2,What does this mean?,"^~~friend_server.c : 159 : 7 : error + : a function declaration without a prototype is deprecated in all versions of C and is not supported in C2x[-Werror, -Wdeprecated - non - prototype] char * + partial_read(fd) {",, +63c648635f004bf4898cfc07,2023-04-03T20:54:30.447Z,Question from Code,v2,will this return the full line read before the bufsize is filled?,"char *partial_read(int fd, int op) { + char *buf = malloc(sizeof(char) * BUFSIZE); + buf[BUFSIZE - 1] = '\0'; + int inbuf = 0; + int room = sizeof(buf); + char *after = buf; + int nbytes; + while ((nbytes = read(fd, after, room)) > 0) { + inbuf += nbytes; + int where; + while ((where = find_network_newline(buf, inbuf)) > 0) { + buf[where - 2] = '\0'; + printf(""hello%s"", buf); + + inbuf -= where; + memmove(buf, buf + where, inbuf); + } + after = buf + inbuf; + room = sizeof(buf) - inbuf; + } + return buf; +}",, +63c648635f004bf4898cfc07,2023-04-03T21:16:52.834Z,Question from Code,v2,"why is this only returning the buffer once over 7 character have been typed, but not immediately when a newline characer is detected? +","char *partial_read(int fd) { + char *buf = malloc(sizeof(char) * BUFSIZE); + buf[BUFSIZE - 1] = '\0'; + int inbuf = 0; + int room = sizeof(buf); + char *after = buf; + int nbytes; + while ((nbytes = read(fd, after, room)) > 0) { + inbuf += nbytes; + int where; + while ((where = find_network_newline(buf, inbuf)) > 0) { + buf[where - 2] = '\0'; + + return buf; + } + + if (find_network_newline(buf, inbuf) == 0) { + buf[inbuf] = '\0'; + return buf; + } + + after = buf + inbuf; + room = sizeof(buf) - inbuf; + } + return buf; +} +",, +63c648635f004bf4898cfc07,2023-04-03T23:32:02.940Z,Question from Code,v2,what is this code doing with the cmd_agrv,"int main(int argc, char *argv[]) { + int batch_mode = (argc == 2); + char input[INPUT_BUFFER_SIZE]; + FILE *input_stream; + + User *user_list = NULL; + + if (batch_mode) { + input_stream = fopen(argv[1], ""r""); + if (input_stream == NULL) { + perror(""Error opening file""); + exit(1); + } + } else { + input_stream = stdin; + } + + printf(""Welcome to FriendMe! (Local version)\nPlease type a command:\n> ""); + + while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) { + if (batch_mode) { + printf(""%s"", input); + } + + char *cmd_argv[INPUT_ARG_MAX_NUM]; + int cmd_argc = tokenize(input, cmd_argv); + + if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { + break; + } + + printf(""> ""); + } + + if (batch_mode) { + fclose(input_stream); + } + + return 0; +} +",, +63c648635f004bf4898cfc07,2023-04-04T04:20:56.737Z,Question from Code,v2,why is this returning from the outside loop when more than 8 characters are read?,"char *partial_read(int fd) { + char *buf = malloc(2 + ((sizeof(char)) * BUFSIZE)); + buf[BUFSIZE - 1] = '\0'; + int inbuf = 0; + int room = sizeof(buf); + char *after = buf; + int nbytes; + while ((nbytes = read(fd, after, room)) > 0) { + inbuf += nbytes; + int where; + + if ((where = find_network_newline(buf, inbuf)) > 0) { + printf(""returned here!\n""); + + buf[where - 2] = '\0'; + inbuf -= where; + memmove(buf, buf + where, inbuf); + + return buf; + } + + after = buf + inbuf; + room = sizeof(buf) - inbuf; + } + printf(""returned outside loop!\n""); + return buf; +}",, +63c648635f004bf4898cfc07,2023-04-04T04:26:02.934Z,Question from Code,v2,how many characters does this read before it will return from outside loop?,"char *partial_read(int fd) { + char *buf = malloc(sizeof(char) * BUFSIZE); + buf[BUFSIZE - 1] = '\0'; + int inbuf = 0; + int room = sizeof(buf); + char *after = buf; + int nbytes; + while ((nbytes = read(fd, after, room)) > 0) { + inbuf += nbytes; + int where; + + if ((where = find_network_newline(buf, inbuf)) > 0) { + printf(""returned here!\n""); + + buf[where - 2] = '\0'; + inbuf -= where; + memmove(buf, buf + where, inbuf); + + return buf; + } + + after = buf + inbuf; + room = sizeof(buf) - inbuf; + } + printf(""returned outside loop!\n""); + return buf; +} +",, +63c648635f004bf4898cfc07,2023-04-04T04:26:47.185Z,Question from Code,v2,how many characters does this loop read?,"char *partial_read(int fd) { + char *buf = malloc(sizeof(char) * BUFSIZE); + buf[BUFSIZE - 1] = '\0'; + int inbuf = 0; + int room = sizeof(buf); + char *after = buf; + int nbytes; + while ((nbytes = read(fd, after, room)) > 0) { + inbuf += nbytes; + int where; + + if ((where = find_network_newline(buf, inbuf)) > 0) { + printf(""returned here!\n""); + + buf[where - 2] = '\0'; + inbuf -= where; + memmove(buf, buf + where, inbuf); + + return buf; + } + + after = buf + inbuf; + room = sizeof(buf) - inbuf; + } + printf(""returned outside loop!\n""); + return buf; +} +",, +63c648635f004bf4898cfc07,2023-04-04T14:26:54.815Z,Question from Code,v2,"why is this only calling process_args on the second input that i read, and not the first one?","int read_from(int client_fd, struct sockname *users, User *user_list) { + int fd = client_fd; + + char *input = partial_read(fd); + + int num_read = strlen(input); + + char *cmd_argv[INPUT_ARG_MAX_NUM]; + int cmd_argc = tokenize(input, cmd_argv); + printf(""returned input!\n""); + + printf(""%d\n"", cmd_argc); + + if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { + printf(""hello""); + } + + if (num_read == 0) { + client_fd = -1; + return fd; + } + + return 0; +}",, +63c648635f004bf4898cfc07,2023-04-04T14:31:53.936Z,Question from Code,v2,why is this only going to else case and prints incorrect syntax after the second time it is called?,"int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) { + User *user_list = *user_list_ptr; + + printf(""function called\n""); + + if (cmd_argc <= 0) { + return 0; + } else if (strcmp(cmd_argv[0], ""quit"") == 0 && cmd_argc == 1) { + return -1; + } else if (strcmp(cmd_argv[0], ""add_user"") == 0 && cmd_argc == 2) { + switch (create_user(cmd_argv[1], user_list_ptr)) { + case 1: + error(""user by this name already exists""); + break; + case 2: + error(""username is too long""); + break; + } + + } else if (strcmp(cmd_argv[0], ""list_users"") == 0 && cmd_argc == 1) { + list_users(user_list); + + } else if (strcmp(cmd_argv[0], ""make_friends"") == 0 && cmd_argc == 3) { + switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) { + case 1: + + break; + } + } else if (strcmp(cmd_argv[0], ""post"") == 0 && cmd_argc >= 4) { + int space_needed = 0; + for (int i = 3; i < cmd_argc; i++) { + space_needed += strlen(cmd_argv[i]) + 1; + } + + char *contents = malloc(space_needed); + if (contents == NULL) { + perror(""malloc""); + exit(1); + } + + strcpy(contents, cmd_argv[3]); + for (int i = 4; i < cmd_argc; i++) { + strcat(contents, "" ""); + strcat(contents, cmd_argv[i]); + } + + User *author = find_user(cmd_argv[1], user_list); + User *target = find_user(cmd_argv[2], user_list); + switch (make_post(author, target, contents)) { + case 1: + error(""the users are not friends""); + break; + case 2: + error(""at least one user you entered does not exist""); + break; + } + } else if (strcmp(cmd_argv[0], ""profile"") == 0 && cmd_argc == 2) { + User *user = find_user(cmd_argv[1], user_list); + print_user(user); + + } else { + printf(""Incorrect syntax""); + } + + return 0; +}",, +63c648635f004bf4898cfc07,2023-04-04T16:56:06.162Z,Question from Code,v2,how to fix this issue?,"error : The following untracked working tree files would be overwritten by merge : a4 / friend_server Please move or remove them before you merge. +}",, +63c648635f004bf4898cfc07,2023-04-04T19:31:34.559Z,Question from Code,v2,is it correctly copying the username from partial read return input?,"client_fd = accept_connection(sock_fd, users, &user_list); + +Client *c = malloc(sizeof(Client)); +c->fd = client_fd; +c->username = NULL; + +char *s1 = ""What is your username?\r\n""; +if (write(c->fd, s1, strlen(s1)) < 0) { + perror(""write""); + exit(1); +} + +char username[MAX_NAME + 1]; + +int num_read = 0; + +char *input = partial_read(c->fd); +printf(""%s\n"", input); + +num_read = strlen(input); + +if (num_read < 0) { + perror(""server: read username""); + exit(1); +} + +username[num_read] = '\0'; + +if ((c->username = malloc(strlen(input) * sizeof(char))) == NULL) { + perror(""malloc at server""); + exit(1); +} + +strcpy(username, input); +strcpy(c->username, username); + +user = process_name(&user_list, client_fd, c->username); +printf(""%s"", user->name); +printf(""returned name\r\n""); + +if (client_fd > max_fd) { + max_fd = client_fd; +} +FD_SET(client_fd, &all_fds); +// printf(""Accepted connection\n"");",, +63c648635f004bf4898cfc07,2023-04-04T19:34:40.194Z,Question from Code,v2,why is this code not printing the username? is it being copied corretly?,"if (FD_ISSET(sock_fd, &listen_fds)) { + client_fd = accept_connection(sock_fd, users, &user_list); + + Client *c = malloc(sizeof(Client)); + c->fd = client_fd; + c->username = NULL; + + char *s1 = ""What is your username?\r\n""; + if (write(c->fd, s1, strlen(s1)) < 0) { + perror(""write""); + exit(1); + } + + char username[MAX_NAME + 1]; + + int num_read = 0; + + char *input = partial_read(c->fd); + printf(""%s\n"", input); + + num_read = strlen(input); + + if (num_read < 0) { + perror(""server: read username""); + exit(1); + } + + if ((c->username = malloc((strlen(input) + 1) * sizeof(char))) == NULL) { + perror(""malloc at server""); + exit(1); + } + + strcpy(username, input); + username[num_read - 1] = '\0'; + + strcpy(c->username, username); + + user = process_name(&user_list, client_fd, c->username); + printf(""%s"", user->name); + printf(""returned name\r\n""); + + if (client_fd > max_fd) { + max_fd = client_fd; + } + FD_SET(client_fd, &all_fds); + + char *s = ""Go ahead and enter user commands>\r\n""; + if (write(client_fd, s, strlen(s)) < 0) { + perror(""write""); + exit(1); + } +}",, +63c648635f004bf4898cfc07,2023-04-04T22:43:38.674Z,Question from Code,v2,wha is causing this error?,malloc() : corrupted top size,, +63c648635f004bf4898cfc07,2023-04-05T03:47:46.629Z,Question from Code,v2,"why is this giving segmentation fault? +","strcpy(contents, cmd_argv[3]); +for (int i = 3; i < cmd_argc; i++) { + strcat(contents, "" ""); + strcat(contents, cmd_argv[i]); +}",, +63c648635f004bf4898cfc07,2023-04-05T03:48:36.753Z,Question from Code,v2,why is this giving segmentation fault?,"int space_needed = 0; +for (int i = 3; i < cmd_argc; i++) { + space_needed += strlen(cmd_argv[i]) + 1; +} + +printf(""before space!\n""); +char *contents = malloc(space_needed); +if (contents == NULL) { + perror(""malloc""); + exit(1); +} + +printf(""after malloc!\n""); +strcpy(contents, cmd_argv[3]); +for (int i = 3; i < cmd_argc; i++) { + strcat(contents, "" ""); + strcat(contents, cmd_argv[i]); +} +",, +63c648635f004bf4898cfc07,2023-04-05T04:04:43.821Z,Question from Code,v2,why is this giving segmentation fault?,"} +else if (strcmp(cmd_argv[0], ""post"") == 0 && cmd_argc >= 3) { + printf(""first stop!\n""); + + int space_needed = 1; + for (int i = 2; i < cmd_argc; i++) { + space_needed += strlen(cmd_argv[i]) + 1; + } + + printf(""before space!\n""); + char *contents = malloc(space_needed); + if (contents == NULL) { + perror(""malloc""); + exit(1); + } + + printf(""after malloc!\n""); + strcpy(contents, cmd_argv[3]); + for (int i = 2; i < cmd_argc; i++) { + strcat(contents, "" ""); + strcat(contents, cmd_argv[i]); + } +",, +63c648635f004bf4898cfc07,2023-04-05T19:11:33.352Z,General Question,v2,why do i get a segmentation fault after my program terminates?,,, +63c648635f004bf4898cfc07,2023-04-05T19:12:16.366Z,General Question,v2,why do i get a segmentation fault after my server is killed?,,, +63c648635f004bf4898cfc07,2023-04-06T01:52:25.482Z,General Question,v2,how to check on a file on valgrind?,,, +63c648635f004bf4898cfc07,2023-04-06T02:30:26.845Z,General Question,v2,"i am getting a segmentation fault after a client disconnects from a server, Think about what happens in the select command when a file descriptor in the read set corresponds to a client program that has died. +Then think about what read returns if you call it on a socket where the process on the read end of the socket has died? how to fix this ",,, +63c648635f004bf4898cfc07,2023-04-06T17:04:56.770Z,Question from Code,v2,"why is this giving segmentation fault? +"," +int read_from(User *users, Client *c, User **head) { + int fd = c->fd; + + char *input = partial_read(c); + int num_read = 0; + + if (input == NULL) { + return fd; + } + + num_read = strlen(input); + input[num_read] = '\0'; + + if (strlen(input) > 0 && (strcmp(input, ""read"") != 0)) { + if (c->username == NULL) { + int create = process_name(c, input, head); + if (create != 0) { + free(input); + return create; + } + } else { + char *cmd_argv[INPUT_ARG_MAX_NUM]; + + for (int i = 0; i < INPUT_ARG_MAX_NUM; i++) { + cmd_argv[i] = NULL; + } + + int cmd_argc = tokenize2(input, cmd_argv); + + char *res = process_args(cmd_argc, cmd_argv, &users, c->user_ptr, fd); + + if (res != NULL) { + if (strcmp(res, ""quit"") == 0) { + return fd; + } + } else if (input == NULL) { + return fd; + } + free(input); + } + } + + return 0; +}",, +63c648635f004bf4898cfc07,2023-04-06T19:16:55.670Z,Question from Code,v2,what does this mean?,Invalid write of size 2,, +63c648635f004bf4898cfc07,2023-04-06T19:25:00.340Z,General Question,v2,does snprinf malloc anything?,,, +63c648635f004bf4898cfc07,2023-04-06T21:52:14.955Z,Question from Code,v2,what does this mean?,free() : double free detected in tcache 2,, +63c648635f004bf4898cfc07,2023-04-06T21:58:15.235Z,Question from Code,v2,is there double free error for this?," +void free_client(int fd, Client **clients) { + if (*clients != NULL) { + if ((*clients)->fd == fd) { + Client *pass = *clients; + *clients = (*clients)->next; + free(pass->username); + free(pass->buffer); + free(pass); + } else { + Client *prev = *clients; + Client *curr = (*clients)->next; + while (curr != NULL && curr->fd != fd) { + prev = curr; + curr = curr->next; + } + if (curr != NULL) { + prev->next = curr->next; + free(curr->username); + free(curr->buffer); + free(curr); + } + } + } +}",, +63c648655f004bf4898cfc16,2023-02-07T05:44:36.320Z,General Question,v1,What is the keyword that indicates that a word is a legal target but isn't an actual file?,,, +63c648655f004bf4898cfc16,2023-02-15T22:12:21.190Z,General Question,v1,how do you printf a formatted date of type time_t?,,, +63c648655f004bf4898cfc16,2023-02-15T23:26:38.392Z,Question from Code,v1,"Can you partially initialize a struct inline, that is, from the code, can you exclude some struct attributes in the inline definition?","*new_user = (User){.name="""", .profile_pic="""", .first_post=NULL, .next=NULL};",, +63c648655f004bf4898cfc16,2023-03-31T21:15:09.424Z,General Question,v2,"the OSI model treats the network and protocol layer as separate. That is, IP and TCP are two separate things. In that case, what is TCP/IP and why does it mingle the layers?",,, +63c648655f004bf4898cfc16,2023-03-31T21:18:28.256Z,General Question,v2,you did not show the examples of other protocols. Please show me them,,, +63c648655f004bf4898cfc16,2023-04-04T04:32:17.926Z,General Question,v2,"subject=CSC + +I am told to write a shell command to print the following message ""Last week of CSC209"", using the variable subject. I am not sure how to do it. Could you show me how or guide me in the right direction?",,, +63c648655f004bf4898cfc1b,2023-01-21T22:01:18.322Z,General Question,v1,can you write me a c program that prints back a user's input?,,, +63c648655f004bf4898cfc1b,2023-01-30T17:37:57.959Z,Help Fix Code,v1,,"#include +#include + + +// TODO: Implement a helper named check_permissions that matches the prototype below. +int check_permissions(char *check_perms, char *req_perms) { + int i; + for (i = 0; i < 9; i++) { + if (req_perms[i] != check_perms[i] && req_perms[i] != '-') { + return 1; + } + } + return 0; +} + + +int main(int argc, char** argv) { + if (!(argc == 2 || argc == 3)) { + fprintf(stderr, ""USAGE: count_large size [permissions]\n""); + return 1; + } + + // TODO: Process command line arguments. + int cutoff = strtol(argv[1], NULL, 10); + int total = 0; + int success; + int current_size; + + scanf(""%*[^\n]\n"");//Get past & ignore the Total (first) line + + + if (argc == 3) { //Check permissions version + char *permissions = malloc(sizeof(char) * 9); + permissions = argv[2]; + char *current_perm = malloc(sizeof(char) * 9); + + success = scanf(""%*1s%9s %*d %*s %*s %d %*[^\n]\n"", current_perm, ¤t_size); + while (success > -1) { + if (current_size > cutoff && check_permissions(current_perm, permissions) == 0) { + total++; + } + success = scanf(""%*s %*d %*s %*s %d %*[^\n]\n"", ¤t_size); + } + + free(permissions); + free(current_perm); + } + else {//Regular version + //Do one check first, so that they are synced properly with EOF check + success = scanf(""%*s %*d %*s %*s %d %*[^\n]\n"", ¤t_size); + while (success > -1) { + if (current_size > cutoff) { + total++; + } + success = scanf(""%*s %*d %*s %*s %d %*[^\n]\n"", ¤t_size); + } + } + + printf(""%d\n"", total); + + return 0; +} +",the intended behavior is to avoid segfaults, +63c648665f004bf4898cfc20,2023-01-25T17:00:01.152Z,Question from Code,v1,Why is there error,"#include +#include + +/* + * 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_ptr, int count) { + fib_ptr = malloc(sizeof(int)*count); + + for (int i = 0; i <= count; i++) { + if (i == 0) { + fib_ptr[i] = 0; + } + else if (i == 1) { + fib_ptr[i] = 1; + } + else { + fib_ptr[i] = fib_ptr[i - 1] + fib_ptr[i - 2]; + } + } +} + + +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; +}",, +63c648665f004bf4898cfc20,2023-01-25T17:02:51.306Z,Question from Code,v1,why is there segmentation error,"#include +#include + +/* + * 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_ptr, int count) { + fib_ptr = malloc(sizeof(int)*count); + + for (int i = 0; i <= count; i++) { + if (i == 0) { + fib_ptr[i] = 0; + } + else if (i == 1) { + fib_ptr[i] = 1; + } + else { + fib_ptr[i] = fib_ptr[i - 1] + fib_ptr[i - 2]; + } + } +} + + +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; +}",, +63c648665f004bf4898cfc20,2023-01-25T17:07:56.467Z,Question from Code,v1,Why is there segmentation error?,"#include +#include + +/* + * 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_ptr, int count) { + fib_ptr = malloc(sizeof(int)*count); + + for (int i = 0; i < count; i++) { + if (i == 0) { + *fib_ptr[i] = 0; + } + else if (i == 1) { + *fib_ptr[i] = 1; + } + else { + *fib_ptr[i] = *fib_ptr[i - 1] + *fib_ptr[i - 2]; + } + } +} + + +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; +}",, +63c648665f004bf4898cfc20,2023-01-25T17:09:38.908Z,Question from Code,v1,why is there segmentation error?,"#include +#include + +/* + * 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_ptr, int count) { + *fib_ptr = malloc(sizeof(int)*count); + + for (int i = 0; i < count; i++) { + if (i == 0) { + *fib_ptr[i] = 0; + } + else if (i == 1) { + *fib_ptr[i] = 1; + } + else { + *fib_ptr[i] = *fib_ptr[i - 1] + *fib_ptr[i - 2]; + } + } +} + + +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; +}",, +63c648665f004bf4898cfc20,2023-01-25T17:11:43.589Z,Help Fix Code,v1,,"#include +#include + +/* + * 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_ptr, int count) { + *fib_ptr = malloc(sizeof(int)*count); + + for (int i = 0; i < count; i++) { + if (i == 0) { + *fib_ptr[i] = 0; + } + else if (i == 1) { + *fib_ptr[i] = 1; + } + else { + *fib_ptr[i] = *fib_ptr[i - 1] + *fib_ptr[i - 2]; + } + } +} + + +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; +}","when the function is called with an int argument, print the fibonacci sequence up to the argument", +63c648665f004bf4898cfc20,2023-01-25T17:15:33.734Z,Explain Code,v1,,"#include +#include + +/* + * 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_ptr, int count) { + *fib_ptr = malloc(sizeof(int)*count); + + for (int i = 0; i < count; i++) { + if (i == 0) { + *fib_ptr[i] = 0; + } + else if (i == 1) { + *fib_ptr[i] = 1; + } + else { + *fib_ptr[i] = *fib_ptr[i - 1] + *fib_ptr[i - 2]; + } + } +} + + +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; +}",, +63c648665f004bf4898cfc20,2023-03-09T17:41:00.513Z,Help Fix Code,v2,"This code gives me error, which is Error: could not read from stdin. Please fix that","#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + printf(""HELLO00""); + // TODO + int ret; + int fd[2]; + + ret = fork(); + if (ret < 0) { + perror(""pipe""); + } + if (pipe(fd) == -1) { + perror(""pipe""); + exit(1); + } + + printf(""HELLO1""); + if (ret == 0) { // child process + close(fd[1]); + dup2(fd[0], STDIN_FILENO); + execl(""./validate"", ""validate"", NULL); + + close(fd[0]); + + } else { // parent process + printf(""HELLO2""); + close(fd[0]); + dup2(fd[1], STDIN_FILENO); + + write(fd[1], user_id, 10); + write(fd[1], password, 10); + close(fd[1]); + + int status; + pid_t child_pid = wait(&status); + + if (child_pid == -1) { + perror(""wait""); + return -1; + } + if (WIFEXITED(status)) { + int exit_status = WEXITSTATUS(status); + if(exit_status == 0) { + printf(""%s"", SUCCESS); + } else if(exit_status == 2) { + printf(""%s"", INVALID); + } else { + printf(""%s"", NO_USER); + } + } + + + } + + + return 0; +} ",, +63c648665f004bf4898cfc20,2023-03-09T17:43:07.447Z,Help Fix Code,v2,This code gives me Input/output error. Fix that,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + printf(""HELLO00""); + // TODO + int ret; + int fd[2]; + + ret = fork(); + if (ret < 0) { + perror(""pipe""); + } + if (pipe(fd) == -1) { + perror(""pipe""); + exit(1); + } + + printf(""HELLO1""); + if (ret == 0) { // child process + close(fd[1]); + dup2(fd[0], STDOUT_FILENO); + execl(""./validate"", ""validate"", NULL); + + close(fd[0]); + + } else { // parent process + printf(""HELLO2""); + close(fd[0]); + dup2(fd[1], STDIN_FILENO); + + write(fd[1], user_id, 10); + write(fd[1], password, 10); + close(fd[1]); + + int status; + pid_t child_pid = wait(&status); + + if (child_pid == -1) { + perror(""wait""); + return -1; + } + if (WIFEXITED(status)) { + int exit_status = WEXITSTATUS(status); + if(exit_status == 0) { + printf(""%s"", SUCCESS); + } else if(exit_status == 2) { + printf(""%s"", INVALID); + } else { + printf(""%s"", NO_USER); + } + } + + + } + + + return 0; +} ",, +63c648665f004bf4898cfc20,2023-03-09T17:45:30.263Z,Help Fix Code,v2,This code gives me Input/output error. Fix that,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + // TODO + int ret; + int fd[2]; + + ret = fork(); + if (ret < 0) { + perror(""pipe""); + } + if (pipe(fd) == -1) { + perror(""pipe""); + exit(1); + } + + + if (ret == 0) { // child process + close(fd[1]); + dup2(fd[0], STDOUT_FILENO); + execl(""./validate"", ""validate"", NULL); + + close(fd[0]); + + } else { // parent process + close(fd[0]); + dup2(fd[1], STDIN_FILENO); + + write(fd[1], user_id, strlen(user_id)); + write(fd[1], password, strlen(user_id)); + close(fd[1]); + + int status; + pid_t child_pid = wait(&status); + + if (child_pid == -1) { + perror(""wait""); + return -1; + } + if (WIFEXITED(status)) { + int exit_status = WEXITSTATUS(status); + if(exit_status == 0) { + printf(""%s"", SUCCESS); + } else if(exit_status == 2) { + printf(""%s"", INVALID); + } else { + printf(""%s"", NO_USER); + } + } + + + } + + + return 0; +} ",, +63c648665f004bf4898cfc20,2023-03-09T17:51:10.877Z,Help Fix Code,v2,I got could not read from stdin error. fix that,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + // TODO + int ret; + int fd[2]; + + ret = fork(); + if (ret < 0) { + perror(""pipe""); + } + if (pipe(fd) == -1) { + perror(""pipe""); + exit(1); + } + + + if (ret == 0) { // child process + close(fd[1]); + dup2(fd[0], STDIN_FILENO); + execl(""./validate"", ""validate"", NULL); + + close(fd[0]); + + } else { // parent process + close(fd[0]); + dup2(fd[1], STDOUT_FILENO); + + write(fd[1], user_id, strlen(user_id)); + write(fd[1], password, strlen(user_id)); + close(fd[1]); + + int status; + pid_t child_pid = wait(&status); + + if (child_pid == -1) { + perror(""wait""); + return -1; + } + if (WIFEXITED(status)) { + int exit_status = WEXITSTATUS(status); + if(exit_status == 0) { + printf(""%s"", SUCCESS); + } else if(exit_status == 2) { + printf(""%s"", INVALID); + } else { + printf(""%s"", NO_USER); + } + } + + + } + + + return 0; +} ",, +63c648665f004bf4898cfc20,2023-03-09T17:52:39.058Z,Help Fix Code,v2,This code gives me could not read from stdin error. Fix that,"#include +#include +#include +#include +#include +#include + +#define MAXLINE 256 + +#define SUCCESS ""Password verified\n"" +#define INVALID ""Invalid password\n"" +#define NO_USER ""No such user\n"" + +int main(void) { + + char user_id[MAXLINE]; + char password[MAXLINE]; + + /* The user will type in a user name on one line followed by a password + on the next. + DO NOT add any prompts. The only output of this program will be one + of the messages defined above. + Please read the comments in validate carefully + */ + + if(fgets(user_id, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + if(fgets(password, MAXLINE, stdin) == NULL) { + perror(""fgets""); + exit(1); + } + + // TODO + int ret; + int fd[2]; + + ret = fork(); + if (ret < 0) { + perror(""pipe""); + } + if (pipe(fd) == -1) { + perror(""pipe""); + exit(1); + } + + + if (ret == 0) { // child process + close(fd[1]); + dup2(fd[0], STDIN_FILENO); + execl(""./validate"", ""validate"", NULL); + + close(fd[0]); + + } else { // parent process + close(fd[0]); + dup2(fd[1], STDOUT_FILENO); + + write(fd[1], user_id, strlen(user_id)); + write(fd[1], password, strlen(password)); + close(fd[1]); + + int status; + pid_t child_pid = wait(&status); + + if (child_pid == -1) { + perror(""wait""); + return -1; + } + if (WIFEXITED(status)) { + int exit_status = WEXITSTATUS(status); + if(exit_status == 0) { + printf(""%s"", SUCCESS); + } else if(exit_status == 2) { + printf(""%s"", INVALID); + } else { + printf(""%s"", NO_USER); + } + } + + + } + + + return 0; +} ",, +63c648665f004bf4898cfc20,2023-03-13T21:39:10.549Z,General Question,v2,I have segmentation fault from strlen. How can I fix this?,,, +63c648675f004bf4898cfc2a,2023-02-09T20:53:57.423Z,General Question,v1,How to read a bitmap file in C?,,, +63c648675f004bf4898cfc2a,2023-02-09T20:54:38.090Z,Help Write Code,v1,,,,Display a bitmap image in C +63c648675f004bf4898cfc2a,2023-02-14T00:16:23.667Z,Question from Code,v1,what will this evaluate to in C,"char name[""32""];",, +63c648675f004bf4898cfc2a,2023-02-14T00:19:20.548Z,Question from Code,v1,What will the declaration on line 2 become after the program has passed through the C pre-processor?,"#define MAXNAME = 32; +char name[MAXNAME];",, +63c648685f004bf4898cfc2f,2023-02-15T20:14:59.309Z,Help Fix Code,v1,," int delete_user(const char *name, User **user_ptr_del) { + User *currUser = *user_ptr_del; + if (currUser==NULL){ + return 1; + } + if(strcmp(currUser->name,name)==0){ + //delete the first user + + *user_ptr_del = currUser->next; + free_user(currUser); + return 0; + + } + while (currUser -> next != NULL){ + if(strcmp((currUser->next)->name,name)==0){ + free_user(currUser->next); + currUser->next= (currUser->next)->next; + + return 0; + } + currUser = currUser->next; + } + + return 1; +} + + +/* + * frees the user pointed to by currUser from the + * heap and all its attributes. + */ + +void free_user(User *currUser){ +//iterate to free all posts + Post *currpost = currUser->first_post; + Post *currpost2 = currUser->first_post; + while(currpost!= NULL){ + currpost2 = currpost; + currpost = currpost->next; + //free contents, date and post + free(currpost2->contents); + free(currpost2->date); + free(currpost2); + // currpost2 = currpost; + // free(currpost); + // currpost = currpost2->next; + } + + //free the user from its friends' friend arrays + for (int i=0; ifriends)[i]; + if (friend!=NULL){ + for (int j=0; jfriends)[j]; + if (current!=NULL && strcmp(current->name, currUser->name)==0){ + (friend->friends)[j] = NULL; + } + } + } + } + free(currUser); +} +","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 and 1 if a user with this name does not exist.", +63c648685f004bf4898cfc2f,2023-02-15T20:17:41.432Z,General Question,v1,why goes valgrind say Invalid read of size 8,,, +63c648685f004bf4898cfc2f,2023-02-15T20:24:28.607Z,Help Fix Code,v1,,"int delete_user(const char *name, User **user_ptr_del) { + User *currUser = *user_ptr_del; + if (currUser==NULL){ + return 1; + } + if(strcmp(currUser->name,name)==0){ + //delete the first user + + *user_ptr_del = currUser->next; + free_user(currUser); + return 0; + + } + while (currUser -> next != NULL){ + if(strcmp((currUser->next)->name,name)==0){ + free_user(currUser->next); + currUser->next= (currUser->next)->next; + + return 0; + } + currUser = currUser->next; + } + + return 1; +} + + +/* + * frees the user pointed to by currUser from the + * heap and all its attributes. + */ + +void free_user(User *currUser){ +//iterate to free all posts + Post *currpost = currUser->first_post; + Post *currpost2 = currUser->first_post; + while(currpost!= NULL){ + currpost2 = currpost; + currpost = currpost->next; + //free contents, date and post + free(currpost2->contents); + free(currpost2->date); + free(currpost2); + // currpost2 = currpost; + // free(currpost); + // currpost = currpost2->next; + } + + //free the user from its friends' friend arrays + for (int i=0; ifriends)[i]; + if (friend!=NULL){ + for (int j=0; jfriends)[j]; + if (current!=NULL && strcmp(current->name, currUser->name)==0){ + (friend->friends)[j] = NULL; + } + } + } + } + free(currUser); +} +",remove user with name name from user_ptr_del, +63c648685f004bf4898cfc2f,2023-02-15T20:34:11.397Z,Help Fix Code,v1,,"for (int i=0; ifriends)[i]; + if (friend!=NULL){ + for (int j=0; jfriends)[j]; + if (current!=NULL){ + if (strcmp(current->name, currUser->name)==0){ + (friend->friends)[j] = NULL; + } + } + } + } + } + free(currUser);",remove the currUser from its friends' friend arrays, +63c6486c5f004bf4898cfc5c,2023-01-17T18:21:14.916Z,General Question,v1,how to I create a function in C?,,, +63c6486c5f004bf4898cfc5c,2023-01-17T18:22:11.129Z,General Question,v1,How would I write a function in C that calculates 2 to the power of n for an integer input n?,,, +63c6486c5f004bf4898cfc5c,2023-01-18T03:43:04.733Z,Help Fix Code,v1,,"#include +#include + +/* + * Write a void function invest that takes your money and multiplies it by the given rate. + */ +void invest(double *principal, double rate) { + *principal = *principal * rate; +} + +/* + * NOTE: don't change the main function! + * Sample usage: + * $ gcc -Wall -std=gnu99 -g -o invest invest.c + * $ ./invest 10000 1.05 + * 10500.00 + */ +int main(int argc, char **argv) { + // Read in the command-line arguments and convert the strings to doubles + double principal = strtod(argv[1], NULL); + double rate = strtod(argv[2], NULL); + // Call invest to make you more money + invest(&principal, rate); + + printf(""%.2f\n"", principal); + return 0; +} +",The function invest should multiply the principle input by the rate input, +63c6486c5f004bf4898cfc5c,2023-01-22T19:58:01.569Z,Help Fix Code,v1,,"int main() { + char **last_names; + last_names = (char **) malloc(sizeof(char) * 4); + last_names[0] = (char *) malloc(sizeof(char) * 20); + last_names[1] = (char *) malloc(sizeof(char) * 20); + last_names[2] = (char *) malloc(sizeof(char) * 20); + last_names[3] = (char *) malloc(sizeof(char) * 20); + + + return 0; +}","Assign a dynamically allocated char * array of length 4 to last_names. Then, allocate a character array of length 20 for each element of the array pointed to by last_names.", +63c6486c5f004bf4898cfc5c,2023-01-22T20:00:32.309Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + float *rainfall; + float rain_today; + rain_today = rainfall; + return 0; +}",rainfall has been dynamically allocated space for a floating point number. Both rainfall and rain_today have been initialized in hidden code. Assign the amount in rain_today to the space rainfall points to, +63c6486c5f004bf4898cfc5c,2023-01-22T20:01:09.749Z,Help Fix Code,v1,,"int main(int argc, char **argv) { + float *rainfall; + float rain_today; + rain_today = *rainfall; + return 0; +}",rainfall has been dynamically allocated space for a floating point number. Both rainfall and rain_today have been initialized in hidden code. Assign the amount in rain_today to the space rainfall points to, +63c6486c5f004bf4898cfc5c,2023-01-22T20:02:41.851Z,Question from Code,v1,why does this code give a warning,"int main(int argc, char **argv) { + float *rainfall; + float rain_today; + rain_today = *rainfall; + return 0; +}",, +63c6486c5f004bf4898cfc5c,2023-01-25T01:36:40.577Z,Help Fix Code,v1,,"void fib(int **point, int n) { + *point = malloc(sizeof(int) * n); + int curr = 1; + int prev = 0; + if (n == 1) { + *point[0] = prev; + } else if (n == 2) { + *point[0] = prev; + *point[0] = curr; + } else { + int temp; + *point[0] = prev; + *point[1] = curr; + for (int e = 2, e