Tuesday 25 September 2012

some IMP Q in C++ interviewer point of view :

some IMP Q in C++ interviewer point of view :

Ques 1: - What is difference between visual c++ & ANSI c++ ?
Ans: -Visual C++ is probably the most popular favored compiler, because of it's history of quality and stablity.But in ANSI C++ is a less popular, but is a much more powerful and robust compiler. The IDE is also a lot more powerful than MSVC.

Ques 2: - When does a name clash occur in C++ ?
Ans: - A name clash occurs when a name is defined in more than one place. For example., two different class libraries could give two different classes the same name. If you try to use many class libraries at the same time, there is a fair chance that you will be unable to compile or link the program because of name clashes.

Ques 3: -What is the difference between "overloading" and "overriding" ?
Ans:- Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overriding is the ability of the inherited class rewriting the virtual method of the base class.overridding is run time polymorphism while overloading is compile time polymorphism.

Ques 4: - What is an accessor in C++ ?
Ans: - An accessor is a class operation that does not modify the state of an object in C++. The accessor functions need to be declared as const operations

Ques 5: - What does #include do ?
Ans: - This is a directive to the preprocessor, which runs when you call your compiler. This specific directive causes the file named after the word include to be read, as if it were typed in at that location in your source code.
 

Thursday 13 September 2012

C++ program to Find out Maximum and Minimum within and Array



#include<iostream.h>
#include<conio.h>
void main()
{
        clrscr();
        int a[10];
        int max=0,min;
        int i;
        for(i=0;i<=9;i++)
        {
                cout<<"Enter the element "<<i+1<<":- ";
                cin>>a[i];
        }
        for(i=0;i<=9;i++)
        {
                if(max<a[i])
                        max=a[i];
                if(min>a[i])
                        min=a[i];
        }
        cout<<"\nMaximum in Array is "<<max;
        cout<<"\nMinimum in Array is "<<min;
   getch();
}



C++ Program to perform Matrix operations i.e Addition and Multiplication of two Matrices.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void add(int a[3][3],int b[3][3])
{
        int i,j;
        int c[3][3];
        for(i=0;i<=2;i++)
        {
                for(j=0;j<=2;j++)
                {
                        c[i][j]=a[i][j]+b[i][j];
                        cout<<c[i][j]<<"\t";
                }
                cout<<"\n";
        }
}
void mul(int a[3][3],int b[3][3])
{
        int k=0;
        int i,j;
        int c[3][3];
        for(i=0;i<3;i++)
        {
                for(j=0;j<3;j++)
                {
                        c[i][j] = 0;
                        for(k=0;k< 3;k++)
                                c[i][j] = c[i][j] + a[i][k] * b[k][j];
                        cout<<c[i][j]<<"\t";
                }
                cout<<"\n";
        }
}
void getMat()
{
        for(i=0;i<=2;i++)
        {
                for(j=0;j<=2;j++)
                {
                        cout<<"Enter the Elements ["<<i<<","<<j<<"] for Matrix 1:- ";
                        cin>>a[i][j];
                        cout<<"Enter the Elements ["<<i<<","<<j<<"] for Matrix 2:- ";
                        cin>>b[i][j];
                }
        }
}
void main()
{
        int a[3][3];
        int b[3][3];
        int i,j;
        int ch;
        cout<<"\n1.Matrix Addition.";
        cout<<"\n2.Matrix Multiplication.";
        cout<<"\n3.Exit.";
        cout<<"\nEnter you choice:- ";
        cin>>ch;
        getMat();
        switch(ch)
        {
                case 1:
                                cout<<"\nAddition of Matrix is :- ";
                                add(a,b);
                                break;
                case 2:
                                cout<<"\nMulitplication of Matrix is :- ";
                                mul(a,b);
                                break;
                case 3:
                                exit(0);
                                break;
                default:
                                cout<<"\nInvalid Option";
                                break;
        }
        getch();
}
C++ Program to Sort and Array in
1] Ascending Order
2] Descending Order


1] Ascending Order

#include<iostream.h>
#include<conio.h>
void main()
{
        int arr[10];
        int i,j,temp;
        for(i=0;i<=9;i++)
        {
                cout<<"Enter the element "<<i+1<<":- ";
                cin>>arr[i];
        }
        cout<<"\nArray before sorting:-   ";
        for(i=0;i<=9;i++)
                cout<<arr[i]<<"  ";
        for(i=0;i<=9;i++)
        {
                for(j=i;j<=9;j++)
                {
                        if(arr[i]<arr[j])
                        {
                                temp=arr[j];
                                arr[j]=arr[i];
                                arr[i]=temp;
                        }
                }
        }
        cout<<"\nArray after sorting:-    ";
        for(i=0;i<=9;i++)
                cout<<arr[i]<<"  ";
}


2] Descending Order
#include<iostream.h>
#include<conio.h>
void main()
{
        int arr[10];
        int i,j,temp;
        for(i=0;i<=9;i++)
        {
                cout<<"Enter the element "<<i+1<<":- ";
                cin>>arr[i];
        }
        cout<<"\nArray before sorting:-   ";
        for(i=0;i<=9;i++)
                cout<<arr[i]<<"  ";
        for(i=0;i<=9;i++)
        {
                for(j=i;j<=9;j++)
                {
                        if(arr[i]>arr[j])
                        {
                                temp=arr[j];
                                arr[j]=arr[i];
                                arr[i]=temp;
                        }
                }
        }
        cout<<"\nArray after sorting:-    ";
        for(i=0;i<=9;i++)
                cout<<arr[i]<<"  ";
}
below is the website where you can compile and run C++, C, PHP etc. code online

http://codepad.org/
Passing structure to the Function using call by value and call by reference.....



#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct person{
        char name[50];
        int age;
};
void disp_val(person p1)
{
        cout<<"\nName of the Person is :- "<<p1.name;
        cout<<"\nAge of the Person is :- "<<p1.age;
}
void disp_ref(person *p1)
{
        cout<<"\nName of the Person is :- "<<p1->name;
        cout<<"\nAge of the Person is :- "<<p1->age;
}
void main()
{
        clrscr();
        person p;
        cout<<"Enter the Person name:- ";
        gets(p.name);
        cout<<"Enter Age of person:- ";
        cin>>p.age;
        cout<<"\n====================================================\n";
        cout<<"\nDisplay Using Structure Passing by value";
        disp_val(p);
        cout<<"\n====================================================\n";
        cout<<"\nDisplay Using Structure Passing by reference";
        disp_ref(&p);
        getch();
}
Receiving structure from the Function using call by value and call by reference

Note:- call by reference is under construction. Will save the Modified one as soon as it is Finished.





#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct person{
        char name[50];
        int age;
};

person getdata_val()
{
        person p1;
        cout<<"Enter the Person name:- ";
        gets(p1.name);
        cout<<"Enter Age of person:- ";
        cin>>p1.age;
        return (p1);
}

/*=============Under Construction====================
person *getdata_ref()
{
        person p1;
        cout<<"Enter the Person name:- ";
        gets(p1.name);
        cout<<"Enter Age of person:- ";
        cin>>p1.age;
        return (&p1);
}

====================================================*/
void main()
{
        clrscr();
        person p1,*p2;
        p1=getdata_val();
        cout<<"\n====================================================\n";
        cout<<"\nDisplay After Receiving Structure from Function by call by value";
        cout<<"\nName of the Person is :- "<<p1.name;
        cout<<"\nAge of the Person is :- "<<p1.age;
/*=============Under Construction====================
        p2=getdata_ref();
        cout<<"\n====================================================\n";
        cout<<"\nDisplay After Receiving Structure from Function by call by reference";
        cout<<"\nName of the Person is :- "<<p2->name;
        cout<<"\nAge of the Person is :- "<<p2->age;
====================================================*/

        getch();
}

Tuesday 4 September 2012

Visit this link to Know details about the IBM Great Mind Challenge 2012.....

What's in it for students?

 http://www-07.ibm.com/in/university/greatmind/tgmc.html


https://www.ibm.com/developerworks/mydeveloperworks/groups/service/html/communityview?communityUuid=f870215a-82d8-4701-88a5-7937fb3c73c0

Monday 3 September 2012

8 Common Programming Mistakes

1. Undeclared Variables

int main()
{
  cin>>x;
  cout<<x;
}
"Huh? Why do I get an error?"

Your compiler doesn't know what x means. You need to declare it as a variable.
int main()
{
  int x;
  cin>>x;
  cout<<x;
}

2. Uninitialized variables

int count;
while(count<100)
{
  cout<<count;
}
"Why doesn't my program enter the while loop?"

In C++ variables are not initialized to zero. In the above snippet of code, count could be any value in the range of int. It might, for example, be 586, and in that situation the while loop's condition would never be true. Perhaps the output of the program would be to print the numbers from -1000 to 99. In that case, once again, the variable was assigned a memory location with garbage data that happened to evaluate to -1000.

Remember to initialize your variables.

3. Setting a variable to an uninitialized value

int a, b;
int sum=a+b;
cout<<"Enter two numbers to add: ";
cin>>a;
cin>>b;
cout<<"The sum is: "<<sum;
When Run:
Enter two numbers to add: 1 3
The sum is: -1393
"What's wrong with my program?"

Often beginning programmers believe that variables work like equations - if you assign a variable to equal the result of an operation on several other variables that whenever those variables change (a and b in this example), the value of the variable will change. In C++ assignment does not work this way: it's a one shot deal. Once you assign a value to a variable, it's that value until you reassign the values. In the example program, because a and b are not initialized, sum will equal an unknown random number, no matter what the user inputs.

To fix this error, move the addition step after the input line.
int a, b;
int sum;
cout<<"Enter two numbers to add: ";
cin>>b;
cin>>a;
sum=a+b;
cout<<"The sum is: "<<sum;

4. Using a single equal sign to check equality

char x='Y';
while(x='Y')
{
  //...
  cout<<"Continue? (Y/N)";
  cin>>x;
}
"Why doesn't my loop ever end?"

If you use a single equal sign to check equality, your program will instead assign the value on the right side of the expression to the variable on the left hand side, and the result of this statement is the value assigned. In this case, the value is 'Y', which is treated as true. Therefore, the loop will never end. Use == to check for equality; furthermore, to avoid accidental assignment, put variables on the right hand side of the expression and you'll get a compiler error if you accidentally use a single equal sign as you can't assign a value to something that isn't a variable.
char x='Y';
while('Y'==x)
{
  //...
  cout<<"Continue? (Y/N)";
  cin>>x;
}

5. Undeclared Functions

int main()
{
  menu();
}
void menu()
{
  //...
}
"Why do I get an error about menu being unknown?"

The compiler doesn't know what menu() stands for until you've told it, and if you wait until after using it to tell it that there's a function named menu, it will get confused. Always remember to put either a prototype for the function or the entire definition of the function above the first time you use the function.
void menu();
int main()
{
  menu();
}
void menu()
{
  ...
}

6. Extra Semicolons

int x;
for(x=0; x<100; x++);
  cout<<x;
"Why does it output 100?"

You put in an extra semicolon. Remember, semicolons don't go after if statements, loops, or function definitions. If you put one in any of those places, your program will function improperly.
int x;
for(x=0; x<100; x++)
  cout<<x;

7. Overstepping array boundaries

int array[10];
//...
for(int x=1; x<=10; x++)
  cout<<array[x];
"Why doesn't it output the correct values?"

Arrays begin indexing at 0; they end indexing at length-1. For example, if you have a ten element array, the first element is at position zero and the last element is at position 9.
int array[10];
//...
for(int x=0; x<10; x++)
  cout<<array[x];

8. Misusing the && and || operators

int value;
do
{
  //...
  value=10;
}while(!(value==10) || !(value==20))
"Huh? Even though value is 10 the program loops. Why?"

Consider the only time the while loop condition could be false: both value==10 and value==20 would have to be true so that the negation of each would be false in order to make the || operation return false. In fact, the statement given above is a tautology; it is always true that value is not equal to 10 or not equal to 20 as it can't be both values at once. Yet, if the intention is for the program only to loop if value has neither the value of ten nor the value of 20, it is necessary to use && : !(value==10) && !(value==20), which reads much more nicely: "if value is not equal to 10 and value is not equal to 20", which means if value is some number other than ten or twenty (and therein is the mistake the programmer makes - he reads that it is when it is "this" or "that", when he forgets that the "other than" applies to the entire statement "ten or twenty" and not to the two terms - "ten", "twenty" - individually). A quick bit of boolean algebra will help you immensely: !(A || B) is the equivalent of !A && !B (Try it and see; you can read more about this rule on Wikipedia: DeMorgan's Law). The sentence "value is other than [ten or twenty]" (brackets added to show grouping) is translatable to !(value==10 || value==20), and when you distribute the !, it becomes !(value==10) && !(value==20).

The proper way to rewrite the program:
int value;
do
{
  //...
  value=10;
}while(!(value==10) && !(value==20))




Sunday 2 September 2012

Recursion

The process of solving a problem by reducing it into smaller versions of itself is called recursion. This problem solving technique can be a very powerful way to solve certain types of problems that would be very verbose and lengthy using other techniques such as an iterative approach.
When implementing a recursive solution one usually has at least two cases:
  1. Base Case
  2. General Case
For a function/method to be called recursive, it usually has a call to itself within its code in the general case, with a smaller case being passed through.
A typical example to illustrate a recursive function is the factorial function (i.e 5!)
//************************************
// Returns the factorial of a number
//************************************
int fact(int num)
{
    if(num <= 0)
        return 1;                            // Base Case
  
else
        return
num * fact(num-1);    // General Case - also note it calls itself }

Things to know about Recursion
Direct Recursion – a function that calls itself within its block.
Indirect Recursion – a function that calls another function and eventually returns in the original function.
Infinite Recursion – when the recursive function never returns to the base case but continually calls the general case. This is one of the big pitfalls of recursion, and one should be careful to ensure that the base case will always be met so that the system can exit the call loop.
To design a recursive function one should do the following:
  • Understand the problem requirements
  • Determine the limiting conditions
  • Identify the base case and provide a direct solution to each base case
  • Identify the general cases and provide a solution to each general case in terms of smaller versions of itself
Some problems that can be solved elegantly using recursion are listed below…
  • Find the largest element in an array
  • Print a Linked List in Reverse Order
  • Fibonacci Number
  • Tower of Hanoi
  • Converting a Number from Decimal to Binary
  • etc

Escape Sequences

Escape Sequence
Represents
\a
Bell (alert)
\b
Backspace
\f
Formfeed
\n
New line
\r
Carriage return
\t
Horizontal tab
\v
Vertical tab
\'
Single quotation mark
\ "
Double quotation mark
\\
Backslash
\?
Literal question mark
\ ooo
ASCII character in octal notation
\x hh
ASCII character in hexadecimal notation
\x hhhh
Unicode character in hexadecimal notation if this escape sequence is used in a wide-character constant or a Unicode string literal.
For example, WCHAR f = L'\x4e00' or WCHAR b[] = L"The Chinese character for one is \x4e00".

Difference between '\n' and endl

When writing output in C++, you can use either std::endl or '\n' to produce a newline, but each has a different effect.
  • std::endl sends a newline character '\n' and flushes the output buffer.


  • '\n' sends the newline character, but does not flush the output buffer. The distinction is very important if you're writing debugging messages that you really need to see immediately, you should always use std::endl rather than '\n' to force the flush to take place immediately.

    The following is an example of how to use both versions, although you cannot see the flushing occuring in this example.
     
    #include <iostream> 
    int main(void)
    {
      cout <<"Testing 1" <<endl;
      cout <<"Testing 2\n";
    }
    
    /*
     * Program output:
     Testing 1
     Testing 2
    
     *
     */
    
    
  • C++ Operators

    Additive
    Assignment
    Bitwise
    Logical
    Miscellaneous
    Multiplicative
    Postfix
    Relational and Equality
    Shift
    Unary
    Address-of: &
    delete
    Indirection: *
    Logical Negation: !
    new
    One's Complement: ~
    Prefix decrement: ––
    Prefix increment: ++
    sizeof
    Unary Plus Operator: +
    Unary Negation Operator: -