Friday, October 26, 2012

Solution to E Balaguruswamy Chapter - Pointers

                                                               Solution to E Balaguruswamy
                                                                     Chapter  - Pointers

Ans 11.4 Pointer is initialized by keyword NULL or  0  in the following fashion
int  * p = NULL  , * m = 0 ;
 Ans 11.5  Following is the effect of the following statements
i.                     int a, *b = &a;  //Error Pointer being initialized with the address of an uninitialized variable
ii.                   int p ,   *p ;      //Error Multiple Declaration of p
iii.                  char *s;          
iv.                 a = (float * ) &x);
v.                   double  ( *f )( ) ;       //It declares the pointer to a function

Ans 11. 6   int M ,  n ;
    int *P1  , *P2 ;
The following are the errors in the expression given below:
i)                    P1 = &m ; //Error Pointer cannot be initialized with an uninitialized variable
ii)                   P2 = n ; // Error Pointer can only be initialized with the address of the variable
iii)                 P2 = &*&m; // Correct as the first dereferencing operator Is over powered by the second & operator
iv)                  M = P2 – P1 //Correct Pointer Subtraction Is allowed
v)                  P1 = &P2 //Error A simple pointer cannot be initialized with memory address of an another pointer, only a pointer to a pointer can be initialized in this way
  Ans11.7 Difference between the notations  *m[5] and (*m)[3]. Since * has lower precedence than  [] , *m[5] declares m  as an array of 5 pointers while (*m)[3] declares m as a pointer to an array of five elements.
Ans 11.9   int x = 10 ,  y  = 10 ;
                    int  *p1 = &x , *p2 = &y;
                The following Expression generate the following output:
i)                    (*p1)++   Output – 10
ii)                   - - (*p2)   Output  -  9
iii)                 *p1 + (*p2)--   Output  - 20
iv)                   ++(*p2) - *p1  Output  -  1
Ans 11.12  Following Output is  generated by the following code :
                int m = 100 ;
                int *p1 = &m;
                int **p2 = &p1;
                printf(“%d” , **p2);
                Output :
                100
Ans 11. 13   Following is the error in the given code
                                int **p1 , * p2 ;
                                p2 = &p1  // It should be  p1 =&p2
Explanation: p1 is a pointer to a pointer so it should always point a pointer
Ans11.14  Difference between the two expression name + 10; and *(name + 10 ) is that the first expression refers to all the characters after the 10th position in the array name or rather simply we  can say that it will the array name from the 10th character whereas the second expression refers specifically to the 10th character of the array of name .
Ans 11.15  Following is the output of the code given below
                                int m[2] ;
                                *(m+1)
                                *m = *(m + 1);
                                printf(“ %d “ , m[0] );
Output:
                                100
Ans 11.16    int m[2];
                       int *p = m ;
                       m[0] = 100;
                       m[1] = 200;
                       printf(“%d %d “ , ++*p , *p );
                      Output:
                      101  100
Ans 11.17    Output of the following code :
                 int f(char *p );
                main()
                {              char str[] = “ANSI”;
                                printf(“%d” , f(str) );
                }
                int f(char *p )
                {              char *q = p ;
                                while (*++p ) ;
                                return (p – q );                  }
Output :
4
Ans 11.18  Following declarations are not the same:
                1 . void search ( int *m[] , int x) { }
                In this the parameter in the definition is a integer pointer array.
                2. void search ( int **m  ,int x ) {}
                In this the parameter in the definition is a integer pointer to a pointer.
Ans 11.19   Following declaration is are same
                char *s;
                char s[5] ;
                As name of the array is a pointer to the first element of the array it’s as same as declaring a pointers which when incremented can be used to store a list of values same as an array.
Ans 11.20  The following is the correct way to declare a pointer to a function.
                int (*p)(void);
                

Thursday, October 25, 2012

FINDING THE MINOR OF 3 X3 MATRIX IN C


//QUERIES ARE WELCOME AS COMMENTS
# include<stdio.h>
# include<conio.h>
#define C 3
int x = 0, y = 0;
void check(int a[][C], int b[][C-1], int c[][C], int m, int n);
void minor(int b[][C-1],int c[][C]);
void main()
{
int a[C][C], b[C-1][C-1], c[C][C], i ,j;
for(i = 0; i < C; i++)
{
for(j = 0; j < C; j++)
{
printf("ENTER YOUR ELEMENT AT %d X %d\n", i , j);
scanf("%d", &a[i][j]);
}
}
for(i = 0; i< C; i++)
{
for(j = 0; j < C; j++)
{
check(a, b, c, i,j);
}
}
for(i = 0; i < C; i ++)
{
for(j = 0; j < C; j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
}
void check(int a[][C], int b[][C-1], int c[][C], int m, int n)
{
int i, j, l = 0, k = 0;
for(i = 0; i< C; i++)
{
for(j = 0; j< C; j++)
{
if (i != m && j != n)
{
b[l][k++]  = a[i][j];
if(k == C-1)
{
l++;
k = 0;
}
}
}
}
minor(b, c);
}
void minor(int b[][C-1],int c[][C])
{
int z;
z = b[0][0] * b[1][1] - b[0][1] * b[1][0];
c[x][y++] = z;
if (y == C)
{
x++;
y = 0;
}
}

The Fibonacci Series In C Using Recurssion


//IN CASE OF DOUBT, COMMENT IT
# include<stdio.h>
int fibo(int n);
int r;
void main()
{
int i, j, k, l = 0;
scanf("%d",&j);
for(i = 0; i < j; i++)
{
for( k = 0; k <= i; k++)
{
l = fibo(k);
printf("%d ", l);
}
printf("\n");
}
}
int fibo(int n)
{
if(n == 0)
{
return (0);
}
else if(n == 1)
{
return (1);
}
else
{
r =  fibo(n- 1) + fibo(n-2);
return r;
}
}

Wednesday, October 24, 2012

MATRIX SOLVER-GAUSS JORDAN METHOD IN C


//KINDLY ASK THE DOUBTS IN COMMENTS
# include<stdio.h>

void subtrac(float b[][6], int k);

float constant(float c[][6], int a, int b);

void divide(float b[][6], int a, float c);

void main()

{

float a[3][6], k = 0.0;

int i, j;

for( i = 0 ;i < 3; i ++)

{

for( j = 0; j <  6; j++)

{

a[i][j] = 0;

}

}

for(j = 0; j < 3; j++)

{

for(i = 0; i < 3;i++)

{

printf("ENTER ELEMENT AT %d x %d\n",j, i);

scanf("%f", &a[j][i]);

}

}

a[0][3] = 1.0;

a[1][4] = 1.0;

a[2][5] = 1.0;

for(i = 0 ; i < 3; i++)

{

subtrac(a,i);

}

for(i = 0; i < 3; i++)

{

k = a[i][i];

divide(a, i, k);

}

for(i = 0 ; i < 3; i++)

{

for(j= 3; j < 6; j++)

{

printf("%f ", a[i][j]);

}

printf("\n");

}

}

void subtrac(float b[][6], int k)

{

int i, z;

float j;

for( i = 0; i < 3; i++)

{

if(i != k)

{

j = constant(b, i , k);

for(z = 0; z < 6; z++)

{

                         b[i][z] = b[i][z] - j * b[k][z];
 
}

printf("R%d -> R%d - (%.2f * R%d)\n",i+1,i+1,j,k+1);

}

}

}

float constant(float c[][6], int a, int b)

{

float con;

con = c[a][b]/ c[b][b];

return (con);

}

void divide(float b[][6], int a, float c)

{

int j;

for(j = 0; j < 6; j++)

{

b[a][j] = b[a][j] / c;

}

printf("R%d -> R%d / %.2f\n", a+1, a+1, c);

}

Tuesday, October 23, 2012

The N- Queens Problem in C


//FEEL FREE TO DROP ANY QUESTION IN THE COMMENTS
# include<stdio.h>
# include<conio.h>
# define C 8
int check(int k[][C], int m, int n);
void backtrack(int k[][C], int f2[], int m);
void increment(int k[][C], int m, int n);
void main()
{
int a[C][C] = {0},i, j = 0, c, fj[C] = {0}, z = 0, k, count = 0;
clrscr();
for(i = 0; i < C; i++)
{
reloop:
while(j < C)
{
c = check(a, i, j);
if(c == 0)
{
increment(a, i, j);
fj[i] = j;
z = 1;
break;
}
j++;
}
if(z != 1)
{
    backtrack(a, fj, i);
    j = fj[i - 1] + 1;
    i = i - 1;
    if(i < 0)
    {
goto end;
    }
    goto reloop;
}
j = 0;
z = 0;
if(i == (C-1))
{
for(k = 0; k < C; k++)
{
printf("(%d,%d);",k,fj[k]);
}
printf("\n");
getch();
count++;
backtrack(a, fj, i + 1);
j = fj[i] + 1;
    goto reloop;
}

}
end:
printf("%d",count);
getch();
}
int check(int k[][C], int m, int n)
{
int i, j, sum = 0;
for(i = 0; i < C; i++)
{
sum = sum + k[i][n] + k[m][i];
}
if(m == n)
{
for(i = 0; i < C; i++)
{
sum = sum + k[i][i];
}
}
else if(m < n)
{
for(i = 0, j = n - m; j < C; j++, i++)
{
sum = sum + k[i][j];
}
}
else if(m > n)
{
for( i = m - n, j = 0; i < C; i++,j++)
{
sum = sum + k[i][j];
}
}
if((m + n) == C-1)
{
for(i = C - 1, j = 0; j < C; j++, i--)
{
sum = sum + k[i][j];
}
}
else if((m + n) < C - 1)
{
for(i = m + n, j = 0; j <= m + n;  i--, j++)
{
sum = sum + k[i][j];
}
}
else if((m + n) > C-1)
{
for(i = C - 1, j = m + n - C + 1; j < C; i--,j++)
{
sum = sum + k[i][j];
}
}
return sum;
}
void increment(int k[][C], int m, int n)
{
k[m][n] = 1;
}
void backtrack(int k[][C], int f2[], int m)
{
int i, j;
i = m - 1;
j = f2[m-1];
k[i][j] = 0;
}

Sunday, October 7, 2012

The Time Paradox

since the invention of the clock, the world has wanted us to rotate our universe around it. rise from the clock and set down with it. the concept of the time table is nothing more than a myth , to just bind us to the never stopping flow of the life we have, and which is segregated into the concept time. most of u would have a notion that time is am measurable entity. the rotation of the earth constitutes of the hours, minutes and seconds and the revolution constitutes of the years, months and days. and when the evidences showed that the rotation of the earth fluctuated randomly, a new definition of time was formulated. it was the duration of  9 192 631 770 periods of the radiation corresponding to the transition between the two hyper fine levels of the ground state of the cesium 133 atom. none of us ever sat down to question why only 24 hours, why 60 min, why 60 sec. when the whole worlds obsessed with 100 and its multiples, why bother with a 60. am not going to give you the ans, a magician never reveals his secrets. what confuses me the most that why do human waste so much time perfecting 1 sec when the same concept of time can be used to reveal the future on retell the past. despite a mixed bag of opinions, and some quite sentimental ones, i believe in the theory of time travel, or most precisely at least zonal transfer. i also but the theory that parallel universes exist, despite whatever the heart says. because it makes me happy. now i know that some one some where would have made an opposite choice of what i have taken and would be living that life. if possible i would even like to visit him or her(i really don't know) in their universe. as the mythologist believe that for each yin there is a yang, for each head there is a tail, for each evil there is good, so why not for each choice i make there is an anti choice or the opposite choice making. so i get satisfied, knowing that at least, if i screw up here, i would be doing well in the alternative universe, or some other zone.
well leaving aside the inter dimensional time, what i wanted was, that i have never(read mostly) run out of time. for me a day contains 48 hours and each hour of 30 min. the end output is the same, but i get to sleep more, for like 16 hours. the concepts called, "FOOLING THE MIND". the mind is a divine entity, and controlling it would be predicting whether you are going to get a girl or not. so when you cannot control your monster, you need to train it or either fool it. i decided to fool my mind, into believing that i had twice the time anyone had. so no assignment or project deadline ever bothered me. same way, i conditioned my mind to believe that my phone company was spying on me(or was it not), to just cut down on my phone bills. then there was a dog in our street who was not particularly fond of anyone stepping in his area(who is??), but me being the lost in thought kind of guy would often just go into his den and would end up with an orgy of barks and an occasional chase down the road. i decided that the dog was actually a human transformed by a witch to stay a mad dog. so as common courtesy demands, you don't visit anyone's house(except your own), without an invitation. BOOM. now the neighbors sleep peacefully and the dog also treats me with respect and the occasional cheerful bark, when he is on his way to work. this idea may be childish, or the story supporting it would definitively be, but mind is attracted to oddity. you may see a 1000 beautiful girls in a party and after some time you would be molded to it. addition of some new girls wouldn't create a difference(hopefully). but now if a dog decided to rise to the occasion and accept the invitation to the party, he would be the first thing in your eye. the same thing happens with the mind. this is the essence of life, or the true way to lead it. you believe what you want to, modify the facts according to it, and lead a happy life. of course, some people will call you, back crap crazy, but that is because of the dry life which they lead. they cannot digest the idea of us controlling the output of the life and leaving a bit of it to the random variables of the universe. the food for though may be tough to comprehend, basically because of the non- traditional way of thinking. but try it. this might not be a 100% fail proof idea, but might certainly lead to a happy life.