ISourceCode

Make the frequent cases fast and the rare case correct

Use of ceil and floor

This is a Program to convert an Array of Double which has a certain sum to an array of Int with the same sum

We need to convert a double array : {19.75, 27.5, 14.25, 38.5} to Int array with same sum

The problems input has simple rules:
1. The array has even number of number
2. The number of occurrence of a double with 0.5 is always even number of times
3. The sum is always of precision type XX.00000

Well overall this is a very simple problem , nothing too extraordinary.This was a blitz problem which a group of friends formulated instantly and with a solution to be submitted within 10 minutes.

It was no surprise everyone worked around their ceil’s and floor’s knowledge and typecasting knowledge to submit code within 5 minutes.

Anyways this is a code in C which was almost similar to most of them who submitted.( The same flavor and thinking)

#include "stdio.h"
#include "math.h"

int main()
{
	double arrayA[4] = {19.75, 27.5, 14.25, 38.5};
    	int i;
	for ( i=0; i<4;i++)
	{
 		int intpart = (int)arrayA[i];
 		double decpart = arrayA[i] - intpart;
 		
		if( decpart > 0.5)
		{
			arrayA[i]=(int)ceil(arrayA[i]);
		}
		else if( decpart < 0.5)
		{
			arrayA[i]=(int)floor(arrayA[i]);
		}
		else if( decpart == 0.50000)
		{
			if( intpart%2 == 1)
			{
				arrayA[i]=(int)ceil(arrayA[i]);
			}
			else if( intpart%2 == 0)
			{
				arrayA[i]=(int)floor(arrayA[i]);
			}
		}
	}
	
	for(i=0;i<4;i++)
	{
		printf("%d\n",(int)arrayA[i]);
	}
	return 0;
}

OUTPUT:
laptop:~$ ./a.out
20
28
14
38

Eventually, the take away from this article is the knowledge of using Ceil, floor and some basic typecasting.

Efficient , quicker and shorter code are welcome to be sent as comments !

4 responses to “Use of ceil and floor

  1. Arjun January 26, 2011 at 7:21 am
    #include 
    #include 
    #include 
    
    int main()
    {
        float d[4] = {19.75, 27.5, 14.25, 38.5};
        float middle;
        int count = 0, in[4];
        for(int i = 0; i  middle)
              {
                      in[i] = (int)ceil(d[i]);
              }
              if(d[i] &lt; middle)
              {
                      in[i] = (int)floor(d[i]);
              }
              if(d[i] == middle)
              {
                      if((count % 2) == 0)
                      {
                                in[i] = (int)ceil(d[i]);
                                count++;
                      }
                      else
                      {
                          in[i] = (int)floor(d[i]);
                          count++;
                      }
              }
              printf(&quot;%d\t&quot;, in[i]);
        }
        getch();
        return 0;
    }
    
  2. Kishore Ravindran January 26, 2011 at 4:56 pm

    Thanks for the help dude.. coming up with a workaround in 10mins is a good achievement given the situation I was in… here’s the code I am using.

    i used a flag to alternate ceil and floor b/w xx.5 values and Round function for other numbers. code is in java

    boolean flag = true;
    for(int i=0; i&lt;targetFundPercentage.length; i++)
    {
    double decimlPart = targetFundPercentage[i] - (int)targetFundPercentage[i];
    if(decimlPart == 0.5)
    {
       if(flag)
       {
          targetFundPercentage[i] = Math.ceil(targetFundPercentage[i]);
          flag = false;
       }
       else
       {
          targetFundPercentage[i] = Math.floor(targetFundPercentage[i]);
          flag = true;
       }
    }
    else
    {
      targetFundPercentage[i] = Math.round(targetFundPercentage[i]);
    }
    }
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: