Tuesday, 28 January 2014

What is a QUINE?

Ever heard of a program that produces it's 'own source code' as the output?
Well, the name's 'QUINE'.

Such programs create replicas of themselves, not by copying, but by assembling. The name 'Quine' was coined by 'Douglas Hofstadter', while he was studying 'Indirect Self Reference'.

A quine is sometimes also referred to as 'self replicating/reproducing/copying' programs.
For more information see: http://en.wikipedia.org/wiki/Quine_(computing).

Sunday, 19 January 2014

Remove shortcut virus from pen drive/ memory card







 By -

 Sandeep Singh

CSE 2nd year

GNDU RC Sathiala



Here are two simple tips by which you can remove shortcut virus from you pen drive etc.

and see your precious files! Solution:

#1. Press the 'windows' button + 'R' for run( this is shortcut key for RUN) and enter 

'cmd' to open command prompt and enter the following command:

" attrib -h -r -s /s /d g:\*.* " NOTE : Don't forget to replace the letter 'g' with the drive-name of your pen drive! This will show your files. Now copy the required file/files and save them to another folder.

Then format the pen drive and... Bazinga!

#2. Open command prompt as show in the previous solution. Then enter the following:

" del * .ink " (for deleting the shortcut).

Now enter the following command

" attrib -s -r -h *.* /s /d /l " NOTE: This will definitely recover all the folders, but it wont completely remove all the 

virus, better you scan the drive with an antivirus.








Arrays in C

Not all of us are aware of the fact that C can assign array elements in ANY ORDER!
To specify an array index and element, the proper syntax is as follows:

[index] = value inside curly braces. 

For example :
int arr[] = {[4] = 1, [1] = 5, [2] = 9 };


is equivalent to 
int arr[] = {0,5,9,0,1,0};



Also,

We can initialize a range of index to a particular value in an array. 
#Note:-This is a GNU extension (features not found in ISO standard C). 

Example :
int arr[] = { [0 ... 9] = 7, [10 ... 20] = 6, [30 ... 40] = 5};



is equivalent to 
int arr[] = { 7, 7, 7, 7, ..... 6, 6, 6 ...... 5, 5, 5};

UVa 837 - Light and Transparencies




 Question

 Solution -
#include<iostream>
#include<iomanip>
#include<vector>
#include<algorithm>
using namespace std;

struct point{
    double x;
    int tci; //  index value of tcs[]
};

struct tc{ //transmission coefficients
bool effective;
double r;
};

bool compare_point(const point& p , const point& q)
{
    return p.x<q.x;

}

int main(){
    int t;
    cin >> t;
    while(t--){
        int nl;
        cin >> nl;
        int np = nl<<1;
        vector <point> points(np);
        vector <tc> tcs(nl);
        for(int i=0;i<nl ;++i){
            double y1,y2;
            cin >> points[i<<1].x>> y1 >> points[i<<1 +1].x>> y2 >> tcs[i].r;
            points[i<<1].tci= points[i<<1+1].tci = i;
            tcs[i].effective = false;
        }
        sort(points.begin(),points.end(),compare_point);
        cout << np+1 << endl;
        cout << fixed;
        for(int i =0;i<=np ; ++i)
        {
            if(i)
            cout << setprecision(3) << points[i-1].x;
            else
            cout << "-inf";
            if(i<np)
            cout<< ' ' << setprecision(3) << points[i].x;
            else
            cout << " +inf";
            double pl = 1.0 ;//%age of light
            if(i && i<np){
                for(int j =0 ;j<nl;++j)
                {
                    if(tcs[j].effective){
                        pl*=tcs[j].r;
                    }
                }
            }
                cout << ' ' << setprecision(3) << pl << endl;
                if(i<np)
                tcs[points[i].tci].effective = !tcs[points[i].tci].effective;
          
      
        }
        if(t)
        cout << endl;
  
    }
return 0;
}

Friday, 17 January 2014

Uva : 573 : The Snail


Solution-
// UVa - 573 - The Snail
#include <cstdio>
using namespace std;
int main() {
        int h, u, d, f;
 while (scanf("%d %d %d %d", &h, &u, &d, &f), h || u || d || f) {
                double currentU = u;
                double distanceMin = u * (f / 100.0);
                double curreentH = 0;
                int currentDay = 0;
                do {
                        currentDay++;
               currentH += currU;
                      
                        if (currentH > h){
                                printf("success on day ");
                                break;
  }
                        currentH -= d;
                       
                        if (currentH < 0){
                                printf("failure on day ");
                                break;
                        }
                        currentU -= distMin;
                        if (currentU < 0)
                                currentU = 0;
                } while (true);
                printf("%d\n",currentDay);
        }
        return 0;
}

Tips to reduce running time in C++

Here are a few tips to reduce to reduce running time in C++:

1. Write ++i in for loops:
Whenever you write variable++, a temporary variable is generated which is unnecessary overhead (unless you want this specifically) . In this case it doesn't matter because the compiler will optimize an int datatype for you, but if you have huge iterators, this can make a small difference in your speed.


2. Never pass objects by copy unless you have to:
Whenever you call funct(string , string ) it will copy your strings into the function. Maybe this is what you want, but many people unknowingly do this. Instead do this, funct( const string & , const string & ) . This passes a constant reference of your string to the function. The reason to add const is that you can take a const reference to a temporary variable. (e.g. funct("Hello","World") will not work if you do funct (string & , string & ) )

3. Use inline functions for small functions:
Marcos are bad news. For small functions append the word inline in front of it. The preprocessor will just copy paste the code wherever it is called. This is much faster but you have to be careful with them.

4. Know how to use STL:
STL is a wonderful library and can be very helpful if you know how to use it. However, STL objects are on the heap ( i.e. dynamic memory) so use them judiciously.

Thursday, 16 January 2014

Create your own Matrix!

Programmers and hackers are not always boring, they always have some fun stuff to play on. 
Today we are sharing a very cool technique: How to make Matrix and feel that rush of adrenaline while watching that green code flashing on the screen! So here's the procedure to make it happen-
1- Open any editor- eg. notepad 2- type this-
@echo off color 02 :method
echo %random%%random%%random%%random%%random%%random%%random%%random% goto method
3- save as matrix.bat 4- Run it , enjoy
Surely that instigates the hacker in you! Doesn't it?

Tuesday, 14 January 2014

UVa Online Judge: Problem# 272

//By: Shreya Gupta
      NIT Srinagar//

#include<iostream>
#include<cstring>
using namespace std;
main()
{
    int len,count=1;
    char str[1000];
    gets(str);
    len=strlen(str);
    for(int i=0;i<len;i++)
    {
        if((str[i]=='"')&&(count%2==1))
        {
            for(int j=len-1;j>=i+1;j--)
            {
                str[j+1]=str[j];
            }
            str[i]='`';
            str[i+1]='`';
            str[++len]='\0';
            count++;
        }

        else if((str[i]=='"')&&(count%2==0))
        {
            for(int j=len-1;j>=i+1;j--)
            {
                str[j+1]=str[j];
            }
            str[i]='\'';
            str[i+1]='\'';
            str[++len]='\0';
            count++;
        }
    }
    puts(str);
    return 0;
}

UVa Online Judge: Problem# 100


#include <stdio.h>
main()
{
    long num,n,i,j,count,max_cycle;
    long tmp,itmp,jtmp;

    while(scanf("%ld %ld",&i,&j)!=EOF)
    {
        if(i==0 && j==0)
            break;
        max_cycle=0;
        itmp=i;
        jtmp=j;
        if(i>j)
        {
            tmp=itmp;
            itmp=jtmp;
            jtmp=tmp;
        }
        for(n=itmp;n<=jtmp;n++)
        {
            num=n;
            count=1;
            while(num!=1)
            {
                if(!(num%2))
                {
                    num/=2;
                    count++;
                }
                else
                {
                    num=(num*3)+1;
                    count++;
                }
                printf("%d\n",n);
            }
            if(max_cycle < count)
                max_cycle=count;
        }
        printf("%ld %ld %ld\n",i,j,max_cycle);
    }
    return 0;
}