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
⌀ |
---|---|---|---|---|---|---|---|
63c648e15f004bf4898d0099 | 2023-02-03T05:38:06.124Z | Question from Code | v1 | it does not cap the final string at maximum length of 19. | int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: greeting message name\n");
return 1;
}
char greeting[20];
char *name = argv[2];
// Your code goes here
int len = strlen(argv[1]);
int space_left = 19 - len;
greeting[0] = '\0';
strncat(greeting, argv[1], 19);
if (space_left > 0) {
strcat(greeting, " ");
space_left--;
}
strncat(greeting, name, space_left);
printf("%s\n", greeting);
return 0;
} | null | null |
63c648e15f004bf4898d0099 | 2023-02-03T05:55:34.384Z | Question from Code | v1 | why does it not concatenate name into greeting at the end? | int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: greeting message name\n");
return 1;
}
char greeting[20];
char *name = argv[2];
// Your code goes here
strcat(argv[1], " ");
int len = strlen(argv[1]);
int space_left = 19 - len;
greeting[0] = '\0';
strncat(greeting, argv[1], 19);
strncat(greeting, name, space_left);
printf("%s\n", greeting);
return 0;
} | null | null |
63c648e15f004bf4898d0099 | 2023-02-03T05:57:13.939Z | Question from Code | v1 | Why can't I access name? When I print it out it does not print anything. | int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: greeting message name\n");
return 1;
}
char greeting[20];
char *name = argv[2];
// Your code goes here
strcat(argv[1], " ");
int len = strlen(argv[1]);
int space_left = 19 - len;
greeting[0] = '\0';
strncat(greeting, argv[1], 19);
strncat(greeting, name, space_left);
printf("%s\n", greeting);
return 0;
} | null | null |
63c648e15f004bf4898d0099 | 2023-02-03T05:59:27.641Z | Question from Code | v1 | Why can't I access name? When I print it out it does not print anything. I have already tried dereferencing it. | int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: greeting message name\n");
return 1;
}
char greeting[20];
char *name = argv[2];
// Your code goes here
strcat(argv[1], " ");
int len = strlen(argv[1]);
int space_left = 19 - len;
greeting[0] = '\0';
strncat(greeting, argv[1], 19);
strncat(greeting, name, space_left);
printf("%s\n", greeting);
return 0;
} | null | null |
63c648e15f004bf4898d0099 | 2023-02-10T20:08:23.889Z | Question from Code | v1 | what is wrong with my code? | #include <stdio.h>
#include <stdlib.h>
#include "bitmap.h"
/*
* Read in the location of the pixel array, the image width, and the image
* height in the given bitmap file.
*/
void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) {
fseek(image, 10, SEEK_SET);
fread(pixel_array_offset, sizeof(int), 1, image);
fseek(image, 18, SEEK_SET);
fread(width, sizeof(int), 1, image);
fseek(image, 22, SEEK_SET);
fread(height, sizeof(int), 1, image);
}
/*
* 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) {
struct pixel **p = malloc(height * sizeof(struct pixel *));
int i;
fseek(image, pixel_array_offset, SEEK_SET);
for (i = 0; i < height; i++) {
p[i] = malloc(width * sizeof(struct pixel));
int j;
for (j = 0; j < width; j++) {
fread(p[i][j], 1, 3, image);
fseek(image, 3, SEEK_CUR);
}
}
return p;
}
/*
* 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 |
63c648e15f004bf4898d0099 | 2023-02-10T20:09:56.832Z | Question from Code | v1 | why should I use `fread(&p[i][j], 1, 3, image);` ? | #include <stdio.h>
#include <stdlib.h>
#include "bitmap.h"
/*
* Read in the location of the pixel array, the image width, and the image
* height in the given bitmap file.
*/
void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) {
fseek(image, 10, SEEK_SET);
fread(pixel_array_offset, sizeof(int), 1, image);
fseek(image, 18, SEEK_SET);
fread(width, sizeof(int), 1, image);
fseek(image, 22, SEEK_SET);
fread(height, sizeof(int), 1, image);
}
/*
* 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) {
struct pixel **p = malloc(height * sizeof(struct pixel *));
int i;
fseek(image, pixel_array_offset, SEEK_SET);
for (i = 0; i < height; i++) {
p[i] = malloc(width * sizeof(struct pixel));
int j;
for (j = 0; j < width; j++) {
fread(p[i][j], 1, 3, image);
fseek(image, 3, SEEK_CUR);
}
}
return p;
}
/*
* 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 |
63c648e15f004bf4898d0099 | 2023-02-10T20:25:48.287Z | Question from Code | v1 | Is the fread and fseek part correct? | #include <stdio.h>
#include <stdlib.h>
#include "bitmap.h"
/*
* Read in the location of the pixel array, the image width, and the image
* height in the given bitmap file.
*/
void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) {
fseek(image, 10, SEEK_SET);
fread(pixel_array_offset, sizeof(int), 1, image);
fseek(image, 18, SEEK_SET);
fread(width, sizeof(int), 1, image);
fseek(image, 22, SEEK_SET);
fread(height, sizeof(int), 1, image);
}
/*
* 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) {
struct pixel **p = malloc(height * sizeof(struct pixel *));
int i;
fseek(image, pixel_array_offset, SEEK_SET);
for (i = 0; i < height; i++) {
p[i] = malloc(width * sizeof(struct pixel));
int j;
for (j = 0; j < width; j++) {
fread(&(p[i][j].red), 1, 1, image);
fseek(image, 1, SEEK_CUR);
fread(&(p[i][j].blue), 1, 1, image);
fseek(image, 1, SEEK_CUR);
fread(&(p[i][j].green), 1, 1, image);
fseek(image, 1, SEEK_CUR);
}
}
return p;
}
/*
* 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 |
63c648e15f004bf4898d0099 | 2023-02-10T20:26:09.549Z | Question from Code | v1 | What's wrong with the fread and fseek part? | #include <stdio.h>
#include <stdlib.h>
#include "bitmap.h"
/*
* Read in the location of the pixel array, the image width, and the image
* height in the given bitmap file.
*/
void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) {
fseek(image, 10, SEEK_SET);
fread(pixel_array_offset, sizeof(int), 1, image);
fseek(image, 18, SEEK_SET);
fread(width, sizeof(int), 1, image);
fseek(image, 22, SEEK_SET);
fread(height, sizeof(int), 1, image);
}
/*
* 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) {
struct pixel **p = malloc(height * sizeof(struct pixel *));
int i;
fseek(image, pixel_array_offset, SEEK_SET);
for (i = 0; i < height; i++) {
p[i] = malloc(width * sizeof(struct pixel));
int j;
for (j = 0; j < width; j++) {
fread(&(p[i][j].red), 1, 1, image);
fseek(image, 1, SEEK_CUR);
fread(&(p[i][j].blue), 1, 1, image);
fseek(image, 1, SEEK_CUR);
fread(&(p[i][j].green), 1, 1, image);
fseek(image, 1, SEEK_CUR);
}
}
return p;
}
/*
* 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 |
63c648e15f004bf4898d0099 | 2023-02-12T03:35:40.698Z | Question from Code | v1 | what does this output? | #define SUPERVISOR(regular) regular + 5
int main() {
int regular_pay = 20;
int hours_worked = 10;
printf("pay is %d\n", (hours_worked * SUPERVISOR(regular_pay)));
// rest omitted
}
| null | null |
63c648e15f004bf4898d0099 | 2023-02-12T03:38:40.436Z | Question from Code | v1 | why is the output not "pay is 250" | #define SUPERVISOR(regular) regular + 5
int main() {
int regular_pay = 20;
int hours_worked = 10;
printf("pay is %d\n", (hours_worked * SUPERVISOR(regular_pay)));
// rest omitted
}
| null | null |
63c648e15f004bf4898d0099 | 2023-02-12T03:43:00.240Z | Question from Code | v1 | why was the answer to this "pay is 2050" | #define SUPERVISOR(regular) regular + 5
int main() {
int regular_pay = 20;
int hours_worked = 10;
printf("pay is %d\n", (hours_worked * SUPERVISOR(regular_pay)));
// rest omitted
} | null | null |
63c648e15f004bf4898d0099 | 2023-02-12T03:43:25.896Z | Question from Code | v1 | why is the output of this code "pay is 2050" | #define SUPERVISOR(regular) regular + 5
int main() {
int regular_pay = 20;
int hours_worked = 10;
printf("pay is %d\n", (hours_worked * SUPERVISOR(regular_pay)));
// rest omitted
} | null | null |
63c648e15f004bf4898d0099 | 2023-02-14T02:15:30.233Z | Question from Code | v1 | what will line 2 be after pre-processing? | #define MAXNAME = 32;
char name[MAXNAME]; | null | null |
63c648e15f004bf4898d0099 | 2023-02-14T02:18:56.359Z | Explain Code | v1 | null | #define MAXNAME = 32;
char name[MAXNAME]; | null | null |
63c648e15f004bf4898d0099 | 2023-02-14T02:24:18.151Z | Question from Code | v1 | why is this wrong | #define MAXNAME = 32;
char name[MAXNAME]; | null | null |
63c648e15f004bf4898d0099 | 2023-02-14T02:32:15.860Z | Question from Code | v1 | what will this become when the program is passed through the preprocessor? | #define MAXNAME = 32;
int main() {
char name[MAXNAME];
} | null | null |
63c648e15f004bf4898d0099 | 2023-02-14T04:00:58.282Z | Question from Code | v1 | how do i complete this function according to the comment? | /*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
return -1;
} | null | null |
63c648e15f004bf4898d0099 | 2023-02-14T04:01:35.677Z | Question from Code | v1 | Complete this function for me. | /*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
return -1;
}/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
return -1;
} | null | null |
63c648e15f004bf4898d0099 | 2023-02-14T07:02:39.052Z | Question from Code | v1 | what is wrong with this code? | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) + 1 > MAX_NAME) { // ???
return 2;
}
User *u = malloc(sizeof(User));
// Empty user list
if (**user_ptr_add == NULL) {
**user_ptr_add = u;
}
// At least 1 existing user
while (**user_ptr_add.next != NULL) {
user_ptr_add = **user_ptr_add.next;
}
return -1;
} | null | null |
63c648e15f004bf4898d0099 | 2023-02-14T21:23:22.213Z | General Question | v1 | how can you check if a file exists in c? | null | null | null |
63c648e15f004bf4898d0099 | 2023-02-14T21:45:18.629Z | General Question | v1 | how do you append to an array where you know the size but don't know how many elements have already been filled? | null | null | null |
63c648e15f004bf4898d0099 | 2023-02-14T21:50:16.038Z | General Question | v1 | how do you find the first empty space in an array in c? | null | null | null |
63c648e15f004bf4898d0099 | 2023-02-14T22:17:01.312Z | General Question | v1 | how do you print out the contents of a text file? | null | null | null |
63c648e15f004bf4898d0099 | 2023-02-14T22:49:05.343Z | General Question | v1 | how do you print a time_t value? | null | null | null |
63c648e15f004bf4898d0099 | 2023-02-15T00:39:43.156Z | General Question | v1 | In C, when you do "int *curr = other", does this set curr to other's value, or create an alias? | null | null | null |
63c648e15f004bf4898d0099 | 2023-02-15T01:30:29.995Z | Question from Code | v1 | why does this only print the last friend | for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) { // Only print this pointer's corresponding friend if the pointer is not null
printf("%s\n", user->friends[i]->name);
}
} | null | null |
63c648e15f004bf4898d0099 | 2023-02-15T01:40:18.766Z | Question from Code | v1 | why doesn't this return the user with the matching name | for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) { // Only print this pointer's corresponding friend if the pointer is not null
printf("%s\n", user->friends[i]->name);
}
}User *find_user(const char *name, const User *head) {
// First, make sure users exist
if (head != NULL) {
// Start from the head
User *curr = (User *) head;
// Traverse linked list until reaching null
while (curr != NULL) {
if (strcmp(curr->name, name) == 0) { // Check if the current username matches
return curr;
}
curr = curr->next; // If not, link to next user and repeat
}
}
return NULL;
}
| null | null |
63c648e15f004bf4898d0099 | 2023-02-15T01:40:48.780Z | Question from Code | v1 | why doesn't this return the user with the matching name | User *find_user(const char *name, const User *head) {
// First, make sure users exist
if (head != NULL) {
// Start from the head
User *curr = (User *) head;
// Traverse linked list until reaching null
while (curr != NULL) {
if (strcmp(curr->name, name) == 0) { // Check if the current username matches
return curr;
}
curr = curr->next; // If not, link to next user and repeat
}
}
return NULL;
}
| null | null |
63c648e15f004bf4898d0099 | 2023-02-15T02:23:18.666Z | Question from Code | v1 | why is this getting a segmentation fault? | int make_post(const User *author, User *target, char *contents) {
// Test if either author or target doesn't exist
if (author == NULL || target == NULL) {
return 2;
}
// Check if the author and target are not friends
if (are_friends(author, target) == 1) {
return 1;
}
// Create post
Post *p = malloc(sizeof(Post)); // Allocate memory
strcpy(p->author, author->name); // Author
strcpy(p->contents, contents); // Contents
time(p->date); // Date
// Insert the new post at the beginning of recipient's post list
p->next = target->first_post;
target->first_post = p;
return 0;
} | null | null |
63c648e15f004bf4898d0099 | 2023-02-15T02:23:46.079Z | Question from Code | v1 | why is this getting a segmentation fault? | int make_post(const User *author, User *target, char *contents) {
// Test if either author or target doesn't exist
if (author == NULL || target == NULL) {
return 2;
}
// Create post
Post *p = malloc(sizeof(Post)); // Allocate memory
strcpy(p->author, author->name); // Author
strcpy(p->contents, contents); // Contents
time(p->date); // Date
// Insert the new post at the beginning of recipient's post list
p->next = target->first_post;
target->first_post = p;
return 0;
} | null | null |
63c648e15f004bf4898d0099 | 2023-02-15T02:52:13.043Z | General Question | v1 | when you use malloc to make space for a struct that contains other structs as attributes, do you also have to use malloc for those structs inside it? | null | null | null |
63c648e15f004bf4898d0099 | 2023-02-15T16:55:31.418Z | Question from Code | v1 | Would this code successfully free the user pointed to by tmp without breaking the linked list? | User *tmp = *curr;
*curr = (*curr)->next;
free(tmp); | null | null |
63c648e15f004bf4898d0099 | 2023-02-15T16:56:00.785Z | Help Fix Code | v1 | null | User *tmp = *curr;
*curr = (*curr)->next;
free(tmp); | Free the memory associated with the tmp user without breaking the linked list. | null |
63c648e15f004bf4898d0099 | 2023-02-15T17:02:00.631Z | Question from Code | v1 | For this struct, how would you allocate memorize to initialize a user? |
typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[MAX_FRIENDS];
struct user *next;
} User;
| null | null |
63c648e15f004bf4898d0099 | 2023-02-15T22:03:07.148Z | Question from Code | v1 | when initializing a user object based on the definition in the code, do I have to malloc space for the first_post attribute or can I simply set it to null? | typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[MAX_FRIENDS];
struct user *next;
} User;
typedef struct post {
char author[MAX_NAME];
char *contents;
time_t *date;
struct post *next;
} Post; | null | null |
63c648e15f004bf4898d0099 | 2023-02-15T22:04:01.885Z | Question from Code | v1 | when initializing a new post object, why do I have to use malloc on the date attribute but not on next? | typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[MAX_FRIENDS];
struct user *next;
} User;
typedef struct post {
char author[MAX_NAME];
char *contents;
time_t *date;
struct post *next;
} Post; | null | null |
63c648e15f004bf4898d0099 | 2023-04-04T23:02:41.221Z | General Question | v2 | In C, how do i break out of two while loops at once? | null | null | null |
63c648e15f004bf4898d0099 | 2023-04-05T16:12:10.896Z | General Question | v2 | do we have to error check snprintf? is it a system call (or does it use system calls)? And if so, how? | null | null | null |
63c648e15f004bf4898d0099 | 2023-04-05T16:13:59.462Z | General Question | v2 | can you perror asprintf? | null | null | null |
63c648e15f004bf4898d0099 | 2023-04-05T16:14:35.633Z | General Question | v2 | can you perror snprintf? | null | null | null |
63c648e25f004bf4898d009e | 2023-01-18T03:58:03.877Z | General Question | v1 | subprocess.TimeoutExpired: Command '['./phone_loop']' timed out after 2 seconds | null | null | null |
63c648e25f004bf4898d009e | 2023-01-18T03:59:09.198Z | General Question | v1 | subprocess.TimeoutExpired: Command '['./phone_loop']' timed out after 2 seconds in markus test | null | null | null |
63c648e25f004bf4898d009e | 2023-01-26T00:32:09.888Z | Question from Code | v1 | why is line 34 adding a huge number? | #include <stdio.h>
int populate_array(int, int *);
int check_sin(int *);
int main() {
int sin_array[9];
int sin = 820320726;
populate_array(sin, sin_array);
check_sin(sin_array);
}
int populate_array(int sin, int *sin_array) {
int digits = 0;
int num = sin;
while (num > 0) {
sin_array[9 - digits] = num % 10;
num = num / 10;
digits++;
}
if (digits != 9) {
return 1;
}
return 0;
}
int check_sin(int *sin_array) {
int sum = 0;
for (int i = 0; i < 9; i++) {
if (i % 2 == 1) {
sin_array[i] *= 2;
}
if (sin_array[i] >= 10) {
sum += (sin_array[i] % 10);
sum += (sin_array[i] / 10);
}
else {
sum += sin_array[i];
}
}
if (sum % 10 == 0) {
return 0;
}
return 1;
} | null | null |
63c648e25f004bf4898d009e | 2023-01-26T02:37:15.765Z | 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 *, char *);
int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
// TODO: Process command line arguments.
int accepted_files;
int required_size = strtol(argv[1], NULL, 10);
int file_size;
char* required_permissions = argv[2];
char* permissions = "---------";
// TODO: Call check_permissions as part of your solution to count the files to
// compute and print the correct value.
while (scanf("%10s %*d %*s %*s %d %*s %*d %*s %*s", permissions, &file_size) == 2) {
if (permissions[0] == 'd' ) {
}
else if (argc == 3) {
if (check_permissions(permissions, required_permissions) == 0 && (file_size > required_size)) {
accepted_files++;
}
}
else if (argc == 2) {
if (file_size > required_size) {
accepted_files++;
}
}
}
return accepted_files;
}
int check_permissions(char* permissions, char* required) {
for (int i = 0; i < 9; i++) {
if (required[i] == 'r' || required[i] == 'w' || required[i] == 'x') {
if (permissions[i+1] == '-') {
return 1;
}
}
}
return 0;
} | count the number of files that are bigger than the entered size and also match the permissions if given | null |
63c648e25f004bf4898d009e | 2023-02-12T21:27:58.798Z | Question from Code | v1 | how do i stop segmentation faults when deleting the first node in the linked list | int delete_user(const char *name, User **user_ptr_del) {
if (find_user(name, *user_ptr_del) == NULL) {
return 1;
}
User *curr = *user_ptr_del;
char *friend_name;
while (curr != NULL) {
for (int i = 0; i < MAX_FRIENDS; i++) {
friend_name = (curr->friends)[i]->name;
if (friend_name != NULL) {
if (strcmp(friend_name, name) == 0) {
(curr->friends)[i] = NULL;
}
}
}
curr = curr->next;
}
curr = *user_ptr_del;
//traverses curr to the node before the user to be deleted
while (curr->next != NULL) {
if (strcmp((curr->next)->name, name) == 0) {
break;
}
curr = curr->next;
}
free(curr->next);
curr->next = (curr->next)->next;
return 0;
} | null | null |
63c648e25f004bf4898d009e | 2023-02-12T21:35:21.219Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
if (find_user(name, *user_ptr_del) == NULL) {
return 1;
}
User *curr = *user_ptr_del;
char *friend_name;
while (curr != NULL) {
for (int i = 0; i < MAX_FRIENDS; i++) {
friend_name = (curr->friends)[i]->name;
if (friend_name != NULL) {
if (strcmp(friend_name, name) == 0) {
(curr->friends)[i] = NULL;
}
}
}
curr = curr->next;
}
curr = *user_ptr_del;
//traverses curr to the node before the user to be deleted
while (curr->next != NULL) {
if (strcmp((curr->next)->name, name) == 0) {
break;
}
curr = curr->next;
}
free(curr->next);
curr->next = (curr->next)->next;
return 0;
} | delete the node with name name from the linked list pointed to by user_ptr_del
| null |
63c648e25f004bf4898d009e | 2023-03-10T02:33:36.142Z | Question from Code | v2 | why is read returning 0 | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLINE 256
#define MAX_PASSWORD 10
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
int status;
int fd[2];
pipe(fd);
pid_t ret = fork();
if (ret < 0) {
perror("fork");
exit(1);
}
else if (ret > 0) {
close(fd[0]);
if (write(fd[1], user_id, MAX_PASSWORD) == -1 ) {
perror("write to pipe");
}
if (write(fd[1], password, MAX_PASSWORD) == -1 ) {
perror("write to pipe");
}
close(fd[1]);
if ((ret = wait(&status)) == -1) {
perror("wait");
exit(1);
}
if (WIFEXITED(status)) {
int exit_status = WEXITSTATUS(status);
if (exit_status == 0) {
printf(SUCCESS);
}
else if (exit_status == 2) {
printf(INVALID);
}
else if (exit_status == 3){
printf(NO_USER);
}
}
}
else {
close(fd[1]);
dup2(fd[0], fileno(stdin));
read(fd[0], user_id, MAX_PASSWORD);
read(fd[0], password, MAX_PASSWORD);
close(fd[0]);
execl("./validate", "validate", user_id, password, NULL);
}
return 0;
} | null | null |
63c648e35f004bf4898d00a8 | 2023-01-17T17:53:12.589Z | General Question | v1 | what is the data type of a string in c | null | null | null |
63c648e35f004bf4898d00a8 | 2023-01-17T17:54:25.439Z | General Question | v1 | what is the type of string in C | null | null | null |
63c648e35f004bf4898d00a8 | 2023-01-17T17:57:14.982Z | General Question | v1 | array of characters data type in c | null | null | null |
63c648e35f004bf4898d00ad | 2023-01-29T09:15:41.582Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
// Implement a helper named check_permissions that matches the prototype below.
int check_permissions(char *input, char *required) {
for (int i = 0; i < 9; i++) {
if (required[i] != '-' && input[i] != required[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;
}
// Process command line arguments.
int result = 2;
char permission[32];
int size;
int sum = 0;
scanf("%*s %*s");
if (argc == 2) {
while (result == 2) {
result = scanf("%s %*s %*s %*s %d %*s %*s %*s %*s", permission, &size);
if (size > strtol(argv[1], NULL, 10)) {
sum += 1;
}
}
printf("%d\n", sum);
}
else {
while (result == 2) {
result = scanf("%s %*s %*s %*s %d %*s %*s %*s %*s", permission, &size);
// Call check_permissions as part of your solution to count the files to
// compute and print the correct value.
if (check_permissions(permission, argv[2]) == 0 && size > strtol(argv[1], NULL, 10)) {
sum += 1;
}
}
printf("%d\n", sum);
}
return 0;
}
| inspect the output from the ls -l command and print the number of files larger than a specified size | null |
63c648e35f004bf4898d00ad | 2023-01-29T09:20:36.900Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
// Implement a helper named check_permissions that matches the prototype below.
int check_permissions(char *input, char *required) {
for (int i = 0; i < 9; i++) {
if (required[i] != '-' && input[i] != required[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;
}
// Process command line arguments.
int result = 2;
char permission[32];
int size;
int sum = 0;
scanf("%*s %*s");
if (argc == 2) {
while (result == 2) {
result = scanf("%s %*s %*s %*s %d %*s %*s %*s %*s", permission, &size);
if (size > strtol(argv[1], NULL, 10)) {
sum += 1;
}
}
printf("%d\n", sum);
}
else {
while (result == 2) {
result = scanf("%s %*s %*s %*s %d %*s %*s %*s %*s", permission, &size);
// Call check_permissions as part of your solution to count the files to
// compute and print the correct value.
if (check_permissions(permission, argv[2]) == 0 && size > strtol(argv[1], NULL, 10)) {
sum += 1;
}
}
printf("%d\n", sum);
}
return 0;
}
| inspect the output from the ls -l command and print the number of files larger than a specified size
program prints to standard output the number of regular files in the input listing that are larger than the cutoff size supplied by the user in the first command-line argument. Directories are not considered to be regular files. Do not print any extra text, just the number followed by a newline character \n. | null |
63c648e35f004bf4898d00ad | 2023-02-13T09:00:36.660Z | Help Fix Code | v1 | null | int main() {
int *data;
printf("data %s\n", data);
return 0;
} | print data | null |
63c648e55f004bf4898d00bc | 2023-01-19T03:04:01.239Z | Question from Code | v1 | what is wrong | int sum_card(int **score_card, int size) {
// TODO: write the body of sum_card according to its description.
int i;
int sum = 0;
for(i = 0, i < size, i++) {
sum += score_card[i];
}
return sum; | null | null |
63c648e55f004bf4898d00bc | 2023-01-22T02:12:43.777Z | Question from Code | v1 | trying to turn int into array of int, but it doesn't seem to be working | int populate_array(int sin, int *sin_array) {
for(int i = 8; i >= 0; i--){
if(sin == 0){
return 1;
}
else{
sin_array[i] = sin%10;
sin = sin/10;
}
}
return 0;
} | null | null |
63c648e55f004bf4898d00bc | 2023-01-25T20:46:03.908Z | Question from Code | v1 | got a segmentation error for all c greater than 2, what is wrong | void fib(int **fib_seq, int c){
*fib_seq = malloc(c*sizeof(int));
fib_seq[0] = 0;
if(c >= 2){
*fib_seq[1] = 1;
}
if(c >= 3){
for(int i = 2; i < c; i++){
*fib_seq[i] = *fib_seq[i-1] + *fib_seq[i-2];
}
}
}
int main(int argc, char **argv) {
/* do not change this main function */
int count = strtol(argv[1], NULL, 10);
int *fib_sequence;
fib(&fib_sequence, count);
for (int i = 0; i < count; i++) {
printf("%d ", fib_sequence[i]);
}
free(fib_sequence);
return 0;
}
| null | null |
63c648e55f004bf4898d00bc | 2023-01-25T20:52:13.429Z | Help Fix Code | v1 | null | void fib(int **fib_seq, int c){
*fib_seq = malloc(c*sizeof(int));
*fib_seq[0] = 0;
if(c >= 2){
*fib_seq[1] = 1;
}
if(c >= 3){
for(int i = 2; i < c; i++){
*fib_seq[i] = *fib_seq[i-1] + *fib_seq[i-2];
}
}
}
int main(int argc, char **argv) {
/* do not change this main function */
int count = strtol(argv[1], NULL, 10);
int *fib_sequence;
fib(&fib_sequence, count);
for (int i = 0; i < count; i++) {
printf("%d ", fib_sequence[i]);
}
free(fib_sequence);
return 0;
}
| got a segmentation error for all c greater than 2, what is wrong | null |
63c648e55f004bf4898d00bc | 2023-01-25T22:18:40.685Z | Help Fix Code | v1 | null | int **split_array(const int *s, int length) {
int size1;
int size2;
if (length%2 == 0){
size1 = size2 = length/2;
}
else{
size1 = length/2 + 1;
size2 = length/2;
}
int *arr1 = malloc(sizeof(int)*size1);
int *arr2 = malloc(sizeof(int)*size2);
for(int i = 0; i < length; i++){
if (i%2==0){
arr1[i] = s[i];
}
else{
arr2[i] = s[i];
}
}
int **arr3 = malloc(sizeof(int)*length);
arr3 = {arr1, arr2};
return arr3;
} | how do I initialize array of array using malloc | null |
63c648e55f004bf4898d00bc | 2023-01-25T22:25:55.907Z | Help Fix Code | v1 | null | int **split_array(const int *s, int length) {
int size1;
int size2;
if (length%2 == 0){
size1 = size2 = length/2;
}
else{
size1 = length/2 + 1;
size2 = length/2;
}
int *arr1 = malloc(sizeof(int)*size1);
int *arr2 = malloc(sizeof(int)*size2);
for(int i = 0; i < length; i++){
if (i%2==0){
arr1[i] = s[i];
}
else{
arr2[i] = s[i];
}
}
int **arr3 = malloc(sizeof(int*)*2);
arr3[0] = arr1;
arr3[1] = arr2;
return arr3;
} | it is suppose to split an array into 2 arrays | null |
63c648e55f004bf4898d00bc | 2023-01-25T22:39:23.558Z | Help Fix Code | v1 | null | int **split_array(const int *s, int length) {
int size1;
int size2;
if (length%2 == 0){
size1 = size2 = length/2;
}
else{
size1 = length/2 + 1;
size2 = length/2;
}
int *arr1 = malloc(sizeof(int)*size1);
int *arr2 = malloc(sizeof(int)*size2);
for(int i = 0; i < length; i++){
if (i%2==0){
arr1[i/2] = s[i];
}
else{
arr2[i/2] = s[i];
}
}
int **arr3 = malloc(sizeof(int*)*2);
arr3[0] = arr1;
arr3[1] = arr2;
return arr3;
} | it is suppose to split an array into 2 arrays | null |
63c648e55f004bf4898d00bc | 2023-01-25T22:49:38.099Z | Help Fix Code | v1 | null | int *build_array(char **strs, int size) {
int *arr = malloc(size*sizeof(int));
for(int i = 0; i < size; i++){
arr[i] = strtol(strs[i], NULL, 10);
}
return arr;
}
| suppose to convert str into array of int | null |
63c648e55f004bf4898d00bc | 2023-01-27T01:23:42.719Z | Help Fix Code | v1 | null | char *cop = "tiramisu";
strncpy(sweet, cop, sizeof(*sweet));
*sweet[8] = "\0"; | The string sweet has been declared on the heap and has space for 20 characters.
Set the value of sweet to "tiramisu" without changing its address. | null |
63c648e55f004bf4898d00bc | 2023-01-27T01:25:32.807Z | Help Fix Code | v1 | null | int main() {
char *sweet = malloc(20 * sizeof(char));
// Submitted code below
char *cop = "tiramisu";
strncpy(sweet, cop, sizeof(*sweet));
sweet[8] = "\0";
// Submitted code above
/* hidden test code */
return 0;
} | The string sweet has been declared on the heap and has space for 20 characters. Set the value of sweet to "tiramisu" without changing its address. | null |
63c648e55f004bf4898d00bc | 2023-01-27T02:07:39.640Z | Help Fix Code | v1 | null | void update_contact(struct contact *c, char name, int num, char add){
strcpy((*c).name, name);
(*c).num = num;
strcpy((*c).address, add);
} | Given the following program, write the function update_contact which takes a pointer to the struct contact, a new name, a new number, and a new address to change the struct’s members. Look at the function call for the order of arguments and appropriate types. | null |
63c648e55f004bf4898d00bc | 2023-01-27T05:40:51.677Z | Help Fix Code | v1 | null | struct node {
int value;
struct node *next;
}
| Create a node struct with the following struct members:
- value, an integer
- next, a pointer to another node | null |
63c648e55f004bf4898d00bc | 2023-01-27T05:41:59.473Z | Help Fix Code | v1 | null | struct node {
int value;
struct node *next;
};
| Create a node struct with the following struct members:
- value, an integer
- next, a pointer to another node
Typedef your structure as Node. | null |
63c648e55f004bf4898d00bc | 2023-02-02T23:21:22.016Z | Help Fix Code | v1 | null | int truncate(char *s, int n){
if (strlen(s) <= n){
return 0;
}
else{
s[n] = "\0";
int result = strlen(s) - n;
return result;
}
}
| truncate string | null |
63c648e55f004bf4898d00bc | 2023-02-02T23:30:43.704Z | Help Fix Code | v1 | null | int truncate(char *s, int n){
if (strlen(s) <= n){
return 0;
}
else{
s[n] = "\0";
int result = strlen(s) - n;
return result;
}
}
| s[n] = "\0" is not working | null |
63c648e55f004bf4898d00bc | 2023-02-03T01:29:17.419Z | Help Fix Code | v1 | null | int truncate(char *s, int n){
int len = strlen(s);
printf("%d", len);
if (len <= n){
return 0;
}
else{
s[n] = '\0';
return (len-n);
}
} | 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. | null |
63c648e55f004bf4898d00bc | 2023-02-03T01:30:10.389Z | Help Fix Code | v1 | null | int truncate(char *s, int n){
int len = strlen(s);
if (len <= n){
return 0;
}
else{
s[n] = '\0';
return (len-n);
}
} | strlen is does not return the actual size of the string in int, what should i do | null |
63c648e55f004bf4898d00bc | 2023-02-03T03:29:16.638Z | Help Fix Code | v1 | null | int strip_q_marks(char *s){
int length = strlen(s);
for (int i = length-1; i <= 0; i--){
if (s[i] != '?'){
s[i+1] = '\0';
return i+1;
}
}
return 0;
} | strip the ? at the end of the string and return the amount of ? stripped | null |
63c648e55f004bf4898d00bc | 2023-02-05T00:52:56.740Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) >= MAX_NAME - 1){ //checks if name is over 32-1 characters (-1 reserved for null terminator)
return 2;
}
User *curr_user = *user_ptr_add;
while (curr_user != NULL){ //goes through the linked list and search for users with the same name
curr_user = curr_user->next;
if (strcmp(curr_user->name, name)==0){ //if a user has the same name as the requested new user, return 1
return 1;
}
}
User *new_user = (User*) malloc(sizeof(User));
if(new_user == NULL){ //if malloc fails, it will return -1;
return -1;
}
strcpy(new_user->name, name); //initializing the user's
strcpy(new_user->profile_pic, "");
new_user->first_post = NULL;
memset(new_user->friends, 0, sizeof(new_user->friends)); //set all the pointers in the friend array to NULL
new_user->next = NULL;
User *curr_user2 = *user_ptr_add;
if (curr_user2 == NULL){
curr_user2 = new_user;
}
else{
while (curr_user2 != NULL){
curr_user2 = curr_user2->next;
}
curr_user2 = 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:
* - -1 if malloc fails
* - 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 |
63c648e55f004bf4898d00bc | 2023-02-05T01:18:25.567Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) >= MAX_NAME - 1){ //checks if name is over 32-1 characters (-1 reserved for null terminator)
return 2;
}
User *curr_user = *user_ptr_add;
while (curr_user != NULL){ //goes through the linked list and search for users with the same name
curr_user = curr_user->next;
if (strcmp(curr_user->name, name)==0){ //if a user has the same name as the requested new user, return 1
return 1;
}
}
User *new_user = (User*) malloc(sizeof(User));
if(new_user == NULL){ //if malloc fails, it will return -1;
return -1;
}
strcpy(new_user->name, name); //initializing the user's
strcpy(new_user->profile_pic, "");
new_user->first_post = NULL;
memset(new_user->friends, 0, sizeof(new_user->friends)); //set all the pointers in the friend array to NULL
new_user->next = NULL;
if (*user_ptr_add == NULL){
*user_ptr_add = new_user;
}
else{
User *curr_user2 = *user_ptr_add;
while (curr_user2->next != NULL){
curr_user2 = curr_user2->next;
}
curr_user2->next = new_user;
}
return 0;
} | getting segmentation error
| null |
63c648e55f004bf4898d00bc | 2023-03-09T05:33:01.752Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLINE 256
#define MAX_PASSWORD 10
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if (strlen(user_id) > MAX_PASSWORD || strlen(password) > MAX_PASSWORD) {
fprintf(stderr, "Error: User id or password is too long");
exit(1);
}
int fd[2];
if (pipe(fd) == -1){
perror("pipe");
exit(1);
}
int r = fork();
if (r == -1){
perror("fork");
exit(1);
}
else if (r == 0){
close(fd[1]);
if (dup2(fd[0], STDIN_FILENO) == -1){
perror("dup2");
exit(1);
}
execl("./validate", "validate", NULL);
perror("execl");
exit(1);
}
else{
if (close(fd[1] == -1)){
perror("close");
exit(1);
}
char result[MAX_PASSWORD];
if (read(fd[0], result, MAX_PASSWORD) == -1){
perror("close");
exit(1);
}
int status;
if (wait(&status) == -1){
perror("wait");
exit(1);
}
if (WIFEXITED(status)) {
int exit_status = WEXITSTATUS(status);
if (exit_status == 0) {
printf(SUCCESS);
}
else if (exit_status == 2) {
printf(INVALID);
}
else {
printf(NO_USER);
}
}
}
return 0;
}
| pipe input to another program | null |
63c648e55f004bf4898d00bc | 2023-03-14T04:21:34.232Z | Help Fix Code | v2 | create a linkedlist for makefile | Rule *parse_file(FILE *fp) {
Rule *rules = NULL;
Rule *current_rule = NULL;
char line[256];
while (fgets(line, sizeof(line), fp)) {
if (is_comment_or_empty(line) == 1 && line[0] != '\n') {
continue; // Skip comments and empty lines
}
// Remove the newline character at the end of the line
line[strcspn(line, "\n")] = '\0';
if (line[0] == '\t') {
// This is an action line
Action *action = malloc(sizeof(Action));
char *token = strtok(line, " ");
int argc = 0;
while (token != NULL) {
strcpy(action->args[argc], token);
argc++;
token = strtok(NULL, " ");
}
action->args[argc] = NULL;
action->next_act = current_rule->actions;
current_rule->actions = action;
}
else {
// This is a rule line
char *target = strtok(line, " ");
Dependency *deps = NULL;
char *dep = strtok(NULL, " ");
while (dep != NULL) {
Dependency *new_dep = malloc(sizeof(Dependency));
new_dep->rule = NULL;
new_dep->next_dep = deps;
deps = new_dep;
dep = strtok(NULL, " ");
}
Rule *rule = malloc(sizeof(Rule));
strcpy(rule->target, target);
rule->dependencies = deps;
rule->actions = NULL;
rule->next_rule = rules;
rules = rule;
current_rule = rule;
}
}
return rules;
} | null | null |
63c648e55f004bf4898d00bc | 2023-03-14T06:46:06.201Z | Help Fix Code | v2 | getting segmentation error | Rule *parse_file(FILE *fp) {
Rule *rules = NULL;
Rule *current_rule = NULL;
char line[256];
while (fgets(line, sizeof(line), fp)) {
if (is_comment_or_empty(line) == 1) {
continue;
}
if (line[0] != '\n' && line[0] != '\r') {
line[strlen(line)-1] = '\0';
if (line[0] == '\t') {
// This is an action line
Action *action = malloc(sizeof(Action));
char *token = strtok(line+1, " ");
int argc = 0;
while (token != NULL) {
action->args[argc] = strdup(token);
argc++;
token = strtok(NULL, " ");
}
action->args[argc] = NULL;
action->next_act = current_rule->actions;
current_rule->actions = action;
}
else {
// This is a rule line
char *target = strtok(line, " ");
Dependency *deps = NULL;
strtok(NULL, " ");
char *dep = strtok(NULL, " ");
while (dep != NULL) {
Dependency *new_dep = malloc(sizeof(Dependency));
new_dep->rule = NULL;
new_dep->next_dep = deps;
deps = new_dep;
dep = strtok(NULL, " ");
}
Rule *rule = malloc(sizeof(Rule));
rule->target = strdup(target);
rule->dependencies = deps;
rule->actions = NULL;
rule->next_rule = rules;
rules = rule;
current_rule = rule;
}
}
}
return rules;
} | null | null |
63c648e55f004bf4898d00bc | 2023-03-14T07:05:02.535Z | Help Write Code | v2 | null | null | null | how do I assign a string literal with char **arg |
63c648e55f004bf4898d00bc | 2023-03-14T07:10:48.474Z | Help Write Code | v2 | null | null | null | typedef struct action_node {
char **args; // An array of strings suitable to be passed to execvp
struct action_node *next_act;
} Action;
how do I assign values to a Action->args |
63c648e55f004bf4898d00bc | 2023-03-14T07:44:21.558Z | Help Fix Code | v2 | getting corrupted top size error | Rule *parse_file(FILE *fp) {
Rule *rules = NULL;
Rule *current_rule = NULL;
char line[MAXLINE];
while (fgets(line, sizeof(line), fp)) {
if (is_comment_or_empty(line) == 1) {
continue;
}
if (line[0] != '\n' && line[0] != '\r') {
line[strlen(line)-1] = '\0';
if (line[0] == '\t') {
// This is an action line
Action *action = malloc(sizeof(Action));
char *token = strtok(line+1, " ");
int argc = 0;
while (token != NULL) {
action->args = malloc(sizeof(char *));
action->args[argc] = strdup(token);
//action->args[argc] = strdup(token);
argc++;
token = strtok(NULL, " ");
}
action->args[argc] = NULL;
action->next_act = current_rule->actions;
current_rule->actions = action;
}
else {
// This is a rule line
char *target = strtok(line, " ");
Dependency *deps = NULL;
strtok(NULL, " ");
char *dep = strtok(NULL, " ");
while (dep != NULL) {
Dependency *new_dep = malloc(sizeof(Dependency));
new_dep->rule = NULL;
new_dep->next_dep = deps;
deps = new_dep;
dep = strtok(NULL, " ");
}
Rule *rule = malloc(sizeof(Rule));
rule->target = strdup(target);
rule->dependencies = deps;
rule->actions = NULL;
rule->next_rule = rules;
rules = rule;
current_rule = rule;
}
}
}
return rules;
} | null | null |
63c648e55f004bf4898d00bc | 2023-03-15T04:42:56.381Z | Explain Code | v2 | null | void run_make(char* target, Rule* rules, int pflag) {
Rule* target_rule = NULL;
target_rule = rules;
}
else {
int check_target = 0;
Rule* curr_rule = rules;
while (curr_rule != NULL) {
if (strcmp(curr_rule->target, target) == 0) {
target_rule = curr_rule;
check_target = 1;
break;
}
curr_rule = curr_rule->next_rule;
}
if (check_target == 0 && target != NULL) {
fprintf(stderr, "Error: no rule found for target '%s'\n", target);
exit(1);
}
}
int check_update;
if (target_rule->dependencies != NULL) {
Dependency* curr_dep = target_rule->dependencies;
while (curr_dep != NULL) {
Rule* curr_rule2 = rules;
int dependency_is_target = 0;
if (strcmp(curr_dep->rule->target, curr_rule2->target) == 0) {
dependency_is_target = 1;
break;
}
curr_rule2 = curr_rule2->next_rule;
}
if (dependency_is_target == 0) {
curr_dep = curr_dep->next_dep;
continue;
}
if (pflag == 0) {
run_make(curr_dep->rule->target, rules, pflag);
} else {
int rp = fork();
if (rp == 0) {
run_make(curr_dep->rule->target, rules, pflag);
exit(0);
} else if (rp < 0) {
perror("fork");
exit(1);
}
}
curr_dep = curr_dep->next_dep;
}
if (pflag == 1) {
int p_status;
while (wait(&p_status) > 0) {
if (!WIFEXITED(p_status) || WEXITSTATUS(p_status) != 0) {
exit(1);
}
}
} | null | null |
63c648e55f004bf4898d00bc | 2023-03-16T22:15:35.416Z | Help Fix Code | v2 | suppose to set up a timer, but doesn't work | /* The purpose of this program is to practice writing signal handling
* functions and observing the behaviour of signals.
*/
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
/* Message to print in the signal handling function. */
#define MESSAGE "%ld reads were done in %ld seconds.\n"
/* Global variables to store number of read operations and seconds elapsed.
*/
long num_reads, seconds;
/* The first command-line argument is the number of seconds to set a timer to run.
* The second argument is the name of a binary file containing 100 ints.
* Assume both of these arguments are correct.
*/
void handler (int sig) {
fprintf(stderr, MESSAGE, num_reads, seconds);
exit(0);
}
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: time_reads s filename\n");
exit(1);
}
seconds = strtol(argv[1], NULL, 10);
FILE *fp;
if ((fp = fopen(argv[2], "r")) == NULL) {
perror("fopen");
exit(1);
}
struct sigaction act;
act.sa_handler = handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGPROF, &act, NULL);
struct itimerval timer;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 0;
timer.it_value.tv_sec = seconds;
timer.it_value.tv_usec = 0;
setitimer(ITIMER_PROF, &timer, NULL);
/* In an infinite loop, read an int from a random location in the file,
* and print it to stderr.
*/
for (;;) {
int num;
long offset = rand() % (400);
fseek(fp, offset * sizeof(int), SEEK_SET);
fread(&num, sizeof(int), 1, fp);
fprintf(stderr, "%d\n", num);
num_reads++;
}
return 1; // something is wrong if we ever get here!
}
| null | null |
63c648e65f004bf4898d00c6 | 2023-01-27T19:42:35.977Z | General Question | v1 | scanf optional * | null | null | null |
63c648e85f004bf4898d00d5 | 2023-01-17T22:46:05.216Z | Explain Code | v1 | null | num = input()
for i in range(2, num):
if (num % i == 0):
return False
return True | null | null |
63c648e85f004bf4898d00da | 2023-01-18T01:51:13.617Z | Explain Code | v1 | null | int main(){
return 0;
} | null | null |
63c648e85f004bf4898d00da | 2023-02-10T00:51:51.116Z | Explain Code | v1 | null | char *section;
while (fread(section, sizeof(char), rodata_size, file)){
printf("%s\n", section);
section = NULL;
} | null | null |
63c648e85f004bf4898d00da | 2023-02-14T05:22:49.057Z | Explain Code | v1 | null | #define SUPERVISOR(regular) regular + 5
int main() {
int regular_pay = 20;
int hours_worked = 10;
printf("pay is %d\n", (hours_worked * SUPERVISOR(regular_pay)));
// rest omitted
} | null | null |
63c648e85f004bf4898d00da | 2023-03-03T16:37:03.356Z | Explain Code | v1 | null | int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
int n = 1;
for (int i = 0; i < iterations; i++) {
if (n > 0){
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
} | null | null |
63c648e85f004bf4898d00da | 2023-03-09T04:44:30.560Z | Question from Code | v1 | are there two things written to the pipe?
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h> // remove errno import
#define MAXLINE 256
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
//user_id[strcspn(user_id, "\n\r")] = '\0';
//password[strcspn(password, "\n\r")] = '\0';
printf("%s %s\n", user_id, password);
// TODO
int fd[2];
if (pipe(fd) == -1){
perror("pipe\n");
}
int n = fork();
// parent process
if (n > 0){
close(fd[0]);
// write username to pipe
if (write(fd[1], user_id, MAXLINE/2) == -1){
perror("write to pipe\n");
}
// write password to pipe
if (write(fd[1], password, MAXLINE/2) == -1){
perror("write to pipe\n");
}
close(fd[1]);
printf("made it to wait\n");
int status;
if (wait(&status) == -1){
perror("wait\n");
}
// child terminated
if (WIFEXITED(status)){
int child_status = WEXITSTATUS(status);
if (child_status == 0){
printf("%s", SUCCESS);
}
else if (child_status == 2){
printf("%s", INVALID);
}
else if (child_status == 3){
printf("%s", NO_USER);
}
// else: error
}
}
// child process
else if (n == 0) {
// close the write file descriptor
close(fd[1]);
printf("made it to child process\n");
char *username_password[] = {"./validate", NULL};
dup2(fd[0], fileno(stdin));
execl("./validate", "validate", NULL);
close(fd[0]);
perror("exec\n");
exit(1);
}
else {
perror("fork\n");
exit(1);
}
return 0;
}
| null | null |
63c648e85f004bf4898d00da | 2023-03-09T04:45:29.208Z | Explain Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h> // remove errno import
#define MAXLINE 256
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
//user_id[strcspn(user_id, "\n\r")] = '\0';
//password[strcspn(password, "\n\r")] = '\0';
printf("%s %s\n", user_id, password);
// TODO
int fd[2];
if (pipe(fd) == -1){
perror("pipe\n");
}
int n = fork();
// parent process
if (n > 0){
close(fd[0]);
// write username to pipe
if (write(fd[1], user_id, MAXLINE/2) == -1){
perror("write to pipe\n");
}
// write password to pipe
if (write(fd[1], password, MAXLINE/2) == -1){
perror("write to pipe\n");
}
close(fd[1]);
printf("made it to wait\n");
int status;
if (wait(&status) == -1){
perror("wait\n");
}
// child terminated
if (WIFEXITED(status)){
int child_status = WEXITSTATUS(status);
if (child_status == 0){
printf("%s", SUCCESS);
}
else if (child_status == 2){
printf("%s", INVALID);
}
else if (child_status == 3){
printf("%s", NO_USER);
}
// else: error
}
}
// child process
else if (n == 0) {
// close the write file descriptor
close(fd[1]);
printf("made it to child process\n");
char *username_password[] = {"./validate", NULL};
dup2(fd[0], fileno(stdin));
execl("./validate", "validate", NULL);
close(fd[0]);
perror("exec\n");
exit(1);
}
else {
perror("fork\n");
exit(1);
}
return 0;
}
| null | null |
63c648ea5f004bf4898d00e9 | 2023-01-25T02:27:51.601Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
char phone[11];
int i, j;
j = scanf("%s %d", phone, &i);
while (j != EOF) {
if (i < -1 || i > 9) {
printf("ERROR\n");
} else if (i == -1) {
printf("%s\n", phone);
} else {
printf("%c\n", phone[i]);}
j = scanf("%d", &i);
}
return 0;
} | null | null |
63c648ea5f004bf4898d00e9 | 2023-01-25T19:59:12.149Z | Help Fix Code | v1 | null | #include <stdlib.h>
#include <stdio.h>
void fib(int *result, int n) {
result = malloc(sizeof(int) * n);
result[0] = 0;
result[1] = 1;
int i;
for (i = 2; i < n; i++) {
result[i] = result[i-2] + result[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;
} | build a dynamic array containing the first n elements of the Fibonacci sequence | null |
63c648ea5f004bf4898d00e9 | 2023-01-25T20:02:14.439Z | 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 **result, int n) {
*result = malloc(sizeof(int) * n);
*result[0] = 0;
*result[1] = 1;
int i;
for (i = 2; i < n; i++) {
*result[i] = *result[i-2] + *result[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;
}
| build a dynamic array containing the first n elements of the Fibonacci sequence | null |
63c648ea5f004bf4898d00e9 | 2023-01-25T20:13:42.054Z | 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 **result, int n) {
*result = malloc(sizeof(int) * n);
int *arr = *result;
arr[0] = 0;
arr[1] = 1;
int i;
for (i = 2; i < n; i++) {
arr[i] = arr[i-2] + arr[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;
}
| build a dynamic array containing the first n elements of the Fibonacci sequence | null |
63c648ea5f004bf4898d00e9 | 2023-01-25T21:16:33.194Z | Help Fix Code | v1 | null | int **split_array(const int *s, int length) {
if (length % 2 == 1) {
int *even = malloc(sizeof(int) * (length / 2 + 1));
} else {
int *even = malloc(sizeof(int) * (length / 2));
}
int *odd = malloc(sizeof(int) * (length / 2));
int i, j;
j = 0;
for (i = 0; i < length; i += 2) {
even[j] = s[i];
j++;
}
j = 0;
for (i = 1; i < length; i += 2) {
odd[j] = s[i];
j++;
}
int arr[2] = {even, odd};
int *ptr = arr;
return ptr;
} | Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices.
Do not allocate any more memory than necessary. You are not permitted
to include math.h. You can do the math with modulo arithmetic and integer
division. | null |
63c648ea5f004bf4898d00e9 | 2023-01-29T01:04:28.655Z | General Question | v1 | how to use scanf to scan a string from index 1 but not 0 | null | null | null |
63c648ea5f004bf4898d00e9 | 2023-01-29T01:06:27.129Z | General Question | v1 | how to use scanf to scan a string starting from a specific index but not the whole string | null | null | null |
63c648ea5f004bf4898d00e9 | 2023-01-29T01:25:57.871Z | General Question | v1 | how to scanf a string from the second char | null | null | null |
63c648ea5f004bf4898d00e9 | 2023-01-29T01:26:39.093Z | General Question | v1 | how to scanf a string starting from the second character in string | null | null | null |
63c648ea5f004bf4898d00e9 | 2023-02-09T21:34:44.784Z | Help Write Code | v1 | null | null | null | 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. |
63c648ea5f004bf4898d00e9 | 2023-02-09T21:42:45.132Z | Help Write Code | v1 | null | null | null | 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. |