user_id
stringlengths
24
24
time
stringlengths
24
24
feature_type
stringclasses
5 values
feature_version
stringclasses
2 values
input_question
stringlengths
1
1.24k
input_code
stringlengths
2
18.5k
input_intention
stringlengths
1
2.48k
input_task_description
stringlengths
18
3.32k
63c648d15f004bf4898d0003
2023-01-27T06:35:06.613Z
Explain Code
v1
null
int **split_array(const int *s, int length) { int **result = malloc(sizeof(int*) * 2); result[0] = malloc(sizeof(int) * (length + 1) / 2); result[1] = malloc(sizeof(int) * (length + 1) / 2); int even = 0; int odd = 0; for(int i = 0; i < length; i ++){ if(s[i] % 2 == 0) { result[0][even] = s[i]; even++; } else { result[1][odd] = s[i]; odd++; } } return result; }
null
null
63c648d15f004bf4898d0003
2023-01-28T18:26:28.351Z
Help Write Code
v1
null
null
null
Your first task is to write 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. Your program prints to standard output the number of regular files in the input listing that are larger than the cutoff size supplied by the user in the first command-line argument. Directories are not considered to be regular files. Do not print any extra text, just the number followed by a newline character \n. Your program optionally accepts a second command-line argument which is a string of 9 characters that represent the required file permissions. Each position is either r,w,x or - as would be appropriate in that location in the file-permission field of the long format of ls output. If called with this option, your program will only count the files that have at least the permissions specified by this argument. 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 Running $ count_large 1000 rwx------ will print 2 (followed by a single newline \n character) because prog and xyz are both larger than 1000 bytes and have at least the permissions specified. Running $ count_large 1000 will print 3 (followed by a single newline \n character) because there are three files larger than 1000 bytes. Notice that the first line of output from running ls -l is the total line. Your program should expect this line and handle it appropriately. That is, you should read and then ignore it. 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. you may assume the following: The user will provide at most two command-line arguments. If they are provided, arguments will be the correct type and in the correct order. In other words, if only one command-line argument is present, it is an integer. If two are provided, the first is an integer and the second is the required permissions string. The input listing contains only regular files and directories. (There are no links or other unusual file types.) Names of things (filenames, usernames, group names, ...) will be at most 31 characters. An integer is large enough to hold any file sizes for this assignment. Filenames do not have spaces in them. You should use scanf to read the input.
63c648d15f004bf4898d0003
2023-01-28T18:27:31.506Z
Help Write Code
v1
null
null
null
Use scanf to read multiple lines of input from a file and store them.
63c648d15f004bf4898d0003
2023-01-28T18:28:06.791Z
Help Write Code
v1
null
null
null
Use scanf to read multiple lines of input from stdin and store them.
63c648d15f004bf4898d0003
2023-01-28T18:28:43.550Z
Help Write Code
v1
null
null
null
Without using the string libraries, use scanf to read multiple lines of input from stdin and store them.
63c648d15f004bf4898d0003
2023-01-29T03:06:27.034Z
Help Write Code
v1
null
null
null
Convert a 9 digit int to a 9 element int array.
63c648d15f004bf4898d0003
2023-01-29T03:34:38.188Z
Help Write Code
v1
null
null
null
check if an int if 9 digits long
63c648d15f004bf4898d0003
2023-01-29T03:35:13.349Z
Help Write Code
v1
null
null
null
check if an int is 9 digits long without using math.h
63c648d15f004bf4898d0003
2023-01-29T04:03:12.422Z
Help Fix Code
v1
null
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 = strtol(argv[1], NULL, 10); int *sin_array = malloc(sizeof(int) * 9); int valid_input = populate_array(sin, sin_array); int i = 0; while (i < 9) { printf("%d ", sin_array[i]); i++; } int valid_sin = check_sin(sin_array); // return 1 when sin is invalid if(valid_input == 1 || valid_sin == 1) { return 1; } // otherwise sin is valid so return 0 return 0; }
Your program reads a single command line argument representing a candidate SIN number. Your program then prints one of two messages (followed by a single newline \n character) to standard output : "Valid SIN", if the value given is a valid Canadian SIN number, or "Invalid SIN", if it is invalid.
null
63c648d15f004bf4898d0003
2023-01-29T04:05:19.469Z
Help Fix Code
v1
null
int *sin_array = malloc(sizeof(int) * 9); int valid_input = populate_array(sin, sin_array);
pass an array to the helper function
null
63c648d15f004bf4898d0003
2023-01-29T04:13:22.340Z
Help Fix Code
v1
null
/* * Convert a 9 digit int to a 9 element int array. */ int populate_array(int sin, int *sin_array) { // checking length of the given int int digits = 0; // going through the number and counting digits while(sin != 0) { sin = sin / 10; digits++; } // return 1 if the given integer is not 9 digits if(digits != 9) { return 1; } // given integer is 9 digits, so create the array *sin_array = malloc(sizeof(int) * 9); // adding digit into array for(int i = 0; i < 9; i++) { sin_array[i] = sin % 10; // removing the digit from the number sin = sin / 10; } return 0; }
Convert a 9 digit int to a 9 element int array.
null
63c648d15f004bf4898d0003
2023-01-29T04:14:16.741Z
Help Fix Code
v1
null
/* * Convert a 9 digit int to a 9 element int array. */ int populate_array(int sin, int *sin_array) { // checking length of the given int int digits = 0; // going through the number and counting digits while(sin != 0) { sin = sin / 10; digits++; } // return 1 if the given integer is not 9 digits if(digits != 9) { return 1; } // given integer is 9 digits, so create the array sin_array = malloc(sizeof(int) * 9); // adding digit into array for(int i = 0; i < 9; i++) { sin_array[i] = sin % 10; // removing the digit from the number sin = sin / 10; } return 0; }
Convert a 9 digit int to a 9 element int array.
null
63c648d15f004bf4898d0003
2023-02-12T01:50:48.635Z
Question from Code
v1
Why am I getting a segmentation fault from calling create_post in make_post?
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> /* * Helper function for create_user to handle the creation of users * Given name and pointer to next user */ User *create_node(const char *name, User *next) { User *new_user = malloc(sizeof(User)); if(new_user == NULL) { printf("Error: Memory cannot be allocated"); exit(1); } strcpy(new_user->name, name); new_user->next = next; return new_user; } Post *create_post(char *contents) { Post *new_post = malloc(sizeof(Post)); if(new_post == NULL) { printf("Error: Memory cannot be allocated"); exit(1); } strcpy(new_post->contents, contents); return new_post; } /* * Helper function for create_user to handle insertion of new user into list * Given name, pointer to front of the list, and the postition to insert at */ void insert_new_user_to_list(const char *name, User *front) { User *curr_user = front; while (curr_user->next != NULL) { curr_user = curr_user->next; } User *new_user = create_node(name, NULL); curr_user->next = new_user; } /* * Helper function for create_user to handle insertion of new user into list * Given name, pointer to front of the list, and the postition to insert at * user2 is the user being added * user 1 is the user whose friends list we are adding to */ void insert_users_to_friends(User *user1, User *user2) { // find how many friends are currently in user1's friendlist // in order to know where to insert the new user int num_items = 0; while(user1->friends[num_items] != NULL) { num_items++; } // printf("num_items: %d\n", num_items); // if user1 has less than 10 friends if (num_items < MAX_FRIENDS) { // add user2 at the first empty space in user1's friendlist user1->friends[num_items] = user2; } // printf("user1 friend: %s\n", (user1->friends[0])->name); } /* * Helper function for create_user to get length of the linked list */ int get_list_length(User *front, int count) { if(front == NULL) { return count; } return get_list_length(front->next, count + 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) { // return 2 if given name cannot fit in the 'name' array // strlen(name)-1 bc strlen doesn't count null terminator if(strlen(name) > MAX_NAME) { return 2; } ////////////////////////////////////////////////////////////////// // check if there are 0 users in the system if(*user_ptr_add == NULL) { User *first_user = create_node(name, NULL); *user_ptr_add = first_user; return 0; } ////////////////////////////////////////////////////////////////// // traverse list of users whose head is pointed to by *user_ptr_add // start from the front User *curr_user = *user_ptr_add; // checking curr_user->next to see when end of the list is reached while(curr_user != NULL) { // return 1 if user by this name already exists in this list if( strcmp(curr_user->name, name) == 0) { return 1; } curr_user = curr_user->next; } ////////////////////////////////////////////////////////////////// // call function to insert new user into the list insert_new_user_to_list(name, *user_ptr_add); // return 0 upon success return 0; ////////////////////////////////////////////////////////////////// return -1; } /* * 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) { // to traverse list, start from the head const User *curr_user = head; while(curr_user != NULL) { // if a user with matching name is found if(strcmp(curr_user->name, name) == 0) { // return pointer to the user return (User *)curr_user; } curr_user = curr_user->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) { const User *curr_user = curr; printf("User List\n"); while(curr_user != NULL) { printf("\t%s\n", curr_user->name); curr_user = curr_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) { // return 2 if given filename too long // strlen(filename)-1 bc strlen doesn't count null terminator if(strlen(filename)-1 > MAX_NAME) { return 2; } ////////////////////////////////////////////////////////////////// FILE *file = fopen(filename, "r"); // if file exists and can be opened if(file != NULL) { fclose(file); strcpy(user->profile_pic, filename); // return 0 on success return 0; } else { // return 1 if file does not exist or cannot be opened return 1; } ////////////////////////////////////////////////////////////////// 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) { User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL) { return 4; } ////////////////////////////////////////////////////////////////// // Return 3: check if users are the same if(user1 == user2) { return 3; } ////////////////////////////////////////////////////////////////// // Return 1: check if they are friends already // find user1 only since friendship is symmetric // search through user1's friendlist for user2 // to traverse list, start from the head User **curr_user = user1->friends; while(*curr_user != NULL) { // if a user with matching name is found if(strcmp((*curr_user)->name, name2) == 0) { // return pointer to the user return 1; } *curr_user = (*curr_user)->next; } ////////////////////////////////////////////////////////////////// // Return 2: now we know theyre not friends // so check if either one has MAX_FRIENDS friends // get sizes of both user's friend lists int user1_size = sizeof(user1->friends) / sizeof(User); int user2_size = sizeof(user2->friends) / sizeof(User); // if either one has MAX_FRIENDS return 2 if (user1_size == MAX_FRIENDS || user2_size == MAX_FRIENDS) { return 2; } ////////////////////////////////////////////////////////////////// // if all things are false (didnt return 4,3,2, or 1), we can make them friends // insert user2 into user1's friend array // insert user1 into user2's friend array insert_users_to_friends(user1, user2); insert_users_to_friends(user2, user1); 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) { if(user == NULL) { return 1; } // TODO: print user's pfp printf("Name: %s\n", user->name); printf("------------------------------------------\n"); printf("Friends: \n"); // print friends in user's friend list 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 all posts in user's array of posts Post *curr_post = user->first_post; while(curr_post != NULL) { printf("From: %s\n", curr_post->author); time(curr_post->date); printf("Date: %s\n", ctime(curr_post->date)); printf("\n"); printf("%s\n", curr_post->contents); // print this between posts unless it is the last post if(curr_post->next != NULL) { printf("\n"); printf("===\n"); printf("\n"); } curr_post = curr_post->next; } printf("------------------------------------------\n"); 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 */ int make_post(const User *author, User *target, char *contents) { // return 2 if either User pointer is NULL if (author == NULL || target == NULL) { return 2; } // checkcing if target is in author's friend list User *curr_user = *(author->friends); while(curr_user != NULL) { // if author and target are friends if(strcmp((curr_user)->name, target->name) == 0) { // insert new post at front of user's list of posts Post *new_post = create_post(contents); // put in contents, author, date strcpy(new_post->author, author->name); time_t date; time(&date); new_post->date = (&date); // make next of new_post the front new_post->next = target->first_post; // make target->first_post point to the new_post target->first_post = new_post; // return 0 on success return 0; } else { // users exist but are not friends return 1; } curr_user = curr_user->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. */ int delete_user(const char *name, User **user_ptr_del) { return -1; }
null
null
63c648d15f004bf4898d0003
2023-02-12T01:56:55.881Z
Question from Code
v1
Why am I getting a segmentation fault when I call make_post?
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> /* * Helper function for create_user to handle the creation of users * Given name and pointer to next user */ User *create_node(const char *name, User *next) { User *new_user = malloc(sizeof(User)); if(new_user == NULL) { printf("Error: Memory cannot be allocated"); exit(1); } strcpy(new_user->name, name); new_user->next = next; return new_user; } Post *create_post(char **contents) { Post *new_post = malloc(sizeof(Post)); if(new_post == NULL) { printf("Error: Memory cannot be allocated"); exit(1); } strcpy(new_post->contents, *contents); return new_post; } /* * Helper function for create_user to handle insertion of new user into list * Given name, pointer to front of the list, and the postition to insert at */ void insert_new_user_to_list(const char *name, User *front) { User *curr_user = front; while (curr_user->next != NULL) { curr_user = curr_user->next; } User *new_user = create_node(name, NULL); curr_user->next = new_user; } /* * Helper function for create_user to handle insertion of new user into list * Given name, pointer to front of the list, and the postition to insert at * user2 is the user being added * user 1 is the user whose friends list we are adding to */ void insert_users_to_friends(User *user1, User *user2) { // find how many friends are currently in user1's friendlist // in order to know where to insert the new user int num_items = 0; while(user1->friends[num_items] != NULL) { num_items++; } // printf("num_items: %d\n", num_items); // if user1 has less than 10 friends if (num_items < MAX_FRIENDS) { // add user2 at the first empty space in user1's friendlist user1->friends[num_items] = user2; } // printf("user1 friend: %s\n", (user1->friends[0])->name); } /* * Helper function for create_user to get length of the linked list */ int get_list_length(User *front, int count) { if(front == NULL) { return count; } return get_list_length(front->next, count + 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) { // return 2 if given name cannot fit in the 'name' array // strlen(name)-1 bc strlen doesn't count null terminator if(strlen(name) > MAX_NAME) { return 2; } ////////////////////////////////////////////////////////////////// // check if there are 0 users in the system if(*user_ptr_add == NULL) { User *first_user = create_node(name, NULL); *user_ptr_add = first_user; return 0; } ////////////////////////////////////////////////////////////////// // traverse list of users whose head is pointed to by *user_ptr_add // start from the front User *curr_user = *user_ptr_add; // checking curr_user->next to see when end of the list is reached while(curr_user != NULL) { // return 1 if user by this name already exists in this list if( strcmp(curr_user->name, name) == 0) { return 1; } curr_user = curr_user->next; } ////////////////////////////////////////////////////////////////// // call function to insert new user into the list insert_new_user_to_list(name, *user_ptr_add); // return 0 upon success return 0; ////////////////////////////////////////////////////////////////// return -1; } /* * 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) { // to traverse list, start from the head const User *curr_user = head; while(curr_user != NULL) { // if a user with matching name is found if(strcmp(curr_user->name, name) == 0) { // return pointer to the user return (User *)curr_user; } curr_user = curr_user->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) { const User *curr_user = curr; printf("User List\n"); while(curr_user != NULL) { printf("\t%s\n", curr_user->name); curr_user = curr_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) { // return 2 if given filename too long // strlen(filename)-1 bc strlen doesn't count null terminator if(strlen(filename)-1 > MAX_NAME) { return 2; } ////////////////////////////////////////////////////////////////// FILE *file = fopen(filename, "r"); // if file exists and can be opened if(file != NULL) { fclose(file); strcpy(user->profile_pic, filename); // return 0 on success return 0; } else { // return 1 if file does not exist or cannot be opened return 1; } ////////////////////////////////////////////////////////////////// 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) { User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL) { return 4; } ////////////////////////////////////////////////////////////////// // Return 3: check if users are the same if(user1 == user2) { return 3; } ////////////////////////////////////////////////////////////////// // Return 1: check if they are friends already // find user1 only since friendship is symmetric // search through user1's friendlist for user2 // to traverse list, start from the head User **curr_user = user1->friends; while(*curr_user != NULL) { // if a user with matching name is found if(strcmp((*curr_user)->name, name2) == 0) { // return pointer to the user return 1; } *curr_user = (*curr_user)->next; } ////////////////////////////////////////////////////////////////// // Return 2: now we know theyre not friends // so check if either one has MAX_FRIENDS friends // get sizes of both user's friend lists int user1_size = sizeof(user1->friends) / sizeof(User); int user2_size = sizeof(user2->friends) / sizeof(User); // if either one has MAX_FRIENDS return 2 if (user1_size == MAX_FRIENDS || user2_size == MAX_FRIENDS) { return 2; } ////////////////////////////////////////////////////////////////// // if all things are false (didnt return 4,3,2, or 1), we can make them friends // insert user2 into user1's friend array // insert user1 into user2's friend array insert_users_to_friends(user1, user2); insert_users_to_friends(user2, user1); 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) { if(user == NULL) { return 1; } // TODO: print user's pfp printf("Name: %s\n", user->name); printf("------------------------------------------\n"); printf("Friends: \n"); // print friends in user's friend list 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 all posts in user's array of posts Post *curr_post = user->first_post; while(curr_post != NULL) { printf("From: %s\n", curr_post->author); time(curr_post->date); printf("Date: %s\n", ctime(curr_post->date)); printf("\n"); printf("%s\n", curr_post->contents); // print this between posts unless it is the last post if(curr_post->next != NULL) { printf("\n"); printf("===\n"); printf("\n"); } curr_post = curr_post->next; } printf("------------------------------------------\n"); 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 */ int make_post(const User *author, User *target, char *contents) { // return 2 if either User pointer is NULL if (author == NULL || target == NULL) { return 2; } // checkcing if target is in author's friend list User *curr_user = *(author->friends); while(curr_user != NULL) { // if author and target are friends if(strcmp((curr_user)->name, target->name) == 0) { // insert new post at front of user's list of posts Post *new_post = create_post(&contents); // put in contents, author, date strcpy(new_post->author, author->name); time_t date; time(&date); new_post->date = (&date); // make next of new_post the front new_post->next = target->first_post; // make target->first_post point to the new_post target->first_post = new_post; // return 0 on success return 0; } else { // users exist but are not friends return 1; } curr_user = curr_user->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. */ int delete_user(const char *name, User **user_ptr_del) { return -1; }
null
null
63c648d15f004bf4898d0003
2023-02-12T01:58:41.135Z
Help Fix Code
v1
null
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> /* * Helper function for create_user to handle the creation of users * Given name and pointer to next user */ User *create_node(const char *name, User *next) { User *new_user = malloc(sizeof(User)); if(new_user == NULL) { printf("Error: Memory cannot be allocated"); exit(1); } strcpy(new_user->name, name); new_user->next = next; return new_user; } Post *create_post(char **contents) { Post *new_post = malloc(sizeof(Post)); if(new_post == NULL) { printf("Error: Memory cannot be allocated"); exit(1); } strcpy(new_post->contents, *contents); return new_post; } /* * Helper function for create_user to handle insertion of new user into list * Given name, pointer to front of the list, and the postition to insert at */ void insert_new_user_to_list(const char *name, User *front) { User *curr_user = front; while (curr_user->next != NULL) { curr_user = curr_user->next; } User *new_user = create_node(name, NULL); curr_user->next = new_user; } /* * Helper function for create_user to handle insertion of new user into list * Given name, pointer to front of the list, and the postition to insert at * user2 is the user being added * user 1 is the user whose friends list we are adding to */ void insert_users_to_friends(User *user1, User *user2) { // find how many friends are currently in user1's friendlist // in order to know where to insert the new user int num_items = 0; while(user1->friends[num_items] != NULL) { num_items++; } // printf("num_items: %d\n", num_items); // if user1 has less than 10 friends if (num_items < MAX_FRIENDS) { // add user2 at the first empty space in user1's friendlist user1->friends[num_items] = user2; } // printf("user1 friend: %s\n", (user1->friends[0])->name); } /* * Helper function for create_user to get length of the linked list */ int get_list_length(User *front, int count) { if(front == NULL) { return count; } return get_list_length(front->next, count + 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) { // return 2 if given name cannot fit in the 'name' array // strlen(name)-1 bc strlen doesn't count null terminator if(strlen(name) > MAX_NAME) { return 2; } ////////////////////////////////////////////////////////////////// // check if there are 0 users in the system if(*user_ptr_add == NULL) { User *first_user = create_node(name, NULL); *user_ptr_add = first_user; return 0; } ////////////////////////////////////////////////////////////////// // traverse list of users whose head is pointed to by *user_ptr_add // start from the front User *curr_user = *user_ptr_add; // checking curr_user->next to see when end of the list is reached while(curr_user != NULL) { // return 1 if user by this name already exists in this list if( strcmp(curr_user->name, name) == 0) { return 1; } curr_user = curr_user->next; } ////////////////////////////////////////////////////////////////// // call function to insert new user into the list insert_new_user_to_list(name, *user_ptr_add); // return 0 upon success return 0; ////////////////////////////////////////////////////////////////// return -1; } /* * 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) { // to traverse list, start from the head const User *curr_user = head; while(curr_user != NULL) { // if a user with matching name is found if(strcmp(curr_user->name, name) == 0) { // return pointer to the user return (User *)curr_user; } curr_user = curr_user->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) { const User *curr_user = curr; printf("User List\n"); while(curr_user != NULL) { printf("\t%s\n", curr_user->name); curr_user = curr_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) { // return 2 if given filename too long // strlen(filename)-1 bc strlen doesn't count null terminator if(strlen(filename)-1 > MAX_NAME) { return 2; } ////////////////////////////////////////////////////////////////// FILE *file = fopen(filename, "r"); // if file exists and can be opened if(file != NULL) { fclose(file); strcpy(user->profile_pic, filename); // return 0 on success return 0; } else { // return 1 if file does not exist or cannot be opened return 1; } ////////////////////////////////////////////////////////////////// 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) { User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if(user1 == NULL || user2 == NULL) { return 4; } ////////////////////////////////////////////////////////////////// // Return 3: check if users are the same if(user1 == user2) { return 3; } ////////////////////////////////////////////////////////////////// // Return 1: check if they are friends already // find user1 only since friendship is symmetric // search through user1's friendlist for user2 // to traverse list, start from the head User **curr_user = user1->friends; while(*curr_user != NULL) { // if a user with matching name is found if(strcmp((*curr_user)->name, name2) == 0) { // return pointer to the user return 1; } *curr_user = (*curr_user)->next; } ////////////////////////////////////////////////////////////////// // Return 2: now we know theyre not friends // so check if either one has MAX_FRIENDS friends // get sizes of both user's friend lists int user1_size = sizeof(user1->friends) / sizeof(User); int user2_size = sizeof(user2->friends) / sizeof(User); // if either one has MAX_FRIENDS return 2 if (user1_size == MAX_FRIENDS || user2_size == MAX_FRIENDS) { return 2; } ////////////////////////////////////////////////////////////////// // if all things are false (didnt return 4,3,2, or 1), we can make them friends // insert user2 into user1's friend array // insert user1 into user2's friend array insert_users_to_friends(user1, user2); insert_users_to_friends(user2, user1); 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) { if(user == NULL) { return 1; } // TODO: print user's pfp printf("Name: %s\n", user->name); printf("------------------------------------------\n"); printf("Friends: \n"); // print friends in user's friend list 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 all posts in user's array of posts Post *curr_post = user->first_post; while(curr_post != NULL) { printf("From: %s\n", curr_post->author); time(curr_post->date); printf("Date: %s\n", ctime(curr_post->date)); printf("\n"); printf("%s\n", curr_post->contents); // print this between posts unless it is the last post if(curr_post->next != NULL) { printf("\n"); printf("===\n"); printf("\n"); } curr_post = curr_post->next; } printf("------------------------------------------\n"); 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 */ int make_post(const User *author, User *target, char *contents) { // return 2 if either User pointer is NULL if (author == NULL || target == NULL) { return 2; } // checkcing if target is in author's friend list User *curr_user = *(author->friends); while(curr_user != NULL) { // if author and target are friends if(strcmp((curr_user)->name, target->name) == 0) { // insert new post at front of user's list of posts Post *new_post = create_post(&contents); // put in contents, author, date strcpy(new_post->author, author->name); time_t date; time(&date); new_post->date = (&date); // make next of new_post the front new_post->next = target->first_post; // make target->first_post point to the new_post target->first_post = new_post; // return 0 on success return 0; } else { // users exist but are not friends return 1; } curr_user = curr_user->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. */ int delete_user(const char *name, User **user_ptr_del) { return -1; }
make_post should be able to run without getting a segmentation fault.
null
63c648d15f004bf4898d0003
2023-02-12T02:30:30.586Z
Question from Code
v1
why is this code causing a segmentation fault
Post *create_post(char **contents) { Post *new_post = malloc(sizeof(Post)); if(new_post == NULL) { printf("Error: Memory cannot be allocated"); exit(1); } strcpy(new_post->contents, *contents); return new_post; }
null
null
63c648d15f004bf4898d0003
2023-02-12T02:32:38.273Z
Question from Code
v1
contents is a pointer to heap-allocated memory. Why is this code causing a segmentation fault?
typedef struct post { char author[MAX_NAME]; char *contents; time_t *date; struct post *next; } Post; Post *create_post(char **contents) { Post *new_post = malloc(sizeof(Post)); if(new_post == NULL) { printf("Error: Memory cannot be allocated"); exit(1); } strcpy(new_post->contents, *contents); return new_post; }
null
null
63c648d15f004bf4898d0003
2023-02-12T02:33:30.224Z
General Question
v1
do i need to initialize a member of a struct when allocating memory for a struct
null
null
null
63c648d15f004bf4898d0003
2023-02-12T02:34:32.484Z
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; Post *create_post(char **contents) { Post *new_post = malloc(sizeof(Post)); if(new_post == NULL) { printf("Error: Memory cannot be allocated"); exit(1); } strcpy(new_post->contents, *contents); return new_post; }
null
null
63c648d15f004bf4898d0003
2023-02-12T02:36:48.014Z
Question from Code
v1
Why am i getting a segmentation fault
typedef struct post { char author[MAX_NAME]; char *contents; time_t *date; struct post *next; } Post; Post *create_post(char *contents) { Post *new_post = malloc(sizeof(Post)); if(new_post == NULL) { printf("Error: Memory cannot be allocated"); exit(1); } strcpy(new_post->contents, contents); return new_post; } int make_post(const User *author, User *target, char *contents) { // return 2 if either User pointer is NULL if (author == NULL || target == NULL) { return 2; } ////////////////////////////////////////////////////////////////// // return 1 if users exist but are not friends int are_friends = 0; User *curr_friend = *(author->friends); while(curr_friend != NULL) { // if target is in author's friendlist, are_friends is true if(curr_friend == target) { are_friends = 1; } curr_friend = curr_friend->next; } // users are not friends since target is not in author's friendlist if (are_friends == 0) { return 1; } ////////////////////////////////////////////////////////////////// // insert new post at front of user's list of posts Post *new_post = create_post(contents); // put in contents, author, date strcpy(new_post->author, author->name); /* // time_t cur_time = time(NULL); // printf("time: %s\n", ctime(&cur_time)); time_t date = time(&date); new_post->date = &date; // make next of new_post the front new_post->next = target->first_post; // make target->first_post point to the new_post target->first_post = new_post; // return 0 on success */ return 0; return -1; }
null
null
63c648d15f004bf4898d0003
2023-02-12T19:05:39.500Z
Question from Code
v1
Why am I getting a segmentation fault from calling this function?
int print_user(const User *user) { if(user == NULL) { return 1; } // print user's pfp if(user->profile_pic != NULL) { // open profile pic file FILE *pfp_file = fopen(user->profile_pic, "r"); // array to hold each line of the ascii file for printing char read_pfp[MAX_NAME]; // if the file exists if (pfp_file != NULL) { // read until the end of the file while (fgets(read_pfp, sizeof(read_pfp), pfp_file) != NULL) { printf("%s", read_pfp); } } printf("\n"); // close file fclose(pfp_file); } printf("Name: %s\n", user->name); printf("------------------------------------------\n"); printf("Friends: \n"); // print friends in user's friend list 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 all posts in user's array of posts /* // testing time stuff time_t cur_time = time(NULL); printf("time: %s\n", ctime(&cur_time)); */ Post *curr_post = user->first_post; while(curr_post != NULL) { printf("From: %s\n", curr_post->author); time_t cur_time = time(curr_post->date); printf("Date: %s\n", ctime(&cur_time)); printf("%s\n", curr_post->contents); // print this between posts unless it is the last post if(curr_post->next != NULL) { printf("\n"); printf("===\n"); printf("\n"); } curr_post = curr_post->next; } printf("------------------------------------------\n"); return 0; }
null
null
63c648d45f004bf4898d001c
2023-01-17T22:03:13.020Z
Question from Code
v1
why does this generate an error
printf("Please enter a 10 character string:"); scanf("%s", &string); printf("Enter an integer from 0-9: "); scanf("%d", &number);
null
null
63c648d45f004bf4898d001c
2023-01-17T22:04:59.195Z
Help Fix Code
v1
null
printf("Please enter a 10 character string:"); scanf("%s", &string);
prompt user to enter a 10 character string
null
63c648d45f004bf4898d001c
2023-01-18T21:44:03.270Z
Help Fix Code
v1
null
int main() { float *rainfall = NULL; // Allocate space for a single floating point number and assign its location to the pointer rainfall. float *num = malloc(sizeof(float)); rainfall = &num; *rainfall = 42.6; return 0; }
Allocate space for a single floating point number and assign its location to the pointer rainfall.
null
63c648d75f004bf4898d003f
2023-01-30T04:00:03.161Z
Help Fix Code
v1
null
#include <stdio.h> #include <stdlib.h> // TODO: Implement a helper named check_permissions that matches the prototype below. int check_permissions(char *per1, char *per2){ int i; for(i = 0; i <9; i++){ if(per2[i] != '-' && per2[i] != per1[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; int num; char per[9]; // 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. while(scanf("%d %s", &num, per) != EOF){ if(argc ==2 && *argv[1] >= num){ count += 1; } if(argc == 3 && *argv[1] >= num && check_permissions(per, argv[2]) == 0){ count += 1; } } printf("%d\n", count); return 0; } }
prints the number of files with a specific set of permissions that exceed a specified size
null
63c648d75f004bf4898d003f
2023-03-13T22:26:26.989Z
General Question
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 and why?
null
null
null
63c648d75f004bf4898d003f
2023-03-13T22:27:15.893Z
Explain Code
v2
null
int x = 5; void handler(int sig) { x += 3; fprintf(stderr, "inside %d ", x); } int main() { fprintf(stderr, "start "); struct sigaction act; act.sa_handler = handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, NULL); x += 2; fprintf(stderr, "outside %d", x); return 0; }
null
null
63c648d75f004bf4898d003f
2023-03-13T22:32:04.779Z
Explain Code
v2
null
int x = 5; void handler(int sig) { x += 3; fprintf(stderr, "inside %d ", x); } int main() { fprintf(stderr, "start "); return 0; }
null
null
63c648d75f004bf4898d003f
2023-03-13T22:35:46.109Z
Explain Code
v2
null
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); return 0; }
null
null
63c648d75f004bf4898d003f
2023-03-13T22:54:42.965Z
Explain Code
v2
null
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); kill(getpid(), SIGINT) return 0; }
null
null
63c648d75f004bf4898d003f
2023-03-13T23:22:37.899Z
Explain Code
v2
null
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; kill(getpid(), SIGINT); return 0; }
null
null
63c648d75f004bf4898d003f
2023-04-04T03:45:21.679Z
General Question
v2
Consider the sh script shifter.sh: echo I live at $1 $2 $3. shift echo Now, I live at $0 $1 $2. Suppose I run the script as follows: sh shifter.sh 100 University Ave What does it output?
null
null
null
63c648d95f004bf4898d0053
2023-01-20T22:12:22.568Z
General Question
v1
how to use the pointer
null
null
null
63c648d95f004bf4898d0053
2023-01-20T22:13:10.218Z
General Question
v1
is array a pointer
null
null
null
63c648d95f004bf4898d0053
2023-01-27T18:53:25.184Z
General Question
v1
how to separate a string in C
null
null
null
63c648d95f004bf4898d0053
2023-01-27T18:55:46.598Z
General Question
v1
how to separate a string when it change to another line
null
null
null
63c648d95f004bf4898d0053
2023-01-27T18:57:36.984Z
General Question
v1
whether n/ is allowed to be the second argument of "strtok"
null
null
null
63c648d95f004bf4898d0053
2023-01-30T19:05:34.738Z
General Question
v1
how to delete first element in a string
null
null
null
63c648d95f004bf4898d0053
2023-02-09T16:11:17.755Z
General Question
v1
how do I know how many element in array rather than the size of array
null
null
null
63c648d95f004bf4898d0053
2023-02-15T09:08:17.798Z
General Question
v1
how to convert time_t to string
null
null
null
63c648d95f004bf4898d0053
2023-02-15T09:09:15.316Z
General Question
v1
example to use ctime
null
null
null
63c648d95f004bf4898d0053
2023-02-15T09:09:55.618Z
General Question
v1
how to get current time in c
null
null
null
63c648d95f004bf4898d0053
2023-03-14T18:35:37.057Z
General Question
v2
how to open a file as a file pointer and check whether success
null
null
null
63c648d95f004bf4898d0053
2023-03-14T18:50:34.927Z
General Question
v2
how to separate a string by a character, give a example
null
null
null
63c648d95f004bf4898d0053
2023-03-14T22:31:45.905Z
General Question
v2
how to separate a sentence as a array of string
null
null
null
63c648d95f004bf4898d0053
2023-03-15T02:47:18.026Z
General Question
v2
how to read a line from a file
null
null
null
63c648d95f004bf4898d0053
2023-03-15T13:32:19.954Z
General Question
v2
how to use execvp to run a comment line
null
null
null
63c648d95f004bf4898d0053
2023-03-15T16:26:01.656Z
General Question
v2
how to use access in c
null
null
null
63c648da5f004bf4898d0058
2023-02-13T17:08:20.793Z
Question from Code
v1
What will char name[MAXNAME]; become after the program has passed through the C pre-processor?
#define MAXNAME = 32; char name[MAXNAME];
null
null
63c648da5f004bf4898d0058
2023-02-13T17:13:59.884Z
General Question
v1
what is |= 1 << (3)
null
null
null
63c648da5f004bf4898d0058
2023-02-13T17:15:53.948Z
General Question
v1
what is 1 << (3)
null
null
null
63c648da5f004bf4898d0058
2023-02-13T17:17:37.187Z
General Question
v1
what is |=
null
null
null
63c648da5f004bf4898d0058
2023-03-17T20:15:07.725Z
General Question
v2
how do random() work, give me some examples
null
null
null
63c648da5f004bf4898d0058
2023-03-20T19:30:46.957Z
General Question
v2
unsigned char a = 0x13, why is ~a ffffffec
null
null
null
63c648da5f004bf4898d005d
2023-03-14T05:33:27.431Z
Explain Code
v2
null
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; }
null
null
63c648da5f004bf4898d005d
2023-03-14T05:40:19.021Z
Question from Code
v2
what happens when ctrl C is pressed
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; }
null
null
63c648da5f004bf4898d005d
2023-03-14T05:40:53.104Z
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; }
null
null
63c648da5f004bf4898d005d
2023-03-14T05:42:04.762Z
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 "); // 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; }
null
null
63c648da5f004bf4898d005d
2023-03-14T05:44:20.108Z
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?
#include <signal.h> #include <stdio.h> #include <unistd.h> 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); kill(getpid(), SIGINT); return 0; }
null
null
63c648da5f004bf4898d005d
2023-03-14T05:49:12.550Z
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?
#include <signal.h> #include <stdio.h> #include <unistd.h> 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 kill(getpid(), SIGINT); x += 2; // POSITION C fprintf(stderr, "outside %d", x); return 0; }
null
null
63c648da5f004bf4898d005d
2023-03-14T05:50:48.473Z
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?
#include <signal.h> #include <stdio.h> #include <unistd.h> 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 kill(getpid(), SIGINT); fprintf(stderr, "outside %d", x); return 0; }
null
null
63c648db5f004bf4898d0062
2023-01-17T18:28:46.494Z
Explain Code
v1
null
./command.sh
null
null
63c648de5f004bf4898d0080
2023-01-23T21:26:09.398Z
Help Fix Code
v1
null
#include <stdlib.h> #include <stdio.h> /* * 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_pt, int count) { *fib_sequence_pt = malloc(count * sizeof(int)); **fib_sequence_pt = 0; if (count >= 2) { **(fib_sequence_pt + 1) = 1; } if (count >= 3) { for (int i = 2; i < count; i++) { **(fib_sequence_pt + i) = **(fib_sequence_pt + i - 2) + **(fib_sequence_pt + 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; }
print the first count numbers of fibonacci numbers
null
63c648de5f004bf4898d0080
2023-01-23T21:29:53.201Z
Question from Code
v1
where is the segmentation fault
#include <stdlib.h> #include <stdio.h> /* * 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_pt, int count) { *fib_sequence_pt = malloc(count * sizeof(int)); *fib_sequence_pt[0] = 0; if (count >= 2) { *fib_sequence_pt[1] = 1; } if (count >= 3) { for (int i = 2; i < count; i++) { *fib_sequence_pt[i] = *fib_sequence_pt[i - 2] + *fib_sequence_pt[i - 1]; } } } int main(int argc, char **argv) { /* do not change this main function */ int count = strtol(argv[1], NULL, 10); int *fib_sequence; fib(&fib_sequence, count); for (int i = 0; i < count; i++) { printf("%d ", fib_sequence[i]); } free(fib_sequence); return 0; }
null
null
63c648de5f004bf4898d0080
2023-01-24T00:51:15.672Z
Question from Code
v1
any problems with memory allocation?
#include <stdio.h> #include <stdlib.h> /* 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 **result = malloc(2 * sizeof(int *)); result[0] = malloc(length / 2); result[1] = malloc((length - 1) / 2); for (int i = 0; i < length - 1; i++) { if (i % 2 == 0) { result[0][i / 2] = s[i]; } else { result[1][i / 2] = s[i]; } } return result; } /* 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(size * sizeof(int)); for (int i = 0; i < size; i++) { full_array[i] = strtol(strs[i], NULL, 10); } 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 + 1, argc - 1); int **result = split_array(full_array, argc); 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; }
null
null
63c648de5f004bf4898d0080
2023-01-24T00:52:24.606Z
Help Fix Code
v1
null
#include <stdio.h> #include <stdlib.h> /* 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 **result = malloc(2 * sizeof(int *)); result[0] = malloc(length / 2); result[1] = malloc((length - 1) / 2); for (int i = 0; i < length - 1; i++) { if (i % 2 == 0) { result[0][i / 2] = s[i]; } else { result[1][i / 2] = s[i]; } } return result; } /* 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(size * sizeof(int)); for (int i = 0; i < size; i++) { full_array[i] = strtol(strs[i], NULL, 10); } 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 + 1, argc - 1); int **result = split_array(full_array, argc); 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; }
null
null
63c648de5f004bf4898d0080
2023-01-24T03:30:33.904Z
Help Fix Code
v1
null
#include <stdio.h> int main() { int BOARD_SIZE = 3; int *board[BOARD_SIZE]; board[0] = {1, 2, 3}; board[1] = {3, 1, 2}; board[2] = {2, 3, 1}; int *validity[BOARD_SIZE]; for (int i = 0; i < BOARD_SIZE; i++) { int row_array[BOARD_SIZE]; validity[i] = row_array; } for (int row = 0; row < BOARD_SIZE; row++) { for (int col = 0; col < BOARD_SIZE; col++) { int element = board[row][col]; // check element unique in row int unique = 0; for (int j = 0; j < BOARD_SIZE; j++) { if (j != col && element == board[row][j]) { unique = 1; } } if (unique!= 0) { validity[row][col] = 0; } // check element unique in column for (int j = 0; j < BOARD_SIZE; j++) { if (j != row && element == board[j][col]) { unique = 1; } } if (unique != 0) { validity[row][col] = 0; } else { validity[row][col] = 1; } } } }
validity[row][col] should be 1 if board[row][col] is unique among the row and column, otherwise 0.
null
63c648de5f004bf4898d0080
2023-01-24T03:32:19.533Z
General Question
v1
how to declare a variable that is an array of arrays
null
null
null
63c648de5f004bf4898d0080
2023-01-26T03:59:53.692Z
Help Fix Code
v1
null
// Please insert your code below 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.
null
63c648de5f004bf4898d0080
2023-01-26T04:01:25.865Z
Help Fix Code
v1
null
// Please insert your code below typedef Node; 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.
null
63c648de5f004bf4898d0080
2023-02-06T07:47:02.529Z
Help Fix Code
v1
null
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } int i = 0; while (author->friends[i] != NULL && i < MAX_FRIENDS) { User *curr_friend = author->friends[i]; if (compare(curr_friend->name, target->name) == 0) { Post *new_post = malloc(sizeof(struct post)); if (new_post == NULL) { perror("malloc"); exit(3); } strcpy(new_post->author, author->name); (new_post->author)[strlen(author->name)] = '\0'; new_post->contents = contents; time(new_post->date); new_post->next = author->first_post; author->first_post = new_post; return 0; } i++; } 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
null
63c648de5f004bf4898d0080
2023-02-06T07:48:54.864Z
Question from Code
v1
why I get this error: friends.c:241:32: error: assignment of member ‘first_post’ in read-only object 241 | author->first_post = new_post;
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } int i = 0; while (author->friends[i] != NULL && i < MAX_FRIENDS) { User *curr_friend = author->friends[i]; if (compare(curr_friend->name, target->name) == 0) { Post *new_post = malloc(sizeof(struct post)); if (new_post == NULL) { perror("malloc"); exit(3); } strcpy(new_post->author, author->name); (new_post->author)[strlen(author->name)] = '\0'; new_post->contents = contents; time(new_post->date); new_post->next = author->first_post; author->first_post = new_post; return 0; } i++; } return 1; }
null
null
63c648de5f004bf4898d0080
2023-02-06T07:53:16.906Z
Question from Code
v1
why I get this error friends.c:180:17: error: ‘pic_line’ may be used uninitialized [-Werror=maybe-uninitialized] 180 | printf("%s\n", pic_line);
FILE *profile_pic = fopen(user->profile_pic, "r"); if (!(profile_pic == NULL)) { char *pic_line; while (fgets(pic_line, 100, profile_pic) != NULL) { printf("%s\n", pic_line); } }
null
null
63c648de5f004bf4898d0080
2023-02-06T08:07:16.065Z
Question from Code
v1
why do I see segmentation fault
void print_post(Post *post) { printf("From: %s\n", post->author); char *time = ctime(post->date); printf("Date: %s", time); printf("\n"); printf("%s\n", post->contents); }
null
null
63c648de5f004bf4898d0080
2023-02-06T08:11:27.769Z
Question from Code
v1
why is there segmentation fault
void print_post(Post *post) { printf("From: %s\n", post->author); char *time = ctime(post->date); char print_time[26]; strcpy(print_time, time); printf("Date: %s", time); printf("\n"); printf("%s\n", post->contents); }
null
null
63c648de5f004bf4898d0080
2023-02-06T08:12:58.764Z
Help Fix Code
v1
null
void print_post(Post *post) { printf("From: %s\n", post->author); char *time = ctime(post->date); char print_time[26]; strcpy(print_time, time); printf("Date: %s", time); printf("\n"); printf("%s\n", post->contents); }
Print the current post to the screen
null
63c648de5f004bf4898d0080
2023-02-06T18:22:17.692Z
Question from Code
v1
why is there segmentation fault
FILE *profile_pic = fopen(user->profile_pic, "r"); if (!(profile_pic == NULL)) { char *pic_line = NULL; while (fgets(pic_line, 100, profile_pic) != NULL) { printf("%s\n", pic_line); } }
null
null
63c648de5f004bf4898d0080
2023-02-28T17:54:58.488Z
General Question
v1
how is true and false indicated in c
null
null
null
63c648de5f004bf4898d0080
2023-03-02T06:53:09.164Z
Question from Code
v1
modify the code such that the child processes do not create any additional processes
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: parentcreates <iterations>\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; }
null
null
63c648de5f004bf4898d0080
2023-03-02T06:54:34.811Z
Question from Code
v1
modify the code such that the child processes do not create any additional processes, where the parent still needs to iterate through the loop
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: parentcreates <iterations>\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; }
null
null
63c648de5f004bf4898d0080
2023-03-04T18:44:49.939Z
Question from Code
v1
why does it have the segmentation fault
while (fgets(line, MAXLINE + 1, fp) != NULL) { // stops reading when no more lines printf("%s\n", line); // just for debugging
null
null
63c648de5f004bf4898d0080
2023-03-04T19:02:31.950Z
Help Fix Code
v1
null
char *trim_line(char *line) { int i = strlen(line) - 1; while (i >= 0) { if (line[i] == '\r' || line[i] == '\n' || line[i] == ' ') { line[i] = '\0'; i -= 1; } else { break; } } while (line[0] == ' ') { line += 1; } return line; }
Remove trailing '\n\, '\r' characters and space as well as possible leading spaces from the line.
null
63c648de5f004bf4898d0080
2023-03-04T19:03:27.793Z
Question from Code
v1
why does it keep running
char *trim_line(char *line) { int i = strlen(line) - 1; while (i >= 0) { if (line[i] == '\r' || line[i] == '\n' || line[i] == ' ') { line[i] = '\0'; i -= 1; } else { break; } } while (line[0] == ' ') { line += 1; } return line; }
null
null
63c648de5f004bf4898d0080
2023-03-04T19:04:28.425Z
General Question
v1
what is the space character
null
null
null
63c648de5f004bf4898d0080
2023-03-04T19:04:49.930Z
General Question
v1
How to express a space character
null
null
null
63c648de5f004bf4898d0080
2023-03-05T04:13:10.970Z
General Question
v1
strtok does not finished when there are consecutive delimiters, what should I do
null
null
null
63c648de5f004bf4898d0080
2023-03-05T04:14:32.480Z
Question from Code
v1
strtok does not finish with consecutive delimiters, what should I do
while ((depend_name = strtok(NULL, " ")) != NULL) { Dependency *new_dependency = malloc(sizeof(Dependency)); if (first_dep == NULL) { perror("malloc"); exit(1); } new_dependency->rule = get_rule(first_rule, depend_name); curr_dependency->next_dep = new_dependency; curr_dependency = new_dependency; }
null
null
63c648de5f004bf4898d0080
2023-03-05T06:03:02.503Z
Help Fix Code
v1
null
/* Check if the actions of the rule needs to be evaluated. Return 0 if the actions should be executed, and return 1 otherwise. Precondition: the file associated with the rule's target name exists and the rule has dependencies */ int check_dep_time(Rule *rule) { struct stat target_stat; if (stat(rule->target, &target_stat) != 0) { perror("stat"); exit(1); } Dependency *curr_dependency = rule->dependencies; while (curr_dependency != NULL) { struct stat dep_stat; if (stat(rule->target, &dep_stat) != 0) { perror("stat"); exit(1); } if (compare_time(target_stat.st_mtim, dep_stat.st_mtim) == 0) { return 0; } curr_dependency = curr_dependency->next_dep; } return 1; } /* Compare the modification time of the target file with a dependency file. If the time of the dependency file is newer than that of the target, return 0; otherwise, return 1. */ int compare_time(struct timespec target_st_mtim, struct timespec dep_st_mtim) { // Check the second part if (target_st_mtim.tv_sec > dep_st_mtim.tv_sec) { return 1; } else if (target_st_mtim.tv_sec < dep_st_mtim.tv_sec) { return 0; } else { // Check the nanosecond part if (target_st_mtim.tv_nsec < dep_st_mtim.tv_nsec) { return 0; } else { return 1; } } }
st_mtim should give me the time of the modification time of the file, but the value I get is not.
null
63c648de5f004bf4898d0080
2023-03-05T06:04:12.559Z
Help Fix Code
v1
null
int compare_time(struct timespec target_st_mtim, struct timespec dep_st_mtim) { // Check the second part if (target_st_mtim.tv_sec > dep_st_mtim.tv_sec) { return 1; } else if (target_st_mtim.tv_sec < dep_st_mtim.tv_sec) { return 0; } else { // Check the nanosecond part if (target_st_mtim.tv_nsec < dep_st_mtim.tv_nsec) { return 0; } else { return 1; } } }
Compare the modification time of the target file with a dependency file. If the time of the dependency file is newer than that of the target, return 0; otherwise, return 1.
null
63c648de5f004bf4898d0080
2023-03-13T06:09:20.866Z
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; }
null
null
63c648de5f004bf4898d0080
2023-03-15T02:11:13.703Z
Help Write Code
v2
null
null
null
generate random integers between 0 and 99. To generate random numbers, use the function random
63c648de5f004bf4898d0080
2023-03-15T02:16:20.150Z
Explain Code
v2
null
random() % 100;
null
null
63c648de5f004bf4898d0080
2023-03-15T02:30:29.863Z
Question from Code
v2
why is the number read differently than the number write
for (int i = 0; i < 100; i++) { int num = random() % 100; fwrite(&num, sizeof(int), 1, fp); } for (int i = 0; i >= 0; i++) { int loc = random() % 100; fseek(fp, loc, SEEK_SET); int num; fread(&num, sizeof(int), 1, fp); fprintf(stderr, "%d ", num); }
null
null
63c648de5f004bf4898d0080
2023-03-24T17:35:20.445Z
Help Fix Code
v2
Why is everytime accept_connection updates new_usock always changes first_usock, and that first_usock and new_usock refer to the same thing?
while (1) { Usock new_usock; // create a new Usock struct if (first_usock == NULL) { // first client connection first_usock = &new_usock; } else { // subsequent connections Usock *curr_usock = first_usock; while (curr_usock->next != NULL) { curr_usock = curr_usock->next; } curr_usock->next = &new_usock; } struct sockaddr_in client_addr; unsigned int client_len = sizeof(struct sockaddr_in); client_addr.sin_family = AF_INET; int client_socket = accept_connection(server_socket, &new_usock, &client_addr, &client_len); }
null
null
63c648de5f004bf4898d0080
2023-04-24T17:29:12.759Z
General Question
v2
what happens when the parent calls wait when it has no child?
null
null
null
63c648de5f004bf4898d0080
2023-04-24T17:32:00.481Z
General Question
v2
if the parent process has created child processes, but the child processes have terminated before the parent calling wait, what happens when the parent calls wait?
null
null
null
63c648de5f004bf4898d0080
2023-04-25T03:08:56.018Z
General Question
v2
what is STDIN_FILENO
null
null
null
63c648e15f004bf4898d0099
2023-01-18T02:21:17.638Z
General Question
v1
In C, does a function named "main" automatically get executed?
null
null
null
63c648e15f004bf4898d0099
2023-01-27T00:22:27.333Z
Question from Code
v1
What is wrong? It gets a segmentation fault.
int *fib(int **seq, int n) { *seq = malloc(sizeof(int) * n); int i; int sum = 0; for (i = 0; i < n; i++) { *seq[i] = sum; if (i >= 1) { sum += *seq[i-1]; } else if (i == 0) { sum += 1; } } return *seq; }
null
null
63c648e15f004bf4898d0099
2023-02-03T05:28:53.984Z
Question from Code
v1
why does greeting print weird characters at the beginning?
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 int len = strlen(argv[1]); int space_left = 19 - len; strncat(greeting, argv[1], 19); if (space_left > 0) { strcat(greeting, " "); space_left--; } strncat(greeting, name, space_left); printf("%s\n", greeting); return 0; }
null
null