#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Link list question


    In the following code, its required for users to enter marks between 0-50 which shuld print Valid link list are.... And if values beyond the range are entered,shud be flagged as invalid and printf invalid link list are.... i couuld'nt figure it out.



    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>

    struct linked list
    {
    int val;
    struct linked list *next;
    };

    typedef struct linked list node;



    void createnode(node *p)
    {
    char c=' ';
    printf("\nEnter student score marks :");
    scanf("%d",&p->val);
    c=getch();
    if (c!='n')
    {
    p->next=(node*)malloc(sizeof(node));
    createnode(p->next);
    }
    else
    {
    p->next=NULL;
    }

    }

    void total(node *p)
    {
    int a=0;
    int n=0;
    int avg=0;
    int max,min;

    max=p->val;
    min=p->val;
    while(p!=NULL)
    {
    if (p->val>max)
    {
    max=p->val;

    }
    else if (p->val<min)
    {
    min=p->val;
    }


    a+=p->val;
    p=p->next;
    n++;

    avg=a/n;

    }



    printf("\nThe total is %d\n",a);
    printf("\nAverage = %d\n",avg);
    printf("\nThe Max Val is %d\n",max);
    printf("\nThe Min Val is %d\n",min);

    }


















    void main()
    {

    node *p;
    p=(node*)malloc(sizeof(node));
    createnode(p);

    if (p->val>=0 ||p->val<=50)

    {
    printf("\nVal : %d = \n",p->val);
    }


    else {
    printf("\nInV= %d\n",p->val);
    }

    total(p);
    getch();

    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    1. Indent your code! There is no excuse for writing unreadable code, especially if you expect others (ie, us) to read it!

    2. Use code tags when posting here to preserve your code's formatting. Failure to do so will result in unreadable code.

    3. You need to tell us the truth, always! You couldn't have run that program, because it won't even compile!

    Here is what I get when I try to compile your program:
    C:TEST>gcc -Wall muax01.c
    muax01.c:6: syntax error before `{'
    muax01.c:8: parse error before `*'
    muax01.c:8: warning: type defaults to `int' in declaration of `next'
    muax01.c:8: warning: data definition has no type or storage class
    muax01.c:9: parse error before `}'
    muax01.c:11: parse error before `node'
    muax01.c:11: warning: type defaults to `int' in declaration of `node'
    muax01.c:11: warning: data definition has no type or storage class
    muax01.c:14: parse error before `*'
    muax01.c: In function `createnode':
    muax01.c:18: `p' undeclared (first use in this function)
    muax01.c:18: (Each undeclared identifier is reported only once
    muax01.c:18: for each function it appears in.)
    muax01.c:22: parse error before `)'
    muax01.c: At top level:
    muax01.c:32: parse error before `*'
    muax01.c: In function `total':
    muax01.c:39: `p' undeclared (first use in this function)
    muax01.c: At top level:
    muax01.c:69: warning: return type of `main' is not `int'
    muax01.c: In function `main':
    muax01.c:71: `p' undeclared (first use in this function)
    muax01.c:71: warning: statement with no effect
    muax01.c:72: parse error before `)'

    C:TEST>
    Correct your code, including all warnings! Then try to actually run it and see what you actually get.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    4. Describe the problem! What's actually happening? What inputs do you give it and what outputs do you get and how do those outputs differ from what you expect? Also, if the program behaves differently than you expect, then tell us! You gave us none of that information!

    Code:
    void createnode(node *p)
    {
        char c=' ';
        printf("\nEnter  student score marks :");
        scanf("%d",&p->val);
        c=getch();
        if (c!='n')
        {
    Does the program wait for you to enter the 'y' or 'n'? Or does it seem to suddenly ask for the next mark? getch is non-standard, but I understand that it behaves like the standard function, getchar. When you read in that number, scanf left the newline character(s) for the Enter key in the input buffer. Then getch reads the next character, which is that newline and which is not 'n', so it does the recursive call. Assuming that you just blindly enter the 'y', then when scanf tries to convert that to a decimal value it will fail and then after that everything is out of sync. Is one of the symptoms that the numbers you think you have entered in don't show up in the linked list, except for the very first one? I have to ask that, I have to play stupid guessing games with you, because you didn't give us any useful information to work with. Three guesses what that does to our attitude about helping you for free.

    To test the behavior of getch there, add a debugging printf statement that will tell you the value of getch:
    Code:
        printf("\nEnter  student score marks :");
        scanf("%d",&p->val);
        c=getch();
        // Echo the character value of c and its ASCII code 
        //   You want the ASCII code in case c is unprintable (eg, a newline)
        printf("c = %c (%d)\n", c, c);
    BTW, this is what you code looks like formatted and with code tags:
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>
    
    struct linked list
    {
        int val;
        struct linked list *next;
    };
    
    typedef struct linked list node;
    
    
    void createnode(node *p)
    {
        char c=' ';
        printf("\nEnter  student score marks :");
        scanf("%d",&p->val);
        c=getch();
        if (c!='n')
        {
            p->next=(node*)malloc(sizeof(node));
            createnode(p->next);
        }
        else
        {
            p->next=NULL;
        }
    
    }
    
    void total(node *p)
    {
        int a=0;
        int n=0;
        int avg=0;
        int max,min;
    
        max=p->val;
        min=p->val;
        while(p!=NULL)
        {
            if (p->val>max)
            {
                max=p->val;
    
            }
            else if (p->val<min)
            {
                min=p->val;
            }
    
    
            a+=p->val;
            p=p->next;
            n++;
    
            avg=a/n;
        }
    
        printf("\nThe total is %d\n",a);
        printf("\nAverage = %d\n",avg);
        printf("\nThe Max Val is %d\n",max);
        printf("\nThe Min Val is %d\n",min);
    }
    
    
    void main()
    {
        
        node *p;
        p=(node*)malloc(sizeof(node));
        createnode(p);
    
        if (p->val>=0 ||p->val<=50)
        {
            printf("\nVal : %d = \n",p->val);
        }
        else 
        {
            printf("\nInV= %d\n",p->val);
        }
    
        total(p);
        getch();
    }
    It is readable, but it still won't compile. Fix that.
    Last edited by dwise1_aol; October 15th, 2013 at 02:45 PM.

IMN logo majestic logo threadwatch logo seochat tools logo