Pages

Friday, March 22, 2013

Jellybean 1.0

A few years back, I was at the grocery store doing some shopping and after I checked out my groceries one of the employees asked me to participate in a jellybean contest where you have to guess how many jellybeans in a jar. So I said sure thing however, I took a different approach.

I decided not to guess how many jellybeans were in the jar, that would be a waste of time. So my friend and I took out a tape measure, a pad and pen and took down the information that we did know to calculate the estimate using basic geometry. A few weeks later I got a call from the store telling me I won, that I was off by sixteen jellybeans.

So, here is what I did:
  1. Measure a single jellybean lengthwise; use that as your base of measurement (i.e. centimeter or inch)
  2. Measure the diameter of the jar’s base
  3. Measure the jar’s height
  4. Calculate the radius by dividing the diameter in half
  5. Calculate the area and multiply that by the height of the jar to get the capacity
  6. Factor in the jellybean’s measurement with the capacity of the jar
This is under the assumption that the jar is cylinder shaped, if it were shaped like a rectangular prism the whole thing would be much easier, just take the width times the depth times the height of the jar. If by any chance the jar is oval shaped, then you can not use the base to get the diameter. You’ll have to get it at the jar’s widest point, calculate the estimate based on its circumference, then make a substantial subtraction. So this program can be modified so that the next time you’re ever given to opportunity to win a jellybean contest, just take the necessary measurements and plug in the numbers into this program and it will estimate for you.

#include "stdafx.h" 
int _tmain(int argc, _TCHAR* argv[])
{
   double pi = 3.1415927;
   float radius, height, bean;
   int beans; 

   printf("What is the length of the bean? ");
   scanf_s("%u",&bean);
   printf("What is the radius of the jar’s base? ");
   scanf_s("%u",&radius);
   printf("What is the height of the jar? ");
   scanf_s("%u",&height); 

   beans = int(((pi * (radius * radius)) * height) * bean); 

   printf("%s","\n\n\nThe estimated number of jellybeans is: ");
   printf("$i",beans);

   getchar();
}

In case you’re wondering, the grand prize was a bag of jellybeans.

No comments:

Post a Comment