1. Become a Developer for CrazyEngineers - Apply NOW!

The CE Coding Challange

Discussion in 'Computer Science | IT | Networking' started by Sanyam Khurana, Sep 7, 2013.

  1. Sanyam Khurana

    Sanyam Khurana Star

    Engineering Discipline:
    Computer Science
    Yeah, it's time to code, here give your tricky questions on Programming, and we will all try to do that.

    This would improve your thinking ability and refine your logic.

    First we would have some simple questions and then move to more tricky ones.

    I would like to roll the dice first.

    Q1. Write a Program in C/C++ to print a statement ( say "Hello World") without using a semicolon (in the entire program).

    Q2. Write a Program to add two numbers without using "+" .

    Q3. Write a Program to multiply two numbers without using "*" .


    Now that was a bit kiddish, don't worry, now let's face some questions which are more tricky

    -> Write a Program that print it's own source code ie it would then recompile and run itself !

    PS :- Though you may find solutions to the above questions googling them, but it would ruin the craziness .

    This thread is created so that everyone can learn and improve their programming.


    And keep adding more questions to this thread..!!

    _______________________________________________________________________
    Edit : While answering the questions, please explain the logic also, it would help others in understanding what you have done and others can also learn more stuff.
     
    • Like Like x 6
    Last edited: Sep 8, 2013
  2. avii

    avii Ace

    Engineering Discipline:
    Computer Science
    Please explain me how does printing Hello World without semi colon improves my logic ?
     
    • Dislike Dislike x 1
  3. Sanyam Khurana

    Sanyam Khurana Star

    Engineering Discipline:
    Computer Science
    Is not just about printing hello world but its more than that

    of course for printing something without semi colon you have to think differently which would improve your logic

    try all the questions and choose them

    lets see who does that and if these questions seems kiddish to you don't worry we will move to more complex questions soon.......
     
    • Agree Agree x 1
  4. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    I think the program will run unconditionally and infinitely if it recompiles itself and runs the exe generated.
    Here's my code there is some problem with process leaving the control of Untitled1.exe and go to recompiled.exe but it works fine.
    (Assuming that the gcc compiler already has a path in variables)


    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<windows.h>
    int main() {
        printf("source code, recompile, display output\n\n");
        system("type Untitled1.cpp");
       
        Sleep(500);
       
        system("g++ -o recompiled Untitled1.cpp");
           
        Sleep(1000);              // stabilizing time
        system("recompiled.exe");
        return 0;
    }
     
  5. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    Code:
    #include<stdio.h>
    int main()
    {
        if(printf("I don't have semi colon"))
        {
              fflush(stdin);
              getchar();
        }   
    }
     
    • Like Like x 3
  6. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    Code:
    #include<stdio.h>
    void multi(int,int);
    int main()
    {
        int a = 40,b = 30;
        if(a>b)
            multi(a,b);
        else
            multi(b,a);
    }
    
    void multi(int greater, int smaller) {
        int ans = 0;
        while(smaller != 0) {
            ans += greater;
            smaller--;
        }
        printf("%d",ans);
    }
     
    • Like Like x 2
  7. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science

    Code:
    #include<stdio.h>
    void multi(int,int);
    int main()
    {
        int a = 32,b = 15;
        while( b != 0) {
            int c = a & b;
            a = a ^ b;
            b = c << 1;
        }
        printf("%d",a);
    }
    
     
    • Like Like x 3
  8. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    Adding another question here!!


    write a program to swap all the occurrences of 0 to the end of the array!
    ex:
    input -> 1,0,2,0,3,0,4
    output -> 1,2,3,4,0,0,0
     
  9. Sanyam Khurana

    Sanyam Khurana Star

    Engineering Discipline:
    Computer Science

    Actually your code shouldn't use any semi colon.
    So, it would be something like this..

    Code:
    #include<stdio.h>
    int main()
    {
        if(printf("I don't have semi colon"))
        {
        }  
    }


    Your logic is right, just there wouldn't be any code in between the if statement.

    I would also like to request you to edit your above posts and explain the logic you used too, it would help many newbies, and other people.
     
  10. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science

    I printed the statement without using semi colon. I don't think it needs to be pointed out
    the code i've written inside if block is just to make it stay
    anyway,
    another way to do that (it satisfies your rules)


    Code:
    #include<stdio.h>
    int main()
    {
        switch(printf(" hello mere doston :P "))
        {
        }
    }
     
    • Like Like x 1
    • Agree Agree x 1
  11. Sanyam Khurana

    Sanyam Khurana Star

    Engineering Discipline:
    Computer Science
    I have designed this code for 10 elements and would work as given.

    Code:
    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    int a[10],i,j,b[10],count;
    clrscr();
    for(i=0;i<10;i++)
    {
    cin>>a[i];
    }
    for(i=0,j=0;i<10;i++)
    {
    
        if(a[i]!=0)
        {
          b[j]=a[i];
          j++;
        }
        else
        count++;
    }
    for(i=j;i<10;i++)
    b[i]=0;
    for(i=0;i<10;i++)
    {
    cout<<b[i];
    }
    getch();
    }
     
  12. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    You need to swap the values instead of taking a separate array!
    I mean the changes are to be made within the array which is used to take input from user
     
  13. Sanyam Khurana

    Sanyam Khurana Star

    Engineering Discipline:
    Computer Science
    Sorry, the question was actually that you don't have to use semi colon anywhere in the entire program. :)

    PS:- I know I forgot to mention it clearly that you can't use the semicolon in the entire program , no problem, I'm going to edit that ...!!!

    Sorry for that, but please explain all your programs too... :cean:

    Ok, will work on it..
     
  14. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    2 of my programs are self explanatory, i'll add tags to my addition program
     
  15. Sanyam Khurana

    Sanyam Khurana Star

    Engineering Discipline:
    Computer Science
    Ok, will work on it...


    Can you explain this program, I'm not getting what you have done here....


    basically these three lines of code is what I need to understand..

    I have never seen this before :(

    Code:
     int c = a & b;
            a = a ^ b;
            b = c << 1;
    Thanks in advance..!!
     
  16. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    I am just operating on the bits... I'll explain this in detail!

    I'll take a few small numbers to give an explanation..
    Say, a = 9 (1001 in binary) and b = 3 (0011 in binary)
    the variable c here indicates the carry. if I perform AND operation on a & b
    i.e. c = a & b;
    it executes like this,
    1 0 0 1
    0 0 1 1
    """"""""""
    0 0 0 1 (performing AND operation according to truth table)
    hence the carry i.e. c = 1 (0001 in binary)

    the next step is a = a ^ b where we are performing XOR operation on bits to calculating sum without adding the carry
    i.e.
    1 0 0 1
    0 0 1 1
    """"""""""
    1 0 1 0 (performing XOR operation according to truth table)
    hence value of a = 10 (1010 in binary)

    the next step is b = c << 1 (left shift of c)
    as we saw above c = 1 (0001) after left shift it becomes (0010) viz stored in b

    so at 1st iteration a = 10 , b = 2 , c = 1

    since it is in loop, we perform the 1st operation again i.e. c = a & b
    at this step, the value of a = 1 0 1 0 and b = 0 0 1 0
    so c becomes
    1 0 1 0
    0 0 1 0
    """"""""""
    0 0 1 0
    hence c = 2 (0010 in binary)
    the next step is a = a ^ b
    i.e.
    1 0 1 0
    0 0 1 0
    """"""""""
    1 0 0 0
    therefore, a = 8 (1000 in binary)
    next step b = c << 1 i.e. b = 0 1 0 0

    so at 2nd iteration a = 8 , b = 4 , c = 2
    again in next iteration, c = a & b
    1 0 0 0
    0 1 0 0
    """"""""""
    0 0 0 0
    i.e. c = 0

    next step, a = a ^ b
    1 0 0 0
    0 1 0 0
    """"""""""
    1 1 0 0
    i.e a = 12

    next step, b = c << 1;
    i.e. b = 0;
    since b = 0, loop breaks and value of a i.e. 12 is printed and that's the answer :)
     
    • Informative Informative x 1
  17. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    @Sanyam Khurana take time to read it and keep a paper and pen with you ;)
     
  18. simplycoder

    simplycoder Ace

    Engineering Discipline:
    Electronics & Communications

    Basically this is the same as full adder. where c is the carry.



    I didn't find time to write a code, but here is an algorithm which I would use.
    Keep an counter of non-zero numbers(n), this would either be less or equal to numbers entered(m).
    print all the numbers the way they are and then print (m-n) zeroes.
     
    Last edited: Sep 8, 2013
  19. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    I think we can sort it normally!?
     
  20. simplycoder

    simplycoder Ace

    Engineering Discipline:
    Electronics & Communications
    My bad, I have edited my previous post, your problem never said sorting, it was my wrong assumption, refer to above edited post.
     

Share This Page