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
⌀ |
---|---|---|---|---|---|---|---|
63c648885f004bf4898cfd5b | 2023-01-24T21:12:12.803Z | Question from Code | v1 | what should this return? | int *build_array(char **strs, int size) {
int num_arr[size];
for (int i = 0; i < size; i++){
num_arr[i] = strtol(strs[i], NULL, 10);
}
} | null | null |
63c648885f004bf4898cfd5b | 2023-01-24T22:30:01.865Z | Help Fix Code | v1 | null | char *greetings = malloc(sizeof(char) * 16);
greetings[] = "salutations"; | define greetings with starting value string salutations
| null |
63c648895f004bf4898cfd60 | 2023-02-14T05:40:52.620Z | Help Fix Code | v1 | null | int make_friends(const char *name1, const char *name2, User *head) {
// Case 4: If at least one user does not exist, Return 4
User *friend2 = head;
User *friend1 = head;
int exist = 0;
User *curr = head;
while (curr != NULL) {
char *curr_name = curr->name;
if(strcmp(name1, curr_name) == 0) {
friend1 = curr;
exist++;
}
if(strcmp(name2, curr_name) == 0) {
friend2 = curr;
exist++;
}
curr = curr->next;
}
if (exist != 2) {
return 4;
}
// Case 3: Same user is passed in twice, Return 3
if(strcmp(name1, name2) == 0){
return 3;
}
// Case 1: If already friends
User friends1[MAX_FRIENDS] = friend1->friends;
int length1 = sizeof(friends1);
for (int i = 0; i < length1; i++) {
char *friend_name11 = friends1[i]->name;
if(strcmp(friend_name11, name2) == 0){
exist++;
}
}
User friends2[MAX_FRIENDS] = friend2->friends;
int length2 = sizeof(friends2);
for (int i = 0; i < length2; i++) {
char *friend_name22 = friends1[i]->name;
if(strcmp(friend_name22, name1) == 0){
exist++;
}
}
if (exist == 4){
return 1;
}
//Case 2: If the users are not already friends, but at least one already has MAX_FRIENDS friends
if((length1 == MAX_FRIENDS) | (length2 == MAX_FRIENDS)) {
return 2;
}
//Case 0: Make two users friends with each other
for (int i = 0; i < length1; i++) {
char *friend_name1 = friends1[i]->name;
if(strcmp(friend_name1, NULL) == 0){
friends1[i] = friend2;
}
}
for (int i = 0; i < length2; i++) {
char *friend_name2 = friends2[i]->name;
if(strcmp(friend_name2, NULL) == 0){
friends2[i] = friend1;
}
}
return 0;
}
| Make friends function, | null |
63c648895f004bf4898cfd60 | 2023-02-14T05:43:07.227Z | General Question | v1 | Suppose
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;
How do I define a pointer to *friends?
| null | null | null |
63c648895f004bf4898cfd60 | 2023-02-14T05:50:10.865Z | Help Fix Code | v1 | null | typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[32];
struct user *next;
} User;
int something(const char *name. User *head ){
User *friend = head;
User *friend1[] = friend->friends;
return 1;
} | I am trying to declare a pointer to friends
| null |
63c648895f004bf4898cfd60 | 2023-02-14T06:18:17.559Z | Question from Code | v1 | Does this work? | 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[32];
struct user *next;
} User;
int something(const char *name. User *head ){
User *friend = head;
char *friend_name1 = (friend->friends[i])->name;
return 1;
} | null | null |
63c648895f004bf4898cfd60 | 2023-02-14T06:19:41.856Z | General Question | v1 | How does a segmentation fault happen? | null | null | null |
63c648895f004bf4898cfd60 | 2023-02-14T21:58:17.810Z | General Question | v1 | What's the format specific to print a time_t? | null | null | null |
63c648895f004bf4898cfd60 | 2023-03-13T04:35:49.173Z | General Question | v2 | why does action->arg[x] give me a segmentation fault? | null | null | null |
63c648895f004bf4898cfd60 | 2023-03-13T04:37:56.265Z | Help Fix Code | v2 | want to store in struct | typedef struct action_node {
char **args; // An array of strings suitable to be passed to execvp
struct action_node *next_act;
} Action;
Action *actions;
actions->args[1] = "hello"; | null | null |
63c6488a5f004bf4898cfd6a | 2023-01-30T08:29:36.077Z | General Question | v1 | how do i call ls from within a file to read? | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-01-30T08:30:48.920Z | Explain Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
int main() {
system("ls");
return 0;
} | null | null |
63c6488a5f004bf4898cfd6a | 2023-01-30T08:33:44.488Z | General Question | v1 | how could you call lf -l from within a file and read the output?
| null | null | null |
63c6488a5f004bf4898cfd6a | 2023-01-30T08:43:27.459Z | General Question | v1 | when reading using scanf, can you skip some inputs | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-01-30T08:54:40.085Z | General Question | v1 | what does !(argc == 2 || argc == 3) mean | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-01-30T09:04:39.039Z | General Question | v1 | what does (fscanf(ls, "%s %*d %*s %*s %d %*s %*d %*s %*s", perms, &size) == 2) do | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-01-30T09:07:26.308Z | General Question | v1 | how to get certain parts of a string
| null | null | null |
63c6488a5f004bf4898cfd6a | 2023-01-30T09:17:03.653Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
int check_permissions(char *perms, char *reqs){
char *a = perms + 1;
if (strcmp(a, reqs) == 0) { //has required permissions
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.
FILE *ls = popen("ls -l", "r"); //suggested using popen by Code Inquiry Assistant
int min = argv[1];
char reqs = argv[2];
int size;
char perms;
int total = 0;
while (fscanf(ls, "%s %*d %*s %*s %d %*s %*d %*s %*s", perms, &size) == 2) {
if (size > min) {
if (if argc == 2) {
total += 1;
} else {
if (check_permissions(perms, reqs)) {
total += 1;
}
}
}
// TODO: Call check_permissions as part of your solution to count the files to
// compute and print the correct value.
return 0;
} | when called from the command line with a number and an optional string of permissions, return how many files have a size larger than the specified input | null |
63c6488a5f004bf4898cfd6a | 2023-01-30T09:44:05.761Z | Help Fix Code | v1 | null | int check_permissions(char *perms, char *reqs){
int a = 0;
for (a; i < 3; a++) {
for (int i; i < 3; i++) {
if (perms[1 + (3 * a) + i] != reqs[(3 * a) + i]){
break;
} else {
return 0;
}
}
}
return 1;
} | compare whether three sequences of chars are the same. if not, check the next three sequences. | null |
63c6488a5f004bf4898cfd6a | 2023-01-30T10:03:59.184Z | Help Fix Code | v1 | null | int check_permissions(char *perms, char *reqs){
char *c = perms + 1;
for (int a = 0; a < 3; a++) {
if (c[(3 * a)] == reqs[(3 * a)] && c[(3 * a) + 1] == reqs[(3 * a) + 1] && c[(3 * a) + 2] == reqs[(3 * a) + 2]) {
return 0;
}
}
return 1;
} | compare whether three sequences of chars are the same. if they are the same, return 0. if not, check the next three characters | null |
63c6488a5f004bf4898cfd6a | 2023-01-30T10:47:01.147Z | General Question | v1 | how to run ls -l in the command line from a file | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-01-30T10:50:44.509Z | General Question | v1 | how to run ls -l in the command line from a file and read it with scanf | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-01-30T11:28:28.500Z | General Question | v1 | how to add one to a value | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-02-03T22:32:50.611Z | General Question | v1 | how to find where the '\0' character is in a string | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-02-03T22:44:16.047Z | General Question | v1 | how to use bool type | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-02-03T22:51:01.603Z | General Question | v1 | how to compare char variables
| null | null | null |
63c6488a5f004bf4898cfd6a | 2023-02-12T23:11:21.142Z | General Question | v1 | how to insert to end of list | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-02-13T02:51:21.330Z | General Question | v1 | how to find an element in an array
| null | null | null |
63c6488a5f004bf4898cfd6a | 2023-02-13T02:52:24.714Z | General Question | v1 | how to compare strings | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-02-13T02:56:29.547Z | General Question | v1 | how to get the length of a string | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-02-13T03:21:18.244Z | General Question | v1 | how to get the size of an array | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-02-13T03:50:34.998Z | Explain Code | v1 | null | typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[MAX_FRIENDS];
struct user *next;
} User; | null | null |
63c6488a5f004bf4898cfd6a | 2023-02-13T04:55:12.002Z | General Question | v1 | what happens if you call fopen for a file that does not exist
| null | null | null |
63c6488a5f004bf4898cfd6a | 2023-02-15T10:07:02.270Z | General Question | v1 | How to print string variables using format specifiers | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-02-15T19:49:48.096Z | Help Fix Code | v1 | null | typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[MAX_FRIENDS];
struct user *next;
} User;
int print_user(const User *user) {
if (user == NULL) {
return 1;
}
char *strike = "------------------------------------------\n";
char *name, *friends, *posts;
name = "Name: ";
strcat(name, user->name);
strcat(name, "\n\0");
int i = 0;
friends = "Friends:\n";
while (i < MAX_FRIENDS && (user->friends)[i] != NULL) {
strcat(friends, ((user->friends)[i])->name);
strcat(friends, "\n");
}
strcat(friends, "\0");
posts = "Posts:\n";
printf("%s%s%s%s%s%s", name, strike, friends, strike, posts, strike);
return 0;
} | Print a User profile formatted as
Name: <name>
------------------------------------------
Friends: <friends>
------------------------------------------
Posts:
------------------------------------------
Return 0 if successful
Return 1 if User is null | null |
63c6488a5f004bf4898cfd6a | 2023-02-15T19:58:50.812Z | General Question | v1 | when you malloc a struct, do you also have to malloc its variables
| null | null | null |
63c6488a5f004bf4898cfd6a | 2023-03-09T10:13:40.251Z | General Question | v2 | what does this do
git config --global http.sslverify false | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-03-10T06:50:26.073Z | General Question | v2 | how do you create a different process in C and get a return value | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-03-14T02:15:04.560Z | General Question | v2 | how to return to the while loop condition without going over the rest of the code inside the loop | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-03-14T02:37:12.052Z | General Question | v2 | how to malloc a struct | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-03-14T08:42:14.266Z | General Question | v2 | does malloc share over forks | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-03-14T08:57:43.591Z | General Question | v2 | can you make a char array of unspecified length? | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-03-14T09:05:52.815Z | General Question | v2 | how to malloc the string "hello" | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-03-14T09:07:20.852Z | General Question | v2 | what's the different between fread and fgets | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-03-14T09:11:52.143Z | Help Fix Code | v2 | return the length of "ay" | int main() {
char *a = "ay";
int x;
x = strlen(a);
return x;
} | null | null |
63c6488a5f004bf4898cfd6a | 2023-03-15T06:55:27.155Z | General Question | v2 | do you have to specify array size in malloc | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-03-15T06:57:32.215Z | General Question | v2 | how to make an array of strings | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-03-15T09:10:28.488Z | General Question | v2 | how to allocate space for a char* array of unspecified length | null | null | null |
63c6488a5f004bf4898cfd6a | 2023-03-15T09:49:44.724Z | Help Fix Code | v2 | allocate memory for an array of strings with ar_count + 1 elements | char *args[ar_count + 1] = malloc(sizeof(char*) * (ar_count + 1)); | null | null |
63c6488a5f004bf4898cfd6a | 2023-03-15T09:54:23.681Z | General Question | v2 | how to malloc array type | null | null | null |
63c6488b5f004bf4898cfd74 | 2023-02-14T04:59:33.269Z | Explain Code | v1 | null | #define MAX = 12;
char name[MAX]; | null | null |
63c6488b5f004bf4898cfd74 | 2023-02-14T05:00:32.250Z | Question from Code | v1 | what will line 3 look like after the C pre-compiler is run? | #define MAX = 12;
char name[MAX]; | null | null |
63c6488c5f004bf4898cfd7e | 2023-01-17T19:46:39.221Z | General Question | v1 | how do I dynamically allocated memory for a string in C
| null | null | null |
63c6488c5f004bf4898cfd7e | 2023-01-27T07:35:33.301Z | Help Fix Code | v1 | null | void fib(int **fib_sequence, int n){
//create an array based on n
int i;
fib_sequence = malloc(sizeof(int*));
if (n==1){
fib_sequence[0] = malloc(2*sizeof(int));
fib_sequence[0][0] = 0;
fib_sequence[0][1] = 1;
}
else{
fib_sequence[0][0] = 0;
fib_sequence[0][1] = 1;
for (i=2; i<n; i++){
fib_sequence[0][i] = fib_sequence[0][i-1] + fib_sequence[0][i-2];
}
}
}
int main(int argc, char **argv) {
/* do not change this main function */
int count = strtol(argv[1], NULL, 10);
//int count = 2;
int *fib_sequence;
fib(&fib_sequence, count);
for (int i = 0; i < count; i++) {
printf("%d ", fib_sequence[i]);
}
free(fib_sequence);
return 0;
}
| allocates memory to a given array and stores values of the fibonacci sequence up to n numbers
| null |
63c6488c5f004bf4898cfd7e | 2023-01-27T07:37:21.154Z | Help Fix Code | v1 | null | void fib(int **fib_sequence, int n){
//create an array based on n
int i;
fib_sequence = malloc(sizeof(int*));
if (n==1){
*fib_sequence[0] = malloc(2*sizeof(int));
*fib_sequence[0][0] = 0;
*fib_sequence[0][1] = 1;
}
else{
*fib_sequence[0][0] = 0;
*fib_sequence[0][1] = 1;
for (i=2; i<n; i++){
fib_sequence[0][i] = fib_sequence[0][i-1] + fib_sequence[0][i-2];
}
}
}
int main(int argc, char **argv) {
/* do not change this main function */
int count = strtol(argv[1], NULL, 10);
//int count = 2;
int *fib_sequence;
fib(&fib_sequence, count);
for (int i = 0; i < count; i++) {
printf("%d ", fib_sequence[i]);
}
free(fib_sequence);
return 0;
}
| allocates memory to a given array and stores values of the fibonacci sequence up to n numbers | null |
63c6488c5f004bf4898cfd7e | 2023-01-27T09:39:21.587Z | Help Fix Code | v1 | null | int main() {
int *full_array[3] = {1, 2, 3};
int **result = split_array(full_array, 3);
return 0;
}
int **split_array(const int *s, int length) {
int **val = malloc(sizeof(int*)*2);
int i;
if (length % 2 == 0){
val[0] = malloc(sizeof(int)*(length/2));
val[1] = malloc(sizeof(int)*(length/2));
for (i=0; i<(length/2); i+=2){
val[0][i] = s[i];
val[1][i] = s[i+1];
}
}
else{
val[0] = malloc(sizeof(int)*((length/2)+1));
val[1] = malloc(sizeof(int)*((length/2)));
for (i=0; i<(length/2); i+=2){
val[0][i] = s[i];
val[1][i] = s[i+1];
}
val[0][i-1] = s[i-1];
}
return val;
} | returns 0 | null |
63c6488c5f004bf4898cfd7e | 2023-01-27T09:40:24.386Z | Help Fix Code | v1 | null | int main() {
int full_array[3] = {1, 2, 3};
int **result = split_array(&full_array, 3);
return 0;
}
int **split_array(const int *s, int length) {
int **val = malloc(sizeof(int*)*2);
int i;
if (length % 2 == 0){
val[0] = malloc(sizeof(int)*(length/2));
val[1] = malloc(sizeof(int)*(length/2));
for (i=0; i<(length/2); i+=2){
val[0][i] = s[i];
val[1][i] = s[i+1];
}
}
else{
val[0] = malloc(sizeof(int)*((length/2)+1));
val[1] = malloc(sizeof(int)*((length/2)));
for (i=0; i<(length/2); i+=2){
val[0][i] = s[i];
val[1][i] = s[i+1];
}
val[0][i-1] = s[i-1];
}
return val;
} | return 0 | null |
63c6488c5f004bf4898cfd7e | 2023-01-27T09:40:51.846Z | Help Fix Code | v1 | null | int main() {
int full_array[3] = {1, 2, 3};
int **result = split_array(full_array, 3);
return 0;
}
int **split_array(const int *s, int length) {
int **val = malloc(sizeof(int*)*2);
int i;
if (length % 2 == 0){
val[0] = malloc(sizeof(int)*(length/2));
val[1] = malloc(sizeof(int)*(length/2));
for (i=0; i<(length/2); i+=2){
val[0][i] = s[i];
val[1][i] = s[i+1];
}
}
else{
val[0] = malloc(sizeof(int)*((length/2)+1));
val[1] = malloc(sizeof(int)*((length/2)));
for (i=0; i<(length/2); i+=2){
val[0][i] = s[i];
val[1][i] = s[i+1];
}
val[0][i-1] = s[i-1];
}
return val;
} | return 0 | null |
63c6488c5f004bf4898cfd7e | 2023-02-09T20:07:18.919Z | 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) {
fseek(image, 10 , SEEK_SET);
fread(pixel_array_offset, sizeof(int), 1, image);
fseek(image, 18, SEEK_SET);
fread(width, sizeof(int), 1, image);
fseek(image, 22, SEEK_SET);
fread(height, sizeof(int), 1, image);
}
/*
* Read in pixel array by following these instructions:
*
* 1. First, allocate space for m `struct pixel *` values, where m is the
* height of the image. Each pointer will eventually point to one row of
* pixel data.
* 2. For each pointer you just allocated, initialize it to point to
* heap-allocated space for an entire row of pixel data.
* 3. Use the given file and pixel_array_offset to initialize the actual
* struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is
* consistent with the bitmap file format.
* NOTE: We've tested this assumption on the Teaching Lab machines, but
* if you're trying to work on your own computer, we strongly recommend
* checking this assumption!
* 4. Return the address of the first `struct pixel *` you initialized.
*/
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height){
//3*width per row; m = height
struct pixel **s = malloc(sizeof(struct pixel *) * height);
int i;
for (i = 0; i < height; i++){
s[i] = malloc(3*width);
fseek(image, pixel_array_offset, SEEK_SET);
}
int n, j;
if (sizeof(struct pixel) == 3){
fseek(image, pixel_array_offset, SEEK_SET);
for (n = 0; n < height; n+=3){
for (j = 0; j < width; j+=3) {
fread(&s[n][j].blue, 1, 1, image);
fread(&s[n][j].green, 1, 1, image);
fread(&s[n][j].red, 1, 1, image);
}
}
}
return s;
}
/*
* 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);
} | reads pixel values from image source into struct | null |
63c6488c5f004bf4898cfd7e | 2023-02-11T21:13:27.052Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > 31){
return 2;
}
else{
User *p = malloc(sizeof(User));
strncpy(p->name, name, 31);
p->name[31] = '\0';
User *curr = *user_ptr_add; // seg fault here
printf("%s\n", curr->name);
while (curr->next != NULL){
printf("made inside while loop\n");
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
return -1;
} | Remove segment fault | null |
63c6488c5f004bf4898cfd7e | 2023-02-11T21:25:45.893Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > 31){
return 2;
}
else{
User *p = malloc(sizeof(User));
strncpy(p->name, name, 31);
p->name[31] = '\0';
User curr = **user_ptr_add; // seg fault here
printf("%s\n", curr.name);
while (curr.next != NULL){
printf("made inside while loop\n");
if (strcmp(curr.name, name) == 0){
return 1;
}
curr = *(curr.next);
}
curr.next = p;
return 0;
}
return -1;
} | fix segmentation fault
| null |
63c6488c5f004bf4898cfd7e | 2023-02-11T21:28:54.242Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > 31){
return 2;
}
else{
User *p = malloc(sizeof(User));
strncpy(p->name, name, 31);
p->name[31] = '\0';
User curr = **user_ptr_add; // seg fault here
printf("%s\n", *curr.name);
while (*curr.next != NULL){
printf("made inside while loop\n");
if (strcmp(*curr.name, name) == 0){
return 1;
}
*curr = *(curr.next);
}
*(curr.next) = p;
return 0;
}
return -1;
} | segmentation fault | null |
63c6488c5f004bf4898cfd7e | 2023-02-12T13:29:06.412Z | Help Fix Code | v1 | null | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
else if (*user_ptr_add == NULL){
*user_ptr_add = malloc(sizeof(User));
strncpy((*user_ptr_add)->name, name, (MAX_NAME - 1));
(*user_ptr_add)->name[(MAX_NAME - 1)] = '\0';
(*user_ptr_add)->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
return 0;
}
else{
User *p = malloc(sizeof(User));
strncpy(p->name, name, (MAX_NAME - 1));
p->name[(MAX_NAME - 1)] = '\0';
p->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
User *curr = *user_ptr_add;
while (curr->next != NULL){
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
return -1;
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
while (curr != NULL){
if (strcmp(curr->name, name) == 0){
return &(*curr);
}
curr = curr->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n");
if (curr != NULL){
User *cur = (User *) curr;
while (cur != NULL){
printf("\t%s\n", cur->name);
cur = cur->next;
}
}
}
/*
* Change the filename for the profile pic of the given user.
*
* Return:
* - 0 on success.
* - 1 if the file does not exist or cannot be opened.
* - 2 if the filename is too long.
*/
int update_pic(User *user, const char *filename) {
FILE *error = fopen(filename, "r");
if (error == NULL){return 1;}
else if (strlen(filename) > (MAX_NAME - 1)){fclose(error); return 2;}
else{
strncpy(user->profile_pic, filename, (MAX_NAME - 1));
user->profile_pic[(MAX_NAME - 1)] = '\0';
fclose(error);
return 0;
}
fclose(error);
return -1;
}
/*
* 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) {
if (strcmp(name1, name2) == 0){return 3;}
else if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){return 4;}
User *first = find_user(name1, head);
User *second = find_user(name2, head);
int i=0, j=0;
int val = 0;
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val = 1;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val = 1;}
j++;
}
if (val == 1){return 1;}
else if ((i == (MAX_FRIENDS - 1)) || (j == (MAX_FRIENDS - 1))){return 2;}
else {
(first->friends)[i] = second;
(second->friends)[j] = first;
return 0;
}
return -1;
}
/*
* 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) {
char *dashes = {"------------------------------------------"};
if (user == NULL){return -1;}
else{
FILE *profile = fopen(user->profile_pic, "r");
if (profile == NULL){
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends[i])->name); i++;}
printf("%s\nPosts:\n", dashes);
if (profile != NULL){fclose(profile);}
return 0;
}
else {
char line[1000];
while (fgets(line, 1000, profile) != NULL){
printf("%s", line);
}
printf("\n");
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends[i])->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){printf("%s", dashes);}
else{
printf("\n===\n\n");
}
}
if (profile != NULL){fclose(profile);}
return 0;
}
}
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){return 2;}
int i=0, j=0;
int val = 0;
while ((author->friends)[i] != NULL){
if (strcmp(((author->friends)[i])->name, target->name) == 0){val = 1;}
i++;
}
while ((target->friends)[j] != NULL){
if (strcmp(((target->friends)[j])->name, author->name) == 0){val = 1;}
j++;
}
if (val == 0){return 1;}
else {
Post *new_post = malloc(sizeof(Post));
time_t seconds = time(&seconds);
new_post->date = &seconds;
new_post->contents = contents;
strcpy(new_post->author, author->name);
printf("%s", (target->first_post)->contents);
new_post->next = target->first_post;
target->first_post = &(*new_post);
printf("%s", (target->first_post)->contents);
return 0;
}
return -1;
}
/*
* From the list pointed to by *user_ptr_del, delete the user
* with the given name.
* Remove the deleted user from any lists of friends.
*
* Return:
* - 0 on success.
* - 1 if a user with this name does not exist.
*/
int delete_user(const char *name, User **user_ptr_del) {
return -1;
}
| no segmentation fault in make_post | null |
63c6488c5f004bf4898cfd7e | 2023-02-12T13:30:56.964Z | Help Fix Code | v1 | null | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
else if (*user_ptr_add == NULL){
*user_ptr_add = malloc(sizeof(User));
strncpy((*user_ptr_add)->name, name, (MAX_NAME - 1));
(*user_ptr_add)->name[(MAX_NAME - 1)] = '\0';
(*user_ptr_add)->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
return 0;
}
else{
User *p = malloc(sizeof(User));
strncpy(p->name, name, (MAX_NAME - 1));
p->name[(MAX_NAME - 1)] = '\0';
p->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
User *curr = *user_ptr_add;
while (curr->next != NULL){
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
return -1;
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
while (curr != NULL){
if (strcmp(curr->name, name) == 0){
return &(*curr);
}
curr = curr->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n");
if (curr != NULL){
User *cur = (User *) curr;
while (cur != NULL){
printf("\t%s\n", cur->name);
cur = cur->next;
}
}
}
/*
* Change the filename for the profile pic of the given user.
*
* Return:
* - 0 on success.
* - 1 if the file does not exist or cannot be opened.
* - 2 if the filename is too long.
*/
int update_pic(User *user, const char *filename) {
FILE *error = fopen(filename, "r");
if (error == NULL){return 1;}
else if (strlen(filename) > (MAX_NAME - 1)){fclose(error); return 2;}
else{
strncpy(user->profile_pic, filename, (MAX_NAME - 1));
user->profile_pic[(MAX_NAME - 1)] = '\0';
fclose(error);
return 0;
}
fclose(error);
return -1;
}
/*
* 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) {
if (strcmp(name1, name2) == 0){return 3;}
else if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){return 4;}
User *first = find_user(name1, head);
User *second = find_user(name2, head);
int i=0, j=0;
int val = 0;
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val = 1;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val = 1;}
j++;
}
if (val == 1){return 1;}
else if ((i == (MAX_FRIENDS - 1)) || (j == (MAX_FRIENDS - 1))){return 2;}
else {
(first->friends)[i] = second;
(second->friends)[j] = first;
return 0;
}
return -1;
}
/*
* 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) {
char *dashes = {"------------------------------------------"};
if (user == NULL){return -1;}
else{
FILE *profile = fopen(user->profile_pic, "r");
if (profile == NULL){
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends[i])->name); i++;}
printf("%s\nPosts:\n", dashes);
if (profile != NULL){fclose(profile);}
return 0;
}
else {
char line[1000];
while (fgets(line, 1000, profile) != NULL){
printf("%s", line);
}
printf("\n");
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends[i])->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){printf("%s", dashes);}
else{
printf("\n===\n\n");
}
}
if (profile != NULL){fclose(profile);}
return 0;
}
}
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){return 2;}
int i=0, j=0;
int val = 0;
while ((author->friends)[i] != NULL){
if (strcmp(((author->friends)[i])->name, target->name) == 0){val = 1;}
i++;
}
while ((target->friends)[j] != NULL){
if (strcmp(((target->friends)[j])->name, author->name) == 0){val = 1;}
j++;
}
if (val == 0){return 1;}
else {
Post *new_post = malloc(sizeof(Post));
time_t seconds = time(&seconds);
new_post->date = &seconds;
new_post->contents = contents;
strcpy(new_post->author, author->name);
printf("%s", (target->first_post)->contents);
new_post->next = target->first_post;
target->first_post = new_post;
printf("%s", (target->first_post)->contents);
return 0;
}
return -1;
}
/*
* From the list pointed to by *user_ptr_del, delete the user
* with the given name.
* Remove the deleted user from any lists of friends.
*
* Return:
* - 0 on success.
* - 1 if a user with this name does not exist.
*/
int delete_user(const char *name, User **user_ptr_del) {
return -1;
}
| no segmentation fault in make_post | null |
63c6488c5f004bf4898cfd7e | 2023-02-12T14:19:58.905Z | General Question | v1 | how do I make a time_t object on the heap | null | null | null |
63c6488c5f004bf4898cfd7e | 2023-02-12T14:26:18.152Z | Help Fix Code | v1 | null | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
else if (*user_ptr_add == NULL){
*user_ptr_add = malloc(sizeof(User));
strncpy((*user_ptr_add)->name, name, (MAX_NAME - 1));
(*user_ptr_add)->name[(MAX_NAME - 1)] = '\0';
(*user_ptr_add)->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
return 0;
}
else{
User *p = malloc(sizeof(User));
strncpy(p->name, name, (MAX_NAME - 1));
p->name[(MAX_NAME - 1)] = '\0';
p->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
User *curr = *user_ptr_add;
while (curr->next != NULL){
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
return -1;
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
while (curr != NULL){
if (strcmp(curr->name, name) == 0){
return &(*curr);
}
curr = curr->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n");
if (curr != NULL){
User *cur = (User *) curr;
while (cur != NULL){
printf("\t%s\n", cur->name);
cur = cur->next;
}
}
}
/*
* Change the filename for the profile pic of the given user.
*
* Return:
* - 0 on success.
* - 1 if the file does not exist or cannot be opened.
* - 2 if the filename is too long.
*/
int update_pic(User *user, const char *filename) {
FILE *error = fopen(filename, "r");
if (error == NULL){return 1;}
else if (strlen(filename) > (MAX_NAME - 1)){fclose(error); return 2;}
else{
strncpy(user->profile_pic, filename, (MAX_NAME - 1));
user->profile_pic[(MAX_NAME - 1)] = '\0';
fclose(error);
return 0;
}
fclose(error);
return -1;
}
/*
* 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) {
if (strcmp(name1, name2) == 0){return 3;}
else if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){return 4;}
User *first = find_user(name1, head);
User *second = find_user(name2, head);
int i=0, j=0;
int val = 0;
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val = 1;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val = 1;}
j++;
}
if (val == 1){return 1;}
else if ((i == (MAX_FRIENDS - 1)) || (j == (MAX_FRIENDS - 1))){return 2;}
else {
(first->friends)[i] = second;
(second->friends)[j] = first;
return 0;
}
return -1;
}
/*
* 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) {
char *dashes = {"------------------------------------------"};
if (user == NULL){return -1;}
else{
FILE *profile = fopen(user->profile_pic, "r");
if (profile == NULL){
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends[i])->name); i++;}
printf("%s\nPosts:\n", dashes);
if (profile != NULL){fclose(profile);}
return 0;
}
else {
char line[1000];
while (fgets(line, 1000, profile) != NULL){
printf("%s", line);
}
printf("\n");
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends[i])->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){printf("%s", dashes);}
else{
printf("\n===\n\n");
}
}
if (profile != NULL){fclose(profile);}
return 0;
}
}
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){return 2;}
int i=0, j=0;
int val = 0;
while ((author->friends)[i] != NULL){
if (strcmp(((author->friends)[i])->name, target->name) == 0){val = 1;}
i++;
}
while ((target->friends)[j] != NULL){
if (strcmp(((target->friends)[j])->name, author->name) == 0){val = 1;}
j++;
}
if (val == 0){return 1;}
else {
Post *new_post = malloc(sizeof(Post));
time_t *seconds = malloc(sizeof(time_t));
*seconds = time(NULL);
new_post->date = seconds;
new_post->contents = contents;
strcpy(new_post->author, author->name);
new_post->next = target->first_post;
target->first_post = &(*new_post);
return 0;
}
return -1;
}
/*
* From the list pointed to by *user_ptr_del, delete the user
* with the given name.
* Remove the deleted user from any lists of friends.
*
* Return:
* - 0 on success.
* - 1 if a user with this name does not exist.
*/
int delete_user(const char *name, User **user_ptr_del) {
return -1;
}
| print_user doesn't print post information correctly | null |
63c6488c5f004bf4898cfd7e | 2023-02-13T00:23:01.951Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
if (find_user(name, *user_ptr_del) == NULL){return 1;}
else {
//find user to get all the friends of name
//for each one, find name in friends list and remove
//shift all values after it down one index space
//deallocate memory for user name
int f = 0;
User *user = find_user(name, *user_ptr_del);
while ((user->friends)[f] != NULL){
User *friend = find_user((user->friends)[f]->name, *user_ptr_del);
int i = 0;
while ((friend->friends)[i] != NULL){
if (strcmp(((friend->friends)[i])->name, name) == 0){
while ((friend->friends)[i] != NULL){
if ((friend->friends)[i+1] == NULL){
(friend->friends)[i] = NULL;
}
else {
(friend->friends)[i] = (friend->friends)[i+1];
}
}
break;
}
i++;
}
f++;
}
//store user->next
//store prev
//assign prev-> to user->next
//free(User)
User *curr = *user_ptr_del;
while (curr != NULL){
if (curr->next != NULL && strcmp(curr->next->name, name) == 0){
User *prev = curr;
User *nex = curr->next->next;
prev->next = nex;
curr->next->next = NULL;
break;
}
curr = curr->next;
}
free(user);
return 0;
}
return -1;
} | segmentation fault | null |
63c6488c5f004bf4898cfd7e | 2023-02-13T00:28:21.700Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
if (find_user(name, *user_ptr_del) == NULL){return 1;}
else {
//find user to get all the friends of name
//for each one, find name in friends list and remove
//shift all values after it down one index space
//deallocate memory for user name
int f = 0;
User *user = find_user(name, *user_ptr_del);
while ((user->friends)[f] != NULL){
User *friend = find_user((user->friends)[f]->name, *user_ptr_del);
int i = 0;
while ((friend->friends)[i] != NULL){
if (strcmp(((friend->friends)[i])->name, name) == 0){
while ((friend->friends)[i] != NULL){
if ((friend->friends)[i+1] == NULL){
(friend->friends)[i] = NULL;
}
else {
(friend->friends)[i] = (friend->friends)[i+1];
}
}
break;
}
i++;
}
f++;
}
//store user->next
//store prev
//assign prev-> to user->next
//free(User)
User *curr = *user_ptr_del;
while (curr != NULL){
if (curr->next != NULL && strcmp(curr->next->name, name) == 0){
User *prev = curr;
if (curr->next->next != NULL){
User *nex = curr->next->next;
}
else {
User *nex = NULL;
}
prev->next = nex;
curr->next->next = NULL;
break;
}
curr = curr->next;
}
free(user);
return 0;
}
return -1;
} | nex is unused | null |
63c6488c5f004bf4898cfd7e | 2023-02-13T00:35:22.022Z | Help Fix Code | v1 | null | }
| remove segmentation fault | null |
63c6488c5f004bf4898cfd7e | 2023-02-13T00:35:49.287Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
if (find_user(name, *user_ptr_del) == NULL){return 1;}
else {
//find user to get all the friends of name
//for each one, find name in friends list and remove
//shift all values after it down one index space
//deallocate memory for user name
int f = 0;
User *user = find_user(name, *user_ptr_del);
while ((user->friends)[f] != NULL){
User *friend = find_user((user->friends)[f]->name, *user_ptr_del);
int i = 0;
while ((friend->friends)[i] != NULL){
if (strcmp(((friend->friends)[i])->name, name) == 0){
while ((friend->friends)[i] != NULL){
if ((friend->friends)[i+1] == NULL){
(friend->friends)[i] = NULL;
}
else {
(friend->friends)[i] = (friend->friends)[i+1];
}
}
break;
}
i++;
}
f++;
}
//store user->next
//store prev
//assign prev-> to user->next
//free(User)
User *curr = *user_ptr_del;
while (curr != NULL){
if (curr->next != NULL && strcmp(curr->next->name, name) == 0){
User *prev = curr;
User *nex;
if (curr->next->next != NULL){
nex = curr->next->next;
}
else {
nex = NULL;
}
prev->next = nex;
curr->next->next = NULL;
break;
}
curr = curr->next;
}
free(user);
return 0;
}
return -1;
} | remove segmentation fault | null |
63c6488c5f004bf4898cfd7e | 2023-02-13T00:37:39.145Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
if (find_user(name, *user_ptr_del) == NULL){return 1;}
else {
//find user to get all the friends of name
//for each one, find name in friends list and remove
//shift all values after it down one index space
//deallocate memory for user name
int f = 0;
User *user = find_user(name, *user_ptr_del);
while ((user->friends)[f] != NULL){
User *friend = find_user((user->friends)[f]->name, *user_ptr_del);
int i = 0;
while ((friend->friends)[i] != NULL){
if (strcmp(((friend->friends)[i])->name, name) == 0){
while ((friend->friends)[i] != NULL){
if ((friend->friends)[i+1] == NULL){
(friend->friends)[i] = NULL;
}
else {
(friend->friends)[i] = (friend->friends)[i+1];
}
}
break;
}
i++;
}
f++;
}
//store user->next
//store prev
//assign prev-> to user->next
//free(User)
User *curr = **user_ptr_del;
while (curr != NULL){
if (curr->next != NULL && strcmp(curr->next->name, name) == 0){
User *prev = curr;
User *nex;
if (curr->next->next != NULL){
nex = curr->next->next;
}
else {
nex = NULL;
}
prev->next = nex;
curr->next->next = NULL;
break;
}
curr = curr->next;
}
free(user);
return 0;
}
return -1;
} | remove segmentation fault | null |
63c6488c5f004bf4898cfd7e | 2023-02-13T00:40:12.851Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
if (find_user(name, *user_ptr_del) == NULL){return 1;}
else {
//find user to get all the friends of name
//for each one, find name in friends list and remove
//shift all values after it down one index space
//deallocate memory for user name
int f = 0;
User *user = find_user(name, *user_ptr_del);
while ((user->friends)[f] != NULL){
User *friend = find_user((user->friends)[f]->name, *user_ptr_del);
int i = 0;
while ((friend->friends)[i] != NULL){
if (strcmp(((friend->friends)[i])->name, name) == 0){
while ((friend->friends)[i] != NULL){
if ((friend->friends)[i+1] == NULL){
(friend->friends)[i] = NULL;
}
else {
(friend->friends)[i] = (friend->friends)[i+1];
}
}
break;
}
i++;
}
f++;
}
//store user->next
//store prev
//assign prev-> to user->next
//free(User)
User *curr = *user_ptr_del;
while (curr != NULL){
if (curr->next != NULL && strcmp(curr->next->name, name) == 0){
User *prev = curr;
User *nex;
if (curr->next->next != NULL){
nex = curr->next->next;
}
else {
nex = NULL;
}
prev->next = nex;
curr->next->next = NULL;
break;
}
curr = curr->next;
}
free(user);
return 0;
}
return -1;
} | segmentation fault
| null |
63c6488c5f004bf4898cfd7e | 2023-02-13T01:11:46.992Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
if (find_user(name, *user_ptr_del) == NULL){return 1;}
else {
int f = 0;
User *user = find_user(name, *user_ptr_del);
while ((user->friends)[f] != NULL){
(user->friends)[f] = NULL;
User *friend = find_user((user->friends)[f]->name, *user_ptr_del);
int i = 0;
while ((friend->friends)[i] != NULL){
if (strcmp(((friend->friends)[i])->name, name) == 0){
while ((friend->friends)[i] != NULL){
if ((friend->friends)[i+1] == NULL){
(friend->friends)[i] = NULL;
}
else {
(friend->friends)[i] = (friend->friends)[i+1];
}
i++;
}
break;
}
i++;
}
f++;
}
User *curr = *user_ptr_del;
User *head = *user_ptr_del;
User *head_next = (*user_ptr_del)->next;
while (curr != NULL){
if (curr->next != NULL && strcmp(curr->next->name, name) == 0){
User *prev = curr;
User *nex;
if (curr->next->next != NULL){
nex = curr->next->next;
}
else {
nex = NULL;
}
prev->next = nex;
curr->next->next = NULL;
break;
}
curr = curr->next;
}
if (strcmp(head->name, name) == 0){
*user_ptr_del = head_next;
}
else {
*user_ptr_del = NULL;
}
free(user);
return 0;
}
return -1;
}
| remove segmentation fault | null |
63c6488c5f004bf4898cfd7e | 2023-02-13T02:33:48.055Z | Help Fix Code | v1 | null | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
else if (*user_ptr_add == NULL){
*user_ptr_add = malloc(sizeof(User));
if (*user_ptr_add == NULL){return 3;} //non-zero return val for malloc fail
strncpy((*user_ptr_add)->name, name, (MAX_NAME - 1));
(*user_ptr_add)->name[(MAX_NAME - 1)] = '\0';
(*user_ptr_add)->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
return 0;
}
else{
User *p = malloc(sizeof(User));
if (p == NULL){return 3;} //non-zero return val for malloc fail
strncpy(p->name, name, (MAX_NAME - 1));
p->name[(MAX_NAME - 1)] = '\0';
p->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
User *curr = *user_ptr_add;
while (curr->next != NULL){
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
return -1;
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
while (curr != NULL){
if (strcmp(curr->name, name) == 0){
return &(*curr);
}
curr = curr->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n");
if (curr != NULL){
User *cur = (User *) curr;
while (cur != NULL){
printf("\t%s\n", cur->name);
cur = cur->next;
}
}
}
/*
* Change the filename for the profile pic of the given user.
*
* Return:
* - 0 on success.
* - 1 if the file does not exist or cannot be opened.
* - 2 if the filename is too long.
*/
int update_pic(User *user, const char *filename) {
FILE *error = fopen(filename, "r");
if (error == NULL){return 1;}
else if (strlen(filename) > (MAX_NAME - 1)){fclose(error); return 2;}
else{
strncpy(user->profile_pic, filename, (MAX_NAME - 1));
user->profile_pic[(MAX_NAME - 1)] = '\0';
fclose(error);
return 0;
}
fclose(error);
return -1;
}
/*
* 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) {
if (strcmp(name1, name2) == 0){return 3;}
else if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){return 4;}
User *first = find_user(name1, head);
User *second = find_user(name2, head);
int i=0, j=0;
int val = 0;
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val++;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val++;}
j++;
}
if (val == 2){return 1;}
else if ((i == (MAX_FRIENDS - 1)) || (j == (MAX_FRIENDS - 1))){return 2;}
else {
(first->friends)[i] = second;
(second->friends)[j] = first;
return 0;
}
return -1;
}
/*
* 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) {
char *dashes = {"------------------------------------------"};
if (user == NULL){return -1;}
else{
FILE *profile = fopen(user->profile_pic, "r");
if (profile == NULL){
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends[i])->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){printf("%s\n", dashes);}
else{
printf("\n===\n\n");
}
}
return 0;
}
else {
char line[1000];
while (fgets(line, 1000, profile) != NULL){
printf("%s", line);
}
printf("\n");
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends[i])->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){printf("%s\n", dashes);}
else{
printf("\n===\n\n");
}
}
if (profile != NULL){fclose(profile);}
return 0;
}
}
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){return 2;}
int i=0, j=0;
int val = 0;
while ((author->friends)[i] != NULL){
if (strcmp(((author->friends)[i])->name, target->name) == 0){val = 1;}
i++;
}
while ((target->friends)[j] != NULL){
if (strcmp(((target->friends)[j])->name, author->name) == 0){val = 1;}
j++;
}
if (val == 0){return 1;}
else {
Post *new_post = malloc(sizeof(Post));
time_t *seconds = malloc(sizeof(time_t));
if (seconds == NULL || new_post == NULL){return 3;} //non-zero return val for malloc fail
*seconds = time(NULL);
new_post->date = seconds;
new_post->contents = contents;
strcpy(new_post->author, author->name);
new_post->next = target->first_post;
target->first_post = &(*new_post);
return 0;
}
return -1;
}
/*
* From the list pointed to by *user_ptr_del, delete the user
* with the given name.
* Remove the deleted user from any lists of friends.
*
* Return:
* - 0 on success.
* - 1 if a user with this name does not exist.
*/
int delete_user(const char *name, User **user_ptr_del) {
if (find_user(name, *user_ptr_del) == NULL){return 1;}
else {
int f = 0, k = 0;
User *user = find_user(name, *user_ptr_del);
while ((user->friends)[f] != NULL){
User *friend = find_user((user->friends)[f]->name, *user_ptr_del);
int i = 0;
while ((friend->friends)[i] != NULL){
if (strcmp(((friend->friends)[i])->name, name) == 0){
while ((friend->friends)[i] != NULL){
if ((friend->friends)[i+1] == NULL){
(friend->friends)[i] = NULL;
}
else {
(friend->friends)[i] = (friend->friends)[i+1];
}
i++;
}
break;
}
i++;
}
f++;
}
while ((user->friends)[k] != NULL){
(user->friends)[k] = NULL;
k++;
}
User *curr = *user_ptr_del;
User *head = *user_ptr_del;
User *head_next = (*user_ptr_del)->next;
while (curr != NULL){
if (curr->next != NULL && strcmp(curr->next->name, name) == 0){
User *prev = curr;
User *nex;
if (curr->next->next != NULL){
nex = curr->next->next;
}
else {
nex = NULL;
}
prev->next = nex;
curr->next->next = NULL;
break;
}
curr = curr->next;
}
if (strcmp(head->name, name) == 0){
*user_ptr_del = head_next;
}
else {
*user_ptr_del = NULL;
}
free(user);
return 0;
}
return -1;
}
| no segmentation fault when deleting a user from the end of the friends list | null |
63c6488c5f004bf4898cfd7e | 2023-02-13T03:23:12.941Z | Help Fix Code | v1 | null | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
else if (*user_ptr_add == NULL){
*user_ptr_add = malloc(sizeof(User));
if (*user_ptr_add == NULL){return 3;} //non-zero return val for malloc fail
strncpy((*user_ptr_add)->name, name, (MAX_NAME - 1));
(*user_ptr_add)->name[(MAX_NAME - 1)] = '\0';
(*user_ptr_add)->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
return 0;
}
else{
User *p = malloc(sizeof(User));
if (p == NULL){return 3;} //non-zero return val for malloc fail
strncpy(p->name, name, (MAX_NAME - 1));
p->name[(MAX_NAME - 1)] = '\0';
p->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
User *curr = *user_ptr_add;
while (curr->next != NULL){
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
return -1;
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
while (curr != NULL){
if (strcmp(curr->name, name) == 0){
return &(*curr);
}
curr = curr->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n");
if (curr != NULL){
User *cur = (User *) curr;
while (cur != NULL){
printf("\t%s\n", cur->name);
cur = cur->next;
}
}
}
/*
* Change the filename for the profile pic of the given user.
*
* Return:
* - 0 on success.
* - 1 if the file does not exist or cannot be opened.
* - 2 if the filename is too long.
*/
int update_pic(User *user, const char *filename) {
FILE *error = fopen(filename, "r");
if (error == NULL){return 1;}
else if (strlen(filename) > (MAX_NAME - 1)){fclose(error); return 2;}
else{
strncpy(user->profile_pic, filename, (MAX_NAME - 1));
user->profile_pic[(MAX_NAME - 1)] = '\0';
fclose(error);
return 0;
}
fclose(error);
return -1;
}
/*
* 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) {
if (strcmp(name1, name2) == 0){return 3;}
else if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){return 4;}
User *first = find_user(name1, head);
User *second = find_user(name2, head);
int i=0, j=0;
int val = 0;
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val++;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val++;}
j++;
}
if (val == 2){return 1;}
else if ((i == (MAX_FRIENDS - 1)) || (j == (MAX_FRIENDS - 1))){return 2;}
else {
(first->friends)[i] = second;
(second->friends)[j] = first;
return 0;
}
return -1;
}
/*
* 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) {
char *dashes = {"------------------------------------------"};
if (user == NULL){return -1;}
else{
FILE *profile = fopen(user->profile_pic, "r");
if (profile == NULL){
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends[i])->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){printf("%s\n", dashes);}
else{
printf("\n===\n\n");
}
}
return 0;
}
else {
char line[1000];
while (fgets(line, 1000, profile) != NULL){
printf("%s", line);
}
printf("\n");
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends[i])->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){printf("%s\n", dashes);}
else{
printf("\n===\n\n");
}
}
if (profile != NULL){fclose(profile);}
return 0;
}
}
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){return 2;}
int i=0, j=0;
int val = 0;
while ((author->friends)[i] != NULL){
if (strcmp(((author->friends)[i])->name, target->name) == 0){val = 1;}
i++;
}
while ((target->friends)[j] != NULL){
if (strcmp(((target->friends)[j])->name, author->name) == 0){val = 1;}
j++;
}
if (val == 0){return 1;}
else {
Post *new_post = malloc(sizeof(Post));
time_t *seconds = malloc(sizeof(time_t));
if (seconds == NULL || new_post == NULL){return 3;} //non-zero return val for malloc fail
*seconds = time(NULL);
new_post->date = seconds;
new_post->contents = contents;
strcpy(new_post->author, author->name);
new_post->next = target->first_post;
target->first_post = &(*new_post);
return 0;
}
return -1;
}
/*
* From the list pointed to by *user_ptr_del, delete the user
* with the given name.
* Remove the deleted user from any lists of friends.
*
* Return:
* - 0 on success.
* - 1 if a user with this name does not exist.
*/
int delete_user(const char *name, User **user_ptr_del) {
if (find_user(name, *user_ptr_del) == NULL){return 1;}
else {
int f = 0, k = 0;
User *user = find_user(name, *user_ptr_del);
while ((user->friends)[f] != NULL){
User *friend = find_user((user->friends)[f]->name, *user_ptr_del);
int i = 0;
while ((friend->friends)[i] != NULL){
if (strcmp(((friend->friends)[i])->name, name) == 0){
while ((friend->friends)[i] != NULL){
if ((friend->friends)[i+1] == NULL){
(friend->friends)[i] = NULL;
}
else {
(friend->friends)[i] = (friend->friends)[i+1];
}
i++;
}
break;
}
i++;
}
f++;
}
while ((user->friends)[k] != NULL){
(user->friends)[k] = NULL;
k++;
}
User *curr = *user_ptr_del;
User *head = *user_ptr_del;
User *head_next = (*user_ptr_del)->next;
while (curr != NULL){
if (curr->next != NULL && strcmp(curr->next->name, name) == 0){
User *prev = curr;
User *nex;
if (curr->next->next != NULL){
nex = curr->next->next;
}
else {
nex = NULL;
}
prev->next = nex;
//curr->next->next = NULL;
break;
}
curr = curr->next;
}
if (strcmp(head->name, name) == 0){
*user_ptr_del = head_next;
}
else {
*user_ptr_del = NULL;
}
free(user);
return 0;
}
return -1;
}
| free all allocated memory | null |
63c6488c5f004bf4898cfd7e | 2023-02-13T04:02:34.003Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
if (find_user(name, *user_ptr_del) == NULL){return 1;}
else {
int f = 0, k = 0;
User *user = find_user(name, *user_ptr_del);
while ((user->friends)[f] != NULL){
User *friend = find_user((user->friends)[f]->name, *user_ptr_del);
int i = 0;
while ((friend->friends)[i] != NULL){
if (strcmp(((friend->friends)[i])->name, name) == 0){
while ((friend->friends)[i] != NULL){
if ((friend->friends)[i+1] == NULL){
(friend->friends)[i] = NULL;
}
else {
(friend->friends)[i] = (friend->friends)[i+1];
}
i++;
}
break;
}
i++;
}
f++;
}
while ((user->friends)[k] != NULL){
(user->friends)[k] = NULL;
k++;
}
User *curr = *user_ptr_del;
User *head = *user_ptr_del;
User *head_next = (*user_ptr_del)->next;
while (curr != NULL){
if (curr->next != NULL && strcmp(curr->next->name, name) == 0){
User *prev = curr;
User *nex;
if (curr->next->next != NULL){
nex = curr->next->next;
}
else {
nex = NULL;
}
prev->next = nex;
//curr->next->next = NULL;
break;
}
curr = curr->next;
}
if (strcmp(head->name, name) == 0){
*user_ptr_del = head_next;
}
else {
*user_ptr_del = curr;
}
free(user);
return 0;
}
return -1;
} | remove segmentation fault | null |
63c6488c5f004bf4898cfd7e | 2023-02-14T05:46:23.906Z | Question from Code | v1 | why is there a segmentation fault | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
else if (*user_ptr_add == NULL){
*user_ptr_add = malloc(sizeof(User));
if (*user_ptr_add == NULL){return 3;}
strncpy((*user_ptr_add)->name, name, (MAX_NAME - 1));
(*user_ptr_add)->name[(MAX_NAME - 1)] = '\0';
(*user_ptr_add)->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
return 0;
}
else{
User *p = malloc(sizeof(User));
if (p == NULL){return 3;}
strncpy(p->name, name, (MAX_NAME - 1));
p->name[(MAX_NAME - 1)] = '\0';
p->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
User *curr = *user_ptr_add;
while (curr->next != NULL){
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
}
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
while (curr != NULL){
if (strcmp(curr->name, name) == 0){
return &(*curr);
}
curr = curr->next;
}
return NULL;
}
int make_friends(const char *name1, const char *name2, User *head) {
if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){return 4;}
else if (strcmp(name1, name2) == 0){return 3;}
else{
User *first = find_user(name1, head);
User *second = find_user(name2, head);
int i=0, j=0;
int val = 0;
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val++;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val++;}
j++;
}
if (((i+1) > (MAX_FRIENDS)) || ((j+1) > (MAX_FRIENDS))){return 2;}
else if (val == 2){return 1;}
else {
printf("reached else\n");
(first->friends)[i] = second;
(second->friends)[j] = first;
return 0;
}
}
}
int main() {
int void_val = create_user(11);
void_val = create_user(1);
void_val = create_user(2);
void_val = create_user(3);
void_val = create_user(4);
void_val = create_user(5);
void_val = create_user(6);
void_val = create_user(7);
void_val = create_user(8);
void_val = create_user(9);
void_val = create_user(10);
void_val = create_user(12);
int return_val = make_friends(11, 1);
return_val = make_friends(11, 2);
return_val = make_friends(11, 3);
return_val = make_friends(11, 4);
return_val = make_friends(11, 5);
return_val = make_friends(11, 6);
return_val = make_friends(11, 7);
return_val = make_friends(11, 8);
return_val = make_friends(11, 9);
return_val = make_friends(11, 10);
return_val = make_friends(11, 12);
return 0;
} | null | null |
63c6488c5f004bf4898cfd7e | 2023-02-14T05:48:25.016Z | Question from Code | v1 | why is there a segmentation fault | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
else if (*user_ptr_add == NULL){
*user_ptr_add = malloc(sizeof(User));
if (*user_ptr_add == NULL){return 3;}
strncpy((*user_ptr_add)->name, name, (MAX_NAME - 1));
(*user_ptr_add)->name[(MAX_NAME - 1)] = '\0';
(*user_ptr_add)->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
return 0;
}
else{
User *p = malloc(sizeof(User));
if (p == NULL){return 3;}
strncpy(p->name, name, (MAX_NAME - 1));
p->name[(MAX_NAME - 1)] = '\0';
p->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
User *curr = *user_ptr_add;
while (curr->next != NULL){
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
}
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
while (curr != NULL){
if (strcmp(curr->name, name) == 0){
return &(*curr);
}
curr = curr->next;
}
return NULL;
}
int make_friends(const char *name1, const char *name2, User *head) {
if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){return 4;}
else if (strcmp(name1, name2) == 0){return 3;}
else{
User *first = find_user(name1, head);
User *second = find_user(name2, head);
int i=0, j=0;
int val = 0;
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val++;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val++;}
j++;
}
if (((i+1) > (MAX_FRIENDS)) || ((j+1) > (MAX_FRIENDS))){return 2;}
else if (val == 2){return 1;}
else {
printf("reached else\n");
(first->friends)[i] = second;
(second->friends)[j] = first;
return 0;
}
}
}
int main() {
int void_val = create_user("11");
void_val = create_user("1");
void_val = create_user("2");
void_val = create_user("3");
void_val = create_user("4");
void_val = create_user("5");
void_val = create_user("6");
void_val = create_user("7");
void_val = create_user("8");
void_val = create_user("9");
void_val = create_user("10");
void_val = create_user("12");
int return_val = make_friends("11", "1");
return_val = make_friends("11", "2");
return_val = make_friends("11", "3");
return_val = make_friends("11", "4");
return_val = make_friends("11", "5");
return_val = make_friends("11", "6");
return_val = make_friends("11", "7");
return_val = make_friends("11", "8");
return_val = make_friends("11", "9");
return_val = make_friends("11", "10");
return_val = make_friends("11", "12");
return 0;
} | null | null |
63c6488c5f004bf4898cfd7e | 2023-02-14T05:49:58.485Z | Question from Code | v1 | why is there a segmentation fault | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
else if (*user_ptr_add == NULL){
*user_ptr_add = malloc(sizeof(User));
if (*user_ptr_add == NULL){return 3;}
strncpy((*user_ptr_add)->name, name, (MAX_NAME - 1));
(*user_ptr_add)->name[(MAX_NAME - 1)] = '\0';
(*user_ptr_add)->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
return 0;
}
else{
User *p = malloc(sizeof(User));
if (p == NULL){return 3;}
strncpy(p->name, name, (MAX_NAME - 1));
p->name[(MAX_NAME - 1)] = '\0';
p->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
User *curr = *user_ptr_add;
while (curr->next != NULL){
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
}
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
while (curr != NULL){
if (strcmp(curr->name, name) == 0){
return &(*curr);
}
curr = curr->next;
}
return NULL;
}
int make_friends(const char *name1, const char *name2, User *head) {
if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){return 4;}
else if (strcmp(name1, name2) == 0){return 3;}
else{
User *first = find_user(name1, head);
User *second = find_user(name2, head);
int i=0, j=0;
int val = 0;
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val++;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val++;}
j++;
}
if (((i+1) > (MAX_FRIENDS)) || ((j+1) > (MAX_FRIENDS))){return 2;}
else if (val == 2){return 1;}
else {
printf("reached else\n");
(first->friends)[i] = second;
(second->friends)[j] = first;
return 0;
}
}
}
int main() {
User *list = NULL;
int void_val = create_user("11", &list);
void_val = create_user("1", &list);
void_val = create_user("2", &list);
void_val = create_user("3", &list);
void_val = create_user("4", &list);
void_val = create_user("5", &list);
void_val = create_user("6", &list);
void_val = create_user("7", &list);
void_val = create_user("8", &list);
void_val = create_user("9", &list);
void_val = create_user("10", &list);
void_val = create_user("12", &list);
int return_val = make_friends("11", "1");
return_val = make_friends("11", "2");
return_val = make_friends("11", "3");
return_val = make_friends("11", "4");
return_val = make_friends("11", "5");
return_val = make_friends("11", "6");
return_val = make_friends("11", "7");
return_val = make_friends("11", "8");
return_val = make_friends("11", "9");
return_val = make_friends("11", "10");
return_val = make_friends("11", "12");
return 0;
} | null | null |
63c6488c5f004bf4898cfd7e | 2023-02-14T05:51:14.793Z | Question from Code | v1 | why is there a segmentation fault | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
else if (*user_ptr_add == NULL){
*user_ptr_add = malloc(sizeof(User));
if (*user_ptr_add == NULL){return 3;}
strncpy((*user_ptr_add)->name, name, (MAX_NAME - 1));
(*user_ptr_add)->name[(MAX_NAME - 1)] = '\0';
(*user_ptr_add)->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
return 0;
}
else{
User *p = malloc(sizeof(User));
if (p == NULL){return 3;}
strncpy(p->name, name, (MAX_NAME - 1));
p->name[(MAX_NAME - 1)] = '\0';
p->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
User *curr = *user_ptr_add;
while (curr->next != NULL){
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
}
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
while (curr != NULL){
if (strcmp(curr->name, name) == 0){
return &(*curr);
}
curr = curr->next;
}
return NULL;
}
int make_friends(const char *name1, const char *name2, User *head) {
if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){return 4;}
else if (strcmp(name1, name2) == 0){return 3;}
else{
User *first = find_user(name1, head);
User *second = find_user(name2, head);
int i=0, j=0;
int val = 0;
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val++;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val++;}
j++;
}
if (((i+1) > (MAX_FRIENDS)) || ((j+1) > (MAX_FRIENDS))){return 2;}
else if (val == 2){return 1;}
else {
printf("reached else\n");
(first->friends)[i] = second;
(second->friends)[j] = first;
return 0;
}
}
}
int main() {
User *list = NULL;
int void_val = create_user("11", &list);
void_val = create_user("1", &list);
void_val = create_user("2", &list);
void_val = create_user("3", &list);
void_val = create_user("4", &list);
void_val = create_user("5", &list);
void_val = create_user("6", &list);
void_val = create_user("7", &list);
void_val = create_user("8", &list);
void_val = create_user("9", &list);
void_val = create_user("10", &list);
void_val = create_user("12", &list);
int return_val = make_friends("11", "1", list);
return_val = make_friends("11", "2", list);
return_val = make_friends("11", "3", list);
return_val = make_friends("11", "4", list);
return_val = make_friends("11", "5", list);
return_val = make_friends("11", "6", list);
return_val = make_friends("11", "7", list);
return_val = make_friends("11", "8", list);
return_val = make_friends("11", "9", list);
return_val = make_friends("11", "10", list);
return_val = make_friends("11", "12", list);
return 0;
} | null | null |
63c6488c5f004bf4898cfd7e | 2023-02-14T05:54:34.530Z | Help Fix Code | v1 | null | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
else if (*user_ptr_add == NULL){
*user_ptr_add = malloc(sizeof(User));
if (*user_ptr_add == NULL){return 3;}
strncpy((*user_ptr_add)->name, name, (MAX_NAME - 1));
(*user_ptr_add)->name[(MAX_NAME - 1)] = '\0';
(*user_ptr_add)->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
return 0;
}
else{
User *p = malloc(sizeof(User));
if (p == NULL){return 3;}
strncpy(p->name, name, (MAX_NAME - 1));
p->name[(MAX_NAME - 1)] = '\0';
p->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
User *curr = *user_ptr_add;
while (curr->next != NULL){
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
}
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
while (curr != NULL){
if (strcmp(curr->name, name) == 0){
return &(*curr);
}
curr = curr->next;
}
return NULL;
}
int make_friends(const char *name1, const char *name2, User *head) {
if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){return 4;}
else if (strcmp(name1, name2) == 0){return 3;}
else{
User *first = find_user(name1, head);
User *second = find_user(name2, head);
int i=0, j=0;
int val = 0;
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val++;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val++;}
j++;
}
if (((i+1) > (MAX_FRIENDS)) || ((j+1) > (MAX_FRIENDS))){return 2;}
else if (val == 2){return 1;}
else {
printf("reached else\n");
(first->friends)[i] = second;
(second->friends)[j] = first;
return 0;
}
}
}
int main() {
User *list = NULL;
int void_val = create_user("11", &list);
void_val = create_user("1", &list);
void_val = create_user("2", &list);
void_val = create_user("3", &list);
void_val = create_user("4", &list);
void_val = create_user("5", &list);
void_val = create_user("6", &list);
void_val = create_user("7", &list);
void_val = create_user("8", &list);
void_val = create_user("9", &list);
void_val = create_user("10", &list);
void_val = create_user("12", &list);
int return_val = make_friends("11", "1", list);
return_val = make_friends("11", "2", list);
return_val = make_friends("11", "3", list);
return_val = make_friends("11", "4", list);
return_val = make_friends("11", "5", list);
return_val = make_friends("11", "6", list);
return_val = make_friends("11", "7", list);
return_val = make_friends("11", "8", list);
return_val = make_friends("11", "9", list);
return_val = make_friends("11", "10", list);
return_val = make_friends("11", "12", list);
return 0;
} | remove segmentation fault | null |
63c6488c5f004bf4898cfd7e | 2023-02-14T07:01:03.580Z | Question from Code | v1 | running this code MAX_FRIENDS amount of times on the same user results in an additional addition and I don't know why | int make_friends(const char *name1, const char *name2, User *head) {
if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){return 4;}
else if (strcmp(name1, name2) == 0){return 3;}
else{
User *first = find_user(name1, head);
User *second = find_user(name2, head);
int i=0, j=0;
int val = 0;
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val++;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val++;}
j++;
}
if (((i+1) > (MAX_FRIENDS)) || ((j+1) > (MAX_FRIENDS))){return 2;}
else if (val == 2){return 1;}
else {
printf("reached else\n");
(first->friends)[i] = second;
(second->friends)[j] = first;
return 0;
}
}
} | null | null |
63c6488c5f004bf4898cfd7e | 2023-02-14T08:15:47.267Z | Question from Code | v1 | can you call add_user to create 11 users, then call make_friends to add 10 users to 1 user and tell my why I the user's friends list goes past 10? | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#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;
typedef struct post {
char author[MAX_NAME];
char *contents;
time_t *date;
struct post *next;
} Post;
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
else if (*user_ptr_add == NULL){
*user_ptr_add = malloc(sizeof(User));
if (*user_ptr_add == NULL){return 3;} //non-zero return val for malloc fail
strncpy((*user_ptr_add)->name, name, (MAX_NAME - 1));
(*user_ptr_add)->name[(MAX_NAME - 1)] = '\0';
(*user_ptr_add)->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
return 0;
}
else{
User *p = malloc(sizeof(User));
if (p == NULL){return 3;} //non-zero return val for malloc fail
strncpy(p->name, name, (MAX_NAME - 1));
p->name[(MAX_NAME - 1)] = '\0';
p->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
User *curr = *user_ptr_add;
while (curr->next != NULL){
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
while (curr != NULL){
if (strcmp(curr->name, name) == 0){
return &(*curr);
}
curr = curr->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n");
if (curr != NULL){
User *cur = (User *) curr;
while (cur != NULL){
printf("\t%s\n", cur->name);
cur = cur->next;
}
}
}
/*
* Change the filename for the profile pic of the given user.
*
* Return:
* - 0 on success.
* - 1 if the file does not exist or cannot be opened.
* - 2 if the filename is too long.
*/
int update_pic(User *user, const char *filename) {
FILE *error = fopen(filename, "r");
if (error == NULL){return 1;}
else if (strlen(filename) > (MAX_NAME - 1)){fclose(error); return 2;}
else{
strncpy(user->profile_pic, filename, (MAX_NAME - 1));
user->profile_pic[(MAX_NAME - 1)] = '\0';
fclose(error);
return 0;
}
fclose(error);
}
/*
* 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) {
if (find_user(name1, head) == NULL || find_user(name2, head) == NULL){return 4;}
else if (strcmp(name1, name2) == 0){return 3;}
User *first = find_user(name1, head);
User *second = find_user(name2, head);
int i=0, j=0;
int val = 0;
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val++;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val++;}
j++;
}
if (val == 2){return 1;}
else if (((i+1) > (MAX_FRIENDS)) || ((j+1) > (MAX_FRIENDS))){return 2;}
else {
(first->friends)[i] = second;
(second->friends)[j] = first;
return 0;
}
}
/*
* Print a user profile.
* For an example of the required output format, see the example output
* linked from the handout.
* Return:
* - 0 on success.
* - 1 if the user is NULL.
*/
int print_user(const User *user) {
char *dashes = {"------------------------------------------"};
if (user == NULL){return -1;}
else{
FILE *profile = fopen(user->profile_pic, "r");
if (profile == NULL){
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends)[i]->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){}
else{
printf("\n===\n\n");
}
}
printf("%s\n", dashes);
return 0;
}
else {
char line[1000];
while (fgets(line, 1000, profile) != NULL){
printf("%s", line);
}
printf("\n");
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends[i])->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){}
else{
printf("\n===\n\n");
}
}
if (profile != NULL){fclose(profile);}
printf("%s\n", dashes);
return 0;
}
}
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){return 2;}
int i=0, j=0;
int val = 0;
while ((author->friends)[i] != NULL){
if (strcmp(((author->friends)[i])->name, target->name) == 0){val = 1;}
i++;
}
while ((target->friends)[j] != NULL){
if (strcmp(((target->friends)[j])->name, author->name) == 0){val = 1;}
j++;
}
if (val == 0){return 1;}
else {
Post *new_post = malloc(sizeof(Post));
time_t *seconds = malloc(sizeof(time_t));
if (seconds == NULL || new_post == NULL){return 3;} //non-zero return val for malloc fail
*seconds = time(NULL);
new_post->date = seconds;
new_post->contents = contents;
strcpy(new_post->author, author->name);
new_post->next = target->first_post;
target->first_post = &(*new_post);
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) {
if (find_user(name, *user_ptr_del) == NULL){return 1;}
else {
int f = 0, k = 0;
User *user = find_user(name, *user_ptr_del);
while ((user->friends)[f] != NULL){
User *friend = find_user((user->friends)[f]->name, *user_ptr_del);
int i = 0;
while ((friend->friends)[i] != NULL){
if (strcmp(((friend->friends)[i])->name, name) == 0){
while ((friend->friends)[i] != NULL){
if ((friend->friends)[i+1] == NULL){
(friend->friends)[i] = NULL;
}
else {
(friend->friends)[i] = (friend->friends)[i+1];
}
i++;
}
break;
}
i++;
}
f++;
}
while ((user->friends)[k] != NULL){
(user->friends)[k] = NULL;
k++;
}
User *curr = *user_ptr_del;
User *head = *user_ptr_del;
User *head_next = (*user_ptr_del)->next;
while (curr != NULL){
if (curr->next != NULL && strcmp(curr->next->name, name) == 0){
//User *prev = curr;
User *nex;
if (curr->next->next != NULL){
nex = curr->next->next;
}
else {
nex = NULL;
}
curr->next = nex;
break;
}
curr = curr->next;
}
if (strcmp(head->name, name) == 0){
*user_ptr_del = head_next;
}
Post *posts = user->first_post;
while (posts != NULL){
free(posts->date);
free(posts->contents);
posts = posts->next;
}
free(user->first_post);
free(user);
return 0;
}
}
| null | null |
63c6488c5f004bf4898cfd7e | 2023-02-14T08:33:44.721Z | Question from Code | v1 | can you call add_user to create 11 users, then call make_friends to add 10 users to 1 user and tell my why I the user's friends list goes past 10? | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
else if (*user_ptr_add == NULL){
*user_ptr_add = malloc(sizeof(User));
if (*user_ptr_add == NULL){return 3;} //non-zero return val for malloc fail
strncpy((*user_ptr_add)->name, name, (MAX_NAME - 1));
(*user_ptr_add)->name[(MAX_NAME - 1)] = '\0';
(*user_ptr_add)->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
return 0;
}
else{
User *p = malloc(sizeof(User));
if (p == NULL){return 3;} //non-zero return val for malloc fail
strncpy(p->name, name, (MAX_NAME - 1));
p->name[(MAX_NAME - 1)] = '\0';
p->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
User *curr = *user_ptr_add;
while (curr->next != NULL){
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
while (curr != NULL){
if (strcmp(curr->name, name) == 0){
return &(*curr);
}
curr = curr->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n");
if (curr != NULL){
User *cur = (User *) curr;
while (cur != NULL){
printf("\t%s\n", cur->name);
cur = cur->next;
}
}
}
/*
* Change the filename for the profile pic of the given user.
*
* Return:
* - 0 on success.
* - 1 if the file does not exist or cannot be opened.
* - 2 if the filename is too long.
*/
int update_pic(User *user, const char *filename) {
FILE *error = fopen(filename, "r");
if (error == NULL){return 1;}
else if (strlen(filename) > (MAX_NAME - 1)){fclose(error); return 2;}
else{
strncpy(user->profile_pic, filename, (MAX_NAME - 1));
user->profile_pic[(MAX_NAME - 1)] = '\0';
fclose(error);
return 0;
}
fclose(error);
}
/*
* Returns the number of friends a user has
* Used as a helper for make_friends
*/
int num_friends_helper(User *user) {
int friend_count = 0;
while ((user->friends)[friend_count] != NULL){friend_count++;}
return friend_count;
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
User *first = find_user(name1, head);
User *second = find_user(name2, head);
if ((first == NULL) || (second == NULL)){return 4;}
else if (strcmp(name1, name2) == 0){return 3;}
//int i=0, j=0;
int val = 0;
int first_count = num_friends_helper(first);
int second_count = num_friends_helper(second);
if ((first_count == MAX_FRIENDS) || (second_count == MAX_FRIENDS)){return 2;}
/*
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val++;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val++;}
j++;
}
*/
if (val == 2){return 1;}
//else if (((i+1) > (MAX_FRIENDS)) || ((j+1) > (MAX_FRIENDS))){return 2;}
else {
(first->friends)[first_count] = second;
(second->friends)[second_count] = first;
return 0;
}
}
/*
* Print a user profile.
* For an example of the required output format, see the example output
* linked from the handout.
* Return:
* - 0 on success.
* - 1 if the user is NULL.
*/
int print_user(const User *user) {
char *dashes = {"------------------------------------------"};
if (user == NULL){return -1;}
else{
FILE *profile = fopen(user->profile_pic, "r");
if (profile == NULL){
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends)[i]->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){}
else{
printf("\n===\n\n");
}
}
printf("%s\n", dashes);
return 0;
}
else {
char line[1000];
while (fgets(line, 1000, profile) != NULL){
printf("%s", line);
}
printf("\n");
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends[i])->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){}
else{
printf("\n===\n\n");
}
}
if (profile != NULL){fclose(profile);}
printf("%s\n", dashes);
return 0;
}
}
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){return 2;}
int i=0, j=0;
int val = 0;
while ((author->friends)[i] != NULL){
if (strcmp(((author->friends)[i])->name, target->name) == 0){val = 1;}
i++;
}
while ((target->friends)[j] != NULL){
if (strcmp(((target->friends)[j])->name, author->name) == 0){val = 1;}
j++;
}
if (val == 0){return 1;}
else {
Post *new_post = malloc(sizeof(Post));
time_t *seconds = malloc(sizeof(time_t));
if (seconds == NULL || new_post == NULL){return 3;} //non-zero return val for malloc fail
*seconds = time(NULL);
new_post->date = seconds;
new_post->contents = contents;
strcpy(new_post->author, author->name);
new_post->next = target->first_post;
target->first_post = &(*new_post);
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) {
if (find_user(name, *user_ptr_del) == NULL){return 1;}
else {
int f = 0, k = 0;
User *user = find_user(name, *user_ptr_del);
while ((user->friends)[f] != NULL){
User *friend = find_user((user->friends)[f]->name, *user_ptr_del);
int i = 0;
while ((friend->friends)[i] != NULL){
if (strcmp(((friend->friends)[i])->name, name) == 0){
while ((friend->friends)[i] != NULL){
if ((friend->friends)[i+1] == NULL){
(friend->friends)[i] = NULL;
}
else {
(friend->friends)[i] = (friend->friends)[i+1];
}
i++;
}
break;
}
i++;
}
f++;
}
while ((user->friends)[k] != NULL){
(user->friends)[k] = NULL;
k++;
}
User *curr = *user_ptr_del;
User *head = *user_ptr_del;
User *head_next = (*user_ptr_del)->next;
while (curr != NULL){
if (curr->next != NULL && strcmp(curr->next->name, name) == 0){
//User *prev = curr;
User *nex;
if (curr->next->next != NULL){
nex = curr->next->next;
}
else {
nex = NULL;
}
curr->next = nex;
break;
}
curr = curr->next;
}
if (strcmp(head->name, name) == 0){
*user_ptr_del = head_next;
}
Post *posts = user->first_post;
while (posts != NULL){
free(posts->date);
free(posts->contents);
posts = posts->next;
}
free(user->first_post);
free(user);
return 0;
}
}
| null | null |
63c6488c5f004bf4898cfd7e | 2023-02-14T08:39:12.356Z | Question from Code | v1 | can you call add_user to create 11 users, then call make_friends to add 10 users to 1 user and tell my why two friends are added at the 10th call? | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
else if (*user_ptr_add == NULL){
*user_ptr_add = malloc(sizeof(User));
if (*user_ptr_add == NULL){return 3;} //non-zero return val for malloc fail
strncpy((*user_ptr_add)->name, name, (MAX_NAME - 1));
(*user_ptr_add)->name[(MAX_NAME - 1)] = '\0';
(*user_ptr_add)->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
return 0;
}
else{
User *p = malloc(sizeof(User));
if (p == NULL){return 3;} //non-zero return val for malloc fail
strncpy(p->name, name, (MAX_NAME - 1));
p->name[(MAX_NAME - 1)] = '\0';
p->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
User *curr = *user_ptr_add;
while (curr->next != NULL){
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
while (curr != NULL){
if (strcmp(curr->name, name) == 0){
return &(*curr);
}
curr = curr->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n");
if (curr != NULL){
User *cur = (User *) curr;
while (cur != NULL){
printf("\t%s\n", cur->name);
cur = cur->next;
}
}
}
/*
* Change the filename for the profile pic of the given user.
*
* Return:
* - 0 on success.
* - 1 if the file does not exist or cannot be opened.
* - 2 if the filename is too long.
*/
int update_pic(User *user, const char *filename) {
FILE *error = fopen(filename, "r");
if (error == NULL){return 1;}
else if (strlen(filename) > (MAX_NAME - 1)){fclose(error); return 2;}
else{
strncpy(user->profile_pic, filename, (MAX_NAME - 1));
user->profile_pic[(MAX_NAME - 1)] = '\0';
fclose(error);
return 0;
}
fclose(error);
}
/*
* Returns the number of friends a user has
* Used as a helper for make_friends
*/
int num_friends_helper(User *user) {
int friend_count = 0;
while ((user->friends)[friend_count] != NULL){friend_count++;}
return friend_count;
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
User *first = find_user(name1, head);
User *second = find_user(name2, head);
if ((first == NULL) || (second == NULL)){return 4;}
else if (strcmp(name1, name2) == 0){return 3;}
//int i=0, j=0;
int val = 0;
int first_count = num_friends_helper(first);
int second_count = num_friends_helper(second);
if ((first_count == MAX_FRIENDS) || (second_count == MAX_FRIENDS)){return 2;}
/*
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val++;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val++;}
j++;
}
*/
if (val == 2){return 1;}
//else if (((i+1) > (MAX_FRIENDS)) || ((j+1) > (MAX_FRIENDS))){return 2;}
else {
(first->friends)[first_count] = second;
(second->friends)[second_count] = first;
return 0;
}
}
/*
* Print a user profile.
* For an example of the required output format, see the example output
* linked from the handout.
* Return:
* - 0 on success.
* - 1 if the user is NULL.
*/
int print_user(const User *user) {
char *dashes = {"------------------------------------------"};
if (user == NULL){return -1;}
else{
FILE *profile = fopen(user->profile_pic, "r");
if (profile == NULL){
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends)[i]->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){}
else{
printf("\n===\n\n");
}
}
printf("%s\n", dashes);
return 0;
}
else {
char line[1000];
while (fgets(line, 1000, profile) != NULL){
printf("%s", line);
}
printf("\n");
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends[i])->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){}
else{
printf("\n===\n\n");
}
}
if (profile != NULL){fclose(profile);}
printf("%s\n", dashes);
return 0;
}
}
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){return 2;}
int i=0, j=0;
int val = 0;
while ((author->friends)[i] != NULL){
if (strcmp(((author->friends)[i])->name, target->name) == 0){val = 1;}
i++;
}
while ((target->friends)[j] != NULL){
if (strcmp(((target->friends)[j])->name, author->name) == 0){val = 1;}
j++;
}
if (val == 0){return 1;}
else {
Post *new_post = malloc(sizeof(Post));
time_t *seconds = malloc(sizeof(time_t));
if (seconds == NULL || new_post == NULL){return 3;} //non-zero return val for malloc fail
*seconds = time(NULL);
new_post->date = seconds;
new_post->contents = contents;
strcpy(new_post->author, author->name);
new_post->next = target->first_post;
target->first_post = &(*new_post);
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) {
if (find_user(name, *user_ptr_del) == NULL){return 1;}
else {
int f = 0, k = 0;
User *user = find_user(name, *user_ptr_del);
while ((user->friends)[f] != NULL){
User *friend = find_user((user->friends)[f]->name, *user_ptr_del);
int i = 0;
while ((friend->friends)[i] != NULL){
if (strcmp(((friend->friends)[i])->name, name) == 0){
while ((friend->friends)[i] != NULL){
if ((friend->friends)[i+1] == NULL){
(friend->friends)[i] = NULL;
}
else {
(friend->friends)[i] = (friend->friends)[i+1];
}
i++;
}
break;
}
i++;
}
f++;
}
while ((user->friends)[k] != NULL){
(user->friends)[k] = NULL;
k++;
}
User *curr = *user_ptr_del;
User *head = *user_ptr_del;
User *head_next = (*user_ptr_del)->next;
while (curr != NULL){
if (curr->next != NULL && strcmp(curr->next->name, name) == 0){
//User *prev = curr;
User *nex;
if (curr->next->next != NULL){
nex = curr->next->next;
}
else {
nex = NULL;
}
curr->next = nex;
break;
}
curr = curr->next;
}
if (strcmp(head->name, name) == 0){
*user_ptr_del = head_next;
}
Post *posts = user->first_post;
while (posts != NULL){
free(posts->date);
free(posts->contents);
posts = posts->next;
}
free(user->first_post);
free(user);
return 0;
}
}
| null | null |
63c6488c5f004bf4898cfd7e | 2023-02-14T08:40:28.878Z | Question from Code | v1 | why are there 11 friends if you only call make_friends 10 times? | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
else if (*user_ptr_add == NULL){
*user_ptr_add = malloc(sizeof(User));
if (*user_ptr_add == NULL){return 3;} //non-zero return val for malloc fail
strncpy((*user_ptr_add)->name, name, (MAX_NAME - 1));
(*user_ptr_add)->name[(MAX_NAME - 1)] = '\0';
(*user_ptr_add)->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
return 0;
}
else{
User *p = malloc(sizeof(User));
if (p == NULL){return 3;} //non-zero return val for malloc fail
strncpy(p->name, name, (MAX_NAME - 1));
p->name[(MAX_NAME - 1)] = '\0';
p->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
User *curr = *user_ptr_add;
while (curr->next != NULL){
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
while (curr != NULL){
if (strcmp(curr->name, name) == 0){
return &(*curr);
}
curr = curr->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n");
if (curr != NULL){
User *cur = (User *) curr;
while (cur != NULL){
printf("\t%s\n", cur->name);
cur = cur->next;
}
}
}
/*
* Change the filename for the profile pic of the given user.
*
* Return:
* - 0 on success.
* - 1 if the file does not exist or cannot be opened.
* - 2 if the filename is too long.
*/
int update_pic(User *user, const char *filename) {
FILE *error = fopen(filename, "r");
if (error == NULL){return 1;}
else if (strlen(filename) > (MAX_NAME - 1)){fclose(error); return 2;}
else{
strncpy(user->profile_pic, filename, (MAX_NAME - 1));
user->profile_pic[(MAX_NAME - 1)] = '\0';
fclose(error);
return 0;
}
fclose(error);
}
/*
* Returns the number of friends a user has
* Used as a helper for make_friends
*/
int num_friends_helper(User *user) {
int friend_count = 0;
while ((user->friends)[friend_count] != NULL){friend_count++;}
return friend_count;
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
User *first = find_user(name1, head);
User *second = find_user(name2, head);
if ((first == NULL) || (second == NULL)){return 4;}
else if (strcmp(name1, name2) == 0){return 3;}
//int i=0, j=0;
int val = 0;
int first_count = num_friends_helper(first);
int second_count = num_friends_helper(second);
if ((first_count == MAX_FRIENDS) || (second_count == MAX_FRIENDS)){return 2;}
/*
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val++;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val++;}
j++;
}
*/
if (val == 2){return 1;}
//else if (((i+1) > (MAX_FRIENDS)) || ((j+1) > (MAX_FRIENDS))){return 2;}
else {
(first->friends)[first_count] = second;
(second->friends)[second_count] = first;
return 0;
}
}
/*
* Print a user profile.
* For an example of the required output format, see the example output
* linked from the handout.
* Return:
* - 0 on success.
* - 1 if the user is NULL.
*/
int print_user(const User *user) {
char *dashes = {"------------------------------------------"};
if (user == NULL){return -1;}
else{
FILE *profile = fopen(user->profile_pic, "r");
if (profile == NULL){
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends)[i]->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){}
else{
printf("\n===\n\n");
}
}
printf("%s\n", dashes);
return 0;
}
else {
char line[1000];
while (fgets(line, 1000, profile) != NULL){
printf("%s", line);
}
printf("\n");
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends[i])->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){}
else{
printf("\n===\n\n");
}
}
if (profile != NULL){fclose(profile);}
printf("%s\n", dashes);
return 0;
}
}
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){return 2;}
int i=0, j=0;
int val = 0;
while ((author->friends)[i] != NULL){
if (strcmp(((author->friends)[i])->name, target->name) == 0){val = 1;}
i++;
}
while ((target->friends)[j] != NULL){
if (strcmp(((target->friends)[j])->name, author->name) == 0){val = 1;}
j++;
}
if (val == 0){return 1;}
else {
Post *new_post = malloc(sizeof(Post));
time_t *seconds = malloc(sizeof(time_t));
if (seconds == NULL || new_post == NULL){return 3;} //non-zero return val for malloc fail
*seconds = time(NULL);
new_post->date = seconds;
new_post->contents = contents;
strcpy(new_post->author, author->name);
new_post->next = target->first_post;
target->first_post = &(*new_post);
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) {
if (find_user(name, *user_ptr_del) == NULL){return 1;}
else {
int f = 0, k = 0;
User *user = find_user(name, *user_ptr_del);
while ((user->friends)[f] != NULL){
User *friend = find_user((user->friends)[f]->name, *user_ptr_del);
int i = 0;
while ((friend->friends)[i] != NULL){
if (strcmp(((friend->friends)[i])->name, name) == 0){
while ((friend->friends)[i] != NULL){
if ((friend->friends)[i+1] == NULL){
(friend->friends)[i] = NULL;
}
else {
(friend->friends)[i] = (friend->friends)[i+1];
}
i++;
}
break;
}
i++;
}
f++;
}
while ((user->friends)[k] != NULL){
(user->friends)[k] = NULL;
k++;
}
User *curr = *user_ptr_del;
User *head = *user_ptr_del;
User *head_next = (*user_ptr_del)->next;
while (curr != NULL){
if (curr->next != NULL && strcmp(curr->next->name, name) == 0){
//User *prev = curr;
User *nex;
if (curr->next->next != NULL){
nex = curr->next->next;
}
else {
nex = NULL;
}
curr->next = nex;
break;
}
curr = curr->next;
}
if (strcmp(head->name, name) == 0){
*user_ptr_del = head_next;
}
Post *posts = user->first_post;
while (posts != NULL){
free(posts->date);
free(posts->contents);
posts = posts->next;
}
free(user->first_post);
free(user);
return 0;
}
}
| null | null |
63c6488c5f004bf4898cfd7e | 2023-02-14T08:43:35.316Z | Question from Code | v1 | when you add 12 users, and then call make_friends 10 times for one user, the 12 created user is added as a friend even though make_friends was not called on that user | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
else if (*user_ptr_add == NULL){
*user_ptr_add = malloc(sizeof(User));
if (*user_ptr_add == NULL){return 3;} //non-zero return val for malloc fail
strncpy((*user_ptr_add)->name, name, (MAX_NAME - 1));
(*user_ptr_add)->name[(MAX_NAME - 1)] = '\0';
(*user_ptr_add)->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
return 0;
}
else{
User *p = malloc(sizeof(User));
if (p == NULL){return 3;} //non-zero return val for malloc fail
strncpy(p->name, name, (MAX_NAME - 1));
p->name[(MAX_NAME - 1)] = '\0';
p->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
User *curr = *user_ptr_add;
while (curr->next != NULL){
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
while (curr != NULL){
if (strcmp(curr->name, name) == 0){
return &(*curr);
}
curr = curr->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n");
if (curr != NULL){
User *cur = (User *) curr;
while (cur != NULL){
printf("\t%s\n", cur->name);
cur = cur->next;
}
}
}
/*
* Change the filename for the profile pic of the given user.
*
* Return:
* - 0 on success.
* - 1 if the file does not exist or cannot be opened.
* - 2 if the filename is too long.
*/
int update_pic(User *user, const char *filename) {
FILE *error = fopen(filename, "r");
if (error == NULL){return 1;}
else if (strlen(filename) > (MAX_NAME - 1)){fclose(error); return 2;}
else{
strncpy(user->profile_pic, filename, (MAX_NAME - 1));
user->profile_pic[(MAX_NAME - 1)] = '\0';
fclose(error);
return 0;
}
fclose(error);
}
/*
* Returns the number of friends a user has
* Used as a helper for make_friends
*/
int num_friends_helper(User *user) {
int friend_count = 0;
while ((user->friends)[friend_count] != NULL){friend_count++;}
return friend_count;
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
User *first = find_user(name1, head);
User *second = find_user(name2, head);
if ((first == NULL) || (second == NULL)){return 4;}
else if (strcmp(name1, name2) == 0){return 3;}
//int i=0, j=0;
int val = 0;
int first_count = num_friends_helper(first);
int second_count = num_friends_helper(second);
if ((first_count == MAX_FRIENDS) || (second_count == MAX_FRIENDS)){return 2;}
/*
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val++;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val++;}
j++;
}
*/
if (val == 2){return 1;}
//else if (((i+1) > (MAX_FRIENDS)) || ((j+1) > (MAX_FRIENDS))){return 2;}
else {
(first->friends)[first_count] = second;
(second->friends)[second_count] = first;
return 0;
}
}
/*
* Print a user profile.
* For an example of the required output format, see the example output
* linked from the handout.
* Return:
* - 0 on success.
* - 1 if the user is NULL.
*/
int print_user(const User *user) {
char *dashes = {"------------------------------------------"};
if (user == NULL){return -1;}
else{
FILE *profile = fopen(user->profile_pic, "r");
if (profile == NULL){
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends)[i]->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){}
else{
printf("\n===\n\n");
}
}
printf("%s\n", dashes);
return 0;
}
else {
char line[1000];
while (fgets(line, 1000, profile) != NULL){
printf("%s", line);
}
printf("\n");
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends[i])->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){}
else{
printf("\n===\n\n");
}
}
if (profile != NULL){fclose(profile);}
printf("%s\n", dashes);
return 0;
}
}
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){return 2;}
int i=0, j=0;
int val = 0;
while ((author->friends)[i] != NULL){
if (strcmp(((author->friends)[i])->name, target->name) == 0){val = 1;}
i++;
}
while ((target->friends)[j] != NULL){
if (strcmp(((target->friends)[j])->name, author->name) == 0){val = 1;}
j++;
}
if (val == 0){return 1;}
else {
Post *new_post = malloc(sizeof(Post));
time_t *seconds = malloc(sizeof(time_t));
if (seconds == NULL || new_post == NULL){return 3;} //non-zero return val for malloc fail
*seconds = time(NULL);
new_post->date = seconds;
new_post->contents = contents;
strcpy(new_post->author, author->name);
new_post->next = target->first_post;
target->first_post = &(*new_post);
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) {
if (find_user(name, *user_ptr_del) == NULL){return 1;}
else {
int f = 0, k = 0;
User *user = find_user(name, *user_ptr_del);
while ((user->friends)[f] != NULL){
User *friend = find_user((user->friends)[f]->name, *user_ptr_del);
int i = 0;
while ((friend->friends)[i] != NULL){
if (strcmp(((friend->friends)[i])->name, name) == 0){
while ((friend->friends)[i] != NULL){
if ((friend->friends)[i+1] == NULL){
(friend->friends)[i] = NULL;
}
else {
(friend->friends)[i] = (friend->friends)[i+1];
}
i++;
}
break;
}
i++;
}
f++;
}
while ((user->friends)[k] != NULL){
(user->friends)[k] = NULL;
k++;
}
User *curr = *user_ptr_del;
User *head = *user_ptr_del;
User *head_next = (*user_ptr_del)->next;
while (curr != NULL){
if (curr->next != NULL && strcmp(curr->next->name, name) == 0){
//User *prev = curr;
User *nex;
if (curr->next->next != NULL){
nex = curr->next->next;
}
else {
nex = NULL;
}
curr->next = nex;
break;
}
curr = curr->next;
}
if (strcmp(head->name, name) == 0){
*user_ptr_del = head_next;
}
Post *posts = user->first_post;
while (posts != NULL){
free(posts->date);
free(posts->contents);
posts = posts->next;
}
free(user->first_post);
free(user);
return 0;
}
}
| null | null |
63c6488c5f004bf4898cfd7e | 2023-02-14T08:47:47.108Z | Help Fix Code | v1 | null | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
else if (*user_ptr_add == NULL){
*user_ptr_add = malloc(sizeof(User));
if (*user_ptr_add == NULL){return 3;} //non-zero return val for malloc fail
strncpy((*user_ptr_add)->name, name, (MAX_NAME - 1));
(*user_ptr_add)->name[(MAX_NAME - 1)] = '\0';
(*user_ptr_add)->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
return 0;
}
else{
User *p = malloc(sizeof(User));
if (p == NULL){return 3;} //non-zero return val for malloc fail
strncpy(p->name, name, (MAX_NAME - 1));
p->name[(MAX_NAME - 1)] = '\0';
p->next = NULL;
(*user_ptr_add)->profile_pic[0] = '\0';
User *curr = *user_ptr_add;
while (curr->next != NULL){
if (strcmp(curr->name, name) == 0){
return 1;
}
curr = curr->next;
}
curr->next = p;
return 0;
}
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
while (curr != NULL){
if (strcmp(curr->name, name) == 0){
return &(*curr);
}
curr = curr->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n");
if (curr != NULL){
User *cur = (User *) curr;
while (cur != NULL){
printf("\t%s\n", cur->name);
cur = cur->next;
}
}
}
/*
* Change the filename for the profile pic of the given user.
*
* Return:
* - 0 on success.
* - 1 if the file does not exist or cannot be opened.
* - 2 if the filename is too long.
*/
int update_pic(User *user, const char *filename) {
FILE *error = fopen(filename, "r");
if (error == NULL){return 1;}
else if (strlen(filename) > (MAX_NAME - 1)){fclose(error); return 2;}
else{
strncpy(user->profile_pic, filename, (MAX_NAME - 1));
user->profile_pic[(MAX_NAME - 1)] = '\0';
fclose(error);
return 0;
}
fclose(error);
}
/*
* Returns the number of friends a user has
* Used as a helper for make_friends
*/
int num_friends_helper(User *user) {
int friend_count = 0;
while ((user->friends)[friend_count] != NULL){friend_count++;}
return friend_count;
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
User *first = find_user(name1, head);
User *second = find_user(name2, head);
if ((first == NULL) || (second == NULL)){return 4;}
else if (strcmp(name1, name2) == 0){return 3;}
//int i=0, j=0;
int val = 0;
int first_count = num_friends_helper(first);
int second_count = num_friends_helper(second);
if ((first_count == MAX_FRIENDS) || (second_count == MAX_FRIENDS)){return 2;}
/*
while ((first->friends)[i] != NULL){
if (strcmp(((first->friends)[i])->name, name2) == 0){val++;}
i++;
}
while ((second->friends)[j] != NULL){
if (strcmp(((second->friends)[j])->name, name1) == 0){val++;}
j++;
}
*/
if (val == 2){return 1;}
//else if (((i+1) > (MAX_FRIENDS)) || ((j+1) > (MAX_FRIENDS))){return 2;}
else {
(first->friends)[first_count] = second;
(second->friends)[second_count] = first;
return 0;
}
}
/*
* Print a user profile.
* For an example of the required output format, see the example output
* linked from the handout.
* Return:
* - 0 on success.
* - 1 if the user is NULL.
*/
int print_user(const User *user) {
char *dashes = {"------------------------------------------"};
if (user == NULL){return -1;}
else{
FILE *profile = fopen(user->profile_pic, "r");
if (profile == NULL){
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends)[i]->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){}
else{
printf("\n===\n\n");
}
}
printf("%s\n", dashes);
return 0;
}
else {
char line[1000];
while (fgets(line, 1000, profile) != NULL){
printf("%s", line);
}
printf("\n");
printf("Name: %s\n%s\nFriends:\n", user->name, dashes);
int i = 0;
while ((user->friends)[i] != NULL){printf("%s\n", (user->friends[i])->name); i++;}
printf("%s\nPosts:\n", dashes);
Post *curr = user->first_post;
while (curr != NULL){
printf("From: %s\nDate: %s\n%s\n", curr->author, ctime(curr->date), curr->contents);
curr = curr->next;
if (curr == NULL){}
else{
printf("\n===\n\n");
}
}
if (profile != NULL){fclose(profile);}
printf("%s\n", dashes);
return 0;
}
}
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){return 2;}
int i=0, j=0;
int val = 0;
while ((author->friends)[i] != NULL){
if (strcmp(((author->friends)[i])->name, target->name) == 0){val = 1;}
i++;
}
while ((target->friends)[j] != NULL){
if (strcmp(((target->friends)[j])->name, author->name) == 0){val = 1;}
j++;
}
if (val == 0){return 1;}
else {
Post *new_post = malloc(sizeof(Post));
time_t *seconds = malloc(sizeof(time_t));
if (seconds == NULL || new_post == NULL){return 3;} //non-zero return val for malloc fail
*seconds = time(NULL);
new_post->date = seconds;
new_post->contents = contents;
strcpy(new_post->author, author->name);
new_post->next = target->first_post;
target->first_post = &(*new_post);
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) {
if (find_user(name, *user_ptr_del) == NULL){return 1;}
else {
int f = 0, k = 0;
User *user = find_user(name, *user_ptr_del);
while ((user->friends)[f] != NULL){
User *friend = find_user((user->friends)[f]->name, *user_ptr_del);
int i = 0;
while ((friend->friends)[i] != NULL){
if (strcmp(((friend->friends)[i])->name, name) == 0){
while ((friend->friends)[i] != NULL){
if ((friend->friends)[i+1] == NULL){
(friend->friends)[i] = NULL;
}
else {
(friend->friends)[i] = (friend->friends)[i+1];
}
i++;
}
break;
}
i++;
}
f++;
}
while ((user->friends)[k] != NULL){
(user->friends)[k] = NULL;
k++;
}
User *curr = *user_ptr_del;
User *head = *user_ptr_del;
User *head_next = (*user_ptr_del)->next;
while (curr != NULL){
if (curr->next != NULL && strcmp(curr->next->name, name) == 0){
//User *prev = curr;
User *nex;
if (curr->next->next != NULL){
nex = curr->next->next;
}
else {
nex = NULL;
}
curr->next = nex;
break;
}
curr = curr->next;
}
if (strcmp(head->name, name) == 0){
*user_ptr_del = head_next;
}
Post *posts = user->first_post;
while (posts != NULL){
free(posts->date);
free(posts->contents);
posts = posts->next;
}
free(user->first_post);
free(user);
return 0;
}
}
| making friends MAX_FRIEND number of times should not result in a segmentation fault | null |
63c6488d5f004bf4898cfd83 | 2023-01-24T03:54:03.066Z | Explain Code | v1 | null | void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// Function to perform Selection Sort
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element
// with the first element
swap(&arr[min_idx], &arr[i]);
}
}
// Function to print an array
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Driver code
int main()
{
int arr[] = { 0, 23, 14, 12, 9 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: \n");
printArray(arr, n);
selectionSort(arr, n);
printf("\nSorted array in Ascending order: \n");
printArray(arr, n);
return 0;
} | null | null |
63c6488d5f004bf4898cfd83 | 2023-01-25T01:08:15.004Z | Help Fix Code | v1 | null | #include <stdlib.h>
#include <stdio.h>
/*
* Define a function void fib(...) below. This function takes parameter n
* and generates the first n values in the Fibonacci sequence. Recall that this
* sequence is defined as:
* 0, 1, 1, 2, 3, 5, ... , fib[n] = fib[n-2] + fib[n-1], ...
* The values should be stored in a dynamically-allocated array composed of
* exactly the correct number of integers. The values should be returned
* through a pointer parameter passed in as the first argument.
*
* See the main function for an example call to fib.
* Pay attention to the expected type of fib's parameters.
*/
/* Write your solution here */
void fib(int **sequence, int n) {
*sequence = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
if (i > 1) {
*sequence[i] = *sequence[i-1] + *sequence[i-2];
}
else if (i == 1) {
*sequence[i] = 1;
}
else {
*sequence[i] = 0;
}
}
}
int main(int argc, char **argv) {
/* do not change this main function */
int count = strtol(argv[1], NULL, 10);
int *fib_sequence;
fib(&fib_sequence, count);
for (int i = 0; i < count; i++) {
printf("%d ", fib_sequence[i]);
}
free(fib_sequence);
return 0;
} | fib should make an array, sequence, of n fibonacci numbers | null |
63c6488d5f004bf4898cfd83 | 2023-01-25T01:11:35.678Z | Question from Code | v1 | why am I getting a segmentation fault? | #include <stdlib.h>
#include <stdio.h>
/*
* Define a function void fib(...) below. This function takes parameter n
* and generates the first n values in the Fibonacci sequence. Recall that this
* sequence is defined as:
* 0, 1, 1, 2, 3, 5, ... , fib[n] = fib[n-2] + fib[n-1], ...
* The values should be stored in a dynamically-allocated array composed of
* exactly the correct number of integers. The values should be returned
* through a pointer parameter passed in as the first argument.
*
* See the main function for an example call to fib.
* Pay attention to the expected type of fib's parameters.
*/
/* Write your solution here */
void fib(int **sequence, int n) {
*sequence = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
if (i > 1) {
*sequence[i] = *sequence[i-1] + *sequence[i-2];
}
else if (i == 1) {
*sequence[i] = 1;
}
else {
*sequence[i] = 0;
}
}
}
int main(int argc, char **argv) {
/* do not change this main function */
int count = strtol(argv[1], NULL, 10);
int *fib_sequence;
fib(&fib_sequence, count);
for (int i = 0; i < count; i++) {
printf("%d ", fib_sequence[i]);
}
free(fib_sequence);
return 0;
} | null | null |
63c6488d5f004bf4898cfd83 | 2023-01-25T03:11:12.801Z | Question from Code | v1 | why am I getting a segmentation fault? | #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) {
int middle = length / 2;
int remainder = length % 2; // if remainder is 1 1st subarray (even) needs 1 more element than odd
int **odd_even_arrays = malloc(2 * sizeof(int*));
if (remainder == 1) {
odd_even_arrays[0] = malloc((middle + 1) * sizeof(int));
}
else {
odd_even_arrays[0] = malloc(middle * sizeof(int));
}
odd_even_arrays[1] = malloc(middle * sizeof(int));
for (int i = 0; i < length; i++) {
odd_even_arrays[i % 2][i / 2] = s[i];
}
return odd_even_arrays;
}
/* 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) {
int *numbers = malloc(size * sizeof(int));
for (int i = 0; i < size; i++) {
numbers[i] = strtol(strs[i], NULL, 10);
}
return numbers;
}
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((char **)(char []) {'0', '1', '2', '3', '4'}, 5);
int **result = split_array(full_array, 5);
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 |
63c6488d5f004bf4898cfd83 | 2023-01-25T03:53:50.983Z | Question from Code | v1 | I am getting an error that says 'invalid read of size 4' | int **split_array(const int *s, int length) {
int middle = length / 2;
int remainder = length % 2; // if remainder is 1 1st subarray (even) needs 1 more element than odd
int **odd_even_arrays = malloc(2 * sizeof(int*));
if (remainder == 1) {
odd_even_arrays[0] = malloc((middle + 1) * sizeof(int));
}
else {
odd_even_arrays[0] = malloc(middle * sizeof(int));
}
odd_even_arrays[1] = malloc(middle * sizeof(int));
for (int i = 0; i < length; i++) {
odd_even_arrays[i % 2][i / 2] = s[i];
}
return odd_even_arrays;
} | null | null |
63c6488d5f004bf4898cfd83 | 2023-01-25T04:01:02.802Z | Question from Code | v1 | why am I getting an error that says "address is 0 bytes after block of 12 bytes is allocated" | int *build_array(char **strs, int size) {
int *numbers = malloc(size * sizeof(int));
for (int i = 0; i < size; i++) {
numbers[i] = strtol(strs[i], NULL, 10);
}
return numbers;
} | null | null |
63c6488e5f004bf4898cfd8d | 2023-01-17T15:08:17.784Z | General Question | v1 | how to compile C | null | null | null |
63c6488e5f004bf4898cfd8d | 2023-01-17T16:49:31.687Z | General Question | v1 | What is the point of life? | null | null | null |
63c6488e5f004bf4898cfd8d | 2023-01-25T22:10:16.183Z | Explain Code | v1 | null | char *scrap_1 = NULL;
int scrap_2 = 0;
scanf("%s %d\n", scrap_1, &scrap_2); | null | null |
63c6488e5f004bf4898cfd8d | 2023-01-25T22:12:18.649Z | Help Fix Code | v1 | null | char *scrap_1 = NULL;
int scrap_2 = 0;
scanf("%s %d\n", scrap_1, &scrap_2); | save user input string in scrap 1 character array | null |
63c6488e5f004bf4898cfd8d | 2023-02-12T22:56:06.230Z | General Question | v1 | how to iterate over a linked list with ** parameter | null | null | null |
63c6488e5f004bf4898cfd8d | 2023-02-12T22:58:44.912Z | General Question | v1 | how to add to the end of linked list | null | null | null |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.