Tuesday, 30 April 2013

Stack Overflow

  Three months ago I have joined Stack Overflow. Now I am very passionate about it. SO is one of the best Computer Science forums I have ever seen. If you are serious about Computer Science then I believe it is a must for you to have membership in Stack Overflow. Membership is free and easy. 



A Humble Beginning


I have proposed a Site for Q & A on Special Education. But in order to set up this site a lot of followers are required. I truly believe that such a Q & A forum will help a lot of children with special needs. If you are interested please follow this proposal. It will be a great help. 

Here is the Link to the proposed Special Education Q & A Site.

Please Click the image below to follow the Proposed Site.
  
Stack Exchange Q&A site proposal: Special EducationEducation


Overloading Increment/Decrement Operators Using Friend Functions

  Friend functions can be used to overload Prefix and Postfix forms of Increment/Decremnt operators in C++. Sample output and code are shown below. 

Figure Below Shows a Sample Output


#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";
    }
    friend over_load operator++(over_load &ob)
    {
        ob.n1++;
        ob.n2++;
        cout<<"\nPost Incremnt Operator Overloaded\n";
    }
    friend over_load operator++(over_load &ob, int x)
    {
        ++ob.n1;
        ++ob.n2;
        cout<<"\nPre Increment Operator Overloaded\n";
    }
};

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


Click here to download the C++ Program.

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

Friday, 12 April 2013

C/C++ Program to Truncate Integers

Consider the Integer 12345. The Number can be truncated from both the left side and the right side to get the output shown in the figure. The C/C++ Program given in this post truncates Integers and prints them.

Figure Below Shows a Sample Output


Click here to download the C++ Program.

C/C++ Program to Print Truncatable Primes

  The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. Remember 2, 3, 5, and 7 are not considered to be Truncatable Primes. The C/C++ Program in this post prints Truncatable Primes truncatable from both left and right. 

Figure Below Shows a Sample Output

C/C++ Program to Print Double-Based Palindromes

  A number is called a Double-based palindrome, if it is palindromic for two different bases. For example consider the decimal number 585. 58510 = 10010010012 (Binary), is palindromic in both bases.Thus 585 is a Double-based palindrome. The C/C++ Program discussed in this post prints Double-based palindromes less than a given limit.

Figure Below Shows a Sample Output

Sunday, 7 April 2013

C/C++ Program to Print Circular Prime Numbers

   A circular prime is a prime number with the property that the number generated at each intermediate step when cyclically permuting its (base 10) digits will be prime.The number, 379, is a circular prime because all rotations of the digits: 793, 937, and 379, are themselves prime numbers. The C/C++ Program provided in this Post prints all the Circular Prime numbers less than the given limit.

Figure Below Shows a Sample Output



Click here to download the C++ Program.

C/C++ Program to Print the Cyclic Permutations of a Number

  Consider the number 1234. The number can be cyclically rotated to obtain the following numbers 4123, 3412, 2341, and 1234. The C/C++ Program in this Post prints the Cyclically Shifted numbers obtained from a given number.

Figure Below Shows a Sample Output


Click here to download the C++ Program.