Pascal Triangle Challenge

A simple challenge for c/c++ fans, use only 2 variable and 2 for loop to print a pascal triangle looks as bellow


        1
       121
      12321
     1234321
    123454321
   12345654321
  1234567654321
 123456787654321
12345678987654321

My solution:

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

void piramid(int const c)
{
    int x,y;
    for(y=0; y<c; y++)
    {
        for(x=0; x<c-(1+y)+((y*2)+1); x++)
        {
            if(x<(c-(y+1)))printf(" ");
            else
            {
                if(x-(c-(y+2))>y+1) printf("%d",(2*y)+2-(x-(c-(y+2)) ));
                else printf("%d",(x-(c-(y+1))+1 ));
            }
        }
        printf("\n");
    }
}

int main(int argc, char* argv[])
{
    if (argc==2)
    {

        if (atoi(argv[1])>9 || atoi(argv[1])<=1)
        {
            printf("Insert range within 2 to 9, ig. %s 9\n",argv[0]);
            return 1;
        }
        piramid(atoi(argv[1]));
    }
    else
        printf("Insert range within 2 to 9, ig. %s 9\n",argv[0]);
    return 0;
}

swap variable quest

This is an interesting quest given by my friend who working in Intel, during our conversation online.

How to swap two variables ?

Let say A=123 and B=546, how to swap them. So this is a quest, be smart enough not to give a plain solution like create a temporary variable to swap them. First of all my answer sounds a bit stupid. I say if I coded in python,

A,B=B,A

Okay that is not the expecting answer. Before you look at the solution below, maybe you would like to try to figure out.

Hint: use logic XOR, no additional variable is needed.

Solution:

A=A XOR B;
B=A XOR B;
A=A XOR B;

Explanation:
XOR is known as exclusive disjunction where the nature of XOR is when 1 XOR 0 will equal to 1 but when 1 XOR 1 will becomes 0. Another important behavior of XOR is when (a XOR b) XOR b will resulting a.

So lets look back at the solution, you will find A XOR B seems to become “something else”, where this “something else” will return A if we XOR B and return B if we XOR A. So first line, A=(A XOR B). Secondly B = (A XOR B) XOR B = A, where we replace the original A into (A XOR B). Last line, A= ( A XOR B) XOR A = B. Seems at second line B already become A, so we replace B as A.

Interesting isn’t it? Look at it few times, if you can’t get it.

simple singleton class

Singleton is one of the common design pattern used to make a class object exist only one instant. I have read some books illustrate the implementation of singleton, I found it all complicated. I have tried to come out a very simple example on singleton class, bellow is my outcome.

#include<stdio.h>
class singleton
{
    public:
    void print()
    {
        printf("hello\n");
    }
    int a;
}singleton;

int main()
{
    //singleton s;  #cannot define a new instant like this anymore.
    singleton.a=5;
    printf("a=%d\n",singleton.a);
    singleton.print();

    return 0;
}

With the class name and the object name appear the same, you make that class object singleton. Which means you can’t instantiate a new object using the name singleton anymore. The way to access the function and variables of a singleton object is direct.

This is my initial idea of how to make singleton simple.