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
63c6489e5f004bf4898cfe23
2023-01-27T07:00:07.547Z
General Question
v1
What is the issue here: ==36527== Invalid write of size 4 ==36527== at 0x1092C9: split_array (split_array.c:25) ==36527== by 0x1093BC: main (split_array.c:59) ==36527== Address 0x4a73144 is 4 bytes inside a block of size 6 alloc'd ==36527== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==36527== by 0x109245: split_array (split_array.c:19) ==36527== by 0x1093BC: main (split_array.c:59) ==36527==
null
null
null
63c6489e5f004bf4898cfe23
2023-01-27T20:39:59.150Z
Question from Code
v1
What is this memory error allocation error that is caused from the code? ==40742== Invalid write of size 4 ==40742== at 0x10929A: split_array (split_array.c:22) ==40742== by 0x1093B3: main (split_array.c:57) ==40742== Address 0x4a730e4 is 0 bytes after a block of size 4 alloc'd ==40742== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==40742== by 0x109224: split_array (split_array.c:17) ==40742== by 0x1093B3: main (split_array.c:57)
#include <stdio.h> #include <stdlib.h> /* Return a pointer to an array of two dynamically allocated arrays of ints. The first array contains the elements of the input array s that are at even indices. The second array contains the elements of the input array s that are at odd indices. Do not allocate any more memory than necessary. You are not permitted to include math.h. You can do the math with modulo arithmetic and integer division. */ int **split_array(const int *s, int length) { // the array containg the two pointers to the even and odd arrays int **complete_arr = malloc(sizeof(int*) * 2); // even array complete_arr[0] = malloc(sizeof(int)*(length/2)); // odd array complete_arr[1] = malloc(sizeof(int)*(length/2)); for (int i = 0; i < length; i++) { if (i % 2 == 0) { complete_arr[0][i/2] = s[i]; } else { complete_arr[1][i/2] = s[i]; } } return complete_arr; } /* Return a pointer to an array of ints with size elements. - strs is an array of strings where each element is the string representation of an integer. - size is the size of the array */ int *build_array(char **strs, int size) { // Create dynamic array int *arr = malloc(sizeof(int)*size); // Convert string to integers for (int i = 0; i < size; i++){ // the first element is always the name of the program and argv is an array of strings if (i != 0) { arr[i - 1] = strtol(strs[i], NULL, 10); // Something is wrong here } } return arr; } int main(int argc, char **argv) { /* Replace the comments in the next two lines with the appropriate arguments. Do not add any additional lines of code to the main function or make other changes. */ int *full_array = build_array(argv, argc); int **result = split_array(full_array, argc); printf("Original array:\n"); for (int i = 0; i < argc - 1; i++) { printf("%d ", full_array[i]); } printf("\n"); printf("result[0]:\n"); for (int i = 0; i < argc / 2; i++) { printf("%d ", result[0][i]); } printf("\n"); printf("result[1]:\n"); for (int i = 0; i < (argc - 1) / 2; i++) { printf("%d ", result[1][i]); } printf("\n"); free(full_array); free(result[0]); free(result[1]); free(result); return 0; }
null
null
63c6489e5f004bf4898cfe23
2023-01-27T20:42:45.245Z
General Question
v1
What is this error? ==40742== Invalid write of size 4 ==40742== at 0x10929A: split_array (split_array.c:22) ==40742== by 0x1093B3: main (split_array.c:57) ==40742== Address 0x4a730e4 is 0 bytes after a block of size 4 alloc'd ==40742== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==40742== by 0x109224: split_array (split_array.c:17) ==40742== by 0x1093B3: main (split_array.c:57)
null
null
null
63c6489e5f004bf4898cfe23
2023-01-27T20:58:08.989Z
Help Fix Code
v1
null
#include <stdio.h> #include <stdlib.h> /* Return a pointer to an array of two dynamically allocated arrays of ints. The first array contains the elements of the input array s that are at even indices. The second array contains the elements of the input array s that are at odd indices. Do not allocate any more memory than necessary. You are not permitted to include math.h. You can do the math with modulo arithmetic and integer division. */ int **split_array(const int *s, int length) { // the array containg the two pointers to the even and odd arrays int **complete_arr = malloc(sizeof(int*) * 2); // even array complete_arr[0] = malloc(sizeof(int)*(length/2)); // odd array complete_arr[1] = malloc(sizeof(int)*(length/2)); for (int i = 0; i < length; i++) { if (i % 2 == 0) { complete_arr[0][i/2] = s[i]; } else { complete_arr[1][i/2] = s[i]; } } return complete_arr; } /* Return a pointer to an array of ints with size elements. - strs is an array of strings where each element is the string representation of an integer. - size is the size of the array */ int *build_array(char **strs, int size) { // Create dynamic array int *arr = malloc(sizeof(int)*size); // Convert string to integers for (int i = 0; i < size; i++){ // the first element is always the name of the program and argv is an array of strings if (i != 0) { arr[i - 1] = strtol(strs[i], NULL, 10); // Something is wrong here } } return arr; } int main(int argc, char **argv) { /* Replace the comments in the next two lines with the appropriate arguments. Do not add any additional lines of code to the main function or make other changes. */ int *full_array = build_array(argv, argc); int **result = split_array(full_array, argc); printf("Original array:\n"); for (int i = 0; i < argc - 1; i++) { printf("%d ", full_array[i]); } printf("\n"); printf("result[0]:\n"); for (int i = 0; i < argc / 2; i++) { printf("%d ", result[0][i]); } printf("\n"); printf("result[1]:\n"); for (int i = 0; i < (argc - 1) / 2; i++) { printf("%d ", result[1][i]); } printf("\n"); free(full_array); free(result[0]); free(result[1]); free(result); return 0; }
It should not cause any memory issues
null
63c6489e5f004bf4898cfe23
2023-01-27T20:59:35.547Z
Help Fix Code
v1
null
#include <stdio.h> #include <stdlib.h> /* Return a pointer to an array of two dynamically allocated arrays of ints. The first array contains the elements of the input array s that are at even indices. The second array contains the elements of the input array s that are at odd indices. Do not allocate any more memory than necessary. You are not permitted to include math.h. You can do the math with modulo arithmetic and integer division. */ int **split_array(const int *s, int length) { // the array containg the two pointers to the even and odd arrays int **complete_arr = malloc(sizeof(int*) * 2); // even array complete_arr[0] = malloc(sizeof(int)*(length/2)); // odd array complete_arr[1] = malloc(sizeof(int)*(length/2)); for (int i = 0; i < length; i++) { if (i % 2 == 0) { complete_arr[0][i/2] = s[i]; } else { complete_arr[1][i/2] = s[i]; } } return complete_arr; } /* Return a pointer to an array of ints with size elements. - strs is an array of strings where each element is the string representation of an integer. - size is the size of the array */ int *build_array(char **strs, int size) { // Create dynamic array int *arr = malloc(sizeof(int)*size); // Convert string to integers for (int i = 0; i < size; i++){ // the first element is always the name of the program and argv is an array of strings if (i != 0) { arr[i - 1] = strtol(strs[i], NULL, 10); // Something is wrong here } } return arr; } int main(int argc, char **argv) { /* Replace the comments in the next two lines with the appropriate arguments. Do not add any additional lines of code to the main function or make other changes. */ int *full_array = build_array(argv, argc); int **result = split_array(full_array, argc); printf("Original array:\n"); for (int i = 0; i < argc - 1; i++) { printf("%d ", full_array[i]); } printf("\n"); printf("result[0]:\n"); for (int i = 0; i < argc / 2; i++) { printf("%d ", result[0][i]); } printf("\n"); printf("result[1]:\n"); for (int i = 0; i < (argc - 1) / 2; i++) { printf("%d ", result[1][i]); } printf("\n"); free(full_array); free(result[0]); free(result[1]); free(result); return 0; }
How to not allocate any more memory than needed.
null
63c6489e5f004bf4898cfe23
2023-01-28T21:21:03.459Z
General Question
v1
How to find the number of files on ls -l in C?
null
null
null
63c6489e5f004bf4898cfe23
2023-01-28T21:22:16.361Z
General Question
v1
How to find the number of files on ls -l in C using scanf?
null
null
null
63c6489e5f004bf4898cfe23
2023-01-28T21:24:17.613Z
General Question
v1
How do you use scanf?
null
null
null
63c6489e5f004bf4898cfe23
2023-01-28T21:25:33.611Z
General Question
v1
How do you use scanf on the following? total 329 -rwx------ 1 reid staff 1734 Jun 22 14:52 prog -rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt -rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz -rw-r--r-- 1 reid staff 88 Feb 15 2013 xyz.c
null
null
null
63c6489e5f004bf4898cfe23
2023-01-28T21:26:43.244Z
General Question
v1
How do you skip the first line of ls-l? total 329 -rwx------ 1 reid staff 1734 Jun 22 14:52 prog -rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt -rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz -rw-r--r-- 1 reid staff 88 Feb 15 2013 xyz.c
null
null
null
63c6489e5f004bf4898cfe23
2023-01-28T21:29:51.058Z
General Question
v1
How do you read input into an array?
null
null
null
63c6489e5f004bf4898cfe23
2023-01-28T23:45:11.058Z
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 check_permissions(char *have, char *need) { int missing = 0; for (int i = 0; i < 11; i++) { // Might have it changes to 9 idk yet lol // Checks for char literal, '-' and if the position on the two arrays don't match if (i != 0 && (need[i] != '-' && need[i] != have[i])){ missing += 1; } } if (missing == 0){ return 0; } else { return 1; } } 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. // Read input of ls -l into variables for each column char *perms = malloc(sizeof(char) * 10); int fileSize = 0; // The first line (Total: ...) scanf("%*s %*d"); // TODO: Call check_permissions as part of your solution to count the files to // compute and print the correct value. int count = 0; while (scanf("%s %*d %*s %*s %d %*s %*d %*s %*s", perms, &fileSize)!=EOF){ if (fileSize > argc && check_permissions(perms, *argv) == 0) { count += 1; } } // free variables and printf number of files printf("%d\n", count); free(perms); return 0; }
It should not always return 1
null
63c6489e5f004bf4898cfe23
2023-01-30T03:09:47.880Z
Help Fix Code
v1
null
#include <stdio.h> #include <stdlib.h> int main() { int sin_array[9] = {1,2,3,4,5,6,7,8,9}; int sum = 0; // Multiplying every 2nd digit by 2 for (int i = 0; i < 9; i++) { // For odd i if (i % 2 != 0) { if (sin_array[i] * 2 >= 10) { int temp = sin_array[i] * 2; int sum_of_digits = 0; while (temp > 0) { int remainder = temp % 10; temp = temp / 10; sum_of_digits += remainder; } sin_array[i] = sum_of_digits; } else { sin_array[i] *= 2; } } } // Add all sin_array digits together int sum = 0; for (int i = 0; i < 9; i++) { sum += sin_array[i]; } // Check if SIN is valid if (sum % 10 == 0) { printf("%s\n", "Valid Sin"); return 0; // It is valid } else { printf("%s\n", "Invalid"); return 1; } }
It should compile and not return a segmentation fault error
null
63c6489e5f004bf4898cfe23
2023-01-30T03:19:48.450Z
Help Fix Code
v1
null
#include <stdio.h> #include <stdlib.h> int main() { int sin_array[9] = {1,2,3,4,5,6,7,8,9}; // Multiplying every 2nd digit by 2 for (int i = 0; i < 9; i++) { // For odd i if (i % 2 != 0) { if (sin_array[i] * 2 >= 10) { int temp = sin_array[i] * 2; int sum_of_digits; while (temp > 0) { int remainder = temp % 10; temp = temp / 10; sum_of_digits += remainder; } sin_array[i] = sum_of_digits; } else { sin_array[i] *= 2; } } } // Add all sin_array digits together int sum; for (int i = 0; i < 9; i++) { sum += sin_array[i]; } // Check if SIN is valid if (sum % 10 == 0) { printf("%s\n", "Valid Sin"); return 0; // It is valid } else { printf("%s\n", "Invalid"); return 1; } }
How to get rid of runtime error?
null
63c6489e5f004bf4898cfe23
2023-01-30T03:20:58.600Z
Help Fix Code
v1
null
#include <stdio.h> #include <stdlib.h> int main() { int sin_array[9] = {1,2,3,4,5,6,7,8,9}; // Multiplying every 2nd digit by 2 for (int i = 0; i < 9; i++) { // For odd i if (i % 2 != 0) { if (sin_array[i] * 2 >= 10) { int temp = sin_array[i] * 2; int sum_of_digits = 0; while (temp > 0) { int remainder = temp % 10; temp = temp / 10; sum_of_digits += remainder; } sin_array[i] = sum_of_digits; } else { sin_array[i] *= 2; } } } // Add all sin_array digits together int sum = 0; for (int i = 0; i < 9; i++) { sum += sin_array[i]; } // Check if SIN is valid if (sum % 10 == 0) { printf("%s\n", "Valid Sin"); return 0; // It is valid } else { printf("%s\n", "Invalid"); return 1; } }
Does not have a runtime error
null
63c6489e5f004bf4898cfe23
2023-01-30T16:43:51.278Z
Explain Code
v1
null
int main(int argc, char** argv) { // If the number of argument is not equal to 2 or 3 if (!(argc == 2 || argc == 3)) { fprintf(stderr, "USAGE: count_large size [permissions]\n"); return 1; } }
null
null
63c6489e5f004bf4898cfe23
2023-02-02T17:32:14.835Z
Help Fix Code
v1
null
#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 numRemoved = 0; if (strlen(s) > n) { for (int i = strlen(s) - 1; i > n; i--){ // replace characters at n characters with '\0' numRemoved += 1; s[i] = '\0'; } return numRemoved; } else { 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; }
What is the segmentation fault in this code?
null
63c6489e5f004bf4898cfe23
2023-02-10T19:30:23.979Z
Help Write Code
v1
null
null
null
/* * Read in the location of the pixel array, the image width, and the image * height in the given bitmap file. */
63c6489e5f004bf4898cfe23
2023-02-10T19:39:16.151Z
Help Write Code
v1
null
null
null
/* * Read in the location of the pixel array, the image width, and the image * height in the given bitmap file. */
63c6489e5f004bf4898cfe23
2023-02-10T19:39:45.675Z
Help Write Code
v1
null
null
null
/* * Read in the location of the pixel array, the image width, and the image * height in the given bitmap file. */
63c6489e5f004bf4898cfe23
2023-02-10T19:40:03.555Z
Help Write Code
v1
null
null
null
/* * Read in the location of the pixel array, the image width, and the image * height in the given bitmap file. */
63c6489e5f004bf4898cfe23
2023-02-10T19:43:04.316Z
Help Write Code
v1
null
null
null
Read in the location of the pixel array, the image width, and the image * height in the given bitmap file.
63c6489e5f004bf4898cfe23
2023-02-10T19:45:08.869Z
Help Write Code
v1
null
null
null
/* * 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. */
63c6489e5f004bf4898cfe23
2023-02-10T19:47:05.932Z
General Question
v1
how can I use `struct pixel *` ?
null
null
null
63c6489e5f004bf4898cfe23
2023-02-10T19:47:43.792Z
Question from Code
v1
What is this code supposed to do?
/* * 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) { }
null
null
63c6489e5f004bf4898cfe23
2023-02-10T19:51:50.060Z
Help Write Code
v1
null
null
null
/* * 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. * 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) { }
63c6489e5f004bf4898cfe23
2023-02-10T20:08:32.011Z
General Question
v1
What is returned when you loop through a bitmap image?
null
null
null
63c6489e5f004bf4898cfe23
2023-02-10T20:13:29.929Z
Help Write Code
v1
null
null
null
/* * 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. * 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) { }
63c6489e5f004bf4898cfe23
2023-02-10T20:27:04.202Z
Question from Code
v1
How to deallocate memory for the code?
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: bitmap_printer input_bmp\n"); exit(1); } FILE *image = fopen(argv[1], "rb"); if (image == NULL) { fprintf(stderr, "Cannot open file\n"); exit(1); } // Read in bitmap file metadata int pixel_array_offset, width, height; read_bitmap_metadata(image, &pixel_array_offset, &width, &height); // Print out metadata. printf("Pixel array offset: %d\n", pixel_array_offset); printf("Width: %d\n", width); printf("Height: %d\n", height); // Read in the pixel data struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height); // Print out some pixels from each of the image's corners. for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { print_pixel(pixels[i][j]); print_pixel(pixels[i][width - 1 - j]); print_pixel(pixels[height - 1 - i][j]); print_pixel(pixels[height - 1 - i][width - 1 - j]); } } // Clean up: you need to do this! return 0; }
null
null
63c6489e5f004bf4898cfe23
2023-02-10T20:28:26.404Z
Question from Code
v1
How do you free the memory for each row of pixels, and then free the memory for the array of rows?
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: bitmap_printer input_bmp\n"); exit(1); } FILE *image = fopen(argv[1], "rb"); if (image == NULL) { fprintf(stderr, "Cannot open file\n"); exit(1); } // Read in bitmap file metadata int pixel_array_offset, width, height; read_bitmap_metadata(image, &pixel_array_offset, &width, &height); // Print out metadata. printf("Pixel array offset: %d\n", pixel_array_offset); printf("Width: %d\n", width); printf("Height: %d\n", height); // Read in the pixel data struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height); // Print out some pixels from each of the image's corners. for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { print_pixel(pixels[i][j]); print_pixel(pixels[i][width - 1 - j]); print_pixel(pixels[height - 1 - i][j]); print_pixel(pixels[height - 1 - i][width - 1 - j]); } } // Clean up: you need to do this! return 0; }
null
null
63c6489e5f004bf4898cfe23
2023-02-10T20:29:03.088Z
Question from Code
v1
What is consider rows for pixels?
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: bitmap_printer input_bmp\n"); exit(1); } FILE *image = fopen(argv[1], "rb"); if (image == NULL) { fprintf(stderr, "Cannot open file\n"); exit(1); } // Read in bitmap file metadata int pixel_array_offset, width, height; read_bitmap_metadata(image, &pixel_array_offset, &width, &height); // Print out metadata. printf("Pixel array offset: %d\n", pixel_array_offset); printf("Width: %d\n", width); printf("Height: %d\n", height); // Read in the pixel data struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height); // Print out some pixels from each of the image's corners. for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { print_pixel(pixels[i][j]); print_pixel(pixels[i][width - 1 - j]); print_pixel(pixels[height - 1 - i][j]); print_pixel(pixels[height - 1 - i][width - 1 - j]); } } // Clean up: you need to do this! return 0; }
null
null
63c6489e5f004bf4898cfe23
2023-02-10T20:29:40.586Z
Question from Code
v1
is the rows of pixels called height or width in this code?
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: bitmap_printer input_bmp\n"); exit(1); } FILE *image = fopen(argv[1], "rb"); if (image == NULL) { fprintf(stderr, "Cannot open file\n"); exit(1); } // Read in bitmap file metadata int pixel_array_offset, width, height; read_bitmap_metadata(image, &pixel_array_offset, &width, &height); // Print out metadata. printf("Pixel array offset: %d\n", pixel_array_offset); printf("Width: %d\n", width); printf("Height: %d\n", height); // Read in the pixel data struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height); // Print out some pixels from each of the image's corners. for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { print_pixel(pixels[i][j]); print_pixel(pixels[i][width - 1 - j]); print_pixel(pixels[height - 1 - i][j]); print_pixel(pixels[height - 1 - i][width - 1 - j]); } } // Clean up: you need to do this! return 0; }
null
null
63c6489e5f004bf4898cfe23
2023-02-10T20:48:14.124Z
Help Fix Code
v1
null
#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) { // store the location of the start of the pixel array fseek(image, 10, SEEK_SET); fread(&pixel_array_offset, sizeof(int), 1, image); // store location of image width fseek(image, 18, SEEK_SET); fread(&width, sizeof(int), 1, image); // store location of image height fseek(image, 22, SEEK_SET); fread(&height, sizeof(int), 1, image); //for (i = 0; i < 4; i++) { // pixel_array_offset[i] = fread() //} } /* * 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) { // Step 1 struct pixel **pixel_data = malloc(sizeof(struct pixel *) * height); // Step 2 (change to while loop later) for (int i = 0; i < height; i++) { pixel_data[i] = malloc(sizeof(struct pixel) * width); } // Step 3 fseek(image, pixel_array_offset, SEEK_SET); for (int h = 0; h < height; h++) { fread(pixel_data[h], sizeof(struct pixel), width, image); } return pixel_data; } /* * 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); }
It should not give a segmentation fault
null
63c6489e5f004bf4898cfe23
2023-02-12T15:20:22.468Z
Help Write Code
v1
null
null
null
/* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { // I think user are like nodes of linked list in this case? return -1; }
63c6489e5f004bf4898cfe23
2023-02-12T15:23:09.008Z
General Question
v1
How do you check if a name already exists in a linked list and traverse a linked list?
null
null
null
63c6489e5f004bf4898cfe23
2023-02-13T20:45:20.325Z
Question from Code
v1
How do you create a new User in main?
#include <time.h> #define MAX_NAME 32 // Max username and profile_pic filename lengths #define MAX_FRIENDS 10 // Max number of friends a user can have 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
63c6489e5f004bf4898cfe23
2023-02-13T21:02:40.163Z
General Question
v1
If you have a char array of size 5 and you you use strncpy to put a const char that has a length of 5, what happens?
null
null
null
63c6489e5f004bf4898cfe23
2023-02-13T21:20:35.353Z
General Question
v1
How do you use strncpy on a const char *?
null
null
null
63c6489e5f004bf4898cfe23
2023-02-13T22:20:29.667Z
General Question
v1
Is this allowed emily->next->user?
null
null
null
63c6489e5f004bf4898cfe23
2023-02-13T23:06:09.628Z
Help Write Code
v1
null
null
null
/* * 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. */
63c6489e5f004bf4898cfe23
2023-02-13T23:07:17.614Z
Help Write Code
v1
null
null
null
/* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { return -1; }
63c6489e5f004bf4898cfe23
2023-02-13T23:08:11.693Z
General Question
v1
generate a detailed documentation of `fopen` with usage examples and explanations
null
null
null
63c6489e5f004bf4898cfe23
2023-02-14T18:22:37.907Z
Help Fix Code
v1
null
/* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { // Don't add a user if their name already exists in the list but still add // user in if their name doesn't fit array and their name is unique. // CASE ONE - if user_ptr_add is not NULL (there's one or more users), traverse through user linked list User **curr = user_ptr_add; if (user_ptr_add != NULL) { while (curr->next != NULL){ if (name == curr->name){ return 1; } curr = curr->next; } } // Turn name into a char array int max_length = MAX_NAME - 1; char *temp_username; strcat(temp_username, name); char *username; strncpy(temp_username, username, max_length); // CASE TWO if (user_ptr_add == NULL || username != curr->name) { // Create new user with name [Have to remember to deallocate memory later when program ends] User *new_user = (User *)malloc(sizeof(User)); // Copy username into new_user strncpy(username, new_user->name, max_length); // sets the last user's next to point to new_user curr->next = new_user; // Check length of name if (strlen(name) >= MAX_NAME) { return 2; } else{ return 0; } } else { // if the last user has the same name as the user we wanted to insert return 1; } }
It should insert a user at the end of the linked list.
null
63c6489e5f004bf4898cfe23
2023-02-14T18:33:54.856Z
Help Fix Code
v1
null
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { // Don't add a user if their name already exists in the list but still add // user in if their name doesn't fit array and their name is unique. // Step 1: Traverse through user linked list User **curr = user_ptr_add; if (user_ptr_add != NULL) { while (curr->next != NULL){ if (name == curr->name){ return 1; } curr = curr->next; } } // Turn name into a char array char *temp_username; strcat(temp_username, name); char *username; strncpy(temp_username, username, MAX_NAME - 1); // Step 1 - if user linked list is empty or Step 2: if we have completed traversing through linked list and curr -> name != username if (user_ptr_add == NULL || username != curr->name) { // Create new user with name [Have to remember to deallocate memory later when program ends] User *new_user = (User *)malloc(sizeof(User)); // Copy username into new_user strncpy(username, new_user->name, MAX_NAME - 1); // Checks if it's an empty user list or not and insert user if (user_ptr_add == NULL) { user_ptr_add = new_user; } else { // sets the last user's next to point to new_user curr->next = new_user; } // Check length of name if (strlen(name) >= MAX_NAME) { return 2; } else{ return 0; } } else { // if the last user has the same name as the user we wanted to insert return 1; } }
It should insert a user at the end of the linked list.
null
63c6489e5f004bf4898cfe23
2023-02-14T18:41:17.269Z
Help Fix Code
v1
null
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { // Don't add a user if their name already exists in the list but still add // user in if their name doesn't fit array and their name is unique. // Step 1: Traverse through user linked list User **curr = user_ptr_add; if (user_ptr_add != NULL) { while (*curr->next != NULL){ if (name == *curr->name){ return 1; } curr = *curr->next; } } // Turn name into a char array char *temp_username; strcat(temp_username, name); char *username; strncpy(temp_username, username, MAX_NAME - 1); // Step 1 - if user linked list is empty OR // Step 2: if we have completed traversing through linked list and curr -> name != username if (user_ptr_add == NULL || username != *curr->name) { // Create new user with name User *new_user = (User *)malloc(sizeof(User)); // Copy username into new_user strncpy(username, new_user->name, MAX_NAME - 1); // Checks if it's an empty user list or not and insert user if (user_ptr_add == NULL) { user_ptr_add = new_user; } else { // sets the last user's next to point to new_user *curr->next = new_user; } // Check length of name if (strlen(name) >= MAX_NAME) { return 2; } else{ return 0; } } else { // if the last user has the same name as the user we wanted to insert return 1; } }
How to get rid of error "member reference base type 'User *' (aka 'struct user *') is not a structure or union"
null
63c6489e5f004bf4898cfe23
2023-02-14T18:44:46.655Z
General Question
v1
How do you access member of a struct if given a pointer to a pointer to the struct?
null
null
null
63c6489e5f004bf4898cfe23
2023-02-14T19:06:23.937Z
General Question
v1
What is this error: friends.c:30:20: error: member reference base type 'User *' (aka 'struct user *') is not a structure or union while (curr->next != NULL){?
null
null
null
63c6489e5f004bf4898cfe23
2023-02-14T19:34:07.815Z
Help Fix Code
v1
null
/* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { // Don't add a user if their name already exists in the list but still add // user in if their name doesn't fit array and their name is unique. // Checks if user linked list is empty or not if (user_ptr_add != NULL) { User *curr = *user_ptr_add; // Dereference so it points to a pointer of User !! // Step 1: Traverse through user linked list while (curr->next != NULL){ if (name == curr->name){ return 1; } curr = curr->next; } // Checks if last user has the same name as user you want to add if ((char *) name != curr->name) { // Create new user with name User *new_user = (User *)malloc(sizeof(User)); // Copy username into new_user strncpy((char *) name, new_user->name, MAX_NAME - 1); // sets the last user's next to point to new_user curr->next = new_user; } else { return 1; } } else { // Create new user with name User *new_user = (User *)malloc(sizeof(User)); // Copy username into new_user strncpy((char *) name, new_user->name, MAX_NAME - 1); // sets new user to be the head of the linked list user_ptr_add = &new_user; } // Check length of name if (strlen(name) >= MAX_NAME) { return 2; } else{ return 0; } }
Does not have segmentation faults
null
63c6489e5f004bf4898cfe23
2023-02-14T20:23:04.432Z
Question from Code
v1
How do you create new User using this struct?
/* * 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) { // Don't add a user if their name already exists in the list but still add // user in if their name doesn't fit array and their name is unique. // Checks if user linked list is empty or not if (user_ptr_add != NULL) { User *curr = *user_ptr_add; // Dereference so it points to a pointer of User !! // Step 1: Traverse through user linked list while (curr->next != NULL){ if (name == curr->name){ return 1; } curr = curr->next; } // Checks if last user has the same name as user you want to add if ((char *) name != curr->name) { // Create new user with name User *new_user = (User *)malloc(sizeof(User)); // Copy username into new_user strncpy((char *) name, new_user->name, MAX_NAME - 1); // sets the last user's next to point to new_user curr->next = new_user; } else { return 1; } } else { // Create new user with name User *new_user = (User *)malloc(sizeof(User)); // Copy username into new_user strncpy((char *) name, new_user->name, MAX_NAME - 1); // sets new user to be the head of the linked list user_ptr_add = &new_user; } // Check length of name if (strlen(name) >= MAX_NAME) { return 2; } else{ return 0; } } #include <time.h> #define MAX_NAME 32 // Max username and profile_pic filename lengths #define MAX_FRIENDS 10 // Max number of friends a user can have 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
63c6489e5f004bf4898cfe23
2023-02-14T20:24:54.706Z
Question from Code
v1
How to create new User struct?
#include <time.h> #define MAX_NAME 32 // Max username and profile_pic filename lengths #define MAX_FRIENDS 10 // Max number of friends a user can have 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
63c6489e5f004bf4898cfe23
2023-02-14T21:36:43.238Z
General Question
v1
How do you add to char array?
null
null
null
63c6489e5f004bf4898cfe23
2023-02-14T23:07:44.470Z
Help Write Code
v1
null
null
null
/* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { return -1; }
63c6489e5f004bf4898cfe23
2023-02-14T23:09:39.430Z
Help Write Code
v1
null
null
null
/* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { return -1; }
63c6489e5f004bf4898cfe23
2023-02-14T23:10:03.601Z
Help Write Code
v1
null
null
null
/* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename)
63c6489e5f004bf4898cfe23
2023-02-15T02:07:05.682Z
Help Fix Code
v1
null
/* * Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. */ int make_friends(const char *name1, const char *name2, User *head) { int largest_error = 0; User *user_1 = find_user(name1, head); User *user_2 = find_user(name2, head); // If the same user is passed in twice if (user_1 == user_2) { if (largest_error < 3){ largest_error = 3; } } // If at least one user doesn't exists if (user_1 == NULL || user_2 == NULL) { if (largest_error < 4){ largest_error = 4; } } // Go through user_1's friend's list User *curr = user_1->friends[0]; User *curr2 = user_2->friends[0]; int num_user1 = 0; int num_user2 = 0; int not_friends = 0; while (curr->next != NULL) { // Go through user_2's friend's list while (curr2->next != NULL) { // If the two users are already friends if (curr2 == curr) { not_friends += 1; if (largest_error < 1) { largest_error = 1; } } curr2 = curr2->next; num_user2 += 1; } curr = curr->next; num_user1 += 1; } // If users are not already friends but one of them have max number of friends if (not_friends != 0 && (num_user1 == MAX_FRIENDS || num_user2 == MAX_FRIENDS)) { if (largest_error < 2) { largest_error = 2; } } // Success else if (not_friends == 0 && (num_user1 < MAX_FRIENDS && num_user2 < MAX_FRIENDS)) { curr->next = user_2; curr2->next = user_1; } return largest_error; }
Get rid of segmentation fault
null
63c6489e5f004bf4898cfe23
2023-02-15T12:54:35.277Z
Help Fix Code
v1
null
/* * Print a user profile. * For an example of the required output format, see the example output * linked from the handout. * Return: * - 0 on success. * - 1 if the user is NULL. */ int print_user(const User *user) { if (user != NULL) { // Open profile picture FILE *file = fopen(user->profile_pic, "r"); char char_part; while (file != NULL && (char_part = fgetc(file)) != EOF) { printf("%c", char_part); } if (file != NULL) { printf("\n"); } fclose(file); // Print out name printf("%s %s\n", "Name:", user->name); printf("%s\n", "------------------------------------------"); // Make friends section printf("%s\n", "Friends:"); int i = 0; while (i < MAX_FRIENDS && (user->friends[i] != NULL)) { printf("\t%s\n", user->friends[i]->name); i += 1; } printf("%s\n", "------------------------------------------"); // Make post section printf("%s\n", "Posts:"); Post *curr = user->first_post; while (curr != NULL){ printf("%s %s\n", "From:", curr->author); printf("%s %s\n", "Date:", ctime(curr->date)); printf("\n"); printf("%s\n", curr->contents); if (curr->next != NULL) { // Checks if there is another post printf("\n"); printf("%s\n", "==="); printf("\n"); } curr = curr->next; } printf("%s\n", "------------------------------------------"); return 0; } else { // User is NULL return 1; } }
How to get rid to segmentation fault?
null
63c6489e5f004bf4898cfe23
2023-02-15T13:03:29.496Z
Help Fix Code
v1
null
/* * Print a user profile. * For an example of the required output format, see the example output * linked from the handout. * Return: * - 0 on success. * - 1 if the user is NULL. */ int print_user(const User *user) { if (user != NULL) { // Open profile picture FILE *file = fopen(user->profile_pic, "r"); char char_part; while (file != NULL && (char_part = fgetc(file)) != EOF) { printf("%c", char_part); } if (file != NULL) { printf("\n"); } fclose(file); // Print out name printf("%s %s\n", "Name:", user->name); printf("%s\n", "------------------------------------------"); // Make friends section printf("%s\n", "Friends:"); int i = 0; while (i < MAX_FRIENDS) { if (user->friends[i] != NULL) { printf("%s\n", user->friends[i]->name); } i += 1; } printf("%s\n", "------------------------------------------"); // Make post section printf("%s\n", "Posts:"); Post *curr = user->first_post; while (curr != NULL){ printf("%s %s\n", "From:", curr->author); printf("%s %s\n", "Date:", ctime(curr->date)); printf("\n"); printf("%s\n", curr->contents); if (curr->next != NULL) { // Checks if there is another post printf("\n"); printf("%s\n", "==="); printf("\n"); } curr = curr->next; } printf("%s\n", "------------------------------------------"); return 0; } // User is NULL return 1; }
No segmentation fault
null
63c6489e5f004bf4898cfe23
2023-02-15T15:34:49.013Z
Help Fix Code
v1
null
/* * 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) { // Checks if either user pointer are NULL if (author == NULL || target == NULL) { return 2; } // Checks if users are friends int i = 0; int they_are_friends = 0; while (i < MAX_FRIENDS) { if ((author->friends[i] != NULL) && author->friends[i]->name == target->name) { they_are_friends += 1; } if ((target->friends[i] != NULL) && target->friends[i]->name == author->name) { they_are_friends += 1; } i += 1; } if (they_are_friends == 0) { return 1; } else { // SEG FAULT HERE!!! // They are friends and add post content to target's profile Post *a_post = (Post *)malloc(sizeof(Post)); strcpy(a_post->author, (char *) author); strcpy(a_post->contents, contents); // IDK About this part // Get time time_t posted_datetime = time(NULL); *a_post->date = posted_datetime; // Insert new post if (target->first_post != NULL) { // NOT too sure about this part a_post->next = target->first_post; target->first_post = a_post->next; } else{ target->first_post = a_post; a_post->next = NULL; } return 0; } }
How to make Post properly and get rid of segmentation fault?
null
63c6489e5f004bf4898cfe23
2023-02-15T16:15:37.657Z
Help Write Code
v1
null
null
null
/* * From the list pointed to by *user_ptr_del, delete the user * with the given name. * Remove the deleted user from any lists of friends. * * Return: * - 0 on success. * - 1 if a user with this name does not exist. */ int delete_user(const char *name, User **user_ptr_del)
63c6489e5f004bf4898cfe23
2023-02-15T17:10:42.816Z
Help Fix Code
v1
null
/* * 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) { // Checks if either user pointer are NULL if (author == NULL || target == NULL) { return 2; } // Checks if users are friends int i = 0; int they_are_friends = 0; while (i < MAX_FRIENDS) { if ((author->friends[i] != NULL) && author->friends[i]->name == target->name) { they_are_friends += 1; } if ((target->friends[i] != NULL) && target->friends[i]->name == author->name) { they_are_friends += 1; } i += 1; } if (they_are_friends == 0) { return 1; } else { // They are friends and add post content to target's profile Post *a_post = (Post *)malloc(sizeof(Post)); if (a_post == NULL) { exit(1); } strcpy(a_post->author, author->name); a_post->contents = contents; // Get time time_t *posted_datetime = malloc(sizeof(time_t)); if (posted_datetime == NULL) { exit(1); } a_post->date = posted_datetime; // Insert new post if (target->first_post != NULL) { // NOT too sure about this part a_post->next = target->first_post; target->first_post = a_post; } else{ // If the user has no post initially target->first_post = a_post; a_post->next = NULL; } return 0; } } /* * From the list pointed to by *user_ptr_del, delete the user * with the given name. * Remove the deleted user from any lists of friends. * * Return: * - 0 on success. * - 1 if a user with this name does not exist. */ int delete_user(const char *name, User **user_ptr_del) { User *wanted_user = find_user(name, *user_ptr_del); // Checks if the user was found if (wanted_user == NULL) { return 1; } User *curr = *user_ptr_del; User *wanted_user_2; // Look for user to delete by traversing through user linked list while (curr != NULL){ // If there are more than one user in the program. If there is only one, head == user with name and they would have no friends. if (curr->next != NULL && (strcmp(name, curr->next->name) == 0)){ wanted_user_2 = curr->next; // Remove user off of their friends list int i = 0; while (i < MAX_FRIENDS && (wanted_user_2->friends[i] != NULL)) { // Pop user off each of their friend's friend list int j = 0; while (j < MAX_FRIENDS && (wanted_user_2->friends[i][j] != NULL)) { // Checks if the friend you found in the person's friend list is them // Means that the first friend is the to be removed user if (wanted_user_2->friends[i][0]->name == wanted_user_2->name) { wanted_user_2->friends[i][0] = wanted_user_2->friends[i][0]->next; // IS THIS LEGAL??? } // Checks if the next friend is not NULL and that they match the user you want to remove else if (wanted_user_2->friends[i][j]->next != NULL && (wanted_user_2->friends[i][0]->next->name == wanted_user_2->name)) { wanted_user_2->friends[i][j]->next = wanted_user_2->friends[i][j]->next->next; } // Checks if the user is at the end of the list else if (wanted_user_2->friends[i][j]->next->next == NULL && (wanted_user_2->friends[i][0]->next->name == wanted_user_2->name)) { wanted_user_2->friends[i][j]->next = NULL; } j+=1; } i += 1; } break; } curr = curr->next; } // Change the pointers // If user is the head (IDK if you write * in front) if (*user_ptr_del->next == NULL) { *user_ptr_del = *user_ptr_del->next; } else { // Set previous user of the wanted user to the user after the wanted user. curr->next = curr->next->next; } // Free the user's post Post *curr_post = wanted_user->first_post; while (curr_post != NULL) { free(curr_post->author); free(curr_post->contents); free(curr_post->date); curr_post = curr_post->next; } // Free the user free(user); return 0; }
Get rid of errors.
null
63c6489e5f004bf4898cfe23
2023-02-15T17:30:43.096Z
Help Fix Code
v1
null
/* * From the list pointed to by *user_ptr_del, delete the user * with the given name. * Remove the deleted user from any lists of friends. * * Return: * - 0 on success. * - 1 if a user with this name does not exist. */ int delete_user(const char *name, User **user_ptr_del) { User *wanted_user = find_user(name, *user_ptr_del); // Checks if the user was found if (wanted_user == NULL) { return 1; } User *curr = *user_ptr_del; User *wanted_user_2; // Look for user to delete by traversing through user linked list while (curr != NULL){ // If there are more than one user in the program. If there is only one, head == user with name and they would have no friends. if (curr->next != NULL && (strcmp(name, curr->next->name) == 0)){ wanted_user_2 = curr->next; // Remove user off of their friends list int i = 0; while (i < MAX_FRIENDS && (wanted_user_2->friends[i] != NULL)) { // Pop user off each of their friend's friend list int j = 0; while (j < MAX_FRIENDS && (wanted_user_2->friends[i]->friends[j] != NULL)) { // Checks if the friend you found in the person's friend list is them // Means that the first friend is the to be removed user if (wanted_user_2->friends[i]->friends[0]->name == wanted_user_2->name) { wanted_user_2->friends[i]->friends[0] = wanted_user_2->friends[i]->friends[0]->next; // IS THIS LEGAL??? } // Checks if the next friend is not NULL and that they match the user you want to remove else if (wanted_user_2->friends[i]->friends[j]->next != NULL && (wanted_user_2->friends[i]->friends[0]->next->name == wanted_user_2->name)) { wanted_user_2->friends[i]->friends[j]->next = wanted_user_2->friends[i]->friends[j]->next->next; } // Checks if the user is at the end of the list else if (wanted_user_2->friends[i]->friends[j]->next->next == NULL && (wanted_user_2->friends[i]->friends[0]->next->name == wanted_user_2->name)) { wanted_user_2->friends[i]->friends[j]->next = NULL; } j+=1; } i += 1; } break; } curr = curr->next; } // Change the pointers // If user is the head (IDK if you write * in front) User *head = *user_ptr_del; if (head->next == NULL) { user_ptr_del = &head->next; } else { // Set previous user of the wanted user to the user after the wanted user. curr->next = curr->next->next; } // Free the user's post Post *curr_post = wanted_user->first_post; while (curr_post != NULL) { free(curr_post->author); free(curr_post->contents); free(curr_post->date); curr_post = curr_post->next; } // Free the user free(wanted_user); return 0; }
How to get rid of segmentation fault?
null
63c6489e5f004bf4898cfe23
2023-02-15T23:24:44.769Z
Help Fix Code
v1
null
/* * 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) { // Checks if either user pointer are NULL if (author == NULL || target == NULL) { return 2; } // Checks if users are friends int i = 0; int they_are_friends = 0; while (i < MAX_FRIENDS) { if ((author->friends[i] != NULL) && author->friends[i]->name == target->name) { they_are_friends += 1; } if ((target->friends[i] != NULL) && target->friends[i]->name == author->name) { they_are_friends += 1; } i += 1; } if (they_are_friends == 0) { return 1; } else { // They are friends and add post content to target's profile Post *a_post = (Post *)malloc(sizeof(Post)); if (a_post == NULL) { exit(1); } strcpy(a_post->author, author->name); a_post->contents = contents; // Get time time_t *posted_datetime = malloc(sizeof(time_t)); if (posted_datetime == NULL) { exit(1); } a_post->date = posted_datetime; // Insert new post if (target->first_post != NULL) { // NOT too sure about this part a_post->next = target->first_post; target->first_post = a_post; } else{ // If the user has no post initially target->first_post = a_post; a_post->next = NULL; } return 0; } } /* * From the list pointed to by *user_ptr_del, delete the user * with the given name. * Remove the deleted user from any lists of friends. * * Return: * - 0 on success. * - 1 if a user with this name does not exist. */ int delete_user(const char *name, User **user_ptr_del) { User *wanted_user = find_user(name, *user_ptr_del); User *curr = *user_ptr_del; // Checks if the user was found if (curr == NULL || wanted_user == NULL) { return 1; } // Traverse through friends list and remove user from all friends list for (int i = 0; i < MAX_FRIENDS; i++) { if (wanted_user->friends[i] != NULL) { int prev = 0; // Go through their friend's list and remove them off of it for (int j = 0; j < MAX_FRIENDS; j++) { if (wanted_user->friends[i]->friends[j] != wanted_user) { wanted_user->friends[i]->friends[prev] = wanted_user->friends[i]->friends[j]; prev +=1; } } } } // Switch pointers to remove user from user linked list while (curr != NULL) { // If wanted_user is the head if (*user_ptr_del == wanted_user) { *user_ptr_del = curr->next; } // Have user point to the user beyond wanted_user else if ((curr->next != NULL) && curr->next == wanted_user){ curr->next = curr->next->next; } curr = curr->next; } // Free the user's post Post *curr_post = wanted_user->first_post; while (curr_post != NULL) { Post *new_post = curr_post->next; free(curr_post->contents); free(curr_post->date); free(curr_post); // Go to next post curr_post = new_post; } // Free the user free(wanted_user); return 0; }
How to free all memory?
null
63c6489e5f004bf4898cfe23
2023-02-15T23:35:12.281Z
Help Fix Code
v1
null
/* * From the list pointed to by *user_ptr_del, delete the user * with the given name. * Remove the deleted user from any lists of friends. * * Return: * - 0 on success. * - 1 if a user with this name does not exist. */ int delete_user(const char *name, User **user_ptr_del) { User *wanted_user = find_user(name, *user_ptr_del); User *curr = *user_ptr_del; // Checks if the user was found if (curr == NULL || wanted_user == NULL) { return 1; } // Traverse through friends list and remove user from all friends list for (int i = 0; i < MAX_FRIENDS; i++) { if (wanted_user->friends[i] != NULL) { int prev = 0; // Go through their friend's list and remove them off of it for (int j = 0; j < MAX_FRIENDS; j++) { if (wanted_user->friends[i]->friends[j] != wanted_user) { wanted_user->friends[i]->friends[prev] = wanted_user->friends[i]->friends[j]; prev +=1; } } } } // Switch pointers to remove user from user linked list while (curr != NULL) { // If wanted_user is the head if (*user_ptr_del == wanted_user) { *user_ptr_del = curr->next; } // Have user point to the user beyond wanted_user else if ((curr->next != NULL) && curr->next == wanted_user){ curr->next = curr->next->next; } curr = curr->next; } // Free the user's post Post *curr_post = wanted_user->first_post; while (curr_post != NULL) { Post *new_post = curr_post->next; free(curr_post->contents); free(curr_post->date); free(curr_post); // Go to next post curr_post = new_post; } // Free the user free(wanted_user); return 0; }
How to free all memory?
null
63c6489e5f004bf4898cfe23
2023-02-17T19:08:29.224Z
Help Write Code
v1
null
null
null
/* Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. Capacity is expected to be the number of bytes of memory allocated to dest. You shoud read the man page to learn how strncpy works. NOTE: You must write this function without using any string functions. The only function that should depend on string.h is memset. */ char *copy(char *dest, const char *src, int capacity) { return dest; }
63c6489e5f004bf4898cfe23
2023-02-24T21:54:41.423Z
General Question
v1
generate documentation for fopen()
null
null
null
63c6489e5f004bf4898cfe23
2023-02-24T21:56:58.439Z
General Question
v1
generate documentation for strncpy()
null
null
null
63c6489e5f004bf4898cfe23
2023-02-24T22:02:05.662Z
General Question
v1
Can you use strlen on a char array?
null
null
null
63c6489e5f004bf4898cfe23
2023-02-24T22:08:52.814Z
General Question
v1
generate documentation for strncat
null
null
null
63c6489e5f004bf4898cfe23
2023-02-24T22:21:00.075Z
General Question
v1
generate documentation for fread
null
null
null
63c6489e5f004bf4898cfe23
2023-02-24T22:22:23.680Z
General Question
v1
generate documentation for fscanf
null
null
null
63c6489e5f004bf4898cfe23
2023-02-25T03:52:44.946Z
General Question
v1
generate documentation for strol
null
null
null
63c6489e5f004bf4898cfe23
2023-02-25T03:56:15.982Z
General Question
v1
what is SEEK_SET?
null
null
null
63c6489e5f004bf4898cfe23
2023-02-25T05:02:24.057Z
General Question
v1
generate fseek documentation
null
null
null
63c6489e5f004bf4898cfe23
2023-02-25T20:40:38.674Z
General Question
v1
What is *(p+2) += 1?
null
null
null
63c6489e5f004bf4898cfe23
2023-02-25T20:55:25.487Z
General Question
v1
how do you use a while loop on a member of a struct?
null
null
null
63c6489e5f004bf4898cfe23
2023-02-26T05:23:54.322Z
Explain Code
v1
null
struct event { char *month; int day; }; /* Set the month and day of an event struct given a string in the format * "MonthName DayNumber" * * For example, if arg is "December 25", the event’s month should be set to "December", * and its day should be set to 25. */ void set_struct(struct event *e, char *arg) { char *tmp = strchr(arg, ’ ’); e->date = strtol(tmp + 1, NULL, 10); int size = strlen(arg) - strlen(tmp) + 1; e->month = malloc(sizeof(char) * size); strncpy(e->month, arg, size - 1); e->month[size] = ’\0’; }
null
null
63c6489e5f004bf4898cfe23
2023-02-26T05:50:31.035Z
Help Fix Code
v1
null
#include <stdio.h> #include <string.h> #include <stdlib.h> char *intertwine(char *s1, char *s2){ int i = 0; char *collection = malloc(strlen(s1) + strlen(s2) + 1); while(i / 2 < strlen(s1) && i / 2 < strlen(s2)){ collection[i] = s1[i / 2]; collection[i+1] = s2[i / 2]; i += 2; } if(i/2 >= strlen(s1)){ for(int j = i/2; j <= strlen(s2); j++){ collection[i / 2 + j] = s2[j]; } } else if(i/2 >= strlen(s2)){ for(int j = i/2; j <= strlen(s1); j++){ collection[i / 2 + j] = s1[j]; } } else { collection[i] = '\0'; } return collection; }
Fix linker problem
null
63c6489e5f004bf4898cfe23
2023-03-03T20:05:16.990Z
General Question
v1
What is ppid?
null
null
null
63c6489e5f004bf4898cfe23
2023-03-03T20:12:42.239Z
Explain Code
v1
null
(base) Emilys-MacBook-Pro:lab7 moon$ ./forkloop 1 ppid = 5962, pid = 7395, i = 0 ppid = 1, pid = 7396, i = 0
null
null
63c6489e5f004bf4898cfe23
2023-03-10T18:01:56.869Z
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
63c6489e5f004bf4898cfe23
2023-03-10T18:29:52.006Z
General Question
v2
What is a file descriptor?
null
null
null
63c6489e5f004bf4898cfe23
2023-03-10T21:25:14.348Z
Help Fix Code
v2
It should be about to reach printf statements
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); } // File descriptor int fd[2]; // Make the pipe if ((pipe(fd)) == -1){ perror("pipe"); exit(1); } // Fork - Create a child process and store its id pid. int pid = fork(); int read_end = fd[0]; int write_end = fd[1]; if (pid == 0) { // Runs child process // reset stdin so it reads from the pipe if (dup2(read_end, STDIN_FILENO) == -1) { perror("dup2"); exit(1); } // close the write end of the pipe if ((close(write_end)) == -1) { perror("close"); } // execute the validate program execl("./validate", "validate", user_id, password, NULL); } else if (pid > 1) { // Runs parent process // reset stdout so it reads from the pipe if (dup2(write_end, STDOUT_FILENO) == -1) { perror("dup2"); exit(1); } // close the write end of the pipe if ((close(read_end)) == -1) { perror("close"); } int status; pid_t child_pid = wait(&status); if (child_pid == -1) { perror("wait"); exit(1); } // Match is found if (WEXITSTATUS(status) == 0) { printf("%s\n", SUCCESS); } // Invalid password else if (WEXITSTATUS(status) == 2) { printf("%s\n", INVALID); } // User doesn't exists else { printf("%s\n", NO_USER); } } else { perror("fork"); exit(1); } return 0; }
null
null
63c6489e5f004bf4898cfe23
2023-03-10T23:07:36.203Z
Explain Code
v2
null
char line[MAXLINE]; while (fgets(line, sizeof(line) - 1, fp)) { line[strlen(line) - 1] = '\0'; if (strcmp(userid, line) == 0) { fclose(fp); exit(0); } else if (strncmp(userid, line, user_length) == 0) { fclose(fp); exit(2); } }
null
null
63c6489e5f004bf4898cfe23
2023-03-10T23:49:31.246Z
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.
63c6489e5f004bf4898cfe23
2023-03-12T19:33:58.984Z
General Question
v2
When should you use fscanf and fgets?
null
null
null
63c6489e5f004bf4898cfe23
2023-03-13T00:22:42.001Z
Help Write Code
v2
null
null
null
How do you turn a makefile into a linked list?
63c6489e5f004bf4898cfe23
2023-03-13T00:24:10.663Z
Help Write Code
v2
null
null
null
How do you write a function to parse a makefile?
63c6489e5f004bf4898cfe23
2023-03-14T03:47:17.952Z
Help Write Code
v2
null
null
null
How do you set a struct member to point to another struct?
63c6489e5f004bf4898cfe23
2023-03-14T03:49:09.385Z
General Question
v2
How do you allocate memory for the struct and assign the pointer to the struct member?
null
null
null
63c6489e5f004bf4898cfe23
2023-03-14T11:57:39.368Z
Question from Code
v2
What is the output of this code if it runs with no error?
int x = 5; void handler(int sig) { x += 3; fprintf(stderr, "inside %d ", x); } int main() { fprintf(stderr, "start "); struct sigaction act; act.sa_handler = handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, NULL); x += 2; fprintf(stderr, "outside %d", x); return 0; }
null
null
63c6489e5f004bf4898cfe23
2023-03-14T12:00:13.827Z
Question from Code
v2
What does the program above (from part 1) print to stderr if the user presses Ctrl+C at the moment when the program is at position A?
int x = 5; void handler(int sig) { x += 3; fprintf(stderr, "inside %d ", x); } int main() { fprintf(stderr, "start "); struct sigaction act; act.sa_handler = handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, NULL); x += 2; fprintf(stderr, "outside %d", x); return 0; }
null
null
63c6489e5f004bf4898cfe23
2023-03-14T13:14:09.570Z
Explain Code
v2
null
int x = 5; void handler(int sig) { x += 3; fprintf(stderr, "inside %d ", x); } int main() { fprintf(stderr, "start "); struct sigaction act; act.sa_handler = handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, NULL); x += 2; fprintf(stderr, "outside %d", x); return 0; }
null
null
63c6489e5f004bf4898cfe23
2023-03-15T02:44:23.385Z
General Question
v2
How do you get rid of "\r\n" from fgets?
null
null
null
63c6489e5f004bf4898cfe23
2023-03-15T05:04:14.000Z
Help Write Code
v2
null
null
null
How do you write a function to parse a makefile given the following function header: Rule *parse_file(FILE *fp)?
63c6489e5f004bf4898cfe23
2023-03-15T05:06:54.410Z
Help Write Code
v2
null
null
null
How do you write a function to parse a makefile using fgets and strtok when given the following function header: Rule *parse_file(FILE *fp)?
63c6489e5f004bf4898cfe23
2023-03-15T05:16:51.954Z
Help Write Code
v2
null
null
null
How do you write a function to parse a makefile using fgets and strtok when given the following function header: Rule *parse_file(FILE *fp) and the function uses the Action struct with members, char **args; and struct action_node *next_act, the Dependency struct with members Rule *rule, struct Dependency *next_dep, and Rule struct with members: char *target, Dependency *dependencies, Action *actions, and struct rule_node *next_rule.
63c6489e5f004bf4898cfe23
2023-03-15T05:21:05.737Z
Help Write Code
v2
null
null
null
How do you write a function to parse a makefile using fgets and strtok when given the following function header: Rule *parse_file(FILE *fp) and the function uses the Action struct with members, char **args; and struct action_node *next_act, the Dependency struct with members Rule *rule, struct Dependency *next_dep, and Rule struct with members: char *target, Dependency *dependencies, Action *actions, and struct rule_node *next_rule.
63c6489e5f004bf4898cfe23
2023-03-16T14:21:09.586Z
Question from Code
v2
Is this the correct way to use pointers for a linked list?
prev_dep = curr_dep; curr_dep = curr_dep->next_dep; prev_dep->next_dep = curr_dep;
null
null
63c6489e5f004bf4898cfe23
2023-03-16T14:24:32.878Z
Question from Code
v2
Is this the correct way to use pointers for a linked list?
prev_dep->next_dep = curr_dep->next_dep; curr_dep = curr_dep->next_dep;
null
null
63c6489e5f004bf4898cfe23
2023-03-16T16:46:43.054Z
General Question
v2
How can you set value to a pointer in a struct without using malloc?
null
null
null