0

I'm a bit stuck on how to make a user defined function that would printout the output. I also have to make a user defined function that will add up the data in each node and print out the total but it's not adding up correctly and the format is a little off as well.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char printout();
int sum();
typedef struct node
{
    int number;
    struct node*next;
} node;

char printout()
{

};
int sum()
{
    int s,sum_all=0, node_sum=0;
    for(s=0;s=100;s++)
    {
        sum_all=node_sum+s;
        return printf("The sum of all nodes is %d.\n",sum_all);
    };

};
int main()
{
    srand (time(NULL));
    int i, total=0;
    struct node*head=malloc(sizeof(struct node));
    head->number = rand()%100;
    printf("Node #%d contains %d.\n", 0, head->number);

    struct node*here=head;

    for (i=1; i<100; i++)
    {
        here->next=malloc(sizeof(struct node));
        here->number=rand()%100;
        printf("Node #%d contains %d.\n", i, here->number);
    };
    total=sum(here->number);
    printf("%2.2d", total);
    return 0;
}
4
  • 1
    I don't see how the insertion can work:( You overwrite 'here->next' every time round the loop without storing it anywhere, ie. leak it. The thing about linked-lists is that the elements have to be actually linked:) Commented Apr 22, 2017 at 20:43
  • What is the point of the for loop in the function sum when on the first iteration you do a return! Commented Apr 22, 2017 at 21:16
  • Why is return type of function printout char? Commented Apr 22, 2017 at 21:38
  • ideone.com/6XQMro Commented Apr 22, 2017 at 23:03

1 Answer 1

1

There is the litany of errors here, but let's just focus on the most important meat:

You should be pass the list's head to the function sum(), ie

sum(head); // This is how you call most linked list functions.

by which you should change the header to

int sum(struct node *head)
{ ... }

This is not an array. You should traverse the linked list correctly.

I can't show all the code for you, as this is what your professor wants you to learn.

But you should be using these

for( struct node*p = head; p!=NULL; p=p->next)

instead of these

for( s=0; s<=100; s++)

You also forgot to step forward in your malloc-and-fill-with-rand loop

here = here->next; // this does in linked lists what i++ does in arrays

and this

sum_all += p->number; // p->number is analogous to array[i]

instead of

sum_all = node_sum +s; // what are s and node_sum anyway?

Also, if you insist that sum return something, It should return, well, the sum;

return sum_all;

And don't print it inside the function

printf("The sum of all nodes is %d.\n",sum_all); // please don't

Because you're already printing it outside.

total = sum(head);
printf("%2.2d", total);

Please try to think first what your code is going to accomplish instead of putting code blankly. It will help you a lot. Good luck!

Sign up to request clarification or add additional context in comments.

3 Comments

I didn't even notice any of that. Thanks for the tips as well. I'm really new at programming and this is my first semester of it as well I'm sorry you had to teach me.
Reading back my answer, I apologize if my words are a bit, asshole-ish. I know many people who suffered through pointers is CS1, and linked lists unluckily reawakened that nightmare :( From my experience, they learned faster by making an analogy of each operation to an array. I hope you learn from all these :)
It's no problem I really appreciate all the help I can get.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.