Friday, 26 April 2013

Overloading Prefix and Postfix Increment/Decrement Operators in C++

  In C++ operators can be overloaded. Problems can arise when we have to overload Increment/Decrement Operators of C++. We need to distinguish between Prefix and Postfix forms of Increment/Decrement Operators. The program given below shows the prototype for Prefix and Postfix forms of Increment/Decrement Operators.

#include<iostream>

using namespace std;

class over_load
{
    int n1,n2;
    public:
    void set(int x, int y)
    {
        n1=x;
        n2=y;
    }
    void show()
    {
        cout<<"\nNumber 1 is "<<n1<<"\n";
        cout<<"\nNumber 2 is "<<n2<<"\n";
    }
    over_load operator++()
    {
        n1++;
        n2++;
        cout<<"\nPost Incremnt Operator Overloaded\n";
        return *this;
    }
    over_load operator++(int x)
    {
        ++n1;
        ++n2;
        cout<<"\nPre Increment Operator Overloaded\n";
        return *this;
    }
};

int main()
{
  over_load ob1;
  ob1.set(100,200);
  cout<<"\nObejct ob1 Initially:\n";
  ob1.show();
  ob1++;
  ob1.show();
  ++ob1;
  ob1.show();
  return 0;
}



Figure Below Shows a Sample Output

Tuesday, 23 April 2013

C/C++ Program to Print Triangular, Pentagonal, and Heptagonal Numbers

  The C/C++ Program provided with this post prints the Triangular, Pentagonal, and Heptagonal Numbers.
Figure Below Shows a Sample Output



C/C++ Puzzles: PART - 33

C Program Showing Errors When Compiled as C++ Program 

  Write a C Program without any errors, but the same when compiled as a C++ Program should show errors? This was a question I had to answer recently. There are many different answers I suppose. The program given below is the one which comes to my mind first. 

#include<stdio.h>

void func();

int main()
{
  int x;
  x=333;
  func(x);
  return 0;
}

void func(int x)
{
    printf("\nValue is %d\n",x);
}

The program gives an error when compiled as a C++ program because in C a function with an empty parameter list can have any number of parameters including zero parameters where as in C++ it means the function can't receive any parameter.

Sunday, 21 April 2013

C Program to Print Pan Digital Prime Numbers

  An n-digit number is pan digital if it makes use of all the digits from 1 to n exactly once. For example, 2143 is a 4-digit pan digital number and is also prime. The C Program in this post prints the Pan digital prime numbers less than a given limit.

Figure Below Shows a Sample Output

Friday, 19 April 2013

Different Standards of C

  Many often beginners to C Programming are unaware of the fact that there are different standards for C Programming language. I had a few posts about C11 and C99 in this blog. But in this post I am listing all the C standards in a chronological order, from the oldest to the newest. 

1. K&R C
    This is the original standard of C. Some of the features and functions provided by this version is deprecated now.

2. ANSI C & ISO C (C89 & C90)
      This is the most common C standard, supported by most IDEs as the default version for programming. Many of the Universities and Colleges still teach this standard of C to students. Fine with me, but they should make sure that there is at least some discussion about the different standards of C. At least about the C99 standard since it adds a lot of power to the C language and implemented by large number of IDEs and Compilers.
  
3. C99
       C99 is having support from fairly large number of IDEs. C99 increases the power of C programming language.


4. C11
          This is the latest standard of C programming language. The proposals made by C11 are not yet fully implemented by many of the popular IDEs.


Wednesday, 17 April 2013

C/C++ Program to Print Large Fibonacci Numbers

   The Fibonacci Series is  0, 1, 1, 2, 3, 5, 8, 13, 21, ... Printing the Fibonacci Numbers is a very trivial task for any person familiar with the basics of programming. But what happens when you have to print very large Fibonacci numbers? Then C/C++ or even languages like MATLAB or Mathematica may not have sufficiently large data types to store the results. In this C/C++ Program I have used an array to store Fibonacci Numbers. The C Program also  stores the results into a text file called fib.txt.

Figure Below Shows a Sample Output



Sunday, 14 April 2013

C/C++ Program to Print Number Spiral Matrix

Consider the following 5 X 5 Matrix. The numbers are printed as a spiral in the matrix starting with 1.

21 22 23 24 25
20  7  8  9 10
19  6  1  2 11
18  5  4  3 12
17 16 15 14 13

The C/C++ Program provided in this Post prints Number Spirals of arbitrary size.

Figure Below Shows Sample Output for a 9 X 9 Matrix