Friday, May 25, 2012

TGIPL 6 solution for all three levels:2 Medium

So below is my submission for TGIPL6 Medium level question link :


#include<stdio.h>
#include<string.h>

//Assume following return types while writing the code for this question.
double output1;

double magic(int a)
{
int b=2,c=3,temp;
int i;
if(a==1)return 2;
if(a==2)return 3;
for(i=2;i<a;i++)
{
temp =c;
c=b+c;
b=temp;
}
return c;
}
void GetPossibleAssignments(int input1)
{
int t=magic(input1);
output1=t*t;   
if(input1<=0||input1>=1000)
output1=0;
//Write code here
}

2 comments:

  1. I think this is long code for medium level
    see my code
    #include
    #include

    //Assume following return types while writing the code for this question.
    double output1,f1=0,f2=1,k=0;
    void GetPossibleAssignments(int input1)
    {
    if(input1<=0||input1>=1000)
    output1=0;else
    for(k=-1,input1+=2;++k<input1;f1=f1+f2,f2=f1-f2,output1=f1);
    }// method
    }//main function

    This is simple and robust program

    ReplyDelete
  2. Santosh please check your algo I am not sure this code will pass any test case ... also can you explain how my code is not robust I think added all the error checks required and i do not see any extra checks in your code either .. About writing simple code I think my code is more readable than yours writing the whole algo inside for is not a good practice .. Thanks

    ReplyDelete