Friday 18 July 2014

Using GCC Compiler on Windows

Being a student of an Indian college I got to learn C and C++ in starting of college using old and ancient TC compiler(on windows) which is not as per current standards that is C11 standards, as the wikipedia page of C programming language says that current standard of C compiler is defined by C11. When I came across experienced programmers they opened my eyes by telling same fact; so I decided to make my hand smooth in gcc and g++ compilers too which are as per C11 standards (have a look at list of C/C++ compilers). In Linux using these compilers is not at all a pain but recently I came across a situation when I have to run few C programs on Windows and I wanted to use only C11 standard compiler.

When I installed gcc on windows, realized that it could not be easy for newbies; so here presenting few steps to be followed to install and configure gcc and g++ compilers on Windows:

  1. Download and Install MinGW from sourceforge. It is a Minimalist GNU for Windows; basically it allows us to run gcc(GNU Compiler Collection) and GNU builtins on windows.
  2. After installion in the end it will ask to add some packages, we need at least GNU C++ compiler. Mark it for installation and other packages if you need.

  3. Click on installion menu above ---> Apply changes ---> Apply. While installing it will look something like below:
  4. Finally after installion it will look something like below image with slowing installed version. Close this window
  5. Set its bin path(mostly it is C:\MinGW\bin) to environment variable. For this follow following steps: 
    • Right click on my computer ---> Properties ---> Advanced ---> Environment Variables ---> system variables search for path variable ---> edit path ---> put a semicolom(;) at end of current path and append required path.
Now to run your C programs:
Open command prompt
Move to the folder where your .c or .cpp files reaside which you want to run. On command prompt write:
> gcc <filename>.c -o <filename>.exe  and hit enter
> <filename>.exe hit enter;
you will see result on command prompt.

That's it, hopefully this would be helpful for you, in case of any problem you are welcome to put them in comments.




Thursday 13 February 2014

Space Complexity

#What

It is just the amount of memory used by a program; the amount of computer memory that is main memory required by the algorithm to complete its execution with respect to the input size.

#Why

In todays world space is no more a hurdle what matters a lot is only time. So does one need to calculate space complexity?
Yes! If a program takes a lot of time, you can still run it, and just wait longer for the result. However if a program takes a lot of memory, you may not be able to run it at all, so this is an important parameter to understand.





 #How

Space Complexity(s(P)) of an algorithm is total space taken by the algorithm to complete its execution with respect to the input size. It includes both Constant space and Auxiliary space.

S(P) = Constant space + Auxiliary space

Constant space is the one which is fixed for that algorithm; generally equals to space used by input and local variables.
Auxiliary Space is the extra/temporary space used by an algorithm.




# EXAMPLE

 We assume that space required for one variable is one unit for simplicity.

1) def calculate(a,b,c):
     return a+b/c

S(p) = 1 + 1 + 1 = 3 ==> O(1)


2) def sum(a, n):
    s = 0
    for i in range(n):
        s += a[i]
    return s

S(p) = (n*1 + 1 + 1) + 1 ==> O(n) and auxiliary S(p) = O(1)




3) def Rsum(a,n):
    if n <= 0:
        return 0   
    return a[-1] + Rsum(a[:-2], n-1)



(# of stack frames)*(space per stack frame)

When the function calls itself there are 3 things that are stored in the stack.
The array pointer a,
The size of the array, n and
The value at the last index a[n].

So space per stack frame equals to {sizeof(a)+sizeof(n)+sizeof(a[n])}



This function calls itself till n becomes 0
so  # of stack frames = n (from n-1 to 0)

Therefor the amount of space required to store execute is
n times the space required to store above 3values once,

i.e., (n)*{sizeof(a)+sizeof(n)+sizeof(a[n])}.

Since the value in the braces remain a constant for a particular i/p,

we say that the space complexity of this is directly proportional to n.
i.e auxilary S(p) = O(n)