I'm working on this piece of code as part of my coursework, ideally any advice I get for this would be educational in some way; I don't want an exact answer, just need someone who knows more than I do to point me in the right direction of where this all might be going wrong.
The problem I'm working on is returning the mode of a linked list of floats in C, and my approach so far goes like this:
- Sort the list in ascending order.
- Create an array containing only the unique values in the list.
- Create an array of equal size to the
uniquesarray filled with zeroes to act as a set of counters for each value inuniques. - Run a for-loop through the uniques array, going from
i = 0toi < arraylen. - Inside the
for-loop, traverse the list until thedata == NULLand compare each value in it touniques[i]. - If the value in the list is a match, increment
counters[i]by 1. - Otherwise, move forward in the list without incrementing anything.
- Once the list has been fully traversed, the
for-loop moves onto the next value ofiand the list is reset back to head. - Set an index tracker
k = 0that stores the index of the highest value in the counters array. - Run a
for-loop through counters that compares the data of each value to that of the data in thekindex. - If the current value in counters is higher than the data of the
kindex, setskas the index of the current value. - Otherwise, continues through the list.
- Takes the mode as
uniques[k]and prints that value.
For some reason though, trying to run this code gives me the error message
[C kernel] Executable exited with code -11
and I don't understand why. Below is the full code; if anyone could help me find what's causing this problem I'd greatly appreciate it.
(Note: Bubblesort and Lengthfinder are two functions that I've written and tested, they both work perfectly fine and aren't the cause of the error as far as I can tell.)
(I am also aware that this is probably more complicated than it really needs to be but I'm not sure of how to simplify it.)
#include <stdio.h>
#include <stdlib.h>
struct Node {
float data;
struct Node *next;
};
//Establish lengthfinder function.
int Lengthfinder(struct Node *head) {
int len = 0;
struct Node *travel = head;
while (travel != NULL) {
len++;
travel = travel->next;
}
return len;
}
void Bubblesort(struct Node *head) {
struct Node *current = head;
int len = Lengthfinder(head);
float sort[len];
//creates an array of equal length to the list.
while (current != NULL) {
for (int i = 0; i < len; i++) {
sort[i] = current->data;
current = current->next;
}
//copies the list to the sort array.
}
for (int j = 1; j <= len; j++) {
//Starts a primary for-loop that will read through the values of Input starting from entry 1.
for (int k = 0; k <= j; k++) {
//Starts a secondary for-loop that will read through the values of Input starting from entry 0.
if (sort[j - 1] < sort[k]) {
/*Compares the current value of the secondary loop to the previous value of the primary loop; if
the previous primary loop entry is greater than the current secondary loop entry, starts the if
statement*/
float temp = sort[k];
/*Assigns the data entry corresponding to the current iteration of the secondary loop to a
placeholder*/
sort[k] = sort[j - 1];
sort[j - 1] = temp;
/*Swaps the previous entry of the primary loop and the current entry of the secondary loop
by overwriting the current entry of the secondary loop and then assigning the previous entry of
the primary loop with the temp value*/
} else {
continue;
//Skips the current entry for the secondary loop if it is >= the previous entry of the primary loop
}
}
}
//Nested loops have sorted the array in ascending order.
struct Node *curr = head;
int l = 0;
while (curr != NULL) {
curr->data = sort[l];
curr = curr->next;
l++;
}
}
void Mode(struct Node *head) {
Bubblesort(head);
int len = Lengthfinder(head);
//Creates an array that can be filled with the unique values in the bubble-sorted list.
float uniques[len];
//Creates an integer that acts as the index for the uniques array.
int i = 0;
struct Node *curr = head;
//While loop to traverse the array.
while (curr != NULL) {
//If the current item of the list is the head, adds it to the uniques array.
if (curr == head) {
uniques[i] = curr->data;
//Increases the index position by 1.
i++;
//Sets the list position to the next item.
curr = curr->next;
}
//If the data in the list item following the current is different to the current, adds that to uniques.
else if (curr->data != curr->next->data) {
uniques[i] = curr->next->data;
i++;
curr = curr->next;
}
//If neither of the above conditions are fulfilled, moves the list forward.
else {
curr = curr->next;
}
}
//Creates a parallel array of empty values equal in size to the uniques array.//
int counters[len];
//Fills the counters array with zeroes so that each value of it can be appended.
for (int j = 0; j < len; j++) {
counters[j] = 0;
}
//Traverses the list.
for (int j = 0; j < len; j++) {
struct Node *travel = head;
//Traverses the array and compares the current index of the uniques array with all the values of the list.
while (travel != NULL) {
/*If the data of the current index is equal to the data of any list values, adds 1 to the current index
of the counters array and moves the list forward.*/
if (uniques[j] == travel->data) {
counters[j] = counters[j] + 1;
travel = travel->next;
}
//Otherwise, moves the list forward.
else {
travel = travel->next;
}
}
}
//Sets up an index that keeps track of which counters value is the highest; automatically starts as 0.
int k = 0;
//Traverses the counters array.
for (int j = 1; j <= len; j++) {
//If the current value is greater than the index of k, sets k to the current value of j.
if (counters[j] > counters[k]) {
k = j;
}
}
//Sets the mode as the k index of uniques and returns it.
float mode = uniques[k];
printf("Mode = %f\n", mode);
}
//Constructing a list based on input values.
struct Node *createNode(float x) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = x;
newNode->next = NULL;
return newNode;
}
int main() {
//Creating a hard-coded list that can be edited for whatever values you want to put in.
struct Node *head = createNode(0.1);
head->next = createNode(0.9);
head->next->next = createNode(0.5);
head->next->next->next = createNode(0.7);
head->next->next->next->next = createNode(0.9);
Bubblesort(head);
struct Node *curry = head;
while (curry != NULL) {
printf("%f ", curry->data);
curry = curry->next;
}
Mode(head);
return 0;
}
valgrindinstrumentation. Then run your program that way.valgrind's purpose is to catch incorrect access to memory and tell you specifically what was incorrect. Fix the very first errorvalgrindprints, run the program again, keep doing that untilvalgrindreports no more errors. Then you can move on from incorrect memory access to logic bugs.