Sunday, 4 August 2013

INTERNAL REPRESENTATION OF FLOATING POINT NUMBERS

QUES:-
Observe and verify the internal representation of floating point numbers.
ANS:-
/******************************************************************************/
#include<conio.h>
#include<stdio.h>

void decimal_to_binary(unsigned char);

union u
{
    float f;
    char c;
};

int main()
{
    int i;
    char*ptr;
    union u a;

    clrscr();
    printf("ENTER THE FLOATING POINT NUMBER : \n");
    scanf("%f",&a.f);

    ptr=&a.c+sizeof(float);

    for(i=0;i<sizeof(float);i++)
    {
        ptr--;
        decimal_to_binary(*ptr);
    }

    getch();
    return 0;
}

void decimal_to_binary(unsigned char n)
{
    int arr[8];
    int i;
    //printf("n = %u  ",n);

    for(i=7;i>=0;i--)
    {
        if(n%2==0)
            arr[i]=0;
        else
            arr[i]=1;
        n/=2;
    }

    for(i=0;i<8;i++)
        printf("%d",arr[i]);
    printf(" ");
}
/******************************************************************************/

CONCEPT OF UNION IN C

QUES:-
Verify the union concept in C uses the same memory to store the various data.
ANS:-
/*******************************************************************************/
 #include<stdio.h>
#include<conio.h>

union u
{
    char c;
    int i;
    float f;
    double d;
};

int main()
{
    union u a;

    clrscr();

    printf("%d",sizeof(a));

    printf("\nAddress of a.c = %u",&a.c);
    printf("\nAddress of a.i = %u",&a.i);
    printf("\nAddress of a.f = %u",&a.f);
    printf("\nAddress of a.d = %u",&a.d);
    getch();
    return 0;
}

/*******************************************************************************/

INTERNAL ORDERING OF STRUCTURE DATA TYPE IN C

QUES:-
Check the internal ordering of struct datatype in C.
ANS:-
/*******************************************************************************/
#include<stdio.h>
#include<conio.h>

struct s1
{
    int i1;
    char c1;
    int i2;
    char c2;
};

struct s2
{
    int i1;
    int i2;
    char c1;
    char c2;
};

int main()
{
    struct s1 var1;
    struct s2 var2;

    clrscr();
    printf("\nSize of var1 = %d",sizeof(var1));
    printf("\nSize of var2 = %d",sizeof(var2));

    printf("\n\nAddress of var1.i1 = %u",&var1.i1);
    printf("\nAddress of var1.c1 = %u",&var1.c1);
    printf("\nAddress of var1.i2 = %u",&var1.i2);
    printf("\nAddress of var1.c2 = %u",&var1.c2);


    printf("\n\nAddress of var1.i1 = %u",&var2.i1);
    printf("\nAddress of var1.i2 = %u",&var2.i2);
    printf("\nAddress of var1.c1 = %u",&var2.c1);
    printf("\nAddress of var1.c2 = %u",&var2.c2);

    getch();
    return  0;
}


/*******************************************************************************/

ADDRESS CALCULATION FUCTION FOR 2-D ARRAY

QUES:-
Verify the address calculation function for 2-D array and verify
ANS:-
/******************************************************************************/
#include<stdio.h>
#include<conio.h>

#define cols 10
#define rows 10

int main()
{
    int arr[rows][cols];
    int r,c;
    unsigned int cal_add,base_add;

    clrscr();
    printf("ENTER THE ROW NUMBER (between 0-9):\n");
    scanf("%d",&r);
    printf("ENTER THE COLUMN NUMBER (between 0-9): \n");
    scanf("%d",&c);

    printf("\nActual Address = %u",&arr[r][c]);

    base_add=(unsigned int)arr;
    /* row major order */
    cal_add=base_add+ (r*cols + c)*sizeof(int);

    printf("\nCalculated address = %u",cal_add);

    getch();
    return 0;
}

/******************************************************************************/

 

ADDRESS CALCULATION FUNCTION OF 1-D ARRAY

QUES:-
Check or verify the address calculation function for 1-D array
ANS:-
/*******************************************************************************/
#include<stdio.h>
#include<conio.h>

int main()
{
    int n;
    unsigned int cal_add,base_add;
    int arr[10];
    clrscr();

    printf("ENTER AN INTEGER BETWEEN 0 - 9 :\n");
    scanf("%d",&n);

    base_add=(unsigned int)arr;
    cal_add=base_add+n*sizeof(int);

    printf("Actual Address = %u\n",&arr[n]);
    printf("Calculated Address = %u",cal_add);
    getch();
    return 0;
}

/*******************************************************************************/

IMPLEMENTING DATES IN C - ALL OPERATIONS ON DATE

QUES:-
Implement how date can be represented as numbers
Implement
  • difference of dates
  • increment/decrement of date by fixed number of days
  • input/output of dates
ANS:-
/******************************************************************************/

/*
    PROGRAM FOR APPLYING DATE OPEREATION
*/

#include<stdio.h>
#include<conio.h>

long formDate(int,int,int);
long incrementByDays(long,long);
long decrementByDays(long,long);
void printDate(long);
long formDate(int,int,int);
void checkForWeekDay(long);
int isLeapYear(int);
long countDaysInYear(int);
int validation(long);
long countNumberOfDays(long,long);
int daysIn(int,int);

int main()
{
    long date,date1,date2,days,newdate;
    int choice,year,month;
    while(1)
    {
        printf("\nENTER ANY DATE IN YYYYMMDD FORMAT WITHOUT SPACES\n");
        printf("\nYOU HAVE THE FOLLOWING CHOICES : ");
        printf("\n1.CHECK WHETHER A DATE IS VALID OR NOT");
        printf("\n2.CHECK FOR LEAP YEAR");
        printf("\n3.COUNT THE NUMBER OF DAYS BETWEEN TWO DATES");
        printf("\n4.CHECK THE NUMBER OF DAYS IN A MONTH OF A PARTICULAR YEAR");
        printf("\n5.INCREMENT THE DATE BY FIXED NUMBER OF DAYS");
        printf("\n6.DECREMENT THE DATE BY FIXED NUMBER OF DAYS");
        printf("\n7.CHECK THE DAY OF WEEK ON A PARTICULAR DATE");
        printf("\n8.EXIT");

        printf("\n\nENTER YOUR CHOICE : ");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                printf("ENTER A DATE : ");
                scanf("%ld",&date);
                if(validation(date))
                    printf("\nYOU HAVE ENTERED A VALID DATE");
                else
                    printf("\nYOU HAVE NOT ENTERED A VALID DATE");
                break;

            case 2:
                printf("\nENTER THE YEAR : ");
                scanf("%d",&year);
                if(isLeapYear(year))
                    printf("\nTHE YEAR YOU ENTERED IS A LEAP YEAR");
                else
                    printf("\nTHE YEAR YOU ENTERED IS NOT A LEAP YEAR");
                break;

            case 3:
                printf("\nENTER FIRST DATE : ");
                scanf("%ld",&date1);
                printf("\nENTER SECOND DATE : ");
                scanf("%ld",&date2);
                if(validation(date1) && validation(date2))
                {
                    days=countNumberOfDays(date1,date2);
                    printf("\nNUMBER OF DAYS = %ld",days);
                }
                else
                    printf("\nYOU HAVE ENTERED AN INVALID DATE");
                break;

            case 4:
                printf("\nENTER THE YEAR");
                scanf("%d",&year);
                printf("\nENTER THE MONTH");
                scanf("%d",&month);
                printf("\nDAYS IN %d OF %d ARE %d",month,year,daysIn(year,month));
                break;

            case 5:
                printf("\nENTER THE DATE : ");
                scanf("%ld",&date);
                if(validation(date))
                {
                    printf("\nENTER THE NUMBER OF DAYS : ");
                    scanf("%ld",&days);
                    newdate=incrementByDays(days,date);
                    printDate(newdate);
                }
                else
                    printf("\nYOU HAVE NOT ENTERED A VALID DATE");

                break;

            case 6:
                printf("\nENTER THE DATE : ");
                scanf("%ld",&date);
                if(validation(date))
                {
                    printf("\nENTER THE NUMBER OF DAYS : ");
                    scanf("%ld",&days);
                    newdate=decrementByDays(days,date);
                    printDate(newdate);
                }
                else
                    printf("\nYOU HAVE NOT ENTERED A VALID DATE");

                break;

            case 7:
                printf("ENTER A DATE : ");
                scanf("%ld",&date);
                if(validation(date))
                {
                    checkForWeekDay(date);
                }
                else
                    printf("\nYOU HAVE NOT ENTERED A VALID DATE");

                break;

            case 8:
                exit(0);

            default:
                printf("\nYOU HAVE ENTERED A WRONG CHOICE");
        }
        getch();
        clrscr();
    }
    getch();
    return 0;
}


long formDate(int y,int j,int k)
{
    long date;
    date=(long)y*10000+j*100+k;
    return date;
}

void checkForWeekDay(long date)
{
    long days;
    days=countNumberOfDays(101,date);
    switch(days%7)
    {
        case 0:
            printf("\nFRIDAY");
            break;
        case 1:
            printf("\nSATURDAY");
            break;
        case 2:
            printf("\nSUNDAY");
            break;
        case 3:
            printf("\nMONDAY");
            break;
        case 4:
            printf("\nTUESDAY");
            break;
        case 5:
            printf("\nWEDNESDAY");
            break;
        case 6:
            printf("\nTHRUSDAY");
            break;
    }
}
void printDate(long date)
{
    int d,m,y;
    if(validation(date))
    {
        d=date%100;
        m=(date%10000)/100;
        y=date/10000;
        printf("\n%d / %d / %d",d,m,y);
    }
    else
        printf("\nTHE DATE THAT YOU WISH TO PRINT IS AN INVALID DATE");
}


long incrementByDays(long days,long date)
{
    int i,j,k,d,m,y;
    long newdate;

    d=date%100;
    m=(date%10000)/100;
    y=date/10000;

    j=m;
    k=d;
    for(i=y;;i++)
    {
        for(;j<=12;j++)
        {
            for(;k<=31;k++)
            {
                newdate=formDate(i,j,k);

                if(validation(newdate))
                {
                    if(countNumberOfDays(date,newdate)==days)
                        return newdate;
                }
            }
            k=1;
        }
        j=1;
    }
    return 0;
}


long decrementByDays(long days,long date)
{
    int i,j,k,d,m,y;
    long newdate;

    d=date%100;
    m=(date%10000)/100;
    y=date/10000;

    j=m;
    k=d;
    for(i=y;;i--)
    {
        for(;j>=1;j--)
        {
            for(;k>=1;k--)
            {
                newdate=formDate(i,j,k);
                if(validation(newdate))
                {
                    if(countNumberOfDays(date,newdate)==days)
                        return newdate;
                }
            }
            k=31;
        }
        j=12;
    }
    return 0;
}


int isLeapYear(int year)
{
    if(countDaysInYear(year)==366)
        return 1;
    return 0;
}


long countNumberOfDays(long date1,long date2)
{
    long days=0,tmp;
    int y1,y2,m1,m2,d1,d2,i;

    if(date2<date1)
    {
        tmp=date2;
        date2=date1;
        date1=tmp;
    }

    /* now date2 contains a bigger number than date1 */

    d1=date1%100;
    m1=(date1%10000)/100;
    y1=date1/10000;

    d2=date2%100;
    m2=(date2%10000)/100;
    y2=date2/10000;

    for(i=y1+1;i<y2;i++)
    {
        days+=countDaysInYear(i);
    }

    if(y1!=y2)
    {
        for(i=m1+1;i<=12;i++)
        {
            if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
                days+=31;
            else
            {
                if(i==2 && countDaysInYear(y1)==366)
                    days+=29;
                else if(i==2 && countDaysInYear(y1)==355)
                    days+=28;
                else
                    days+=30;
            }
        }

        for(i=d1+1;i<=daysIn(y1,m1);i++)
            days+=1;

        for(i=1;i<m2;i++)
        {
            if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
                days+=31;
            else
            {
                if(i==2 && countDaysInYear(y1)==366)
                    days+=29;
                else if(i==2 && countDaysInYear(y1)==355)
                    days+=28;
                else
                    days+=30;
            }
        }

        for(i=1;i<=d2;i++)
            days+=1;
    }
    else
    {
        if(m1!=m2)
        {
            for(i=m1+1;i<m2;i++)
            {
                days+=daysIn(y1,i);
            }

            for(i=d1+1;i<=daysIn(y1,m1);i++)
                days++;
            for(i=1;i<=d2;i++)
                days++;

        }
        else
        {
            for(i=d1;i<d2;i++)
                days++;
        }
    }

    return days;
}


int daysIn(int year,int month)
{
    if(month>12||month<0||year<0)
    {
        printf("\nYOU HAVE ENTERED A WRONG YEAR");
        return 0;
    }
    if(isLeapYear(year))
    {
        if(month==2)
            return 29;
    }
    if(month==2)
        return 28;
    else if(month==2||month==4||month==6||month==9||month==11)
        return 30;
    else
        return 31;
}


int countDigits(long date)
{
    int counter=0;
    while(date)
    {
        counter++;
        date/=10;
    }
    return counter;
}


int validation(long date)
{
    int y,m,d;
    if(date<=0||date>99999999)
        return 0;

    if(countDigits(date)<3&&countDigits(date)>8)
        return 0;

    d=date%100;
    m=(date%10000)/100;
    y=date/10000;

    if(m>12||y>9999||m==0)
        return 0;
    if(d>31)
        return 0;

    if(m==2||m==4||m==6||m==9||m==11)
        if(d>30)
            return 0;


    if(countDaysInYear(y)==366 && m==2 && d>29)
        return 0;


    if(countDaysInYear(y)==365 && m==2 && d>28)
        return 0;

    return 1;
}


long countDaysInYear(int year)
{
    if(year%4!=0)
    {
        return 365;
    }
    else
    {
        if(year%100==0 && year%400==0)
            return 366;
        if(year%100==0 && year%400!=0)
            return 365;
    }
    return 366;
}
/******************************************************************************/

OPERATORS ALLOWED ON POINTERS

QUES:-
Check which operations are not allowed on pointer data types, as compared to integers.
ANS:-
/******************************************************************************/
#include<stdio.h>
#include<conio.h>

int main()
{
int*p,*q;

p--;

p++;

p=p-q;

//p=p+q; NOT ALLOWED
//p=p*q; NOT ALLOWED
//p=p/q; NOT ALLOWED
//p=p%q; NOT ALLOWED
//p=p<<q; NOT ALLOWED
//p=p>>p; NOT ALLOWED

getch();
return 0;
}

/******************************************************************************/

INTERNAL REPRESENTATION OF BOOLEAN DATATYPES

QUES:-
Find/verify how boolean data types are internally represented.
ANS:-
/********************************************************************************/
#include<stdio.h>
#include<conio.h>

int main()
{
int i=8,j=9;
clrscr();
printf("%d\n%d\n",(i>j),(j>i));
printf("%d",sizeof(i+j));
getch();
return 1;
}
/*******************************************************************************/

INTERNAL REPRESENTATION OF BIT PATTERN OF INTEGERS

QUES:-
Display the internal representation of bit pattern of integerrs and observe and find the representation of negative numbers;
ANS:-
/*******************************************************************************/
#include<stdio.h>
#include<conio.h>

void decimal_to_binary(unsigned int);

int main()
{
int n;

clrscr();

printf("ENTER THE NUMBER FOR WHICH YOU WANT THE INTERNAL REPRESENTATION OF BIT PATTERN\n");
scanf("%d",&n);

decimal_to_binary((unsigned int)n);
getch();
return 0;
}

void decimal_to_binary(unsigned int n)
{
int *arr;
int counter;
int i;
counter=sizeof(int)*8;
/*
now counter represents the number of
bits that are used to store an integer on
your compiler
*/
arr=(int *)malloc(sizeof(int)*counter);

for(i=counter-1;i>=0;i--)
{
if(n%2==0)
arr[i]=0;
else
arr[i]=1;
n/=2;
}

for(i=0;i<counter;i++)
printf("%d",arr[i]);

}
/*******************************************************************************/

ALTERNATE SOLUTION:-
/*******************************************************************************/
#include<conio.h>
#include<stdio.h>

void decimal_to_binary(unsigned char);

union u
{
int i;
char c;
};

int main()
{
char*ptr;
union u a;

printf("ENTER AN INTEGER : \n");
scanf("%d",&a.i);

ptr=&a.i;

ptr++;
decimal_to_binary(*ptr);

ptr--;
decimal_to_binary(*ptr);

getch();
return 0;
}

void decimal_to_binary(unsigned char n)
{
int arr[8];
int i;

for(i=7;i>=0;i--)
{
if(n%2==0)
arr[i]=0;
else
arr[i]=1;
n/=2;
}

for(i=0;i<8;i++)
printf("%d",arr[i]);
printf(" ");
}
/*******************************************************************************/

RANGE AND SIZE OF INTEGERS

QUES :-
Find the range and size of integer data type in your C compiler, through programming and verify using sizeof opereator.
ANS:-
/******************************************************************************/
#include<stdio.h>
#include<conio.h>

int main()
{
int *ptr,i=0;
long j;
clrscr();
ptr=0;
ptr++;
printf("Size of integer on your compiler is %d\n",ptr);
printf("Size of integer using sizeof operator = %d\n",sizeof(int));
while(i>=0)
j=i++;

printf("Range = %d to %ld",i,j);
getch();
return 1;
}

/******************************************************************************/