user_id
stringlengths 24
24
| time
stringlengths 24
24
| feature_type
stringclasses 5
values | feature_version
stringclasses 2
values | input_question
stringlengths 1
1.24k
⌀ | input_code
stringlengths 2
18.5k
⌀ | input_intention
stringlengths 1
2.48k
⌀ | input_task_description
stringlengths 18
3.32k
⌀ |
---|---|---|---|---|---|---|---|
63c6487e5f004bf4898cfcfc
|
2023-01-29T16:03:11.916Z
|
Help Fix Code
|
v1
| null |
int main(int argc, char **argv) {
// TODO: Verify that command line arguments are valid.
if (argc < 2 || argc > 2){
return 2;
}
else{
int sin_number = strtol(argv[1], NULL, 10);
int array[9];
if ((populate_array(sin_number, array) | check_sin(array)) == 1){
printf("Invalid SIN \n");
return 1;
}
else{
printf("Valid SIN\n");
// TODO: Parse arguments and then call the two helpers in sin_helpers.c
// to verify the SIN given as a command line argument.
return 0;
}
}
|
why does main not run?
| null |
63c6487e5f004bf4898cfcfc
|
2023-01-29T16:04:02.194Z
|
Help Fix Code
|
v1
| null |
int main(int argc, char **argv) {
// TODO: Verify that command line arguments are valid.
if (argc < 2 || argc > 2){
return 2;
}
else{
int sin_number = strtol(argv[1], NULL, 10);
int array[9];
if ((populate_array(sin_number, array) | check_sin(array)) == 1){
printf("Invalid SIN \n");
return 1;
}
else{
printf("Valid SIN\n");
// TODO: Parse arguments and then call the two helpers in sin_helpers.c
// to verify the SIN given as a command line argument.
return 0;
}
}
}
|
why does main not run?
| null |
63c6487e5f004bf4898cfcfc
|
2023-01-29T16:05:27.843Z
|
Help Fix Code
|
v1
| null |
int main(int argc, char **argv) {
// TODO: Verify that command line arguments are valid.
if (argc < 2 || argc > 2){
return 2;
}
else{
int sin_number = strtol(argv[1], NULL, 10);
int array[9];
if ((populate_array(sin_number, array) | check_sin(array)) == 1){
printf("Invalid SIN \n");
return 1;
}
else{
printf("Valid SIN\n");
// TODO: Parse arguments and then call the two helpers in sin_helpers.c
// to verify the SIN given as a command line argument.
return 0;
}
}
}
|
i don't understand why my helper functions aren't working
| null |
63c6487e5f004bf4898cfcfc
|
2023-01-29T16:09:59.516Z
|
Help Fix Code
|
v1
| null |
int main(int argc, char **argv) {
// TODO: Verify that command line arguments are valid.
if (argc < 2 || argc > 2){
return 2;
}
else{
int sin_number = strtol(argv[1], NULL, 10);
int array[9];
if ((populate_array(sin_number, array) | check_sin(array)) == 1){
printf("Invalid SIN \n");
return 1;
}
else{
printf("Valid SIN\n");
// TODO: Parse arguments and then call the two helpers in sin_helpers.c
// to verify the SIN given as a command line argument.
return 0;
}
}
}
|
I want to pipe from helper function populate_array to check_sin. How do i do that?
| null |
63c6487e5f004bf4898cfcfc
|
2023-01-29T16:11:34.573Z
|
Help Fix Code
|
v1
| null |
int main(int argc, char **argv) {
// TODO: Verify that command line arguments are valid.
if (argc < 2 || argc > 2){
return 2;
}
else{
int sin_number = strtol(argv[1], NULL, 10);
int array[9];
if ((populate_array(sin_number, array) | check_sin(array)) == 1){
printf("Invalid SIN \n");
return 1;
}
else{
printf("Valid SIN\n");
// TODO: Parse arguments and then call the two helpers in sin_helpers.c
// to verify the SIN given as a command line argument.
return 0;
}
}
}
|
why is my main not running? I get an error saying implicit declaration of helper function
| null |
63c6487e5f004bf4898cfcfc
|
2023-01-29T16:35:55.314Z
|
Help Fix Code
|
v1
| null |
// TODO: Implement populate_array
/*
* Convert a 9 digit int to a 9 element int array.
*/
int populate_array(int sin, int *sin_array) {
int count; // keeps track of the number of digits so far
int quotient; // result after /
int remainder; // result after %
int curr_index = 0; // curr index for the remainder to be inserted
// add the digit of sin to the empty array
while (sin != 0 && count < 10){
count += 1;
quotient = sin / 10;
remainder = sin % 10;
sin_array[curr_index] = remainder;
curr_index += 1;
}
// check if count is 9 and return 1 / 0
if (count == 9){
return 0;
}
else{
return 1;
}
// flip the array to get the sin inputted
}
// TODO: Implement check_sin
/*
* Return 0 if the given sin_array is a valid SIN, and 1 otherwise.
*/
int check_sin(int *sin_array) {
// sin starts with zero makes SIN # invalid
if (sin_array[0] == 0){
return 1;
}
else{
int sum = 0, nDigits = 9;
int isSecond = 0;
for (int i = nDigits - 1 ; i >=0 ; i --){
int d = sin_array[i] - '0';
if (isSecond == 1){
d = d * 2;
}
sum += d / 10;
sum += d % 10;
if (isSecond == 0){
isSecond = 1;
}
else{
isSecond = 0;
}
}
if (sum % 10 == 0){ // the total should add to a number divisible by 10
return 0;
}
else{
return 1;
}
}
}
int main(int argc, char **argv) {
// TODO: Verify that command line arguments are valid.
if (argc < 2 || argc > 2){
return 2;
}
else{
int sin_number = strtol(argv[1], NULL, 10);
int array[9];
if ((populate_array(sin_number, array) | check_sin(array)) == 1){
printf("Invalid SIN \n");
return 1;
}
else{
printf("Valid SIN\n");
// TODO: Parse arguments and then call the two helpers in sin_helpers.c
// to verify the SIN given as a command line argument.
return 0;
}
}
}
|
Intended behavior: why is my main not running? I get an error saying implicit declaration of helper function
| null |
63c6487e5f004bf4898cfcfc
|
2023-01-29T16:39:51.450Z
|
Help Fix Code
|
v1
| null |
int populate_array(int , int *);
int check_sin(int *);
int main(int argc, char **argv) {
// TODO: Verify that command line arguments are valid.
if (argc < 2 || argc > 2){
return 2;
}
else{
int sin_number = strtol(argv[1], NULL, 10);
int array[9];
if ((populate_array(sin_number, array) | check_sin(array)) == 1){
printf("Invalid SIN \n");
return 1;
}
else{
printf("Valid SIN\n");
// TODO: Parse arguments and then call the two helpers in sin_helpers.c
// to verify the SIN given as a command line argument.
return 0;
}
}
}
|
why is my main not running?
| null |
63c6487e5f004bf4898cfcfc
|
2023-01-29T19:30:48.590Z
|
Explain Code
|
v1
| null |
test_part1: count_large
@test_cl_output=`./count_large 1000 rwx------ < handout.test`; \
if [ ! -z $$test_cl_output ] && [ $$test_cl_output -eq 2 ]; then \
echo Compiled and sanity check passed; \
else \
echo Failed sanity check; \
fi
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-01-30T21:58:41.006Z
|
General Question
|
v1
|
how to 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.
| null | null | null |
63c6487e5f004bf4898cfcfc
|
2023-01-30T21:59:39.251Z
|
Question from Code
|
v1
|
why is my code not working
|
void update_contact(struct contact *person , char name, int num, char address){
p = &person;
strcpy(p->name, name);
p->phone_number = num;
strcpy(p->address, address);
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-01-30T22:03:53.498Z
|
General Question
|
v1
|
how to takes a pointer to the struct contact, a new name, a new number, and a new address to change the struct’s members
| null | null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-02T20:50:51.697Z
|
Help Fix Code
|
v1
| null |
int truncate(char *s, int n){
if (n < 0){
return 1;
}
else{
if (strlen(s) - 1 <= n){
return 0;
}
else{
int i;
for (i = 0; i <= n - 1; i++){
s[i] = s[i];
}
s[i] = '/0';
return strlen(s) - n - 1;
}
}
}
|
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 |
63c6487e5f004bf4898cfcfc
|
2023-02-02T20:52:58.941Z
|
Help Fix Code
|
v1
| null |
int truncate(char *s, int n){
if (n < 0){
return 1;
}
else{
if (strlen(s) - 1 <= n){
return 0;
}
else{
int i;
for (i = 0; i <= n - 1; i++){
s[i] = s[i];
}
s[i] = '/0';
return strlen(s) - n - 1;
}
}
}
|
how to shorten a string
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-04T03:49:06.154Z
|
Help Write Code
|
v1
| null | null | null |
how to insert tail of the linked list
|
63c6487e5f004bf4898cfcfc
|
2023-02-04T04:27:08.343Z
|
Question from Code
|
v1
|
what is curr referring to ?
|
int create_user(const char *name, User **user_ptr_add) {
User *curr = *user_ptr_add;
return -1;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:28:45.944Z
|
Question from Code
|
v1
|
why do i get a segmentation fault
|
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(3 * width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(&ptr, 3, 1, image);
}
return &ptr[0];
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:30:01.141Z
|
Question from Code
|
v1
|
why do i get a segmentation fault?
|
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(3 * width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], 3, width, image);
}
return &ptr[0];
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:31:09.962Z
|
Question from Code
|
v1
|
how to allocate ptr on heap when function returns?
|
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(3 * width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], 3, width, image);
}
return &ptr[0];
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:32:06.070Z
|
Question from Code
|
v1
|
how to allocate space for the pointer to the struct before calling the function?
|
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(3 * width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], 3, width, image);
}
return &ptr[0];
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:32:56.670Z
|
Question from Code
|
v1
|
why do i get a segmentation fault?
|
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(3 * width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], 3, width, image);
}
return &ptr[0];
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:35:33.673Z
|
Question from Code
|
v1
|
is there a potential segmentation fault with this code?
|
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = (struct pixel**) malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(3 * width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], 3, width, image);
}
return &ptr[0];
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:36:38.388Z
|
Question from Code
|
v1
|
how to store ptr array on the heap?
|
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = (struct pixel**) malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(3 * width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], 3, width, image);
}
return &ptr[0];
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:40:43.351Z
|
Question from Code
|
v1
|
segmentation fault?
|
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = (struct pixel**) malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(3 * width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], 3, width, image);
}
return &ptr[0];
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:41:14.659Z
|
Question from Code
|
v1
|
am i allocating enough memory?
|
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = (struct pixel**) malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(3 * width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], 3, width, image);
}
return &ptr[0];
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:44:40.872Z
|
Help Fix Code
|
v1
| null |
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = (struct pixel**) malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(sizeof(int)* width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], 3, width, image);
}
return &ptr[0];
}
|
this function should return the address of the first `struct pixel *` you initialized
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:45:59.692Z
|
Help Fix Code
|
v1
| null |
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = (struct pixel**) malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(sizeof(int)* width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], 3, width, image);
}
return ptr[0];
}
|
this function should return the address of the first `struct pixel *` you initialized
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:46:59.941Z
|
Question from Code
|
v1
|
Is the function allocating space for m `struct pixel *` values, where m is the
* height of the image.
|
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = (struct pixel**) malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(sizeof(int)* width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], 3, width, image);
}
return ptr[0];
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:47:50.880Z
|
Question from Code
|
v1
|
Is the function doing the following?
For each pointer you just allocated, initialize it to point to * heap-allocated space for an entire row of pixel data.
|
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = (struct pixel**) malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(sizeof(int)* width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], 3, width, image);
}
return ptr[0];
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:49:48.998Z
|
Help Fix Code
|
v1
| null |
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = (struct pixel**) malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(sizeof(int)* width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], 3, width, image);
}
return ptr[0];
}
|
the function is supposed to do the following but it does not.
First, allocate space for m `struct pixel *` values, where m is the * height of the image.
* 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`.
* 4. Return the address of the first `struct pixel *` you initialized
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:50:41.571Z
|
Help Fix Code
|
v1
| null |
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = (struct pixel**) malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(sizeof(int)* width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], 3, width, image);
}
return ptr;
}
|
the function is supposed to do the following but it does not. First, allocate space for m `struct pixel *` values, where m is the * height of the image. * 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`. * 4. Return the address of the first `struct pixel *` you initialized
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:52:12.645Z
|
Help Fix Code
|
v1
| null |
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = (struct pixel**) malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(sizeof(struct pixel)* width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], sizeof(struct pixel), width, image);
}
return ptr;
}
|
the function is supposed to do the following but it does not. First, allocate space for m `struct pixel *` values, where m is the * height of the image. * 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`. * 4. Return the address of the first `struct pixel *` you initialized
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:53:56.264Z
|
Help Fix Code
|
v1
| null |
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = (struct pixel**) malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(sizeof(struct pixel)* width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], sizeof(struct pixel), width, image);
}
return ptr;
}
|
segmentation fault?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:54:58.189Z
|
Question from Code
|
v1
|
segmentation fault?
|
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = (struct pixel**) malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(sizeof(struct pixel)* width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], sizeof(struct pixel), width, image);
pixel_array_offset += width * sizeof(struct pixel);
}
return ptr;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:56:03.859Z
|
Help Fix Code
|
v1
| null |
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = (struct pixel**) malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(sizeof(struct pixel)* width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], sizeof(struct pixel), width, image);
pixel_array_offset += width * sizeof(struct pixel);
}
return ptr;
}
|
segmentation fault
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T19:57:30.237Z
|
Help Fix Code
|
v1
| null |
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
// allocate space for m struct pixel * values
struct pixel **ptr = (struct pixel**) malloc((sizeof(struct pixel*)) * height);
for (int i = 0; i < height; i++){
ptr[i] = malloc(sizeof(struct pixel)* width);
fseek(image, pixel_array_offset, SEEK_SET);
fread(ptr[i], sizeof(struct pixel), width, image);
pixel_array_offset += width * sizeof(struct pixel);
}
return ptr;
}
|
segmentation fault
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T20:03:50.614Z
|
Help Fix Code
|
v1
| null |
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!
free(pixels);
fclose(image);
return 0;
}
|
why segmentation fault?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T20:07:50.817Z
|
Help Fix Code
|
v1
| null |
#include <stdio.h>
#include <stdlib.h>
#include "bitmap.h"
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!
for (int i = 0; i < 5; i++){
free(pixels[i]);
}
free(pixels);
fclose(image);
return 0;
}
|
segmentation fault?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T20:09:10.323Z
|
Help Fix Code
|
v1
| null |
#include <stdio.h>
#include <stdlib.h>
#include "bitmap.h"
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!
for (int i = 0; i < 5; i++){
free(pixels[i]);
}
free(pixels);
fclose(image);
return 0;
}
|
how to free memory allocated for array of pointers?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T20:10:14.772Z
|
Help Fix Code
|
v1
| null |
#include <stdio.h>
#include <stdlib.h>
#include "bitmap.h"
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!
for (int i = 0; i < height; i++){
free(pixels[i]);
}
free(pixels);
fclose(image);
return 0;
}
|
why segmentation fault?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T20:10:54.739Z
|
Question from Code
|
v1
|
segmentation fault?
|
#include <stdio.h>
#include <stdlib.h>
#include "bitmap.h"
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!
for (int i = 0; i < height; i++){
free(pixels[i]);
}
free(pixels);
fclose(image);
return 0;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T20:11:39.938Z
|
Question from Code
|
v1
|
why am i getting a segmentation fault?
|
#include <stdio.h>
#include <stdlib.h>
#include "bitmap.h"
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!
free(pixels);
fclose(image);
return 0;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T20:13:15.446Z
|
Question from Code
|
v1
|
why am i getting a segmentation fault?
|
#include <stdio.h>
#include <stdlib.h>
#include "bitmap.h"
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!
for (int i = 0; i < 5; i++) {
free(pixels[i]);
}
free(pixels);
fclose(image);
return 0;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T20:14:16.565Z
|
Question from Code
|
v1
|
where am i accessing memory that's been freed or doesn't exist?
|
#include <stdio.h>
#include <stdlib.h>
#include "bitmap.h"
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!
for (int i = 0; i < 5; i++) {
free(pixels[i]);
}
free(pixels);
fclose(image);
return 0;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T20:15:28.962Z
|
Question from Code
|
v1
|
how to free each row separately?
|
#include <stdio.h>
#include <stdlib.h>
#include "bitmap.h"
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!
for (int i = 0; i < 5; i++) {
free(pixels[i]);
}
free(pixels);
fclose(image);
return 0;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T20:18:46.716Z
|
Help Fix Code
|
v1
| null |
#include <stdio.h>
#include <stdlib.h>
#include "bitmap.h"
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!
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++){
free(pixels);
}
}
fclose(image);
return 0;
}
|
segmentation fault?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-08T20:20:39.248Z
|
Help Fix Code
|
v1
| null |
#include <stdio.h>
#include <stdlib.h>
#include "bitmap.h"
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!
for (int i = 0; i < height; i++) {
free(pixels[i]);
}
free(pixels);
fclose(image);
return 0;
}
|
segmentation fault?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-10T19:49:45.187Z
|
Question from Code
|
v1
|
does this return 4 if at least one user does not exist?
|
if (head == NULL){
return 4;
}
else if (head->next == NULL){
return 4;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-10T19:51:11.180Z
|
Question from Code
|
v1
|
does this check if user1 and user2 are in the list
|
else{ // more than one users in lst
User *prev1 = head;
User *curr1 = head->next;
User *first_user;
while (curr1->next != NULL){
// check if user1 / user 2 is in the list
if (strcmp(prev1->name, name1)==0){
if (prev1->friends[10] != NULL){
return 2;
}
first_user = prev1;
}
prev1 = curr1;
curr1 = curr1->next;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-10T19:54:02.065Z
|
Help Write Code
|
v1
| null | null | 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.
*/
|
63c6487e5f004bf4898cfcfc
|
2023-02-10T19:59:17.229Z
|
Question from Code
|
v1
|
is there a problem with my code?
|
int make_friends(const char *name1, const char *name2, User *head) {
// check if same user name is passed in 2x
if (strcmp(name1, name2) == 0){
error = 3;
}
else{
// check if there's 0 / one user -> user DNE
if (head == NULL){
return 4;
}
else if (head->next == NULL){
return 4;
}
else{ // more than one users in lst
int error;
User *prev1 = head;
User *curr1 = head->next;
User *first_user;
while (curr1->next != NULL){
// check if user1 / user 2 is in the list
if (strcmp(prev1->name, name1)==0){
if (prev1->friends[10] != NULL){
return 2;
}
first_user = prev1;
}
prev1 = curr1;
curr1 = curr1->next;
}
User *prev = head;
User *curr = head->next;
User *second_user;
while (curr->next != NULL){
// check if user1 / user 2 is in the list
if (strcmp(prev->name, name1)==0){
if (prev->friends[10] != NULL){
return 2;
}
second_user = prev;
}
prev = curr;
curr = curr->next;
}
if ((first_user->friends[10] != NULL) && (second_user->friends[10] != NULL)){
for (int i = 0; i < 10; i++){
if(first_user->friends[i] == second_user){
return 1;
}
if (first_user->friends[i] == NULL){
first_user->friends[i] = second_user;
}
}
for (int j = 0; j < 10; j++){
if(second_user->friends[j] == first_user){
error = 1;
}
if (second_user->friends[j] == NULL){
second_user->friends[j] = first_user;
}
}
}
return 0;
}
}
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-10T20:42:26.292Z
|
Question from Code
|
v1
|
is there any problem with this?
|
int make_friends(const char *name1, const char *name2, User *head) {
int error;
if (strcmp(name1, name2) == 0){
error = 3;
}
if (head == NULL){
return 4;
}
else{
if (head->next == NULL){
if (strcmp(head->name, name1) != 0){
return 4;
}
}
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
if (first_user == NULL || second_user == NULL){
return 4;
}
int stop = 0;
if ((first_user->friends[9] != NULL) && (second_user->friends[9] != NULL)){
for (int i = 0; i < 10; i++){
if(first_user->friends[i] == second_user){
if (error < 1){
error = 1;
}
}
if (stop == 0){
if (first_user->friends[i] == NULL){
first_user->friends[i] = second_user;
stop = 1;
error = 0;
}
}
}
}
if (first_user->friends[9] != NULL && second_user->friends[9] != NULL){
for (int j = 0; j < 10; j++){
if(second_user->friends[j] == first_user){
if (error < 1){
error = 1;
}
}
if (stop == 0){
if (second_user->friends[j] == NULL){
second_user->friends[j] = first_user;
stop = 1;
error = 0;
}
}
}
}
}
if (error ==0){
return 0;
}
else{
return error;
}
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-10T21:07:47.562Z
|
Question from Code
|
v1
|
why is there an error?
|
int make_friends(const char *name1, const char *name2, User *head) {
int error;
if (strcmp(name1, name2) == 0){
error = 3;
}
if (head == NULL){
return 4;
}
else{
if (head->next == NULL){
if (strcmp(head->name, name1) != 0){
return 4;
}
}
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
if (first_user == NULL || second_user == NULL){
return 4;
}
int stop = 0;
if ((first_user->friends[9] != NULL) && (second_user->friends[9] != NULL)){
for (int i = 0; i < 10; i++){
if(first_user->friends[i] == second_user){
if (error < 1){
error = 1;
}
}
if (stop == 0){
if (first_user->friends[i] == NULL){
first_user->friends[i] = second_user;
stop = 1;
error = 0;
}
}
fprintf(stdout, "%s\n", first_user->friends[i]->name);
}
}
if (first_user->friends[9] != NULL && second_user->friends[9] != NULL){
for (int j = 0; j < 10; j++){
if(second_user->friends[j] == first_user){
if (error < 1){
error = 1;
}
}
if (stop == 0){
if (second_user->friends[j] == NULL){
second_user->friends[j] = first_user;
stop = 1;
error = 0;
}
}
fprintf(stdout, "%s\n", first_user->friends[j]->name);
}
}
}
if (error ==0){
return 0;
}
else{
return error;
}
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-10T21:13:08.611Z
|
Question from Code
|
v1
|
why is there an error?
|
int make_friends(const char *name1, const char *name2, User *head) {
int error;
if (strcmp(name1, name2) == 0){
error = 3;
}
if (head == NULL){
return 4;
}
else{
if (head->next == NULL){
if (strcmp(head->name, name1) != 0){
return 4;
}
}
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
if (first_user == NULL || second_user == NULL){
return 4;
}
int stop = 0;
if ((first_user->friends[9] != NULL) && (second_user->friends[9] != NULL)){
for (int i = 0; i < 10; i++){
if(first_user->friends[i] == second_user){
if (error < 1){
error = 1;
}
}
else{
if (stop == 0){
if (first_user->friends[i] == NULL){
first_user->friends[i] = second_user;
stop = 1;
error = 0;
}
}
fprintf(stdout, "%s\n", first_user->friends[i]->name);
}
}
}
if (first_user->friends[9] != NULL && second_user->friends[9] != NULL){
for (int j = 0; j < 10; j++){
if(second_user->friends[j] == first_user){
if (error < 1){
error = 1;
}
}
else{
if (stop == 0){
if (second_user->friends[j] == NULL){
second_user->friends[j] = first_user;
stop = 1;
error = 0;
}
fprintf(stdout, "%s\n", first_user->friends[j]->name);
}
}
}
}
}
if (error == 0){
return 0;
}
else{
return error;
}
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-10T21:14:37.817Z
|
Help Fix Code
|
v1
| null |
int make_friends(const char *name1, const char *name2, User *head) {
int error;
if (strcmp(name1, name2) == 0){
error = 3;
}
if (head == NULL){
return 4;
}
else{
if (head->next == NULL){
if (strcmp(head->name, name1) != 0){
return 4;
}
}
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
if (first_user == NULL || second_user == NULL){
return 4;
}
int stop = 0;
if ((first_user->friends[9] != NULL) && (second_user->friends[9] != NULL)){
for (int i = 0; i < 10; i++){
if(first_user->friends[i] == second_user){
if (error < 1){
error = 1;
}
}
else{
if (stop == 0){
if (first_user->friends[i] == NULL){
first_user->friends[i] = second_user;
stop = 1;
error = 0;
}
}
fprintf(stdout, "%s\n", first_user->friends[i]->name);
}
}
}
if (first_user->friends[9] != NULL && second_user->friends[9] != NULL){
for (int j = 0; j < 10; j++){
if(second_user->friends[j] == first_user){
if (error < 1){
error = 1;
}
}
else{
if (stop == 0){
if (second_user->friends[j] == NULL){
second_user->friends[j] = first_user;
stop = 1;
error = 0;
}
fprintf(stdout, "%s\n", first_user->friends[j]->name);
}
}
}
}
}
if (error == 0){
return 0;
}
else{
return error;
}
}
|
why is there an error?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-11T16:46:09.117Z
|
Help Fix Code
|
v1
| null |
int make_friends(const char *name1, const char *name2, User *head) {
int error;
if (strcmp(name1, name2) == 0){
error = 3;
}
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
if (first_user == NULL || second_user == NULL){
return 4;
}
if ((first_user->friends[9] != NULL) && (second_user->friends[9] != NULL)){
for (int i = 0; i < 9; i++){
if(strcmp(first_user->friends[i]->name, second_user->name) && first_user->friends[i] != NULL){
if (error < 1){
error = 1;
}
}
else{
// add to each friend's lst
int stop1 = 0;
if (stop1 == 0){
first_user->friends[i] = second_user;
stop1 = 1;
error = 0;
}
}
}
for (int j = 0; j < 9; j++){
if(strcmp(second_user->friends[j]->name, first_user->name) && second_user->friends[j] != NULL){
if (error < 1){
error = 1;
}
}
else{
// add to each friend's lst
int stop = 0;
if (stop == 0){
second_user->friends[j] = first_user;
stop = 1;
error = 0;
}
}
}
}
else {
if (error < 2){
error = 2;
}
}
fprintf(stdout, "%s\n", first_user->friends[0]->name);
fprintf(stdout, "%s\n", second_user->friends[0]->name);
if (error != 0){
return error;
}
return 0;
}
|
does the code do the following:
add second user to the first user's friend list and
add first user to the second user's friends list if both exist and their friend's list is not at a maximum of 10 and they're not already friends
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-11T16:48:46.861Z
|
Question from Code
|
v1
|
* 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.
*
|
int make_friends(const char *name1, const char *name2, User *head) {
int error;
if (strcmp(name1, name2) == 0){
error = 3;
}
// if (head == NULL){
// return 4;
// }
// else{
// if (head->next == NULL){
// if (strcmp(head->name, name1) != 0){
// return 4;
// }
// }
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
if (first_user == NULL || second_user == NULL){
return 4;
}
if ((first_user->friends[9] != NULL) && (second_user->friends[9] != NULL)){
for (int i = 0; i < 9; i++){
if((strcmp(first_user->friends[i]->name, second_user->name)==0) && (first_user->friends[i] != NULL)){
if (error < 1){
error = 1;
}
}
else{
// add to each friend's lst
int stop1 = 0;
if (stop1 == 0){
first_user->friends[i] = second_user;
stop1 = 1;
error = 0;
}
}
}
for (int j = 0; j < 9; j++){
if((strcmp(second_user->friends[j]->name, first_user->name) == 0) && second_user->friends[j] != NULL){
if (error < 1){
error = 1;
}
}
else{
// add to each friend's lst
int stop = 0;
if (stop == 0){
second_user->friends[j] = first_user;
stop = 1;
error = 0;
}
}
}
else {
if (error < 2){
error = 2;
}
}
fprintf(stdout, "%s\n", first_user->friends[0]->name);
fprintf(stdout, "%s\n", second_user->friends[0]->name);
if (error != 0){
return error;
}
return 0;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-11T16:49:40.089Z
|
Question from Code
|
v1
|
does the code do the following: add second user to the first user's friend list and add first user to the second user's friends list if both exist and their friend's list is not at a maximum of 10 and they're not already friends
|
int make_friends(const char *name1, const char *name2, User *head) {
int error;
if (strcmp(name1, name2) == 0){
error = 3;
}
// if (head == NULL){
// return 4;
// }
// else{
// if (head->next == NULL){
// if (strcmp(head->name, name1) != 0){
// return 4;
// }
// }
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
if (first_user == NULL || second_user == NULL){
return 4;
}
if ((first_user->friends[9] != NULL) && (second_user->friends[9] != NULL)){
for (int i = 0; i < 9; i++){
if((strcmp(first_user->friends[i]->name, second_user->name)==0) && (first_user->friends[i] != NULL)){
if (error < 1){
error = 1;
}
}
else{
// add to each friend's lst
int stop1 = 0;
if (stop1 == 0){
first_user->friends[i] = second_user;
stop1 = 1;
error = 0;
}
}
}
for (int j = 0; j < 9; j++){
if((strcmp(second_user->friends[j]->name, first_user->name) == 0) && second_user->friends[j] != NULL){
if (error < 1){
error = 1;
}
}
else{
// add to each friend's lst
int stop = 0;
if (stop == 0){
second_user->friends[j] = first_user;
stop = 1;
error = 0;
}
}
}
else {
if (error < 2){
error = 2;
}
}
fprintf(stdout, "%s\n", first_user->friends[0]->name);
fprintf(stdout, "%s\n", second_user->friends[0]->name);
if (error != 0){
return error;
}
return 0;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-11T16:53:55.345Z
|
Question from Code
|
v1
|
does this add the second user to the friend's list of first user?
|
if ((first_user->friends[9] != NULL) && (second_user->friends[9] != NULL)){
for (int i = 0; i < 9; i++){
if((strcmp(first_user->friends[i]->name, second_user->name)==0) && (first_user->friends[i] != NULL)){
if (error < 1){
error = 1;
}
}
else{
// add to each friend's lst
int stop1 = 0;
if (stop1 == 0){
first_user->friends[i] = second_user;
stop1 = 1;
error = 0;
}
}
}
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-11T17:03:04.535Z
|
Question from Code
|
v1
|
am i adding second user to the first user's friend list and first user to the second user's friend list ?
|
int make_friends(const char *name1, const char *name2, User *head) {
int error;
// check if the two names are the same
if (strcmp(name1, name2) == 0){
error = 3;
}
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
// check if users exist or not
if (first_user == NULL || second_user == NULL){
return 4;
}
// check if either user's have 10 friends or not
if ((first_user->friends[9] != NULL) && (second_user->friends[9] != NULL)){
int already_friends = 0;
for (int i = 0; i < 9; i++){
// check if the second user is already friends with first user and that it's not NULL
if((strcmp(first_user->friends[i]->name, second_user->name)==0) && (first_user->friends[i] != NULL)){
if (error < 1){
error = 1;
already_friends = 1;
}
}
if ((already_friends == 0) && (first_user->friends[i] == NULL)){
int stop = 0;
if (stop == 0){
first_user->friends[i] = second_user;
stop = 1;
error = 0;
}
}
}
for (int j = 0; j < 9; j++){
int already_friends1 = 0;
if((strcmp(second_user->friends[j]->name, first_user->name) == 0) && second_user->friends[j] != NULL){
if (error < 1){
error = 1;
already_friends1 = 1;
}
}
else{
// add to each friend's lst
if ((already_friends1 == 0) && (second_user->friends[j] == NULL)){
int stop1 = 0;
if (stop1 == 0){
second_user->friends[j] = first_user;
stop1 = 1;
error = 0;
}
}
}
}
}
else {
if (error < 2){
error = 2;
}
}
fprintf(stdout, "%s\n", first_user->friends[0]->name);
fprintf(stdout, "%s\n", second_user->friends[0]->name);
if (error != 0){
return error;
}
return 0;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-11T17:05:14.099Z
|
Help Fix Code
|
v1
| null |
int make_friends(const char *name1, const char *name2, User *head) {
int error;
// check if the two names are the same
if (strcmp(name1, name2) == 0){
error = 3;
}
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
// check if users exist or not
if (first_user == NULL || second_user == NULL){
return 4;
}
// check if either user's have 10 friends or not
if ((first_user->friends[9] != NULL) && (second_user->friends[9] != NULL)){
int already_friends = 0;
for (int i = 0; i < 9; i++){
// check if the second user is already friends with first user and that it's not NULL
if((strcmp(first_user->friends[i]->name, second_user->name)==0) && (first_user->friends[i] != NULL)){
if (error < 1){
error = 1;
already_friends = 1;
}
}
if ((already_friends == 0) && (first_user->friends[i] == NULL)){
int stop = 0;
if (stop == 0){
first_user->friends[i] = second_user;
stop = 1;
error = 0;
}
}
}
for (int j = 0; j < 9; j++){
int already_friends1 = 0;
if((strcmp(second_user->friends[j]->name, first_user->name) == 0) && second_user->friends[j] != NULL){
if (error < 1){
error = 1;
already_friends1 = 1;
}
}
else{
// add to each friend's lst
if ((already_friends1 == 0) && (second_user->friends[j] == NULL)){
int stop1 = 0;
if (stop1 == 0){
second_user->friends[j] = first_user;
stop1 = 1;
error = 0;
}
}
}
}
}
else {
if (error < 2){
error = 2;
}
}
fprintf(stdout, "%s\n", first_user->friends[0]->name);
fprintf(stdout, "%s\n", second_user->friends[0]->name);
if (error != 0){
return error;
}
return 0;
}
|
how to make the code less confusing
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-11T17:44:58.786Z
|
Help Fix Code
|
v1
| null |
int make_friends(const char *name1, const char *name2, User *head) {
int error = 0;
// check if the two names are the same
if (strcmp(name1, name2) == 0){
error = 3;
}
else{
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
// check if users exist or not
if (first_user == NULL || second_user == NULL){
return 4;
}
// check if either user's have 10 friends or not
if ((first_user->friends[9] == NULL) && (second_user->friends[9] == NULL)){
int already_friends = 0;
for (int i = 0; i < 10; i++){
// check if the second user is already friends with first user and that it's not NULL
if((strcmp(first_user->friends[i]->name, second_user->name)==0) && (first_user->friends[i] != NULL)){
if (error < 1){
error = 1;
already_friends = 1;
}
error += 0;
already_friends += 0;
}
if ((already_friends == 0) && (first_user->friends[i] == NULL)){
int stop = 0;
if (stop == 0){
first_user->friends[i] = second_user;
stop = 1;
error = 0;
fprintf(stdout, "%s\n", first_user->friends[i]->name);
}
stop += 0;
error += 0;
}
}
for (int j = 0; j < 10; j++){
int already_friends1 = 0;
if((strcmp(second_user->friends[j]->name, first_user->name) == 0) && second_user->friends[j] != NULL){
if (error < 1){
error = 1;
already_friends1 = 1;
}
error += 0;
already_friends1 += 0;
}
else{
// add to each friend's lst
if ((already_friends1 == 0) && (second_user->friends[j] == NULL)){
int stop1 = 0;
if (stop1 == 0){
second_user->friends[j] = first_user;
stop1 = 1;
error = 0;
fprintf(stdout, "%s\n", second_user->friends[j]->name);
}
error += 0;
stop1 += 0;
}
}
}
}
else {
if (error < 2){
error = 2;
}
error += 0;
}
}
if (error != 0){
return error;
}
return 0;
}
|
why segmentation fault?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-11T17:47:02.344Z
|
Question from Code
|
v1
|
why segmentation error ?
|
int make_friends(const char *name1, const char *name2, User *head) {
int error = 0;
// check if the two names are the same
if (strcmp(name1, name2) == 0){
error = 3;
}
else{
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
// check if users exist or not
if (first_user == NULL || second_user == NULL){
return 4;
}
// check if either user's have 10 friends or not
if ((first_user->friends[9] == NULL) && (second_user->friends[9] == NULL)){
int already_friends = 0;
for (int i = 0; i < 10; i++){
// check if the second user is already friends with first user and that it's not NULL
if((strcmp(first_user->friends[i]->name, second_user->name)==0) && (first_user->friends[i] != NULL)){
if (error < 1){
error = 1;
already_friends = 1;
}
error += 0;
already_friends += 0;
}
if ((already_friends == 0) && (first_user->friends[i] == NULL)){
int stop = 0;
if (stop == 0){
first_user->friends[i] = second_user;
stop = 1;
error = 0;
fprintf(stdout, "%s\n", first_user->friends[i]->name);
}
stop += 0;
error += 0;
}
}
for (int j = 0; j < 10; j++){
int already_friends1 = 0;
if((strcmp(second_user->friends[j]->name, first_user->name) == 0) && second_user->friends[j] != NULL){
if (error < 1){
error = 1;
already_friends1 = 1;
}
error += 0;
already_friends1 += 0;
}
else{
// add to each friend's lst
if ((already_friends1 == 0) && (second_user->friends[j] == NULL)){
int stop1 = 0;
if (stop1 == 0){
second_user->friends[j] = first_user;
stop1 = 1;
error = 0;
fprintf(stdout, "%s\n", second_user->friends[j]->name);
}
error += 0;
stop1 += 0;
}
}
}
}
else {
if (error < 2){
error = 2;
}
error += 0;
}
}
if (error != 0){
return error;
}
return 0;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-11T17:48:56.787Z
|
Help Fix Code
|
v1
| null |
int make_friends(const char *name1, const char *name2, User *head) {
int error = 0;
// check if the two names are the same
if (strcmp(name1, name2) == 0){
error = 3;
}
else{
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
// check if users exist or not
if (first_user == NULL || second_user == NULL){
return 4;
}
// check if either user's have 10 friends or not
if ((first_user->friends[9] == NULL) && (second_user->friends[9] == NULL)){
int already_friends = 0;
for (int i = 0; i < 10; i++){
// check if the second user is already friends with first user and that it's not NULL
if((strcmp(first_user->friends[i]->name, second_user->name)==0) && (first_user->friends[i] != NULL)){
if (error < 1){
error = 1;
already_friends = 1;
}
error += 0;
already_friends += 0;
}
if ((already_friends == 0) && (first_user->friends[i] == NULL)){
int stop = 0;
if (stop == 0){
first_user->friends[i] = second_user;
stop = 1;
error = 0;
fprintf(stdout, "%s\n", first_user->friends[i]->name);
}
stop += 0;
error += 0;
}
}
for (int j = 0; j < 10; j++){
int already_friends1 = 0;
if((strcmp(second_user->friends[j]->name, first_user->name) == 0) && second_user->friends[j] != NULL){
if (error < 1){
error = 1;
already_friends1 = 1;
}
error += 0;
already_friends1 += 0;
}
else{
// add to each friend's lst
if ((already_friends1 == 0) && (second_user->friends[j] == NULL)){
int stop1 = 0;
if (stop1 == 0){
second_user->friends[j] = first_user;
stop1 = 1;
error = 0;
fprintf(stdout, "%s\n", second_user->friends[j]->name);
}
error += 0;
stop1 += 0;
}
}
}
}
else {
if (error < 2){
error = 2;
}
error += 0;
}
}
if (error != 0){
return error;
}
return 0;
}
|
why segmentation fault
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-11T18:09:31.044Z
|
Question from Code
|
v1
|
why segmentation fault?
|
int make_friends(const char *name1, const char *name2, User *head) {
int error = 0;
// check if the two names are the same
if (strcmp(name1, name2) == 0){
error = 3;
}
else{
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
// check if users exist or not
if (first_user == NULL || second_user == NULL){
return 4;
}
// check if either user's have 10 friends or not
if ((first_user->friends[9] == NULL) && (second_user->friends[9] == NULL)){
int already_friends = 0;
for (int i = 0; i < 10; i++){
// check if the second user is already friends with first user and that it's not NULL
if((strcmp(first_user->friends[i]->name, second_user->name) ==0) && (first_user->friends[i] != NULL)){
if (error < 1){
error = 1;
already_friends = 1;
}
error += 0;
already_friends += 0;
}
if ((already_friends == 0) && (first_user->friends[i] == NULL)){
int stop = 0;
if (stop == 0){
first_user->friends[i] = second_user;
stop = 1;
error = 0;
fprintf(stdout, "%s\n", first_user->friends[i]->name);
}
stop += 0;
error += 0;
}
}
for (int j = 0; j < 10; j++){
int already_friends1 = 0;
if((strcmp(second_user->friends[j]->name, first_user->name) == 0) && second_user->friends[j] != NULL){
if (error < 1){
error = 1;
already_friends1 = 1;
}
error += 0;
already_friends1 += 0;
}
else{
// add to each friend's lst
if ((already_friends1 == 0) && (second_user->friends[j] == NULL)){
int stop1 = 0;
if (stop1 == 0){
second_user->friends[j] = first_user;
stop1 = 1;
error = 0;
fprintf(stdout, "%s\n", second_user->friends[j]->name);
}
error += 0;
stop1 += 0;
}
}
}
}
else {
if (error < 2){
error = 2;
}
error += 0;
}
}
if (error != 0){
return error;
}
return error;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-11T18:10:43.423Z
|
Question from Code
|
v1
|
any errors?
|
int make_friends(const char *name1, const char *name2, User *head) {
int error = 0;
// check if the two names are the same
if (strcmp(name1, name2) == 0){
error = 3;
}
else{
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
// check if users exist or not
if (first_user == NULL || second_user == NULL){
return 4;
}
// check if either user's have 10 friends or not
if ((first_user->friends[9] == NULL) && (second_user->friends[9] == NULL)){
int already_friends = 0;
for (int i = 0; i < 10; i++){
// check if the second user is already friends with first user and that it's not NULL
if((strcmp(first_user->friends[i]->name, second_user->name) ==0) && (first_user->friends[i] != NULL)){
if (error < 1){
error = 1;
already_friends = 1;
}
error += 0;
already_friends += 0;
}
if ((already_friends == 0) && (first_user->friends[i] == NULL)){
int stop = 0;
if (stop == 0){
first_user->friends[i] = second_user;
stop = 1;
error = 0;
fprintf(stdout, "%s\n", first_user->friends[i]->name);
}
stop += 0;
error += 0;
}
}
for (int j = 0; j < 10; j++){
int already_friends1 = 0;
if((strcmp(second_user->friends[j]->name, first_user->name) == 0) && second_user->friends[j] != NULL){
if (error < 1){
error = 1;
already_friends1 = 1;
}
error += 0;
already_friends1 += 0;
}
else{
// add to each friend's lst
if ((already_friends1 == 0) && (second_user->friends[j] == NULL)){
int stop1 = 0;
if (stop1 == 0){
second_user->friends[j] = first_user;
stop1 = 1;
error = 0;
fprintf(stdout, "%s\n", second_user->friends[j]->name);
}
error += 0;
stop1 += 0;
}
}
}
}
else {
if (error < 2){
error = 2;
}
error += 0;
}
}
if (error != 0){
return error;
}
return error;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-11T18:15:01.635Z
|
Help Fix Code
|
v1
| null |
int make_friends(const char *name1, const char *name2, User *head) {
int error = 0;
// check if the two names are the same
if (strcmp(name1, name2) == 0){
error = 3;
}
else{
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
// check if users exist or not
if (first_user == NULL || second_user == NULL){
return 4;
}
// check if either user's have 10 friends or not
if ((first_user->friends[9] == NULL) && (second_user->friends[9] == NULL)){
int already_friends = 0;
for (int i = 0; i < 10; i++){
// check if the second user is already friends with first user and that it's not NULL
if((strcmp(first_user->friends[i]->name, second_user->name) ==0) && (first_user->friends[i] != NULL)){
if (error < 1){
error = 1;
already_friends = 1;
}
error += 0;
already_friends += 0;
}
if ((already_friends == 0) && (first_user->friends[i] == NULL)){
int stop = 0;
if (stop == 0){
first_user->friends[i] = second_user;
stop = 1;
error = 0;
fprintf(stdout, "%s\n", first_user->friends[i]->name);
}
stop += 0;
error += 0;
}
}
for (int j = 0; j < 10; j++){
int already_friends1 = 0;
if((strcmp(second_user->friends[j]->name, first_user->name) == 0) && second_user->friends[j] != NULL){
if (error < 1){
error = 1;
already_friends1 = 1;
}
error += 0;
already_friends1 += 0;
}
else{
// add to each friend's lst
if ((already_friends1 == 0) && (second_user->friends[j] == NULL)){
int stop1 = 0;
if (stop1 == 0){
second_user->friends[j] = first_user;
stop1 = 1;
error = 0;
fprintf(stdout, "%s\n", second_user->friends[j]->name);
}
error += 0;
stop1 += 0;
}
}
}
}
else {
if (error < 2){
error = 2;
}
error += 0;
}
}
if (error != 0){
return error;
}
return error;
}
|
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.
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.
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-11T18:17:07.776Z
|
Help Fix Code
|
v1
| null |
int make_friends(const char *name1, const char *name2, User *head) {
int error = 0;
// check if the two names are the same
if (strcmp(name1, name2) == 0){
error = 3;
}
else{
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
// check if users exist or not
if (first_user == NULL || second_user == NULL){
return 4;
}
// check if either user's have 10 friends or not
if ((first_user->friends[9] == NULL) && (second_user->friends[9] == NULL)){
int already_friends = 0;
for (int i = 0; i < 10; i++){
// check if the second user is already friends with first user and that it's not NULL
if((strcmp(first_user->friends[i]->name, second_user->name) ==0) && (first_user->friends[i] != NULL)){
if (error < 1){
error = 1;
already_friends = 1;
}
error += 0;
already_friends += 0;
}
if ((already_friends == 0) && (first_user->friends[i] == NULL)){
int stop = 0;
if (stop == 0){
first_user->friends[i] = second_user;
stop = 1;
error = 0;
fprintf(stdout, "%s\n", first_user->friends[i]->name);
}
stop += 0;
error += 0;
}
}
for (int j = 0; j < 10; j++){
int already_friends1 = 0;
if((strcmp(second_user->friends[j]->name, first_user->name) == 0) && second_user->friends[j] != NULL){
if (error < 1){
error = 1;
already_friends1 = 1;
}
error += 0;
already_friends1 += 0;
}
else{
// add to each friend's lst
if ((already_friends1 == 0) && (second_user->friends[j] == NULL)){
int stop1 = 0;
if (stop1 == 0){
second_user->friends[j] = first_user;
stop1 = 1;
error = 0;
fprintf(stdout, "%s\n", second_user->friends[j]->name);
}
error += 0;
stop1 += 0;
}
}
}
}
else {
if (error < 2){
error = 2;
}
error += 0;
}
}
if (error != 0){
return error;
}
return 0;
}
|
Return:
0 on success and 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.
1 if the two users are already friends.
2 if the users are not already friends, but at least one already has MAX_FRIENDS friends.
3 if the same user is passed in twice.
4 if at least one user does not exist.
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-11T18:19:28.623Z
|
Help Fix Code
|
v1
| null |
int make_friends(const char *name1, const char *name2, User *head) {
int error = 0;
// check if the two names are the same
if (strcmp(name1, name2) == 0){
error = 3;
}
else{
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
// check if users exist or not
if (first_user == NULL || second_user == NULL){
return 4;
}
// check if either user's have 10 friends or not
if ((first_user->friends[9] == NULL) && (second_user->friends[9] == NULL)){
int already_friends = 0;
for (int i = 0; i < 10; i++){
// check if the second user is already friends with first user and that it's not NULL
if((strcmp(first_user->friends[i]->name, second_user->name) ==0) && (first_user->friends[i] != NULL)){
if (error < 1){
error = 1;
already_friends = 1;
}
error += 0;
already_friends += 0;
}
if ((already_friends == 0) && (first_user->friends[i] == NULL)){
int stop = 0;
if (stop == 0){
first_user->friends[i] = second_user;
stop = 1;
error = 0;
fprintf(stdout, "%s\n", first_user->friends[i]->name);
}
stop += 0;
error += 0;
}
error += 0;
}
for (int j = 0; j < 10; j++){
int already_friends1 = 0;
if((strcmp(second_user->friends[j]->name, first_user->name) == 0) && second_user->friends[j] != NULL){
if (error < 1){
error = 1;
already_friends1 = 1;
}
error += 0;
already_friends1 += 0;
}
else{
// add to each friend's lst
if ((already_friends1 == 0) && (second_user->friends[j] == NULL)){
int stop1 = 0;
if (stop1 == 0){
second_user->friends[j] = first_user;
stop1 = 1;
error = 0;
fprintf(stdout, "%s\n", second_user->friends[j]->name);
}
error += 0;
stop1 += 0;
}
error += 0;
}
}
}
else {
if (error < 2){
error = 2;
}
error += 0;
}
}
if (error != 0){
return error;
}
return 0;
}
|
segmentation fault ?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-11T18:28:13.542Z
|
Question from Code
|
v1
|
why segmentation fault?
|
int make_friends(const char *name1, const char *name2, User *head) {
int error = 0;
// check if the two names are the same
if (strcmp(name1, name2) == 0){
error = 3;
}
else{
User *first_user = find_user(name1, head);
User *second_user = find_user(name2, head);
// check if users exist or not
if (first_user == NULL || second_user == NULL){
return 4;
}
// check if either user's have 10 friends or not
if ((first_user->friends[9] == NULL) && (second_user->friends[9] == NULL)){
int already_friends = 0;
for (int i = 0; i < 10; i++){
// check if the second user is already friends with first user and that it's not NULL
if((strcmp(first_user->friends[i]->name, second_user->name) ==0) && (first_user->friends[i] != NULL)){
if (error < 1){
error = 1;
already_friends = 1;
}
error += 0;
already_friends += 0;
}
if ((already_friends == 0) && (first_user->friends[i] == NULL)){
int stop = 0;
if (stop == 0){
first_user->friends[i] = second_user;
stop = 1;
error = 0;
fprintf(stdout, "%s\n", first_user->friends[i]->name);
}
stop += 0;
error += 0;
}
error += 0;
}
for (int j = 0; j < 10; j++){
int already_friends1 = 0;
if(strcmp(second_user->friends[j]->name, first_user->name) == 0 && second_user->friends[j] != NULL){
if (error < 1){
error = 1;
already_friends1 = 1;
}
error += 0;
already_friends1 += 0;
}
else{
// add to each friend's lst
if (already_friends1 == 0 && second_user->friends[j] == NULL){
int stop1 = 0;
if (stop1 == 0){
second_user->friends[j] = first_user;
stop1 = 1;
error = 0;
fprintf(stdout, "%s\n", second_user->friends[j]->name);
}
error += 0;
stop1 += 0;
}
error += 0;
}
}
}
else {
if (error < 2){
error = 2;
}
error += 0;
}
}
if (error != 0){
return error;
}
return 0;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T19:19:23.670Z
|
Help Write Code
|
v1
| null | null | 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
|
63c6487e5f004bf4898cfcfc
|
2023-02-13T19:25:13.148Z
|
Question from Code
|
v1
|
I need to assign the contents of type char* to the new post contents which is type char * post. Am i doing this right
|
Post *new_post;
strcpy(new_post->author, author->name);
new_post->contents = contents;
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T19:26:09.144Z
|
Help Fix Code
|
v1
| null |
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
}
for (int j = 0; j < 10; j++){
// doing this for author cuz if they're friends - it's symmetric
if (target->friends[j]->name != author->name){
return 1;
}
}
// create new post struct
Post *new_post;
strcpy(new_post->author, author->name);
new_post->contents = contents;
if (target->first_post->next == NULL){
target->first_post->next = new_post;
}
Post *curr = target->first_post->next;
target->first_post->next = new_post;
new_post->next = curr;
return 0;
}
|
This function is supposed to do the following.
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
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T19:27:43.535Z
|
General Question
|
v1
|
how do i allocate memory for a struct of the following:
typedef struct post {
char author[MAX_NAME];
char *contents;
time_t *date;
struct post *next;
} Post;
| null | null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T19:29:00.901Z
|
Help Fix Code
|
v1
| null |
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
}
for (int j = 0; j < 10; j++){
// doing this for author cuz if they're friends - it's symmetric
if (target->friends[j]->name != author->name){
return 1;
}
}
// create new post struct
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
new_post->contents = contents;
if (target->first_post->next == NULL){
target->first_post->next = new_post;
}
Post *curr = target->first_post->next;
target->first_post->next = new_post;
new_post->next = curr;
return 0;
}
|
why segmentation fault?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T19:33:02.613Z
|
Help Fix Code
|
v1
| null |
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
}
for (int j = 0; j < 10; j++){
// doing this for author cuz if they're friends - it's symmetric
if ((strcmp(target->friends[j]->name, author->name) != 0) && (target->friends[j] != NULL)){
return 1;
}
}
// create new post struct
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
new_post->contents = contents;
if (target->first_post->next == NULL){
target->first_post->next = new_post;
}
Post *curr = target->first_post->next;
target->first_post->next = new_post;
new_post->next = curr;
return 0;
}
|
why segmentation fault?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T19:33:50.860Z
|
Help Fix Code
|
v1
| null |
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
}
for (int j = 0; j < 10; j++){
// doing this for author cuz if they're friends - it's symmetric
if ((strcmp(target->friends[j]->name, author->name) != 0) && (target->friends[j] != NULL)){
return 1;
}
}
// create new post struct
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
new_post->contents = contents;
if (target->first_post->next == NULL){
target->first_post->next = new_post;
}
Post *curr = target->first_post->next;
target->first_post->next = new_post;
new_post->next = curr;
return 0;
}
|
why segmentation fault?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T19:37:18.237Z
|
Question from Code
|
v1
|
why segmentation fault?
|
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
}
for (int j = 0; j < 10; j++){
// doing this for author cuz if they're friends - it's symmetric
if ((strcmp(target->friends[j]->name, author->name) != 0) && (target->friends[j] != NULL)){
return 1;
}
}
// create new post struct
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
new_post->contents = contents;
if (target->first_post == NULL){
target->first_post = new_post;
}
Post *curr = target->first_post;
target->first_post = new_post;
new_post->next = curr;
return 0;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T19:42:29.775Z
|
Question from Code
|
v1
|
is there segmentation fault?
|
int print_user(const User *user){
if (user == NULL){
return 1;
}
FILE *profile = fopen(user->profile_pic, "rb");
char string[1000] = "";
while(fgets(string, sizeof(string), profile)){
printf("%s\n", string);
}
return 0;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T20:25:28.646Z
|
Help Fix Code
|
v1
| null |
if (user->first_post == NULL){
return 0;
}
else{
Post *curr = user->first_post;
while(curr != NULL){
char *date_char = ctime(curr->date);
fprintf(stdout,"------------------------------------------\nPosts:\nFrom: %s\nDate: %s\n\n%s\n\n==\n", curr->author, date_char, curr->contents);
curr = curr->next;
}
}
|
what's wrong?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T20:26:32.664Z
|
Help Fix Code
|
v1
| null |
if (user->first_post == NULL){
return 0;
}
else{
Post *curr = user->first_post;
while(curr != NULL){
char *date_char = ctime(curr->date);
fprintf(stdout,"------------------------------------------\nPosts:\nFrom: %s\nDate: %s\n\n%s\n\n==\n", curr->author, date_char, curr->contents);
curr = curr->next;
}
return 0;
}
|
what's wrong with this code?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T20:27:28.989Z
|
Help Fix Code
|
v1
| null |
int print_user(const User *user){
if (user == NULL){
return 1;
}
if (strcmp(user->profile_pic, "") == 0){
return 0;
}
FILE *profile = fopen(user->profile_pic, "rb");
char string[1000] = "";
while(fgets(string, sizeof(string), profile)){
printf("%s", string);
}
fclose(profile);
fprintf(stdout, "\nName: %s\n------------------------------------------\nFriends:\n", user->name);
for (int i = 0; i < 10; i++){
if (user->friends[i] != NULL){
fprintf(stdout,"%s\n", user->friends[i]->name);
}
}
if (user->first_post == NULL){
return -1;
}
else{
Post *curr = user->first_post;
while(curr != NULL){
char *date_char = ctime(curr->date);
fprintf(stdout,"------------------------------------------\nPosts:\nFrom: %s\nDate: %s\n\n%s\n\n==\n", curr->author, date_char, curr->contents);
curr = curr->next;
}
}
return 0;
}
|
why segmentation fault?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T20:47:12.852Z
|
Question from Code
|
v1
|
is there a segmentation fault?
|
int print_user(const User *user){
if (user == NULL){
return 1;
}
if (strcmp(user->profile_pic, "") == 0){
return -1;
}
FILE *profile = fopen(user->profile_pic, "rb");
char string[1000] = "";
while(fgets(string, sizeof(string), profile)){
printf("%s", string);
}
fclose(profile);
fprintf(stdout, "\nName: %s\n------------------------------------------\nFriends:\n", user->name);
for (int i = 0; i < 10; i++){
if (user->friends[i] != NULL){
fprintf(stdout,"%s\n", user->friends[i]->name);
}
}
if (user->first_post != NULL){
Post *curr = user->first_post;
while(curr != NULL){
char *date_char = ctime(curr->date);
fprintf(stdout,"------------------------------------------\nPosts:\nFrom: %s\nDate: %s\n\n%s\n\n==\n", curr->author, date_char, curr->contents);
curr = curr->next;
}
}
return 0;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T20:47:31.538Z
|
Question from Code
|
v1
|
where is the segmentation fault?
|
int print_user(const User *user){
if (user == NULL){
return 1;
}
if (strcmp(user->profile_pic, "") == 0){
return -1;
}
FILE *profile = fopen(user->profile_pic, "rb");
char string[1000] = "";
while(fgets(string, sizeof(string), profile)){
printf("%s", string);
}
fclose(profile);
fprintf(stdout, "\nName: %s\n------------------------------------------\nFriends:\n", user->name);
for (int i = 0; i < 10; i++){
if (user->friends[i] != NULL){
fprintf(stdout,"%s\n", user->friends[i]->name);
}
}
if (user->first_post != NULL){
Post *curr = user->first_post;
while(curr != NULL){
char *date_char = ctime(curr->date);
fprintf(stdout,"------------------------------------------\nPosts:\nFrom: %s\nDate: %s\n\n%s\n\n==\n", curr->author, date_char, curr->contents);
curr = curr->next;
}
}
return 0;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T20:49:35.020Z
|
Question from Code
|
v1
|
what is wrong with this code chunk
|
if (user->first_post != NULL){
Post *curr = user->first_post;
while(curr != NULL){
char *date_char = ctime(curr->date);
fprintf(stdout,"------------------------------------------\nPosts:\nFrom: %s\nDate: %s\n\n%s\n\n==\n", curr->author, date_char, curr->contents);
curr = curr->next;
}
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T20:50:41.102Z
|
Help Fix Code
|
v1
| null |
if (user->first_post != NULL){
Post *curr = user->first_post;
while(curr != NULL){
char *date_char = ctime(curr->date);
fprintf(stdout,"------------------------------------------\nPosts:\nFrom: %s\nDate: %s\n\n%s\n\n==\n", curr->author, date_char, curr->contents);
curr = curr->next;
}
}
|
how to make the date_char refer to the current post
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T20:52:42.081Z
|
Help Fix Code
|
v1
| null |
int print_user(const User *user){
if (user == NULL){
return 1;
}
if (strcmp(user->profile_pic, "") == 0){
return -1;
}
FILE *profile = fopen(user->profile_pic, "rb");
char string[1000] = "";
while(fgets(string, sizeof(string), profile)){
printf("%s", string);
}
fclose(profile);
fprintf(stdout, "\nName: %s\n------------------------------------------\nFriends:\n", user->name);
for (int i = 0; i < 10; i++){
if (user->friends[i] != NULL){
fprintf(stdout,"%s\n", user->friends[i]->name);
}
}
if (user->first_post != NULL){
Post *curr = user->first_post;
char *date_char;
while(curr != NULL){
date_char = ctime(curr->date);
fprintf(stdout,"------------------------------------------\nPosts:\nFrom: %s\nDate: %s\n\n%s\n\n==\n", curr->author, date_char, curr->contents);
curr = curr->next;
}
}
return 0;
}
|
why can't I print the posts of the user?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-13T21:32:35.060Z
|
Help Fix Code
|
v1
| null |
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
}
int friends = 0;
for (int j = 0; j < 10; j++){
// doing this for author cuz if they're friends - it's symmetric
if (target->friends[j] != NULL && strcmp(target->friends[j]->name, author->name) == 0){
friends = 1;
}
friends += 0;
}
if (friends != 0){ // means that they are not friends
// create new post struct as they are friends
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
strcpy(new_post->contents, contents);
time_t local_time;
time(&local_time);
new_post->date = &local_time;
if (target->first_post == NULL){
target->first_post = new_post;
}
Post *curr = target->first_post;
target->first_post = new_post;
new_post->next = curr;
return 0;
}
return 1;
}
|
why segmentation fault?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-14T02:55:44.172Z
|
General Question
|
v1
|
how to add the current time to a struct attribute of type time_t *date?
| null | null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-14T02:57:19.721Z
|
Question from Code
|
v1
|
why do i get this error - incompatible integer to pointer conversion assigning to time_t *.
|
new_post->date = time(0);
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-14T02:58:42.152Z
|
Question from Code
|
v1
|
how to dereference new_post->date pointer?
|
new_post->date = time(0);
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-14T02:59:27.629Z
|
General Question
|
v1
|
how to dereference new_post->date pointer?
| null | null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-14T03:05:23.496Z
|
Help Fix Code
|
v1
| null |
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
}
int friends = 0;
for (int j = 0; j < 10; j++){
// doing this for author cuz if they're friends - it's symmetric
if (target->friends[j] != NULL && strcmp(target->friends[j]->name, author->name) == 0){
friends = 0;
}
friends += 1;
}
if (friends == 0){ // means that they are friends
// create new post struct as they are friends
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
strcpy(new_post->contents, contents);
*new_post->date = time(0);
if (target->first_post == NULL){
target->first_post = new_post;
}
Post *curr = target->first_post;
target->first_post = new_post;
new_post->next = curr;
return 0;
}
return 1;
}
|
does the function do the following?
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
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-14T03:17:41.362Z
|
General Question
|
v1
|
how do i print the contents of each node in a linked list using fprintf
| null | null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-14T03:19:24.922Z
|
General Question
|
v1
|
how do i print the contents of each node in a linked list using fprintf
What is my while loop condition?
| null | null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-14T03:20:10.530Z
|
Question from Code
|
v1
|
why does the while loop never end?
|
while (head != NULL){
fprintf(stdout, "Name: %s\n", head->author);
head = head->next;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-14T03:20:48.177Z
|
Question from Code
|
v1
|
how to change the head pointer in the loop
|
while (head != NULL){
fprintf(stdout, "Name: %s\n", head->author);
head = head->next;
}
| null | null |
63c6487e5f004bf4898cfcfc
|
2023-02-14T03:21:46.481Z
|
Help Fix Code
|
v1
| null |
Post *head = user->first_post;
while (head != NULL){
fprintf(stdout, "Name: %s\n", head->author);
head = head->next;
}
|
how to make this while loop work
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-14T03:22:22.100Z
|
Help Fix Code
|
v1
| null |
Post *head = user->first_post;
while (head != NULL){
fprintf(stdout, "Name: %s\n", head->author);
head = head->next;
}
|
what's wrong
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-14T03:24:04.666Z
|
Help Fix Code
|
v1
| null |
Post *head = user->first_post;
while (head != NULL){
fprintf(stdout, "Name: %s\n", head->author);
head = head->next;
}
|
it is supposed to print every name of each node in the linked list so what's wrong?
| null |
63c6487e5f004bf4898cfcfc
|
2023-02-14T03:47:35.423Z
|
Question from Code
|
v1
|
how to not end up in an infinite loop
|
Post *head = user->first_post;
while (head != NULL){
fprintf(stdout, "Name: %s\n", head->author);
head = head->next;
}
| null | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.