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
⌀ |
---|---|---|---|---|---|---|---|
63c648825f004bf4898cfd1f | 2023-03-10T16:41:18.460Z | General Question | v2 | man waitpid | null | null | null |
63c648825f004bf4898cfd1f | 2023-03-10T16:44:29.039Z | General Question | v2 | man wifexitstatus | null | null | null |
63c648825f004bf4898cfd1f | 2023-03-10T16:47:13.077Z | General Question | v2 | man wexitstatus | null | null | null |
63c648825f004bf4898cfd1f | 2023-03-14T20:48:36.017Z | General Question | v2 | How to compare string literals | null | null | null |
63c648825f004bf4898cfd1f | 2023-03-14T20:52:00.297Z | General Question | v2 | turn an array into a string literal | null | null | null |
63c648825f004bf4898cfd1f | 2023-03-14T21:08:48.391Z | General Question | v2 | how to initialize string array | null | null | null |
63c648825f004bf4898cfd1f | 2023-03-14T22:44:17.571Z | General Question | v2 | create a string literal from a variable | null | null | null |
63c648825f004bf4898cfd1f | 2023-03-14T22:46:58.630Z | General Question | v2 | return variable of strtok | null | null | null |
63c648825f004bf4898cfd1f | 2023-03-14T22:47:56.122Z | General Question | v2 | man strlen | null | null | null |
63c648825f004bf4898cfd1f | 2023-03-15T00:43:51.126Z | General Question | v2 | why does it say unused variable when I used the variable | null | null | null |
63c648825f004bf4898cfd1f | 2023-03-15T03:39:59.858Z | General Question | v2 | man page for fgets | null | null | null |
63c648825f004bf4898cfd1f | 2023-03-15T03:40:25.708Z | General Question | v2 | when does fgets stop reading a line | null | null | null |
63c648825f004bf4898cfd1f | 2023-03-15T15:42:49.319Z | General Question | v2 | man page stat | null | null | null |
63c648825f004bf4898cfd1f | 2023-03-24T18:26:49.645Z | General Question | v2 | man page for read | null | null | null |
63c648825f004bf4898cfd1f | 2023-04-05T21:19:25.618Z | General Question | v2 | man page write | null | null | null |
63c648825f004bf4898cfd1f | 2023-04-05T22:10:15.320Z | Help Fix Code | v2 | does it compile | memset(buf, '\0', BUFSIZE - 1); | null | null |
63c648825f004bf4898cfd1f | 2023-04-06T19:42:46.410Z | General Question | v2 | man page read | null | null | null |
63c648825f004bf4898cfd1f | 2023-04-06T20:59:42.930Z | General Question | v2 | man page fd_clr | null | null | null |
63c648825f004bf4898cfd1f | 2023-04-06T20:59:58.157Z | General Question | v2 | man FD_CLR | null | null | null |
63c648825f004bf4898cfd1f | 2023-04-25T04:13:37.556Z | General Question | v2 | man fscanf | null | null | null |
63c648825f004bf4898cfd1f | 2023-04-25T04:20:40.802Z | General Question | v2 | how to use printf | null | null | null |
63c648825f004bf4898cfd1f | 2023-04-25T17:54:22.249Z | General Question | v2 | How to declare a function with a function return value | null | null | null |
63c648845f004bf4898cfd2e | 2023-03-13T20:08:01.288Z | Question from Code | v2 | where is the segmentation fault | Rule *check_rule_in_list(Rule *rules, char *target) {
Rule *curr = rules;
if (curr == NULL) {
return curr;
}
while (curr->next_rule != NULL) {
if (strcmp(target, curr->target) == 0) {
return curr;
} else {
curr = curr->next_rule;
}
}
return curr;
} | null | null |
63c648845f004bf4898cfd2e | 2023-03-13T20:10:08.925Z | Question from Code | v2 | is there a segmentation fault? | Rule *check_rule_in_list(Rule *rules, char *target) {
Rule *curr = rules;
while (curr != NULL && curr->next_rule != NULL) {
if (strcmp(target, curr->target) == 0) {
return curr;
} else {
curr = curr->next_rule;
}
}
return curr;
} | null | null |
63c648845f004bf4898cfd2e | 2023-03-13T20:10:45.681Z | Question from Code | v2 | is there a segmentation fault? | Rule *create_rule(Rule *rules, char *target) {
Rule *curr = check_rule_in_list(rules, target);
if (curr == NULL) {
Rule *newRule = malloc(sizeof(Rule));
strcpy(newRule->target, target);
newRule->next_rule = NULL;
newRule->actions = NULL;
newRule->dependencies = NULL;
curr = newRule;
return newRule;
} else {
Rule *newRule = malloc(sizeof(Rule));
strcpy(newRule->target, target);
newRule->next_rule = NULL;
newRule->actions = NULL;
newRule->dependencies = NULL;
curr->next_rule = newRule;
return newRule;
}
} | null | null |
63c648845f004bf4898cfd2e | 2023-03-13T20:11:30.995Z | Question from Code | v2 | is there a segmentation fault? | void create_dependancy(Rule* rules, Rule* parent, char* target) {
Dependency* curr = parent->dependencies;
int exists = 0;
while (curr != NULL) {
if (strcmp(curr->rule->target, target) == 0) {
exists = 1;
}
curr = curr->next_dep;
}
if (exists != 1) {
Dependency* newDep = malloc(sizeof(Dependency));
newDep->rule = create_rule(rules, target);
newDep->next_dep = NULL;
curr = newDep;
}
} | null | null |
63c648845f004bf4898cfd2e | 2023-03-13T20:13:26.847Z | Question from Code | v2 | is there a segmentation fault? | Rule *parse_file(FILE *fp) {
Rule *rules = NULL;
char line[MAXLINE];
Rule *currParent = NULL;
while (fgets(line, sizeof(line) - 1, fp)) {
line[strlen(line) - 1] = '\0';
char *token;
if (strchr(line, '#') != NULL) {
line[strlen(line) - strlen(strchr(line, '#'))] = '\0';
}
token = strtok(line, " ");
if (token == NULL | strcmp(token, "\n") == 0) {
} else {
char *newTarget = token;
token = strtok(NULL, line);
if (strcmp(token, ":") == 0) {
Rule *parent = create_rule(rules, newTarget);
currParent = parent;
while (token != NULL) {
create_dependancy(rules, parent, token);
token = strtok(NULL, line);
}
} else {
if (currParent == NULL) {
printf("error: action line leading file without respective target line");
exit(1);
}
Action *actionCurr = currParent->actions;
while (actionCurr != NULL) {
actionCurr = actionCurr->next_act;
}
Action *newAction = malloc(sizeof(Action));
newAction->next_act = NULL;
newAction->args[0] = newTarget;
int i = 0;
while (token != NULL) {
newAction->args[i] = token;
i += 1;
token = strtok(NULL, line);
}
newAction->args[i] = NULL;
actionCurr = newAction;
}
}
}
return rules;
} | null | null |
63c648845f004bf4898cfd2e | 2023-03-13T20:14:00.106Z | Question from Code | v2 | is there any issues with my code? | Rule *parse_file(FILE *fp) {
Rule *rules = NULL;
char line[MAXLINE];
Rule *currParent = NULL;
while (fgets(line, sizeof(line) - 1, fp)) {
line[strlen(line) - 1] = '\0';
char *token;
if (strchr(line, '#') != NULL) {
line[strlen(line) - strlen(strchr(line, '#'))] = '\0';
}
token = strtok(line, " ");
if (token == NULL | strcmp(token, "\n") == 0) {
} else {
char *newTarget = token;
token = strtok(NULL, line);
if (strcmp(token, ":") == 0) {
Rule *parent = create_rule(rules, newTarget);
currParent = parent;
while (token != NULL) {
create_dependancy(rules, parent, token);
token = strtok(NULL, line);
}
} else {
if (currParent == NULL) {
printf("error: action line leading file without respective target line");
exit(1);
}
Action *actionCurr = currParent->actions;
while (actionCurr != NULL) {
actionCurr = actionCurr->next_act;
}
Action *newAction = malloc(sizeof(Action));
newAction->next_act = NULL;
newAction->args[0] = newTarget;
int i = 0;
while (token != NULL) {
newAction->args[i] = token;
i += 1;
token = strtok(NULL, line);
}
newAction->args[i] = NULL;
actionCurr = newAction;
}
}
}
return rules;
} | null | null |
63c648845f004bf4898cfd2e | 2023-03-13T20:16:08.310Z | Question from Code | v2 | is there any issues with my code? | void create_dependancy(Rule* rules, Rule* parent, char* target) {
Dependency* curr = parent->dependencies;
int exists = 0;
while (curr != NULL) {
if (strcmp(curr->rule->target, target) == 0) {
exists = 1;
}
curr = curr->next_dep;
}
if (exists != 1) {
Dependency* newDep = malloc(sizeof(Dependency));
newDep->rule = create_rule(rules, target);
newDep->next_dep = NULL;
curr = newDep;
}
} | null | null |
63c648845f004bf4898cfd2e | 2023-03-14T18:22:48.667Z | Question from Code | v2 | why is there a segmentation fault here? | Rule *parse_file(FILE *fp) {
Rule *rules = NULL;
char line[MAXLINE];
Rule *currParent = NULL;
while (fgets(line, sizeof(line) - 1, fp) != NULL) {
line[strlen(line)] = '\0';
char *token;
if (strchr(line, '#') != NULL) {
line[strlen(line) - strlen(strchr(line, '#'))] = '\0';
}
token = strtok(line, " ");
if (token != NULL && strcmp(token, "\n") != 0) {
char *newTarget = token;
token = strtok(NULL, " ");
if (strcmp(token, ":") == 0) {
Rule *parent = create_rule(rules, newTarget);
currParent = parent;
while (token != NULL) {
create_dependancy(rules, parent, token);
token = strtok(NULL, " ");
}
} | null | null |
63c648845f004bf4898cfd2e | 2023-03-14T19:16:23.054Z | Help Fix Code | v2 | take in a file, read it, tokenize each line and create new structures using helper fuctions without a segmentation error. | Rule *parse_file(FILE *fp) {
Rule *rules = NULL;
char line[MAXLINE];
Rule *currParent = NULL;
while(fgets(line, sizeof(line) - 1, fp) != NULL) {
line[strlen(line)] = '\0';
char *token;
printf("%s\n", line);
//handling #, so that line ignores everything after it
if (strchr(line, '#') != NULL){
char* remove = strchr(line, '#');
*remove = '\0';
}
token = strtok(line, " \n");
//skip lines with nothing or just a newline char
if (token != NULL){
printf("%s\n", token);
char *newTarget = NULL;
strcpy(newTarget, token);
token = strtok(NULL, " \n");
printf("%s\n", token);
printf("HERE\n");
//handling Target lines
if (token == NULL){
}
else if (strchr(token, ':') != NULL) {
printf("gotheretoo\n");
//create initial rule
Rule *parent = create_rule(rules, newTarget);
printf("gotheretooAFTER\n");
currParent = parent;
printf("gotheretooAFTER\n");
while (token != NULL){
printf("gotheretoo\n");
create_dependancy(rules, parent, token);
token = strtok(NULL, " \n");
}
}
else {
if (currParent == NULL){
printf("error: action line leading file without respective target line");
exit(1);
}
Action *actionCurr = currParent->actions;
while (actionCurr != NULL){
actionCurr = actionCurr->next_act;
}
Action *newAction = malloc(sizeof(Action));
newAction->next_act = NULL;
newAction->args[0] = newTarget;
int i = 0;
while(token != NULL){
newAction->args[i] = token;
i += 1;
token = strtok(NULL, " ");
}
newAction->args[i] = NULL;
actionCurr = newAction;
}
}
}
return rules;
} | null | null |
63c648845f004bf4898cfd2e | 2023-03-14T19:19:11.248Z | Question from Code | v2 | why does this code keep giving me a segmentation error
| Rule *parse_file(FILE *fp) {
Rule *rules = NULL;
char line[MAXLINE];
Rule *currParent = NULL;
while (fgets(line, sizeof(line) - 1, fp) != NULL) {
line[strlen(line)] = '\0';
char *token;
printf("%s\n", line);
if (strchr(line, '#') != NULL) {
char *remove = strchr(line, '#');
*remove = '\0';
}
token = strtok(line, " \n");
if (token != NULL) {
printf("%s\n", token);
char *newTarget;
strcpy(newTarget, token);
token = strtok(NULL, " \n");
printf("%s\n", token);
printf("HERE\n");
if (token == NULL) {
} else if (strchr(token, ':') != NULL) {
printf("gotheretoo\n");
Rule *parent = create_rule(rules, newTarget);
printf("gotheretooAFTER\n");
currParent = parent;
printf("gotheretooAFTER\n");
while (token != NULL) {
printf("gotheretoo\n");
create_dependancy(rules, parent, token);
token = strtok(NULL, " \n");
}
} else {
if (currParent == NULL) {
printf("error: action line leading file without respective target line");
exit(1);
}
Action *actionCurr = currParent->actions;
while (actionCurr != NULL) {
actionCurr = actionCurr->next_act;
}
Action *newAction = malloc(sizeof(Action));
newAction->next_act = NULL;
newAction->args[0] = newTarget;
int i = 0;
while (token != NULL) {
newAction->args[i] = token;
i += 1;
token = strtok(NULL, " ");
}
newAction->args[i] = NULL;
actionCurr = newAction;
}
}
}
return rules;
} | null | null |
63c648845f004bf4898cfd2e | 2023-03-14T20:42:27.291Z | Question from Code | v2 | i can't find the segmentation fault here, can you? | void create_dependancy(Rule* rules, Rule* parent, char* target) {
Dependency* curr = parent->dependencies;
int exists = 0;
while (curr != NULL && exists != 1) {
printf("%s\n", curr->rule->target);
if (strcmp(curr->rule->target, target) == 0) {
exists = 1;
}
curr = curr->next_dep;
}
if (exists != 1) {
printf("before malloc");
curr = malloc(sizeof(struct dep_node));
printf("after malloc");
curr->rule = create_rule(rules, target);
curr->next_dep = NULL;
}
} | null | null |
63c648845f004bf4898cfd2e | 2023-03-14T20:43:41.978Z | General Question | v2 | i got this error message, what does it mean?
pmake: malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
50 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory. | null | null | null |
63c648855f004bf4898cfd38 | 2023-02-09T19:29:40.226Z | Help Fix Code | v1 | null | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// 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.
struct pixel* rows[height];
for (int i = 0; i < height; i++){
rows[i] = (struct pixel*)malloc(width);
}
// Initialize actual struct values
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
rows[i][j] = (struct pixel*)malloc(3));
fread(rows[i][j]->blue, 1, 1, image);
fread(rows[i][j]->green, 1, 1, image);
fread(rows[i][j]->red, 1, 1, image);
}
}
return rows;
} | 1. First, allocate space for m `struct pixel` values, where m is the height of the image. Each pointer will eventually point to one row of pixel data.
2. For each pointer you just allocated, initialize it to point to heap-allocated space for an entire row of pixel data.
3. Use the given file and pixel_array_offset to initialize the actual struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is consistent with the bitmap file format.
4. Return the address of the first `struct pixel *` you initialized. | null |
63c648855f004bf4898cfd38 | 2023-02-09T19:42:04.384Z | Explain Code | v1 | null | void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) {
// Pixel arrary, byte offset 10-13
fseek(image, 10, SEEK_SET);
fread(pixel_array_offset, 1, 4, image);
// Width, byte offset 18-21
fseek(image, 18, SEEK_SET);
fread(width, 1, 4, image);
// Height, byte offset 22-25
fseek(image, 22, SEEK_SET);
fread(height, 1, 4, image);
} | null | null |
63c648855f004bf4898cfd38 | 2023-02-09T19:46:41.810Z | Explain Code | v1 | null | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// 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.
struct pixel* rows[height];
for (int i = 0; i < height; i++){
rows[i] = (struct pixel*)malloc(width);
}
// Initialize actual struct values
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
fread(rows[i][j]->blue, 1, 1, image);
fread(rows[i][j]->green, 1, 1, image);
fread(rows[i][j]->red, 1, 1, image);
}
}
return rows;
} | null | null |
63c648855f004bf4898cfd38 | 2023-02-09T20:14:27.108Z | Help Fix Code | v1 | null | struct pixel new_row;
fread(new_row->blue, 1, 1, image);
fread(new_row->green, 1, 1, image);
fread(new_row->red, 1, 1, image);
| Read bytes from file and save them to elements of the pixel struct new_row. | null |
63c648865f004bf4898cfd42 | 2023-01-26T15:41:12.854Z | Help Fix Code | v1 | null | int **double_pointer = malloc(sizeof(int) * 2) | allocate memory to a double pointer. | null |
63c648865f004bf4898cfd42 | 2023-01-26T15:43:03.681Z | Explain Code | v1 | null | int **double_pointer = malloc(sizeof(int*) * 2) | null | null |
63c648865f004bf4898cfd42 | 2023-02-10T05:52:09.811Z | General Question | v1 | how to use fseek
| null | null | null |
63c648865f004bf4898cfd42 | 2023-02-10T05:55:19.954Z | General Question | v1 | how to use fread | null | null | null |
63c648865f004bf4898cfd42 | 2023-02-12T03:27:46.879Z | General Question | v1 | How do i print the contents of a file, line by line to standard output | null | null | null |
63c648865f004bf4898cfd42 | 2023-02-12T04:34:40.111Z | Help Fix Code | v1 | null | /*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
// check to see if name fits into name array.
if (strlen(name) + 1 > MAX_NAME) {
return 2;
}
User *new_user = malloc(sizeof(struct user *));
if (new_user == NULL) {
return 3; // malloc failed.
}
strncpy(new_user->name, name, MAX_NAME -1);
// initailie all values of friends array to null. Usefull for make_friends
// function.
for (int i = 0; i < MAX_FRIENDS; i++) {
new_user->friends[i] = NULL;
}
// if linked list is empty
if (*user_ptr_add == NULL) {
*user_ptr_add = new_user;
return 0;
}
User *curr = *user_ptr_add;
// traverse through the linked list, checking if the name is already taken.
while (curr->next != NULL) {
if (strcmp(name, curr->name) == 0) {
free(new_user);
return 1;
}
curr = curr->next;
}
curr->next = new_user;
return 0;
} | Create a new user with the given name. Insert it at the tail of the list of users whose head is pointed to by *user_ptr_add.
Return: 0 if successful. - 1 if a user by this name already exists in this list. - 2 if the given name cannot fit in the 'name' array. | null |
63c648865f004bf4898cfd42 | 2023-02-12T09:39:35.415Z | Help Fix Code | v1 | null | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
// check to see if name fits into name array.
if (strlen(name) + 1 > MAX_NAME) {
return 2;
}
User *new_user = malloc(sizeof(struct user));
if (new_user == NULL) {
return 3; // malloc failed.
}
strncpy(new_user->name, name, MAX_NAME);
// initailie all values of friends array to null. Usefull for make_friends
// function.
for (int i = 0; i < MAX_FRIENDS; i++) {
new_user->friends[i] = NULL;
}
new_user->next = NULL;
new_user->first_post = NULL;
// if linked list is empty
if (*user_ptr_add == NULL) {
*user_ptr_add = new_user;
return 0;
}
User *curr = *user_ptr_add;
// traverse through the linked list, checking if the name is already taken.
while (curr->next != NULL) {
if (strcmp(name, curr->name) == 0) {
free(new_user);
return 1;
}
curr = curr->next;
}
curr->next = new_user;
return 0;
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *curr = (User *)head;
while (curr != NULL) {
if (strcmp(name, curr->name) == 0) {
return curr;
}
curr = curr->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
// simple traversel of the linked list.
printf("User List\n");
while (curr != NULL) {
printf("%s\n", curr->name);
curr = curr->next;
}
}
/*
* Change the filename for the profile pic of the given user.
*
* Return:
* - 0 on success.
* - 1 if the file does not exist or cannot be opened.
* - 2 if the filename is too long.
*/
int update_pic(User *user, const char *filename) {
// check if file exists.
FILE *f_ptr = fopen(filename, "r");
if (f_ptr == NULL) {
return 1;
}
fclose(f_ptr);
// check if filename can fit in the profile_pic array.
if (strlen(filename) + 1 > MAX_NAME) {
return 2;
}
strncpy(user->profile_pic, filename, MAX_NAME);
return 0;
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
User *user1 = find_user(name1, head);
User *user2 = find_user(name2, head);
if (user1 == NULL || user2 == NULL) {
return 4;
}
if (strcmp(name1, name2) == 0) {
return 3;
}
for (int i = 0; i < MAX_FRIENDS; i++) {
User *user1_friend = user1->friends[i];
if (user1_friend != NULL && user1_friend == user2) {
return 1;
}
}
int user1_reached_max = 1;
int user2_reached_max = 1;
for (int i = 0; i < MAX_FRIENDS; i++) {
User *user1_friend = user1->friends[i];
User *user2_friend = user2->friends[i];
if (user1_friend == NULL) {
user1_reached_max = 0;
}
if (user2_friend == NULL) {
user2_reached_max = 0;
}
}
if (user1_reached_max + user2_reached_max >= 1) {
return 2;
}
for (int i = 0; i < MAX_FRIENDS; i++) {
User *user1_friend = user1->friends[i];
if (user1_friend == NULL) {
user1->friends[i] = user2;
break;
}
}
for (int i = 0; i < MAX_FRIENDS; i++) {
User *user2_friend = user2->friends[i];
if (user2_friend == NULL) {
user2->friends[i] = user1;
break;
}
}
return 0;
}
/*
* Print a user profile.
* For an example of the required output format, see the example output
* linked from the handout.
* Return:
* - 0 on success.
* - 1 if the user is NULL.
*/
int print_user(const User *user) {
if (user == NULL) {
return 1;
}
FILE *f_ptr = fopen(user->profile_pic, "r");
char buffer[100];
if (f_ptr != NULL) {
while (fgets(buffer, 100, f_ptr) != NULL) {
printf("%s", buffer);
}
}
printf("\nName: %s\n", user->name);
printf("------------------------------------------\n");
printf("Friends:\n");
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] == NULL) {
break;
}
printf("%s\n", user->friends[i]->name);
}
printf("------------------------------------------\n");
printf("Posts:\n");
Post *head = user->first_post;
if (head == NULL) {
printf("------------------------------------------\n");
return 0;
}
while (head->next != NULL) {
printf("From: %s\n", head->author);
printf("date: %s\n", ctime(head->date));
printf("\n%s", head->contents);
printf("\n\n");
printf("===");
printf("\n\n");
head = head->next;
}
printf("From: %s\n", head->author);
printf("date: %s\n", ctime(head->date));
printf("\n%s\n", head->contents);
printf("------------------------------------------\n");
return 0;
}
/*
* Check if user1 and user2 are friends.
* Return:
* - 0 on success
* - 1 if users are not friends
*/
int check_friends(const User *user1, const User *user2) {
for (int i = 0; i < MAX_FRIENDS; i++) {
User *user1_friend = user1->friends[i];
if (user1_friend != NULL && user1_friend == user2) {
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) {
if (author == NULL || target == NULL) {
return 2;
}
if (check_friends(author, target) == 1) {
return 1;
}
Post *new_post = malloc(sizeof(Post));
if(new_post == NULL) {
return 3;
}
strncpy(new_post->author, author->name, MAX_NAME);
new_post->contents = contents;
time_t *t = malloc(sizeof(time_t));
if (t == NULL) {
return 3;
}
time(t);
new_post->date = t;
Post *head = target->first_post;
if (head == NULL) {
target->first_post = new_post;
new_post->next = NULL;
return 0;
}
Post *temp = head;
target->first_post = new_post;
new_post->next = temp;
return 0;
}
/*
* From the list pointed to by *user_ptr_del, delete the user
* with the given name.
* Remove the deleted user from any lists of friends.
*
* Return:
* - 0 on success.
* - 1 if a user with this name does not exist.
*/
int delete_user(const char *name, User **user_ptr_del) {
User *del_user = find_user(name, *user_ptr_del);
if (del_user == NULL) {
return 1;
}
// remove dangling pointers in the friends of the deleted user.
for (int i = 0; i < MAX_FRIENDS; i++) {
User *del_user_friend = del_user->friends[i];
for (int j = 0; j < MAX_FRIENDS; j++) {
if (del_user_friend != NULL && del_user_friend->friends[j] == del_user) {
del_user_friend->friends[j] = NULL;
break;
}
}
}
// free posts received by user.
while (del_user->first_post != NULL) {
free(del_user->first_post->contents);
free(del_user->first_post->date);
Post *next_tmp = del_user->first_post->next;
free(del_user->first_post);
del_user->first_post = next_tmp;
}
User* head = *user_ptr_del;
if (head == del_user) {
*user_ptr_del = del_user->next;
free(del_user);
return 0;
}
while (head->next != NULL) {
if (head->next == del_user) {
head->next = del_user->next;
free(del_user);
break;
}
head = head->next;
}
return 0;
}
| valgrind outputs this error:
```
==29619== Address 0x4a75700 is 32 bytes inside a block of size 160 alloc'd
==29619== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==29619== by 0x109AF8: create_user (friends.c:29)
==29619== by 0x109450: process_args (friendme.c:31)
==29619== by 0x109A04: main (friendme.c:166)
``` | null |
63c648865f004bf4898cfd42 | 2023-02-12T09:41:57.016Z | Help Fix Code | v1 | null | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
// check to see if name fits into name array.
if (strlen(name) + 1 > MAX_NAME) {
return 2;
}
User *new_user = malloc(sizeof(struct user));
if (new_user == NULL) {
return 3; // malloc failed.
}
strncpy(new_user->name, name, MAX_NAME);
// initailie all values of friends array to null. Usefull for make_friends
// function.
for (int i = 0; i < MAX_FRIENDS; i++) {
new_user->friends[i] = NULL;
}
new_user->next = NULL;
new_user->first_post = NULL;
// if linked list is empty
if (*user_ptr_add == NULL) {
*user_ptr_add = new_user;
return 0;
}
User *curr = *user_ptr_add;
// traverse through the linked list, checking if the name is already taken.
while (curr->next != NULL) {
if (strcmp(name, curr->name) == 0) {
free(new_user);
return 1;
}
curr = curr->next;
}
curr->next = new_user;
return 0;
}
| /*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
| null |
63c648865f004bf4898cfd42 | 2023-02-12T09:43:32.877Z | Help Fix Code | v1 | null | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
// check to see if name fits into name array.
if (strlen(name) + 1 > MAX_NAME) {
return 2;
}
User *new_user = malloc(sizeof(struct user));
if (new_user == NULL) {
return 3; // malloc failed.
}
strncpy(new_user->name, name, MAX_NAME);
// initailie all values of friends array to null. Usefull for make_friends
// function.
for (int i = 0; i < MAX_FRIENDS; i++) {
new_user->friends[i] = NULL;
}
new_user->next = NULL;
new_user->first_post = NULL;
// if linked list is empty
if (*user_ptr_add == NULL) {
*user_ptr_add = new_user;
return 0;
}
User *curr = *user_ptr_add;
// traverse through the linked list, checking if the name is already taken.
while (curr->next != NULL) {
if (strcmp(name, curr->name) == 0) {
free(new_user);
return 1;
}
curr = curr->next;
}
curr->next = new_user;
return 0;
}
| Create a new user with the given name. Insert it at the tail of the list of users whose head is pointed to by *user_ptr_add. Return: 0 if successful. - 1 if a user by this name already exists in this list - 2 if the given name cannot fit in the 'name' array (don't forget about the null terminator).
Valgrind says that there is an error.
==38140== Address 0x4a75700 is 32 bytes inside a block of size 160 alloc'd
==38140== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==38140== by 0x109AF8: create_user (friends.c:29)
==38140== by 0x109450: process_args (friendme.c:31)
==38140== by 0x109A04: main (friendme.c:166) | null |
63c648865f004bf4898cfd42 | 2023-02-12T09:45:02.543Z | General Question | v1 | what does this error mean in valgrind:
==38140== Address 0x4a75700 is 32 bytes inside a block of size 160 alloc'd
==38140== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==38140== by 0x109AF8: create_user (friends.c:29)
==38140== by 0x109450: process_args (friendme.c:31)
==38140== by 0x109A04: main (friendme.c:166) | null | null | null |
63c648865f004bf4898cfd42 | 2023-02-27T02:24:24.671Z | Explain Code | v1 | null | int **p = malloc(sizeof(int**));
int x = 10;
*p = &x; | null | null |
63c648865f004bf4898cfd42 | 2023-02-27T02:26:15.735Z | Question from Code | v1 | Why does this code work. Shouldn't the variable p be a pointer to a a pointer to a pointer to an iny? | int **p = malloc(sizeof(int**));
int x = 10;
*p = &x; | null | null |
63c648865f004bf4898cfd42 | 2023-02-27T02:26:59.698Z | General Question | v1 | Whats the difference between
int **p = malloc(sizeof(int**)); and int ***p = malloc(sizeof(int**));
| null | null | null |
63c648875f004bf4898cfd4c | 2023-01-20T01:43:20.638Z | Question from Code | v1 | will this get the value stored at the principal memory address and multiply it by rate? | void invest (&principal, double rate) {
return *principal * rate;
} | null | null |
63c648875f004bf4898cfd4c | 2023-01-20T01:44:22.558Z | Question from Code | v1 | do i need to specify the type on the return statement
| void invest (&principal, double rate) {
return *principal * rate;
} | null | null |
63c648875f004bf4898cfd4c | 2023-01-20T01:45:05.440Z | General Question | v1 | if the function type is void can it still return a value? | null | null | null |
63c648875f004bf4898cfd4c | 2023-01-20T02:03:13.148Z | Help Fix Code | v1 | null |
double invest (double *principal, double rate) {
double total = *principal * rate;
return total;
}
| dereference principal and return principal times rate | null |
63c648875f004bf4898cfd4c | 2023-01-20T02:14:23.524Z | Help Fix Code | v1 | null | int sum_card(int **score_card, int size) {
// TODO: write the body of sum_card according to its description.
int sum;
for (int i = 0; i < size; i++) {
sum = sum + **score_card[i];
}
return sum;
} | add all the integers in score_card and return the sum | null |
63c648875f004bf4898cfd4c | 2023-01-20T18:48:57.065Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
char phone[11];
int i;
scanf("%10s %d", phone, &i);
if (i == -1) {
printf("%s\n", phone);
return 0;
} else if (i >= 0 && i <= 9) {
printf("%c\n", str[i]);
return 0;
} else {
printf("ERROR\n");
return 1;
}
}
| output the digit at index i
| null |
63c648875f004bf4898cfd4c | 2023-01-20T18:50:06.653Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
char phone[11];
int i;
scanf("%10s %d", phone, &i);
if (i == -1) {
printf("%s\n", phone);
return 0;
} else if (i >= 0 && i <= 9) {
printf("%c\n", phone[i]);
return 0;
} else {
printf("ERROR\n");
return 1;
}
}
| output the digit at index i | null |
63c648875f004bf4898cfd4c | 2023-01-20T22:58:14.210Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
int main() {
char phone[11];
int i;
scanf("%10s", phone);
while (scanf("%d", &i) != EOF) {
if (i == -1){
printf("%s\n", phone);
return 0;
}
if (i < -1 ){
printf("ERROR\n");
return 1;
}
if (i > 9 ){
printf("ERROR\n");
return 1;
}
else{
printf("%c\n", phone[i]);
return 0;
}
}
return 0;
} | allow users to keep entering ints in a new line and displays the output | null |
63c648875f004bf4898cfd4c | 2023-01-27T18:45:10.446Z | Help Fix Code | v1 | null | void fib(int **fib_sequence, int n){
*fib_sequence = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n;i++){
if (i==0){
*fib_sequence[0] = 0;
}
else if (i==1){
*fib_sequence[1] = 1;
}
else{
*fib_sequence[i] = *fib_sequence[i-2] + *fib_sequence[i-1];
}
}
}
int main(int argc, char **argv) {
/* do not change this main function */
int count = strtol(argv[1], NULL, 10);
int *fib_sequence;
fib(&fib_sequence, count);
for (int i = 0; i < count; i++) {
printf("%d ", fib_sequence[i]);
}
free(fib_sequence);
return 0;
}
| output the first n number of fib sequence. | null |
63c648875f004bf4898cfd4c | 2023-01-27T21:39:04.818Z | Help Write Code | v1 | null | null | null | /* 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) {
} |
63c648875f004bf4898cfd4c | 2023-01-30T04:45:10.389Z | Explain Code | v1 | null | reader = popen("ls -l", "r");
while (fgets(current_file, 1024, reader) != NULL){ | null | null |
63c648875f004bf4898cfd4c | 2023-01-30T04:47:13.217Z | Question from Code | v1 | how can i take the input and split it into 2 variables curr_size and curr_perms
| reader = popen("ls -l", "r");
while (fgets(current_file, 1024, reader) != NULL){ | null | null |
63c648875f004bf4898cfd4c | 2023-01-30T17:12:28.027Z | 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 *file, char *permissions){
for (int i = 0; i < 9;i++){
// if the permission is blank then continue
if(strcmp(permissions[i], "-") == 0){
continue;
}
// else if the permission matches, continue
else if (strcmp(permissions[i], file[i]) == 0){
continue;
}
// otherwise the permission does not match and we can end the loop and set is_good to 1 and return
else{
return 1;
break;
}
return 0;
}
}
int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
// TODO: Process command line arguments.
printf("Hello, World!\n");
int inputted_size;
printf("Hello, inputted_size added!\n");
char inputted_perms[10];
scanf("%d %s", &inputted_size, inputted_perms)
int num_files_valid = 0;
FILE *file_output;
char line[1024];
int size;
char perms[11];
if ((file_output = popen("ls -lS", "r")) == NULL) {
perror("popen");
exit(EXIT_FAILURE);
}
// TODO: Call check_permissions as part of your solution to count the files to
// compute and print the correct value.
while(fgets(line, 1024, file_output) != NULL){
sscanf(line, "%s %*d %*s %*s %d", perms, &size);
if (size <= inputted_size){
break;
}
else{
if(check_permissions(perms, inputted_perms) == 0){
num_files_valid ++;
}
else{
continue;
}
}
}
printf("%d\n", num_files_valid);
return 0;
}
| the code should input the number of files larger than specified size
| null |
63c648875f004bf4898cfd4c | 2023-03-15T18:12:08.361Z | Help Fix Code | v2 | Evaluate the rule in rules corresponding to the given target.
If target is NULL, evaluate the first rule instead.
If pflag is 0, evaluate each dependency in sequence.
If pflag is 1, then evaluate each dependency in parallel (by creating one
new process per dependency). In this case, the parent process will wait until
all child processes have terminated before checking dependency modified times
to decide whether to execute the actions. |
void update_dependency(Rule *dep_rule, Rule *rules, int pflag);
int execute_actions(Action *actions) {
pid_t pid;
int status;
while (actions != NULL) {
printf("%s\n", args_to_string(actions->args, (char[MAXLINE]){0}, MAXLINE));
if ((pid = fork()) == 0) {
execvp(actions->args[0], actions->args);
perror("execvp");
exit(1);
}
if (wait(&status) < 0) {
perror("wait");
exit(1);
}
if (WEXITSTATUS(status) != 0) {
return -1;
}
actions = actions->next_act;
}
return 0;
}
void run_make(char *target, Rule *rules, int pflag) {
Rule *rule_to_execute = rules;
if (target != NULL) {
while (rule_to_execute != NULL && strcmp(rule_to_execute->target, target) != 0) {
rule_to_execute = rule_to_execute->next_rule;
}
if (rule_to_execute == NULL) {
fprintf(stderr, "No rule found for target: %s\n", target);
exit(1);
}
}
update_dependency(rule_to_execute, rules, pflag);
}
void update_dependency(Rule *dep_rule, Rule *rules, int pflag) {
Dependency *dep = dep_rule->dependencies;
int need_to_execute = 0;
struct stat target_stat, dep_stat;
if (stat(dep_rule->target, &target_stat) < 0) {
need_to_execute = 1;
}
while (dep != NULL) {
Rule *dependency_rule = dep->rule;
update_dependency(dependency_rule, rules, pflag);
if (stat(dependency_rule->target, &dep_stat) < 0) {
perror("stat");
exit(1);
}
if (!need_to_execute && (dep_stat.st_mtimespec.tv_sec > target_stat.st_mtimespec.tv_sec ||
(dep_stat.st_mtimespec.tv_sec == target_stat.st_mtimespec.tv_sec &&
dep_stat.st_mtimespec.tv_nsec > target_stat.st_mtimespec.tv_nsec))) {
need_to_execute = 1;
}
dep = dep->next_dep;
}
if (need_to_execute) {
if (execute_actions(dep_rule->actions) != 0) {
fprintf(stderr, "Failed to execute actions for target: %s\n", dep_rule->target);
exit(1);
}
}
}
| null | null |
63c648875f004bf4898cfd4c | 2023-03-15T18:39:55.013Z | Help Fix Code | v2 | for some reason it does not work when there are dependancies | void update_dependency(Rule *dep_rule, Rule *rules, int pflag);
int execute_actions(Action *actions) {
pid_t pid;
int status;
while (actions != NULL) {
printf("%s\n", args_to_string(actions->args, (char[MAXLINE]){0}, MAXLINE));
if ((pid = fork()) == 0) {
execvp(actions->args[0], actions->args);
perror("execvp");
exit(1);
}
if (wait(&status) < 0) {
perror("wait");
exit(1);
}
if (WEXITSTATUS(status) != 0) {
return -1;
}
actions = actions->next_act;
}
return 0;
}
void run_make(char *target, Rule *rules, int pflag) {
Rule *rule_to_execute = rules;
if (target != NULL) {
while (rule_to_execute != NULL && strcmp(rule_to_execute->target, target) != 0) {
rule_to_execute = rule_to_execute->next_rule;
}
if (rule_to_execute == NULL) {
fprintf(stderr, "No rule found for target: %s\n", target);
exit(1);
}
}
update_dependency(rule_to_execute, rules, pflag);
}
void update_dependency(Rule *dep_rule, Rule *rules, int pflag) {
Dependency *dep = dep_rule->dependencies;
int need_to_execute = 0;
struct stat target_stat, dep_stat;
if (stat(dep_rule->target, &target_stat) < 0) {
need_to_execute = 1;
}
while (dep != NULL) {
Rule *dependency_rule = dep->rule;
update_dependency(dependency_rule, rules, pflag);
if (stat(dependency_rule->target, &dep_stat) < 0) {
perror("stat");
exit(1);
}
if (!need_to_execute && (dep_stat.st_mtimespec.tv_sec > target_stat.st_mtimespec.tv_sec ||
(dep_stat.st_mtimespec.tv_sec == target_stat.st_mtimespec.tv_sec &&
dep_stat.st_mtimespec.tv_nsec > target_stat.st_mtimespec.tv_nsec))) {
need_to_execute = 1;
}
dep = dep->next_dep;
}
if (need_to_execute) {
if (execute_actions(dep_rule->actions) != 0) {
fprintf(stderr, "Failed to execute actions for target: %s\n", dep_rule->target);
exit(1);
}
}
} | null | null |
63c648875f004bf4898cfd4c | 2023-03-15T18:45:21.929Z | Help Fix Code | v2 | for some reason it does not work when there are dependancies and only works on calls where there are no dependancies involved
| void update_dependency(Rule *dep_rule, Rule *rules, int pflag);
int execute_actions(Action *actions) {
pid_t pid;
int status;
while (actions != NULL) {
printf("%s\n", args_to_string(actions->args, (char[MAXLINE]){0}, MAXLINE));
if ((pid = fork()) == 0) {
execvp(actions->args[0], actions->args);
perror("execvp");
exit(1);
}
if (wait(&status) < 0) {
perror("wait");
exit(1);
}
if (WEXITSTATUS(status) != 0) {
return -1;
}
actions = actions->next_act;
}
return 0;
}
void run_make(char *target, Rule *rules, int pflag) {
Rule *rule_to_execute = rules;
if (target != NULL) {
while (rule_to_execute != NULL && strcmp(rule_to_execute->target, target) != 0) {
rule_to_execute = rule_to_execute->next_rule;
}
if (rule_to_execute == NULL) {
fprintf(stderr, "No rule found for target: %s\n", target);
exit(1);
}
}
update_dependency(rule_to_execute, rules, pflag);
}
void update_dependency(Rule *dep_rule, Rule *rules, int pflag) {
Dependency *dep = dep_rule->dependencies;
int need_to_execute = 0;
struct stat target_stat, dep_stat;
if (stat(dep_rule->target, &target_stat) < 0) {
need_to_execute = 1;
}
while (dep != NULL) {
Rule *dependency_rule = dep->rule;
update_dependency(dependency_rule, rules, pflag);
if (stat(dependency_rule->target, &dep_stat) < 0) {
perror("stat");
exit(1);
}
if (!need_to_execute && (dep_stat.st_mtimespec.tv_sec > target_stat.st_mtimespec.tv_sec ||
(dep_stat.st_mtimespec.tv_sec == target_stat.st_mtimespec.tv_sec &&
dep_stat.st_mtimespec.tv_nsec > target_stat.st_mtimespec.tv_nsec))) {
need_to_execute = 1;
}
dep = dep->next_dep;
}
if (need_to_execute) {
if (execute_actions(dep_rule->actions) != 0) {
fprintf(stderr, "Failed to execute actions for target: %s\n", dep_rule->target);
exit(1);
}
}
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T03:23:00.895Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
int check_permissions(char *filter, char *permission){
for(int i = 0; i<9;i++){
if (filter[i] != '-'){
if(filter[i] != permission[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;
}
char line[101];
char permission[9];
int size;
int num = 0;
int min = strtol(argv[1], NULL, 10);
fgets(line, 101, stdin);
while(fscanf(stdin, "%s %*d %*s %*s %d %*[^\n]", permission, &size) == 2){
if((size > min) && (permission[0] != 'd')){
if(argc == 3){
if(check_permissions(argv[2], permission + 1) == 0){
num ++;
}
}else{
num ++;
}
}
};
printf("%d\n", num);
return 0;
}
| 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.
| null |
63c648885f004bf4898cfd56 | 2023-02-11T03:26:20.076Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
int check_permissions(char *filter, char *permission){
for(int i = 0; i<9;i++){
if (filter[i] != '-'){
if(filter[i] != permission[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;
}
char line[101];
char permission[9];
int size;
int num = 0;
int min = strtol(argv[1], NULL, 10);
fgets(line, 101, stdin);
while(fscanf(stdin, "%s %*d %*s %*s %d %*[^\n]", permission, &size) == 2){
if((size > min) && (permission[0] != 'd')){
if(argc == 3){
if(check_permissions(argv[2], permission + 1) == 0){
num ++;
}
}else{
num ++;
}
}
};
printf("%d\n", num);
return 0;
}
| For this assignment only, 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.
In general these are not valid assumptions - either in the real world or in later CSC209 assignments - but making them puts the focus of this assignment on basic C syntax and use of arrays, rather than command line parsing.
Command-line arguments and return codes
Assumption 1 above is particularly unrealistic for real C programs. We would normally want to have many different options and would design our programs to behave like other C tools where options can be specified in any order and grouped together. In the future, you will learn about the getopt library that makes processing command-line arguments easy. But for now, you are not expected to use getopt. Feel free to try it on your own, but for this assignment, you are not expected to use it.
For this program, the user might still forget command-line arguments altogether. When this happens, your program shouldn't crash. Instead you should print to standard error the message "USAGE: count_large size [permissions]" (followed by a single newline \n character), and return from main with return code 1. The starter code actually implements this behaviour for you already. Following the standard C conventions, main should return 0 when the program runs successfully. | null |
63c648885f004bf4898cfd56 | 2023-02-11T04:51:44.997Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
int check_permissions(char *filter, char *permission){
for(int i = 0; i<9;i++){
if (filter[i] != '-'){
if(filter[i] != permission[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;
}
char line[101];
char permission[9];
int size;
int num = 0;
int min = strtol(argv[1], NULL, 10);
fgets(line, 101, stdin);
while(fscanf(stdin, "%s %*d %*s %*s %d %*[^\n]", permission, &size) == 2){
if((size > min) && (permission[0] != 'd')){
if(argc == 3){
if(check_permissions(argv[2], permission + 1) == 0){
num ++;
}
}else{
num ++;
}
}
};
printf("%d\n", num);
return 0;
}
| 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. | null |
63c648885f004bf4898cfd56 | 2023-02-11T04:58:54.090Z | Help Fix Code | v1 | null | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if(strlen(name) >= MAX_NAME){return 2;}
else{
User *new_user = malloc(sizeof(User));
strcpy(new_user->name, name);
new_user->profile_pic[0] = '\0';
for(int i = 0;i<MAX_FRIENDS;i++){
new_user->friends[i] = NULL;
}
new_user->first_post = NULL;
new_user->next = NULL;
User *prev = NULL;
User *user = *user_ptr_add;
while(user != NULL){
if(strcmp(name, user->name) == 0){return 1;}
prev = user;
user = user-> next;
}
if(prev == NULL){*user_ptr_add = new_user;}
else{prev->next = new_user;}
return 0;
}
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *user = (User *) head;
while(user != NULL){
if(strcmp(name, user->name) == 0){return user;}
user = 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) {
User *user = (User *) curr;
fprintf(stdout, "User List\n");
while(user != NULL){
fprintf(stdout, " %s\n", user->name);
user = user-> next;
}
}
/*
* Change the filename for the profile pic of the given user.
*
* Return:
* - 0 on success.
* - 1 if the file does not exist or cannot be opened.
* - 2 if the filename is too long.
*/
int update_pic(User *user, const char *filename) {
if(strlen(filename) >= MAX_NAME){return 2;}
FILE * f = fopen(filename, "r");
if(f == NULL){return 1;}
fclose(f);
strcpy(user->profile_pic, filename);
return 0;
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
User *usr1 = find_user(name1, head);
User *usr2 = find_user(name2, head);
if(strcmp(name1, name2)==0){return 3;}
if(usr1 == NULL || usr2 == NULL){return 4;}
int i = 0;
while((usr1->friends)[i] != NULL && i != MAX_FRIENDS){
if(strcmp((usr1->friends)[i]->name, name2) == 0){
return 1;
}
i ++;
}
int j = 0;
while((usr2->friends)[j] != NULL && j != MAX_FRIENDS){
if(strcmp((usr2->friends)[j]->name, name1) ==0){
return 1;
}
j ++;
}
if(i==MAX_FRIENDS || j==MAX_FRIENDS){return 2;}
(usr1->friends)[i] = usr2;
(usr2->friends)[j] = usr1;
return 0;
}
/*
* Print a user profile.
* For an example of the required output format, see the example output
* linked from the handout.
* Return:
* - 0 on success.
* - 1 if the user is NULL.
*/
int print_user(const User *user) {
if(user == NULL){return 1;}
else{
if(user->profile_pic[0] == '\0'){
FILE * f = fopen(user->profile_pic, "r");
if(f!=NULL){
char line[101];
while(fgets(line, 101, f)!= NULL){
fprintf(stdout, "%s", line);
}
fprintf(stdout, "\n");
fclose(f);
}
}
fprintf(stdout, "Name: %s\n", user->name);
fprintf(stdout, "------------------------------------------\n");
fprintf(stdout, "Friends:\n");
int i = 0;
while((user->friends)[i] != NULL && i != MAX_FRIENDS){
fprintf(stdout, "%s\n", (user->friends)[i]->name);
i++;
}
fprintf(stdout, "------------------------------------------\n");
fprintf(stdout, "Posts:\n");
Post *post;
post = user->first_post;
while(post != NULL){
fprintf(stdout, "From: %s\n", post->author);
fprintf(stdout, "Date: %s", ctime(post->date));
fprintf(stdout,"\n");
fprintf(stdout,"%s\n", post->contents);
if(post->next != NULL){
fprintf(stdout,"\n");
fprintf(stdout,"===\n");
fprintf(stdout,"\n");
}
post = post->next;
}
fprintf(stdout, "------------------------------------------\n");
return 0;
}
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if(author == NULL || target == NULL){
return 2;
free(contents);
}
int i =0;
while((author->friends)[i] != NULL && i != MAX_FRIENDS){
if((author->friends)[i] == target){
Post *p = malloc(sizeof(Post));
strcpy(p->author, author->name);
p->contents = contents;
p->date = malloc(sizeof(time_t));
time(p->date);
p->next = target->first_post;
target->first_post = p;
return 0;
}
i++;
}
free(contents);
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 *prev = NULL;
User *user = *user_ptr_del;
if(user == NULL){return 0;}
while(user != NULL){
if(strcmp(name, user->name) == 0){
int i = 0;
User *friend = user->friends[0];
while(friend != NULL && i < MAX_FRIENDS){
int j = 0;
int indx = 0;
while(friend->friends[j]!= NULL && j < MAX_FRIENDS){
if(strcmp(friend->friends[j]->name, user->name)==0){
indx = j;
}
j++;
}
if(j!=0){
friend->friends[indx] = friend->friends[j-1];
if(friend->friends[j-1]!= NULL){
friend->friends[j-1] = NULL;
}
}
i++;
friend = user->friends[i];
}
Post* p = user->first_post;
Post* del = NULL;
while(p != NULL){
free(p->contents);
free(p->date);
del = p;
p = p->next;
free(del);
}
user->first_post = NULL;
if(prev == NULL){
*user_ptr_del = user->next;
fprintf(stdout, "DELETE HEAD%s\n", user->name);
}
else{
prev->next = user->next;
fprintf(stdout, "DELETE some%s\n", user->name);
}
free(user);
return 0;
}
prev = user;
user = user -> next;
}
return 1;
}
| create_user /*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
find_user Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
list_users Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
update_pic 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.
*/
make_friends 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.
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.
*
*/
print_user 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.
make_post Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
delete_user 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.
*/a
| null |
63c648885f004bf4898cfd56 | 2023-02-11T05:13:56.113Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
if(strlen(name) >= MAX_NAME){return 2;}
else{
User *new_user = malloc(sizeof(User));
strcpy(new_user->name, name);
new_user->profile_pic[0] = '\0';
for(int i = 0;i<MAX_FRIENDS;i++){
new_user->friends[i] = NULL;
}
new_user->first_post = NULL;
new_user->next = NULL;
User *prev = NULL;
User *user = *user_ptr_add;
while(user != NULL){
if(strcmp(name, user->name) == 0){return 1;}
prev = user;
user = user-> next;
}
if(prev == NULL){*user_ptr_add = new_user;}
else{prev->next = new_user;}
return 0;
}
} | /*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful.
* - 1 if a user by this name already exists in this list.
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/ | null |
63c648885f004bf4898cfd56 | 2023-02-11T05:17:50.079Z | Explain Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
if(strlen(name) >= MAX_NAME){return 2;}
else{
User *new_user = malloc(sizeof(User));
strcpy(new_user->name, name);
new_user->profile_pic[0] = '\0';
for(int i = 0;i<MAX_FRIENDS;i++){
new_user->friends[i] = NULL;
}
new_user->first_post = NULL;
new_user->next = NULL;
User *prev = NULL;
User *user = *user_ptr_add;
while(user != NULL){
if(strcmp(name, user->name) == 0){return 1;}
prev = user;
user = user-> next;
}
if(prev == NULL){*user_ptr_add = new_user;}
else{prev->next = new_user;}
return 0;
}
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T05:20:51.049Z | Explain Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
User *prev = NULL;
User *user = *user_ptr_del;
if(user == NULL){return 0;}
while(user != NULL){
if(strcmp(name, user->name) == 0){
int i = 0;
User *friend = user->friends[0];
while(friend != NULL && i < MAX_FRIENDS){
int j = 0;
int indx = 0;
while(friend->friends[j]!= NULL && j < MAX_FRIENDS){
if(strcmp(friend->friends[j]->name, user->name)==0){
indx = j;
}
j++;
}
if(j!=0){
friend->friends[indx] = friend->friends[j-1];
if(friend->friends[j-1]!= NULL){
friend->friends[j-1] = NULL;
}
}
i++;
friend = user->friends[i];
}
Post* p = user->first_post;
Post* del = NULL;
while(p != NULL){
free(p->contents);
free(p->date);
del = p;
p = p->next;
free(del);
}
user->first_post = NULL;
if(prev == NULL){
*user_ptr_del = user->next;
fprintf(stdout, "DELETE HEAD%s\n", user->name);
}
else{
prev->next = user->next;
fprintf(stdout, "DELETE some%s\n", user->name);
}
free(user);
return 0;
}
prev = user;
user = user -> next;
}
return 1;
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T05:21:47.742Z | Explain Code | v1 | null | int make_post(const User *author, User *target, char *contents) {
if(author == NULL || target == NULL){
return 2;
free(contents);
}
int i =0;
while((author->friends)[i] != NULL && i != MAX_FRIENDS){
if((author->friends)[i] == target){
Post *p = malloc(sizeof(Post));
strcpy(p->author, author->name);
p->contents = contents;
p->date = malloc(sizeof(time_t));
time(p->date);
p->next = target->first_post;
target->first_post = p;
return 0;
}
i++;
}
free(contents);
return 1;
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T05:23:47.273Z | Explain Code | v1 | null | int print_user(const User *user) {
if(user == NULL){return 1;}
else{
if(user->profile_pic[0] == '\0'){
FILE * f = fopen(user->profile_pic, "r");
if(f!=NULL){
char line[101];
while(fgets(line, 101, f)!= NULL){
fprintf(stdout, "%s", line);
}
fprintf(stdout, "\n");
fclose(f);
}
}
fprintf(stdout, "Name: %s\n", user->name);
fprintf(stdout, "------------------------------------------\n");
fprintf(stdout, "Friends:\n");
int i = 0;
while((user->friends)[i] != NULL && i != MAX_FRIENDS){
fprintf(stdout, "%s\n", (user->friends)[i]->name);
i++;
}
fprintf(stdout, "------------------------------------------\n");
fprintf(stdout, "Posts:\n");
Post *post;
post = user->first_post;
while(post != NULL){
fprintf(stdout, "From: %s\n", post->author);
fprintf(stdout, "Date: %s", ctime(post->date));
fprintf(stdout,"\n");
fprintf(stdout,"%s\n", post->contents);
if(post->next != NULL){
fprintf(stdout,"\n");
fprintf(stdout,"===\n");
fprintf(stdout,"\n");
}
post = post->next;
}
fprintf(stdout, "------------------------------------------\n");
return 0;
}
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T05:26:00.273Z | Explain Code | v1 | null | int make_friends(const char *name1, const char *name2, User *head) {
User *usr1 = find_user(name1, head);
User *usr2 = find_user(name2, head);
if(strcmp(name1, name2)==0){return 3;}
if(usr1 == NULL || usr2 == NULL){return 4;}
int i = 0;
while((usr1->friends)[i] != NULL && i != MAX_FRIENDS){
if(strcmp((usr1->friends)[i]->name, name2) == 0){
return 1;
}
i ++;
}
int j = 0;
while((usr2->friends)[j] != NULL && j != MAX_FRIENDS){
if(strcmp((usr2->friends)[j]->name, name1) ==0){
return 1;
}
j ++;
}
if(i==MAX_FRIENDS || j==MAX_FRIENDS){return 2;}
(usr1->friends)[i] = usr2;
(usr2->friends)[j] = usr1;
return 0;
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T05:28:59.511Z | Explain Code | v1 | null | int update_pic(User *user, const char *filename) {
if(strlen(filename) >= MAX_NAME){return 2;}
FILE * f = fopen(filename, "r");
if(f == NULL){return 1;}
fclose(f);
strcpy(user->profile_pic, filename);
return 0;
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T05:31:22.106Z | Explain Code | v1 | null | User *find_user(const char *name, const User *head) {
User *user = (User *) head;
while(user != NULL){
if(strcmp(name, user->name) == 0){return user;}
user = user-> next;
}
return NULL;
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T05:32:56.146Z | Explain Code | v1 | null | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if(strlen(name) >= MAX_NAME){return 2;}
else{
User *new_user = malloc(sizeof(User));
strcpy(new_user->name, name);
new_user->profile_pic[0] = '\0';
for(int i = 0;i<MAX_FRIENDS;i++){
new_user->friends[i] = NULL;
}
new_user->first_post = NULL;
new_user->next = NULL;
User *prev = NULL;
User *user = *user_ptr_add;
while(user != NULL){
if(strcmp(name, user->name) == 0){return 1;}
prev = user;
user = user-> next;
}
if(prev == NULL){*user_ptr_add = new_user;}
else{prev->next = new_user;}
return 0;
}
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *user = (User *) head;
while(user != NULL){
if(strcmp(name, user->name) == 0){return user;}
user = 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) {
User *user = (User *) curr;
fprintf(stdout, "User List\n");
while(user != NULL){
fprintf(stdout, " %s\n", user->name);
user = user-> next;
}
}
/*
* Change the filename for the profile pic of the given user.
*
* Return:
* - 0 on success.
* - 1 if the file does not exist or cannot be opened.
* - 2 if the filename is too long.
*/
int update_pic(User *user, const char *filename) {
if(strlen(filename) >= MAX_NAME){return 2;}
FILE * f = fopen(filename, "r");
if(f == NULL){return 1;}
fclose(f);
strcpy(user->profile_pic, filename);
return 0;
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
User *usr1 = find_user(name1, head);
User *usr2 = find_user(name2, head);
if(strcmp(name1, name2)==0){return 3;}
if(usr1 == NULL || usr2 == NULL){return 4;}
int i = 0;
while((usr1->friends)[i] != NULL && i != MAX_FRIENDS){
if(strcmp((usr1->friends)[i]->name, name2) == 0){
return 1;
}
i ++;
}
int j = 0;
while((usr2->friends)[j] != NULL && j != MAX_FRIENDS){
if(strcmp((usr2->friends)[j]->name, name1) ==0){
return 1;
}
j ++;
}
if(i==MAX_FRIENDS || j==MAX_FRIENDS){return 2;}
(usr1->friends)[i] = usr2;
(usr2->friends)[j] = usr1;
return 0;
}
/*
* Print a user profile.
* For an example of the required output format, see the example output
* linked from the handout.
* Return:
* - 0 on success.
* - 1 if the user is NULL.
*/
int print_user(const User *user) {
if(user == NULL){return 1;}
else{
if(user->profile_pic[0] == '\0'){
FILE * f = fopen(user->profile_pic, "r");
if(f!=NULL){
char line[101];
while(fgets(line, 101, f)!= NULL){
fprintf(stdout, "%s", line);
}
fprintf(stdout, "\n");
fclose(f);
}
}
fprintf(stdout, "Name: %s\n", user->name);
fprintf(stdout, "------------------------------------------\n");
fprintf(stdout, "Friends:\n");
int i = 0;
while((user->friends)[i] != NULL && i != MAX_FRIENDS){
fprintf(stdout, "%s\n", (user->friends)[i]->name);
i++;
}
fprintf(stdout, "------------------------------------------\n");
fprintf(stdout, "Posts:\n");
Post *post;
post = user->first_post;
while(post != NULL){
fprintf(stdout, "From: %s\n", post->author);
fprintf(stdout, "Date: %s", ctime(post->date));
fprintf(stdout,"\n");
fprintf(stdout,"%s\n", post->contents);
if(post->next != NULL){
fprintf(stdout,"\n");
fprintf(stdout,"===\n");
fprintf(stdout,"\n");
}
post = post->next;
}
fprintf(stdout, "------------------------------------------\n");
return 0;
}
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if(author == NULL || target == NULL){
return 2;
free(contents);
}
int i =0;
while((author->friends)[i] != NULL && i != MAX_FRIENDS){
if((author->friends)[i] == target){
Post *p = malloc(sizeof(Post));
strcpy(p->author, author->name);
p->contents = contents;
p->date = malloc(sizeof(time_t));
time(p->date);
p->next = target->first_post;
target->first_post = p;
return 0;
}
i++;
}
free(contents);
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 *prev = NULL;
User *user = *user_ptr_del;
if(user == NULL){return 0;}
while(user != NULL){
if(strcmp(name, user->name) == 0){
int i = 0;
User *friend = user->friends[0];
while(friend != NULL && i < MAX_FRIENDS){
int j = 0;
int indx = 0;
while(friend->friends[j]!= NULL && j < MAX_FRIENDS){
if(strcmp(friend->friends[j]->name, user->name)==0){
indx = j;
}
j++;
}
if(j!=0){
friend->friends[indx] = friend->friends[j-1];
if(friend->friends[j-1]!= NULL){
friend->friends[j-1] = NULL;
}
}
i++;
friend = user->friends[i];
}
Post* p = user->first_post;
Post* del = NULL;
while(p != NULL){
free(p->contents);
free(p->date);
del = p;
p = p->next;
free(del);
}
user->first_post = NULL;
if(prev == NULL){
*user_ptr_del = user->next;
fprintf(stdout, "DELETE HEAD%s\n", user->name);
}
else{
prev->next = user->next;
fprintf(stdout, "DELETE some%s\n", user->name);
}
free(user);
return 0;
}
prev = user;
user = user -> next;
}
return 1;
}
| null | null |
63c648885f004bf4898cfd56 | 2023-02-11T20:36:00.859Z | Question from Code | v1 | will this code generate any errors? | int create_user(const char *name, User **user_ptr_add) {
if(strlen(name) >= MAX_NAME){return 2;}
else{
User *new_user = malloc(sizeof(User));
strcpy(new_user->name, name);
new_user->profile_pic[0] = '\0';
for(int i = 0;i<MAX_FRIENDS;i++){
new_user->friends[i] = NULL;
}
new_user->first_post = NULL;
new_user->next = NULL;
User *prev = NULL;
User *user = *user_ptr_add;
while(user != NULL){
if(strcmp(name, user->name) == 0){return 1;}
prev = user;
user = user-> next;
}
if(prev == NULL){*user_ptr_add = new_user;}
else{prev->next = new_user;}
return 0;
}
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T20:37:09.827Z | Question from Code | v1 | any problems with the code? | User *find_user(const char *name, const User *head) {
User *user = (User *) head;
while(user != NULL){
if(strcmp(name, user->name) == 0){return user;}
user = user-> next;
}
return NULL;
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T20:38:24.977Z | Question from Code | v1 | any problems? | void list_users(const User *curr) {
User *user = (User *) curr;
fprintf(stdout, "User List\n");
while(user != NULL){
fprintf(stdout, " %s\n", user->name);
user = user-> next;
}
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T20:45:35.866Z | Question from Code | v1 | any problem with the code? | int update_pic(User *user, const char *filename) {
if(strlen(filename) >= MAX_NAME){return 2;}
FILE * f = fopen(filename, "r");
if(f == NULL){return 1;}
fclose(f);
strcpy(user->profile_pic, filename);
return 0;
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T20:46:32.163Z | Question from Code | v1 | any possible errors? | int make_friends(const char *name1, const char *name2, User *head) {
User *usr1 = find_user(name1, head);
User *usr2 = find_user(name2, head);
if(strcmp(name1, name2)==0){return 3;}
if(usr1 == NULL || usr2 == NULL){return 4;}
int i = 0;
while((usr1->friends)[i] != NULL && i != MAX_FRIENDS){
if(strcmp((usr1->friends)[i]->name, name2) == 0){
return 1;
}
i ++;
}
int j = 0;
while((usr2->friends)[j] != NULL && j != MAX_FRIENDS){
if(strcmp((usr2->friends)[j]->name, name1) ==0){
return 1;
}
j ++;
}
if(i==MAX_FRIENDS || j==MAX_FRIENDS){return 2;}
(usr1->friends)[i] = usr2;
(usr2->friends)[j] = usr1;
return 0;
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T20:48:46.530Z | Question from Code | v1 | the code should work as explaied in the doc:
* 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.
*/
will it fail? | int make_friends(const char *name1, const char *name2, User *head) {
User *usr1 = find_user(name1, head);
User *usr2 = find_user(name2, head);
if(strcmp(name1, name2)==0){return 3;}
if(usr1 == NULL || usr2 == NULL){return 4;}
int i = 0;
while((usr1->friends)[i] != NULL && i != MAX_FRIENDS){
if(strcmp((usr1->friends)[i]->name, name2) == 0){
return 1;
}
i ++;
}
int j = 0;
while((usr2->friends)[j] != NULL && j != MAX_FRIENDS){
if(strcmp((usr2->friends)[j]->name, name1) ==0){
return 1;
}
j ++;
}
if(i==MAX_FRIENDS || j==MAX_FRIENDS){return 2;}
(usr1->friends)[i] = usr2;
(usr2->friends)[j] = usr1;
return 0;
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T20:51:27.341Z | Question from Code | v1 | the code should work as explaied in the doc: * 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. */
Will this code work as expected? will there be any errors?
| int make_friends(const char *name1, const char *name2, User *head) {
User *usr1 = find_user(name1, head);
User *usr2 = find_user(name2, head);
if(strcmp(name1, name2)==0){return 3;}
if(usr1 == NULL || usr2 == NULL){return 4;}
int i = 0;
while((usr1->friends)[i] != NULL && i != MAX_FRIENDS){
if(strcmp((usr1->friends)[i]->name, name2) == 0){
return 1;
}
i ++;
}
int j = 0;
while((usr2->friends)[j] != NULL && j != MAX_FRIENDS){
if(strcmp((usr2->friends)[j]->name, name1) ==0){
return 1;
}
j ++;
}
if(i==MAX_FRIENDS || j==MAX_FRIENDS){return 2;}
(usr1->friends)[i] = usr2;
(usr2->friends)[j] = usr1;
return 0;
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T20:52:31.839Z | Question from Code | v1 | the code should work as explained in the doc :
/*
* 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
*/
Will it work as expected? will it generate any errors? | int make_post(const User *author, User *target, char *contents) {
if(author == NULL || target == NULL){
return 2;
free(contents);
}
int i =0;
while((author->friends)[i] != NULL && i != MAX_FRIENDS){
if((author->friends)[i] == target){
Post *p = malloc(sizeof(Post));
strcpy(p->author, author->name);
p->contents = contents;
p->date = malloc(sizeof(time_t));
time(p->date);
p->next = target->first_post;
target->first_post = p;
return 0;
}
i++;
}
free(contents);
return 1;
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T20:53:13.729Z | Question from Code | v1 | How does this code fail to work as explained in the doc below:
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/ | int make_post(const User *author, User *target, char *contents) {
if(author == NULL || target == NULL){
return 2;
free(contents);
}
int i =0;
while((author->friends)[i] != NULL && i != MAX_FRIENDS){
if((author->friends)[i] == target){
Post *p = malloc(sizeof(Post));
strcpy(p->author, author->name);
p->contents = contents;
p->date = malloc(sizeof(time_t));
time(p->date);
p->next = target->first_post;
target->first_post = p;
return 0;
}
i++;
}
free(contents);
return 1;
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T23:14:48.118Z | Question from Code | v1 | the code is suposed to work like this : /*
* 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.
*/ and free any memory
is is working as expected
| int delete_user(const char *name, User **user_ptr_del) {
User *prev = NULL;
User *user = *user_ptr_del;
if(user == NULL){return 0;}
while(user != NULL){
if(strcmp(name, user->name) == 0){
int i = 0;
User *friend = user->friends[0];
while(friend != NULL && i < MAX_FRIENDS){
int j = 0;
int indx = 0;
while(friend->friends[j]!= NULL && j < MAX_FRIENDS){
if(strcmp(friend->friends[j]->name, user->name)==0){
indx = j;
}
j++;
}
if(j!=0){
friend->friends[indx] = friend->friends[j-1];
if(friend->friends[j-1]!= NULL){
friend->friends[j-1] = NULL;
}
}
i++;
friend = user->friends[i];
}
Post* p = user->first_post;
Post* del = NULL;
while(p != NULL){
free(p->contents);
free(p->date);
del = p;
p = p->next;
free(del);
}
user->first_post = NULL;
if(prev == NULL){
*user_ptr_del = user->next;
}
else{
prev->next = user->next;
}
free(user);
return 0;
}
prev = user;
user = user -> next;
}
return 1;
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-11T23:25:43.285Z | Question from Code | v1 | what is the problem with this code?
it should work as follows:
/*
* 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 *prev = NULL;
User *user = *user_ptr_del;
if(user == NULL){return 0;}
while(user != NULL){
if(strcmp(name, user->name) == 0){
int i = 0;
User *friend = user->friends[0];
while(friend != NULL && i < MAX_FRIENDS){
int j = 0;
int indx = 0;
while(friend->friends[j]!= NULL && j < MAX_FRIENDS){
if(strcmp(friend->friends[j]->name, user->name)==0){
indx = j;
}
j++;
}
if(j!=0){
friend->friends[indx] = friend->friends[j-1];
if(friend->friends[j-1]!= NULL){
friend->friends[j-1] = NULL;
}
}
i++;
friend = user->friends[i];
}
Post* p = user->first_post;
Post* del = NULL;
while(p != NULL){
free(p->contents);
free(p->date);
del = p;
p = p->next;
free(del);
}
user->first_post = NULL;
if(prev == NULL){
*user_ptr_del = user->next;
}
else{
prev->next = user->next;
}
free(user);
return 0;
}
prev = user;
user = user -> next;
}
return 1;
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-15T17:59:27.888Z | Question from Code | v1 | /*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
is this code working as specified in the doc? if not why? | int make_friends(const char *name1, const char *name2, User *head) {
User *usr1 = find_user(name1, head);
User *usr2 = find_user(name2, head);
int already = 1;
if(usr1 == NULL || usr2 == NULL){return 4;}
if(strcmp(name1, name2)==0){return 3;}
int i = 0;
while((usr1->friends)[i] != NULL && i != MAX_FRIENDS){
if(strcmp((usr1->friends)[i]->name, name2) == 0){
already = 0;
}
i ++;
}
int j = 0;
while((usr2->friends)[j] != NULL && j != MAX_FRIENDS){
if(strcmp((usr2->friends)[j]->name, name1) ==0){
already = 0;
}
j ++;
}
if(i==MAX_FRIENDS || j==MAX_FRIENDS){return 2;}
if(already == 0){return 1;}
(usr1->friends)[i] = usr2;
(usr2->friends)[j] = usr1;
return 0;
} | null | null |
63c648885f004bf4898cfd56 | 2023-02-15T18:01:22.864Z | Question from Code | v1 | does this code pass case 2 | int make_friends(const char *name1, const char *name2, User *head) {
User *usr1 = find_user(name1, head);
User *usr2 = find_user(name2, head);
int already = 1;
if(usr1 == NULL || usr2 == NULL){return 4;}
if(strcmp(name1, name2)==0){return 3;}
int i = 0;
while((usr1->friends)[i] != NULL && i != MAX_FRIENDS){
if(strcmp((usr1->friends)[i]->name, name2) == 0){
already = 0;
}
i ++;
}
int j = 0;
while((usr2->friends)[j] != NULL && j != MAX_FRIENDS){
if(strcmp((usr2->friends)[j]->name, name1) ==0){
already = 0;
}
j ++;
}
if(i==MAX_FRIENDS || j==MAX_FRIENDS){return 2;}
if(already == 0){return 1;}
(usr1->friends)[i] = usr2;
(usr2->friends)[j] = usr1;
return 0;
} | null | null |
63c648885f004bf4898cfd5b | 2023-01-24T20:27:36.407Z | 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_ptr, int n){
fib_ptr = malloc(n * sizeof(int)); //set space in heap
//init first two elements to 0 and 1
*fib_ptr = 0;
if (n == 1){
return 0;
}
*(fib_ptr + 1) = 1;
for (int i = 2; i < n; i++){
fib_ptr[i] = fib_ptr[i - 2] + fib_ptr[i - 1];
}
return 0;
}
int main(int argc, char **argv) {
/* do not change this main function */
int count = strtol(argv[1], NULL, 10);
int *fib_sequence;
fib(&fib_sequence, count);
for (int i = 0; i < count; i++) {
printf("%d ", fib_sequence[i]);
}
free(fib_sequence);
return 0;
} | null | null |
63c648885f004bf4898cfd5b | 2023-01-24T20:28:11.474Z | 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_ptr, int n){
int *fib_ptr = malloc(n * sizeof(int)); //set space in heap
//init first two elements to 0 and 1
*fib_ptr = 0;
if (n == 1){
return 0;
}
*(fib_ptr + 1) = 1;
for (int i = 2; i < n; i++){
fib_ptr[i] = fib_ptr[i - 2] + fib_ptr[i - 1];
}
return 0;
}
int main(int argc, char **argv) {
/* do not change this main function */
int count = strtol(argv[1], NULL, 10);
int *fib_sequence;
fib(&fib_sequence, count);
for (int i = 0; i < count; i++) {
printf("%d ", fib_sequence[i]);
}
free(fib_sequence);
return 0;
} | null | null |
63c648885f004bf4898cfd5b | 2023-01-24T20:28:42.155Z | 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_ptr, int n){
fib_ptr = malloc(n * sizeof(int)); //set space in heap
//init first two elements to 0 and 1
*fib_ptr = 0;
if (n == 1){
return 0;
}
*(fib_ptr + 1) = 1;
for (int i = 2; i < n; i++){
fib_ptr[i] = fib_ptr[i - 2] + fib_ptr[i - 1];
}
return 0;
}
int main(int argc, char **argv) {
/* do not change this main function */
int count = strtol(argv[1], NULL, 10);
int *fib_sequence;
fib(&fib_sequence, count);
for (int i = 0; i < count; i++) {
printf("%d ", fib_sequence[i]);
}
free(fib_sequence);
return 0;
}
| null | null |
63c648885f004bf4898cfd5b | 2023-01-24T20:33:04.214Z | Question from Code | v1 | what should the header of fib be? | 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 |
63c648885f004bf4898cfd5b | 2023-01-24T20:49:57.140Z | 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_ptr, int n){
*fib_ptr = malloc(n * sizeof(int)); //set space in heap
//init first two elements to 0 and 1
**fib_ptr = 0;
int curr_sum = 1;
for (int i = 1; i < n; i++){
*fib_ptr[i] = *fib_ptr[i - 1] + curr_sum;
curr_sum = *fib_ptr[i - 1];
}
return 0;
}
int main(int argc, char **argv) {
/* do not change this main function */
int count = strtol(argv[1], NULL, 10);
int *fib_sequence;
fib(&fib_sequence, count);
for (int i = 0; i < count; i++) {
printf("%d ", fib_sequence[i]);
}
free(fib_sequence);
return 0;
} | null | null |
63c648885f004bf4898cfd5b | 2023-01-24T20:50:12.927Z | 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_ptr, int n){
*fib_ptr = malloc(n * sizeof(int)); //set space in heap
//init first two elements to 0 and 1
**fib_ptr = 0;
int curr_sum = 1;
for (int i = 1; i < n; i++){
*fib_ptr[i] = *fib_ptr[i - 1] + curr_sum;
curr_sum = *fib_ptr[i - 1];
}
return 0;
}
int main(int argc, char **argv) {
/* do not change this main function */
int count = strtol(argv[1], NULL, 10);
int *fib_sequence;
fib(&fib_sequence, count);
for (int i = 0; i < count; i++) {
printf("%d ", fib_sequence[i]);
}
free(fib_sequence);
return 0;
} | 0 1 1 | null |
63c648885f004bf4898cfd5b | 2023-01-24T21:08:32.489Z | General Question | v1 | what should this function return?
| 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.