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
⌀ |
---|---|---|---|---|---|---|---|
63c647ca5f004bf4898cf6bc | 2023-02-15T01:49:41.041Z | General Question | v1 | How do you make a struct pointer array of size 10? | null | null | null |
63c647ca5f004bf4898cf6bc | 2023-02-15T02:01:39.108Z | General Question | v1 | how to change an array to point to another array | null | null | null |
63c647ca5f004bf4898cf6bc | 2023-02-15T02:05:37.656Z | General Question | v1 | How to point an array of equal size with an array | null | null | null |
63c647ca5f004bf4898cf6bc | 2023-02-15T02:07:04.100Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
User *leaver = find_user(name, *user_ptr_del);
User *curr = *user_ptr_del;
if(leaver == NULL || curr == NULL){
return 1;
}
if(curr == leaver){
*user_ptr_del = curr->next;
}
for(int i = 0; i < 10; i++){
if(curr->next->friends[i]==leaver){
curr->next->friends[i] = NULL;
}
}
while(curr->next != NULL){
if(curr->next == leaver){
User *a = curr->next;
free(a);
curr->next = curr->next->next;
}
else{
User *friends_prime[MAX_FRIENDS]; //making new friends list to avoid segmentation faults
for(int i = 0; i < 10; i++){
friends_prime[i] = (User *) malloc(sizeof(User));
int j = 0;
if(curr->next->friends[i] != leaver){
friends_prime[j] = curr->next->friends[i];
j++;
}
if(i == 9 && j < i){
friends_prime[i] = NULL;
}
}
free(curr->friends);
curr->friends = *friends_prime;
}
curr = curr->next;
}
return 1;
} | replace curr->friends with the array friends_prime | null |
63c647ca5f004bf4898cf6bc | 2023-02-15T02:08:59.829Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
User *leaver = find_user(name, *user_ptr_del);
User *curr = *user_ptr_del;
if(leaver == NULL || curr == NULL){
return 1;
}
if(curr == leaver){
*user_ptr_del = curr->next;
}
for(int i = 0; i < 10; i++){
if(curr->next->friends[i]==leaver){
curr->next->friends[i] = NULL;
}
}
while(curr->next != NULL){
if(curr->next == leaver){
User *a = curr->next;
free(a);
curr->next = curr->next->next;
}
else{
User *friends_prime[MAX_FRIENDS]; //making new friends list to avoid segmentation faults
for(int i = 0; i < 10; i++){
friends_prime[i] = (User *) malloc(sizeof(User));
int j = 0;
if(curr->next->friends[i] != leaver){
friends_prime[j] = curr->next->friends[i];
j++;
}
if(i == 9 && j < i){
friends_prime[i] = NULL;
}
}
free(curr->friends);
curr->friends = friends_prime;
}
curr = curr->next;
}
return 1;
} | replace curr->friends with the array friends_prime | null |
63c647ca5f004bf4898cf6bc | 2023-02-15T02:59:19.937Z | Question from Code | v1 | how to change curr->friends to point to the array friends_prime | int delete_user(const char *name, User **user_ptr_del) {
User *leaver = find_user(name, *user_ptr_del);
User *curr = *user_ptr_del;
if(leaver == NULL || curr == NULL){
return 1;
}
if(curr == leaver){
*user_ptr_del = curr->next;
}
for(int i = 0; i < 10; i++){
if(curr->next->friends[i]==leaver){
curr->next->friends[i] = NULL;
}
}
while(curr->next != NULL){
if(curr->next == leaver){
User *a = curr->next;
free(a);
curr->next = curr->next->next;
}
else{
User *friends_prime[MAX_FRIENDS]; //making new friends list to avoid segmentation faults
for(int i = 0; i < 10; i++){
friends_prime[i] = (User *) malloc(sizeof(User));
int j = 0;
if(curr->next->friends[i] != leaver){
friends_prime[j] = curr->next->friends[i];
j++;
}
if(i == 9 && j < i){
friends_prime[i] = NULL;
}
}
free(curr->friends);
curr->friends = friends_prime;
}
curr = curr->next;
}
return 1;
} | null | null |
63c647ca5f004bf4898cf6bc | 2023-02-15T03:22:56.953Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
User *leaver = find_user(name, *user_ptr_del); //The user we want to delete
User *curr = *user_ptr_del; //for traversal
if(leaver == NULL || curr == NULL){
return 1;
}
if(curr == leaver){
*user_ptr_del = curr->next;
}
else if(curr != leaver){
int j = 0;
for(int i = 0; i<10; i++){
if(curr->next->friends[i] != leaver){
curr->next->friends[j] = curr->next->friends[i];
j++;
}
}
if(j == 8){
curr->friends[9] = NULL;
}
}
while(curr->next != NULL){
if(curr->next == leaver){
User *a = curr->next;
free(a);
curr->next = curr->next->next;
}
else{
int j = 0;
for(int i = 0; i < 10; i++){
if(curr->next->friends[i] != leaver){
curr->next->friends[j] = curr->next->friends[i];
j++;
}
}
if(j == 8){
curr->next->friends[9] = NULL;
}
}
curr = curr->next;
}
return 0;
}
| remove segmentation fault | null |
63c647ca5f004bf4898cf6bc | 2023-02-15T03:25:53.012Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
User *leaver = find_user(name, *user_ptr_del); //The user we want to delete
User *curr = *user_ptr_del; //for traversal
if(leaver == NULL || curr == NULL){
return 1;
}
if(curr == leaver){
*user_ptr_del = curr->next;
}
else if(curr != leaver){
int j = 0;
for(int i = 0; i<10; i++){
if(curr->next->friends[i] != leaver){
curr->next->friends[j] = curr->next->friends[i];
j++;
}
}
if(j == 8){
curr->friends[9] = NULL;
}
}
while(curr->next != NULL){
if(curr->next == leaver){
User *a = curr->next;
free(a);
curr->next = curr->next->next;
}
else{
int j = 0;
for(int i = 0; i < 10; i++){
if(curr->next->friends[i] != leaver){
curr->next->friends[j] = curr->next->friends[i];
j++;
}
}
if(j == 8){
curr->next->friends[9] = NULL;
}
}
curr = curr->next;
}
return 0;
}
| help remove segmentation fault | null |
63c647ca5f004bf4898cf6bc | 2023-02-15T16:26:19.730Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
User *leaver = find_user(name, *user_ptr_del); //The user we want to delete
User *curr = *user_ptr_del; //for traversal
if(leaver == NULL || curr == NULL){ //check if leaver exists and if the userlist has anything in it
return 1;
}
if(curr == leaver){ //checks base case if leaver is there
*user_ptr_del = curr->next;
}
else if(curr != leaver){ //checks friendlist of curr to remove leaver if leaver is not the first element in the linked list
int j = 0;
for(int i = 0; i<10; i++){
if(curr->next->friends[i] != leaver){
curr->next->friends[j] = curr->next->friends[i];
j++;
}
}
if(j == 8){
curr->friends[9] = NULL;
}
}
while(curr->next != NULL){ //how we traverse through the loop
if(curr->next == leaver){ //action if a user is the leaver
curr->next = curr->next->next;
}
else{ //checkes elements for leaver as a friend
int j = 0;
for(int i = 0; i < 10; i++){
if(curr->next->friends[i] != leaver){
curr->next->friends[j] = curr->next->friends[i];
j++;
}
}
if(j == 8){
curr->next->friends[9] = NULL;
}
}
curr = curr->next;
}
free(leaver->profile_pic);
free(leaver->name);
for(int i = 0; i<10; i++){
leaver->friends[i] = NULL;
}
free(leaver->friends);
while(leaver->first_post != NULL){
Post *b = leaver->first_post->next;
free(leaver->first_post->author);
free(leaver->first_post->contents);
free(leaver->first_post->date);
free(leaver->first_post);
leaver->first_post = b;
}
free(leaver);
return 0;
} | remove non-zero offset errors | null |
63c647ca5f004bf4898cf6bc | 2023-02-15T16:28:23.168Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
User *leaver = find_user(name, *user_ptr_del); //The user we want to delete
User *curr = *user_ptr_del; //for traversal
if(leaver == NULL || curr == NULL){ //check if leaver exists and if the userlist has anything in it
return 1;
}
if(curr == leaver){ //checks base case if leaver is there
*user_ptr_del = curr->next;
}
else if(curr != leaver){ //checks friendlist of curr to remove leaver if leaver is not the first element in the linked list
int j = 0;
for(int i = 0; i<10; i++){
if(curr->next->friends[i] != leaver){
curr->next->friends[j] = curr->next->friends[i];
j++;
}
}
if(j == 8){
curr->friends[9] = NULL;
}
}
while(curr->next != NULL){ //how we traverse through the loop
if(curr->next == leaver){ //action if a user is the leaver
curr->next = curr->next->next;
}
else{ //checkes elements for leaver as a friend
int j = 0;
for(int i = 0; i < 10; i++){
if(curr->next->friends[i] != leaver){
curr->next->friends[j] = curr->next->friends[i];
j++;
}
}
if(j == 8){
curr->next->friends[9] = NULL;
}
}
curr = curr->next;
}
free(leaver->profile_pic);
free(leaver->name);
for(int i = 0; i<10; i++){
leaver->friends[i] = NULL;
}
free(leaver->friends);
while(leaver->first_post != NULL){
Post *b = leaver->first_post->next;
free(leaver->first_post->author);
free(leaver->first_post->contents);
free(leaver->first_post->date);
free(leaver->first_post);
leaver->first_post = b;
}
free(leaver);
return 0;
} | help with freeing memory stored in leaver | null |
63c647ca5f004bf4898cf6bc | 2023-02-15T16:30:26.607Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
User *leaver = find_user(name, *user_ptr_del); //The user we want to delete
User *curr = *user_ptr_del; //for traversal
if(leaver == NULL || curr == NULL){ //check if leaver exists and if the userlist has anything in it
return 1;
}
if(curr == leaver){ //checks base case if leaver is there
*user_ptr_del = curr->next;
}
else if(curr != leaver){ //checks friendlist of curr to remove leaver if leaver is not the first element in the linked list
int j = 0;
for(int i = 0; i<10; i++){
if(curr->next->friends[i] != leaver){
curr->next->friends[j] = curr->next->friends[i];
j++;
}
}
if(j == 8){
curr->friends[9] = NULL;
}
}
while(curr->next != NULL){ //how we traverse through the loop
if(curr->next == leaver){ //action if a user is the leaver
curr->next = curr->next->next;
}
else{ //checkes elements for leaver as a friend
int j = 0;
for(int i = 0; i < 10; i++){
if(curr->next->friends[i] != leaver){
curr->next->friends[j] = curr->next->friends[i];
j++;
}
}
if(j == 8){
curr->next->friends[9] = NULL;
}
}
curr = curr->next;
}
free(leaver->profile_pic);
free(leaver->name);
for(int i = 0; i<10; i++){
leaver->friends[i] = NULL;
}
free(leaver->friends);
while(leaver->first_post != NULL){
Post *b = leaver->first_post->next;
free(leaver->first_post->author);
free(leaver->first_post->contents);
free(leaver->first_post->date);
free(leaver->first_post);
leaver->first_post = b;
}
free(leaver);
return 0;
} | friends.c:348:5: error: ‘free’ called on pointer ‘leaver’ with nonzero offset 72 | null |
63c647ca5f004bf4898cf6bc | 2023-02-15T16:32:12.182Z | General Question | v1 | how do you free an element in a linked list struct? | null | null | null |
63c647ca5f004bf4898cf6bc | 2023-02-15T16:34:31.794Z | Question from Code | v1 | given these structs, how would you free a user? | typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[MAX_FRIENDS];
struct user *next;
} User;
typedef struct post {
char author[MAX_NAME];
char *contents;
time_t *date;
struct post *next;
} Post; | null | null |
63c647ca5f004bf4898cf6bc | 2023-02-15T16:35:44.805Z | Question from Code | v1 | given the structs, make a program that frees the memory associated with a user's friends | typedef struct user {
char name[32];
char profile_pic[32]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[10];
struct user *next;
} User;
typedef struct post {
char author[32];
char *contents;
time_t *date;
struct post *next;
} Post; | null | null |
63c647ca5f004bf4898cf6bc | 2023-02-16T00:06:43.481Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
User *leaver = find_user(name, *user_ptr_del); //The user we want to delete
User *curr = *user_ptr_del; //for traversal
if(leaver == NULL || curr == NULL){ //check if leaver exists and if the userlist has anything in it
return 1;
}
if(curr == leaver){ //checks head case if leaver is there
*user_ptr_del = curr->next;
}
while(curr->next != NULL){
if(curr->next == leaver){
curr->next = curr->next->next;
}
curr = curr->next;
}
for(int i = 0; i< 10; i++){
if(leaver->friends[i] == NULL){
continue;
}
int k = 0;
for(int j = 0; j<10; j++){
if(leaver->friends[i]->friends[j] != leaver){
leaver->friends[i]->friends[k] = leaver->friends[i]->friends[j];
k++;
}
}
while(k<9){
leaver->friends[i]->friends[k+1] = NULL;
k+=1;
}
}
// free(leaver->name);
for(int i = 0; i<10; i++){
leaver->friends[i] = NULL;
}
while(leaver->first_post != NULL){
Post *temp = leaver->first_post->next; //temp post
free(leaver->first_post->contents);
free(leaver->first_post->date);
free(leaver->first_post);
leaver->first_post = temp;
}
free(leaver);
return 0;
} | fix memory leak | null |
63c647ca5f004bf4898cf6bc | 2023-02-16T00:07:53.037Z | 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) {
User *curr = *user_ptr_add; // pointer to head of user list
if(strlen(name) >= MAX_NAME){
return 2;
}
if(*user_ptr_add == NULL){ // if user list is empty
User *newbie = (User *) malloc(sizeof(User));
if(newbie == NULL){
exit(1);
}
strcpy(newbie->name, name); //copy name
*user_ptr_add = newbie; // add user with name to head of list
return 0;
}
while(curr->next != NULL){ //traverse through link list
if(strcmp(curr->name, name) == 0){ //check for case 1 at each node
return 1;
}
curr = curr->next; //traversal step
}
if(strcmp(name, curr->name)==0){
return 1;
}
User *user = (User *) malloc(sizeof(User));
if(user == NULL){
exit(1);
}
strcpy(user->name, name); //copy name into user
for(int i = 0; i<10; i++){
user->friends[i] = NULL;
}
strcpy(user->profile_pic, ""); //set profile pic to empty string
user->first_post = NULL; //set first post to NULL
user->next = NULL; //set next to NULL
curr->next = user; //add user to tail of list
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; //current user in head after first if statement
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) {
User *user = (User *) curr; // pointer to head of user list
printf("User List\n");
while(user != NULL){
printf("\t%s\n", user->name);
user = user->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 *f = fopen(filename, "r"); // Open the file for reading.
// Check for if the filename is too long.
if(strlen(filename) >= MAX_NAME){
fclose(f);
return 2;
}
// Check for if the file could not be opened.
else if(f == NULL){
fclose(f);
return 1;
}
else{
strcpy(user->profile_pic, filename); // Copy the filename to the user's profile picture field.
fclose(f);
return 0;
}
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already fri ends, 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) {
// Find user a(name1) and b(name2) using find_user
User *a = find_user(name1, head);
User *b = find_user(name2, head);
// Initialize counters for user a and b
int num_a = 0; //number of friends of user a
int num_b = 0; //number of friends of user b
// Check if either user does not exist
if(a == NULL || b == NULL){
return 4;
}
// Check if the two users are the same
if(a == b){
return 3;
}
// Find an empty spot in user a's friend list
while(num_a < 10 && a->friends[num_a] != NULL){
if(a->friends[num_a] == b){
return 1;
}
num_a += 1;
}
// Find an empty spot in user b's friend list
while(num_b < 10 && b->friends[num_b] != NULL){
if(b->friends[num_b] == a){
return 1;
}
num_b += 1;
}
// Check if either user has reached the friend limit (10)
if(num_a > 9 || num_b > 9){
return 2;
}
// Add user b to user a's friend list
a->friends[num_a] = b;
// Add user a to user b's friend list
b->friends[num_b] = a;
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) {
// check if user is NULL
if(user == NULL){
return 1;
}
// declare a file pointer, a char and a Post pointer
FILE *f;
char c;
Post *currpost = user->first_post;
// set the file pointer to the opened profile picture file for reading
f = fopen(user->profile_pic, "r");
// if the file exists, print its contents
if(f!=NULL){
while ((c = fgetc(f)) != EOF) {
printf("%c", c);
}
fclose(f);
}
// print the user's name
printf("\n");
printf("%s %s\n", "Name:", user->name);
printf("------------------------------------------\n");
// print the user's friends
printf("Friends:\n");
for(int i = 0; i < 10; i++){
if(user->friends[i] != NULL){
printf("%s\n", user->friends[i]->name);
}
}
printf("------------------------------------------\n");
// print the user's posts
printf("Posts:\n");
if(currpost != NULL){
printf("From: %s\n", currpost->author);
printf("Date: %s\n", ctime(currpost->date));
printf("%s\n", currpost->contents);
// iterate over all other elements of the linked list and print each post
while(currpost->next != NULL){
printf("\n");
printf("===\n");
printf("\n");
printf("From: %s\n", currpost->next->author);
printf("Date: %s\n", ctime(currpost->next->date));
printf("%s\n", currpost->next->contents);
currpost = currpost->next;
}
}
// print the separator line and return 0 to indicate success
printf("------------------------------------------\n");
return 0;
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
Post *target_first_post = target->first_post; // the first post of the target user
time_t *t = malloc(sizeof(time_t)); // allocate memory for time
if(t == NULL){ //checking for malloc error
exit(1);
}
time(t); // get the current time and store it in the time t
Post *newpost = (Post *) malloc(sizeof(Post)); // allocate memory for the new post
if(newpost == NULL){
exit(1);
}
if(author == NULL || target == NULL){
return 2;
}
for(int i = 0; i < 10; i++){ // check if target is a friend of author
if(author->friends[i] == target){
strcpy(newpost->author, author->name); //copy author name into new post
newpost->date = t; // set the date of the new post to the current time
newpost->contents = contents; // set the contents of the new post to the given contents
newpost->next = target_first_post; // adds the old first post to newpost's next to keep order consistent
target->first_post = newpost; // update the first post of the target user to the 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) {
User *leaver = find_user(name, *user_ptr_del); //The user we want to delete
User *curr = *user_ptr_del; //for traversal
if(leaver == NULL || curr == NULL){ //check if leaver exists and if the userlist has anything in it
return 1;
}
if(curr == leaver){ //checks head case if leaver is there
*user_ptr_del = curr->next;
}
while(curr->next != NULL){
if(curr->next == leaver){
curr->next = curr->next->next;
}
curr = curr->next;
}
for(int i = 0; i< 10; i++){
if(leaver->friends[i] == NULL){
continue;
}
int k = 0;
for(int j = 0; j<10; j++){
if(leaver->friends[i]->friends[j] != leaver){
leaver->friends[i]->friends[k] = leaver->friends[i]->friends[j];
k++;
}
}
while(k<9){
leaver->friends[i]->friends[k+1] = NULL;
k+=1;
}
}
free(leaver->name);
for(int i = 0; i<10; i++){
leaver->friends[i] = NULL;
}
while(leaver->first_post != NULL){
Post *temp = leaver->first_post->next; //temp post
free(leaver->first_post->contents);
free(leaver->first_post->date);
free(leaver->first_post);
leaver->first_post = temp;
}
free(leaver);
return 0;
} | fix memory leak | null |
63c647ca5f004bf4898cf6bc | 2023-02-28T22:17:57.421Z | General Question | v1 | What does char arr[] mean? | null | null | null |
63c647ca5f004bf4898cf6bc | 2023-02-28T22:18:22.040Z | General Question | v1 | is char arr[] stack or read only memory | null | null | null |
63c647ca5f004bf4898cf6bc | 2023-02-28T22:22:59.168Z | General Question | v1 | what does a regular file mean | null | null | null |
63c647ca5f004bf4898cf6bc | 2023-02-28T22:23:38.244Z | General Question | v1 | how to chmod | null | null | null |
63c647ca5f004bf4898cf6bc | 2023-03-03T21:43:31.914Z | General Question | v1 | How do we make sure only a parent can call fork? | null | null | null |
63c647ca5f004bf4898cf6bc | 2023-03-03T22:00:01.021Z | Question from Code | v1 | Why is each the pid of each child class the same? | #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
} | null | null |
63c647ca5f004bf4898cf6bc | 2023-03-03T22:07:38.925Z | General Question | v1 | How do you make sure a process only creates one fork? | null | null | null |
63c647ca5f004bf4898cf6bc | 2023-03-03T22:13:34.457Z | Question from Code | v1 | how could I make sure each process only produces one distinct process? | #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
} | null | null |
63c647ca5f004bf4898cf6bc | 2023-03-10T20:30:25.097Z | Explain Code | v2 | null | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAXLINE 32
#define MAX_PASSWORD 10
#define PASSWORD_FILE "pass.txt"
int main(void) {
int n, user_length;
char userid[MAXLINE];
char password[MAXLINE];
if ((n = read(STDIN_FILENO, userid, MAX_PASSWORD)) == -1) {
perror("read");
exit(1);
} else if (n == 0) {
fprintf(stderr, "Error: could not read from stdin");
exit(1);
}
if (n <= MAX_PASSWORD) {
userid[n] = '\0';
}
char *newline;
if ((newline = strchr(userid, '\n')) != NULL) {
*newline = '\0';
}
if ((n = read(STDIN_FILENO, password, MAX_PASSWORD)) == -1) {
perror("read");
exit(1);
} else if (n == 0) {
fprintf(stderr, "Error: could not read from stdin");
exit(1);
}
if (n <= MAX_PASSWORD) {
password[n] = '\0';
}
if ((newline = strchr(password, '\n')) != NULL) {
*newline = '\0';
}
strncat(userid, ":", MAXLINE - strlen(userid) - 1);
user_length = strlen(userid);
strncat(userid, password, MAXLINE - strlen(userid) - 1);
FILE *fp = fopen(PASSWORD_FILE, "r");
if (!fp) {
perror("fopen");
exit(1);
}
char line[MAXLINE];
while (fgets(line, sizeof(line) - 1, fp)) {
line[strlen(line) - 1] = '\0';
if (strcmp(userid, line) == 0) {
fclose(fp);
exit(0);
} else if (strncmp(userid, line, user_length) == 0) {
fclose(fp);
exit(2);
}
}
fclose(fp);
exit(3);
} | null | null |
63c647ca5f004bf4898cf6bc | 2023-03-10T20:49:27.936Z | Help Write Code | v2 | null | null | null | Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful. |
63c647ca5f004bf4898cf6bc | 2023-03-10T21:51:32.999Z | Help Fix Code | v2 | The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLINE 256
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
int fd[2];
pipe(fd);
int pid = fork();
if (pid == 0) {
close(fd[1]);
dup2(fd[0], 0);
execl("./validate", "validate", user_id, password, NULL);
}
else {
close(fd[0]);
write(fd[1], user_id, strlen(user_id));
write(fd[1], "\n", 1);
write(fd[1], password, strlen(password));
close(fd[1]);
int status;
wait(&status);
if (WEXITSTATUS(status) == 1) {
printf(SUCCESS"\n");
}
else if (WEXITSTATUS(status) == 2) {
printf(INVALID"\n");
}
else if (WEXITSTATUS(status) == 3) {
printf(NO_USER"\n");
}
}
return 0;
} | null | null |
63c647ca5f004bf4898cf6bc | 2023-03-10T22:03:33.059Z | Help Fix Code | v2 | prints if password verified, invalid password, or no such user | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLINE 256
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
int fd[2];
pipe(fd);
int pid = fork();
if (pid == 0) {
close(fd[1]);
dup2(fd[0], 0);
execl("./validate", "validate", user_id, password, NULL);
}
else {
close(fd[0]);
write(fd[1], user_id, strlen(user_id));
write(fd[1], "\n", 1);
write(fd[1], password, strlen(password));
close(fd[1]);
int status;
wait(&status);
if (WEXITSTATUS(status) == 1) {
printf(SUCCESS"\n");
}
else if (WEXITSTATUS(status) == 2) {
printf(INVALID"\n");
}
else if (WEXITSTATUS(status) == 3) {
printf(NO_USER"\n");
}
}
return 0;
} | null | null |
63c647cb5f004bf4898cf6c6 | 2023-01-19T23:37:13.639Z | Question from Code | v1 | What does this code do? | #include <stdio.h>
#include <stdlib.h>
int main() {
char phone[11];
int num;
int error = 0;
scanf("%10s", phone);
while (scanf("%d", &num) > 0) {
if (num == -1) {
printf("%s\n", phone);
} else if (num >= 0 && num <= 9) {
printf("%c\n", phone[num]);
} else {
printf("ERROR\n");
error = 1;
}
}
return error;
} | null | null |
63c647cb5f004bf4898cf6c6 | 2023-01-19T23:38:48.037Z | Explain Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
int main() {
char phone[11];
int num;
int error = 0;
scanf("%10s", phone);
while (scanf("%d", &num) > 0) {
if (num == -1) {
printf("%s\n", phone);
} else if (num >= 0 && num <= 9) {
printf("%c\n", phone[num]);
} else {
printf("ERROR\n");
error = 1;
}
}
return error;
} | null | null |
63c647cb5f004bf4898cf6c6 | 2023-01-22T03:05:56.037Z | General Question | v1 | How do you convert string to int? | null | null | null |
63c647cc5f004bf4898cf6d0 | 2023-01-30T00:42:01.850Z | Question from Code | v1 | why does this code have segmentation fault? | int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2; }
int are_friends = 0;
for (int i = 0; i < MAX_FRIENDS; i++){
if ((author->friends)[i] == target){
are_friends = 1;
}
}
if (are_friends == 0){
return 1;
}
struct post *temp1=malloc(sizeof(struct post));
temp1 = author->first_post;
struct post *temp2=malloc(sizeof(struct post));
temp2 = target->first_post;
strcpy((char *)author->name, author->first_post->author);
author->first_post->contents = contents;
time(author-> first_post->date);
time(target-> first_post->date);
strcpy((char*)author->name, author->first_post->author);
target->first_post->contents = contents;
author->first_post->next = temp1;
target->first_post->next = temp2;
return 0;
}
| null | null |
63c647cc5f004bf4898cf6d0 | 2023-01-30T01:06:05.303Z | Help Fix Code | v1 | null | int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2; }
int are_friends = 0;
for (int i = 0; i < MAX_FRIENDS; i++){
if ((author->friends)[i] == target){
are_friends = 1;
}
}
if (are_friends == 0){
return 1;
}
Post *temp1=malloc(sizeof(Post));
*temp1 = *(author->first_post);
Post *temp2=malloc(sizeof(Post));
*temp2 = *(target->first_post);
strcpy((char *)author->name, author->first_post->author);
author->first_post->contents = contents;
time(author-> first_post->date);
time(target-> first_post->date);
strcpy((char*)(author->name), author->first_post->author);
target->first_post->contents = contents;
(author->first_post)->next = temp1;
(target->first_post)->next = temp2;
return 0;
} | adds the post to the front of the linkedlist of author's and target's posts. | null |
63c647cc5f004bf4898cf6d0 | 2023-03-08T03:53:11.102Z | Question from Code | v1 | why does this code result in segmentation fault? | Rule *parse_file(FILE *fp) {
// Implement this function and remove the stubbed return statement below.
int count;
char *placeholder = malloc(sizeof(char)*MAXLINE);
char *line = fgets(placeholder, MAXLINE, fp);
Rule *curr = malloc(sizeof(Rule));
Rule *head = curr;
int com_count=0;
int act = 0;
char **tab;
while (line != NULL){
if (is_comment_or_empty(placeholder)){
line = fgets(placeholder, MAXLINE, fp);
continue;
}
char *target = malloc(sizeof(char)*MAXLINE);
char *colon = strchr(placeholder, ':');
Action *acurr;
if (colon != NULL){
act = 0;
strncpy(target, placeholder, colon - line-1);
target[colon-line-1] = '\0';
strcpy(curr->target, target);
curr -> next_rule = malloc(sizeof(Rule));
curr = curr->next_rule;
char *dep = colon + 2;
char *tok = strtok(dep, " ");
count = 0;
while (tok != NULL){
tab[com_count][count] = *tok;
tok = strtok(NULL, " ");
count += 1;
}
tab[com_count][count] = '\0';
}
else{
if (act==0){
act = 1;
acurr = malloc(sizeof(Action));
char *tok = strtok(placeholder, " ");
int index = 0;
while (tok != NULL){
(acurr -> args)[index] = tok;
tok = strtok(NULL, " ");
index ++;
}
(acurr -> args)[index] = NULL;
}
else{
Action *next = malloc(sizeof(Action));
char *tok = strtok(placeholder, " ");
int index = 0;
while (tok != NULL){
(next->args)[index] = tok;
tok = strtok(NULL, " ");
index ++;
}
(acurr -> args)[index] = NULL;
acurr->next_act = next;
acurr = acurr->next_act;
}
}
com_count ++;
free(target);
line = fgets(placeholder, MAXLINE, fp);
}
Rule *cur = head;
com_count = 0;
while (cur != NULL){
count = 0;
while (tab[com_count][count] != '\0'){
Rule *inner_curr = head;
Dependency *dep_cur = malloc(sizeof(Dependency));
while (inner_curr != NULL){
if(strcmp(inner_curr->target, &tab[com_count][count]) == 0){
dep_cur->rule = inner_curr;
dep_cur->next_dep = malloc(sizeof(Dependency));
dep_cur = dep_cur->next_dep;
}
inner_curr = inner_curr->next_rule;
}
count ++;
}
com_count ++;
cur = cur->next_rule;
}
return head;
}
| null | null |
63c647cc5f004bf4898cf6d0 | 2023-03-08T10:21:16.907Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pmake.h"
#define deli " \t\n\r\f\v\\\0"
int is_comment_or_empty(const char *line) {
for (int i = 0; i < strlen(line); i++){
if (line[i] == '#') {
return 1;
}
if (line[i] != '\t' && line[i] != ' ') {
return 0;
}
}
return 1;
}
int main() {
char *filename = "Makefile";
FILE *fp=fopen(filename, "r");
int count;
char *placeholder = malloc(sizeof(char)*MAXLINE);
char *line = fgets(placeholder, MAXLINE, fp);
int line_count=0;
Rule *curr = malloc(sizeof(Rule));
while (line != NULL){
line = fgets(placeholder, MAXLINE, fp);
line_count ++;
}
fseek(fp, 0, SEEK_SET);
int start = 0;
Rule *temp = curr;
int com_count=0;
char ***tab = malloc(sizeof(char**)*line_count);
line = fgets(placeholder, MAXLINE, fp);
while (line != NULL){
if (is_comment_or_empty(placeholder)){
line = fgets(placeholder, MAXLINE, fp);
continue;
}
else{
char *target = malloc(sizeof(char*)*MAXLINE);
char *colon = strchr(placeholder, ':');
Action *acurr;
if (colon != NULL){
strncpy(target, placeholder, colon - line);
target[colon-line-1] = '\0';
curr->target=target;
char *dep = malloc(sizeof(Dependency)*MAXLINE);
strcpy(dep, colon + 2);
char *tok = strtok(dep, deli);
count = 0;
char **str = malloc(sizeof(char*)*MAXLINE);
while (tok != NULL){
str[count] = tok;
tok = strtok(NULL, deli);
count += 1;
}
str[count] = NULL;
tab[com_count] = str;
colon = NULL;
com_count++;
}
else{
Action *acurr = malloc(sizeof(Action));
Action *aprev;
aprev = NULL;
char *tok1 = strtok(placeholder, deli);
int index = 0;
int space_count = 0;
for (int i = 1; i < MAXLINE; i++){
if (placeholder[i-1] == ' ' && placeholder[i]!= ' '){
space_count ++;
}
}
char **args = malloc((space_count +1)* sizeof(char*));
while (tok1 != NULL){
args[index]= tok1;
tok1 = strtok(NULL, deli);
index ++;
}
args[index]=NULL;
acurr->args=args;
if (aprev != NULL){
aprev->next_act= acurr;}
acurr=NULL;
}
}
line = fgets(placeholder, MAXLINE, fp);
if (line==NULL){
curr->next_rule = NULL;
}
else {
curr->next_rule = malloc(sizeof(Rule));
curr=curr->next_rule;
}
}
printf("%s\n", temp->target);
return 0;
}
| there shouldn't be an abortion. | null |
63c647cc5f004bf4898cf6d0 | 2023-03-09T01:51:03.692Z | Help Fix Code | v1 | null | void tokenize(char *line, Action *act; Action *next_a){
int white_spaces = 0;
int count = 0;
for (int i = 1; i < MAXLINE; i++){
if (line[i-1] == ' ' &&line[i] != ' '){
white_spaces += 1;
}
if (line[i] == '\t' || line[i] == '\n' || line[i] == '\r'){
break;
}
}
char **args = malloc(sizeof(char*)*(white_spaces+1));
char *tok = strtok(placeholder, deli);
while (tok != NULL){
args[count] = tok;
tok = strtok(NULL, deli);
count ++;
}
act->args = args;
act->next_act = next_a;
}
| avoid forward declaration error/ | null |
63c647cc5f004bf4898cf6d0 | 2023-03-09T01:52:52.016Z | General Question | v1 | what should i do if i encounter a forward declaration error?
| null | null | null |
63c647cc5f004bf4898cf6d0 | 2023-03-09T03:44:31.299Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pmake.h"
#define deli " \t\n\r\f\v\\"
int is_comment_or_empty(const char *line) {
for (int i = 0; i < strlen(line); i++){
if (line[i] == '#') {
return 1;
}
if (line[i] != '\t' && line[i] != ' ') {
return 0;
}
}
return 1;
}
void tokenize(char *line, Action *act, Action *next_a){
int white_spaces = 0;
int count = 0;
for (int i = 1; i < MAXLINE; i++){
if (line[i-1] == ' ' &&line[i] != ' '){
white_spaces += 1;
}
if (line[i] == '\t' || line[i] == '\n' || line[i] == '\r'){
break;
}
}
char *placeholder;
char **args = malloc(sizeof(char*)*(white_spaces+1));
char *tok = strtok(placeholder, deli);
while (tok != NULL){
args[count] = tok;
tok = strtok(NULL, deli);
count ++;
}
act->args = args;
act->next_act = next_a;
}
int main() {
char *filename = "Makefile";
FILE *fp=fopen(filename, "r");
Action *acurr=NULL;
int count=0;
int com_count=0;
char *tok1;
Action *aprev=NULL;
Action *ahead=NULL;
char *placeholder = malloc(sizeof(char)*MAXLINE);
char *line = fgets(placeholder, MAXLINE, fp);
int line_count=0;
Rule *head = malloc(sizeof(Rule));
Rule *prev = NULL;
while (line != NULL){
line = fgets(placeholder, MAXLINE, fp);
line_count ++;
}
fseek(fp, 0, SEEK_SET);
Rule *curr = head;
com_count=0;
char ***tab = malloc(sizeof(char**)*line_count);
line = fgets(placeholder, MAXLINE, fp);
char *colon = strchr(line, ':');
while (line != NULL){
if (is_comment_or_empty(placeholder)){
line = fgets(placeholder, MAXLINE, fp);
}
else{
colon = strchr(placeholder, ':');
char *target = malloc(sizeof(char)*MAXLINE);
if (colon != NULL){
if (ahead != NULL){
curr->actions=ahead;
}
if (aprev != NULL){
aprev->next_act = NULL;}
aprev = NULL;
acurr == NULL;
ahead=NULL;
strncpy(target, placeholder, colon - line);
target[colon-line-1] = '\0';
curr->target=target;
char *dep = malloc(sizeof(Dependency)*MAXLINE);
strcpy(dep, colon + 2);
char *tok = strtok(dep, deli);
count = 0;
char **str = malloc(sizeof(char*)*MAXLINE);
while (tok != NULL){
str[count] = tok;
tok = strtok(NULL, deli);
count += 1;
}
str[count] = NULL;
tab[com_count] = str;
com_count++;}
}
line = fgets(placeholder, MAXLINE, fp);
if (colon != NULL){
curr->next_rule = malloc(sizeof(Rule));
prev = curr;
curr=curr->next_rule;
}
}
prev->next_rule = NULL;
free(curr);
curr = head;
Action *act = NULL;
Action *act_head;
Action *prev_a = NULL;
int state = 0;
fseek(fp, 0, SEEK_SET);
line = fgets(placeholder, MAXLINE, fp);
Rule *last_state = NULL;
Action *next_a = NULL;
while (line != NULL && curr != NULL){
colon = strchr(placeholder, ':');
char *target = malloc(sizeof(char)*MAXLINE);
strncpy(target, placeholder, colon-line);
target[colon-line-1]='\0';
if (colon != NULL && strcmp(curr->target, target)==0&& state==0){
last_state = curr;
curr = curr -> next_rule;
state = 1;}
else if (colon == NULL && state == 1){
act = malloc(sizeof(Action));
tokenize(line, act, NULL);
curr->actions = act_head;
last_state -> actions = act_head;
state = 2;
}
else if (state == 2 && colon == NULL){
prev_a = act;
next_a = malloc(sizeof(Action));
tokenize(line, act, next_a);
}
else if (state == 2 && colon != NULL){
state = 1;
act->next_act = NULL;
next_a = NULL;
act_head=NULL;
last_state = curr;
}
else if (state == 1 && colon != NULL){
state = 1;
next_a = NULL;
act_head = NULL;
last_state = curr;
}
line = fgets(placeholder, MAXLINE, fp);
colon = strchr(placeholder, ':');
}
curr = head;
com_count = 0;
Rule *cur = head;
fseek(fp, 0, SEEK_SET);
while (cur != NULL){
count = 0;
while (tab[com_count][count] != NULL){
Rule *inner_curr = head;
Dependency *dep_curr = NULL;
Dependency *dep_head = NULL;
int found=0;
while (inner_curr != NULL){
if(strcmp(inner_curr->target, tab[com_count][count]) == 0){
found=1;
if (dep_curr==NULL){dep_curr = malloc(sizeof(Dependency));
dep_head = dep_curr;}
dep_curr->rule = inner_curr;
if (tab[com_count][count+1]!=NULL){
dep_curr->next_dep = malloc(sizeof(Dependency));
dep_curr = dep_curr->next_dep;
}
else{
dep_curr->next_dep=NULL;
cur->dependencies = dep_head;
}
}
inner_curr = inner_curr->next_rule;
}
if (found == 0 && dep_head == NULL){
dep_curr = malloc(sizeof(Dependency));
dep_head = dep_curr;
dep_curr->rule = malloc(sizeof(Rule));
dep_curr->rule->target = tab[com_count][count];
dep_curr->rule->dependencies = NULL;
dep_curr->rule->actions = NULL;
dep_curr->rule->next_rule = NULL;
curr->next_rule = dep_curr->rule;
curr = curr->next_rule;
cur = cur->next_rule;
}
else if(found == 0){
dep_curr->next_dep = malloc(sizeof(Dependency));
dep_curr->next_dep->rule=malloc(sizeof(Rule));
dep_curr->next_dep->rule->target = tab[com_count][count];
dep_curr->next_dep->rule->next_rule=NULL;
dep_curr->next_dep->rule->dependencies = NULL;
curr->next_rule = dep_curr->next_dep->rule;
curr = curr->next_rule;
dep_curr = dep_curr->next_dep;
cur = cur->next_rule;
}
count ++;
}
com_count ++;
cur = cur->next_rule;
}
curr->next_rule = NULL;
while (head != NULL){
printf("Targ: %s\n",head->target);
Dependency *dep = head->dependencies;
while (dep != NULL){
printf("Dep: %s\n", dep->rule->target);
dep = dep->next_dep;
}
Action *act = head->actions;
while (act != NULL){
printf("Act: %s\n", *(head->actions->args));
act = act->next_act;
}
head = head->next_rule;
}
return 0;
} | segmentation fault | null |
63c647cd5f004bf4898cf6da | 2023-01-28T19:13:05.920Z | Question from Code | v1 | How to create a post with the current time? | typedef struct post {
char author[MAX_NAME];
char *contents;
time_t *date;
struct post *next;
} Post; | null | null |
63c647cd5f004bf4898cf6da | 2023-01-28T19:14:00.507Z | Question from Code | v1 | Can you generate a c code segment that creates a post with the current timestamp? | typedef struct post {
char author[MAX_NAME];
char *contents;
time_t *date;
struct post *next;
} Post; | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-09T17:03:03.937Z | General Question | v1 | When I declare a struct, can I use it as a pointer the same way as an array? | null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-09T17:04:33.094Z | General Question | v1 | how do I create a struct | null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-09T17:08:44.428Z | General Question | v1 | How do I assign values to parameters of structs | null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-09T17:09:38.107Z | General Question | v1 | When I use the dot operator on structs, does it give me a pointer? | null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-09T17:10:13.036Z | General Question | v1 | How do I assign a value to a struct? | null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-09T17:15:34.092Z | General Question | v1 | How do I close a bitmap file | null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-09T17:17:04.911Z | General Question | v1 | how do I deallocate memory | null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-09T17:28:06.769Z | General Question | v1 | what does segmentation fault mean | null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-09T17:31:54.493Z | General Question | v1 | how do I use gdb | null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-09T17:34:17.441Z | General Question | v1 | how do I use gdb to find the value of a variable | null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-09T17:36:53.750Z | General Question | v1 | how to step out using gdb | null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-09T17:39:10.557Z | Question from Code | v1 | I'm getting a segmentation fault, and I think its from this function. Do you know why? | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel **to_return = malloc(sizeof(struct pixel **) * height);
for (int i = 0; i < height; i++) {
to_return[i] = malloc(sizeof(struct pixel *) * width);
}
fseek(image, pixel_array_offset, SEEK_SET);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
struct pixel my_pixel;
fread(my_pixel.blue, sizeof(unsigned char), 1, image);
fread(my_pixel.green, sizeof(unsigned char), 1, image);
fread(my_pixel.red, sizeof(unsigned char), 1, image);
to_return[i][j] = my_pixel;
}
}
return to_return;
} | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-09T17:41:41.736Z | General Question | v1 | Can you explain a little more from your previous answer? | null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-09T17:43:09.951Z | Question from Code | v1 | I still don't understand why I'm getting a segmentation fault | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel **to_return = malloc(sizeof(struct pixel **) * height);
for (int i = 0; i < height; i++) {
to_return[i] = malloc(sizeof(struct pixel *) * width);
}
fseek(image, pixel_array_offset, SEEK_SET);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
struct pixel my_pixel;
fread(my_pixel.blue, sizeof(unsigned char), 1, image);
fread(my_pixel.green, sizeof(unsigned char), 1, image);
fread(my_pixel.red, sizeof(unsigned char), 1, image);
to_return[i][j] = my_pixel;
}
}
return to_return;
} | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-10T16:44:34.961Z | General Question | v1 | what does "expression must be a modifiable lvalue" mean? | null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-10T17:06:03.444Z | Question from Code | v1 | I'm getting a segmentation fault at line 27. Why? | int create_user(const char *name, User **user_ptr_add) {
// MAX_NAME - 1 since we need the null terminator in a name, we need the last char to be \0
if (strlen(name) > MAX_NAME - 1) {
return 2;
}
// Head of the linked list
User *curr = *user_ptr_add;
// Case where curr is null
if (curr == NULL) {
User *user = malloc(sizeof(User));
strncpy(user->name, name, MAX_NAME - 1);
curr = user;
return 0;
}
// Keep looping until curr is null
while (curr != NULL) {
// Return if curr has the same name as name i.e. name alraedy exists in linked list
if (curr->name == name) {
return 1;
}
// Add a new user with name as name to end of list if we reach the end of the linked list
if (curr->next == NULL) {
User *user = malloc(sizeof(User));
strncpy(user->name, name, MAX_NAME - 1);
user->name[MAX_NAME - 1] = '\0';
curr->next = user;
return 0;
}
// Keep traversing linked list
curr = curr->next;
}
} | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-14T04:29:40.413Z | Question from Code | v1 | What happens to the declaration on line 2 after the c pre-processor? | #define MAXNAME = 32;
char name[MAXNAME]; | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-15T00:53:26.734Z | Question from Code | v1 | I'm getting the error "Conditional jump or move depends on uninitialised value(s)" from this function at line 26. | int print_user(const User *user) {
// Return if the user doesn't exist
if (user == NULL) {
return 1;
}
// Print the user's profile pic only if the file exists
FILE *file = fopen(user->profile_pic, "r");
if (file != NULL) {
char line[MAX_NAME + 100];
while(fgets(line, MAX_NAME + 100, file) != NULL) {
printf("%s", line);
}
printf("\n");
fclose(file);
}
// Print the user's name
printf("%s", "Name: ");
printf("%s\n", user->name);
printf("%s\n", "------------------------------------------");
// Print the user's friends' names
printf("%s\n", "Friends:");
for (int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] != NULL) {
printf("%s\n", user->friends[i]->name);
}
}
printf("%s\n", "------------------------------------------");
// Print posts
printf("%s\n", "Posts:");
Post *curr = user->first_post;
while (curr != NULL) {
printf("%s", "From: ");
printf("%s\n", curr->author);
printf("%s", "Date: ");
printf("%s\n", ctime(curr->date));
printf("%s\n", curr->contents);
if (curr->next != NULL) {
printf("\n");
printf("%s\n", "===");
printf("\n");
}
curr = curr->next;
}
printf("%s\n", "------------------------------------------");
return 0;
} | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-28T07:47:24.424Z | General Question | v1 | Which macro is used with wait's status to check if the child process was terminated by a signal?
| null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-02-28T07:49:48.739Z | General Question | v1 | What is the name for a child process whose parent terminates before it does? | null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-03-10T21:13:35.206Z | Question from Code | v2 | my code looks like this.
Validate is a program that will validate the username and password.
If the password is correct for this user, it exits with 0.
If the password is incorrect, it exits with 2.
My program keeps exiting with 2 even with a correct password. I'm wondering if my pipes are incorrect | int fd[2];
if (pipe(fd) == -1) {
perror("pipe");
}
int r = fork();
if (r > 0) { // Parent
close(fd[0]);
// // Connect stdout to pipe
// dup2(fd[1], fileno(stdout));
// close(fd[1]);
// Feed information into the pipe
if (write(fd[1], user_id, MAXLINE) == -1) {
perror("write to pipe");
}
if (write(fd[1], password, MAXLINE) == -1) {
perror("write to pipe");
}
close(fd[1]);
int status;
if (wait(&status) != -1) {
if (WIFEXITED(status)) {
printf("[%d] Child exited with %d\n", getpid(), WEXITSTATUS(status));
} else {
printf("[%d] Child exited abnormally\n", getpid());
}
}
} else if (r == 0) { // Child
// Connect stdin to pipe
dup2(fd[0], fileno(stdin));
close(fd[0]);
close(fd[1]);
// Call exec for validate
execl("./validate", "validate", NULL);
} | null | null |
63c647cf5f004bf4898cf6e4 | 2023-03-10T21:28:03.526Z | Question from Code | v2 | Valgrind gives an error for lines 17 and 21. What should I do to fix it? | int fd[2];
if (pipe(fd) == -1) {
perror("pipe");
}
int r = fork();
if (r > 0) { // Parent
close(fd[0]);
// // Connect stdout to pipe
// dup2(fd[1], fileno(stdout));
// close(fd[1]);
// Feed information into the pipe
if (write(fd[1], user_id, MAXLINE) == -1) {
perror("write to pipe");
}
if (write(fd[1], password, MAXLINE) == -1) {
perror("write to pipe");
}
close(fd[1]);
int status;
if (wait(&status) != -1) {
if (WIFEXITED(status)) {
printf("[%d] Child exited with %d\n", getpid(), WEXITSTATUS(status));
} else {
printf("[%d] Child exited abnormally\n", getpid());
}
}
} else if (r == 0) { // Child
// Connect stdin to pipe
dup2(fd[0], fileno(stdin));
close(fd[0]);
close(fd[1]);
// Call exec for validate
execl("./validate", "validate", NULL);
} | null | null |
63c647cf5f004bf4898cf6e4 | 2023-03-12T22:53:46.977Z | Question from Code | v2 | I'm getting a segmentation fault at line 50 | typedef struct rule_node {
char *target;
Dependency *dependencies;
Action *actions;
struct rule_node *next_rule;
} Rule;
Rule *parse_file(FILE *fp) {
// Implement this function and remove the stubbed return statement below.
// For reading lines
char line[MAXLINE + 1];
// For linked list traversal
Rule *curr = NULL;
// Create a pointer to the head of our rules linked list
Rule *head = curr;
while (fgets(line, MAXLINE + 1, fp) != NULL) {
// Change new line character to null terminator
int len = strlen(line);
line[len - 1] = '\0';
// Only do things to lines that are not comments or empty
if (!is_comment_or_empty(line)) {
// Case for action line
if (line[0] == '\t') {
}
// Case for target line
else {
// Getting target
char *p = strstr(line, " :");
int length = p - line;
line[length] = '\0';
// Creating a new rule
Rule *rule = malloc(sizeof(Rule));
if (curr == NULL) {
curr = rule;
head = curr;
} else {
curr->next_rule = rule;
curr = curr->next_rule;
}
rule->next_rule = NULL;
strcpy(rule->target, line);
rule->dependencies = NULL;
rule->actions = NULL;
// TODO: deal with dependencies
char *dependencies = p + 2;
}
}
}
curr = head;
while (curr != NULL) {
printf("%s\n", curr->target);
curr = curr->next_rule;
}
return head;
} | null | null |
63c647cf5f004bf4898cf6e4 | 2023-03-13T00:51:12.972Z | Question from Code | v2 | I'm getting a segmentation fault. Line is a character array. | char *action;
strcpy(action, line);
printf("%s\n", action); | null | null |
63c647cf5f004bf4898cf6e4 | 2023-03-13T02:49:04.919Z | General Question | v2 | how do I use stat | null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-03-13T03:01:31.764Z | Question from Code | v2 | I'm confused how to use stat to find last modified time. Here's what I have. | struct stat *stats = NULL;
stat("testy.txt", stats); | null | null |
63c647cf5f004bf4898cf6e4 | 2023-03-17T18:09:13.254Z | General Question | v2 | How does setitimer work | null | null | null |
63c647cf5f004bf4898cf6e4 | 2023-03-17T18:21:12.870Z | General Question | v2 | How do I use a timer to have a program wait | null | null | null |
63c647d05f004bf4898cf6f3 | 2023-01-25T02:00:26.085Z | Help Fix Code | v1 | null | void fib(int **arr, int count){
*arr = malloc(sizeof(int) * count);
if (count == 1){
*arr[0] = 0;
printf("*arr[0] %d", *arr[0]);
}
else {
*arr[0] = 0;
printf("*arr[0] %d", *arr[0]);
*arr[1] = 1;
printf("*arr[1] %d", *arr[1]);
for (int i = 2; i < count; i++){
*arr[i] = *arr[i-2] + *arr[i-1];
printf("*arr[%d] %d", i, *arr[i]);
}
}
}
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 |
63c647d05f004bf4898cf6f3 | 2023-01-27T21:42:54.854Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
// TODO: Implement a helper named check_permissions that matches the prototype below.
int check_permissions(char *, char *);
int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
// TODO: Process command line arguments.
int cutoff = strtol(argv[1], NULL, 10);
char type;
char permission[10];
int link;
char owner[32];
char group[32];
int size;
char remainder[50];
int n;
int eof;
eof = scanf("%s %d", remainder, &n);
int count = 0;
if (argc == 3){
while (eof != EOF){
eof = scanf("%c%s %d %s %s %d %[^\n]s\n", &type, permission, &link, owner, group, &size, remainder);
printf("type: %c permission: %s link: %d owner: %s group: %s size: %d remainder: %s", type, permission, link, owner, group, size, remainder);
if ((type == '-') && (check_permissions(permission, argv[2]) == 0) && (size > cutoff)){
count++;
}
printf(" COUNT %d\n", count);
}
}
else {
while (eof != EOF){
eof = scanf("%c%s %d %s %s %d %s", &type, permission, &link, owner, group, &size, remainder);
printf("type: %c permission: %s link: %d owner: %s group: %s size: %d remainder: %s", type, permission, link, owner, group, size, remainder);
if ((type == '-') && (size > cutoff)){
count++;
}
printf(" COUNT %d\n", count);
}
}
printf("%d\n", count);
return 0;
}
int check_permissions(char* p1, char* p2){
for (int i = 0; i < 9; i++){
if((p2[i] != '-') && (p1[i] == '-')) return 1;
}
return 0;
}
| read each line from standard input and parse it | null |
63c647d05f004bf4898cf6f3 | 2023-01-27T21:57:16.276Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
// TODO: Implement a helper named check_permissions that matches the prototype below.
int check_permissions(char *, char *);
int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
// TODO: Process command line arguments.
int cutoff = strtol(argv[1], NULL, 10);
char type;
char permission[10];
int link;
char owner[32];
char group[32];
int size;
char remainder[50];
int n;
int eof;
eof = scanf("%s %d", remainder, &n);
int count = 0;
while (eof != EOF){
eof = scanf("%c%s %d %s %s %d %[^\n]s\n", &type, permission, &link, owner, group, &size, remainder);
printf("type: %c permission: %s link: %d owner: %s group: %s size: %d remainder: %s", type, permission, link, owner, group, size, remainder);
if ((type == '-') && ((argc == 2) || (check_permissions(permission, argv[2]) == 0)) && (size > cutoff)){
count++;
}
printf(" COUNT %d\n", count);
}
printf("%d\n", count);
return 0;
}
int check_permissions(char* p1, char* p2){
for (int i = 0; i < 9; i++){
if((p2[i] != '-') && (p1[i] == '-')) return 1;
}
return 0;
}
| parse each line read from the standard input. The first character from each line is not being read correctly. | null |
63c647d05f004bf4898cf6f3 | 2023-01-27T21:58:48.693Z | General Question | v1 | how can I read from standard input, line by line (separated by newline)? | null | null | null |
63c647d05f004bf4898cf6f3 | 2023-01-28T00:50:05.030Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
int populate_array(int, int *);
int check_sin(int *);
int main(int argc, char **argv) {
// TODO: Verify that command line arguments are valid.
if (argc != 2) {
return 2;
}
int sin = strtol(argv[1], NULL, 10);
int sin_array[10];
// TODO: Parse arguments and then call the two helpers in sin_helpers.c
// to verify the SIN given as a command line argument.
int valid = populate_array(sin, sin_array);
if (valid != 0){
return 1;
}
return check_sin(sin_array);
}
int populate_array(int sin, int *sin_array) {
for (int i = 8; i >= 0; i--){
if (sin == 0) {
return 1; // less than 9 digits
}
sin_array[i] = sin % 10;
sin /= 10;
}
if (sin != 0){
return 1; // more than 9 digits
}
return 0;
}
// TODO: Implement check_sin
/*
* Return 0 if the given sin_array is a valid SIN, and 1 otherwise.
*/
int check_sin(int *sin_array) {
int sum = 0;
for (int i = 0; i < 9; i += 2){
sum += sin_array[i];
}
for (int i = 1; i < 9; i += 2){
int doubled = sin_array[i] * 2;
sum += doubled % 10 + doubled / 10;
}
if (sum % 10 == 0){
return 0;
}
else {
return 1;
}
}
| check if the SIN given by command line argument is valid, using the Luhn algorithm | null |
63c647d65f004bf4898cf720 | 2023-01-26T20:44:46.458Z | Help Fix Code | v1 | null | #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 *array = malloc(sizeof(int) * length);
int i1 = 0;
int i2 = 0;
for (int i = 0; i < length; i++) {
if (i1 != i2) {
array[1][i2] = s[i];
i2++;
}
else {
array[0][i1] = s[i];
i2++;
}
}
return array;
}
| split an array from s | null |
63c647d65f004bf4898cf720 | 2023-01-26T20:51:28.556Z | General Question | v1 | i keep getting the error subscripted value is neither array nor pointer nor vector | null | null | null |
63c647d65f004bf4898cf720 | 2023-01-26T21:06:33.713Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
/* Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices.
Do not allocate any more memory than necessary. You are not permitted
to include math.h. You can do the math with modulo arithmetic and integer
division.
*/
int **split_array(const int *s, int length) {
int **array = malloc(sizeof(int) * length);
array[0][0] = s[0];
return array;
}
| split the array s into two arrays of index 0 and 1 in array | null |
63c647d75f004bf4898cf72a | 2023-01-18T15:18:48.951Z | General Question | v1 | what does this mean
c_helper.py:148: in _t
self.assertEqual(stdout, expected_stdout)
E AssertionError: '16416123456733' != '1\n6\n4161234567\n3'
E - 16416123456733
E ? ^
E + 1
E 6
E 4161234567
E 3
E ? + + ^ | null | null | null |
63c647d75f004bf4898cf72a | 2023-01-18T15:20:12.081Z | Question from Code | v1 | how do I fix this error
c_helper.py:148: in _t
self.assertEqual(stdout, expected_stdout)
E AssertionError: '16416123456733' != '1\n6\n4161234567\n3'
E - 16416123456733
E ? ^
E + 1
E 6
E 4161234567
E 3
E ? + + ^ | #include <stdio.h>
int main() {
char phone[11];
scanf("%s", phone);
int val = 1;
int num;
while (val == 1) {
val = scanf("%d", &num);
if (num == -1) {
printf("%s", phone);
} else if (num >= 0 && num <= 9) {
char arg = phone[num];
printf("%c", arg);
} else {
printf("ERROR");
return 1;
}
}
return 0;
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-18T15:22:14.677Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
char phone[11];
scanf("%s", phone);
int val = 1;
int num;
while (val == 1) {
val = scanf("%d", &num);
if (num == -1) {
printf("%s", phone);
} else if (num >= 0 && num <= 9) {
char arg = phone[num];
printf("%c", arg);
} else {
printf("ERROR");
return 1;
}
}
return 0;
} | why does this code raise this error
c_helper.py:148: in _t
self.assertEqual(stdout, expected_stdout)
E AssertionError: '16416123456733' != '1\n6\n4161234567\n3'
E - 16416123456733
E ? ^
E + 1
E 6
E 4161234567
E 3
E ? + + ^ | null |
63c647d75f004bf4898cf72a | 2023-01-18T15:27:04.894Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
char phone[11];
scanf("%s", phone);
int val = 1;
int num;
while (val == 1) {
val = scanf("%d", &num);
if (num == -1) {
printf("%s", phone);
} else if (num >= 0 && num <= 9) {
char arg = phone[num];
printf("%c", arg);
} else {
printf("ERROR");
return 1;
}
}
return 0;
} | c_helper.py:148: in _t
self.assertEqual(stdout, expected_stdout)
E AssertionError: '1 \n6 \n41612345673 \n3' != '1\n6\n4161234567\n3'
E - 1
E ? -
E + 1
E - 6
E ? -
E + 6
E - 41612345673
E ? --
E + 4161234567
E 3 | null |
63c647d75f004bf4898cf72a | 2023-01-18T15:28:40.594Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
char phone[11];
scanf("%s", phone);
int val = 1;
int num;
while (val == 1) {
val = scanf("%d", &num);
if (num == -1) {
printf("%s", phone);
} else if (num >= 0 && num <= 9) {
char arg = phone[num];
printf("%c \n", arg);
} else {
printf("ERROR");
return 1;
}
}
return 0;
} | c_helper.py:148: in _t
self.assertEqual(stdout, expected_stdout)
E AssertionError: '1 \n6 \n41612345673 \n3' != '1\n6\n4161234567\n3'
E - 1
E ? -
E + 1
E - 6
E ? -
E + 6
E - 41612345673
E ? --
E + 4161234567
E 3 | null |
63c647d75f004bf4898cf72a | 2023-01-18T15:32:15.943Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
char phone[11];
scanf("%s", phone);
int val = 1;
int num;
while (val == 1) {
val = scanf("%d", &num);
if (num == -1) {
printf("%s\n", phone);
} else if (num >= 0 && num <= 9) {
char arg = phone[num];
printf("%c\n", arg);
} else {
printf("ERROR");
return 1;
}
}
return 0;
} | c_helper.py:148: in _t
self.assertEqual(stdout, expected_stdout)
E AssertionError: '1\n6\n4161234567\n3\n3' != '1\n6\n4161234567\n3'
E 1
E 6
E 4161234567
E - 3
E 3 | null |
63c647d75f004bf4898cf72a | 2023-01-18T15:33:46.938Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
char phone[11];
scanf("%s", phone);
int val = 1;
int num;
while (val == 1) {
val = scanf("%d", &num);
if (num == -1) {
printf("%s\n", phone);
} else if (num >= 0 && num <= 9) {
char arg = phone[num];
printf("%c\n", arg);
} else {
printf("ERROR");
return 1;
}
}
return 0;
} | Your task is to write a C program called phone_loop.c. This program will again read from standard input using scanf and take no command-line arguments. Similar to phone.c, this program reads a 10-character string as the first input value but then it repeatedly reads integers until standard input is closed. (Hint: Use a while loop that checks the return value of scanf to accomplish this. Check the man page for scanf to determine the return value when the input stream is closed.)
After each integer the output produced is as before:
if the integer is -1, the full string is printed
if the integer is between 0 and 9, the individual character at that position is printed
if the integer is less than -1 or greater than 9, the message "ERROR" is printed (to stdout)
In each case the printing is followed by a newline character.
When the program finishes running, main returns with a 0 return code if there were no errors and with a 1 return code otherwise. | null |
63c647d75f004bf4898cf72a | 2023-01-18T15:38:27.299Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
char phone[11];
scanf("%s", phone);
int val = 1;
int num;
while (val == 1) {
val = scanf("%d", &num);
if (num == -1) {
printf("%s\n", phone);
} else if (num >= 0 && num <= 9) {
char arg = phone[num];
printf("%c\n", arg);
} else {
printf("ERROR");
return 1;
}
}
return 0;
} | I have this error:
c_helper.py:148: in _t
self.assertEqual(stdout, expected_stdout)
E AssertionError: '1\n6\n4161234567\n3\n3' != '1\n6\n4161234567\n3'
I want my code to do this.
Your task is to write a C program called phone_loop.c. This program will again read from standard input using scanf and take no command-line arguments. Similar to phone.c, this program reads a 10-character string as the first input value but then it repeatedly reads integers until standard input is closed. (Hint: Use a while loop that checks the return value of scanf to accomplish this. Check the man page for scanf to determine the return value when the input stream is closed.)
After each integer the output produced is as before:
if the integer is -1, the full string is printed
if the integer is between 0 and 9, the individual character at that position is printed
if the integer is less than -1 or greater than 9, the message "ERROR" is printed (to stdout)
In each case the printing is followed by a newline character.
When the program finishes running, main returns with a 0 return code if there were no errors and with a 1 return code otherwise.
Note: the program must not print anything other than then the values specified. | null |
63c647d75f004bf4898cf72a | 2023-01-18T15:40:05.188Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
char phone[11];
scanf("%s", phone);
int val = 1;
int num;
while (val == 1) {
val = scanf("%d", &num);
if (num == -1) {
printf("%s\n", phone);
} else if (num >= 0 && num <= 9) {
char arg = phone[num];
printf("%c\n", arg);
} else {
printf("ERROR");
return 1;
}
}
return 0;
} | I have this error: c_helper.py:148: in _t self.assertEqual(stdout, expected_stdout) E AssertionError: '1\n6\n4161234567\n3\n3' != '1\n6\n4161234567\n3' I want my code to do this. Your task is to write a C program called phone_loop.c. This program will again read from standard input using scanf and take no command-line arguments. Similar to phone.c, this program reads a 10-character string as the first input value but then it repeatedly reads integers until standard input is closed. (Hint: Use a while loop that checks the return value of scanf to accomplish this. Check the man page for scanf to determine the return value when the input stream is closed.) After each integer the output produced is as before: if the integer is -1, the full string is printed if the integer is between 0 and 9, the individual character at that position is printed if the integer is less than -1 or greater than 9, the message "ERROR" is printed (to stdout) In each case the printing is followed by a newline character. When the program finishes running, main returns with a 0 return code if there were no errors and with a 1 return code otherwise. Note: the program must not print anything other than then the values specified.
I do have a return 0 | null |
63c647d75f004bf4898cf72a | 2023-01-18T15:42:05.506Z | General Question | v1 | in C how can I check if standard input is closed using scanf | null | null | null |
63c647d75f004bf4898cf72a | 2023-01-18T15:49:05.292Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
char phone[11];
scanf("%s", phone);
int val = 1;
int num;
while (val == 1) {
val = scanf("%d", &num);
if (num == -1) {
printf("%s\n", phone);
} else if (num >= 0 && num <= 9) {
char arg = phone[num];
printf("%c\n", arg);
} else {
printf("ERROR");
return 1;
}
}
return 0;
} | why does my code raise this error?
c_helper.py:148: in _t
self.assertEqual(stdout, expected_stdout)
E AssertionError: '1\n6\n4161234567\n3\n3' != '1\n6\n4161234567\n3'
E 1
E 6
E 4161234567
E - 3
E 3 | null |
63c647d75f004bf4898cf72a | 2023-01-24T18:17:02.449Z | General Question | v1 | how can scanf read certain lines | null | null | null |
63c647d75f004bf4898cf72a | 2023-01-24T18:18:02.362Z | General Question | v1 | how to use scanf to read permissions and file size from redirected input | null | null | null |
63c647d75f004bf4898cf72a | 2023-01-25T01:44:10.583Z | Question from Code | v1 | why does this function give me a Segmentation fault | void fib(int **heap_loc, int n) {
*heap_loc = malloc(sizeof(int) * n);
for (int i = 2; i <= n; n++) {
if (i == 0) {
*heap_loc[i] = 0;
} else if (i == 1) {
*heap_loc[i] = 1;
} else {
*heap_loc[i] = *heap_loc[i - 1] + *heap_loc[i - 2];
}
}
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-25T01:49:41.277Z | General Question | v1 | what is this error
malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed. | null | null | null |
63c647d75f004bf4898cf72a | 2023-01-25T01:50:56.566Z | Question from Code | v1 | how to fix this error malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed. | void fib(int **heap_loc, int n) {
*heap_loc = malloc(sizeof(int) * n);
for (int i = 0; i <= n; i++) {
if (i == 0) {
(*heap_loc)[i] = 0;
} else if (i == 1) {
(*heap_loc)[i] = 1;
} else {
(*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2];
}
}
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-25T01:52:42.864Z | Question from Code | v1 | fix this error
malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed. | void fib(int **heap_loc, int n) {
*heap_loc = malloc(sizeof(int) * n);
for (int i = 0; i < n + 1; i++) {
if (i == 0) {
(*heap_loc)[i] = 0;
} else if (i == 1) {
(*heap_loc)[i] = 1;
} else {
(*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2];
}
}
}
int main(int argc, char **argv) {
/* do not change this main function */
int count = strtol(argv[1], NULL, 10);
int *fib_sequence;
fib(&fib_sequence, count);
for (int i = 0; i < count; i++) {
printf("%d ", fib_sequence[i]);
}
free(fib_sequence);
return 0;
}
| null | null |
63c647d75f004bf4898cf72a | 2023-01-25T03:38:14.327Z | Question from Code | v1 | how to fix this error malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed. | void fib(int **heap_loc, int n) {
*heap_loc = malloc(sizeof(int) * n);
for (int i = 0; i <= n; i++) {
if (i == 0) {
(*heap_loc)[i] = 0;
} else if (i == 1) {
(*heap_loc)[i] = 1;
} else {
(*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2];
}
}
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-25T03:38:58.776Z | Question from Code | v1 | error malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
why does changing loop condition <= to < fix the problem? | void fib(int **heap_loc, int n) {
*heap_loc = malloc(sizeof(int) * n);
for (int i = 0; i <= n; i++) {
if (i == 0) {
(*heap_loc)[i] = 0;
} else if (i == 1) {
(*heap_loc)[i] = 1;
} else {
(*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2];
}
}
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-25T03:39:40.523Z | Question from Code | v1 | why would an incorrect loop condition result in malloc error | void fib(int **heap_loc, int n) {
*heap_loc = malloc(sizeof(int) * n);
for (int i = 0; i <= n; i++) {
if (i == 0) {
(*heap_loc)[i] = 0;
} else if (i == 1) {
(*heap_loc)[i] = 1;
} else {
(*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2];
}
}
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-25T03:40:20.223Z | Question from Code | v1 | why would an incorrect loop condition result in malloc error, but only for certain n values (such as n = 10, n = 14) | void fib(int **heap_loc, int n) {
*heap_loc = malloc(sizeof(int) * n);
for (int i = 0; i <= n; i++) {
if (i == 0) {
(*heap_loc)[i] = 0;
} else if (i == 1) {
(*heap_loc)[i] = 1;
} else {
(*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2];
}
}
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-27T00:49:08.439Z | Question from Code | v1 | why does this result in an 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 **arr_indexes = malloc(2 * sizeof(int));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
}
return arr_indexes;
}
/* 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 **ints = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ints[i] = (int *) strtol(strs[i], NULL, 10);
}
return *ints;
}
int main(int argc, char **argv) {
/* Replace the comments in the next two lines with the appropriate
arguments. Do not add any additional lines of code to the main
function or make other changes.
*/
int *full_array = build_array(argv, sizeof(argv) / sizeof(char));
int **result = split_array(full_array, sizeof(argv) / sizeof(char));
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 |
63c647d75f004bf4898cf72a | 2023-01-27T00:49:59.031Z | Question from Code | v1 | where would an segmentation fault arise in this code? | #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 **arr_indexes = malloc(2 * sizeof(int));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
}
return arr_indexes;
}
/* 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 **ints = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ints[i] = (int *) strtol(strs[i], NULL, 10);
}
return *ints;
}
int main(int argc, char **argv) {
/* Replace the comments in the next two lines with the appropriate
arguments. Do not add any additional lines of code to the main
function or make other changes.
*/
int *full_array = build_array(argv, sizeof(argv) / sizeof(char));
int **result = split_array(full_array, sizeof(argv) / sizeof(char));
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 |
63c647d75f004bf4898cf72a | 2023-01-27T00:50:24.677Z | Question from Code | v1 | which line causes an 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 **arr_indexes = malloc(2 * sizeof(int));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
}
return arr_indexes;
}
/* 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 **ints = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ints[i] = (int *) strtol(strs[i], NULL, 10);
}
return *ints;
}
int main(int argc, char **argv) {
/* Replace the comments in the next two lines with the appropriate
arguments. Do not add any additional lines of code to the main
function or make other changes.
*/
int *full_array = build_array(argv, sizeof(argv) / sizeof(char));
int **result = split_array(full_array, sizeof(argv) / sizeof(char));
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 |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.