Search code examples
ccountlinked-listmedian

C program doesn't print out the values


I have a program that is looking for a total numbers and what is the middle number in a linked list. The question that I have is why it doesn't print out the values?

Here is the code:

int count(list values){
    if(values == NULL)
        return 0;
    else
        return 1 + count(values->next);
}

void middle(struct node *head){
    int count = 0;
    struct node *mid = head;

    while (head != NULL){
        if(count & 1)
            mid = mid->next;

        count++;
        head = head->next;
    }
}

void traverse(list values){
     if(values->next)
     printf("\n# of the values: %.1f% \nMiddle: %.1f%\n", count, middle);
}

int main(int argc, char *argv[]){
    FILE *input = stdin;

    list values = readNumbers(input);
    traverse(values);
    return 0;
}

Solution

  • It's hard to know where to start here. I can't really tell what you are trying to do.

    But let's look at this line:

    printf("\n# of the values: %.1f% \nMiddle: %.1f%\n", count, middle);
    

    count and middle are functions, but you are not calling those functions here. You are simply passing the address of those functions to printf(), which has no knowledge that these are functions. You need to include parentheses after the function names (count(args) and middle(args) in order to call those functions.