none
Quiz-IV (Questions and Solutions) RRS feed

  • Question

  • Guys what do you suggest ?
    Do you want me to post the quiz question that i was asked, and the solution to it right now, or after the quiz.

    If noone replies till the quiz ends, then i will post the question and answer to the quiz-IV that i got here.

    I request you guys also to post the questions that you got along with the solutions, so that one can see others code, and may be learn from it about how to improve the efficiency of the code, and the logic, and all other aspects.


    Saturday, May 19, 2007 1:15 PM

Answers

  • Yes this is the last quiz, we should more be active....
    Saturday, May 19, 2007 3:10 PM
  •  

    I think, We should wait untill the end of quiz.

     

    We will discuss quiz que / ans from monday onwards.

    Saturday, May 19, 2007 4:15 PM
  • I respect your suggestion  Hiren. We will start the discussions from monday.
    Saturday, May 19, 2007 4:58 PM
  • Yeah, if we discuss the event now, whats the fun. Lets discuss after the quiz is Monday...
    Saturday, May 19, 2007 10:14 PM
  • Dear Friend,

     

    Greetings, respecting others is an great self respect too. Sure we can post our questions from monday and shall discuss the logic to simplify the code.

     

    regards,

    Sunday, May 20, 2007 3:35 AM
  • I am waiting fr monday to come Smile so that i can post the code immediately.

    Sunday, May 20, 2007 5:22 AM
  • Ya will post mine too on monday... the quiz was too easy...
    Sunday, May 20, 2007 9:42 AM
  • Patience guys, abhi delhi bahut khareeb hai....
    Sunday, May 20, 2007 1:08 PM
  • Well ,we will post our codes on monday.. but let us have many threads, else a single thread will become too long and will be difficult to browse thru...
    Sunday, May 20, 2007 4:52 PM
  • @Raghuram - No need for multiple threads about the same,. it will become harder for people to find stuffs. As per my experience, the quiz threads dont get too long, it hardly gets to 5 pages.

    And even if it gets, then we can start a new thread that can continue with this one. but having duplicates at the same time will create more confusion .
    Sunday, May 20, 2007 5:02 PM
  • Ok guys, its monday now. Start posting the quiz questions and solutions. To start with ill post my questions along with the answers.

    If anyone has done it with better logic, or think that the same program can be done in a better / easier, you guys are free to discuss them.
    Monday, May 21, 2007 4:10 AM

  • Bouncing Ball
    When a ball is pitched repeatedly, it loses its height with each bounce. Assuming that the height to which the ball bounces gets reduced by half after each pitch, you are required to find out the height of the bounce after 'n' pitches. Suppose, the initial height of the ball is 8 mts. After the first pitch its height would be 4 mts. After the second pitch its height would be 2 mts. The initial height of the ball is given as an input.
    The function attainHeight contains the following components defined as input

        * Initial Height
        * Pitch number.

    Write a program to find the height of the ball after its 'n' th pitch. (pitch number is given as an input)

    # Constraints The initial height will be a number of power of 2
    # The number of pitches will be < 30
    # The output will always be >=1

    Example 1:
    Input:
        int height = 2000
        int pitchnumber = 4

    Output: Returns 125

    Explanation: The height of the bounce gradually decreases into half of the previous height when it bounces. In this example as the ball is thrown from a height of 2000 for the first pitch its height gets reduced by half and the so the height will be 1000. In the next bounce it again gets reduced by half to 500. In the third pitch, its height will be 250 and in the fourth pitch its height will be 125. This figure 125 has to be returned as the output as the total number of pitches is 4.

    Example 2:
    Input:
        int height = 5450
        int pitchnumber = 7

    Output: Returns 42

    For C solutions
    Header File    :    BouncingBall.h
    Function Name    :    int attainHeight(int height, int pitchnumber)
    Directory Name    :    bouncingball
    File Name    :    BouncingBall.c
    For C++ solutions
    Header File    :    BouncingBall.h
    Class Name    :    ball
    Function Name    :    int attainHeight(int height, int pitchnumber)
    Directory Name    :    bouncingball
    FileName    :    BouncingBall.cpp


    #include "BouncingBall.h"
    #include<stdio.h>
    #include<math.h>

    int attainHeight(int height, int pitchnumber) {
            if(pitchnumber>=30 || pitchnumber<=-1) return 0;
        return (int)floor(height/pow(2,pitchnumber));
    }

    void dsmain() {
        printf("%d\n",attainHeight(2000,4));
        printf("%d\n",attainHeight(5450,7));
    }

    Monday, May 21, 2007 4:12 AM
  • Inter Prime

    Inter Prime
    Inter Prime are positive integers obtained by the following process: An interprime is the average of two consecutive odd primes. For example, 9 is an interprime because it is the average of 7 and 11.

    Constraints:
    All integers are positive
    Integers should be > 0 and < 100000
    Example 1
    Input:
    int from = 1
    int to = 20
    Output
         Returns 4, 6, 9, 12, 15, 18

    Explanation: odd primes of first two numbers is 3,5 so (3+5 = 8 and average of this two no is 4 i.e 8/2) like wise 5+7 = 12 /2 = 6, 7+11 = 18 /2 = 9, 11+13 = 24/2 = 12; 13+17 = 30/2 = 15, 17+19 = 36/2= 18.

    Example 2
    Input:
    int from = 120
    int to = 200
    Output
         Returns 120, 129, 134, 138, 144, 150, 154, 160, 165, 170, 176, 180, 186, 192, 195, 198,

    For C solutions
    Header File    :    InterPrime.h
    Function Name    :    int* getInterPrime(int from, int to)
    Directory Name    :    interprime
    File Name    :    InterPrime.c
    For C++ solutions
    Header File    :    InterPrime.h
    Class Name    :    interprime
    Function Name    :    int* getInterPrime(int from, int to)
    Directory Name    :    interprime
    FileName    :    InterPrime.cpp


    Monday, May 21, 2007 4:13 AM

  • Solution to the InterPrime Problem

    #include"InterPrime.h"
    #include<stdio.h>
    #include<stdlib.h>

    int isPrime(int no) {
        int i=0,flag=0;
        if(no==1 || no==2) return 0;
        for(flag=0, i=2 ; i<=no-1 ; i++) {
            if(no%i==0) {
                flag++;
                break;
            }
        }
        if(flag==0) return no;
        else return 0;
    }

    int getNextPrime(int no) {
        int i=0;
        for (i=no+1 ; ; i++ ) {
            if(isPrime(i)) return i;
        }
    }
    int *ans, cnt=0;
    int* getInterPrime(int from, int to) {
        int i,avg=0;
        cnt=0;
        if (!(from>=0 && to>=0 && to >=from)) return 0;
        ans=(int *)malloc(sizeof(int)*5000);
        for(i=1 ; i<=to+getNextPrime(to) ; i++) {
            avg = (i+getNextPrime(i))/2;
            if(isPrime(i) && avg>=from && avg<=to) {
                ans[cnt++] = avg;
                printf("%d  ",avg);

            }
        }
        return ans;
    }

    void dsmain() {
        int i=1, avg=0;
        int *arr;
        arr = getInterPrime(120 ,200);
        for(i=0 ; i<cnt ; i++) {
            printf("%d ",arr[i]);
        }
    }

    Monday, May 21, 2007 4:16 AM
  • Hey Harshil This were the same questions asked to me.. well, your code has many points to pin out.. but i will post my code and you can very well see what i wana say
    Just have a look at them and tell me how they are
    Monday, May 21, 2007 4:24 AM
  • This is the code for inter-prime problem
    Code Snippet


    #include<iostream>

    #include<math.h>
    #include<conio.h>

    using namespace std;
    int ans_size = 0;

    int* getInterPrime(int from, int to)
    {
         ans_size = 0;
         if(from<0 || to <0)
         {
                   cout<<"Sorry the numbers have to be positive";
                   return NULL;
         }
         if(!(from>0 && to<100000))
         {
                    cout<<"Sorry the range is not correct as per the standard";
                    return NULL;
         }
        int n, ncount=0, square, divisor, prime=1, number=from;
        int prime_no_array[(to-from)/2];
        if(number <3)
                  number = 3;
        else if(number%2 == 0)
            number++;
           
            while (number<=to)
            {
                square=sqrt(number); prime=1;
                divisor=3;
                while ((divisor<=square) && (prime))
                {
                    if (number%divisor==0)
                        prime=0;
                    else
                        divisor+=2;
                }
                if (prime)
                {
                    prime_no_array[ncount++] = number;
                }
                number+=2;
            }
        if(ncount<2)
                    return NULL;
                   
        int *ans;
        int i;
        ans = new int[ncount-1];
        for(i=0; i<ncount-1; i++)
        {
                ans[i] = (prime_no_array[i] + prime_no_array[i+1])/2;
        }
        ans_size = ncount-1;

        cout<<endl;
        return ans;
    }

    int main()
    {
         int from = 1, to = 20;
         int *ans = NULL;
        
         ans = getInterPrime(from, to);
         cout<<"Displaying the ans :"<<endl;
         for(int i=0; i<ans_size; i++)
                 cout<<ans[i]<<", ";
         cout<<endl;
        
         from = 120; to = 200;
         ans = getInterPrime(from, to);
         cout<<"Displaying the ans :"<<endl;
         for(int i=0; i<ans_size; i++)
                 cout<<ans[i]<<", ";
         getch();
         return 1;
    }


    Monday, May 21, 2007 4:26 AM
  • This is the code for bouncing ball problem...


    Code Snippet

    #include<iostream>
    #include<math.h>
    #include<conio.h>

    using namespace std;

    int attainHeight(int height, int pitchnumber)
    {
        if(height %2 != 0)
        {
                  cout<<"The initial height has to be power of 2";
                  return -1;
        }
        if(pitchnumber>29)
        {
                  cout<<"The pitch number has to be smaller than 30";
                  return -1;
        }
        int ans;   
        ans = height/pow(2,pitchnumber);
        if(ans <1)
               return 1;
        else
            return ans;

    }

    main()
    {
          int height = 2000, pitchnumber = 4, output;
          output = attainHeight(height, pitchnumber);
          if(output != -1)
                 cout<<output<<endl;
          height = 5450; pitchnumber = 7;
          output = attainHeight(height, pitchnumber);
          if(output != -1)
                 cout<<output<<endl;
          getch();
    }


    Monday, May 21, 2007 4:28 AM
  • Hey varun, i have seen your code. Seems good.

    Please point out the things that you feel isn't right in my code. so that i can know what i did wrong. One thing i didn't do is
        if(!(from>0 && to<100000))

    i purposefully didn't add that, because signed int's range cannot be more an +32767, so whats the reason in checking for that condition, even if anyone enters that number it will be stored as a different number. So no use of checking that condition unless we have the parameters as LONG. but in the given function prototype its int, so i just ignored it.
    Monday, May 21, 2007 6:08 AM
  • Well you are talking about the 32 bit computers.. what if the computer is a 64 bit ????
    And most of all, they have put the constraint, so i had put it.. but anyways nice observation...
    I will post some of the points in observed in your code...
    Monday, May 21, 2007 6:47 AM
  • 1.) The calculation of prime number can be optimised, 1st of all no even number greater than 3 is prime, so why to check for those numbers?? So instead of i++, you could have i+2, and skip all the even number.. it will reduce the number of operations to half..

    2.) There is a theorem which states that if the number is not divisible by any number less than or equal to the square root of the given number, the given number is a prime..
          So when you are checking for the prime number, dont need to check till n-1, just check it till sqrt(n)...  this will again reduce the number of operations not just by half, but eponentially...
    i.e. if you are checking lets say for 36, you need to check it with numbers till 6 only.. same way for lets say 41, check it till 7 only....

       So you see how you can optimize the prime number calculation....

    3.) No need to have a globle variable for *ans, because the function will return the value of the new address...

    4.) Why to create so large space for the array size.. create a temp array and , then count the size and create an array of that size.. it will save a lot of memory space....

    Have a look at my code after this points, and you will see the difference...
    Monday, May 21, 2007 7:06 AM
  • This was a simple code, so there is not much problem.. just that in case of Error, you should have not displayed the value 0, so you could have checked that in the code.. also there is a constraint saying that in any case the outcome is >=1, so if the function comes to value lets say .25 or .125, then it should return 1 instead of 0, so you could have taken care of it..


    Well there was one point in the question i was not able to understand.. they said that the height will be of power of 2, but that was not the case in both the examples.. so i assumed it to be even initially.. wat do u say on it???
    Monday, May 21, 2007 7:26 AM
  • @Varun - Good notice.

    Ill clear out all the points that you pointed out.

    Ponint#1 : I had i+=2 earlier in my code, but i did a bit of modifications, i had put isPrime(i++) so , because of that i changed the i+=2 in that for loop to i++. and then somehow i changed the code, and forgot to change it back. Good notice Varun. You won on me in this one

    Point #2 : I knew that there is no need to check uptil n-1. Instead of calling sqrt in the for loop everytime,  it would be useless, i could have used a variable instead string the sqrt(n) before the lop, but i didn;t want to increase the line of the code. As Devsquare check that also . And to be frank, even though i knew that theorem, that sqrt thing didn;t didn't come into my mind while i was coding Stick out tongue  You win on me in this one too...

    Point#3. I know i wasted lots of memory, and used that *ans outside the function which was not at all required, but i did that purposefully, because sometimes gcc compiler raises an error when you return a  address of a variable that is declared within the function. I could have used static variable instead, but it would have the same effect as i did by declaring it outside, as static variables lives during the entire execution of the program.  I know that i could have done it as you did by creating an array, and then doing it. But i thought why do things twice, and copy from one array to the other. I just let it go.

    And about that return 0 thing,  i guess what i did was right, cause it would return 0 if anything was invalid. The contraint written was >=1 should be the output. and and anything less than 1 bounces is not a  complete bounce, so i returned 0. I hope the admins understand my points while checking the program.

    Varun good job once again . Thanks for letting me know what was missing in my code, and how i could optimize it. This is how  we learn from the mistakes Smile
    Monday, May 21, 2007 7:54 AM
  •  

    @Varun

    In your interprime program,

    Enter the input 120 and 200.

     

    Now in the question. example gives output like 120,129....

    but your program gives output 129,.... not 120.

    Monday, May 21, 2007 8:34 AM
  • Hey guys tell me whether my code is right?????

    #include<stdio.h>
    int* getInterPrime(int from, int to)
    {
    int arr[100];
    int k=0,i,c=0,a,sum,x,y,m;
    for(m=from;m<to;m++)
    {  c=0;
        for(i=1;i<=m;i++)
        {
         a=m%i;
         if(a==0)
              c=c+1;   
        }
        if (c==2)
        { 
             if((m%2)!=0)
             {
                arr[k]=m;
                k++;
             }
        }
    }
    for(x=0;x<(k-1);x++)
    {
         sum=arr[x]+arr[x+1];
         printf("%d ",sum/2);
    }
    }
    void dsmain()
    {
    getInterPrime(1,20);
    }
    Monday, May 21, 2007 9:38 AM
  •  Hiren_Kotadiya_0230fd wrote:

    @Varun

    In your interprime program,

    Enter the input 120 and 200.

    Now in the question. example gives output like 120,129....

    but your program gives output 129,.... not 120.


    Hiren Observe it closely and you will see that the example given is itself wrong.. you are starting from the number 120 and you are giving an average between 2 numbers.. so even if you select 120 and 121 and give its average, it is going to be 120.5 so in no case you could get 120 as the output.. so you see the example was not correct.. the numbes start with 129, 120 is a printing mistake
    Monday, May 21, 2007 10:46 AM
  • Well buddy to be honest.. your code is not impressive, you did only half the work.. or even less... you are only displaying the answer.. but you are suppose to create a array of this answers and return it.. that is the reason that the function is having a return type as int *

    So the things you are displaying, store it in an dynamic array and return the address of that array... Other points are optimization which i had given above..
    Monday, May 21, 2007 10:55 AM
  • @Varun

    They wanted to print interprime numbers between that range, and not the internumbers that lie between the average of the two numbers between the range that you specified.

    Here is more info for your clarification

    Prime   NextPrime   Average (InterPrime Numbers)
    113     127             120
    127     131             129
    131     137             134
    137     139             138
    139     149             144
    149     151             150
    151     157             154
    157     163             160
    163     167             165
    167     173             170
    173     179             176
    179     181             180
    181     191             186
    191     193             192
    193     197             195
    197     199             198

    I hope you get it now.


    Monday, May 21, 2007 10:55 AM
  • @Mohan - I haven't tried executing the code, but what i have noted immediately and what Varun mentioned, that you have to return the output as an pointer to the array, which you have not done. You need to store it in the array, and then return an address to it.

    Varun has explained in more detail how to do it.
    Monday, May 21, 2007 10:58 AM
  • Ohh, that makes sense.. those people should have specified it.. they just gave the range but did not specifically said that range is for what...
    I had even asked the person on live help.. and he said that dont consider 120.. what kind of help centre they have....


    Still nice observation harshil
    Monday, May 21, 2007 11:01 AM
  • Dear Friends,

     

    Greetings, check out this code.

     

    Create a file named Interprime.c

     

    #include<stdio.h>

    #include<math.h>

    #include<InterPrime.h>

     

    void dsmain(){

            int *pb,i;

            pb=getInterPrime(0,100000);       

            for(i=0;i<317;i++){

                printf("%d\n",pbIdea);

            }

    };

     

    int chkPrime(int n){

         int i;

         if(n<=2)

             return 0;

         if(n%2==0)

             return 0;

         for(i=3;i<=sqrt(n);i++)

             if(n%i==0) return 0;

         return 1 ;

    };

     

    int getPrime(int a[],int from,int to){

          int i,j=0;

          for(i=from;i<=to;i++){

              if(chkPrime(i)==1) a[j++]=i;

           }

           return  j;

     };

     

     int* getInterPrime(int from, int to){

         int a[317],b[317],i,l;

          int *pb=b;

         l=getPrime(a,from,to);

        

         for(i=0;i<l-1;i++)

             bIdea=(aIdea+a[i+1])/2;

         return pb;

     };

     

    Header file Code:

     

    void dsmain();

     int chkPrime(int n);

     int getPrime(int a[],int from,int to);

     int* getInterPrime(int from, int to);

    Monday, May 21, 2007 1:26 PM
  • There were problems in the questions about not clearly mentioning the range for what was asked. Even when i first solved the problem, mine had 120 excluded, then i checked the sample solution, and then came to know that they wanted interprime numbers of that range.
    Monday, May 21, 2007 1:59 PM
  • Fredrick 1st off all please use the code block to put ur code in the forums.. it gets hard to read them...
    Now you made the same mistake as most of us... the range problem.. which i think only Harshil was able to observe...
    There are some points where it can be optimized.. 1st calculate the sqrt and put it into a variable.. you are calculating sqrt at each check, which is too overhead...
    2nd No even number is prime, so why check those numers for prime..
    3rd you have fixed the size of the array which is going to be returned, it can change.. lets say in 1st example you have only 6 interprime, for that you are giving a space of 317 integers....
    4th the array you are returning is a local member which will get distroyed.. so you wont be able to retain the answer after the function call.. hence most of the time there will be garbage...

    Monday, May 21, 2007 4:05 PM
  •  

    Yes Harshil,

    I had faced same problem.

    I found it after doing program then I modified my program.

    Monday, May 21, 2007 5:58 PM
  • @ Varun -when i executed your code in TC , it shows an error at the line

    int prime_no_array[(to-from)/2];
     It says "constant parameter required as array subscript"

    Tuesday, May 22, 2007 1:35 AM
  • Altered Sentence Problem

    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>       
    # include "AlterSentence.h"

    char* getAlteredSentence(char* fullSentence,char* findWord,char* replaceWord);

    void dsmain()
    {
        char *fullSentence = "Honestly are the best policy";
        char *findWord = "are";
        char *replaceWord = "is";
        char *temp = getAlteredSentence(fullSentence,findWord,replaceWord);
        printf("%s",temp);
    }
       
    char* getAlteredSentence(char *fs,char *fw,char *rw)
    {
        char *a;
        char *words[100];
        int w = 0;
        int final_round;
        int sentence_len;
        char word[100];
        int temp=0,temp1,temp2=0;
        strcpy(word,"");
        sentence_len=strlen(fs);
           
        while(temp2<=sentence_len)
        {
            if(fs[temp2]==' ')
            {
                word[temp]='\0';
                a=(char *)malloc(strlen(word)+1);
                strcpy(a,word);
                words[w]=a;
                w++;
                temp=0;
                temp2++;
            }
            else
            {
                word[temp++]=fs[temp2];
                temp2++;
            }
        }
        word[temp]='\0';
        a=(char *)malloc(strlen(word)+1);
        strcpy(a,word);
        words[w]=a;
        temp1=w;
        temp=0;

        while(w>-1)
        {
            if(strcmp(words[w],fw)==0)
            {
      words[w]=rw;
     }
     w--;
        }
        strcpy(fs,"");
        final_round=0;
        while(final_round<temp1)
        {
            strcat(fs,words[final_round]);
            strcat(fs," ");
            final_round++;
        }
        strcat(fs,words[final_round]);
        return fs;
    }

     

     

    Tuesday, May 22, 2007 1:46 AM
  • Buddy, I just checked it.. I had made the prog in Bloodshed Compiler.. it has a very much resemblance with the Devsquare compiler.. that is the reason it doesnot show any error in it and neither at the Devsquare.. I also checked it in VC++ and yes it does show an error their..

    What you can do in this case is declare a pointer variable of prime_no_array and dynamically allocate the memory... it should work then.. but remmber to free the memory for it before leaving the function...
    Tuesday, May 22, 2007 3:35 AM
  • Have a look at this code for the similar problem...


    Code Snippet

    #include<iostream>
    #include<string>
    #include<conio.h>

    using namespace std;
    char* getAlteredSentence(char* sentence,char* find,char* replace)
    {
          string s(sentence);
          char *tmp;
          tmp = new char[s.length() + 100];
          int location;
          location = s.find(find);
          while(location != -1)
          {
                         s.replace(location,strlen(find),replace);
                         location = s.find(find);
          }
          int i;
          for(i=0; i<s.length(); i++)
          {
                  tmp[i] = s.at(i);
          }
          tmp[i] = '\0';
          return tmp;
    }
    main()
    {
          char s1[]=("Do not worry about your difficulties in Mathematics. I can assure you mine is still greater.");
          char *s2;
          s2 = getAlteredSentence(s1, "is", "are");
          cout<<s2;
          getch();



    The code is short and sweet, depicting the power of C++, also i had to make the conversion from the string class to a Char *, so had to do extra work, or else if would have been even shorter... So how is it???
    Tuesday, May 22, 2007 3:51 AM
  • @Varun - Good code man.

    Guys, please post the other questions that you guys got. We are free now, we can try solving it during the vacations.
    Tuesday, May 22, 2007 4:56 AM
  • @Raghuram - The error you get is normal in TC and VC++.  You will get that error if you use non constant variables/values  to allocate the array size. Try using gcc compiler, or as varun said use the Bloodshed compiler. They support to allocate dynmanic memory to the arrays and pointers.

    But for just the purpose of testing the code, replace
    int prime_no_array[(to-from)/2];
    with

    int prime_no_array[SOME_CONSTANT_VALUE];
    and it will work.
    Tuesday, May 22, 2007 5:11 AM
  • Ok i think noone is interested in posting their questions. Have you guys been affected by the admins announcement ? Guys that was for the people who posted the questions before the quiz ended. The quiz has ended , and now i think there is no problem in discussing it. Guys lets continue. The show must go on.
    Tuesday, May 22, 2007 4:27 PM

  • Magical Card

    Magical card problem is a simple card game where you can select a card from the given card matrix arranged in 3*3 array. The program should perfom two shuffling and at the end of two shuffling, the card that you have selected should appear in the middle of the matrix i.e the row r2 and column c2 should contain the card that you have selected. :

    Description: You will be given a 3 by 3 matrix of cards. The cards will be arranged in 3 rows and 3 columns. What you need to do is, to select any one card from the list of matrix and keep it in mind. You should not disclose the card that you have selected. You will be asked to tell in which column does the card (the card that you selected initially) falls. After that the cards will be taken and arranged once again in 3 by 3 matrix. Again for the second time you will be asked to tell in which column does the card (the card that you selected initially) falls. Based on the input that you have provided the system will calculate and generate the output. The system generated output will be the card that you selected initially.

    The methods used in this program are getFirstShuffleResult, getSecShuffleResult and getFinalResult. The input value for the first method getFirstShuffleResult is an 3 by 3 matrix and the column number("C1", "C2", "C3") does the card falls. getFirstShuffleResult method is applied once for shuffling the matrix as per the description mentioned above. getSecShuffleResult method is applied again for shuffling the matrix as per the description mentioned above. The third getFinalResult uses the 3 by 3 matrix which is the result of the second method and returns the magical card.

    # Calculation process: Initially you will be provided with a 3 by 3 matrix and you be asked to select any one card from the card matrix.
    # You will be asked to tell in which column the card falls.
    # If the card falls in first column, then first take the cards from the second column and make it as first row and then take the cards from first column and arrange it in the second row and then the third column cards and then arrange it in the third row.
    # If the card falls in second column, then first take the cards from first column and place it in the first row and then take the cards from second column and arrange it in the second row and then third column cards and arrange it in the third row.
    # Similarly if the card falls in third column, then first take the cards from second column and arrange it in the first row and then arrange the third column in the second row and then arrange the first column cards in the third row.
    # Now you will be having the cards arranged one above the other.
    # Again arrange the cards in 3 by 3 matrixes.
    # Again the same procedure will be followed from step 2 to step 5.
    # Now you will be having the cards arranged one above the other.
    # Again arrange the cards in 3 by 3 matrixes.
    # Again the same procedure will be followed from step 2 to step 5.
    # Now the card that you selected initially will be present exactly in the middle of the matrix.
    # Extract the card from the middle of the matrix. And this will be the outputted result and the card that you selected.

    Constraints
    No assumptions to be made.
    Consider that all the inputs are given.
    Card numbers will fall between 0 to 100

    Example 1:
    Input:
    char* firstShuffleColPos= "C1"
    char* secShuffleColPos= "C2"
    int** cardList = { {12, 5, 37},
                                   {23, 67, 3},
                                   {3, 36, 56} };

    output for first shuffle: int** firstShuffle={ {5, 67, 36},
                                                                     {12, 23, 3},
                                                                     {37, 3, 56} };

    output for second shuffle: int** secShuffle={ {5, 12, 37},
                                                                          {67, 23, 3},
                                                                          {36, 3, 56} };

    Output : getFinalResult function returns = 23

    In this example, the card selected is in the column C1 and after the first shuffle the card selected in coulmn C2. As per the descrption since the card is in the C1 column the first shuffle will be done as follows: first take the cards from second column and then take the cards from first column and then third column cards and arrange in the rowwise. This will result in the arrangement depicted in example 1 output for first shuffle. Again the input for the second shuffle is C2. So by following the same method the resultant matrix is as per the arrangement shown in the example 1 output for second shuffle. So after shuffling again the output for thrid function is as depicted in example 1. Now looking at this matrx the card selected is 23.

    Example 2:
    Input
    char* firstShuffleColPos= "C3"
    char* secShuffleColPos= "C1"
    int** cardList = { {5, 56, 99},
                                   {1, 45, 30},
                                    {6, 69, 23} };

    output for first shuffle: int** firstShuffle={ {56, 45, 69},
                                                                     {99, 30, 23},
                                                                     {5 , 1 , 6} };

    output for second shuffle: int** secShuffle={ {45, 30, 1},
                                                                          {56, 99, 5},
                                                                          {69, 23 ,6} };


    Output : getFinalResult function returns = 99

    For C solutions
    Header File    :    MagicalCardGame.h
    Function Name    :    int** getFirstShuffleResult(int** cardList, char* firstShuffleColPos),
    int** getSecShuffleResult(int** firstShuffle, char* secShuffleColPos),
    int getFinalResult(int** secShuffle)
    Directory Name    :    magicalcardgame
    File Name    :    MagicalCardGame.c
    For C++ solutions
    Header File    :    MagicalCardGame.h
    Class Name    :    cards
    Function Name    :    int** getFirstShuffleResult(int** cardList, char* firstShuffleColPos),
    int** getSecShuffleResult(int** firstShuffle, char* secShuffleColPos),
    int getFinalResult(int** secShuffle)
    Directory Name    :    magicalcardgame
    FileName    :    MagicalCardGame.cpp

    Tuesday, May 22, 2007 4:28 PM

  • NewsPaper Problem

    In this problem a paragraph has to be modified such that it can be printed in newspaper columns. Newspaper articles are written in fixed length columns (of size 10 ) and text has to be modified in such a way that all lines have fixed number of characters except perhaps the last line.
    # The function newspaperLastline contains the String as components defined and the inputs given, char* text
    Write a program to return the number of characters in the last line of the printed text.
    # Constraints: Split words need not be hyphenated.
    # A space is also considered as a character.

    Example 1:
    Input:
        char* test = "The Stonehenge today stands insulated on a separate plane of existence; all its worldly functions duly given."

    In a column of length 10 this text should be written as:
    The Stoneh
    enge today
      stands in
    sulated on
      a separat
    e plane of
     existence
    ; all its
    worldly fu
    nctions du
    ly given.

    Output: Returns 9 (length of "ly given.")

    Explanation: Here the last line is taken into consideration after aligning the sentence as per the column length of 10 including the spaces that occur in the sentence. The output returns 9 as there are 9 characters including the space.

    Example 2:
    Input:
        char* test = "You can login in DevSquare from anywhere over the Internet and compile and execute code without needing a compiler / execution engine on your local machine."
    In a column of length 10 this text should be written as:
    You can lo
    gin in Dev
    Square fro
    m anywhere
      over the
    Internet a
    nd compile
      and execu
    te code wi
    thout need
    ing a comp
    iler / exe
    cution eng
    ine on you
    r local ma
    chine.

    Output: Returns 6 (length of "chine.")

    For C solutions
    Header File    :    NewsPaperPrint.h
    Function Name    :    int newspaperLastline(char* text)
    Directory Name    :    newspaperprint
    File Name    :    NewsPaperPrint.c
    For C++ solutions
    Header File    :    NewsPaperPrint.h
    Class Name    :    newspaper
    Function Name    :    int newspaperLastline(char* text)
    Directory Name    :    newspaperprint
    FileName    :    NewsPaperPrint.cpp
    Tuesday, May 22, 2007 4:29 PM
  • anyone who has got the above questions in their quiz ? Try posting your code so that we can have an idea. If noone posts, then ill try solving the questions and post it when i get time.
    Tuesday, May 22, 2007 4:31 PM

  •     long price=(long)pric;
        char str[15];
        long i=100000;
        int j=0;

        long n=price/i;
        price=price%i;

        if(n>0)
        {
            int k,l;
            k=n/10;
            l=n%10;

            if(k>0)
            {
                str[j]=k+48;   j++;
                str[j]=l+48;   j++;
                str[j]=',';    j++;
            }
            if(k==0)
            {
                str[j]=l+48;   j++;
                str[j]=',';    j++;
            }
        }
        i=i/100;

        n=price/i;
        price=price%i;

        if(n>0)
        {
            int k,l;
            k=n/10;
            l=n%10;

            if(k>0)
            {
                str[j]=k+48;   j++;  
                str[j]=l+48;   j++;
                str[j]=',';    j++;
            }
            if(k==0)
            {
                str[j]=l+48;   j++;
                str[j]=',';    j++;
            }
        }

        int k,l;
        n=price/100;
        price=price%100;

        str[j]=n+48;    j++;

        k=price/10;
        l=price%10;

        str[j]=k+48;    j++;
        str[j]=l+48;    j++;
     
        str[j]='\0';

        cout<<str;

    Tuesday, May 22, 2007 6:08 PM
  • Man if possible mention the question for the code that you posted. So that one can know for what question the code is.
    Different participants have got different questions in the quiz, so many of them might not know for which question you posted the solution.
    Tuesday, May 22, 2007 6:24 PM
  • Harshil I think there were not too many questions this time.. just have a look how many people got the interprime question... So I think we are out of questions this time.. that is the reason people are not posting questions...
    Tuesday, May 22, 2007 6:34 PM
  • I have modified the above program to use a sting class so that i can attach the program in my C++ asignment file.. but as i have shown earlier.. you can convert the output from the string class object to a simple string....

    so here goes one more powerful example of the String Class of C++

    Code Snippet

    #include<iostream>
    #include<string>
    #include<conio.h>

    using namespace std;

    int newspaperLastline(string &text)
    {

        int length = text.length();
        int i;
        for(i=1; i<=length/10; i++)
        {
                 text.insert(i*10 + i - 1, "\n");
        }
        return (length%10);
    }

    main()
    {
         string s1("The Stonehenge today stands insulated on a separate plane of existence; all its worldly functions duly given.");
         string s2("You can login in DevSquare from anywhere over the Internet and compile and execute code without needing a compiler / execution engine on your local machine.");
        
         cout<<"The size of last line : "<<newspaperLastline(s1)<<endl;
         cout<<s1<<endl;
         cout<<"The size of last line : "<<newspaperLastline(s2)<<endl;
         cout<<s2;
         getch();
         return 1;
    }


    Wednesday, May 23, 2007 4:20 AM
  • I agree with you this time about the less number of questions being asked in this month' quiz.

    My friends who got different quiz questions are enjoying vacation, they dont got internet access at home, so cant get the questions from them. But i am sure many of the students might have gone to my college and attended the quiz. I might go to the college in a day or two, and try to check if anyone has saved the questions or not. If so ill post it Smile
    Wednesday, May 23, 2007 5:42 AM

  • @Varun - I think there will be a wrong answer in your above code. What if the string has exactly 20 characters ? then the last line will contain 10 characters. while your code will return 0.


    int newspaperLastline(char* text) {

    int lastlen;

    return (lastlen=strlen(text)%10, lastlen==0)?10:lastlen;
    }



    I think this will work properly. I have directly written the code, without any compilation or testing. But i guess this is enough to discuss the logic.


    Wednesday, May 23, 2007 6:24 AM
  • hmmm.. Actually I didnt had an idea of what to do in that case.. well as it was not mentioned, i assumed that we had to return 0 only.. but ya we could very well do that.. and also, what i did extra in my code is that i had insterted '\n' at every 10th postion.. so now whenever you print the line.. it will be printed in the required way... Though it was not mentioned to specifically do it, i had assumed it...
    Wednesday, May 23, 2007 11:10 AM
  • Anyways, the logic aint much important. The point that you wanted to make was the  illustration of the solution using the power of C++ using the string class. And that was really a good one. It decreased the amount of code  using that   string.insert()   Smile
    Wednesday, May 23, 2007 12:37 PM

  • //C PROGRAM TO CALCULATE THE TOTAL COST TO CONSTRUCT AN AQUARIUM

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

    void dsmain()
    {

    //VARIABLE DECLARATION

        double total_cost;
        int Length = 20;               // int Length = 10;
        int Breadth = 15;              // int Breadth = 5;
        int Height = 7;                // int Height = 6;
        char* Thickness = "4mm";       // char* Thickness = "8mm";
        double LaborCharge = 650;      // double LaborCharge = 750;
       
    //FUNCTION CALL TO GETCONSTRUCTED PRICE, BY PASSING INPUT PARAMETERS   
        total_cost=getConstructionPrice(Thickness,LaborCharge,Length,Breadth,Height);
        

    //PRINTING THE CALCULATED TOTAL COST RETURNED BY THE FUNCTION
        printf("Total Cost = %lf",total_cost);
    }

     

    //FUNCTION getConstructionPrice

    double getConstructionPrice(char* thickness, double laborCharge, int length, int breadth, int height)
    {
    //LOCAL VARIABLE DECLARATION TO THE FUNCTION   
        double totalcost;
        int glass_cost;

    //CHECKING THE GLASS THICKNESS
        if(strstr(thickness,"4mm")) glass_cost=20;
        else if (strstr(thickness,"6mm")) glass_cost=40;
        else if (strstr(thickness,"8mm")) glass_cost=80;   
        else if (strstr(thickness,"10mm")) glass_cost=120;

    //CALCULATING TOTAL COSTS
         totalcost =(( (length*height*2) + (breadth*height*2) ) * glass_cost ) + laborCharge ;


    //RETURNING THE CALCULATED TOTAL COST
         return(totalcost);
    }

    Wednesday, May 23, 2007 3:15 PM
  • hi guys, i have noticed couple fo things here

     

    1. quiz questions were different

    2. i thought we were suppose to code using the same variable names as provided in the question and by the quiz, but i have noticed many members, use their own variables in the program....??? discuss and comment

    Wednesday, May 23, 2007 3:17 PM
  • Well Anoop, the only criteria was to keep the function name same.. we can have any variable name inside...
    The reason i think was because we only need to know the name of the function to call it.. then the variable name inside the function is not required to know.. so no problem in it...
    Wednesday, May 23, 2007 4:16 PM
  • @Anoop - Nice code. I liked the way you used strstr(). But i guess it would have been better to use strcmp(), but i dont know what the question is, so cant comment more on it. I think the reaosn why you put strstr() is that may be the user can enter "4mm" in any part of the string may be also include spaces before or after. not sure. Please explain your reason .

    If you have the question of that quiz question, then it would be nice of you if you post the question also.
    What was the other quiz question you got ? post it also.
    Wednesday, May 23, 2007 4:42 PM
  • thanks varun for clearing it out ....however since this was my first quiz and didnt have much experience so i decided to be on the safer side rather and follow ike mama' sboy ...lol
    Thursday, May 24, 2007 11:08 AM
  • hi harshil,
    i used strstr , so that , if there are any case differences then it will still work, and strstr also completes the task of strcmp, meaning to say that, even if both the strings are equal and you use strstr, it will still work the way desired.
    In this program you can use strcmp without any flaws because , we are not having any input from the STDIN, so there will not be any case differences as we are directly providing the values in our program itself....[avoiding the use of scanf]....

    but if we are using scanf or cin or anyother STDIN, than strstr would be a better option...

    But while coding it came naturally to me, to avoid that error....and so used strstr...

    anyways am trying to find, if i can get that question, i will post it as soon as i find it...
    Thursday, May 24, 2007 11:15 AM
  • Nice thinking... keep it up...
    Thursday, May 24, 2007 11:27 AM
  • hi varun, even u guys have coded really well....good one man...
    Thursday, May 24, 2007 12:09 PM
  • hi
    harshil heres the first question

    Code Snippet

    AlteringSentenceProblem is a problem which returns an altered sentence. Initially a sentence will be given with multiple words. Later we prefer to change some of the words in that sentence. The objective of this problem is to find the word that needs to be replaced in the sentence and replace it with new word given. The final output should be the full sentence with all replaced words.The following are the inputs given based on which output has to be generated:

    Full sentence with more than one word
    Which word to replace
    new word that will replace the old word

    Based on these inputs you have to generate the final sentence.

    Constraints:

    No assumptions to be made.

    Consider that all the inputs are given.

    Example 1:
    Input
        char* FullSentence = "Do not worry about your difficulties in Mathematics. I can assure you mine is still greater."
        char* FindWord = "is"
        char* ReplaceWord = "are"

    Output : Returns "Do not worry about your difficulties in Mathematics. I can assure you mine are still greater."

    Example 2:
    Input:
        char* FullSentence = "Yo will never be happy if Yo continue to search for what happiness consists of. Yo will never live if Yo are looking for the meaning of life."
        char* FindWord = "Yo"
        char* ReplaceWord = "You".

    Output: Returns "You will never be happy if You continue to search for what happiness consists of. You will never live if You are looking for the meaning of life."

    For C solutions 
    Header File : AlterSentence.h
    Function Name : char* getAlteredSentence(char* fullSentence,char* findWord,char* replaceWord)
    Directory Name : altersentence
    File Name : AlterSentence.c
    For C++ solutions 
    Header File : AlterSentence.h
    Class Name : sentence
    Function Name : char* getAlteredSentence(char* fullSentence,char* findWord,char* replaceWord)
    Directory Name : altersentence
    FileName : AlterSentence.cpp


    General Instructions:     * The directory , file / class names, functions, method signatures, header files to be used are mentioned in the problem statement. Do not use your own names or change the method signatures and fields. You can add any number of additional methods.
     
        * First add the directory in the Main Program Directory and then add the particular file in that directory. Do not forget to mention the file extension, either .c or .cpp as the case maybe.
     
        * For C solutions, change the value of "C_OR_CPP" macro in header file as 1 and for C++ solutions change the value as 2.
     
        * Incase of iostream.h specify as iostream only.  



    Thursday, May 24, 2007 12:11 PM

  • Code Snippet
    Solution to the aletring sentence problem


    /*
    C PROGRAM TO ALTER THE SENTENCE, ACCORDING TO THE GIVEN SUBSTRING, AND REPLACING THE GIVEN SUBSTRING BY A NEW STRING, AND THEN PRINTING THE NEW STRING[FULL]
    */

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


    void dsmain()
    {
    //VARIABLE DECLARATION

        char* FullSentence = "Yo will never be happy if Yo continue to search for what happiness consists of. Yo will never live if Yo are looking for the meaning of life.";
        char* FindWord = "Yo";
        char* ReplaceWord = "You";      
        char* ptr = " ";


    //FUNCTION CALL TO AlteredSentence FUNCTION BY PASSING FULLSENTENCE AND THE WORD TO REPLACE
        ptr=getAlteredSentence(FullSentence,FindWord,ReplaceWord);

    //PRINTING THE FINAL ALTERED OUTPUT SENTENCE
        printf("%s",ptr);

    }


    char* getAlteredSentence(char* fullSentence,char* findWord,char* replaceWord)
    {   
    //LOCAL VARIABLE DECLARATION TO THE FUNCTION
        char* temp;
        char* alteredString = " ";

    //LOGIC FOR ALTERATION, TOKENIZING THE STRING
        temp = strtok(fullSentence," ");
       
        while(temp != NULL)
        {

            //printf("Enter while");

            if(strcmp(temp,findWord)== 0)
            {
                //printf("enter");
                strcat(alteredString,replaceWord);
            }
            else
                strcat(alteredString,temp);


            temp = strtok(NULL, " ");

        }
       
    //RETURNING THE NEW ALTERED STRING
        return(alteredString);
    }



    Thursday, May 24, 2007 12:13 PM

  • Code Snippet
    Second question : - aquarium constructor problem
    Aquarium Construction Problem is a problem which calculates the total cost required to construct an aquarium. The construction cost is calculated based on different constraints. The objective is to find the total cost required to create an aquarium. The length, breadth and height of the aquarium will be given in feets. The cost of the glass used to construct an aquarium depends on the thickness of the glass. The price of the glass is fixed per square feet. Square feet for the glass with 4mm thickness will be different from 6mm thickness glass and so on. The following are the standard price for glass per square feet based on the thickness.

    4mm glass – 20Rs/SqFt
    6mm glass – 40Rs/SqFt
    8mm glass – 80Rs/SqFt
    10mm glass – 120Rs/SqFt
    The labor charge varies from each constructor.

    The getConstructionPrice function contains the following components as input
    1. Thickness of glass
    2. Labor charge for constructing the aquarium
    3. Length of the aquarium
    4. Breadth of the aquarium
    5. Height of the aquarium

    Based on these inputs, you have to calculate the total cost required for constructing an aquarium. Total construction cost will be the cost of the glass and the cost of labor charge.

    Constraints:

    No assumptions to be made.

    Consider that the thickness of the glass and labor charge will be given as the input.

    Example 1:
    Input
        int Length = 20
        int Breadth = 15
        int Height = 7
        char* Thickness = "4mm"
        double LaborCharge = 650

    Output: Returns 10450.
    Explanation : First calculate the total Square Feet of the Aquarium with the 20 feet length, 15 feet breadth and 7 feet height. The total length will be 280 square feet. The total breadth will be 210 square feet, therefore total square feet of the aquarium is 490 square feet. The cost for 4mm thickness aquarium is 20rs, hence 490 * 20 = 9800. Labour charge is 650rs. Therefore the total cost for constructing the aquarium construction is 9800 + 650 = 10450.

    Example 2:
    Input:
        int Length = 10
        int Breadth = 5
        int Height = 6
        char* Thickness = "8mm"
        double LaborCharge = 750

    Output: Returns 15150.

    For C solutions 
    Header File : AquariumConstruction.h
    Function Name : double getConstructionPrice(char* thickness, double laborCharge, int length, int breadth, int height)
    Directory Name : aquariumconstruction
    File Name : AquariumConstruction.c
    For C++ solutions 
    Header File : AquariumConstruction.h
    Class Name : aquarium
    Function Name : double getConstructionPrice(char* thickness, double laborCharge, int length, int breadth, int height)
    Directory Name : aquariumconstruction
    FileName : AquariumConstruction.cpp


    General Instructions:     * The directory , file / class names, functions, method signatures, header files to be used are mentioned in the problem statement. Do not use your own names or change the method signatures and fields. You can add any number of additional methods.
     
        * First add the directory in the Main Program Directory and then add the particular file in that directory. Do not forget to mention the file extension, either .c or .cpp as the case maybe.
     
        * For C solutions, change the value of "C_OR_CPP" macro in header file as 1 and for C++ solutions change the value as 2.
     
        * Incase of iostream.h specify as iostream only.  




    Thursday, May 24, 2007 12:15 PM
  • hi guys, i have already posted the solution to the second question, so am not posting it again, anyways

    the quiz was quite simple.....


    Thursday, May 24, 2007 12:17 PM
  • Thanks Anoop for posting for posting the questions. I think i dont need to say thanks anymore to you cause we have become good friends Smile

    Anyways, now i understand why you used that strstr, good strategic way of thinking Smile Also now i understand that aquarium code, hehe.

    Ill go to college tomorrow, and try to fetch some more quiz questions if i can Smile
    Thursday, May 24, 2007 6:03 PM
  • definitely harshil, no sorry and thnaks in friendship....

    anywayz do post the questions, when you get some....
    Friday, May 25, 2007 12:42 PM

  • Phone Bill

    Phone Bill
    Phone bill calculation program basically focuses on calculating the phone bill based on various criteria and tariff selected. In this problem there are four different tarrifs available and based each tarrif you are required to calculate the bill. The function or method used in this program is getCalculatedBillAmount. This method uses candidateName, selectedTariff, isCorparate, clipCharge, hiddenCharge, totLocalCalMinute, totSTDCalMinute, totISDCalMinute, totRoamingCalMinute, totLocalSMS, totNationalSMS, totInternationalSMS as input value.

    The four tarrifs available are:
    # Corporate tariff 199
    # Corporate tariff 249
    # Corporate tariff 299
    # Non Corporate tariff 300

    Procedure for calculating the phone bill for non corporate connection:
    # Will be having the clip charge.
    # Local call rate is 1rs/min.
    # STD call rate is 2.69rs/min.
    # Roaming call rate is 4rs/min.
    # ISD call rate is 9rs/min
    # Local SMS rate is 1rs/SMS.
    # National SMS rate is 2rs/SMS.
    # International SMS rate is 5rs/SMS.
    # Other hidden charges.

    Procedure for calculating the phone bill for corporate connection with tariff 199.
    # Will not be having any clip charge.
    # Local call 200 minutes free, after that the call rate is .30rs/min.
    # STD call rate is 2rs/min.
    # Roaming call rate is 3.75rs/min.
    # ISD call rate is 7.5rs/min.
    # 200 local SMS free, after that the rate is 50 paise/SMS.
    # National SMS rate is 2rs/SMS.
    # International SMS rate is 5rs/SMS.

    Procedure for calculating the phone bill for corporate connection with tariff 249.
    # Will not be having any clip charge.
    # Local call 300 minutes free, after that the call rate is .30rs/min.
    # STD call rate is 2.40rs/min.
    # Roaming call rate is 1rs/min.
    # ISD call rate is 7.5rs/min.
    # 200 local SMS free, after that the rate is 50 paise/SMS.
    # National SMS rate is 2rs/SMS.
    # International SMS rate is 4rs/SMS.

    Procedure for calculating the phone bill for corporate connection with tariff 299.
    # Will not be having any clip charge.
    # Local call 600 minutes free, after that the call rate is .40rs/min.
    # STD call rate is 2.40rs/min.
    # Roaming call rate is 3.5rs/min.
    # ISD call rate is 7.5rs/min.
    # 200 local SMS free, after that the rate is 50 paise/SMS.
    # National SMS rate is 2rs/SMS.
    # International SMS rate is 5rs/SMS.

    Calculation process: We have to calculate the phone bill for each candidate based the above conditions

    Example 1:
    Input:
    # char* candidateName ="Ram";
    # char* selectedTariff ="199" ;
    # char* isCorparate = "Yes";
    # double clipCharge = 0.0;
    # double hiddenCharge = 0;
    # int totLocalCalMinute =200;
    # int totSTDCalMinute = 80;
    # int totISDCalMinute= 50;
    # int totRoamingCalMinute= 100;
    # int totLocalSMS= 150;
    # int totNationalSMS= 25;
    # int totInternationalSMS= 5;.

    Output value is 985.0

    Explanation:

    In this example the calculaion of the bill is done as follows: The total local calls made is 200. Since this is a corporate tariff, 200 minutes of local call is free. So the total cost of the local calls is 0. Now the total STD calls made is 80 and the tariff is Rs 2 per min so the cost is 80*2 which is 160. The total ISD call made is 50 and the tariff is 7.5 rs per min. So the total cost for ISD calls is 7.5*50 which is 375 rs. The total number of roaming calls made is 100 and the cost for roaming is 3.75 rs per min. So the totoal cost for roaming calls is 100*3.75 which is 375 rs. Total number of local SMS sent is 150, but this being a corporate connection ad 200 local SMS is free. So the cost for local SMS is nill or 0. Total number National SMS sent is 25 and the tariff for national SMS is 2 rs. So the total cost for national SMS is 25*2 which is 50 rs. Total number of international SMS sent is 5 and the cost for international SMS is 5. So the total cost for International SMS is 5*5 which is 25. Now adding all these cost i.e 0+160+375+375+50+25=985 rs.

    Example 2:
    Input
    # char* candidateName ="Radha";
    # char* selectedTariff ="300" ;
    # char* isCorparate = "No";
    # double clipCharge = 60.0;
    # double hiddenCharge = 100;
    # int totLocalCalMinute =2000;
    # int totSTDCalMinute = 560;
    # int totISDCalMinute= 150;
    # int totRoamingCalMinute= 300;
    # int totLocalSMS= 950;
    # int totNationalSMS= 90;
    # int totInternationalSMS=45

    Output value is = 7571.4

    For C solutions
    Header File    :    PhoneBill.h
    Function Name    :    double getCalculatedBillAmount( char* candidateName, char* selectedTariff, char* isCorparate, double clipCharge, double hiddenCharge, int totLocalCalMinute, int totSTDCalMinute, int totISDCalMinute, int totRoamingCalMinute, int totLocalSMS, int totNationalSMS, int totInternationalSMS )
    Directory Name    :    phonebillcalc
    File Name    :    PhoneBill.c
    For C++ solutions
    Header File    :    PhoneBill.h
    Class Name    :    phone
    Function Name    :    double getCalculatedBillAmount()
    Directory Name    :    phonebillcalc
    FileName    :    PhoneBill.cpp
    Friday, May 25, 2007 2:00 PM
  • Very long question. Anyone got that question in the quiz ?

    If not, then may be someone from us can try out the solution for it.
    Friday, May 25, 2007 2:01 PM

All replies

  • Yes this is the last quiz, we should more be active....
    Saturday, May 19, 2007 3:10 PM
  •  

    I think, We should wait untill the end of quiz.

     

    We will discuss quiz que / ans from monday onwards.

    Saturday, May 19, 2007 4:15 PM
  • I respect your suggestion  Hiren. We will start the discussions from monday.
    Saturday, May 19, 2007 4:58 PM
  • Yeah, if we discuss the event now, whats the fun. Lets discuss after the quiz is Monday...
    Saturday, May 19, 2007 10:14 PM
  • Dear Friend,

     

    Greetings, respecting others is an great self respect too. Sure we can post our questions from monday and shall discuss the logic to simplify the code.

     

    regards,

    Sunday, May 20, 2007 3:35 AM
  • I am waiting fr monday to come Smile so that i can post the code immediately.

    Sunday, May 20, 2007 5:22 AM
  • Ya will post mine too on monday... the quiz was too easy...
    Sunday, May 20, 2007 9:42 AM
  • Patience guys, abhi delhi bahut khareeb hai....
    Sunday, May 20, 2007 1:08 PM
  • Well ,we will post our codes on monday.. but let us have many threads, else a single thread will become too long and will be difficult to browse thru...
    Sunday, May 20, 2007 4:52 PM
  • @Raghuram - No need for multiple threads about the same,. it will become harder for people to find stuffs. As per my experience, the quiz threads dont get too long, it hardly gets to 5 pages.

    And even if it gets, then we can start a new thread that can continue with this one. but having duplicates at the same time will create more confusion .
    Sunday, May 20, 2007 5:02 PM
  • Ok guys, its monday now. Start posting the quiz questions and solutions. To start with ill post my questions along with the answers.

    If anyone has done it with better logic, or think that the same program can be done in a better / easier, you guys are free to discuss them.
    Monday, May 21, 2007 4:10 AM

  • Bouncing Ball
    When a ball is pitched repeatedly, it loses its height with each bounce. Assuming that the height to which the ball bounces gets reduced by half after each pitch, you are required to find out the height of the bounce after 'n' pitches. Suppose, the initial height of the ball is 8 mts. After the first pitch its height would be 4 mts. After the second pitch its height would be 2 mts. The initial height of the ball is given as an input.
    The function attainHeight contains the following components defined as input

        * Initial Height
        * Pitch number.

    Write a program to find the height of the ball after its 'n' th pitch. (pitch number is given as an input)

    # Constraints The initial height will be a number of power of 2
    # The number of pitches will be < 30
    # The output will always be >=1

    Example 1:
    Input:
        int height = 2000
        int pitchnumber = 4

    Output: Returns 125

    Explanation: The height of the bounce gradually decreases into half of the previous height when it bounces. In this example as the ball is thrown from a height of 2000 for the first pitch its height gets reduced by half and the so the height will be 1000. In the next bounce it again gets reduced by half to 500. In the third pitch, its height will be 250 and in the fourth pitch its height will be 125. This figure 125 has to be returned as the output as the total number of pitches is 4.

    Example 2:
    Input:
        int height = 5450
        int pitchnumber = 7

    Output: Returns 42

    For C solutions
    Header File    :    BouncingBall.h
    Function Name    :    int attainHeight(int height, int pitchnumber)
    Directory Name    :    bouncingball
    File Name    :    BouncingBall.c
    For C++ solutions
    Header File    :    BouncingBall.h
    Class Name    :    ball
    Function Name    :    int attainHeight(int height, int pitchnumber)
    Directory Name    :    bouncingball
    FileName    :    BouncingBall.cpp


    #include "BouncingBall.h"
    #include<stdio.h>
    #include<math.h>

    int attainHeight(int height, int pitchnumber) {
            if(pitchnumber>=30 || pitchnumber<=-1) return 0;
        return (int)floor(height/pow(2,pitchnumber));
    }

    void dsmain() {
        printf("%d\n",attainHeight(2000,4));
        printf("%d\n",attainHeight(5450,7));
    }

    Monday, May 21, 2007 4:12 AM
  • Inter Prime

    Inter Prime
    Inter Prime are positive integers obtained by the following process: An interprime is the average of two consecutive odd primes. For example, 9 is an interprime because it is the average of 7 and 11.

    Constraints:
    All integers are positive
    Integers should be > 0 and < 100000
    Example 1
    Input:
    int from = 1
    int to = 20
    Output
         Returns 4, 6, 9, 12, 15, 18

    Explanation: odd primes of first two numbers is 3,5 so (3+5 = 8 and average of this two no is 4 i.e 8/2) like wise 5+7 = 12 /2 = 6, 7+11 = 18 /2 = 9, 11+13 = 24/2 = 12; 13+17 = 30/2 = 15, 17+19 = 36/2= 18.

    Example 2
    Input:
    int from = 120
    int to = 200
    Output
         Returns 120, 129, 134, 138, 144, 150, 154, 160, 165, 170, 176, 180, 186, 192, 195, 198,

    For C solutions
    Header File    :    InterPrime.h
    Function Name    :    int* getInterPrime(int from, int to)
    Directory Name    :    interprime
    File Name    :    InterPrime.c
    For C++ solutions
    Header File    :    InterPrime.h
    Class Name    :    interprime
    Function Name    :    int* getInterPrime(int from, int to)
    Directory Name    :    interprime
    FileName    :    InterPrime.cpp


    Monday, May 21, 2007 4:13 AM

  • Solution to the InterPrime Problem

    #include"InterPrime.h"
    #include<stdio.h>
    #include<stdlib.h>

    int isPrime(int no) {
        int i=0,flag=0;
        if(no==1 || no==2) return 0;
        for(flag=0, i=2 ; i<=no-1 ; i++) {
            if(no%i==0) {
                flag++;
                break;
            }
        }
        if(flag==0) return no;
        else return 0;
    }

    int getNextPrime(int no) {
        int i=0;
        for (i=no+1 ; ; i++ ) {
            if(isPrime(i)) return i;
        }
    }
    int *ans, cnt=0;
    int* getInterPrime(int from, int to) {
        int i,avg=0;
        cnt=0;
        if (!(from>=0 && to>=0 && to >=from)) return 0;
        ans=(int *)malloc(sizeof(int)*5000);
        for(i=1 ; i<=to+getNextPrime(to) ; i++) {
            avg = (i+getNextPrime(i))/2;
            if(isPrime(i) && avg>=from && avg<=to) {
                ans[cnt++] = avg;
                printf("%d  ",avg);

            }
        }
        return ans;
    }

    void dsmain() {
        int i=1, avg=0;
        int *arr;
        arr = getInterPrime(120 ,200);
        for(i=0 ; i<cnt ; i++) {
            printf("%d ",arr[i]);
        }
    }

    Monday, May 21, 2007 4:16 AM
  • Hey Harshil This were the same questions asked to me.. well, your code has many points to pin out.. but i will post my code and you can very well see what i wana say
    Just have a look at them and tell me how they are
    Monday, May 21, 2007 4:24 AM
  • This is the code for inter-prime problem
    Code Snippet


    #include<iostream>

    #include<math.h>
    #include<conio.h>

    using namespace std;
    int ans_size = 0;

    int* getInterPrime(int from, int to)
    {
         ans_size = 0;
         if(from<0 || to <0)
         {
                   cout<<"Sorry the numbers have to be positive";
                   return NULL;
         }
         if(!(from>0 && to<100000))
         {
                    cout<<"Sorry the range is not correct as per the standard";
                    return NULL;
         }
        int n, ncount=0, square, divisor, prime=1, number=from;
        int prime_no_array[(to-from)/2];
        if(number <3)
                  number = 3;
        else if(number%2 == 0)
            number++;
           
            while (number<=to)
            {
                square=sqrt(number); prime=1;
                divisor=3;
                while ((divisor<=square) && (prime))
                {
                    if (number%divisor==0)
                        prime=0;
                    else
                        divisor+=2;
                }
                if (prime)
                {
                    prime_no_array[ncount++] = number;
                }
                number+=2;
            }
        if(ncount<2)
                    return NULL;
                   
        int *ans;
        int i;
        ans = new int[ncount-1];
        for(i=0; i<ncount-1; i++)
        {
                ans[i] = (prime_no_array[i] + prime_no_array[i+1])/2;
        }
        ans_size = ncount-1;

        cout<<endl;
        return ans;
    }

    int main()
    {
         int from = 1, to = 20;
         int *ans = NULL;
        
         ans = getInterPrime(from, to);
         cout<<"Displaying the ans :"<<endl;
         for(int i=0; i<ans_size; i++)
                 cout<<ans[i]<<", ";
         cout<<endl;
        
         from = 120; to = 200;
         ans = getInterPrime(from, to);
         cout<<"Displaying the ans :"<<endl;
         for(int i=0; i<ans_size; i++)
                 cout<<ans[i]<<", ";
         getch();
         return 1;
    }


    Monday, May 21, 2007 4:26 AM
  • This is the code for bouncing ball problem...


    Code Snippet

    #include<iostream>
    #include<math.h>
    #include<conio.h>

    using namespace std;

    int attainHeight(int height, int pitchnumber)
    {
        if(height %2 != 0)
        {
                  cout<<"The initial height has to be power of 2";
                  return -1;
        }
        if(pitchnumber>29)
        {
                  cout<<"The pitch number has to be smaller than 30";
                  return -1;
        }
        int ans;   
        ans = height/pow(2,pitchnumber);
        if(ans <1)
               return 1;
        else
            return ans;

    }

    main()
    {
          int height = 2000, pitchnumber = 4, output;
          output = attainHeight(height, pitchnumber);
          if(output != -1)
                 cout<<output<<endl;
          height = 5450; pitchnumber = 7;
          output = attainHeight(height, pitchnumber);
          if(output != -1)
                 cout<<output<<endl;
          getch();
    }


    Monday, May 21, 2007 4:28 AM
  • Hey varun, i have seen your code. Seems good.

    Please point out the things that you feel isn't right in my code. so that i can know what i did wrong. One thing i didn't do is
        if(!(from>0 && to<100000))

    i purposefully didn't add that, because signed int's range cannot be more an +32767, so whats the reason in checking for that condition, even if anyone enters that number it will be stored as a different number. So no use of checking that condition unless we have the parameters as LONG. but in the given function prototype its int, so i just ignored it.
    Monday, May 21, 2007 6:08 AM
  • Well you are talking about the 32 bit computers.. what if the computer is a 64 bit ????
    And most of all, they have put the constraint, so i had put it.. but anyways nice observation...
    I will post some of the points in observed in your code...
    Monday, May 21, 2007 6:47 AM
  • 1.) The calculation of prime number can be optimised, 1st of all no even number greater than 3 is prime, so why to check for those numbers?? So instead of i++, you could have i+2, and skip all the even number.. it will reduce the number of operations to half..

    2.) There is a theorem which states that if the number is not divisible by any number less than or equal to the square root of the given number, the given number is a prime..
          So when you are checking for the prime number, dont need to check till n-1, just check it till sqrt(n)...  this will again reduce the number of operations not just by half, but eponentially...
    i.e. if you are checking lets say for 36, you need to check it with numbers till 6 only.. same way for lets say 41, check it till 7 only....

       So you see how you can optimize the prime number calculation....

    3.) No need to have a globle variable for *ans, because the function will return the value of the new address...

    4.) Why to create so large space for the array size.. create a temp array and , then count the size and create an array of that size.. it will save a lot of memory space....

    Have a look at my code after this points, and you will see the difference...
    Monday, May 21, 2007 7:06 AM
  • This was a simple code, so there is not much problem.. just that in case of Error, you should have not displayed the value 0, so you could have checked that in the code.. also there is a constraint saying that in any case the outcome is >=1, so if the function comes to value lets say .25 or .125, then it should return 1 instead of 0, so you could have taken care of it..


    Well there was one point in the question i was not able to understand.. they said that the height will be of power of 2, but that was not the case in both the examples.. so i assumed it to be even initially.. wat do u say on it???
    Monday, May 21, 2007 7:26 AM
  • @Varun - Good notice.

    Ill clear out all the points that you pointed out.

    Ponint#1 : I had i+=2 earlier in my code, but i did a bit of modifications, i had put isPrime(i++) so , because of that i changed the i+=2 in that for loop to i++. and then somehow i changed the code, and forgot to change it back. Good notice Varun. You won on me in this one

    Point #2 : I knew that there is no need to check uptil n-1. Instead of calling sqrt in the for loop everytime,  it would be useless, i could have used a variable instead string the sqrt(n) before the lop, but i didn;t want to increase the line of the code. As Devsquare check that also . And to be frank, even though i knew that theorem, that sqrt thing didn;t didn't come into my mind while i was coding Stick out tongue  You win on me in this one too...

    Point#3. I know i wasted lots of memory, and used that *ans outside the function which was not at all required, but i did that purposefully, because sometimes gcc compiler raises an error when you return a  address of a variable that is declared within the function. I could have used static variable instead, but it would have the same effect as i did by declaring it outside, as static variables lives during the entire execution of the program.  I know that i could have done it as you did by creating an array, and then doing it. But i thought why do things twice, and copy from one array to the other. I just let it go.

    And about that return 0 thing,  i guess what i did was right, cause it would return 0 if anything was invalid. The contraint written was >=1 should be the output. and and anything less than 1 bounces is not a  complete bounce, so i returned 0. I hope the admins understand my points while checking the program.

    Varun good job once again . Thanks for letting me know what was missing in my code, and how i could optimize it. This is how  we learn from the mistakes Smile
    Monday, May 21, 2007 7:54 AM
  •  

    @Varun

    In your interprime program,

    Enter the input 120 and 200.

     

    Now in the question. example gives output like 120,129....

    but your program gives output 129,.... not 120.

    Monday, May 21, 2007 8:34 AM
  • Hey guys tell me whether my code is right?????

    #include<stdio.h>
    int* getInterPrime(int from, int to)
    {
    int arr[100];
    int k=0,i,c=0,a,sum,x,y,m;
    for(m=from;m<to;m++)
    {  c=0;
        for(i=1;i<=m;i++)
        {
         a=m%i;
         if(a==0)
              c=c+1;   
        }
        if (c==2)
        { 
             if((m%2)!=0)
             {
                arr[k]=m;
                k++;
             }
        }
    }
    for(x=0;x<(k-1);x++)
    {
         sum=arr[x]+arr[x+1];
         printf("%d ",sum/2);
    }
    }
    void dsmain()
    {
    getInterPrime(1,20);
    }
    Monday, May 21, 2007 9:38 AM
  •  Hiren_Kotadiya_0230fd wrote:

    @Varun

    In your interprime program,

    Enter the input 120 and 200.

    Now in the question. example gives output like 120,129....

    but your program gives output 129,.... not 120.


    Hiren Observe it closely and you will see that the example given is itself wrong.. you are starting from the number 120 and you are giving an average between 2 numbers.. so even if you select 120 and 121 and give its average, it is going to be 120.5 so in no case you could get 120 as the output.. so you see the example was not correct.. the numbes start with 129, 120 is a printing mistake
    Monday, May 21, 2007 10:46 AM
  • Well buddy to be honest.. your code is not impressive, you did only half the work.. or even less... you are only displaying the answer.. but you are suppose to create a array of this answers and return it.. that is the reason that the function is having a return type as int *

    So the things you are displaying, store it in an dynamic array and return the address of that array... Other points are optimization which i had given above..
    Monday, May 21, 2007 10:55 AM
  • @Varun

    They wanted to print interprime numbers between that range, and not the internumbers that lie between the average of the two numbers between the range that you specified.

    Here is more info for your clarification

    Prime   NextPrime   Average (InterPrime Numbers)
    113     127             120
    127     131             129
    131     137             134
    137     139             138
    139     149             144
    149     151             150
    151     157             154
    157     163             160
    163     167             165
    167     173             170
    173     179             176
    179     181             180
    181     191             186
    191     193             192
    193     197             195
    197     199             198

    I hope you get it now.


    Monday, May 21, 2007 10:55 AM
  • @Mohan - I haven't tried executing the code, but what i have noted immediately and what Varun mentioned, that you have to return the output as an pointer to the array, which you have not done. You need to store it in the array, and then return an address to it.

    Varun has explained in more detail how to do it.
    Monday, May 21, 2007 10:58 AM
  • Ohh, that makes sense.. those people should have specified it.. they just gave the range but did not specifically said that range is for what...
    I had even asked the person on live help.. and he said that dont consider 120.. what kind of help centre they have....


    Still nice observation harshil
    Monday, May 21, 2007 11:01 AM
  • Dear Friends,

     

    Greetings, check out this code.

     

    Create a file named Interprime.c

     

    #include<stdio.h>

    #include<math.h>

    #include<InterPrime.h>

     

    void dsmain(){

            int *pb,i;

            pb=getInterPrime(0,100000);       

            for(i=0;i<317;i++){

                printf("%d\n",pbIdea);

            }

    };

     

    int chkPrime(int n){

         int i;

         if(n<=2)

             return 0;

         if(n%2==0)

             return 0;

         for(i=3;i<=sqrt(n);i++)

             if(n%i==0) return 0;

         return 1 ;

    };

     

    int getPrime(int a[],int from,int to){

          int i,j=0;

          for(i=from;i<=to;i++){

              if(chkPrime(i)==1) a[j++]=i;

           }

           return  j;

     };

     

     int* getInterPrime(int from, int to){

         int a[317],b[317],i,l;

          int *pb=b;

         l=getPrime(a,from,to);

        

         for(i=0;i<l-1;i++)

             bIdea=(aIdea+a[i+1])/2;

         return pb;

     };

     

    Header file Code:

     

    void dsmain();

     int chkPrime(int n);

     int getPrime(int a[],int from,int to);

     int* getInterPrime(int from, int to);

    Monday, May 21, 2007 1:26 PM
  • There were problems in the questions about not clearly mentioning the range for what was asked. Even when i first solved the problem, mine had 120 excluded, then i checked the sample solution, and then came to know that they wanted interprime numbers of that range.
    Monday, May 21, 2007 1:59 PM
  • Fredrick 1st off all please use the code block to put ur code in the forums.. it gets hard to read them...
    Now you made the same mistake as most of us... the range problem.. which i think only Harshil was able to observe...
    There are some points where it can be optimized.. 1st calculate the sqrt and put it into a variable.. you are calculating sqrt at each check, which is too overhead...
    2nd No even number is prime, so why check those numers for prime..
    3rd you have fixed the size of the array which is going to be returned, it can change.. lets say in 1st example you have only 6 interprime, for that you are giving a space of 317 integers....
    4th the array you are returning is a local member which will get distroyed.. so you wont be able to retain the answer after the function call.. hence most of the time there will be garbage...

    Monday, May 21, 2007 4:05 PM
  •  

    Yes Harshil,

    I had faced same problem.

    I found it after doing program then I modified my program.

    Monday, May 21, 2007 5:58 PM
  • @ Varun -when i executed your code in TC , it shows an error at the line

    int prime_no_array[(to-from)/2];
     It says "constant parameter required as array subscript"

    Tuesday, May 22, 2007 1:35 AM
  • Altered Sentence Problem

    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>       
    # include "AlterSentence.h"

    char* getAlteredSentence(char* fullSentence,char* findWord,char* replaceWord);

    void dsmain()
    {
        char *fullSentence = "Honestly are the best policy";
        char *findWord = "are";
        char *replaceWord = "is";
        char *temp = getAlteredSentence(fullSentence,findWord,replaceWord);
        printf("%s",temp);
    }
       
    char* getAlteredSentence(char *fs,char *fw,char *rw)
    {
        char *a;
        char *words[100];
        int w = 0;
        int final_round;
        int sentence_len;
        char word[100];
        int temp=0,temp1,temp2=0;
        strcpy(word,"");
        sentence_len=strlen(fs);
           
        while(temp2<=sentence_len)
        {
            if(fs[temp2]==' ')
            {
                word[temp]='\0';
                a=(char *)malloc(strlen(word)+1);
                strcpy(a,word);
                words[w]=a;
                w++;
                temp=0;
                temp2++;
            }
            else
            {
                word[temp++]=fs[temp2];
                temp2++;
            }
        }
        word[temp]='\0';
        a=(char *)malloc(strlen(word)+1);
        strcpy(a,word);
        words[w]=a;
        temp1=w;
        temp=0;

        while(w>-1)
        {
            if(strcmp(words[w],fw)==0)
            {
      words[w]=rw;
     }
     w--;
        }
        strcpy(fs,"");
        final_round=0;
        while(final_round<temp1)
        {
            strcat(fs,words[final_round]);
            strcat(fs," ");
            final_round++;
        }
        strcat(fs,words[final_round]);
        return fs;
    }

     

     

    Tuesday, May 22, 2007 1:46 AM
  • Buddy, I just checked it.. I had made the prog in Bloodshed Compiler.. it has a very much resemblance with the Devsquare compiler.. that is the reason it doesnot show any error in it and neither at the Devsquare.. I also checked it in VC++ and yes it does show an error their..

    What you can do in this case is declare a pointer variable of prime_no_array and dynamically allocate the memory... it should work then.. but remmber to free the memory for it before leaving the function...
    Tuesday, May 22, 2007 3:35 AM
  • Have a look at this code for the similar problem...


    Code Snippet

    #include<iostream>
    #include<string>
    #include<conio.h>

    using namespace std;
    char* getAlteredSentence(char* sentence,char* find,char* replace)
    {
          string s(sentence);
          char *tmp;
          tmp = new char[s.length() + 100];
          int location;
          location = s.find(find);
          while(location != -1)
          {
                         s.replace(location,strlen(find),replace);
                         location = s.find(find);
          }
          int i;
          for(i=0; i<s.length(); i++)
          {
                  tmp[i] = s.at(i);
          }
          tmp[i] = '\0';
          return tmp;
    }
    main()
    {
          char s1[]=("Do not worry about your difficulties in Mathematics. I can assure you mine is still greater.");
          char *s2;
          s2 = getAlteredSentence(s1, "is", "are");
          cout<<s2;
          getch();



    The code is short and sweet, depicting the power of C++, also i had to make the conversion from the string class to a Char *, so had to do extra work, or else if would have been even shorter... So how is it???
    Tuesday, May 22, 2007 3:51 AM
  • @Varun - Good code man.

    Guys, please post the other questions that you guys got. We are free now, we can try solving it during the vacations.
    Tuesday, May 22, 2007 4:56 AM
  • @Raghuram - The error you get is normal in TC and VC++.  You will get that error if you use non constant variables/values  to allocate the array size. Try using gcc compiler, or as varun said use the Bloodshed compiler. They support to allocate dynmanic memory to the arrays and pointers.

    But for just the purpose of testing the code, replace
    int prime_no_array[(to-from)/2];
    with

    int prime_no_array[SOME_CONSTANT_VALUE];
    and it will work.
    Tuesday, May 22, 2007 5:11 AM
  • Ok i think noone is interested in posting their questions. Have you guys been affected by the admins announcement ? Guys that was for the people who posted the questions before the quiz ended. The quiz has ended , and now i think there is no problem in discussing it. Guys lets continue. The show must go on.
    Tuesday, May 22, 2007 4:27 PM

  • Magical Card

    Magical card problem is a simple card game where you can select a card from the given card matrix arranged in 3*3 array. The program should perfom two shuffling and at the end of two shuffling, the card that you have selected should appear in the middle of the matrix i.e the row r2 and column c2 should contain the card that you have selected. :

    Description: You will be given a 3 by 3 matrix of cards. The cards will be arranged in 3 rows and 3 columns. What you need to do is, to select any one card from the list of matrix and keep it in mind. You should not disclose the card that you have selected. You will be asked to tell in which column does the card (the card that you selected initially) falls. After that the cards will be taken and arranged once again in 3 by 3 matrix. Again for the second time you will be asked to tell in which column does the card (the card that you selected initially) falls. Based on the input that you have provided the system will calculate and generate the output. The system generated output will be the card that you selected initially.

    The methods used in this program are getFirstShuffleResult, getSecShuffleResult and getFinalResult. The input value for the first method getFirstShuffleResult is an 3 by 3 matrix and the column number("C1", "C2", "C3") does the card falls. getFirstShuffleResult method is applied once for shuffling the matrix as per the description mentioned above. getSecShuffleResult method is applied again for shuffling the matrix as per the description mentioned above. The third getFinalResult uses the 3 by 3 matrix which is the result of the second method and returns the magical card.

    # Calculation process: Initially you will be provided with a 3 by 3 matrix and you be asked to select any one card from the card matrix.
    # You will be asked to tell in which column the card falls.
    # If the card falls in first column, then first take the cards from the second column and make it as first row and then take the cards from first column and arrange it in the second row and then the third column cards and then arrange it in the third row.
    # If the card falls in second column, then first take the cards from first column and place it in the first row and then take the cards from second column and arrange it in the second row and then third column cards and arrange it in the third row.
    # Similarly if the card falls in third column, then first take the cards from second column and arrange it in the first row and then arrange the third column in the second row and then arrange the first column cards in the third row.
    # Now you will be having the cards arranged one above the other.
    # Again arrange the cards in 3 by 3 matrixes.
    # Again the same procedure will be followed from step 2 to step 5.
    # Now you will be having the cards arranged one above the other.
    # Again arrange the cards in 3 by 3 matrixes.
    # Again the same procedure will be followed from step 2 to step 5.
    # Now the card that you selected initially will be present exactly in the middle of the matrix.
    # Extract the card from the middle of the matrix. And this will be the outputted result and the card that you selected.

    Constraints
    No assumptions to be made.
    Consider that all the inputs are given.
    Card numbers will fall between 0 to 100

    Example 1:
    Input:
    char* firstShuffleColPos= "C1"
    char* secShuffleColPos= "C2"
    int** cardList = { {12, 5, 37},
                                   {23, 67, 3},
                                   {3, 36, 56} };

    output for first shuffle: int** firstShuffle={ {5, 67, 36},
                                                                     {12, 23, 3},
                                                                     {37, 3, 56} };

    output for second shuffle: int** secShuffle={ {5, 12, 37},
                                                                          {67, 23, 3},
                                                                          {36, 3, 56} };

    Output : getFinalResult function returns = 23

    In this example, the card selected is in the column C1 and after the first shuffle the card selected in coulmn C2. As per the descrption since the card is in the C1 column the first shuffle will be done as follows: first take the cards from second column and then take the cards from first column and then third column cards and arrange in the rowwise. This will result in the arrangement depicted in example 1 output for first shuffle. Again the input for the second shuffle is C2. So by following the same method the resultant matrix is as per the arrangement shown in the example 1 output for second shuffle. So after shuffling again the output for thrid function is as depicted in example 1. Now looking at this matrx the card selected is 23.

    Example 2:
    Input
    char* firstShuffleColPos= "C3"
    char* secShuffleColPos= "C1"
    int** cardList = { {5, 56, 99},
                                   {1, 45, 30},
                                    {6, 69, 23} };

    output for first shuffle: int** firstShuffle={ {56, 45, 69},
                                                                     {99, 30, 23},
                                                                     {5 , 1 , 6} };

    output for second shuffle: int** secShuffle={ {45, 30, 1},
                                                                          {56, 99, 5},
                                                                          {69, 23 ,6} };


    Output : getFinalResult function returns = 99

    For C solutions
    Header File    :    MagicalCardGame.h
    Function Name    :    int** getFirstShuffleResult(int** cardList, char* firstShuffleColPos),
    int** getSecShuffleResult(int** firstShuffle, char* secShuffleColPos),
    int getFinalResult(int** secShuffle)
    Directory Name    :    magicalcardgame
    File Name    :    MagicalCardGame.c
    For C++ solutions
    Header File    :    MagicalCardGame.h
    Class Name    :    cards
    Function Name    :    int** getFirstShuffleResult(int** cardList, char* firstShuffleColPos),
    int** getSecShuffleResult(int** firstShuffle, char* secShuffleColPos),
    int getFinalResult(int** secShuffle)
    Directory Name    :    magicalcardgame
    FileName    :    MagicalCardGame.cpp

    Tuesday, May 22, 2007 4:28 PM

  • NewsPaper Problem

    In this problem a paragraph has to be modified such that it can be printed in newspaper columns. Newspaper articles are written in fixed length columns (of size 10 ) and text has to be modified in such a way that all lines have fixed number of characters except perhaps the last line.
    # The function newspaperLastline contains the String as components defined and the inputs given, char* text
    Write a program to return the number of characters in the last line of the printed text.
    # Constraints: Split words need not be hyphenated.
    # A space is also considered as a character.

    Example 1:
    Input:
        char* test = "The Stonehenge today stands insulated on a separate plane of existence; all its worldly functions duly given."

    In a column of length 10 this text should be written as:
    The Stoneh
    enge today
      stands in
    sulated on
      a separat
    e plane of
     existence
    ; all its
    worldly fu
    nctions du
    ly given.

    Output: Returns 9 (length of "ly given.")

    Explanation: Here the last line is taken into consideration after aligning the sentence as per the column length of 10 including the spaces that occur in the sentence. The output returns 9 as there are 9 characters including the space.

    Example 2:
    Input:
        char* test = "You can login in DevSquare from anywhere over the Internet and compile and execute code without needing a compiler / execution engine on your local machine."
    In a column of length 10 this text should be written as:
    You can lo
    gin in Dev
    Square fro
    m anywhere
      over the
    Internet a
    nd compile
      and execu
    te code wi
    thout need
    ing a comp
    iler / exe
    cution eng
    ine on you
    r local ma
    chine.

    Output: Returns 6 (length of "chine.")

    For C solutions
    Header File    :    NewsPaperPrint.h
    Function Name    :    int newspaperLastline(char* text)
    Directory Name    :    newspaperprint
    File Name    :    NewsPaperPrint.c
    For C++ solutions
    Header File    :    NewsPaperPrint.h
    Class Name    :    newspaper
    Function Name    :    int newspaperLastline(char* text)
    Directory Name    :    newspaperprint
    FileName    :    NewsPaperPrint.cpp
    Tuesday, May 22, 2007 4:29 PM
  • anyone who has got the above questions in their quiz ? Try posting your code so that we can have an idea. If noone posts, then ill try solving the questions and post it when i get time.
    Tuesday, May 22, 2007 4:31 PM

  •     long price=(long)pric;
        char str[15];
        long i=100000;
        int j=0;

        long n=price/i;
        price=price%i;

        if(n>0)
        {
            int k,l;
            k=n/10;
            l=n%10;

            if(k>0)
            {
                str[j]=k+48;   j++;
                str[j]=l+48;   j++;
                str[j]=',';    j++;
            }
            if(k==0)
            {
                str[j]=l+48;   j++;
                str[j]=',';    j++;
            }
        }
        i=i/100;

        n=price/i;
        price=price%i;

        if(n>0)
        {
            int k,l;
            k=n/10;
            l=n%10;

            if(k>0)
            {
                str[j]=k+48;   j++;  
                str[j]=l+48;   j++;
                str[j]=',';    j++;
            }
            if(k==0)
            {
                str[j]=l+48;   j++;
                str[j]=',';    j++;
            }
        }

        int k,l;
        n=price/100;
        price=price%100;

        str[j]=n+48;    j++;

        k=price/10;
        l=price%10;

        str[j]=k+48;    j++;
        str[j]=l+48;    j++;
     
        str[j]='\0';

        cout<<str;

    Tuesday, May 22, 2007 6:08 PM
  • Man if possible mention the question for the code that you posted. So that one can know for what question the code is.
    Different participants have got different questions in the quiz, so many of them might not know for which question you posted the solution.
    Tuesday, May 22, 2007 6:24 PM
  • Harshil I think there were not too many questions this time.. just have a look how many people got the interprime question... So I think we are out of questions this time.. that is the reason people are not posting questions...
    Tuesday, May 22, 2007 6:34 PM
  • I have modified the above program to use a sting class so that i can attach the program in my C++ asignment file.. but as i have shown earlier.. you can convert the output from the string class object to a simple string....

    so here goes one more powerful example of the String Class of C++

    Code Snippet

    #include<iostream>
    #include<string>
    #include<conio.h>

    using namespace std;

    int newspaperLastline(string &text)
    {

        int length = text.length();
        int i;
        for(i=1; i<=length/10; i++)
        {
                 text.insert(i*10 + i - 1, "\n");
        }
        return (length%10);
    }

    main()
    {
         string s1("The Stonehenge today stands insulated on a separate plane of existence; all its worldly functions duly given.");
         string s2("You can login in DevSquare from anywhere over the Internet and compile and execute code without needing a compiler / execution engine on your local machine.");
        
         cout<<"The size of last line : "<<newspaperLastline(s1)<<endl;
         cout<<s1<<endl;
         cout<<"The size of last line : "<<newspaperLastline(s2)<<endl;
         cout<<s2;
         getch();
         return 1;
    }


    Wednesday, May 23, 2007 4:20 AM
  • I agree with you this time about the less number of questions being asked in this month' quiz.

    My friends who got different quiz questions are enjoying vacation, they dont got internet access at home, so cant get the questions from them. But i am sure many of the students might have gone to my college and attended the quiz. I might go to the college in a day or two, and try to check if anyone has saved the questions or not. If so ill post it Smile
    Wednesday, May 23, 2007 5:42 AM

  • @Varun - I think there will be a wrong answer in your above code. What if the string has exactly 20 characters ? then the last line will contain 10 characters. while your code will return 0.


    int newspaperLastline(char* text) {

    int lastlen;

    return (lastlen=strlen(text)%10, lastlen==0)?10:lastlen;
    }



    I think this will work properly. I have directly written the code, without any compilation or testing. But i guess this is enough to discuss the logic.


    Wednesday, May 23, 2007 6:24 AM
  • hmmm.. Actually I didnt had an idea of what to do in that case.. well as it was not mentioned, i assumed that we had to return 0 only.. but ya we could very well do that.. and also, what i did extra in my code is that i had insterted '\n' at every 10th postion.. so now whenever you print the line.. it will be printed in the required way... Though it was not mentioned to specifically do it, i had assumed it...
    Wednesday, May 23, 2007 11:10 AM
  • Anyways, the logic aint much important. The point that you wanted to make was the  illustration of the solution using the power of C++ using the string class. And that was really a good one. It decreased the amount of code  using that   string.insert()   Smile
    Wednesday, May 23, 2007 12:37 PM

  • //C PROGRAM TO CALCULATE THE TOTAL COST TO CONSTRUCT AN AQUARIUM

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

    void dsmain()
    {

    //VARIABLE DECLARATION

        double total_cost;
        int Length = 20;               // int Length = 10;
        int Breadth = 15;              // int Breadth = 5;
        int Height = 7;                // int Height = 6;
        char* Thickness = "4mm";       // char* Thickness = "8mm";
        double LaborCharge = 650;      // double LaborCharge = 750;
       
    //FUNCTION CALL TO GETCONSTRUCTED PRICE, BY PASSING INPUT PARAMETERS   
        total_cost=getConstructionPrice(Thickness,LaborCharge,Length,Breadth,Height);
        

    //PRINTING THE CALCULATED TOTAL COST RETURNED BY THE FUNCTION
        printf("Total Cost = %lf",total_cost);
    }

     

    //FUNCTION getConstructionPrice

    double getConstructionPrice(char* thickness, double laborCharge, int length, int breadth, int height)
    {
    //LOCAL VARIABLE DECLARATION TO THE FUNCTION   
        double totalcost;
        int glass_cost;

    //CHECKING THE GLASS THICKNESS
        if(strstr(thickness,"4mm")) glass_cost=20;
        else if (strstr(thickness,"6mm")) glass_cost=40;
        else if (strstr(thickness,"8mm")) glass_cost=80;   
        else if (strstr(thickness,"10mm")) glass_cost=120;

    //CALCULATING TOTAL COSTS
         totalcost =(( (length*height*2) + (breadth*height*2) ) * glass_cost ) + laborCharge ;


    //RETURNING THE CALCULATED TOTAL COST
         return(totalcost);
    }

    Wednesday, May 23, 2007 3:15 PM
  • hi guys, i have noticed couple fo things here

     

    1. quiz questions were different

    2. i thought we were suppose to code using the same variable names as provided in the question and by the quiz, but i have noticed many members, use their own variables in the program....??? discuss and comment

    Wednesday, May 23, 2007 3:17 PM
  • Well Anoop, the only criteria was to keep the function name same.. we can have any variable name inside...
    The reason i think was because we only need to know the name of the function to call it.. then the variable name inside the function is not required to know.. so no problem in it...
    Wednesday, May 23, 2007 4:16 PM
  • @Anoop - Nice code. I liked the way you used strstr(). But i guess it would have been better to use strcmp(), but i dont know what the question is, so cant comment more on it. I think the reaosn why you put strstr() is that may be the user can enter "4mm" in any part of the string may be also include spaces before or after. not sure. Please explain your reason .

    If you have the question of that quiz question, then it would be nice of you if you post the question also.
    What was the other quiz question you got ? post it also.
    Wednesday, May 23, 2007 4:42 PM
  • thanks varun for clearing it out ....however since this was my first quiz and didnt have much experience so i decided to be on the safer side rather and follow ike mama' sboy ...lol
    Thursday, May 24, 2007 11:08 AM
  • hi harshil,
    i used strstr , so that , if there are any case differences then it will still work, and strstr also completes the task of strcmp, meaning to say that, even if both the strings are equal and you use strstr, it will still work the way desired.
    In this program you can use strcmp without any flaws because , we are not having any input from the STDIN, so there will not be any case differences as we are directly providing the values in our program itself....[avoiding the use of scanf]....

    but if we are using scanf or cin or anyother STDIN, than strstr would be a better option...

    But while coding it came naturally to me, to avoid that error....and so used strstr...

    anyways am trying to find, if i can get that question, i will post it as soon as i find it...
    Thursday, May 24, 2007 11:15 AM
  • Nice thinking... keep it up...
    Thursday, May 24, 2007 11:27 AM
  • hi varun, even u guys have coded really well....good one man...
    Thursday, May 24, 2007 12:09 PM
  • hi
    harshil heres the first question

    Code Snippet

    AlteringSentenceProblem is a problem which returns an altered sentence. Initially a sentence will be given with multiple words. Later we prefer to change some of the words in that sentence. The objective of this problem is to find the word that needs to be replaced in the sentence and replace it with new word given. The final output should be the full sentence with all replaced words.The following are the inputs given based on which output has to be generated:

    Full sentence with more than one word
    Which word to replace
    new word that will replace the old word

    Based on these inputs you have to generate the final sentence.

    Constraints:

    No assumptions to be made.

    Consider that all the inputs are given.

    Example 1:
    Input
        char* FullSentence = "Do not worry about your difficulties in Mathematics. I can assure you mine is still greater."
        char* FindWord = "is"
        char* ReplaceWord = "are"

    Output : Returns "Do not worry about your difficulties in Mathematics. I can assure you mine are still greater."

    Example 2:
    Input:
        char* FullSentence = "Yo will never be happy if Yo continue to search for what happiness consists of. Yo will never live if Yo are looking for the meaning of life."
        char* FindWord = "Yo"
        char* ReplaceWord = "You".

    Output: Returns "You will never be happy if You continue to search for what happiness consists of. You will never live if You are looking for the meaning of life."

    For C solutions 
    Header File : AlterSentence.h
    Function Name : char* getAlteredSentence(char* fullSentence,char* findWord,char* replaceWord)
    Directory Name : altersentence
    File Name : AlterSentence.c
    For C++ solutions 
    Header File : AlterSentence.h
    Class Name : sentence
    Function Name : char* getAlteredSentence(char* fullSentence,char* findWord,char* replaceWord)
    Directory Name : altersentence
    FileName : AlterSentence.cpp


    General Instructions:     * The directory , file / class names, functions, method signatures, header files to be used are mentioned in the problem statement. Do not use your own names or change the method signatures and fields. You can add any number of additional methods.
     
        * First add the directory in the Main Program Directory and then add the particular file in that directory. Do not forget to mention the file extension, either .c or .cpp as the case maybe.
     
        * For C solutions, change the value of "C_OR_CPP" macro in header file as 1 and for C++ solutions change the value as 2.
     
        * Incase of iostream.h specify as iostream only.  



    Thursday, May 24, 2007 12:11 PM

  • Code Snippet
    Solution to the aletring sentence problem


    /*
    C PROGRAM TO ALTER THE SENTENCE, ACCORDING TO THE GIVEN SUBSTRING, AND REPLACING THE GIVEN SUBSTRING BY A NEW STRING, AND THEN PRINTING THE NEW STRING[FULL]
    */

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


    void dsmain()
    {
    //VARIABLE DECLARATION

        char* FullSentence = "Yo will never be happy if Yo continue to search for what happiness consists of. Yo will never live if Yo are looking for the meaning of life.";
        char* FindWord = "Yo";
        char* ReplaceWord = "You";      
        char* ptr = " ";


    //FUNCTION CALL TO AlteredSentence FUNCTION BY PASSING FULLSENTENCE AND THE WORD TO REPLACE
        ptr=getAlteredSentence(FullSentence,FindWord,ReplaceWord);

    //PRINTING THE FINAL ALTERED OUTPUT SENTENCE
        printf("%s",ptr);

    }


    char* getAlteredSentence(char* fullSentence,char* findWord,char* replaceWord)
    {   
    //LOCAL VARIABLE DECLARATION TO THE FUNCTION
        char* temp;
        char* alteredString = " ";

    //LOGIC FOR ALTERATION, TOKENIZING THE STRING
        temp = strtok(fullSentence," ");
       
        while(temp != NULL)
        {

            //printf("Enter while");

            if(strcmp(temp,findWord)== 0)
            {
                //printf("enter");
                strcat(alteredString,replaceWord);
            }
            else
                strcat(alteredString,temp);


            temp = strtok(NULL, " ");

        }
       
    //RETURNING THE NEW ALTERED STRING
        return(alteredString);
    }



    Thursday, May 24, 2007 12:13 PM

  • Code Snippet
    Second question : - aquarium constructor problem
    Aquarium Construction Problem is a problem which calculates the total cost required to construct an aquarium. The construction cost is calculated based on different constraints. The objective is to find the total cost required to create an aquarium. The length, breadth and height of the aquarium will be given in feets. The cost of the glass used to construct an aquarium depends on the thickness of the glass. The price of the glass is fixed per square feet. Square feet for the glass with 4mm thickness will be different from 6mm thickness glass and so on. The following are the standard price for glass per square feet based on the thickness.

    4mm glass – 20Rs/SqFt
    6mm glass – 40Rs/SqFt
    8mm glass – 80Rs/SqFt
    10mm glass – 120Rs/SqFt
    The labor charge varies from each constructor.

    The getConstructionPrice function contains the following components as input
    1. Thickness of glass
    2. Labor charge for constructing the aquarium
    3. Length of the aquarium
    4. Breadth of the aquarium
    5. Height of the aquarium

    Based on these inputs, you have to calculate the total cost required for constructing an aquarium. Total construction cost will be the cost of the glass and the cost of labor charge.

    Constraints:

    No assumptions to be made.

    Consider that the thickness of the glass and labor charge will be given as the input.

    Example 1:
    Input
        int Length = 20
        int Breadth = 15
        int Height = 7
        char* Thickness = "4mm"
        double LaborCharge = 650

    Output: Returns 10450.
    Explanation : First calculate the total Square Feet of the Aquarium with the 20 feet length, 15 feet breadth and 7 feet height. The total length will be 280 square feet. The total breadth will be 210 square feet, therefore total square feet of the aquarium is 490 square feet. The cost for 4mm thickness aquarium is 20rs, hence 490 * 20 = 9800. Labour charge is 650rs. Therefore the total cost for constructing the aquarium construction is 9800 + 650 = 10450.

    Example 2:
    Input:
        int Length = 10
        int Breadth = 5
        int Height = 6
        char* Thickness = "8mm"
        double LaborCharge = 750

    Output: Returns 15150.

    For C solutions 
    Header File : AquariumConstruction.h
    Function Name : double getConstructionPrice(char* thickness, double laborCharge, int length, int breadth, int height)
    Directory Name : aquariumconstruction
    File Name : AquariumConstruction.c
    For C++ solutions 
    Header File : AquariumConstruction.h
    Class Name : aquarium
    Function Name : double getConstructionPrice(char* thickness, double laborCharge, int length, int breadth, int height)
    Directory Name : aquariumconstruction
    FileName : AquariumConstruction.cpp


    General Instructions:     * The directory , file / class names, functions, method signatures, header files to be used are mentioned in the problem statement. Do not use your own names or change the method signatures and fields. You can add any number of additional methods.
     
        * First add the directory in the Main Program Directory and then add the particular file in that directory. Do not forget to mention the file extension, either .c or .cpp as the case maybe.
     
        * For C solutions, change the value of "C_OR_CPP" macro in header file as 1 and for C++ solutions change the value as 2.
     
        * Incase of iostream.h specify as iostream only.  




    Thursday, May 24, 2007 12:15 PM
  • hi guys, i have already posted the solution to the second question, so am not posting it again, anyways

    the quiz was quite simple.....


    Thursday, May 24, 2007 12:17 PM
  • Thanks Anoop for posting for posting the questions. I think i dont need to say thanks anymore to you cause we have become good friends Smile

    Anyways, now i understand why you used that strstr, good strategic way of thinking Smile Also now i understand that aquarium code, hehe.

    Ill go to college tomorrow, and try to fetch some more quiz questions if i can Smile
    Thursday, May 24, 2007 6:03 PM
  • definitely harshil, no sorry and thnaks in friendship....

    anywayz do post the questions, when you get some....
    Friday, May 25, 2007 12:42 PM

  • Phone Bill

    Phone Bill
    Phone bill calculation program basically focuses on calculating the phone bill based on various criteria and tariff selected. In this problem there are four different tarrifs available and based each tarrif you are required to calculate the bill. The function or method used in this program is getCalculatedBillAmount. This method uses candidateName, selectedTariff, isCorparate, clipCharge, hiddenCharge, totLocalCalMinute, totSTDCalMinute, totISDCalMinute, totRoamingCalMinute, totLocalSMS, totNationalSMS, totInternationalSMS as input value.

    The four tarrifs available are:
    # Corporate tariff 199
    # Corporate tariff 249
    # Corporate tariff 299
    # Non Corporate tariff 300

    Procedure for calculating the phone bill for non corporate connection:
    # Will be having the clip charge.
    # Local call rate is 1rs/min.
    # STD call rate is 2.69rs/min.
    # Roaming call rate is 4rs/min.
    # ISD call rate is 9rs/min
    # Local SMS rate is 1rs/SMS.
    # National SMS rate is 2rs/SMS.
    # International SMS rate is 5rs/SMS.
    # Other hidden charges.

    Procedure for calculating the phone bill for corporate connection with tariff 199.
    # Will not be having any clip charge.
    # Local call 200 minutes free, after that the call rate is .30rs/min.
    # STD call rate is 2rs/min.
    # Roaming call rate is 3.75rs/min.
    # ISD call rate is 7.5rs/min.
    # 200 local SMS free, after that the rate is 50 paise/SMS.
    # National SMS rate is 2rs/SMS.
    # International SMS rate is 5rs/SMS.

    Procedure for calculating the phone bill for corporate connection with tariff 249.
    # Will not be having any clip charge.
    # Local call 300 minutes free, after that the call rate is .30rs/min.
    # STD call rate is 2.40rs/min.
    # Roaming call rate is 1rs/min.
    # ISD call rate is 7.5rs/min.
    # 200 local SMS free, after that the rate is 50 paise/SMS.
    # National SMS rate is 2rs/SMS.
    # International SMS rate is 4rs/SMS.

    Procedure for calculating the phone bill for corporate connection with tariff 299.
    # Will not be having any clip charge.
    # Local call 600 minutes free, after that the call rate is .40rs/min.
    # STD call rate is 2.40rs/min.
    # Roaming call rate is 3.5rs/min.
    # ISD call rate is 7.5rs/min.
    # 200 local SMS free, after that the rate is 50 paise/SMS.
    # National SMS rate is 2rs/SMS.
    # International SMS rate is 5rs/SMS.

    Calculation process: We have to calculate the phone bill for each candidate based the above conditions

    Example 1:
    Input:
    # char* candidateName ="Ram";
    # char* selectedTariff ="199" ;
    # char* isCorparate = "Yes";
    # double clipCharge = 0.0;
    # double hiddenCharge = 0;
    # int totLocalCalMinute =200;
    # int totSTDCalMinute = 80;
    # int totISDCalMinute= 50;
    # int totRoamingCalMinute= 100;
    # int totLocalSMS= 150;
    # int totNationalSMS= 25;
    # int totInternationalSMS= 5;.

    Output value is 985.0

    Explanation:

    In this example the calculaion of the bill is done as follows: The total local calls made is 200. Since this is a corporate tariff, 200 minutes of local call is free. So the total cost of the local calls is 0. Now the total STD calls made is 80 and the tariff is Rs 2 per min so the cost is 80*2 which is 160. The total ISD call made is 50 and the tariff is 7.5 rs per min. So the total cost for ISD calls is 7.5*50 which is 375 rs. The total number of roaming calls made is 100 and the cost for roaming is 3.75 rs per min. So the totoal cost for roaming calls is 100*3.75 which is 375 rs. Total number of local SMS sent is 150, but this being a corporate connection ad 200 local SMS is free. So the cost for local SMS is nill or 0. Total number National SMS sent is 25 and the tariff for national SMS is 2 rs. So the total cost for national SMS is 25*2 which is 50 rs. Total number of international SMS sent is 5 and the cost for international SMS is 5. So the total cost for International SMS is 5*5 which is 25. Now adding all these cost i.e 0+160+375+375+50+25=985 rs.

    Example 2:
    Input
    # char* candidateName ="Radha";
    # char* selectedTariff ="300" ;
    # char* isCorparate = "No";
    # double clipCharge = 60.0;
    # double hiddenCharge = 100;
    # int totLocalCalMinute =2000;
    # int totSTDCalMinute = 560;
    # int totISDCalMinute= 150;
    # int totRoamingCalMinute= 300;
    # int totLocalSMS= 950;
    # int totNationalSMS= 90;
    # int totInternationalSMS=45

    Output value is = 7571.4

    For C solutions
    Header File    :    PhoneBill.h
    Function Name    :    double getCalculatedBillAmount( char* candidateName, char* selectedTariff, char* isCorparate, double clipCharge, double hiddenCharge, int totLocalCalMinute, int totSTDCalMinute, int totISDCalMinute, int totRoamingCalMinute, int totLocalSMS, int totNationalSMS, int totInternationalSMS )
    Directory Name    :    phonebillcalc
    File Name    :    PhoneBill.c
    For C++ solutions
    Header File    :    PhoneBill.h
    Class Name    :    phone
    Function Name    :    double getCalculatedBillAmount()
    Directory Name    :    phonebillcalc
    FileName    :    PhoneBill.cpp
    Friday, May 25, 2007 2:00 PM
  • Very long question. Anyone got that question in the quiz ?

    If not, then may be someone from us can try out the solution for it.
    Friday, May 25, 2007 2:01 PM