Monday, 30 June 2014

Yet Another Difference Between C and C++: Initializing Constant Variables


  Constant variable initialization is yet another minute difference between C and C++. Consider the program given below.

#include<stdio.h>
int main()
{
  int x = 111;
  static int y = x;
  if(x == y)
     printf("Equal");
  else
     printf("Not Equal");
  return 0;
}


If you save the program as a C++ program, it compiles fine and shows the output as "Equal". But the same program when compiled as C will give you the following error "initializer element is not constant|". What is the reason? This is another minor difference between C and C++. In C a constant variable can only be initialized with a constant value and not by using another variable. But C++ do not have this restriction. You can initialize a constant variable with any other variable.

Click here to download the C++ program.

Yet Another Difference Between C and C++: Case Labels


 There are many major differences between C and C++. Such differences are matters for Text Books and Manuals. Here I am pointing out yet another minor difference between C and C++. This time the difference arises from the way case labels are treated in C and C++. 

 C++ will allow const int variables as case labels, whereas C will give you an error. Consider the program given below.


#include<stdio.h>

int main()
{
    int i=1;
    const int j=1;
    switch(i)
    {
        case j:
            printf("\nHello\n\n");
            break;
        case 2:
            printf("\nHi\n\n");
            break;
        default:
            printf("\nWelcome\n\n");
    }
    return 0;
}

The program will give the error 'case label does not reduce to an integer constant' when compiled as a C program. But when compiled as a C++ program the program executes without any errors and gives 'Hello' as output. 

C/C++ Puzzles: PART - 36


  The following program will give you syntax error.


#include<stdio.h>

int main()
{
    int a=10,b=5,c;
    c=a>b?a:;
    printf("\n%d\n\n",c);
    return 0;
}

Whereas the program given below will work without any syntax errors. Predict the output of the program.


#include<stdio.h>

int main()
{
    int a=10,b=5,c;
    c=a>b?:b;
    printf("\n%d\n\n",c);
    return 0;
}





Thursday, 29 May 2014

C/C++ Puzzles: PART - 35


   C Program Tricked to Believe X equals X+2

  The puzzle has been discussed previously, but only one solution was provided. But now I have added a few more solutions to this puzzle. The challenge is to get a true value for the following conditional statement. 

x==x+2  

You are free to use any feature of the C preprocessor or Compiler. If you can't get a solution then check the programs shown below.

Solution 1

#define x 2|0
#include<stdio.h>

int main()
{
    if(x == x+2)
    {
        printf("\n Hello");
    }
    else
    {
        printf("\n Hi");
    }
}


Solution 2

#include<stdio.h>

int main()
{
    float x=1.0/0;
    if(x==x+2)
    {
        printf("\nHello");
    }
    else
    {
        printf("\nHi");
    }
}

Solution 3

#include<stdio.h>

int main()
{

    float x = 1e100;
    if(x==x+2)
    {
        printf("\nHello");
    }
    else
    {
        printf("\nHi");
    }
}

Solution 4

#include<stdio.h>

struct {}*x=0;

int main()
{
    if(x==x+2)
    {
        printf("\nHello");
    }
    else
    {
        printf("\nHi");
    }
}

Solution 5

#include<stdio.h>

int (*x)[0x20000000];

int main()
{

    if(x==x+2)
    {

        printf("\nHello");
    }
    else
    {
        printf("\nHi");
    }
}

Solution 6

#include<stdio.h>

int main()
{
/////////////////////////////////////
// we initialize tr = 1            //

int tr = 1;
int x = 111;

////////////////////////////////////
// Can You change the value of tr??/
tr = (x == x + 2);

/////////////////////////////////////
// The value of tr is not changed  //
    if(tr==1)
    {
        printf("\nHello");
    }
    else
    {
        printf("\nHi");
    }
}


Power Method to Find the Largest Eigenvalue


   Power method is used to find the numerically largest eigenvalue. The method sometimes converge very slowly. In this post I am providing the C and C++ programs to implement power method.

Figure Below Shows a Sample Output




Crout's Reduction Method for Solving System of Linear Equations.


   Crout's reduction method is used to solve system of linear equations. In this post I have included the C and C++ program implementing Crout's reduction method.

Figure Below Shows a Sample Output

Wednesday, 30 April 2014

Fitting a Parabola Using Method of Moments : C/C++ Program


   In this post I have shared the C and C++ programs to fit a Parabola using the method of moments.

Figure Below Shows a Sample Output