Monday, July 19, 2010

Weekend fun with Simplest Pyramid Generator!

I just get bored during this weekend and try to solve some puzzle that my friends mentioned on my email last week. It is making a sort of Pyramid but only with odd numbers. So, what we have to do is try to make a simplest pyramid generator program to generate a pyramid with a single argument given. For example:

C:\pyramid.exe 7

would generate a pyramid like this:

I know that there is some script like Python, Ruby and so on can make a single line of code to generate this pyramid. But the condition is not to use a special function or library. I just solved this puzzle within 3 hours with Visual C++ 6.0. Here it is a single long line of code:

void main(int r,char * g[]){for(int i=((atoi(g[1])+1)/2);i>0;i--){for(int j=i-1;j>0;j--)cout<<" ";for(int k=1; k<=(((atoi(g[1])+1)/2)-i+1);k++)cout<<"*";for(k=(((atoi(g[1])+1)/2)-i+1)-1;k>0;k--)cout<<"*";for(j=i-1;j>0;j--)cout<<" ";cout<<"\n";}}

Thus, this code still need to insert a header file <iostream.h> and <stdlib.h> for print the result and the atoi function for converting ASCII to Integer from the given argument. But this is standard for C programming. All scripting language for sure need all these stuff for the script that communicate with core engine. Coding with C++ might be various depend on your platform and compiler.



  1. hahah... sorang lagi dah siap..

  2. aik.. ada game ke abg xanda? teringat sem1 dlu.. buat menda nilah... pastu buat yang segi tiga terbalik plak.. XD

    saudara alternator, kongsilah skit ilmu kaji malware..XD

  3. hehehe, aku dulu buat pakai c++ nih, piramid terbalik semua ada..