Thread: find the maximum width & length of a box using for loops?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    Question find the maximum width & length of a box using for loops?

    There four sides of a wall(a square), the cost of building this wall:

    1 side(length) $1200 per meter
    2 side(width) $2500 per meter
    1 side(length) $2500 per meter
    the max of length & width are both 100

    you have limited budget $20000 to best use to build the wall, the accuracy of length and width should be down to 0.1 meter, use two for loops to find out the best combination of length & width.

    here is my code (wrong)

    Code:
    #include <stdio.h>
    
    struct dimension {
      float length; /* river side length */
      float width;
    };
    
    void maxSize(float amount, struct dimension* dim) {
    
      /* Enter your code here */
      float i, j;
      float max_area = 0;
      if ((2500 * 300 + 1200 * 100 ) < amount) {
         dim->length = 100;
         dim->width = 100;
         return;
      }
      //length = i //width = j
      for (i=100; i>=0.1; i-=0.1)
        for (j=100; j>=0.1; j-=0.1)
          area = i*j;
          cost = i*1200 + (i+j*2) * 2500;
          if( cost < amount && area < max_area)
          break;
    }
    
    int main() {
      struct dimension dim;
      maxSize(20000, &dim);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You shouldn't break on whatever the first match is. You should instead store that, and compare your next loop pass to that. That way you can get whatever beats the current value, but still meets the requirement (over and over).

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  2. the open box problem
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-28-2003, 05:53 AM
  3. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 08:50 PM
  4. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM

Tags for this Thread