Thread: Help finding bugs

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    2

    Help finding bugs

    Hello, I'm completely new to programming and I'm really struggling to find the bugs in the following pieces of code. Please help me out.

    Code 1:
    Code:
    /* BUG ZONE!!!Example: integer arithmetic */
    
    #include <stdio.h>
    
    main()
    {
      int r1 = 22000;
      int r2 = 10000;
      int r3 = 15000;
      int r;
      
      puts("Three resistors in parallel");
      printf("%i || %i || %i\n\n", r1, r2, r3);
      
      puts("Method 1: r = (r1 * r2 * r3)/(r1*r2 + r2*r3 + r3*r1)\n");
      r = (r1 * r2 * r3)/(r1*r2 + r2*r3 + r3*r1);  /* BUG */
      printf("Total resistance = %i\n\n", r);
      
      puts("Method 2: r = 1/(1/r1 + 1/r2 + 1/r3)\n");
      r = 1/(1/r1 + 1/r2 + 1/r3);  /* BUG */
      printf("Total resistance = %i\n", r);
    }


    Code 2:
    Code:
    /* BUG ZONE!!!
    Example: integer arithmetic */
    
    #include <stdio.h>
    
    main()
    {
      int StudentsInClass = 116;
      int MaxGroupSize = 5;
      int NumberOfGroups;
      int passes;
      int GirlsWithBrownEyes;
      
      NumberOfGroups = StudentsInClass/MaxGroupSize; /* BUG */
      
      printf("%i students at %i max. per group means %i groups\n\n", 
        StudentsInClass, MaxGroupSize, NumberOfGroups);
        
      /* Three-quarters of the class pass the exam: */
      
      passes = 3/4 * StudentsInClass;  /* BUG */
      printf("%i students passed the exam!\n\n", passes);
      
      /* Half the students are female, and 
      half of these have brown eyes */
      
      GirlsWithBrownEyes = StudentsInClass / 2*2;  /* BUG */
      
      printf("There are %i brown-eyed girls in the class\n",
        GirlsWithBrownEyes);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,603
    Consider things like
    - integer overflow
    - division by zero
    - integer division truncates (no fractions).
    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
    Registered User
    Join Date
    Feb 2017
    Posts
    2
    Thanks for replying. So do I just need to replace the ints with floats?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Have you tried doing that and seeing the results? It takes literally seconds to compile and run a program of this size, so doing some experimentation would help you test your ideas.

    Code:
    3/4
    This is an example of what Salem was talking about. 3 and 4 are both integers, and integer division yields an integer result (i.e. any fractional part is discarded).

    Try this instead:

    Code:
    3.0 / 4.0
    This won't solve all your problems, but should be a good nudge in the right direction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding nasty bugs
    By Jodes in forum C Programming
    Replies: 4
    Last Post: 03-06-2014, 08:15 AM
  2. Bugs!
    By LeighRogers in forum C++ Programming
    Replies: 7
    Last Post: 05-13-2007, 07:33 AM
  3. Need help finding bugs
    By Shakti in forum Game Programming
    Replies: 16
    Last Post: 02-13-2005, 01:42 AM
  4. someone who is good at finding and fixing bugs?
    By elfjuice in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2002, 03:59 PM

Tags for this Thread