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
⌀ |
---|---|---|---|---|---|---|---|
63c6484b5f004bf4898cfb35 | 2023-01-18T00:49:22.194Z | Question from Code | v1 | what is the type of test? | int *test[2]; | null | null |
63c6484b5f004bf4898cfb35 | 2023-01-18T02:44:04.960Z | General Question | v1 | what are the arguments for scanf? | null | null | null |
63c6484b5f004bf4898cfb35 | 2023-01-18T02:45:00.420Z | General Question | v1 | What does each of the format specifiers %s, %d, %f, %c mean? | null | null | null |
63c6484b5f004bf4898cfb35 | 2023-01-18T03:01:04.392Z | General Question | v1 | How do I hold a string of 10 characters from scanf to a variable? | null | null | null |
63c6484b5f004bf4898cfb35 | 2023-01-18T03:02:46.442Z | Help Fix Code | v1 | null | 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 | null |
63c6484b5f004bf4898cfb35 | 2023-01-18T03:03:48.318Z | Help Fix Code | v1 | null | 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 | null |
63c6484b5f004bf4898cfb35 | 2023-01-18T03:04:12.487Z | Help Fix Code | v1 | null | 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 | null |
63c6484b5f004bf4898cfb35 | 2023-01-18T03:10:09.485Z | Help Fix Code | v1 | null | 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 | null |
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" | null | null | null |
63c6484b5f004bf4898cfb35 | 2023-01-18T03:20:09.552Z | General Question | v1 | generate a detailed documentation of `EOF` with usage examples and explanations | null | null | null |
63c6484b5f004bf4898cfb35 | 2023-01-18T03:30:06.841Z | General Question | v1 | How do we redirect the input to come from a file? | null | null | null |
63c6484b5f004bf4898cfb35 | 2023-01-18T03:32:21.961Z | Help Fix Code | v1 | null | 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 | null |
63c6484b5f004bf4898cfb35 | 2023-01-21T04:26:26.003Z | Question from Code | v1 | what is the type of argv[1] | int main(int argc, char **argv) | null | null |
63c6484b5f004bf4898cfb35 | 2023-01-21T04:43:28.009Z | General Question | v1 | what is the difference between %s and %3s in scanf | null | null | null |
63c6484b5f004bf4898cfb35 | 2023-01-21T04:44:01.185Z | General Question | v1 | does %s specifier have a character length limit? | null | null | null |
63c6484b5f004bf4898cfb35 | 2023-01-21T04:44:51.179Z | General Question | v1 | what does scanf("%1s%9s") mean | null | null | null |
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") | null | null |
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? | null | null | null |
63c6484b5f004bf4898cfb35 | 2023-01-21T04:48:10.669Z | General Question | v1 | what does each column of ls -l mean? | null | null | null |
63c6484b5f004bf4898cfb35 | 2023-01-21T05:05:19.532Z | General Question | v1 | how to check if a string is empty or not? | null | null | null |
63c6484b5f004bf4898cfb35 | 2023-01-21T05:06:41.639Z | Help Fix Code | v1 | null | 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 | null |
63c6484b5f004bf4898cfb35 | 2023-01-21T05:16:50.894Z | Help Fix Code | v1 | null | char test[2];
if (test[0] != '\0') | check if test is an empty string | null |
63c6484b5f004bf4898cfb35 | 2023-01-21T05:22:39.146Z | Help Fix Code | v1 | null | int main() {
char test[3];
if (test[0] == '\0') {
printf("Hello");
}
return 0;
} | print Hello if the string test is empty | null |
63c6484b5f004bf4898cfb35 | 2023-01-21T05:24:27.063Z | Question from Code | v1 | what value does test[0] hold after declaring |
char test[3]; | null | null |
63c6484b5f004bf4898cfb35 | 2023-01-21T05:24:55.501Z | General Question | v1 | what value can I use to compare with an undefined value | null | null | null |
63c6484b5f004bf4898cfb35 | 2023-01-21T05:25:52.066Z | Help Fix Code | v1 | null | int main() {
char test[3];
if (test == NULL) {
printf("Hello");
}
return 0;
} | print Hello if the string test is empty | null |
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); | null | null |
63c6484b5f004bf4898cfb35 | 2023-01-27T09:45:34.426Z | General Question | v1 | what does typedef mean? | null | null | null |
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;
} | null | null |
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;
} | null | null |
63c6484b5f004bf4898cfb35 | 2023-02-02T05:40:26.782Z | General Question | v1 | does strcat add a null terminator to the end? | null | null | null |
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) | null | null |
63c6484b5f004bf4898cfb35 | 2023-02-02T05:44:17.171Z | Question from Code | v1 | will there be an error? | strncat(s1, s2, 0) | null | null |
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;
} | null | null |
63c6484b5f004bf4898cfb35 | 2023-02-02T05:46:16.676Z | Help Fix Code | v1 | null |
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 | null |
63c6484c5f004bf4898cfb3a | 2023-02-11T22:32:33.847Z | Help Fix Code | v1 | null | struct Node {
int value;
struct Node *next;
}; | make a node struct | null |
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;
} | null | null |
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;
} | null | null |
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;
} | null | null |
63c6484c5f004bf4898cfb3a | 2023-02-12T18:16:25.709Z | Help Fix Code | v1 | null | 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 | null |
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;
} | null | null |
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;
} | null | null |
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;
} | null | null |
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;
} | null | null |
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;
} | null | null |
63c6484c5f004bf4898cfb3a | 2023-02-13T01:30:48.137Z | Help Fix Code | v1 | null | /*
* 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 | null |
63c6484c5f004bf4898cfb3a | 2023-02-13T01:34:40.598Z | Help Fix Code | v1 | null | /*
* 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 | null |
63c6484c5f004bf4898cfb3a | 2023-02-13T01:37:42.414Z | Help Fix Code | v1 | null | /*
* 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 | null |
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;
} | null | null |
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;
}
| null | null |
63c6484e5f004bf4898cfb4e | 2023-01-19T22:18:05.264Z | Help Fix Code | v1 | null | #include <stdio.h>
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 | null |
63c6484e5f004bf4898cfb4e | 2023-01-19T22:20:17.126Z | Help Fix Code | v1 | null | #include <stdio.h>
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
| null |
63c6484e5f004bf4898cfb4e | 2023-01-19T22:52:34.910Z | Help Fix Code | v1 | null | #include <stdio.h>
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 | null |
63c6484e5f004bf4898cfb4e | 2023-01-22T19:57:04.209Z | Help Fix Code | v1 | null | 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. | null |
63c6484e5f004bf4898cfb4e | 2023-01-22T20:00:10.983Z | Help Fix Code | v1 | null | 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. | null |
63c6484e5f004bf4898cfb4e | 2023-01-22T20:01:59.277Z | Help Fix Code | v1 | null | 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. | null |
63c6484e5f004bf4898cfb4e | 2023-01-22T20:22:42.724Z | Help Fix Code | v1 | null | 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 | null |
63c6484e5f004bf4898cfb4e | 2023-01-25T00:39:27.991Z | Help Fix Code | v1 | null | 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. | null |
63c6484e5f004bf4898cfb4e | 2023-01-25T00:41:30.498Z | Help Fix Code | v1 | null | 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. | null |
63c6484e5f004bf4898cfb4e | 2023-01-25T00:45:07.853Z | Help Fix Code | v1 | null | 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. | null |
63c6484e5f004bf4898cfb4e | 2023-01-25T00:48:00.164Z | Help Write Code | v1 | null | null | null | 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 | null | 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 | null |
63c6484e5f004bf4898cfb4e | 2023-01-25T02:31:32.976Z | Help Fix Code | v1 | null | 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 | null |
63c6484e5f004bf4898cfb4e | 2023-01-25T02:31:57.442Z | Help Fix Code | v1 | null | 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;
} | null | null |
63c6484e5f004bf4898cfb4e | 2023-01-25T02:32:26.470Z | Help Fix Code | v1 | null | 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 | null |
63c6484e5f004bf4898cfb4e | 2023-01-25T02:34:17.545Z | Help Fix Code | v1 | null | 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 | null |
63c6484e5f004bf4898cfb4e | 2023-01-25T17:17:26.329Z | Help Fix Code | v1 | null | 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 | null |
63c6484e5f004bf4898cfb4e | 2023-02-03T03:55:16.623Z | Help Fix Code | v1 | null | 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. | null |
63c6484e5f004bf4898cfb4e | 2023-02-03T03:56:46.494Z | Help Write Code | v1 | null | null | null | 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 | null | null | null |
63c6484e5f004bf4898cfb4e | 2023-02-03T03:58:39.716Z | Help Fix Code | v1 | null | 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. | null |
63c6484e5f004bf4898cfb4e | 2023-02-03T04:09:44.175Z | Help Write Code | v1 | null | null | null | remove trailing ? in a string |
63c6484e5f004bf4898cfb4e | 2023-02-03T04:18:43.529Z | Help Fix Code | v1 | null | 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. | null |
63c6484e5f004bf4898cfb4e | 2023-02-14T19:34:26.634Z | Help Fix Code | v1 | null | 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 | null |
63c648515f004bf4898cfb67 | 2023-02-06T21:41:50.914Z | Explain Code | v1 | null | #include <stdio.h>
#include <sys/types.h>
int main()
{
while(1)
fork();
return 0;
} | null | null |
63c648515f004bf4898cfb67 | 2023-02-10T21:26:09.616Z | Help Fix Code | v1 | null | 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.
*/ | null |
63c648515f004bf4898cfb6c | 2023-01-23T16:20:31.371Z | General Question | v1 | write a fibonacci sequence with n elements and save it in array | null | null | null |
63c648515f004bf4898cfb6c | 2023-01-23T16:26:54.601Z | Explain Code | v1 | null | 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];
}
}
| null | null |
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;
}
| null | null |
63c648525f004bf4898cfb76 | 2023-02-15T01:12:52.019Z | General Question | v1 | how do you use booleans? | null | null | null |
63c648525f004bf4898cfb76 | 2023-02-15T01:16:43.983Z | General Question | v1 | how do you use time_t? | null | null | null |
63c648525f004bf4898cfb76 | 2023-03-10T02:18:55.765Z | Question from Code | v1 | Why is this code not compiling? | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#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;
}
| null | null |
63c648525f004bf4898cfb76 | 2023-03-14T17:48:19.178Z | General Question | v2 | how can I use strtok to seperate by a space? | null | null | null |
63c648525f004bf4898cfb76 | 2023-03-14T17:59:28.936Z | Question from Code | v2 | Why am I getting compilation errors | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}
| null | null |
63c648525f004bf4898cfb76 | 2023-03-14T18:07:56.554Z | General Question | v2 | What does "implicit declaration of function" mean | null | null | null |
63c648525f004bf4898cfb76 | 2023-03-14T19:42:34.690Z | General Question | v2 | is there a function that returns the number of tokens in a string | null | null | null |
63c648525f004bf4898cfb76 | 2023-03-15T01:59:53.415Z | General Question | v2 | what is an example of execvp | null | null | null |
63c648525f004bf4898cfb76 | 2023-03-15T02:50:29.235Z | General Question | v2 | how do you use stat? | null | null | null |
63c648525f004bf4898cfb76 | 2023-03-15T03:13:30.896Z | General Question | v2 | can a struct be returned from a function? | null | null | null |
63c648525f004bf4898cfb76 | 2023-03-17T15:26:22.887Z | General Question | v2 | how do you generate random numbers in a range | null | null | null |
63c648525f004bf4898cfb76 | 2023-04-06T19:15:34.794Z | General Question | v2 | can I set default values in a struct | null | null | null |
63c648545f004bf4898cfb85 | 2023-02-10T16:34:53.504Z | Help Fix Code | v1 | null | 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. | null |
63c648545f004bf4898cfb85 | 2023-02-10T17:28:49.980Z | General Question | v1 | difference between printf and fprintf | null | null | null |
63c648545f004bf4898cfb85 | 2023-02-10T18:47:24.237Z | General Question | v1 | how to write to a file | null | null | null |
63c648545f004bf4898cfb85 | 2023-02-10T20:04:18.534Z | General Question | v1 | what is the return value when fread successfully reads a file. | null | null | null |
63c648545f004bf4898cfb85 | 2023-02-10T20:40:09.945Z | General Question | v1 | WHAT IS THE RETURN VALUE OF fseek | null | null | null |
63c648545f004bf4898cfb85 | 2023-02-10T20:43:54.276Z | General Question | v1 | give me an example of using fseek | null | null | null |
63c648545f004bf4898cfb85 | 2023-02-10T21:56:15.108Z | General Question | v1 | what does this mean "First, allocate space for m `struct pixel *` values" | null | null | null |
63c648555f004bf4898cfb8f | 2023-01-27T19:09:16.963Z | General Question | v1 | what is a fibonacci sequence | null | null | null |
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? | null | null | null |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.