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

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Unhappy Deleting stuff in C


    In this one there is a scenario whereby 20 children are in a room. every fourth one is deleted until. there are none left. Im getting 1 1 1 1 1 1 1 1 as the output. I can't figure out what is wrong with my code at the moment. Help would be greatly appreciated

    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    int i, n;
    struct mode
    {
    int num;
    struct mode * next;
    };
    struct mode *header, *last, *temp, *present;
    header = NULL;
    printf("Enter the amount of people \n");
    scanf("%d", &n);
    for (i=1; i<n; i++)
    {
    temp = malloc(sizeof(struct mode));
    temp -> num = 1;
    temp -> next = NULL;
    if (i == 1)
    header = temp;
    else
    last-> next = temp;
    last = temp;
    }
    present = header;
    while (present != NULL)
    {
    printf("%6d", present->num);
    temp = present;
    present = present ->next;
    free(temp);
    }
    return 0;
    }
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    As has already been said yesterday, it is critical that you indent your code suitably, and use [code] or [highlight] tags around your code samples. The forum software does not retain indentation by default, and in order for the code to be readable, you need to use the [code] tags. You can do this manually, or by using the '#' button at the top of the editing window and pasting your code into the edit box that comes up.

    C Code:
    #include<stdio.h>
    #include<stdlib.h>
     
    int main()
    {
        int i, n;
        struct mode
        {
            int num; 
            struct mode * next;
        };
        struct mode *header, *last,  *temp, *present;
        header = NULL;
     
        printf("Enter the amount of people \n");
        scanf("%d", &n);
        for (i=1; i<n; i++)
        {
            temp = malloc(sizeof(struct mode));
            temp -> num = 1;
            temp -> next = NULL;
            if (i == 1) 
                header = temp;
            else 
                last-> next = temp; 
            last = temp;
        }
        present = header; 
        while (present != NULL)  
        {
            printf("%6d", present->num);
            temp = present;
            present = present ->next;
            free(temp);
        }
        return 0;
    }


    As a minor quibble, I would move the struct definition out of the main() function; having it in main() limits the visibility of the type, meaning you can't extract the code from main() into separate functions - something I would recommend you do as well.

    As for the specific problem you are having, the main reason it is occurring is because you are always assigning temp->num as 1. You presumably meant to set it to i instead.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

IMN logo majestic logo threadwatch logo seochat tools logo