locked
sunday special quiz contd......... RRS feed

  • Question

  • let's make a new beginning from here.


    void main()
    {
    if(condition)
     printf("say hello ");
    else
    printf("to me");
    }



    suggest what should be the condition to have an output.
    OUTPUT: say hello to me
    Sunday, May 27, 2007 3:10 AM

Answers

  • Here is your answer....

    main()
    {
    if(!(printf("say hello ")))
     printf("say hello ");
    else
     printf("to me");
    }
    Sunday, May 27, 2007 6:24 AM
  • but what if i change the question to:

    how can i execute both if- & else statements.
    Sunday, May 27, 2007 6:38 AM
  • yes it is possible.
    just think out of the box. given you a hint.
    after all i am here to tell the answer.
    & you all come up with mind boggling puzzles
    Sunday, May 27, 2007 11:58 AM
  • void main()
    {
        if (1)
        {
            loop1:  printf("say hello ");
            goto loop2;
        }
        else
        {
            goto loop1;
            loop2: printf("to me");
        }
    }


    I think this is the correct Solution.
    Monday, May 28, 2007 9:03 AM
  • nice dude.
    but there is no need for the second goto statement.
    well, the next question:
    how to convert string into integer of float?
    note: no in built function is allowed
    Monday, May 28, 2007 10:02 AM
  • by  manipulating the ascii values?
    Monday, May 28, 2007 2:37 PM
  • hey that's a partial answer.
    try to give a snippet for it.
    let's say you give strings on a command prompt which you have to convert t integer or a float if the string contains a decimal(dot)
    Monday, May 28, 2007 2:47 PM
  • Well this is the program for integer... let someone else post the prog for the float or else i will do it in some time...

    Code Snippet
    #include<stdio.h>
    #include<conio.h>


    void main()

    {

        char s[]="1234";

        int number=0,i;

        for(i=0; s[i]!='\0'; i++)

        {

           number *= 10;

           number += s[i] - '0';

        }

        printf("%d", number);

    }


    Monday, May 28, 2007 4:30 PM
  • good dude.
    but 1 thing.
    i told you that the string will be passed on a command prompt.
    & you don't know whether the passed string is actually containing numbers or the alphabets.
    but your answer is partially right.
    that's why i marked that.

    Tuesday, May 29, 2007 2:10 AM
  • Well if you want those constraints, we can have them like this, i m writing it here directly so there can be problems...

    Code Snippet

    #include<stdio.h>
    #include<conio.h>



    void main(int argc, char *argv[])

    {

    //    char s[]="1234";

        int number=0,i;

        for(i=0; argv[1][i]!='\0'; i++)

        {

           if(argv[1][i] <= '0' || argv[1][i] >='9')

           {

              printf("Sorry the string is not of numbers");

              exit(1);

           }

           number *= 10;

           number += s[i] - '0';

        }

        printf("%d", number);

    }


    Tuesday, May 29, 2007 4:31 AM
  • good buddy.
    but i want the alphabets to be interpretted. leave it.
    here's next 1.

    Write a snippet for determining the largest number from the given large finite sequence but less than 1500.
    constraint: you can't use an array.
    Tuesday, May 29, 2007 7:11 AM
  • arre you can't use an extra array for implementing your answer.
    & let's say you are  given an array which contains the initial sequence(pretty large).
    now do it.
    1 more constraint: time & space complexity have to be the lowest
    Tuesday, May 29, 2007 11:49 AM
  • Time complexity will be O(N), where n = size....

    Code Snippet

    #include<iostream.h>



    int findlargest(int *arr, int size)

    {

        int max;

        max = arr[o];

        for(int i=1; i<size; i++)

        {

           if(arr[1]>max)

              max = arr[1];

        }

        return max;

    }


    Tuesday, May 29, 2007 12:52 PM
  • hey read the question carefully.
    i asked for the largest number in a sequence but less than 1500.
    & no increasing or decreasing order.
    numbers are stored randomly in an array
    Tuesday, May 29, 2007 3:03 PM
  • Well there is a possibility that the array has elements in some sequence.. lets say asscending or descending.. in that case the complexity will be only O(1), you need to check only one condition.. now where can we find such thing.. remember when we create a binary search tree, the elements are arranged in an order, you can directly get the max and min number in a tree..

    so if numbers are in a sequence, you directly get the max or min from it...
    Tuesday, May 29, 2007 3:53 PM
  • but what if i give you a sequence like this:
    51 52 53 10 11 16 1.
    your program will infer that the listed is sorted by comparing just 2 elements. however this is not the case.
    there is no way to infer whether the numbers in the array are in any order.
    Wednesday, May 30, 2007 1:42 AM
  • this is from the book on yashwant kanetkar.

    write a code for determining the determinant of n x n matrix
    Wednesday, May 30, 2007 4:51 AM
  • & hey you still haven't answered the previous 1
    Wednesday, May 30, 2007 4:52 AM
  • talking about the question where you have to find the largest number but less than 1500.
    & for determinant problem, can you explain to me the logic?
    i have never seen this idea/logic before in the calculation of  a matrix.
    Wednesday, May 30, 2007 11:17 AM
  • Well the logic is simple, you know how we calculate the determinant,
    like for
    1 2 3
    4 5 6
    7 8 9

    we have the formula 1*(5*9 - 8*6) - 2*(4*9 - 6*7) + 3*(4*8 - 5*1)

    If you expand it, you will get this format...

    (1*5*9 + 4*8*3 + 7*2*6) - ( 7*5*3 + 4*2*9 + 1*8*6)

    Try to get the pattern, i have used it in my program...
    This patter was given by a mathematician, i dont remember the name, but it works for any N*N matrix, and its easy to code... you can also find determinant in another way or recursive fassion, but i have not tried it out.. may be you can find it out...
    Wednesday, May 30, 2007 12:25 PM
  • back with bang.
    here's a new question.


    implement bus topology using C.
    Monday, June 4, 2007 2:44 AM
  • this is again a networking topology
    topology is an arrangement(physical & logical) of the communicating devices
    Monday, June 4, 2007 6:52 AM

All replies

  • is this possible?
    Sunday, May 27, 2007 3:23 AM
  • Here is your answer....

    main()
    {
    if(!(printf("say hello ")))
     printf("say hello ");
    else
     printf("to me");
    }
    Sunday, May 27, 2007 6:24 AM
  • but what if i change the question to:

    how can i execute both if- & else statements.
    Sunday, May 27, 2007 6:38 AM
  • Now that is something i dont know... please tell the answer, its killing me..
    Sunday, May 27, 2007 6:56 AM
  • Hey Sunil,

    sounds interesting if you change you condition is such a way that both if and else blocks are executed. But tell mei, is it really possible ?
    Sunday, May 27, 2007 7:46 AM
  • yes it is possible.
    just think out of the box. given you a hint.
    after all i am here to tell the answer.
    & you all come up with mind boggling puzzles
    Sunday, May 27, 2007 11:58 AM
  • void main()
    {
        if (1)
        {
            loop1:  printf("say hello ");
            goto loop2;
        }
        else
        {
            goto loop1;
            loop2: printf("to me");
        }
    }


    I think this is the correct Solution.
    Monday, May 28, 2007 9:03 AM
  • nice dude.
    but there is no need for the second goto statement.
    well, the next question:
    how to convert string into integer of float?
    note: no in built function is allowed
    Monday, May 28, 2007 10:02 AM
  • by  manipulating the ascii values?
    Monday, May 28, 2007 2:37 PM
  • hey that's a partial answer.
    try to give a snippet for it.
    let's say you give strings on a command prompt which you have to convert t integer or a float if the string contains a decimal(dot)
    Monday, May 28, 2007 2:47 PM
  • Well this is the program for integer... let someone else post the prog for the float or else i will do it in some time...

    Code Snippet
    #include<stdio.h>
    #include<conio.h>


    void main()

    {

        char s[]="1234";

        int number=0,i;

        for(i=0; s[i]!='\0'; i++)

        {

           number *= 10;

           number += s[i] - '0';

        }

        printf("%d", number);

    }


    Monday, May 28, 2007 4:30 PM
  • good dude.
    but 1 thing.
    i told you that the string will be passed on a command prompt.
    & you don't know whether the passed string is actually containing numbers or the alphabets.
    but your answer is partially right.
    that's why i marked that.

    Tuesday, May 29, 2007 2:10 AM
  • Well if you want those constraints, we can have them like this, i m writing it here directly so there can be problems...

    Code Snippet

    #include<stdio.h>
    #include<conio.h>



    void main(int argc, char *argv[])

    {

    //    char s[]="1234";

        int number=0,i;

        for(i=0; argv[1][i]!='\0'; i++)

        {

           if(argv[1][i] <= '0' || argv[1][i] >='9')

           {

              printf("Sorry the string is not of numbers");

              exit(1);

           }

           number *= 10;

           number += s[i] - '0';

        }

        printf("%d", number);

    }


    Tuesday, May 29, 2007 4:31 AM
  • good buddy.
    but i want the alphabets to be interpretted. leave it.
    here's next 1.

    Write a snippet for determining the largest number from the given large finite sequence but less than 1500.
    constraint: you can't use an array.
    Tuesday, May 29, 2007 7:11 AM
  • No array so does it mean we have a link list.. 1st give us the structure in which the finite sequence is given...
    Tuesday, May 29, 2007 11:37 AM
  • arre you can't use an extra array for implementing your answer.
    & let's say you are  given an array which contains the initial sequence(pretty large).
    now do it.
    1 more constraint: time & space complexity have to be the lowest
    Tuesday, May 29, 2007 11:49 AM
  • Now one more doubt.. you said sequence.. than does it mean they are arrange in a sequence, like asscending or descending.. I will give you answer for both the cases....
    Tuesday, May 29, 2007 12:48 PM
  • Time complexity will be O(N), where n = size....

    Code Snippet

    #include<iostream.h>



    int findlargest(int *arr, int size)

    {

        int max;

        max = arr[o];

        for(int i=1; i<size; i++)

        {

           if(arr[1]>max)

              max = arr[1];

        }

        return max;

    }


    Tuesday, May 29, 2007 12:52 PM
  • Time complexity will be O(c), c = constant, i leave it upto you to find out..

    Code Snippet

    #include<iostream.h>



    int findlargest(int *arr, int size)

    {

        if(arr[0] < arr[1])

           return arr[size-1];

        else

           return arr[o];

    }



    Tuesday, May 29, 2007 12:55 PM
  • hey read the question carefully.
    i asked for the largest number in a sequence but less than 1500.
    & no increasing or decreasing order.
    numbers are stored randomly in an array
    Tuesday, May 29, 2007 3:03 PM
  • what are you trying to do here?
    Tuesday, May 29, 2007 3:04 PM
  • Well there is a possibility that the array has elements in some sequence.. lets say asscending or descending.. in that case the complexity will be only O(1), you need to check only one condition.. now where can we find such thing.. remember when we create a binary search tree, the elements are arranged in an order, you can directly get the max and min number in a tree..

    so if numbers are in a sequence, you directly get the max or min from it...
    Tuesday, May 29, 2007 3:53 PM
  • but what if i give you a sequence like this:
    51 52 53 10 11 16 1.
    your program will infer that the listed is sorted by comparing just 2 elements. however this is not the case.
    there is no way to infer whether the numbers in the array are in any order.
    Wednesday, May 30, 2007 1:42 AM
  • Well there can be many type of sequences, but if you take linear or exponential sequence, then this can be the case...
    anyways lets leave it apart... so any other question???
    Wednesday, May 30, 2007 4:36 AM
  • this is from the book on yashwant kanetkar.

    write a code for determining the determinant of n x n matrix
    Wednesday, May 30, 2007 4:51 AM
  • & hey you still haven't answered the previous 1
    Wednesday, May 30, 2007 4:52 AM
  • Which one are you talking about...

    As for the determinant, i had made it on my own, without any help... here is the code for it.. I m sure you will like it...

    Code Snippet

    // This program will find the determinant value of a square matrix.

    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int mat[10][10],deter,n,i,j,k,k1,add=0, sub=0, temp=1;
        clrscr();
        printf("Please give the row or column No. of a square matrix = ");
        scanf("%d", &n);
        printf("Please give the values of the Matrix");
        for(i=0;i<n;i++)
        {

            printf("\n");
            for(j=0;j<n;j++)
            {
                printf("Row no %d Column No. %d = ",i+1,j+1);
                scanf("%d",&mat[i][j]);
            }
        }

    // Now we will start calculating the addition part...
        for (k=0;k<n;k++)
        {
            temp=1;
            for (i=k,k1=0,j=0; k1<n; k1++, j++, i++)
            {
                i=i%n;
                temp*=mat[i][j];
            }
            add+=temp;
        }

    // Now we will start the subtraction portion....
        for (k=1; k<=n; k++)
        {
            temp=1;
            for (i=n-k, k1=0, j=0; k1<n; k1++, j++, i--)
            {
                i= (i+n)%n;
                temp*=mat[i][j];
            }
            sub+=temp;
        }

        deter = add - sub;
        printf("\nThe matrix is :");
        for(i=0; i<n; i++)
        {
            printf("\n");
            for(j=0; j<n; j++)
            {
                printf(" %d", mat[i][j]);
            }
        }
        printf("\n The Determinant of the matrix is %d", deter);
        getch();
    }


    Wednesday, May 30, 2007 7:08 AM
  • talking about the question where you have to find the largest number but less than 1500.
    & for determinant problem, can you explain to me the logic?
    i have never seen this idea/logic before in the calculation of  a matrix.
    Wednesday, May 30, 2007 11:17 AM
  • Well the logic is simple, you know how we calculate the determinant,
    like for
    1 2 3
    4 5 6
    7 8 9

    we have the formula 1*(5*9 - 8*6) - 2*(4*9 - 6*7) + 3*(4*8 - 5*1)

    If you expand it, you will get this format...

    (1*5*9 + 4*8*3 + 7*2*6) - ( 7*5*3 + 4*2*9 + 1*8*6)

    Try to get the pattern, i have used it in my program...
    This patter was given by a mathematician, i dont remember the name, but it works for any N*N matrix, and its easy to code... you can also find determinant in another way or recursive fassion, but i have not tried it out.. may be you can find it out...
    Wednesday, May 30, 2007 12:25 PM
  • back with bang.
    here's a new question.


    implement bus topology using C.
    Monday, June 4, 2007 2:44 AM
  • its possible
    Monday, June 4, 2007 6:19 AM
  • What is a bus topology???
    Monday, June 4, 2007 6:33 AM
  • this is again a networking topology
    topology is an arrangement(physical & logical) of the communicating devices
    Monday, June 4, 2007 6:52 AM