Monday, 29 September 2014

Setting up gcc compiler in Windows Operating System

The biggest issue with Windows Users doing Coding for the first time is to find a reliable C Compiler for Windows. Though we have a number of compilers available for Windows, gcc compiler which is available in Unix based systems is the best C Compiler accepted unanimously worldwide.

Hence in this step by step tutorial I will try to help you install a gcc compiler on your Windows System.

Step 1 

Downloading and Installing MinGW and GCC
Go to the MinGW download site and download the file mingw-get-setup.exe. 

http://sourceforge.net/projects/mingw/files/Installer/

Run the downloaded file on your computer, accept all the default settings and continue. You should get the following screen:


Click on “Continue” and the MinGW Installation Manager will open and look like this:

Mark the following packages for installation
mingw-base
mingw-gcc-g++
Click on Installation->Apply Changes.
After the installation is complete click on “Close” and exit the MinGW Installation Manager.

Step 2

Download and Install Notepad++
Go to the Notepad++ download page:

http://notepad-plus-plus.org/download/v6.6.9.html

Download Notepad++ Installer.
Run the Installer and accept all the default options to install Notepad++.
You should now be able to run Notepad++ from the Start menu.

Step 3

Setting up the system for programming
First of all create new folder on your disk to keep all the programs separately. Lets say you create it in your C drive can call it “code”.
Since MinGW was installed using default options, it should be in the C:\MinGW folder.
We will now create a short batch file to setup the command prompt to work with MinGW.  
Open Notepad++, create a new file with just the following command:
PATH=C:\MinGW\bin;%PATH%
Save the file as “setup.bat” in the folder c:\code.

Alternatively Right click on My Computer and in the Environmental Variables copy C:\MinGW\bin; and add it to the Path. 

Now you are ready to edit and compile programs.

Step 4

How to edit and compile programs

Open Notepad++, write your program there and save it in the C:\code folder. Lets say the program is called “sample.c”.
Open the Windows Command Prompt by typing cmd in the Start --> Run. Change to the code folder by typing:
cd c:\code
Setup the command prompt using our batch file like this
C:\code>setup.bat
You can now compile your program like this:
C:\code>gcc sample.c
If there are errors, you will have to go back to Notepad++ to change the code.
If compilation succeeds, you will see the file a.exe created in the same folder.
Run the program like this:
C:\code>a.exe
By default every successful compilation will result in a.exe getting created and overwritten for every subsequent compile. If you want a different name for your compiled program, you should add the output file name to the gcc command like this:
C:\code>gcc -o sample.exe sample.c
And then run the program like this:
C:\code>sample.exe

Tips
You only need to run the setup.bat file once when you open the command prompt. After that the settings will stay until you close that command prompt.

That’s it !! You are ready to code.
Happy Coding.

Tuesday, 24 September 2013

C++ - Assignment No. 1

Question 1 :- 1.     Fibonacci Series where upper & lower bound will be user defined.

      #include<iostream>

using namespace std;

int Fibonacci (int);                                     //Function prototype declaration

int main(void)
{
int m,n,i;

cout<<"\nEnter the lower bound : ";
cin>>m;

cout<<"\nEnter the upper bound : ";
cin>>n;
       
        if(m>=n)  
cout<<"\nInvalid Range! Check your boundary values. ";  //Checking range validity

else
for(i=m; i<=n; i++)
cout<<Fibonacci(i)<<"  ";     //Calling the Fibonacci function
cout<<"\n";
return 0;
}

int Fibonacci (int number)
{
if (number==0)                                         //First number is 0
return 0;
if (number==1)                                        //Second number is 1
return 1;
else
return Fibonacci(number-1) + Fibonacci(number-2);  //Formula for number >=2
}

Question 2 :- Write a Program to find GCD, LCM of two numbers using recursion.

#include<iostream>
using namespace std;
int gcd(int, int);
int lcm(int, int);
int main()
{
int x, y;
cout<<"\n Enter the first number : ";
cin>>x;
cout<<"\n Enter the second number : ";
cin>>y;
cout<<"\n The GCD of "<<x<<" and "<<y<<" is : "<<gcd(x,y);
cout<<"\n The LCM of "<<x<<" and "<<y<<" is : "<<lcm(x,y);
cout<<"\n ";
return 0;
}
int gcd(int x, int y)
{
    while (x !=y)
    {
        if (x > y)
        {
            return gcd(x-y, y);
        }
        else
        {
            return gcd(x, y-x);
        }
    }
    return x;
}
int lcm (int x, int y)
{
int result;
result = (x*y)/gcd(x,y);           //Since x*y=lcm*gcd
return result;
}

Question 3 :- Find the smallest and largest no in an array.

#include<iostream>
using namespace std;
int main () 
{

    int values[ 20 ];
    int i,n,small,big; 

    cout<<"\nEnter the number of elements in the array: ";
cin>>n;
    for (i = 0; i < n; i++ ) 
    {
        cout << "\nEnter the value no." << i << ": ";
        cin >> values[i];
    }
     
big=small=values[0];
    for (i = 0; i < n; i++)
    {
        if(values[i]>big) 
        {
            big=values[i];
        }
    }

    for (i = 0; i < n; i++)
    {
        if(values[i]<small) 
        {
            small=values[i];
        }
    }

    cout << "\nThe biggest number is " << big; 
    cout << "\nThe smallest number is " << small ; 
    cout<<"\n";
    return 0;
}

#     
 

Monday, 19 August 2013

Generating Fibonacci Series with user defined lower and upper limits in C++

#include<iostream>

using namespace std;

int Fibonacci (int);                                     //Function prototype declaration

int main(void)
{
int m,n,i;

cout<<"\nEnter the lower bound : ";
cin>>m;

cout<<"\nEnter the upper bound : ";
cin>>n;
       
        if(m>=n)  
cout<<"\nInvalid Range! Check your boundary values. ";  //Checking range validity

else
for(i=m; i<=n; i++)
cout<<Fibonacci(i)<<"  ";     //Calling the Fibonacci function
cout<<"\n";
return 0;
}

int Fibonacci (int number)
{
if (number==0)                                         //First number is 0
return 0;
if (number==1)                                        //Second number is 1
return 1;
else
return Fibonacci(number-1) + Fibonacci(number-2);  //Formula for number >=2
}

Wednesday, 14 August 2013

TCS CodeVita 2013


TCS CodeVita 2013 Round 1 held on 13-14th August 2013

Problem No. 1  :- Pagination

Pagination is the process of dividing content into discrete pages. Pagination is common in Web-based applications, and is used for limiting the result set and displaying a limited number of records on the web page.
E.g.:
Consider the Department-Employee scenario. If the search operation is performed on the basis of Department Id, and there are 100,000 employees per department, then it does not make sense to display the details of 100,000 employees in single page. So, pagination allows to display limited results e.g. 20 records per page. "Previous" and "Next" links or the Page numbers are usually provided on the user interface so that users can navigate to other pages.
Q. Write a program for selecting the records that need to be displayed on user interface. Records should be filtered as per the input parameters, and should be sorted by Employee Name. Requirement is to ensure faster initial page load.
Your program should extract the records from this file on the basis of input parameters. For example, if the input is (Department ID-10, Page Size-20, and Page Number -1), then the program should retrieve first set of 20 records for Department ID-10 sorted by employee name. If the input is (Department ID-10, Page Size-20, and Page Number -2), then the program should retrieve second set of 20 records. As the data for a given department will be sorted by employee name, there will be no overlapping between first page and second page.
Asumptions:
->Input file data may or may not be sorted by Department and Employee ID
->Expected Volumes: Department Data File may have approx. 100,000 records per department ID. Page Size is expected to be less than 100 records.

Problem No. 2 :- Isotope


Scientist recently found a new element X, which can have different isotopes upto an atomic value of 199. Speciality of element X is that when two atoms fuse they produce energy multiple of their atomic value and forms an atom with atomic value of their multiple modulus 199.
For Eg:
If atom1 with value 56 and atom2 with value 61 fuse.
They produce energy of 3416 KJ (56 * 61)
Resulting atom will have atomic value (56*61) mod 199 = 33
Scientists created a nuclear reactor to create energy by this method. Everyday they get several atoms of X from supplier in a particular sequence. Since this element highly radioactive they cant risk by changing its sequence. So each atom can fuse only with another atom nearby. Nevertheless scientists can choose order of fusion thereby maximizing total energy produced.
Now, for given sequence of atomic values, output maximum energy that can be produced.
For Eg:
If sequence of atoms are
56,61 & 2
Then they can produce 3416KJ by fusing 56&61 which results in an atom of value 33. Then they can fuse 33 and 2 to get energy of 66KJ. So total energy generated is 3482.

But if they cleverly choose to fuse atoms 61 & 2 first then they generate 122 KJ with a resulting atom of value 122. Then if they fuse 122 and 56, they can generate 6832 KJ. So total energy is 6954.
Hence Maximum energy that can be generated from this sequence is 6954.
Input Format
Input starts with a number specifying number of inputs(n) followed by n space separated integers specifying atomic values of n atoms.
Line 1
N,where N is the number of atoms in the sequence to be fused.
Line 2
a1 a2 a3 .... an
where ai -> Atomic value of i th atom.
and two atoms are space delimited
Constraints:\0 < ai < 199
1 < n < 1000

Problem No. 3 – Fun with Primes


A prime number (or a prime) is a natural number greater than 1 that can only be divided by 1 and itself.
Ex-First few Prime number are-
2, 3, 5, 7, 11, 13, 17, 19,...
A digit prime is a prime number whose sum of digits is also prime.
For example the prime number 23 is a digit prime because 2+3=5 and 5 is a prime number. 17 is not a digit prime because 1+7 = 8, and 8 is not a prime number. Write a program to find out the number of digit primes within a certain range less than 1000000.and also print the Mth digit prime number between the range.
Input:
Input line contains three positive integers (T1 T2 M) separated by space where lower limit is T1 and upper limit is T2 and M is the position of the required digit prime number.
Line 1
T1 T2 M, where T1 is upper limit, T2 is lower limit and M is the position of the required digit prime number.
Constraints:
0 < T1 <= T2 < 1000000
0 < M
Output:
Print Mth digit prime number between T1 and T2(inclusive).

Problem No. 4 – Play with Alphabets


Byteland country is very famous for it's own variety of rules and customs. People from the normal world only knows 26 alphabets but the scientists in the Byteland country always try to invent new alphabets. Let N be the number of alphabets invented by the Byteland country scientists. Alice is living in Byteland country and one of her friends gifted her a new pet on her Birthday. In the Byteland country each pet must contain a name as suggested by the astronomer. After consulting an astronomer Alice came to know that length of the pet name must be of L letters and among L letters she need to keep X number of letter prefix as suggested by the astronomer.
Now Alice has decided to form the names by following the astronomer's suggestion, as Alice is weak in mathematics can you please help her to find out the number of valid names that she can form. As the combinations can be too high so mod the result by 1000000007.

Input Format:
Input contains three integers N, L, X in each line.
Given a file with certain records, find records that start with or end with a particular pattern.

Problem No. 5 :- Company Problem


Information:
Each record in a file consists of 3 distinct parts
Name of the company
Company Information Number (CIN)
Region code (where the company is based out of)
File Format
File consists of records. Records are delimited by a new-line character. Within a record, the 3 distinct parts are delimited by a string #@%#. For e.g consider the following lines

GERMAN CHEMICALS PRIVATE LIMITED#@%#U99999MH2000PTC123825#@%#RC201
GERMAN EXPRESS SHIPPING AGENCY (INDIA) PVT.LTD.#@%#U61100MH1993PTC071596#@%#RC202
GERMAN FABS PRIVATE LIMITED#@%#U17110MH1995PTC089052#@%#RC203
Here GERMAN CHEMICALS PRIVATE LIMITED is the name of the company, U99999MH2000PTC123825 is the CIN and RC201 is Region Code.
Similarly, GERMAN EXPRESS SHIPPING AGENCY (INDIA) PVT.LTD. is the name of the company, U61100MH1993PTC071596 is the CIN and RC202 is Region Code.
And, GERMAN FABS PRIVATE LIMITED is the name of the company, U17110MH1995PTC089052 is the CIN and RC203 is Region Code.
Constraints:
Company Name can be alphanumeric, can include white-space and special characters. Length of Company Name can vary.
CIN can be alphanumeric, and cannot include white-space or special characters. Length of CIN can vary.
Region Code can be alphanumeric, and cannot include white-space or special characters. Length of Region Code can vary.

Problem No. 6 :- Sculptor


Rama is a talented painter. A less known fact is that Rama also dabbles in modern sculptor. Recently, he has taken a fancy to protecting the environment, recycling junk etc. Since then, he has been using only recycled material in his sculptores.
The construction work at the site for the new campus for the Green Society of India has generated its own pile of junk. Among this junk are a large number of steel rods of varying lengths. Rama plans to use some of these in his new sculptor to be placed at the entrance, which will consist of a sequence of steel rods placed upright in a line. However, he wants the rods to be arranged in increasing order of height and, further, demands that the difference in height between each pair of adjacent rods be the same. Your task is to help him identify the largest set of rods that can be used in his sculptor.
For example, if there are 5 rods of lengths 2, 3, 6, 7 and 11 then the desired set consists of the three rods of lengths 3, 7 and 11.

Input Format:

The first line of the input consists of a single integer N indicating the number of rods. This is followed by a single line consisting of N integers indicating the lengths of the N rods.
Line 1 :- N,where N is the number of rods
Line 2
a1 a2 a3 .... an,where ai -> the lengths of the rods
and two length of rods are space delimited


Output Format:
The first line of the output consists of a single integer M indicating the maximum number of rods from this collection that may be used in Rama's sculptor. This is followed by a single line containing M integers indicating the lengths of the rods in a sculptor with M rods, in ascending order. If a group of rods with increasing order of height could not be found then you can identify the largest group of rods with difference in height between each pair of adjacent rods as zero. If there is more than one such solution then it suffices to print out one which have maximum value of length. The program should print Invalid Input when the input values do not lie within the specified boundary.
Line 1   
For Valid Input,print
M,where M is Integer stating maximum number of rods that can be used
For Invalid Input,print
Invalid Input
Line 2   
Containing M integers indicating the lengths of the rods in a sculptor with M rods, in ascending order
Two length of rods are space delimited
Constraints:
1 <= N <= 2000

M > 0