Question
TASK: You will modify the code you wrote in inclass12 teamwork assignment (I provided myown code under week#12, you can use it if your code
TASK: You will modify the code you wrote in inclass12 teamwork assignment (I provided myown code under week#12, you can use it if your code is not working 100%) such that
the file name for the matrix is fed as an argument to the program itself
o implement the necessary error checks for nonexisting file, etc and use theappropriate error messaging mechanism or assertions.
add delete_node function
o given a row and a column pair, it finds and deletes the node from the linked list
o implement the necessary error checks for nonexisting node or out-of-rangerow/column values
add update_node function
o given a row and a column pair, it finds and updates the node in the linked list
o implement the necessary error checks for nonexisting node or out-of-rangerow/column values
add print_linked_list function that
o prints the whole linked list on the screen
o prints it on a file provided an output file name
o in my code, theres code for printing the linked list on the screen, refactor it intoa function and add the necessary additional functionality.
o implement the necessary error checks for empty file name, etc
test and show that your implemented functions work
o make the necessary calls and print the updated linked list after each call on thescreen and finally, print it into the output file
C code used in the 12th week mentioned in the task:
#include
#include
struct Node* create_node(int col, int val); void print(struct Node* id); void tear_down(struct Node** row_ptr, int size);
struct Node { int col; float val; struct Node* next; };
int main() { FILE* f_ptr = fopen("matrix.txt", "r"); if(f_ptr != NULL) { int num_rows, num_cols;
fscanf(f_ptr, "%d %d ", &num_rows, &num_cols); struct Node** row_ptr = (struct Node**)calloc(num_rows, sizeof(struct Node*));
int row, col, val; while(!feof(f_ptr)) { fscanf(f_ptr, "%d %d %d ", &row, &col, &val);
struct Node* ptr = create_node(col, val); if(row_ptr[row] == NULL) row_ptr[row] = ptr; else { struct Node* track = row_ptr[row]; while(track->next != NULL) track = track->next; track->next = ptr; } }
for(int i = 0; i < num_rows; i++) { struct Node* track = row_ptr[i]; printf("row: %d ", i); while(track != NULL) { print(track); track = track->next; } printf(" "); }
tear_down(row_ptr, num_rows); } else printf("couldn't open the file "); return 0;
}
struct Node* create_node(int col, int val) { struct Node* ptr = (struct Node*)malloc(sizeof(struct Node));
ptr->col = col; ptr->val = val; ptr->next = NULL;
return ptr; }
void print(struct Node* id) { printf("col: %d, val:%.1f ", id->col, id->val);
}
void tear_down(struct Node** row_ptr, int size) {
for(int i = 0; i < size; i++) { struct Node* track = row_ptr[i]; while(track != NULL) { struct Node* track_prev = track; track = track->next; free(track_prev); } row_ptr[i] = NULL; }
free(row_ptr); row_ptr = NULL; }
Code the tasks in the task with C language THANK YOUU!!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started