Thread: Bugs in Program Question

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    11

    Bugs in Program Question

    Hey guys,
    I was studying for my test and I came across a question that left me baffled. I am supposed to find and fix the bugs in the code below. I was able to find 2 bugs and I was hoping one of you could help me find the rest, if there are any more.

    The following program creates a linked list of nodes with random numbers between 1-100 as the data values. It then traverses and outputs the data values 8 per line. At the end of the program the for loop is supposed to delete all the nodes and release the dynamic memory space that they occupy.

    Here is the code(with err0rs):

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main ()
    {
    struct node /* a linked list node type */
    { int data;
    struct node *next;
    };
    struct node *first = NULL, *temp;
    int i, n, k, count;
    k = rand( ) % 1000 + 1;
    /* create the linked list of k nodes with random values between 1-100 */
    for (i = 1, i <= k; i++);
    {
    n = rand( ) % 100 + 1;
    temp = malloc(sizeof(struct node));
    temp -> data = n;
    temp -> next = first;
    first = temp;
    }
    /* traverse the linked list and output the node data values, 8 per line */
    count = 0;
    for (temp = first; temp != NULL; temp = temp -> next)
    { count++;
    printf ( “%d ”, temp->data) ;
    if (count = 8) {printf (“\n”); count = 1;}
    }
    /* traverse and release the memory for the nodes of the linked list */
    for (temp = first; temp != NULL; temp = temp -> next)
    {
    free (temp);
    }
    return 0;
    }
    the errors I have found so far are that the if(count=8) should actually be if(count==8) and in the same line count=1 should be count=0 since count should start at 0, not 1.
    Could you please help me find the rest of the bugs?
    Please hurry, my test is tommorow morning!!
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Help everyone by indenting it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    and no, we're not going to do your homework for you.
    You tell us what you think is wrong, and someone might tell you whether you're right or not.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I'm feeling gratuitous tonight. I've indented your code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node /* a linked list node type */
    { 
    	int data;
    	struct node *next;
    };
    
    int main ()
    {
    	
    	struct node *first = NULL, *temp;
    	
    	int i, n, k, count;
    	
    	k = rand( ) &#37; 1000 + 1;
    	
    	/* create the linked list of k nodes with random values between 1-100 */
    	for (i = 1, i <= k; i++);
    	{
    		n = rand( ) % 100 + 1;
    		temp = malloc(sizeof(struct node));
    		temp -> data = n;
    		temp -> next = first;
    		first = temp;
    	}
    	
    	/* traverse the linked list and output the node data values, 8 per line */
    	count = 0;
    	for (temp = first; temp != NULL; temp = temp -> next)
    	{ 
    		count++;
    		printf ( “%d ”, temp->data) ;
    		if (count = 8) {printf (“\n”); count = 1;}
    	}
    	
    	/* traverse and release the memory for the nodes of the linked list */
    	for (temp = first; temp != NULL; temp = temp -> next)
    	{
    		free (temp);
    	}
    	return 0;
    }
    Last edited by Dino; 12-05-2008 at 12:45 AM.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What if the missing indentation was a bug, though!?!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    he'd still fail his assignment as there no doubt is a lot more wrong with his code that he's too lazy to and/or incapable of figuring out for himself and is therefore waiting for us to spoonfeed him.
    And as it's already "tomorrow" he's now late to turn in his homework

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, no, no. The question was posted today. The test is... err, tomorrow... saturday?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I can find two more bugs at first glance (4 total). Maybe I'm missing some, though.
    But keep looking . There are more.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... racerday182, is this supposed be to practice for a C++ test or for a C test?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    "Spot the error"
    1. Not checking for NULL after malloc.
    2. Using temp after free.
    3. Using "fancy" quotes - was the code typed into "word" or copied from a web-site somewhere.
    4. For loop incorrect syntax.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    thank you for doing yet another lazy kiddo's homework for him, potentially creating yet another clueless, lazy, colleague for someone, somewhere.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Luckily, he missed one :P. Well, I think so at least .

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by Elysia View Post
    What if the missing indentation was a bug, though!?!
    Well, the only thing I did was move the struct outside of main. Other than that, I just re-indented with my IDE.
    Mainframe assembler programmer by trade. C coder when I can.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, well, my point was that what if the missing indentation was one of the bugs the OP was supposed to fix!?! In that case, you solved that "problem" for the OP
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I see two more, over what matsp has posted.
    3, if you count the other for loop problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-lvalue in Program Question
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2008, 03:52 PM
  2. Question regarding a this C program
    By cnb in forum C Programming
    Replies: 10
    Last Post: 10-11-2008, 04:43 AM
  3. Password Program Question
    By SirTalkAlot415 in forum C++ Programming
    Replies: 13
    Last Post: 11-06-2007, 12:35 PM
  4. perl program question
    By newbie2c in forum Tech Board
    Replies: 2
    Last Post: 02-03-2003, 10:19 AM
  5. Simple Question: How do yo uend a C program?
    By S. Omnipotence in forum C Programming
    Replies: 7
    Last Post: 01-16-2002, 07:29 AM

Tags for this Thread