Try noting down the address of a local variable. Call another function with a local variable declared in it and check the address of that local variable and compare!.
#include < stdio.h >
#include < stdlib.h >
void stack(int *local1);
int main()
{
int local1;
stack(&local1);
exit(0);
}
void stack(int *local1)
{
int local2;
printf("\nAddress of first local : [%u]", local1);
printf("\nAddress of second local : [%u]", &local2);
if(local1 < &local2)
{
printf("\nStack is growing downwards.\n");
}
else
{
printf("\nStack is growing upwards.\n");
}
printf("\n\n");
}
Monday, July 9, 2007
Given two strings A and B, how would you find out if the characters in B were a subset of the characters in A?
Here is a simple, yet efficient C program to accomplish the same...
#include < stdio.h >
#include < conio.h >
int isSubset(char *a, char *b);
int main()
{
char str1[]="defabc";
char str2[]="abcfed";
if(isSubset(str1, str2)==0)
{
printf("\nYes, characters in B=[%s] are a subset of characters in A=[%s]\n",str2,str1);
}
else
{
printf("\nNo, characters in B=[%s] are not a subset of characters in A=[%s]\n",str2,str1);
}
getch();
return(0);
}
// Function to check if characters in "b" are a subset
// of the characters in "a"
int isSubset(char *a, char *b)
{
int letterPresent[256];
int i;
for(i=0; i < 256; i++)
letterPresent[i]=0;
for(i=0; a[i]!='\0'; i++)
letterPresent[a[i]]++;
for(i=0; b[i]!='\0'; i++)
if(!letterPresent[b[i]])
return(1);
return(0);
}
#include < stdio.h >
#include < conio.h >
int isSubset(char *a, char *b);
int main()
{
char str1[]="defabc";
char str2[]="abcfed";
if(isSubset(str1, str2)==0)
{
printf("\nYes, characters in B=[%s] are a subset of characters in A=[%s]\n",str2,str1);
}
else
{
printf("\nNo, characters in B=[%s] are not a subset of characters in A=[%s]\n",str2,str1);
}
getch();
return(0);
}
// Function to check if characters in "b" are a subset
// of the characters in "a"
int isSubset(char *a, char *b)
{
int letterPresent[256];
int i;
for(i=0; i < 256; i++)
letterPresent[i]=0;
for(i=0; a[i]!='\0'; i++)
letterPresent[a[i]]++;
for(i=0; b[i]!='\0'; i++)
if(!letterPresent[b[i]])
return(1);
return(0);
}
How can we sum the digits of a given number in single statement?
Try something like this
# include < stdio.h >
void main()
{
int num=123456;
int sum=0;
for(;num > 0;sum+=num%10,num/=10); // This is the "single line".
printf("\nsum = [%d]\n", sum);
}
If there is a simpler way to do this, let me know!
# include < stdio.h >
void main()
{
int num=123456;
int sum=0;
for(;num > 0;sum+=num%10,num/=10); // This is the "single line".
printf("\nsum = [%d]\n", sum);
}
If there is a simpler way to do this, let me know!
Is there a way to multiply matrices in lesser than o(n^3) time complexity?
Yes. Divide and conquer method suggests Strassen's matrix multiplication method to be used. If we follow this method, the time complexity is O(n^2.81) times rather O(n^3) times.
Here are some more details about this method.
Suppose we want to multiply two matrices of size N x N: for example A x B = C
[C11 C12] [A11 A12] [B11 B12]
[C21 C22] = [A21 A22] [B21 B22]
Now, this guy called Strassen's somehow :) came up with a bunch of equations to calculate the 4 elements of the resultant matrix
C11 = a11*b11 + a12*b21
C12 = a11*b12 + a12*b22
C21 = a21*b11 + a22*b21
C22 = a21*b12 + a22*b22
If you are aware, the rudimentary matrix multiplication goes something like this
void matrix_mult()
{
for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
{
compute Ci,j;
}
}
}
So, essentially, a 2x2 matrix multiplication can be accomplished using 8 multiplications. And the complexity becomes
2^log 8 =2^3
Strassen showed that 2x2 matrix multiplication can be accomplished in 7 multiplications and 18 additions or subtractions. So now the complexity becomes
2^log7 =2^2.807
This is how he did it
P1 = (A11+ A22)(B11+B22)
P2 = (A21 + A22) * B11
P3 = A11 * (B12 - B22)
P4 = A22 * (B21 - B11)
P5 = (A11 + A12) * B22
P6 = (A21 - A11) * (B11 + B12)
P7 = (A12 - A22) * (B21 + B22)
C11 = P1 + P4 - P5 + P7
C12 = P3 + P5
C21 = P2 + P4
C22 = P1 + P3 - P2 + P6
Now, there is no need to memorize this stuff!
Here are some more details about this method.
Suppose we want to multiply two matrices of size N x N: for example A x B = C
[C11 C12] [A11 A12] [B11 B12]
[C21 C22] = [A21 A22] [B21 B22]
Now, this guy called Strassen's somehow :) came up with a bunch of equations to calculate the 4 elements of the resultant matrix
C11 = a11*b11 + a12*b21
C12 = a11*b12 + a12*b22
C21 = a21*b11 + a22*b21
C22 = a21*b12 + a22*b22
If you are aware, the rudimentary matrix multiplication goes something like this
void matrix_mult()
{
for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
{
compute Ci,j;
}
}
}
So, essentially, a 2x2 matrix multiplication can be accomplished using 8 multiplications. And the complexity becomes
2^log 8 =2^3
Strassen showed that 2x2 matrix multiplication can be accomplished in 7 multiplications and 18 additions or subtractions. So now the complexity becomes
2^log7 =2^2.807
This is how he did it
P1 = (A11+ A22)(B11+B22)
P2 = (A21 + A22) * B11
P3 = A11 * (B12 - B22)
P4 = A22 * (B21 - B11)
P5 = (A11 + A12) * B22
P6 = (A21 - A11) * (B11 + B12)
P7 = (A12 - A22) * (B21 + B22)
C11 = P1 + P4 - P5 + P7
C12 = P3 + P5
C21 = P2 + P4
C22 = P1 + P3 - P2 + P6
Now, there is no need to memorize this stuff!
Write a simple piece of code to split a string at equal intervals.
Suppose you have a big string
This is a big string which I want to split at equal intervals, without caring about the words.
Now, to split this string say into smaller strings of 20 characters each, try this
#define maxLineSize 20
split(char *string)
{
int i, length;
char dest[maxLineSize + 1];
i = 0;
length = strlen(string);
while((i+maxLineSize) <= length)
{
strncpy(dest, (string+i), maxLineSize);
dest[maxLineSize - 1] = '\0';
i = i + strlen(dest) - 1;
printf("\nChunk : [%s]\n", dest);
}
strcpy(dest, (string + i));
printf("\nChunk : [%s]\n", dest);
}
This is a big string which I want to split at equal intervals, without caring about the words.
Now, to split this string say into smaller strings of 20 characters each, try this
#define maxLineSize 20
split(char *string)
{
int i, length;
char dest[maxLineSize + 1];
i = 0;
length = strlen(string);
while((i+maxLineSize) <= length)
{
strncpy(dest, (string+i), maxLineSize);
dest[maxLineSize - 1] = '\0';
i = i + strlen(dest) - 1;
printf("\nChunk : [%s]\n", dest);
}
strcpy(dest, (string + i));
printf("\nChunk : [%s]\n", dest);
}
How to fast multiply a number by 7?
Try
(num << 3 - num)
This is same as
num*8 - num = num * (8-1) = num * 7
(num << 3 - num)
This is same as
num*8 - num = num * (8-1) = num * 7
How do you get the line numbers in C?
Use the following Macros
__FILE__ Source file name (string constant) format "patx.c"
__LINE__ Current source line number (integer)
__DATE__ Date compiled (string constant)format "Dec 14 1985"
__TIME__ Time compiled (string constant) format "15:24:26"
__TIMESTAMP__ Compile date/time (string constant)format "Tue Nov 19 11:39:12 1997"
Usage example
static char stamp[] = "***\nmodule " __FILE__ "\ncompiled " __TIMESTAMP__ "\n***";
...
int main()
{
...
if ( (fp = fopen(fl,"r")) == NULL )
{
printf( "open failed, line %d\n%s\n",__LINE__, stamp );
exit( 4 );
}
...
}
And the output is something like
*** open failed, line 67
******
module myfile.c
compiled Mon Jan 15 11:15:56 1999
***
__FILE__ Source file name (string constant) format "patx.c"
__LINE__ Current source line number (integer)
__DATE__ Date compiled (string constant)format "Dec 14 1985"
__TIME__ Time compiled (string constant) format "15:24:26"
__TIMESTAMP__ Compile date/time (string constant)format "Tue Nov 19 11:39:12 1997"
Usage example
static char stamp[] = "***\nmodule " __FILE__ "\ncompiled " __TIMESTAMP__ "\n***";
...
int main()
{
...
if ( (fp = fopen(fl,"r")) == NULL )
{
printf( "open failed, line %d\n%s\n",__LINE__, stamp );
exit( 4 );
}
...
}
And the output is something like
*** open failed, line 67
******
module myfile.c
compiled Mon Jan 15 11:15:56 1999
***
Subscribe to:
Posts (Atom)