Bugs in Program Question

This is a discussion on Bugs in Program Question within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I was studying for my test and I came across a question that left me baffled. I am ...

  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
    32,824
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Katy, Texas
    Posts
    2,309
    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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    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
    Posts
    23,012
    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
    22,269
    hmm... racerday182, is this supposed be to practice for a C++ test or for a C test?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Katy, Texas
    Posts
    2,309
    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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    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
    32,824
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 05: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21