Saturday, 28 July 2012

Going to College and C Programming

The last two weeks have been pretty intense. I was moving out of my house and into a college for the first time in my life. The settling in period was quite hectic and ... well ... unsettling.
I am right now in a college hostel room in the National Institute of Technology (NIT), Trichy in Tamil Nadu, India. There were insane amounts of stuff to think about, buy and drag across the campus into my new residence (No, I cannot and never will call it home.)

After a week of classes, things are finally starting to feel normal.

On a related note, I'm learning C programming in my computer science class (one of the 8 subjects I have to take during the first year of engineering.) and I'm finding (to my delight) that C is extremely easy to learn. There is an excellent reason for this though. Back when I was in Year 10, during my month long holiday in December, I had hours of endless fun self learning Java Programming. I never thought at that time that those hours of endless fun would translate into a general ability to pick up within days almost any C related programming language.  (Like Python, C++, Java ...).

I got an assignment to write down/create an interesting program. The only requirement our professor set was that I (the programmer) had to be able to explain exactly how the program worked if asked to do so. So, I decided to write a program which generated prime numbers up to a certain user specified upper limit. I thought it was a good place to start as it uses almost all the basic concepts of C programming.

But the program felt a little dull even for something related to prime numbers. So I decided to make it really interesting. I'm sure the geeks among my readers will appreciate much of the code, especially  the variable names.


#include <stdio.h>
#include <stdlib.h>
int gandalf_the_primechecker(int charles_CABBAGE);
int main()
{
    printf("This is a program I wrote which spews forth all prime numbers that fall   below an upper limit.\n");
    int charles_CABBAGE;
    int ashwin_limit;  //The upper limit. I call it the "ashwin_limit"
    printf("\nI now demand that you give me an upper limit! ");
    scanf("%d",&ashwin_limit);
 
    for(charles_CABBAGE=2;charles_CABBAGE<ashwin_limit;charles_CABBAGE++)
    {
        if(charles_CABBAGE==2 || charles_CABBAGE==3 || gandalf_the_primechecker(charles_CABBAGE))
        {
            printf("\n%d\n",charles_CABBAGE);
         
        }
        else
        {
            printf(".");
        }
    }
    printf("That's all folks! Bye for now!\n");
    printf("\nThe meaning of life is 42.\n");
    printf("\nSo follow the white rabbit.");
    system("PAUSE");
    return 0;
}
int gandalf_the_primechecker(int charles_CABBAGE)
{
    int Middle_Earth;//The midpoint between the ashwin_limit (upper limit) and zero.
    Middle_Earth = charles_CABBAGE/2;
    int The_Shire;
    The_Shire = 2;
    while(The_Shire <=Middle_Earth)
    {
        if(charles_CABBAGE%The_Shire != 0 && The_Shire==Middle_Earth)
        {
            return 1;
        }
        else if(charles_CABBAGE%The_Shire !=0)
        {
            The_Shire++;
        }
        else if(charles_CABBAGE%The_Shire ==0)
        {
            return 0;
            break;
        }
    }
}



Post a Comment