Thursday, 30 May 2013

The memcpy Function in C

  The function memcpy( ) is defined in the header file <string.h>. The prototype of the function memcpy( ) is as follows.

void * memcpy(void* destination,const void* source,size_t num);

The function memcpy( ) copies the values of num bytes from the location pointed by source directly to the memory block pointed by destination. 

#include<stdio.h>
#include <string.h>

int main()
{
    int abc[ ] = {111, 222, 333};
    int copy[3],i;
    memcpy(copy, abc, sizeof(int)*3);
    abc[0] = 444;
    printf("\nArray abc is :");
    for(i=0;i<3;i++)
    {
        printf("\n%d",abc[i]);
    }
    printf("\nArray copy is :");
    for(i=0;i<3;i++)
    {
        printf("\n%d",copy[i]);
    }
    return 0;
}


Click here to download the C Program. 

The assert Macro in C

The assert macro is useful in checking conditions at run time and is very useful while debugging a program. To use assert macro the header file <assert.h> is included. The prototype of assert is as follows. 

void assert(int expression);

Where expression is any valid c language expression. If the condition holds true then the program execution will continue otherwise the execution is terminated and an error message is displayed. The error message displays file name, line number, function name, and the condition which failed.

#include<stdio.h>
#include<assert.h>

int main()
{
   int a, b;
   printf("Input two integers to divide\n");
   scanf("%d%d", &a, &b);
   assert(b != 0);
   printf("%d/%d = %.2f\n", a, b, a/(float)b);
   return 0;
}


Click here to download the C Program. 

NAN (Not a Number) in C

  The header file <math.h> defines NAN and INFINITY. NAN is a constant expression of type float representing a quiet NaN. This symbolic constant is only defined if the implementation supports quiet NaNs for the float type. INFINITY is a constant expression of type float representing positive or unsigned infinity, if available; else a positive constant of type float that overflows at translation time.

The Program given below shows the working of NAN in C.

#include <math.h>
#include <stdio.h>

double sum(double in[])
{
    double out=0;
    int i;
    for (i=0; !isnan(in[i]); i++) out += in[i];
    return out;
}

int main()
{
    double list[] = {111.1, 222.2, 333.3, NAN};
    printf("sum: %E\n", sum(list));
    printf("sum: %e\n", sum((double[]){111.1, 222.2, 333.3, NAN}));
}


Click here to download the C Program.

Pointer Arithmetic in C

  Pointers are always a great source of confusion in C Programming. Here I have included two programs which might help you learn more about pointer arithmetic.


Program 1

int main()
{
    char *list[] = {"first", "second", "third", NULL};
    char** p;
    for(p=list; *p != NULL; p++)
    {
        printf("%s\n", p[0]);
    }
    return 0;
}
Program 2
int main()
{
    char* list[] = {"one", "two", "three", NULL};
    char** p;
    for(p=list; *p != NULL; p++)
    {
        printf("%s\n", *p);
    }
    return 0;
}


Python Module to Check whether a Number is Pandigital or Not

  This Python module checks whether a given number is Pandigital or not. A number is said to be Pandigital if it contains each of the digits from 0 to 9. For example, 123456789000 is a Pandigital number in base 10.

Figure Below Shows a Sample Output


Python Module to find the Factors of a Number

  This module finds the factors of a given number. This a very useful module for solving many larger problems.

Figure Below Shows a Sample Output

Python Module to Check the Primality of a Number

  This Python module checks whether a given number is prime or not. This module is quite useful in solving many larger problems.

Figure Below Shows a Sample Output

Python Module to Remove Duplicate Elements from a List

  This module helps you to remove duplicate elements from a list. This module is very useful in solving many other larger problems. I have used this module to solve a few problems in Project Euler.

Figure Below Shows a Sample Output

Python Module to Print Fibonacci Numbers

  A module in Python is a file containing Python definitions and statements. This module helps you to print and return the list of Fibonacci numbers less than a given number n.

Figure Shows a Sample Output

Wednesday, 1 May 2013

C/C++ Program to Print Pascal's Triangle

  Pascal's Triangle is a triangular array of the binomial coefficients. The C/C++ program given in this post prints the Pascal's Triangle.