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
⌀ |
---|---|---|---|---|---|---|---|
63c6487e5f004bf4898cfcfc | 2023-02-14T03:50:57.732Z | Help Fix Code | v1 | null | Post *head = user->first_post;
while (head != NULL){
fprintf(stdout, "Name: %s\n", head->author);
head = head->next;
} | the while loop never ends | null |
63c6487e5f004bf4898cfcfc | 2023-03-09T22:05:51.394Z | Help Write Code | v2 | null | null | null | Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
You will find the following system calls useful: fork, exec, pipe, dup2, write, wait (along with WIFEXITED, WEXITSTATUS). You may not use popen or pclose in your solution. |
63c6487e5f004bf4898cfcfc | 2023-03-09T22:11:31.678Z | Help Write Code | v2 | null | null | null | how to use write to send the user id and password in the parent process to the child process |
63c6487e5f004bf4898cfcfc | 2023-03-09T23:45:00.405Z | Help Write Code | v2 | null | null | null | Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
Your program should use the exit status of the validate program to determine which of the three following messages to print:
"Password verified" if the user id and password match.
"Invalid password" if the user id exists, but the password does not match.
"No such user" if the user id is not recognized
|
63c6487e5f004bf4898cfcfc | 2023-03-09T23:52:43.050Z | Help Write Code | v2 | null | null | null | Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful. Your program should use the exit status of the validate program to determine which of the three following messages to print: "Password verified" if the user id and password match. "Invalid password" if the user id exists, but the password does not match. "No such user" if the user id is not recognized. Use execl, wait, fork and dup2 |
63c6487e5f004bf4898cfcfc | 2023-03-10T02:40:22.332Z | Help Write Code | v2 | null | null | null | our task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
Your program should use the exit status of the validate program to determine which of the three following messages to print:
"Password verified" if the user id and password match.
"Invalid password" if the user id exists, but the password does not match.
"No such user" if the user id is not recognized
The exact messages are given in the starter code as defined constants.
The only case that should be handled directly in checkpasswd is the case where either the userid or password are too long. This is to prevent sending the wrong number of bytes to validate.
Note that in the given password file pass.txt, the "killerwhales:swim" has a user id that is too large, and "monkeys:eatcoconuts" has a password that is too long. The examples are expected to fail, but the other cases should work correctly.
You will find the following system calls useful: fork, exec, pipe, dup2, write, wait (along with WIFEXITED, WEXITSTATUS). You may not use popen or pclose in your solution. |
63c6487e5f004bf4898cfcfc | 2023-03-10T03:51:11.311Z | Help Write Code | v2 | null | null | null | how to run this function -
Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
Your program should use the exit status of the validate program to determine which of the three following messages to print:
"Password verified" if the user id and password match.
"Invalid password" if the user id exists, but the password does not match.
"No such user" if the user id is not recognized
The exact messages are given in the starter code as defined constants.
|
63c6487e5f004bf4898cfcfc | 2023-03-10T13:05:13.254Z | Help Write Code | v2 | null | null | null | how to make a linked list structure for a makefile with the following format: Target line
Starts with a target word, followed by a colon, followed by a space-separated list of dependencies.
The colon has space on each side of it.
Action line
Begins with a tab
After the tab is a command that can be executed in the shell.
Must follow a target line or another action line (possibly with comments/empty lines in between).
Comment or empty line
Contains only spaces and/or tabs.
Or, contains 0 or more spaces/tabs followed by a #. Any characters following the # are ignored.
The struct nodes provided are struct rule, struct dependencies and struct actions |
63c6487e5f004bf4898cfcfc | 2023-03-10T13:08:30.101Z | Help Write Code | v2 | null | null | null | how to deal with whitespaces in the makefile in order to make a linked list structure of rules, dependencies and actions |
63c6487e5f004bf4898cfcfc | 2023-03-10T13:23:12.694Z | Help Write Code | v2 | null | null | null | how do you know if line is a target / dependency / action for if condition |
63c6487e5f004bf4898cfcfc | 2023-03-10T17:58:00.958Z | Help Write Code | v2 | null | null | null | When you have completed Part 1, the print_rules function should print the rules data structure as a makefile. In other words, we should be able to save the output to a file and use it with the regular make program. The print_rules function has been given in the starter code and you must not change it. To run your pmake program on the handout example and see the output generated from print_rules you should cd into handout_example and then run ../pmake -f handout.mk -o. All of your work for this part of the assignment will be done in parse.c.
|
63c6487e5f004bf4898cfcfc | 2023-03-10T18:22:05.887Z | Help Write Code | v2 | null | null | null | Your first task is to implement parse_file, so that it reads a makefile and constructs a corresponding linked data structure. The necessary structs are defined in pmake.h, and the comments at the top of the file explain what each line of a makefile will contain. You may not change the struct definitions. pmake.h also contains function prototypes for functions that are either already provided, or that you will need to write.
The starter code also contains a makefile named handout.mk. (This file and the required source and header files are in the subdirectory handout_example. ) The picture above shows a data structure that could result from parsing this file. Notice that there is one struct rule_node for each rule in the original makefile and an additional struct rule_node for each dependency that is not also a target. You must follow this design. You are are also required to have the head of the list be the first rule in the original makefile. However, depending on how you do your parsing, the struct rule_node elements may come in a different order in your linked list of rules.
Before reading any more of this handout, spend time to make sure you understand this figure and how it connects to the original makefile and the structs defined in pmake.h.
You will see that the actions are stored in a struct action_node that has a member args. This array has the format required by the second parameter to execvp. Use the man page for execvp to understand this format. In particular, notice that the first element is the executable name, subsequent elements are the arguments for that executable, and these are followed by an extra NULL pointer element. |
63c6487e5f004bf4898cfcfc | 2023-03-10T19:35:15.567Z | Help Write Code | v2 | null | null | null | Your first task is to implement parse_file, so that it reads a makefile and constructs a corresponding linked data structure. The necessary structs are defined in pmake.h, and the comments at the top of the file explain what each line of a makefile will contain. You may not change the struct definitions. pmake.h also contains function prototypes for functions that are either already provided, or that you will need to write.
The starter code also contains a makefile named handout.mk. (This file and the required source and header files are in the subdirectory handout_example. ) The picture above shows a data structure that could result from parsing this file. Notice that there is one struct rule_node for each rule in the original makefile and an additional struct rule_node for each dependency that is not also a target. You must follow this design. You are are also required to have the head of the list be the first rule in the original makefile. However, depending on how you do your parsing, the struct rule_node elements may come in a different order in your linked list of rules.
Before reading any more of this handout, spend time to make sure you understand this figure and how it connects to the original makefile and the structs defined in pmake.h.
You will see that the actions are stored in a struct action_node that has a member args. This array has the format required by the second parameter to execvp. Use the man page for execvp to understand this format. In particular, notice that the first element is the executable name, subsequent elements are the arguments for that executable, and these are followed by an extra NULL pointer element.
The real make program supports a fairly complex syntax for writing makefiles. For the purposes of this assignment, your implementation of pmake only needs to support makefiles with the following simplified syntax.
A line from a makefile is either a target line, an action line, or a comment or line to ignore.
Target line
Starts with a target word, followed by a colon, followed by a space-separated list of dependencies.
The colon has space on each side of it.
Action line
Begins with a tab
After the tab is a command that can be executed in the shell.
Must follow a target line or another action line (possibly with comments/empty lines in between).
Comment or empty line
Contains only spaces and/or tabs.
Or, contains 0 or more spaces/tabs followed by a #. Any characters following the # are ignored.
You may make the following assumptions about the format and contents of the Makefile:
The Makefile syntax is valid (i.e., follows the structure described above).
Each target name, dependency name, and action word contains no spaces. This allows you to tokenize each line by splitting on spaces.
Every line in the file contains at most MAXLINE characters, where MAXLINE is a macro defined in pmake.h. This limit includes the newline character \n at the end of a line, if present. It does not include a null-terminator character.
Does not contain variables (e.g., $@), wild cards (e.g., %.o), pattern rules, .PHONY, or special characters at the start of an action line (e.g., @ or -).
The Makefile contains at least one rule.
The Makefile does not contain any circular dependencies (e.g., where target A depends on B, and target B depends on A). |
63c6487e5f004bf4898cfcfc | 2023-03-11T20:01:05.723Z | Question from Code | v2 | is there a segmentation fault | Rule *head;
char *token;
char line[MAXLINE] = "";
Rule *curr_rule;
char *target; | null | null |
63c6487e5f004bf4898cfcfc | 2023-03-11T20:02:50.264Z | Question from Code | v2 | is there a segmentation fault | while (fgets(line, MAXLINE, fp) != NULL) {
token = strtok(line, " ");
if (token[0] != '\t' && token[0] != '#' && token[0] != '\n') {
target = token;
token = strtok(NULL, " ");
while (token != NULL) {
if (token[0] == ':') {
token = strtok(NULL, " ");
} else {
fprintf(stdout, "hello");
}
token = strtok(NULL, " ");
}
}
Rule *new_rule = malloc(sizeof(Rule));
new_rule->target = strdup(target);
new_rule->actions = NULL;
new_rule->dependencies = NULL;
new_rule->next_rule = NULL;
if (head == NULL) {
head = new_rule;
printf("%s", new_rule->target);
} else {
curr_rule = head;
int i = 0;
while (curr_rule != NULL) {
printf("%s = %s, %d", "in the while loop", curr_rule->target, i);
i = i + 1;
curr_rule = curr_rule->next_rule;
}
curr_rule->next_rule = new_rule;
printf("%s + %s == %s", "this is executed", curr_rule->next_rule->target, new_rule->target);
}
}
| null | null |
63c6487e5f004bf4898cfcfc | 2023-03-11T20:04:24.403Z | Question from Code | v2 | is there a segmentation fault
| Rule *curr_rule;
while (curr_rule != NULL) {
printf("%s = %s, %d", "in the while loop", curr_rule->target, i);
i = i + 1;
curr_rule = curr_rule->next_rule;
} | null | null |
63c6487e5f004bf4898cfcfc | 2023-03-11T20:04:55.978Z | Question from Code | v2 | is there a segmentation fault? | Rule *new_rule = malloc(sizeof(Rule));
new_rule->target = strdup(target);
new_rule->actions = NULL;
new_rule->dependencies = NULL;
new_rule->next_rule = NULL; | null | null |
63c6487e5f004bf4898cfcfc | 2023-03-11T20:05:39.711Z | Question from Code | v2 | is there a segmentation fault? | token = strtok(line, " ");
if (token[0] != '\t' && token[0] != '#' && token[0] != '\n') {
target = token;
token = strtok(NULL, " ");
while (token != NULL) {
if (token[0] == ':') {
token = strtok(NULL, " ");
} else {
fprintf(stdout, "hello");
}
token = strtok(NULL, " ");
}
}
| null | null |
63c6487e5f004bf4898cfcfc | 2023-03-11T20:06:23.763Z | Question from Code | v2 | is there seg fault | Rule *parse_file(FILE *fp) {
Rule *head;
char *token;
char line[MAXLINE] = "";
Rule *curr_rule;
char *target;
while (fgets(line, MAXLINE, fp) != NULL) {
token = strtok(line, " ");
if (token[0] != '\t' && token[0] != '#' && token[0] != '\n') {
target = token;
token = strtok(NULL, " ");
while (token != NULL) {
if (token[0] == ':') {
token = strtok(NULL, " ");
} else {
fprintf(stdout, "hello");
}
token = strtok(NULL, " ");
}
}
Rule *new_rule = malloc(sizeof(Rule));
new_rule->target = strdup(target);
new_rule->actions = NULL;
new_rule->dependencies = NULL;
new_rule->next_rule = NULL;
if (head == NULL) {
head = new_rule;
printf("%s", new_rule->target);
} else {
curr_rule = head;
int i = 0;
while (curr_rule != NULL) {
printf("%s = %s, %d", "in the while loop", curr_rule->target, i);
i = i + 1;
curr_rule = curr_rule->next_rule;
}
curr_rule->next_rule = new_rule;
printf("%s + %s == %s", "this is executed", curr_rule->next_rule->target, new_rule->target);
}
}
return head;
}
| null | null |
63c6487e5f004bf4898cfcfc | 2023-03-11T20:51:14.817Z | General Question | v2 | how to check if token[0] is a a tab space | null | null | null |
63c6487e5f004bf4898cfcfc | 2023-03-11T20:52:23.241Z | General Question | v2 | Question: how to check if token[0] is only a tab space
| null | null | null |
63c6487e5f004bf4898cfcfc | 2023-03-11T20:58:59.704Z | Help Write Code | v2 | null | null | null | how to check if token[0] == '\t'? |
63c6487e5f004bf4898cfcfc | 2023-03-11T21:33:57.873Z | Help Fix Code | v2 | what's the seg fault? | if (head != NULL){
Rule *curr_rule;
curr_rule = head;
while(curr_rule != NULL){
curr_rule = curr_rule->next_rule;
}
curr_rule = new_rule;
}
else{
head = malloc(sizeof(Rule));
head = new_rule;
} | null | null |
63c6487e5f004bf4898cfcfc | 2023-03-11T21:34:40.517Z | Question from Code | v2 | seg fault? | if (head != NULL) {
Rule *curr_rule;
curr_rule = head;
while (curr_rule != NULL) {
curr_rule = curr_rule->next_rule;
}
curr_rule = new_rule;
} else {
head = malloc(sizeof(Rule));
head = new_rule;
} | null | null |
63c6487e5f004bf4898cfcfc | 2023-03-11T23:08:31.335Z | Help Write Code | v2 | null | null | null | How to code the following
Add space-separated list of dependencies to struct dependency if dependency is not in rule struct. If it is, assign the dependency to that rule. The list of dependencies must be in the struct dependency
|
63c6487e5f004bf4898cfcfc | 2023-03-11T23:38:43.943Z | Help Fix Code | v2 | what are the errors here |
if (token[0] != '\t' && token[0] != '#' && token[0] != '\n'){
// we are in target line so we deal with target and dependencies together
// we are
target = token; // assign first token to variable - target to store name
dependency = NULL;
while(token != NULL){
if (token[0] == ':'){
token = strtok(NULL, " "); // go to the next token
}
else{
// in the dependency
// create a dependency for each string and keep track of count to malloc at the end
// add each dependency into an array of dependencies at the end if dependency.rule is not in the rule
int i;
if (dependency == NULL){
dependency = malloc(sizeof(Dependency));
dependency->rule = malloc(sizeof(Rule));
dependency->rule->target = strdup(token);
dependency->rule->actions = NULL;
dependency->rule->dependencies = NULL;
dependency->next_dep = malloc(sizeof(Dependency));
dependency->next_dep = NULL;
}
else{
Dependency *curr1 = dependency;
while (curr1 != NULL && strcmp(curr1->rule->target, target) != 0){
curr1 = curr1->next_dep;
}
if (curr1 == NULL){ // no token in the lst
curr1->rule->target = token;
}
else{
token = strtok(NULL, " "); // skip to next dependency
}
}
}
token = strtok(NULL, " ");
} | null | null |
63c6487e5f004bf4898cfcfc | 2023-03-11T23:39:19.153Z | Help Write Code | v2 | null | null | null | how to add the dependencies |
63c6487e5f004bf4898cfcfc | 2023-03-11T23:39:45.651Z | Help Write Code | v2 | null | null | null | how to add dependencies into linked list structure |
63c6487e5f004bf4898cfcfc | 2023-03-12T14:27:27.888Z | Help Write Code | v2 | null | null | null | The starter code also contains a makefile named handout.mk. (This file and the required source and header files are in the subdirectory handout_example. ) The picture above shows a data structure that could result from parsing this file. |
63c6487e5f004bf4898cfcfc | 2023-03-12T14:28:34.733Z | Help Write Code | v2 | null | null | null | how can i implement a linked list of rules, dependencies and action when i parse through a makefile? |
63c6487e5f004bf4898cfcfc | 2023-03-12T14:42:02.782Z | Help Fix Code | v2 | segmentation fault | char*action_args;
char*another_token;
another_token = strtok(line, "\t");
if(another_token[0] != '\t'){
// we are in action
action = malloc(sizeof(Action));
// in the format {"gcc", "-std"}
int i;
while(another_token != NULL){
if (token[0] != '\t'){
action_args[i] = token[0];
printf("%s", action_args[i]);
i = i + 1;
}
another_token = strtok(NULL, " ");
}
action->args = &action_args;
action->next_act = NULL;
printf("%s", action->args);
} | null | null |
63c6487e5f004bf4898cfcfc | 2023-03-12T14:43:14.147Z | Help Fix Code | v2 | segmentation fault? | char*action_args;
char*another_token;
another_token = strtok(line, "\t");
if(another_token[0] != '\t'){
// we are in action
action = malloc(sizeof(Action));
// in the format {"gcc", "-std"}
int i;
while(another_token != NULL){
if (token[0] != '\t'){
action_args[i] = token[0];
printf("%s", action_args[i]);
i = i + 1;
}
another_token = strtok(NULL, " ");
}
action->args = &action_args;
action->next_act = NULL;
printf("%s", action->args);
} | null | null |
63c6487e5f004bf4898cfcfc | 2023-03-12T14:48:07.848Z | Help Fix Code | v2 | how to fix this | char*action_args;
char*another_token;
another_token = strtok(line, "\t");
if(another_token[0] != '\t'){
// we are in action
action = malloc(sizeof(Action));
// in the format {"gcc", "-std"}
int i;
while(another_token != NULL){
if (token[0] != '\t'){
strcpy(action_args[i], token);
printf("%s", action_args[i]);
i = i + 1;
}
another_token = strtok(NULL, " ");
}
action->args = strdup(action_args);
action->next_act = NULL;
printf("%s", action->args);
} | null | null |
63c6487e5f004bf4898cfcfc | 2023-03-12T14:48:37.359Z | Help Fix Code | v2 | how to fix this | char*action_args;
char*another_token;
another_token = strtok(line, "\t");
if(another_token[0] != '\t'){
// we are in action
action = malloc(sizeof(Action));
// in the format {"gcc", "-std"}
int i;
while(another_token != NULL){
if (token[0] != '\t'){
strcpy(action_args[i], token);
printf("%s", action_args[i]);
i = i + 1;
}
another_token = strtok(NULL, " ");
}
action->args = strdup(action_args);
action->next_act = NULL;
printf("%s", action->args);
}
} | null | null |
63c6487e5f004bf4898cfcfc | 2023-03-12T14:49:27.403Z | Help Fix Code | v2 | action.arg is the same as the second parameter to execvp? how to fix this code | char*action_args;
char*another_token;
another_token = strtok(line, "\t");
if(another_token[0] != '\t'){
// we are in action
action = malloc(sizeof(Action));
// in the format {"gcc", "-std"}
int i;
while(another_token != NULL){
if (token[0] != '\t'){
strcpy(action_args[i], token);
printf("%s", action_args[i]);
i = i + 1;
}
another_token = strtok(NULL, " ");
}
action->args = strdup(action_args);
action->next_act = NULL;
printf("%s", action->args);
}
}
} | null | null |
63c6487e5f004bf4898cfcfc | 2023-03-13T02:01:47.266Z | Help Fix Code | v2 | is there a seg fault? | Rule *parse_file(FILE *fp) {
// Implement this function and remove the stubbed return statement below.
Rule *head = NULL;
Rule *target = NULL;
char line[MAXLINE];
while(fgets(line, MAXLINE, fp)){
line[MAXLINE - 1] = '\0';
int i = strlen(line) -1;
while((isspace(line[i])!= 0) && (is_comment_or_empty(line) != 1)){
line[i] = '\0';
i = i - 1;
}
if (((is_comment_or_empty(line) != 1)) && (strlen(line)!= 0)){
char * duplicate = strdup(line);
char *token = strtok(line, " ");
if (token[0] != '\r'){
if (token[0] != '\t'){
if (token[0] == ':'){
token = strtok(NULL, " "); // go to the next token
}
else{
parse_rule(&head, &target, token); // add the target rule and dependency in a helper
}
}
else{
// action line as first char is a tab
parse_action(&head, token, duplicate); // add the action line to the node
}
}
}
}
return head;
} | null | null |
63c6487e5f004bf4898cfcfc | 2023-03-14T21:45:08.135Z | Help Write Code | v2 | null | null | null | How to implement run make dependencies in sequential
|
63c6487e5f004bf4898cfcfc | 2023-03-14T21:46:23.003Z | Help Write Code | v2 | null | null | null | Intended Behavior: How to implement run make dependencies in sequential
|
63c6487e5f004bf4898cfcfc | 2023-03-14T21:46:50.729Z | Help Write Code | v2 | null | null | null | Intended Behavior: How to implement run make dependencies in sequential
|
63c6487f5f004bf4898cfd06 | 2023-02-10T18:03:19.050Z | Explain Code | v1 | null |
/*
* Read in the location of the pixel array, the image width, and the image
* height in the given bitmap file.
*/
void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) {
}
/*
* Read in pixel array by following these instructions:
*
* 1. First, allocate space for m `struct pixel *` values, where m is the
* height of the image. Each pointer will eventually point to one row of
* pixel data.
* 2. For each pointer you just allocated, initialize it to point to
* heap-allocated space for an entire row of pixel data.
* 3. Use the given file and pixel_array_offset to initialize the actual
* struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is
* consistent with the bitmap file format.
* NOTE: We've tested this assumption on the Teaching Lab machines, but
* if you're trying to work on your own computer, we strongly recommend
* checking this assumption!
* 4. Return the address of the first `struct pixel *` you initialized.
*/
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
}
int height = ** pixel.height;
malloc(sizeof(struct pixel*)*)
/*
* Print the blue, green, and red colour values of a pixel.
* You don't need to change this function.
*/
void print_pixel(struct pixel p) {
printf("(%u, %u, %u)\n", p.blue, p.green, p.red);
}
| null | null |
63c6487f5f004bf4898cfd06 | 2023-02-15T01:03:43.260Z | General Question | v1 | How do I traverse through a linked list until I find the element with the name I want and return a pointer to it?
| null | null | null |
63c6487f5f004bf4898cfd06 | 2023-02-15T01:13:13.671Z | Question from Code | v1 | How to cast from a const struct to a struct? | typedef struct user {
char name[MAX_NAME]; // name of a user is an array of max 32 chars
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post; // pointer to the user's first post, which will contain 1) a pointer to the post itself and 2) a .next attribute, that will in turn point to another post which will be stored in the linked list of posts of the recipient of the post
struct user *friends[MAX_FRIENDS]; //Points to another user
struct user *next;
} User;
User *find_user(const char *name, const User *head) {
}
| null | null |
63c6487f5f004bf4898cfd06 | 2023-02-15T01:36:23.720Z | Help Fix Code | v1 | null | User *find_user(const char *name, const User *head) {
User *temp = (User*) head; //Is this what it means by casting?
while (temp->next != NULL)
{
if (temp->name == name)
{
return temp;
}
}
| This should traverse through the linked list starting with head, and return a pointer to the node whose name is equal to the parameter name | null |
63c6487f5f004bf4898cfd06 | 2023-02-15T01:40:51.346Z | Help Fix Code | v1 | null | User *find_user(const char *name, const User *head) {
User *temp = (User*) head; //Is this what it means by casting?
while (temp->next != NULL)
{
if (strcmp(temp->name, *name) == 0)
{
return temp;
}
temp = temp->next;
}
return NULL;
}
| This should traverse through the linked list starting with head, and return a pointer to the node whose name is equal to the parameter name | null |
63c6487f5f004bf4898cfd06 | 2023-02-15T02:11:57.827Z | Help Fix Code | v1 | null | typedef struct user {
char name[MAX_NAME]; // name of a user is an array of max 32 chars
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post; // pointer to the user's first post, which will contain 1) a pointer to the post itself and 2) a .next attribute, that will in turn point to another post which will be stored in the linked list of posts of the recipient of the post
struct user *friends[MAX_FRIENDS]; //Points to another user
struct user *next;
} User;
void list_users(const User *curr) {
while (curr->next != NULL)
{
fprintf("%s\n", curr->name);
curr = curr->next;
}
} | list_users prints the usernames of all users in the list starting at curr. Names are printed one per line | null |
63c6487f5f004bf4898cfd06 | 2023-02-15T02:14:52.818Z | Help Fix Code | v1 | null | typedef struct user {
char name[MAX_NAME]; // name of a user is an array of max 32 chars
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post; // pointer to the user's first post, which will contain 1) a pointer to the post itself and 2) a .next attribute, that will in turn point to another post which will be stored in the linked list of posts of the recipient of the post
struct user *friends[MAX_FRIENDS]; //Points to another user
struct user *next;
} User;
void list_users(const User *curr) {
while (curr != NULL)
{
fprintf("%s\n", curr->name);
curr = curr->next;
}
} | list users prints to standard output the namesof Users in the linked list starting at curr, one per line.
This gives the folloeing error: error: format not a string literal and no format arguments | null |
63c6487f5f004bf4898cfd06 | 2023-02-15T03:56:56.458Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
// ptr is pointer to a pointer to a struct
// we are basically creating a new node in a linked list
// to create a new node, we need the node that follows it
// structs are not passed to functions in the ame way as arrays; we don't pass a pointer by default
if(strlen(name) > (MAX_NAME + 1)) {return 2;} //stlen does not consider null terminator
//Now, let's check if the name already exists: let's travense through the
// linked list and check for the name of every other user:
User* curr = *user_ptr_add;
while (curr->next!= NULL)
{
if(curr->name == name){return 1;}
curr = curr->next;
}
User* new_user = malloc(sizeof(User)); //allocate space for new user
strcpy((*new_user).name, name);
curr->next = new_user;
return 0;
} | create_user should crreate 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. | null |
63c6487f5f004bf4898cfd06 | 2023-02-15T04:02:13.345Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
// ptr is pointer to a pointer to a struct
// we are basically creating a new node in a linked list
// to create a new node, we need the node that follows it
// structs are not passed to functions in the ame way as arrays; we don't pass a pointer by default
if(strlen(name) > (MAX_NAME + 1)) {return 2;} //stlen does not consider null terminator
//Now, let's check if the name already exists: let's travense through the
// linked list and check for the name of every other user:
User* curr = *user_ptr_add;
while (curr->next!= NULL)
{
if(strcmp(curr->name, name)){return 1;}
curr = curr->next;
}
User* new_user = malloc(sizeof(User)); //allocate space for new user
strcpy(new_user->name, name);
new_user->next = NULL;
curr->next = new_user;
return 0;
} | create_user should crreate 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 |
63c6487f5f004bf4898cfd06 | 2023-02-15T04:08:43.030Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
// ptr is pointer to a pointer to a struct
// we are basically creating a new node in a linked list
// to create a new node, we need the node that follows it
// structs are not passed to functions in the ame way as arrays; we don't pass a pointer by default
if(strlen(name) > (MAX_NAME + 1)) {return 2;} //stlen does not consider null terminator
//Now, let's check if the name already exists: let's travense through the
// linked list and check for the name of every other user:
User* curr = *user_ptr_add;
while (curr->next!= NULL)
{
if(strcmp(curr->name, name)){return 1;}
curr = curr->next;
}
User* new_user = malloc(sizeof(User)); //allocate space for new user
strcpy(new_user->name, name);
new_user->next = NULL;
*curr->next = *new_user;
return 0;
} | Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* | null |
63c6487f5f004bf4898cfd06 | 2023-02-15T22:24:55.643Z | Help Fix Code | v1 | null | Post* new_post = malloc(sizeof(Post));
strncpy(new_post->author, author_casted->name, MAX_NAME); //set author
//new_post->contents = NULL;
new_post->contents = contents; //set contents
time_t *curtime;
time(curtime);
new_post->date = curtime; // set date
new_post->next = target->first_post;
target->first_post = new_post;
return 0;
} | I am trying to save the current time, as a time object in the new_post->date attribute, which is of typetime_t * | null |
63c6487f5f004bf4898cfd06 | 2023-02-17T18:29:52.832Z | Explain Code | v1 | null | int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: copy size src\n");
exit(1);
}
int size = strtol(argv[1], NULL, 10);
char *src = argv[2];
char dummy1[size];
char dest[size];
char dummy2[size];
memset(dest, 'x', size);
// Set all the bytes of dest to a dummy value to facilitate testing
// The two dummy arrays should be allocated before and after dest
// We fill them with values to help with debugging and testing
memset(dummy1, 'A', size);
dummy1[size - 1] = '\0';
memset(dummy2, 'Z', size);
dummy2[size - 1] = '\0';
copy(dest, src, size);
printf("%s\n", dest);
printf("%s %s\n", dummy1, dummy2);
return 0;
}
| null | null |
63c6487f5f004bf4898cfd06 | 2023-02-17T18:58:17.350Z | Help Fix Code | v1 | null | char *copy(char *dest, const char *src, int capacity) {
// dest is the starting memory address to be filled
// capacity is already the number of bytes
int j = 0;
int* i = &j; //declare i as a pointer so we can access it after for loop
for(*i = 0; *i < capacity/sizeof(char); i++){
memset(&(dest[*i]), src[*i], 1); //copy each string exactly once
}
// now let's add the null pointer
memset(&(dest[*i+1]), '\0', 1);
return dest;
}
| The function copy should copy as many characters in src into dest as indicated by capacity, but it should add a null terminator at the end | null |
63c6487f5f004bf4898cfd06 | 2023-02-17T20:48:55.321Z | Explain Code | v1 | null | char *copy(char *dest, const char *src, int capacity) {
// dest is the starting memory address to be filled
// capacity is already the number of bytes
int j = 0;
int* i = &j; //declare i as a pointer so we can access it after for loop
for(*i = 0; *i < capacity/sizeof(char); i++){
memset(&(dest[*i]), src[*i], 1); //copy each string exactly once
}
// now let's add the null pointer
memset(&(dest[*i+1]), '\0', 1);
return dest;
}
#include <stdio.h>
#define SIZE 4
#define OVERFLOW 4
int main() {
int index = 0;
int i;
int before[SIZE] = {10, 10, 10, 10};
int a[SIZE] = {0, 0, 0, 0};
int after[SIZE] = {10, 10, 10, 10};
printf("Address of the variables:\n");
for (index = 0; index < SIZE; index++) {
printf("%lx -> &after[%d]\n", (unsigned long) &after[index], index);
}
for (index = 0; index < SIZE; index++) {
printf("%lx -> &a[%d]\n", (unsigned long) &a[index], index);
}
for (index = 0; index < SIZE; index++) {
printf("%lx -> &before[%d]\n", (unsigned long) &before[index], index);
}
printf("%lx -> &i\n", (unsigned long)&i);
printf("%lx -> &index\n", (unsigned long)&index);
printf("\n");
printf("Initial values:\n");
printf("i = %d\n", i);
printf("before = {%d, %d, %d, %d}\n", before[0], before[1], before[2], before[3]);
printf("a = {%d, %d, %d, %d}\n", a[0], a[1], a[2], a[3]);
printf("after = {%d, %d, %d, %d}\n", after[0], after[1], after[2], after[3]);
printf("\n");
for (i = 0; i < OVERFLOW; i++) {
a[i] = i * 10;
printf("i = %d\n", i);
printf("before = {%d, %d, %d, %d}\n", before[0], before[1], before[2], before[3]);
printf("a = {%d, %d, %d, %d}\n", a[0], a[1], a[2], a[3]);
printf("after = {%d, %d, %d, %d}\n", after[0], after[1], after[2], after[3]);
}
return 0;
} | null | null |
63c6487f5f004bf4898cfd06 | 2023-03-14T19:52:07.437Z | Help Fix Code | v2 | Create a dynamically allocated string array that has each of the tokens returned by strtok as elements. I also want it to have one extra space for a NULL terminator. | char* temp_array[MAXLINE + 1];
int i = 0;
int instruction_count = 0;
curr_token = strtok(NULL, delimiter);
while (curr_token != NULL)
{
strcpy(temp_array[i], curr_token);
instruction_count ++;
i ++;
curr_token = strtok(NULL, delimiter);
}
char* arg_array = malloc(sizeof(char*)*(instruction_count + 1));
for(int i = 0; i < instruction_count; i++)
{
arg_array[i] = malloc(sizeof(char)*MAXLINE);
strcpy(arg_array[i], temp_array[i]);
} | null | null |
63c6487f5f004bf4898cfd06 | 2023-03-14T20:47:44.683Z | Help Fix Code | v2 | Save every string retuned by strtok in a dynamically allocated array with space for exactly the number of tokens in line, plus one | int i = 0;
int instruction_count = 0;
char buffer[MAXLINE + 1];
strcpy(buffer, line);
curr_token = strtok(NULL, delimiter);
while (curr_token != NULL)
{
instruction_count ++;
i ++;
curr_token = strtok(NULL, delimiter);
}
char* arg_array = malloc(sizeof(char*)*(instruction_count + 1));
if(arg_array == NULL)
{
perror("malloc");
exit(1);
}
char* new_curr_token = strtok(buffer, delimiter);
int j = 0;
while (new_curr_token != NULL)
{
arg_array[j] = malloc(sizeof(char)*(strlen(new_curr_token) + 1));
if(arg_array[j] == NULL)
{
perror("malloc");
exit(1);
}
strcpy(arg_array[j], new_curr_token);
new_curr_token = strtok(NULL, delimiter);
}
for(int i = 0; i < instruction_count; i++){
printf("%s\n", arg_array[i]);
} | null | null |
63c6487f5f004bf4898cfd06 | 2023-03-14T20:53:23.329Z | Question from Code | v2 | Why is the compiler giving a warning when I assign
arg_array[j] = malloc(sizeof(char)*(strlen(new_curr_token) + 1)); | int instruction_count = 0;
char buffer[MAXLINE + 1];
strcpy(buffer, line);
curr_token = strtok(NULL, delimiter);
while (curr_token != NULL) {
instruction_count++;
curr_token = strtok(NULL, delimiter);
}
char* arg_array = malloc(sizeof(char*) * (instruction_count + 1));
if (arg_array == NULL) {
perror("malloc");
exit(1);
}
char* new_curr_token = strtok(buffer, delimiter);
new_curr_token = strtok(NULL, delimiter);
int j = 0;
while (new_curr_token != NULL) {
arg_array[j] = malloc(sizeof(char) * (strlen(new_curr_token) + 1));
if (arg_array[j] == NULL) {
perror("malloc");
exit(1);
}
strcpy(arg_array[j], new_curr_token);
new_curr_token = strtok(NULL, delimiter);
j++;
}
for (int i = 0; i < instruction_count; i++) {
printf("%s\n", arg_array[i]);
}
}
} | null | null |
63c6487f5f004bf4898cfd06 | 2023-03-14T21:01:31.514Z | Question from Code | v2 | Why does this produce a segmentation fault? | int instruction_count = 0;
char buffer[MAXLINE + 1];
strcpy(buffer, line);
curr_token = strtok(NULL, delimiter);
while (curr_token != NULL) {
instruction_count++;
curr_token = strtok(NULL, delimiter);
}
// char* arg_array = malloc(sizeof(char*)*(instruction_count + 1));
char* arg_array[instruction_count + 1];
if (arg_array == NULL) {
perror("malloc");
exit(1);
}
char* new_curr_token = strtok(buffer, delimiter);
new_curr_token = strtok(NULL, delimiter);
int j = 0;
while (new_curr_token != NULL) {
sleep(5);
arg_array[j] = malloc(sizeof(char) * (strlen(new_curr_token) + 1));
if (arg_array[j] == NULL) {
perror("malloc");
exit(1);
}
printf(" current intruction: %s", new_curr_token);
sleep(5);
strcpy(arg_array[j], new_curr_token);
new_curr_token = strtok(NULL, delimiter);
j++;
} | null | null |
63c6487f5f004bf4898cfd06 | 2023-03-14T21:15:30.882Z | Help Fix Code | v2 | Store strings in a dynamically allocated array | Rule* next_rule = add_rule_node(&first_rule, curr_token, &tail);
Dependency* tail_dependencies2 = NULL;
curr_token = strtok(NULL, delimiter);
//let us build the node
while (curr_token != NULL)
{
add_dependency_node(&tail_dependencies2, curr_token, &next_rule);
if(is_dependency_target(&first_rule, curr_token))
{
printf("Dependency %s is also a target", curr_token);
}
curr_token = strtok(NULL, delimiter);
}
} else if(line[0] == '\t'){
//printf("ACTION!");
//create an array of strings from the recipie
int instruction_count = 0;
char buffer[MAXLINE + 1];
strcpy(buffer, line);
curr_token = strtok(NULL, delimiter);
while (curr_token != NULL)
{
instruction_count ++;
curr_token = strtok(NULL, delimiter); | null | null |
63c6487f5f004bf4898cfd06 | 2023-03-15T07:16:58.341Z | Explain Code | v2 | null | void remove_spaces(char* s) {
char* d = s;
do {
while (*d == ' ') {
++d;
}
} while (*s++ = *d++);
} | null | null |
63c6487f5f004bf4898cfd06 | 2023-03-15T18:21:11.400Z | General Question | v2 | How do I compare the last modified times of two files using stat? | null | null | null |
63c6487f5f004bf4898cfd06 | 2023-03-15T20:53:56.225Z | Help Fix Code | v2 | execute the instruction written in curr_action->args, by forking and using execvp in the child process. | void execute_instructions(Rule* rule)
{
printf("building %s\n", rule->target);
Action* curr_action = rule->actions;
while(curr_action != NULL)
{
int f = fork();
if(f < 0)
{
perror("fork");
exit(1);
} else if(f == 0)// we are in the child
{
//run instructions
printf("Child");
char* recipie;
recipie = args_to_string(curr_action->args, recipie, MAXLINE);
printf("%s\n", recipie);
execvp(curr_action->args[0], curr_action->args);
} else
{
int status;
if(wait(&status) == -1)
{
perror("wait");
exit(1);
}
}
curr_action = curr_action->next_act;
}
}
| null | null |
63c6487f5f004bf4898cfd06 | 2023-03-22T20:37:07.424Z | Explain Code | v2 | null | void write_random_pieces(int soc, const char *message, int times) {
char piece[MAXCHARS];
int message_len = strlen(message);
int total_bytes = times * message_len;
int current_byte = 0;
while (current_byte < total_bytes) {
int piece_size = rand() % (MAXCHARS - MINCHARS + 1) + MINCHARS;
int bytes_left = total_bytes - current_byte;
if (piece_size > bytes_left) {
piece_size = bytes_left;
}
for (int i = 0; i < piece_size; i++) {
piece[i] = message[(current_byte + i) % message_len];
}
write(soc, piece, piece_size);
current_byte += piece_size;
} | null | null |
63c6487f5f004bf4898cfd06 | 2023-03-31T15:06:05.019Z | Help Fix Code | v2 | read an input from a socket. If the user does not have a username, then the input is its username. Else, echo the input back through the same file descriptor | int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if(users[client_index].username == NULL)//we haven't set it yet
{
users[client_index].username = malloc(num_read + 1);
strncpy(users[client_index].username, buf, BUF_SIZE);
}
char new_buf[BUF_SIZE + sizeof(users[client_index].username)];
strcpy(strcat(new_buf, (char*)':'), users[client_index].username);
strcpy(&(new_buf[sizeof(users[client_index].username) - 1]), buf);
printf("message to write: %s", new_buf);
if (num_read == 0 || write(fd, new_buf, strlen(new_buf)) != strlen(new_buf)) {
users[client_index].sock_fd = -1;
return fd;
}
| null | null |
63c6487f5f004bf4898cfd06 | 2023-04-05T15:33:06.876Z | Question from Code | v2 | what does curr point to after line 4 is executed? get_size_buf_lu loops through the linked list and perfoms ->next operations on curr | char* list_users(const User* curr) {
printf("User List\n");
// let's go through the list once to know exactly how much space we need to allocate
int username_chars = get_size_buff_lu(curr);
char* buff = malloc(sizeof(char) * (username_chars + 2)); // add 2 for the \r\n?
int max_chars = username_chars;
while (curr != NULL) {
int written_chars = snprintf(buff, max_chars, "%s", curr->name);
if (written_chars < 0 || written_chars > username_chars) {
perror("writting to buffer with snprintf");
}
max_chars -= written_chars;
buff += written_chars;
curr = curr->next;
}
// add network newline
buff[username_chars - 1] = '\r';
buff[username_chars] = '\n';
return buff;
}
| null | null |
63c6487f5f004bf4898cfd06 | 2023-04-05T15:55:39.545Z | Question from Code | v2 | Why is this code chopping off the last character of the string buff? | char* list_users(const User* curr) {
printf("User List\n");
// let's go through the list once to know exactly how much space we need to allocate
int username_chars = get_size_buff_lu(curr);
char* buff = malloc(sizeof(char) * (username_chars + 2)); // add 2 for the \r\n?
int max_chars = username_chars;
char* offset = buff;
while (curr != NULL) {
int written_chars = snprintf(offset, max_chars, "%s", curr->name);
if (written_chars < 0 || written_chars > username_chars) {
perror("writting to buffer with snprintf");
}
max_chars -= written_chars;
offset += written_chars;
curr = curr->next;
}
// add network newline
buff[username_chars] = '\r';
buff[username_chars + 1] = '\n';
return buff;
} | null | null |
63c6487f5f004bf4898cfd06 | 2023-04-05T19:06:43.953Z | Question from Code | v2 | This fuction writes string to a dynamically allocated buffer, but line 25 is raising a warning. Why? | int print_user(const User* user) {
if (user == NULL) {
return 1;
}
// let us first go through this user first to see how much memory we need to allocate:
int total_chars = get_size_buf_user(user);
// we need extra 2 spaces for \r\n
char* buff = malloc(sizeof(char) * (total_chars + 2));
char* offset = buff;
int max_chars = total_chars + 1;
// Now, let's actually write stuff in the buffer:
// For that let us go through this user's attributes again
int written_chars = snprintf(offset, max_chars, "%s", "Name: %s\n\n");
// check for errors in sprintf
if (written_chars < 0 || written_chars > total_chars) {
perror("writting to buffer with snprintf");
}
max_chars -= written_chars;
offset += written_chars;
written_chars = snprintf(offset, max_chars, "%s", user->name);
// check for errors in sprintf
if (written_chars < 0 || written_chars > total_chars) {
perror("writting to buffer with snprintf");
}
max_chars -= written_chars;
offset += written_chars; | null | null |
63c6487f5f004bf4898cfd06 | 2023-04-06T00:53:20.266Z | General Question | v2 | When creating a struct sockaddr_in what is the difference between allocating it in the stack and allocating it in the heap using malloc?
| null | null | null |
63c6487f5f004bf4898cfd06 | 2023-04-22T22:57:01.597Z | Help Fix Code | v2 | replace every item in idk with the string "NO" | void answer(char** list, int size)
{
for (int i = 0; i < size; i++)
{
//char new_word[] = "nope";
strcpy(list[i], "NO");
}
}
int main()
{
char* idk[10] = {"Hi", "we", "all", "looove", "C", "syntax", "dont", "we", "?"};
answer(idk, 9);
for (int i = 0; i < sizeof(idk); i++)
{
printf("%s", idk[i]);
}
} | null | null |
63c648825f004bf4898cfd1a | 2023-01-26T00:39:06.325Z | Help Fix Code | v1 | null | int check_permissions(char *first, char *second) {
// first array represent the permission field of a file
// second array represent permissions that are required
int result = 0;
int i;
for (i = 0; i < 9; i++) {
if (second[i] != '-') {
if (first[i] != second[i]) {
result = 1;
}
}
}
return result;
} | the code returns 0 if for every element that is not '-' in the second array, it is included in the first array | null |
63c648825f004bf4898cfd1a | 2023-02-11T23:05:41.509Z | Help Fix Code | v1 | null | User *find_user(const char *name, const User *head) {
User *curr = (User *)head;
while ((curr != NULL) && (curr->name != name)) {
curr = curr->next;
}
if (curr->name == name) {
return curr;
}
return NULL;
} | to find a user from the linked list starting with the parameter head | null |
63c648825f004bf4898cfd1a | 2023-02-11T23:08:28.187Z | Help Fix Code | v1 | null | void list_users(const User *curr) {
printf("User List");
while (curr != NULL) {
printf("\t%s\n", curr->name);
}
} | Print the usernames of all users in the list starting at curr | null |
63c648825f004bf4898cfd1a | 2023-02-11T23:19:37.854Z | Help Fix Code | v1 | null | int print_user(const User *user) {
if (user == NULL) {
return 1;
}
// format
// Name:
printf("Name: %s\n", user->name);
printf("------------------------------------------\n");
printf("Friends:\n");
// getting all the friends
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) {
User *friend = user->friends[i];
printf("%s\n", friend->name);
}
}
printf("------------------------------------------\n");
printf("Posts:\n");
Post *post = user->first_post;
while (post != NULL) {
printf("From: %s\n", post->author);
printf("Date: %s\n\n", asctime(localtime(post->date)));
printf("%s", post->contents);
}
post = post->next;
if (post != NULL) {
printf("===\n");
}
printf("------------------------------------------\n");
return 0;
} | print the user profile in this format | null |
63c648825f004bf4898cfd1a | 2023-02-11T23:23:53.553Z | General Question | 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. | null | null | null |
63c648825f004bf4898cfd1a | 2023-02-11T23:24:59.732Z | Help Fix Code | v1 | null | int make_friends(const char *name1, const char *name2, User *head) {
// then get the users from the linked list
// loop thorugh the linked list until you either get the names of the friends or NULL
// if at least one user does not exist, return 4
// just use find user
// find_user(const char *name, const User *head)
User *user1 = find_user(name1, head);
User *user2 = find_user(name2, head);
// first check if name1 = name2
// if true, return 3
if (user1 == user2) {
return 3;
}
// check if user do not exist
if ((user1 == NULL) || (user2 == NULL)) {
return 4;
}
// check if users are friends
// return 1 if already friends
// return 2 if not friends but at least one has max friends
// loop through the array to determine how many friends there are
int num_friends_one = 0;
int num_friends_two = 0;
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user1->friends[i] != NULL) {
num_friends_one += 1;
}
if (user2->friends[i] != NULL) {
num_friends_two += 1;
}
}
int friend_in_array = 1; // this means not in the array
for (int i = 0; i < num_friends_one; i++) {
if (user1->friends[i] == user2) {
friend_in_array = 0; // this means the friend is in the array
}
}
if (friend_in_array == 0) { // this means they are already friends
return 1;
}
if ((num_friends_one == MAX_FRIENDS) || (num_friends_two == MAX_FRIENDS)) {
return 2;
}
// if you get to here it means everything is good
// loop through the first user's friends list and add it to the end
// or can just use strlen() and determine where to add it - this is better
// add other friend user to second user's friend list
// return 0;
user1->friends[num_friends_one - 1] = user2;
user2->friends[num_friends_two - 1] = user1;
return 0;
} | Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist. | null |
63c648825f004bf4898cfd1a | 2023-02-11T23:28:51.236Z | Help Fix Code | v1 | null | int update_pic(User *user, const char *filename) {
if (strlen(filename) > MAX_NAME) {
return 2;
}
FILE *output_file = fopen(filename, "w");
if (output_file == NULL) {
return 1;
} else {
strcpy(user->profile_pic, filename);
return 0;
}
} | Change the filename for the profile pic of the given user. | null |
63c648825f004bf4898cfd1a | 2023-02-11T23:51:22.338Z | Help Fix Code | v1 | null | int print_user(const User *user) {
if (user == NULL) {
return 1;
}
// char filename[MAX_NAME];
// strncpy(filename, user->profile_pic, MAX_NAME);
// need to add profile pic part
FILE *file = fopen(user->profile_pic, "r");
char str[60];
while (fgets(str, 60, file) != NULL) {
printf(str);
}
fclose(file);
printf("Name: %s\n", user->name);
printf("------------------------------------------\n");
printf("Friends:\n");
// getting all the friends
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) {
// User *friend = user->friends[i];
printf("%s\n", user->friends[i]->name);
}
}
printf("------------------------------------------\n");
printf("Posts:\n");
Post *post = user->first_post;
while (post != NULL) {
printf("From: %s\n", post->author);
printf("Date: %s\n\n", asctime(localtime(post->date)));
printf("%s", post->contents);
post = post->next;
}
if (post != NULL) {
printf("===\n");
}
printf("------------------------------------------\n");
return 0;
} | print a user profile | null |
63c648825f004bf4898cfd1a | 2023-02-12T01:45:41.414Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
// case 1: user is at start of list
// case 2: user at middle of list
// case 3: user at end of list
// case 4: user does not exist
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
// loop through user's friends, for each friend, loop through their friends and remove the user friend
for (int i = 0; i < MAX_FRIENDS; i++) {
User *friend = user->friends[i];
if (friend != NULL) {
for (int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(friend->friends[j]->name, name) == 0){
friend->friends[j] = NULL;
}
}
}
}
// don't forget to free the user
// // case 1: user at start of linked list
// if (user == *user_ptr_del) {
// *user_ptr_del = *user_ptr_del->next;
// }
// case 2 and 3
User *prev = NULL;
User *curr = *user_ptr_del;
while ((curr != NULL) && (curr->name != name)) {
prev = curr;
curr = curr->next;
}
if (prev == NULL) {
*user_ptr_del = curr->next;
} else if (curr->name == name && curr->next == NULL) { // this means user is at the end of the list
prev->next = NULL;
} else if (curr->name == name && curr->next != NULL){ // this means that element is in middle of list?
prev->next = curr->next;
} else { // in the case user is not in list?
return 1;
}
// case 3: the element is at the end of the list
free(user);
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. | null |
63c648825f004bf4898cfd1a | 2023-02-12T01:49:00.684Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
// case 1: user is at start of list
// case 2: user at middle of list
// case 3: user at end of list
// case 4: user does not exist
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
// loop through user's friends, for each friend, loop through their friends and remove the user friend
for (int i = 0; i < MAX_FRIENDS; i++) {
User *friend = user->friends[i];
if (friend != NULL) {
for (int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(friend->friends[j]->name, name) == 0){
friend->friends[j] = NULL;
}
}
}
}
// don't forget to free the user
// // case 1: user at start of linked list
// if (user == *user_ptr_del) {
// *user_ptr_del = *user_ptr_del->next;
// }
// case 2 and 3
User *prev = NULL;
User *curr = *user_ptr_del;
while ((curr != NULL) && (strcmp(curr->name, name) != 0)) {
prev = curr;
curr = curr->next;
}
if (prev == NULL) {
*user_ptr_del = curr->next;
} else if ((strcmp(curr->name, name) == 0) && curr->next == NULL) { // this means user is at the end of the list
prev->next = NULL;
} else if ((strcmp(curr->name, name) == 0) && curr->next != NULL){ // this means that element is in middle of list?
prev->next = curr->next;
} else { // in the case user is not in list?
return 1;
}
// case 3: the element is at the end of the list
// free(user);
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. | null |
63c648825f004bf4898cfd1a | 2023-02-12T01:55:02.645Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
// case 1: user is at start of list
// case 2: user at middle of list
// case 3: user at end of list
// case 4: user does not exist
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
// loop through user's friends, for each friend, loop through their friends and remove the user friend
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) {
User *user2 = user->friends[i];
for (int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(user2->friends[j]->name, name) == 0){
user2->friends[j] = NULL;
break;
}
}
}
}
// don't forget to free the user
// // case 1: user at start of linked list
// if (user == *user_ptr_del) {
// *user_ptr_del = *user_ptr_del->next;
// }
// case 2 and 3
User *prev = NULL;
User *curr = *user_ptr_del;
while ((curr != NULL) && (strcmp(curr->name, name) != 0)) {
prev = curr;
curr = curr->next;
}
if (prev == NULL) {
*user_ptr_del = curr->next;
} else if ((strcmp(curr->name, name) == 0) && curr->next == NULL) { // this means user is at the end of the list
prev->next = NULL;
} else if ((strcmp(curr->name, name) == 0) && curr->next != NULL){ // this means that element is in middle of list?
prev->next = curr->next;
} else { // in the case user is not in list?
return 1;
}
// case 3: the element is at the end of the list
free(user);
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. | null |
63c648825f004bf4898cfd1a | 2023-02-12T02:00:09.245Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
// case 1: user is at start of list
// case 2: user at middle of list
// case 3: user at end of list
// case 4: user does not exist
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
// loop through user's friends, for each friend, loop through their friends and remove the user friend
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) {
User *user2 = user->friends[i];
for (int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(user2->friends[j]->name, name) == 0){
user2->friends[j] = NULL;
break;
}
}
}
}
// don't forget to free the user
// // case 1: user at start of linked list
// if (user == *user_ptr_del) {
// *user_ptr_del = *user_ptr_del->next;
// }
// case 2 and 3
User *prev = NULL;
User *curr = *user_ptr_del;
while ((curr != NULL) && (strcmp(curr->name, name) != 0)) {
prev = curr;
curr = curr->next;
}
if (prev == NULL) {
*user_ptr_del = curr->next;
} else if ((strcmp(curr->name, name) == 0) && curr->next == NULL) { // this means user is at the end of the list
prev->next = NULL;
} else if ((strcmp(curr->name, name) == 0) && curr->next != NULL){ // this means that element is in middle of list?
prev->next = curr->next;
} else { // in the case user is not in list?
return 1;
}
// case 3: the element is at the end of the list
free(user);
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. | null |
63c648825f004bf4898cfd1a | 2023-02-12T02:01:20.564Z | Question from Code | v1 | Why doesn't the while loop work properly | int delete_user(const char *name, User **user_ptr_del) {
// case 1: user is at start of list
// case 2: user at middle of list
// case 3: user at end of list
// case 4: user does not exist
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
// loop through user's friends, for each friend, loop through their friends and remove the user friend
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) {
User *user2 = user->friends[i];
for (int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(user2->friends[j]->name, name) == 0){
user2->friends[j] = NULL;
break;
}
}
}
}
// don't forget to free the user
// // case 1: user at start of linked list
// if (user == *user_ptr_del) {
// *user_ptr_del = *user_ptr_del->next;
// }
// case 2 and 3
User *prev = NULL;
User *curr = *user_ptr_del;
while ((curr != NULL) && (strcmp(curr->name, name) != 0)) {
prev = curr;
curr = curr->next;
}
if (prev == NULL) {
*user_ptr_del = curr->next;
} else if ((strcmp(curr->name, name) == 0) && curr->next == NULL) { // this means user is at the end of the list
prev->next = NULL;
} else if ((strcmp(curr->name, name) == 0) && curr->next != NULL){ // this means that element is in middle of list?
prev->next = curr->next;
} else { // in the case user is not in list?
return 1;
}
// case 3: the element is at the end of the list
free(user);
return 0;
} | null | null |
63c648825f004bf4898cfd1a | 2023-02-12T02:07:07.759Z | Explain Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
// case 1: user is at start of list
// case 2: user at middle of list
// case 3: user at end of list
// case 4: user does not exist
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
// loop through user's friends, for each friend, loop through their friends and remove the user friend
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) {
User *user2 = user->friends[i];
for (int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(user2->friends[j]->name, name) == 0){
user2->friends[j] = NULL;
break;
}
}
}
}
// don't forget to free the user
// // case 1: user at start of linked list
// if (user == *user_ptr_del) {
// *user_ptr_del = *user_ptr_del->next;
// }
// case 2 and 3
User *prev = NULL;
User *curr = *user_ptr_del;
while ((curr != NULL) && (strcmp(curr->name, name) != 0)) {
prev = curr;
curr = curr->next;
}
if (prev == NULL) {
*user_ptr_del = curr->next;
} else if ((strcmp(curr->name, name) == 0) && curr->next == NULL) { // this means user is at the end of the list
prev->next = NULL;
} else if ((strcmp(curr->name, name) == 0) && curr->next != NULL){ // this means that element is in middle of list?
prev->next = curr->next;
} else { // in the case user is not in list?
return 1;
}
// case 3: the element is at the end of the list
free(user);
return 0;
} | null | null |
63c648825f004bf4898cfd1a | 2023-02-12T02:09:44.134Z | Question from Code | v1 | what is the error in the code | int delete_user(const char *name, User **user_ptr_del) {
// case 1: user is at start of list
// case 2: user at middle of list
// case 3: user at end of list
// case 4: user does not exist
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
// loop through user's friends, for each friend, loop through their friends and remove the user friend
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) {
User *user2 = user->friends[i];
for (int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(user2->friends[j]->name, name) == 0){
user2->friends[j] = NULL;
break;
}
}
}
}
// don't forget to free the user
// // case 1: user at start of linked list
// if (user == *user_ptr_del) {
// *user_ptr_del = *user_ptr_del->next;
// }
// case 2 and 3
User *prev = NULL;
User *curr = *user_ptr_del;
while ((curr != NULL) && (strcmp(curr->name, name) != 0)) {
prev = curr;
curr = curr->next;
}
if (prev == NULL) {
*user_ptr_del = curr->next;
} else if ((strcmp(curr->name, name) == 0) && curr->next == NULL) { // this means user is at the end of the list
prev->next = NULL;
} else if ((strcmp(curr->name, name) == 0) && curr->next != NULL){ // this means that element is in middle of list?
prev->next = curr->next;
} else { // in the case user is not in list?
return 1;
}
// case 3: the element is at the end of the list
free(user);
return 0;
} | null | null |
63c648825f004bf4898cfd1a | 2023-02-12T02:12:55.263Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
// case 1: user is at start of list
// case 2: user at middle of list
// case 3: user at end of list
// case 4: user does not exist
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
// loop through user's friends, for each friend, loop through their friends and remove the user friend
// THIS PART DOESN'T WORK PROPERLY: FIX LOGIC
// the while loop is not properly checking if the current user is the user to be deleted.
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) {
User *user2 = user->friends[i];
for (int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(user2->friends[j]->name, name) == 0){
user2->friends[j] = NULL;
break;
}
}
}
}
// don't forget to free the user
// case 1: user at start of linked list
if (user == *user_ptr_del) {
*user_ptr_del = (*user_ptr_del)->next;
}
// case 2 and 3
User *prev = NULL;
User *curr = *user_ptr_del;
while ((curr != NULL) && (strcmp(curr->name, name) != 0)) {
prev = curr;
curr = curr->next;
}
if (prev == NULL) {
*user_ptr_del = curr->next;
} else if ((strcmp(curr->name, name) == 0) && curr->next == NULL) { // this means user is at the end of the list
prev->next = NULL;
} else if ((strcmp(curr->name, name) == 0) && curr->next != NULL){ // this means that element is in middle of list?
prev->next = curr->next;
} else { // in the case user is not in list?
return 1;
}
// case 3: the element is at the end of the list
free(user);
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. | null |
63c648825f004bf4898cfd1a | 2023-02-12T02:13:47.283Z | Question from Code | v1 | why do I get a segmentation error? | int delete_user(const char *name, User **user_ptr_del) {
// case 1: user is at start of list
// case 2: user at middle of list
// case 3: user at end of list
// case 4: user does not exist
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
// loop through user's friends, for each friend, loop through their friends and remove the user friend
// THIS PART DOESN'T WORK PROPERLY: FIX LOGIC
// the while loop is not properly checking if the current user is the user to be deleted.
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) {
User *user2 = user->friends[i];
for (int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(user2->friends[j]->name, name) == 0){
user2->friends[j] = NULL;
break;
}
}
}
}
// don't forget to free the user
// case 1: user at start of linked list
if (user == *user_ptr_del) {
*user_ptr_del = (*user_ptr_del)->next;
}
// case 2 and 3
User *prev = NULL;
User *curr = *user_ptr_del;
while ((curr != NULL) && (strcmp(curr->name, name) != 0)) {
prev = curr;
curr = curr->next;
}
if (prev == NULL) {
*user_ptr_del = curr->next;
} else if ((strcmp(curr->name, name) == 0) && curr->next == NULL) { // this means user is at the end of the list
prev->next = NULL;
} else if ((strcmp(curr->name, name) == 0) && curr->next != NULL){ // this means that element is in middle of list?
prev->next = curr->next;
} else { // in the case user is not in list?
return 1;
}
// case 3: the element is at the end of the list
free(user);
return 0;
} | null | null |
63c648825f004bf4898cfd1a | 2023-02-12T22:31:55.974Z | Help Fix Code | v1 | null | int update_pic(User *user, const char *filename) {
if (strlen(filename) > MAX_NAME) {
return 2;
}
FILE *output_file = fopen(filename, "r");
if (output_file == NULL) {
fclose(output_file);
return 1;
} else {
strncpy(user->profile_pic, filename, MAX_NAME);
return 0;
}
} | /*
* Change the filename for the profile pic of the given user.
*
* Return:
* - 0 on success.
* - 1 if the file does not exist or cannot be opened.
* - 2 if the filename is too long.
*/ | null |
63c648825f004bf4898cfd1a | 2023-02-12T22:37:03.386Z | Help Fix Code | v1 | null | int update_pic(User *user, const char *filename) {
if (strlen(filename) > MAX_NAME) {
return 2;
}
FILE *output_file = fopen(filename, "r");
if (output_file == NULL) {
fclose(output_file);
return 1;
} else {
strncpy(user->profile_pic, filename, MAX_NAME);
return 0;
}
fclose(output_file);
} | /*
* 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.
*/ | null |
63c648825f004bf4898cfd1a | 2023-02-12T22:38:10.612Z | Help Fix Code | v1 | null | int update_pic(User *user, const char *filename) {
if (strlen(filename) > MAX_NAME) {
return 2;
}
FILE *output_file = fopen(filename, "r");
if (output_file == NULL) {
fclose(output_file);
return 1;
} else {
strncpy(user->profile_pic, filename, MAX_NAME);
fclose(output_file);
return 0;
}
} | /*
* Change the filename for the profile pic of the given user.
*
* Return:
* - 0 on success.
* - 1 if the file does not exist or cannot be opened.
* - 2 if the filename is too long.
*/ | null |
63c648825f004bf4898cfd1a | 2023-02-12T22:50:03.871Z | Help Fix Code | v1 | null | int print_user(const User *user) {
if (user == NULL) {
return 1;
}
// char filename[MAX_NAME];
// strncpy(filename, user->profile_pic, MAX_NAME);
// // need to add profile pic part
// FILE *file = fopen(filename, "r");
// char str[60];
// while (fgets(str, 60, file) != NULL) {
// printf("%s", str);
// }
// fclose(file);
char filename[MAX_NAME];
strncpy(filename, user->profile_pic, MAX_NAME);
FILE *fp = fopen(filename, "r");
char s[1];
// while (fgets(s, MAX_NAME))
fgets(s, 1, fp);
printf("%s", s);
fclose(fp);
printf("Name: %s\n", user->name);
printf("------------------------------------------\n");
printf("Friends:\n");
// getting all the friends
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) {
// User *friend = user->friends[i];
printf("%s\n", user->friends[i]->name);
}
}
printf("------------------------------------------\n");
printf("Posts:\n");
Post *post = user->first_post;
while (post != NULL) {
printf("From: %s\n", post->author);
printf("Date: %s\n", asctime(localtime(post->date)));
printf("%s\n", post->contents);
post = post->next;
}
if (post != NULL) {
printf("===\n");
}
printf("------------------------------------------\n");
return 0;
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL) {
return 2;
}
int friends = 0;
for (int i = 0; i < MAX_FRIENDS && author->friends[i] != NULL; i++) {
if (strcmp(author->friends[i]->name, target->name) == 0) {
friends = 1;
break;
}
}
if (friends == 0) {
return 1;
}
Post *new_post = malloc(sizeof(Post));
if (new_post == NULL) {
perror("malloc");
exit(1);
}
strncpy(new_post->author, author->name, MAX_NAME);
new_post->contents = contents;
new_post->date = malloc(sizeof(time_t));
if (new_post->date == NULL) {
perror("malloc");
exit(1);
}
time(new_post->date);
new_post->next = target->first_post;
target->first_post = new_post;
return 0;
} | /*
* 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.
*/ | null |
63c648825f004bf4898cfd1a | 2023-02-12T23:56:00.168Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
// case 1: user is at start of list
// case 2: user at middle of list
// case 3: user at end of list
// case 4: user does not exist
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
// loop through user's friends, for each friend, loop through their friends and remove the user friend
// THIS PART DOESN'T WORK PROPERLY: FIX LOGIC
// the while loop is not properly checking if the current user is the user to be deleted.
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) {
User *user2 = user->friends[i];
for (int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(user2->friends[j]->name, name) == 0){
user2->friends[j] = NULL;
}
}
}
}
// don't forget to free the user
// case 1: user at start of linked list
if (user == *user_ptr_del) {
*user_ptr_del = (*user_ptr_del)->next;
}
// case 2 and 3
User *prev = NULL;
User *curr = *user_ptr_del;
while ((curr != NULL) && (strcmp(curr->name, name) != 0)) {
prev = curr;
curr = curr->next;
}
if (prev == NULL) {
*user_ptr_del = curr->next;
} else if ((strcmp(curr->name, name) == 0) && curr->next == NULL) { // this means user is at the end of the list
prev->next = NULL;
} else if ((strcmp(curr->name, name) == 0) && curr->next != NULL){ // this means that element is in middle of list?
prev->next = curr->next;
} else { // in the case user is not in list?
return 1;
}
// case 3: the element is at the end of the list
// free the posts
Post *post = user->first_post;
while (post != NULL) {
free(post->date);
free(post->contents);
free(post);
post = post->next;
}
free(user);
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.
*/ | null |
63c648825f004bf4898cfd1a | 2023-02-13T00:01:38.664Z | Question from Code | v1 | why is the user not being deleted correctly from the linked list? | int delete_user(const char *name, User **user_ptr_del) {
// case 1: user is at start of list
// case 2: user at middle of list
// case 3: user at end of list
// case 4: user does not exist
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
// loop through user's friends, for each friend, loop through their friends and remove the user friend
// THIS PART DOESN'T WORK PROPERLY: FIX LOGIC
// the while loop is not properly checking if the current user is the user to be deleted.
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) {
User *user2 = user->friends[i];
for (int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(user2->friends[j]->name, name) == 0){
user2->friends[j] = NULL;
}
}
}
}
// don't forget to free the user
// case 1: user at start of linked list
if (user == *user_ptr_del) {
*user_ptr_del = (*user_ptr_del)->next;
}
// case 2 and 3
User *prev = NULL;
User *curr = *user_ptr_del;
while ((curr != NULL) && (strcmp(curr->name, name) != 0)) {
prev = curr;
curr = curr->next;
}
if (prev == NULL) {
*user_ptr_del = curr->next;
} else if ((strcmp(curr->name, name) == 0) && curr->next == NULL) { // this means user is at the end of the list
prev->next = NULL;
} else if ((strcmp(curr->name, name) == 0) && curr->next != NULL){ // this means that element is in middle of list?
prev->next = curr->next;
} else { // in the case user is not in list?
return 1;
}
// case 3: the element is at the end of the list
// free the posts
Post *post = user->first_post;
while (post != NULL) {
free(post->date);
free(post->contents);
free(post);
post = post->next;
}
free(user);
return 0;
} | null | null |
63c648825f004bf4898cfd1a | 2023-02-13T00:02:05.644Z | Question from Code | v1 | why is while loop is not properly checking if the current user is the user to be deleted | int delete_user(const char *name, User **user_ptr_del) {
// case 1: user is at start of list
// case 2: user at middle of list
// case 3: user at end of list
// case 4: user does not exist
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
// loop through user's friends, for each friend, loop through their friends and remove the user friend
// THIS PART DOESN'T WORK PROPERLY: FIX LOGIC
// the while loop is not properly checking if the current user is the user to be deleted.
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) {
User *user2 = user->friends[i];
for (int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(user2->friends[j]->name, name) == 0){
user2->friends[j] = NULL;
}
}
}
}
// don't forget to free the user
// case 1: user at start of linked list
if (user == *user_ptr_del) {
*user_ptr_del = (*user_ptr_del)->next;
}
// case 2 and 3
User *prev = NULL;
User *curr = *user_ptr_del;
while ((curr != NULL) && (strcmp(curr->name, name) != 0)) {
prev = curr;
curr = curr->next;
}
if (prev == NULL) {
*user_ptr_del = curr->next;
} else if ((strcmp(curr->name, name) == 0) && curr->next == NULL) { // this means user is at the end of the list
prev->next = NULL;
} else if ((strcmp(curr->name, name) == 0) && curr->next != NULL){ // this means that element is in middle of list?
prev->next = curr->next;
} else { // in the case user is not in list?
return 1;
}
// case 3: the element is at the end of the list
// free the posts
Post *post = user->first_post;
while (post != NULL) {
free(post->date);
free(post->contents);
free(post);
post = post->next;
}
free(user);
return 0;
} | null | null |
63c648825f004bf4898cfd1a | 2023-02-13T00:02:31.827Z | Question from Code | v1 | why am I getting a segmentation error | int delete_user(const char *name, User **user_ptr_del) {
// case 1: user is at start of list
// case 2: user at middle of list
// case 3: user at end of list
// case 4: user does not exist
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
// loop through user's friends, for each friend, loop through their friends and remove the user friend
// THIS PART DOESN'T WORK PROPERLY: FIX LOGIC
// the while loop is not properly checking if the current user is the user to be deleted.
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) {
User *user2 = user->friends[i];
for (int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(user2->friends[j]->name, name) == 0){
user2->friends[j] = NULL;
}
}
}
}
// don't forget to free the user
// case 1: user at start of linked list
if (user == *user_ptr_del) {
*user_ptr_del = (*user_ptr_del)->next;
}
// case 2 and 3
User *prev = NULL;
User *curr = *user_ptr_del;
while ((curr != NULL) && (strcmp(curr->name, name) != 0)) {
prev = curr;
curr = curr->next;
}
if (prev == NULL) {
*user_ptr_del = curr->next;
} else if ((strcmp(curr->name, name) == 0) && curr->next == NULL) { // this means user is at the end of the list
prev->next = NULL;
} else if ((strcmp(curr->name, name) == 0) && curr->next != NULL){ // this means that element is in middle of list?
prev->next = curr->next;
} else { // in the case user is not in list?
return 1;
}
// case 3: the element is at the end of the list
// free the posts
Post *post = user->first_post;
while (post != NULL) {
free(post->date);
free(post->contents);
free(post);
post = post->next;
}
free(user);
return 0;
} | null | null |
63c648825f004bf4898cfd1a | 2023-03-12T01:48:05.290Z | Question from Code | v2 | Why am I getting a segmentation fault? | while (fgets(curr_line, MAXLINE, fp) != NULL) {
if ((curr_line[0] != ' ') && (curr_line[0] != '\t') && (strchr(curr_line, '#') == NULL) && (strchr(curr_line, ':') != NULL)) {
char *target_token = strtok(curr_line, ":");
char *target_name = target_token;
Dependency *curr_dependency = NULL;
while (target_token != NULL) {
target_token = strtok(NULL, " ");
Rule *target_rule = curr_rule;
while (target_rule != NULL && strcmp(target_rule->target, target_name) != 0) {
target_rule = target_rule->next_rule;
}
Rule *dependency_rule = curr_rule;
while (dependency_rule != NULL && strcmp(dependency_rule->target, target_token) != 0) {
dependency_rule = dependency_rule->next_rule;
}
Dependency *new_dependency = malloc(sizeof(Dependency));
new_dependency->rule = dependency_rule;
new_dependency->next_dep = NULL;
if (curr_dependency == NULL) {
target_rule->dependencies = new_dependency;
curr_dependency = new_dependency;
} else {
curr_dependency->next_dep = new_dependency;
}
}
} | null | null |
63c648825f004bf4898cfd1f | 2023-01-30T21:22:50.128Z | Help Fix Code | v1 | null | int goodfiles = 0;
while (scanf("%s, %d, %*s, %*s, %*d, %*s, %*d, %*s, %*s\n", dir, &currsize) != EOF) {
if (dir[0] == '-') {
if (currsize > neededsize) {
goodfiles = goodfiles + 1;
goodfiles = goodfiles - x;
}
}
} | count the number of files with size greater than neededsize
| null |
63c648825f004bf4898cfd1f | 2023-02-03T21:38:09.450Z | Question from Code | v1 | Why doesn't it change s | int truncate(char *s, int n) {
int l = strlen(s);
if (l > n) {
s[n] = '\0';
return l - n;
}
else {
// then strlen(s) <= n
return 0;
}
}
| null | null |
63c648825f004bf4898cfd1f | 2023-02-03T21:38:49.789Z | Question from Code | v1 | Why isn't a string returned | #include <stdlib.h>
#include <stdio.h>
#include <string.h>
/*
Write a function named truncate() that takes a string s and a
non-negative integer n. If s has more than n characters (not including the
null terminator), the function should truncate s at n characters and
return the number of characters that were removed. If s has n or
fewer characters, s is unchanged and the function returns 0. For example,
if s is the string "function" and n is 3, then truncate() changes s to
the string "fun" and returns 5.
*/
int truncate(char *s, int n) {
int l = strlen(s);
if (l > n) {
s[n] = '\0';
return l - n;
}
else {
// then strlen(s) <= n
return 0;
}
}
int main(int argc, char **argv) {
/* Do not change the main function */
if (argc != 3) {
fprintf(stderr, "Usage: truncate number string\n");
return 1;
}
int amt = strtol(argv[1], NULL, 10);
char *target = argv[2];
int soln_val = truncate(target, amt);
printf("%d %s\n", soln_val, target);
return 0;
}
| null | null |
63c648825f004bf4898cfd1f | 2023-02-03T21:39:16.351Z | Question from Code | v1 | Why isn't a string printed? | #include <stdlib.h>
#include <stdio.h>
#include <string.h>
/*
Write a function named truncate() that takes a string s and a
non-negative integer n. If s has more than n characters (not including the
null terminator), the function should truncate s at n characters and
return the number of characters that were removed. If s has n or
fewer characters, s is unchanged and the function returns 0. For example,
if s is the string "function" and n is 3, then truncate() changes s to
the string "fun" and returns 5.
*/
int truncate(char *s, int n) {
int l = strlen(s);
if (l > n) {
s[n] = '\0';
return l - n;
}
else {
// then strlen(s) <= n
return 0;
}
}
int main(int argc, char **argv) {
/* Do not change the main function */
if (argc != 3) {
fprintf(stderr, "Usage: truncate number string\n");
return 1;
}
int amt = strtol(argv[1], NULL, 10);
char *target = argv[2];
int soln_val = truncate(target, amt);
printf("%d %s\n", soln_val, target);
return 0;
}
| null | null |
63c648825f004bf4898cfd1f | 2023-02-03T21:39:40.348Z | Question from Code | v1 | Why doesn't main print a string? | #include <stdlib.h>
#include <stdio.h>
#include <string.h>
/*
Write a function named truncate() that takes a string s and a
non-negative integer n. If s has more than n characters (not including the
null terminator), the function should truncate s at n characters and
return the number of characters that were removed. If s has n or
fewer characters, s is unchanged and the function returns 0. For example,
if s is the string "function" and n is 3, then truncate() changes s to
the string "fun" and returns 5.
*/
int truncate(char *s, int n) {
int l = strlen(s);
if (l > n) {
s[n] = '\0';
return l - n;
}
else {
// then strlen(s) <= n
return 0;
}
}
int main(int argc, char **argv) {
/* Do not change the main function */
if (argc != 3) {
fprintf(stderr, "Usage: truncate number string\n");
return 1;
}
int amt = strtol(argv[1], NULL, 10);
char *target = argv[2];
int soln_val = truncate(target, amt);
printf("%d %s\n", soln_val, target);
return 0;
}
| null | null |
63c648825f004bf4898cfd1f | 2023-03-10T16:28:33.650Z | General Question | v2 | man wifexited | 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.