Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

 

Set-1
*1)​ Write the output of this program

main(){
int *a, *s, i;
s = a = (int *) malloc( 4 * sizeof(int));
for (i=0; i<4; i++) *(a+i) = i * 10;
printf("%d\n", *s++);
printf("%d\n", (*s)++);
printf("%d\n", *s);
printf("%d\n", *++s);
printf("%d\n", ++*s);
}

Solution

The output will be : 0 10 11 20 21


*s++ => *(s++)
*++s => *(++s)
++*s => ++(*s)
 
2) ​What is the o/p of the following program?
#include <stdio.h>
void main(){
int y[ ] = {1,2,3,4,5,6,7}
int *x = y ;
int *z = &y[6] ;

printf(“%d”,z-x) ;
 }

3) ​What is the output of this program?


#include<stdio.h>
#define SQUARE(x) (x*x)
void main(){
int i=10,j=5,k=0;
k=SQUARE(i-j);
printf("%d",k);
}
Answer:​ -45
 
*4)​ Print the output of the program
main(){
struct Data {
int a;
int b;
} y[4] = { 1, 10, 3, 30, 2, 20, 4, 40};
struct Data *x = y;
int i;

for(i=0; i<4; i++) {


x->a = x->b, ++x++->b;
printf("%d %d\t", y[i].a, y[i].b);
}
}

Solution

  10 11 30 31 20 21 40 41

5)​ How many times the following program would print 'Jamboree'?
main(){
printf("\n Jamboree");
main();
}
(a) infinite number of times (b) 32767 times
c) 65535 times ​(d) till the stack does not overflow

6)​ What is the output of the following program?


main(){
int i,j;
i=10;
j=sizeof(++i);
printf("%d",i);
}
(a) 11​ ​ (b) 10 (c) 4 (d) compilation error

 
7)​ Write a program to calculate number of 1's (bit) in a given integer number i.e) Number of 1's
 in the given integer's equivalent binary representation.

8)​ What is the output of the following program?


main(){
char string[]="Hello World";
display(string);
}
void display(char *string){
printf("%s",string);
}

9)​ What is the output of the following program?


enum colors {BLACK,BLUE,GREEN};
main(){
printf("%d..%d..%d",BLACK,BLUE,GREEN);
return(1);
}
Answer: ​0..1..2
 
10)​ Write a program to swap two integers without using 3​rd​ integer (ie.
 Without using any temporary variable using bitwise operators)

Solution
main(){
int a, b;

printf("Enter two numbers A, B : ");


scanf("%d %d", &a, &b);
a^=b^=a^=b; /* swap A and B */
printf("\nA = %d, B= %d\n", a, b);
 }

11)​ Write a general swap macro in C - A macro which can swap any type of data
 (ie. int, char,float, struct, etc..)

Solution

/* Generic Swap macro*/


 #define swap(a, b, type) { type t = a; a = b; b = t; }

12)​ What is the output of the following program?


#define f(x,y) x##y
main(){
int var12=100;
printf("%d",f(var,12));
}

13) ​What will be the output of this program?


main(){
int *a, *savea, i;
savea = a = (int *) malloc(4 * sizeof(int));
for (i=0; i<4; i++) *a++ = 10 * i;
for (i=0; i<4; i++) {
printf("%d\n", *savea);
savea += sizeof(int);
}

Solution
The first value will be 0, the rest of the three values will not be predictable. Actually it prints the
values of the following location in each step
* savea
* (savea + sizeof(int) * sizeof(int))
etc...
ie. savea += sizeof(int) => savea = savea +
sizeof(savea_type) * sizeof(int)
( by pointer arithmetic)
=> save = savea + sizeof(int) * sizeof(int)
Note: You can verify the above by varing the type of 'savea'variable to
char, double, struct, etc.
Instead of statement 'savea += sizeof(int)' use savea++ then the values 0, 10, 20 and 30 will be
printed. This behaviour is because of pointer arithmetic.

14)​ What is the output of the following program?


main(){
int i=32765;
for(;i++;printf("%d",i)) ;
}

15)​ What would be the output of the following program.


main(){
int a[5]={2,3};
printf("\n %d %d %d",a[2],a[3],a[4]);
}
​Answer :​ 0 0 0

​16)​ Write the output of this program


#define calc(a, b) (a * b) / (a - b)
void main(){
int a = 20, b = 10;
printf("%d\n", calc(a + 4, b -2));
}
Answer :
Actual substitution is like this :
calc(20+4, 10 -2) is calculated as follows
(20+4 * 10-2) / (20+4 - 10-2)
(20+40-2) / 12
58 / 12 = 4.8
since it is printed in %d the ans is 4

17)​ What is the output of the following program ?

#define AB "hello, "


#define CD "world"
#define ABCD "fine day"

#define cat(x,y) x##y


#define xcat(x,y) cat(x,y)
 printf ( xcat(AB, CD) "\n");
printf ( cat(AB, CD) "\n");

18)​ What will be output of the following program ?

void main(){

int cnt = 5, a;

do {
a /= cnt;
} while (cnt --);
printf ("%d\n", a);
}
 
19)​ What is the output of the following program?
#include<stdio.h>
void main(){

printf("%d",3^2);

}
Answer: ​1

20) ​ Print the output of this program

void main(){
int a, b, c, abc = 0;
a = b = c = 40;
if (c) {
int abc;
abc = a*b+c;
}
printf ("c = %d, abc = %d\n", c, abc);
}
Answer :
the result will be c = 40 and abc = 0;because the scope of the variable 'abc' inside if(c) {..
}is not valid out side that if (.) { .. }.

21)​ What is the output of the following program?


#include<stdio.h>
void main(){
printf("%d",3|2);

22)​ What is the o/p of the following program?


#define one 0
#ifdef one
printf("one is defined ");
#ifndef one
printf("one is not defined ");

Ans: "one is defined"

23)​ How do you declare an array of N pointers to functions returning pointers to functions
returning pointers to characters?

Ans: The first part of this question can be answered in at least three ways:

1. char *(*(*a[N])())();

2. Build the declaration up incrementally, using typedefs:

typedef char *pc; /* pointer to char */


typedef pc fpc(); /* function returning pointer to char */
typedef fpc *pfpc; /* pointer to above */
typedef pfpc fpfpc(); /* function returning... */
typedef fpfpc *pfpfpc; /* pointer to... */
pfpfpc a[N]; /* array of... */

3. Use the cdecl program, which turns English into C and viceversa:

cdecl> declare a as array of pointer to function returning


pointer to function returning pointer to char
char *(*(*a[])())()

cdecl can also explain complicated declarations, help with casts, and indicate which set of
parentheses the arguments go in (for complicated function definitions, like the one above). Any
good book on C should explain how to read these complicated C declarations "inside out" to
understand them ("declaration mimics use").The pointer-to-function declarations in the examples
above have not included parameter type information. When the parameters have complicated
types, declarations can *really* get messy. (Modern versions of cdecl can help here, too.)

24)​ What is the output of the following program?


#include<stdio.h>
void main(){
printf("%d",3&4);

25)​ What is the o/p of the following program?

main(){
static int i=3;
printf("%d",i--);
return (i>0 ?) main():0;
}

Ans: 321

26)​ What is the output of the following program?


char *foo(){
char result[100];
strcpy(result,"anything is good");
return(result);
}
void main(){
char *j;
j=foo()
printf("%s",j);
}

Ans: anything is good.

27)​ How many times will the printf stmt be executed?


main( ){
unsigned int i=3;
while( i >=0)
printf( "%d", i--);
}

Ans: Infinite, b'cos 'i' is an unsigned integer and it will not decrement below '0' and hence end up
in an infinite loop.

28)​ What is the o/p of the following program?


main( ){
int x,y, z;
x=2;
y=5;
z= x+++y;
printf("%d %d %d", x, y z);
}
Ans: 3 5 7

29)​ # define swap(a,b) temp=a; a=b; b=temp;


main( ){
int i, j, temp;
i=5;
j=10;
temp=0;
if( i > j)
swap( i, j );
printf( "%d %d %d", i, j, temp);
}
​ nswer​ : 10, 0, 0.
A

30)​ What is the output of the following program?


#include<stdio.h>
main(){
extern int a;
printf("%d",a);;
}
int a=20;
​Answer​ : 20

31) ​What is the output of the following program.?


main(){
int i=-3,j=2,k=0,m;
m=++i&&++j||++k;
printf("\n %d %d %d %d",i,j,k,m);
}
​Answer​ : 4 3 0 1

32) ​What is the output of the following program?


main(){
int a,b;
a=sumdig(123);
b=sumdig(123);
printf("%d %d",a,b);
}
sumdig(int n){
static int s=0;
int d;
if(n!=0){
d=n%10;
n=(n-d)/10;
s=s+d;
sumdig(n);
}
else return(s);
}
a) 12 6 (b) 6 12 (c) 3 15 (d) error

33) ​What is the output of the following program?


#define CUBE(x) (x*x*x)
main(){
int a,b=3;
a=CUBE(b++);
printf("\n %d %d",a,b);
}
(a) 64 4 (b) 27 4 ​(c) 27 6​ (d) 64 6
34) ​What is the output of the following program?
#include<stdio.h>
void main(){
printf("%d",3>>2?100:200);

}
Answer:​ 200

35) ​A function has this prototype void f1(int **x), How will you call this
function?
​ (a) int **a;​ (b) int a; ​(c) int *a;​ (d) int a=5;
​ f1(a);​ f1(&a); ​f1(&a);​ f1(&&a);

36) ​What is the output of the following program?


#include<stdio.h>
void main(){
int i=0;
switch(printf("k"),printf("ku"))
{
case 1:printf("%d\n",i);break;
case 2:printf("%d",++i);break;
}
}

37)​ What is the output of the following program?


main(){
printf("%x",-1<<4);
}
Answer:
fff0
Explanation :
-1 is internally represented as all 1's. When left shifted four times the least
significant 4 bits are filled with 0's.The %x format specifier specifies that the
integer value be printed as a hexadecimal value.

38) ​What is the output of the following program?


main(){
int a[3]={2,3,4};
char *p;
p=(char *)a;
p=(char *)((int *)p+1);
printf("%d",*p);
}
(a) 2 (b) 0 (c) junk value ​(d) 3

39) ​ What will be the value of i & j after the loop is executed? for(i=0,j=0;i<5,j<25;i++,j++)
(a) i=4,j= 24 (b) i=24,j= 24 ​(c) i=25,j= 25 ​ (d) i=5,j=25

40) ​What is the output of the following program?


main(){
extern int i;
i=20;
printf("%d",i);
}
Answer:
Linker Error : Undefined symbol '_i'
Explanation:
extern storage class in the following declaration,
extern int i;
specifies to the compiler that the memory for ​i is allocated in somewhere else and that
address will be given to the current program at the time of linking. But linker finds that no other
variable of name ​i is available somewhere else with memory space allocated for it. Hence a
linker error has occurred .

41)​ What is the output of this program?


#include<stdio.h>
void main(){
if(0,1)
printf("hello");
}
42)​ What is the output of this program?
#include<stdio.h>
void main(){
int i=3;
while(i--){
int i=100;
i--;
printf("%d",i);
}
}

43)​ What is the output of this program?


main(){
const int i=100;
i++;
printf("%d",i);
}
Error : reason is const value cant be changed

44)​ What is the output of this program?


#include<stdio.h>
#define COMBINE(x,y) (x##y)
void main(){
int i;
i=COMBINE(10+5,5+10);
printf("%d",i);
}
Answer: 75
 
45)​ What is the output of the following program?
#define int char
main(){
int i=65;
printf("sizeof(i)=%d",sizeof(i));
}
Answer:
sizeof(i)=1

46) ​What is the output of the following program?


main()
{
char *p1="Name";
char *p2;
p2=(char *)malloc(20);
while(*p2++=*p1++);
printf("%s\n",p2);
}
47)​ ​What is the output of the following program?
#include <stdio.h>
main(){
int i;
i =0x10+010+10;
printf("%d",i);
}

48)​ ​What is the output of the following program?


#include<stdio.h>
void main(){
int x=4,z;
int *p=&x ;
z = 8/*p ;
printf(“%d”,z) ;
}

49) ​What is the output of the following program?


#include<stdio.h>
main(){
int x,y=1,z;
if (z=(y==0))
x=5;
else
x=10;
printf("x=%d\t y = %d\t z=%d ",x,y,z);
}

50) ​What is the output of the following program?


#include<stdio.h>
main(){
char s1[]="Ramco";
char s2[]="Systems";
s1=s2;
printf("%s",s1);
}

Set-2
1. What is the o/p of the program given below?
main()
{
int size = 10;
int arr[size];
for (i = 1; i <= size; i++)
{
scanf (“%d”, &arr[i]);
printf(“\n%d”, arr[i]);
}
}

2. What is the o/p of the program given below?


main()
{
int arr[ ] = {0, 1, 2, 3, 4};
int i, *p;
for (p = arr, i = 0; p + i <= arr + 4; p+=, i++)
printf(“%d”, *(p + i));
}

3. What is the o/p of the program given below?


main()
{
int arr[ ] = {0, 1, 2, 3, 4};
int i, *ptr;
for (ptr = arr + 4, i = 0; i <= 4; i++)
printf(“%d”, ptr[-i]);
}

4. What is the o/p of the program given below?


main()
{
int arr[ ] = {0, 1, 2, 3, 4};
int *ptr, i;
for (ptr = arr + 4; ptr >= arr; ptr--)
printf(“%d”, arr[ptr – arr]);
}

5. What is the o/p of the program given below?


main()
{
static int a[ ] = {0, 1, 2, 3, 4};
static int *p[ ] = {a, a + 1, a + 2, a + 3, a + 4};
int **ptr = p;
printf (“%d %d\n”, a, *a);
printf (“%d %d %d\n”, p, *p, **p);
printf (“%d %d %d\n”, ptr, *ptr, **ptr);
}

6. What is the o/p of the program given below?


main()
{
static int a[ ] = {0, 1, 2, 3, 4};
static int *p[ ] = {a, a + 1, a + 2, a + 3, a + 4};
int **ptr = p;
ptr++;
printf (“%d %d %d\n”, ptr - p, *ptr - a, **ptr);
*ptr++;
printf (“%d %d %d\n”, ptr - p, *ptr - a, **ptr);
*++ptr;
printf (“%d %d %d\n”, ptr - p, *ptr - a, **ptr);
++*ptr;
printf (“%d %d %d\n”, ptr - p, *ptr - a, **ptr);
}

7. What is the o/p of the program given below?


main()
{
static int a[ ] = {0, 1, 2, 3, 4};
static int *p[ ] = {a, a + 1, a + 2, a + 3, a + 4};
int **ptr;
ptr = p;

**ptr++;
printf (“%d %d %d\n”, ptr - p, *ptr - a, **ptr);
*++*ptr;
printf (“%d %d %d\n”, ptr - p, *ptr - a, **ptr);
++**ptr;
printf (“%d %d %d\n”, ptr - p, *ptr - a, **ptr);
}

8. What is the o/p of the program given below?


main()
{
static int n[3][3] = {
2, 4, 3,
6, 8, 5,
3, 5, 1
};
/* Assume that array begins at addresses 404 */
printf (“%d %d %d”, n, n[2], n[2][2]);
}

9. What is the o/p of the program given below?


main()
{
static int n[3][3] = {
2, 4, 3,
6, 8, 5,
3, 5, 1
};
int i, *ptr;
ptr = n;
printf (“%d”, n[2]);
printf (“%d”, ptr[2]);
printf (“%d”, *(ptr + 2));
}

10. What is the o/p of the program given below?


main()
{
static int n[3][3] = {
1, 2, 3,
4, 5, 6,
7, 8, 9
};
int *ptr[3] = { a[0], a[1], a[2]};
int **ptr1 = ptr;
int i;
for ( i = 0; i <= 2; i++)
printf (“%d”, *ptr[i]);

printf (“\n”);
for ( i = 0; i <= 2; i++)
printf (“%d”, *a[i]);

printf (“\n”);
for ( i = 0; i <= 2; i++)
{
printf (“%d”, **ptr1);
ptr1++;
}
}

11. What is the o/p of the program given below?


main()
{
static char str[ ] = “Limericks”;
char *s;
s = &str[6] – 6;
while (*s)
printf (“%c”, *s++);
}

12. What is the o/p of the program given below?


main()
{
static char *s[ ] = {
“ice”,
“green”,
“cone”,
“please”
};
static char **ptr[ ] = {s + 3, s + 2, s + 1, s};
char **p = ptr;
printf (“%s\n”, **++p);
printf (“%s\n”, *--*++p + 3);
printf (“%s\n”, *p[-2] + 3);
printf (“%s\n”, p[-1][-1] + 1);
}

13. What is the o/p of the program given below?


main()
{
static char *mess[ ] = {
“Some love one”,
“Some love two”,
“I love one”,
“That is you”
};
printf (“%d %d”, sizeof (mess), sizeof (mess[1]));
}

14. What is the o/p of the program given below?


main()
{
static char mess[2][2][20] = {
{
“A chink in your armour”,
“ A voice in your mailbox”
};
{
“A foot in your tooth”,
“ A hole in your pocket”
};
printf (“%s\n%s”, mess[1][0], mess[0][1]);
}
15) ​What is the o/p of the program given below?
main(){
int array[] = { 1,2,3,4,5,6,7}
char * b = (char *) array;
char *c = (char *)&array[0];
printf("%d %d", b[1], c[4]);
}
a) Compilation error b) 1, 5 c) 2, 6 ​d) 0, 3​ e) Run time error

16) ​What is the o/p of the program given below?


main(){
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love U");
else
printf("I hate U");
}

1​7) ​What is the o/p of the program given below?


main(){
char *p;
printf("%d %d ",sizeof(*p),sizeof(p));
}

18) ​What is the o/p of the program given below?


main(){
int i=5;
printf("%d%d%d%d%d",i++,i--,++i,--i,i);
}

19) ​What is the o/p of the program given below?


main(){
char *p="hai",*p1;
p1=p;
while(*p!='\0') ++*p++;
printf("%s %s",p,p1);
}

20) ​What is the o/p of the program given below?


main(){
static char names[5][20]={"pascal","ada","cobol","fortran","perl"};
int i;
char *t;
t=names[3];
names[3]=names[4];
names[4]=t;
for (i=0;i<=4;i++)
printf("%s",names[i]);
}

21) ​What is the o/p of the program given below?


main( ){
int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
printf(“%u %u %u %d \n”,a,*a,**a,***a);
printf(“%u %u %u %d \n”,a+1,*a+1,**a+1,***a+1);
}
Answer:
100, 100, 100, 2
114, 104, 102, 3
Explanation:
The given array is a 3-D one. It can also be viewed as a 1-D array.

2 4 7 8 3 4 2 2 2 3 3 4
100 102 104 106 108 110 112 114 116 118 120 122

thus, for the first printf statement a, *a, **a give address of first element. Since
the indirection ***a gives the value. Hence, the first line of the output. For the
second printf a+1 increases in the third dimension thus points to value at 114,
*a+1 increments in second dimension thus points to 104, **a +1 increments the
first dimension thus points to 102 and ***a+1 first gets the value at first location
and then increments it by 1. Hence, the output.

22) ​What is the o/p of the program given below?


#include<stdio.h>
main(){
char s[]={'a','b','c','\n','c','\0'};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
printf("%d",++*p + ++*str1-32);
}

23) ​What is the o/p of the program given below?


main(){
char *ptr = "Ramco Systems";
(*ptr)++;
printf("%s\n",ptr);
ptr++;
printf("%s\n", ptr);
}

24) ​What is the o/p of the program given below?


int x;
main(){
int x=0;
{
int x=10;
x++;
change_value(x);
x++;
Modify_value();
printf("First output: %d\n",x);
}
x++;
change_value(x);
printf("Second Output : %d\n",x);
Modify_value();
printf("Third Output : %d\n",x);
}
Modify_value(){
return (x+=10);
}
change_value(){
return(x+=1);
}

25) ​What is the o/p of the program given below?


main(){
int i, n;
char *x = “girl”;
n = strlen(x);
*x = x[n];
for(i=0; i<n; ++i){
printf(“%s\n”,x);
x++;
}

26) ​What is the o/p of the program given below?


main(){
char *str1="abcd";
char str2[]="abcd";
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
}

27) ​What is the o/p of the program given below?


main(){
int arr2D[3][3];
printf("%d\n", ((arr2D==* arr2D)&&(* arr2D == arr2D[0])) );
}

28) ​What is the o/p of the program given below?


main(){
char *p = “album”;
printf(“%c”,++*(p++));
}
29) ​What does the decleration given below mean?
void ( * abc( int, void ( *def) () ) ) ();

Answer:
abc is a ptr to a function which takes 2 parameters .(a). an integer variable.(b) a
pointer to a funtion which returns void. the return type of the function is void.

30) ​What is the o/p of the program given below?


void main(){
char *s[]={ "dharma","hewlett-packard","siemens","ibm"};
char **p;
p=s;
printf("%s",++*p);
printf("%s",*p++);
printf("%s",++*p);
}

31) ​What is the o/p of the program given below?


main( ){
int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
printf(“%u %u %u %d \n”,a,*a,**a,***a);
printf(“%u %u %u %d \n”,a+1,*a+1,**a+1,***a+1);
}
Answer:
100, 100, 100, 2
112, 104, 102, 3
Explanation:
The given array is a 3-D one. It can also be viewed as a 1-D array.

2 4 7 8 3 4 2 2 2 3 3 4
100 102 104 106 108 110 112 114 116 118 120 122

thus, for the first printf statement a, *a, **a give address of first element . since the indirection
***a gives the value. Hence, the first line of the output.
for the second printf a+1 increases in the third dimension thus points to value at 112, *a+1
increments in second dimension thus points to 104, **a +1 increments the first dimension thus
points to 102 and ***a+1 first gets the value at first location and then increments it by 1.
Hence, the output.

32) ​What is the o/p of the program given below?


main(){
static char *s[ ] = {“black”, “white”, “yellow”, “violet”};
char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
**++p;
printf(“%s”,*--*++p + 3);
}
33) ​What is the o/p of the program given below?
int aaa() {printf(“Hi”);}
int bbb(){printf(“hello”);}
iny ccc(){printf(“bye”);}
main(){
int ( * ptr[3]) ();
ptr[0] = aaa;
ptr[1] = bbb;
ptr[2] =ccc;
ptr[2]();
}
Answer:
​bye
Explanation:
int (* ptr[3])() says that ptr is an array of pointers to functions that takes no arguments and returns
the type int. By the assignment ptr[0] = aaa; it means that
the first function pointer in the array is initialized with the address of the function aaa. Similarly, the
other two array elements also get initialized with the addresses of the functions bbb and ccc.
Since ptr[2] contains the address of the function ccc, the call to the function ptr[2]() is same as
calling ccc(). So it results in printing "bye".

34) ​Differentiate the above declarations.


1. const char *a;
2. char* const a;
3. char const *a;
Answer:
1. 'const' applies to *a rather than 'a' (a is pointer to a constant char )
*a='F' : illegal
a="Hi" : legal
2. 'const' applies to 'a' rather than *a (constant pointer to char )
*a='F' : legal
a="Hi" : illegal
3. Same as 1.

35) ​Declare ​a​ as array of integer pointers

int *a[10] ;

36)​ Declare ​abc​ as array of pointers to functions returning char.

char ( * abc[10] ) ( ) ;

37)​ What does the declaration ​int (*a)[6 ]​ mean?

a​ is a pointer to array of 6 int

38) ​What does the declaration ​char *(*a)[20 ]​ mean?


a​ is a pointer to array of 20 character pointers

39) ​What does the declaration ​char **a[20]​ mean?

a ​is an array of 20 pointer to character pointer

40)​ Declare ​abc​ as array of pointer to function returning integer pointer.

int * ( * abc[ ] ) ( ) ;

41) ​What does the declaration ​void (*signal())()​ mean?

signal​ is a function returning pointer to function returning void

42) ​What does the declaration ​void (*signal(int x,void (*y)(int)))(int) ​ mean?

signal​ is a function which takes arguments(int and pointer to function


taking int as argument and returning void ) returning pointer to function
taking argument(int) returning void

43)​ What does the declaration ​char (*(*f( ))[ ] )( )​ mean?

f is a function returning a pointer to an array of pointers to functions returning a char

44) ​What does the declaration ​int ( * ( * ( *f )( ) )[10] )( )​ mean?

f is a pointer to a function returning a pointer to an array of 10 pointers to function


returning an int

45) ​What is the o/p of the program given below?


main(){ /* Assume base address of the array is 2051*/
int a[][4] = {0};
int *p ;
int (*q)[4] ;
p = (int *)a ;
q=a;
printf(“\n %u , %u”,p,q) ;
p++ ;
q++ ;
printf(“\n %u , %u”,p,q) ;
}

46) ​What is the o/p of the program given below?


main(){
char *mess[ ] = { “This is c”, “Pointers are easy”,“I got job”} ;
printf(“%d, %d”,sizeof(mess),sizeof(mess[1]) ;
}

47) ​What is the o/p of the program given below?


main(){
printf(&3[“sundaram”]) ;
}

48) ​What is the o/p of the program given below?


#include<stdarg.h>
#include<stdio.h>
myfun(int tot_num,...){
int m = -32768, count, num ;
va_list ptr ;
va_start(ptr, tot_num);
for ( count = 1 ; count < tot_num ; count++ ){
num = va_arg(ptr,int) ;
if ( num > m )
m = num ;
}
va_end(ptr);
return(m) ;
}
main(){

int x=myfun(5,23,31,56,11,120) ;
clrscr() ;
printf("%d",x) ;
getch();
}

49) ​What is the o/p of the program given below?


main(){
struct a{
char *str ;
struct a *ptr ;
};
static struct arr[ ] = { {“kamal”,arr+2},
{“kiran”,arr},
{“kunal”,arr+1}};
struct a *p[3] ;
int I ;

for ( i = 0 ; I <= 2 ; i++ )


p[i] = arr[i].ptr ;
printf(“%s\n”,p[0]->str) ;
printf(“%s\n”,(*p)->str) ;
printf(“%s\n”,**p) ;
}

50) ​What is the o/p of the program given below?


void display(){
printf(“impossible \n”) ;
}
main(){
void (*f)() ;
f = display ;
(*f)() ;
}

Bonus Question

Declare q as a pointer to function returning a pointer to an array of 10


pointers to functions returning a float pointer.

float *( * ( * ( *q ) ( ) )[ ] ) ( )

Exam-01
1)​ Write the output of this program
main(){
int *a, *s, i;
s = a = (int *) malloc( 4 * sizeof(int));
for (i=0; i<4; i++) *(a+i) = i * 10;
printf("%d\n", *s++);
printf("%d\n", (*s)++);
printf("%d\n", *s);
printf("%d\n", *++s);
printf("%d\n", ++*s);
}
2) ​What is the output of this program?
#include<stdio.h>
#define SQUARE(x) (x*x)
void main(){
int i=10,j=5,k=0;
k=SQUARE(i-j);
printf("%d",k);
}
3)​ Print the output of the program
main(){
struct Data {
int a;
int b;
} y[4] = { 1, 10, 3, 30, 2, 20, 4, 40};
struct Data *x = y;
int i;
for(i=0; i<4; i++) {
x->a = x->b, ++x++->b;
printf("%d %d\t", y[i].a, y[i].b);
}
}
4)​ How many times the following program would print 'Jamboree'?
main(){
printf("\n Jamboree");
main();
}
(a) infinite number of times (b) 32767 times c) 65535 times
5)​ What is the output of the following program?
main(){
int i,j;
i=10;
j=sizeof(++i);
printf("%d",i);
}
(a) 11​ ​ ​ (b) 10 (c) 4 (d) compilation error
6)​ What is the output of the following program?
main(){
char string[]="Hello World";
display(string);
}
void display(char *string){
printf("%s",string);
}
7)​ What is the output of the following program?
enum colors {BLACK,BLUE,GREEN};
main(){
printf("%d..%d..%d",BLACK,BLUE,GREEN);
return(1);
}
8)​ What does program given below do?
main(){
int a, b;

printf("Enter two numbers A, B : ");


scanf("%d %d", &a, &b);
a^=b^=a^=b; /* swap A and B */
printf("\nA = %d, B= %d\n", a, b);
}
9)​ Write a general swap macro in C - A macro which can swap any type of data
(ie. int, char,float, struct, etc..)
10)​ What is the output of the following program?
main(){
int i=32765;
for(;i++;printf("%d",i)) ;
}
11)​ What would be the output of the following program.
main(){
int a[5]={2,3};
printf("\n %d %d %d",a[2],a[3],a[4]);
}
12)​ Write the output of this program
#define calc(a, b) (a * b) / (a - b)
void main(){
int a = 20, b = 10;
printf("%d\n", calc(a + 4, b -2));
}
13)​ What will be output of the following program ?
void main(){
int cnt = 5, a;
do {
a /= cnt;
} while (cnt --);
printf ("%d\n", a);
}
14)​ What is the output of the following program?
#include<stdio.h>
void main(){
printf("%d",3^2);
}
15)​ What is the output of the following program?
#include<stdio.h>
void main(){
printf("%d",3|2);
}

16)​ What is the o/p of the following program?


main(){
static int i=3;
printf("%d",i--);
return (i>0 ?) main():0;
}
17​)​ # define swap(a,b) temp=a; a=b; b=temp;
main( ){
int i, j, temp;
i=5;
j=10;
temp=0;
if( i > j)
swap( i, j );
printf( "%d %d %d", i, j, temp);
}
18) ​What is the output of the following program.?
main(){
int i=-3,j=2,k=0,m;
m=++i&&++j||++k;
printf("\n %d %d %d %d",i,j,k,m);
}

19) ​What is the output of the following program?


#define CUBE(x) (x*x*x)
main(){
int a,b=3;
a=CUBE(b++);
printf("\n %d %d",a,b);
}
(a) 64 4 (b) 27 4 (c) 27 6 (d) 64 6

20) ​What is the output of the following program?


#include<stdio.h>
void main(){
printf("%d",3>>2?100:200);

21​)​ hat is the output of the following program?


W
#include<stdio.h>
void main(){
int i=0;
switch(printf("k"),printf("ku"))
{
case 1:printf("%d\n",i);break;
case 2:printf("%d",++i);break;
}
}
22)​ What is the output of the following program?
main(){
printf("%x",-1<<4);
}

23) ​What is the output of the following program?


main(){
int a[3]={2,3,4};
char *p;
p=(char *)a;
p=(char *)((int *)p+1);
printf("%d",*p);
}
(a) 2 (b) 0 (c) junk value (d) 3
24)​ What is the output of this program?
#include<stdio.h>
void main(){
if(0,1)
printf("hello");
}
25)​ What is the output of this program?
#include<stdio.h>
#define COMBINE(x,y) (x##y)
void main(){
int i;
i=COMBINE(10+5,5+10);
printf("%d",i);
}
26)​ ​What is the output of the following program?
#include <stdio.h>
main(){
int i;
i =0x10+010+10;
printf("%d",i);
}
27) ​What is the output of the following program?
#include<stdio.h>
main(){
char s1[]="Ramco";
char s2[]="Systems";
s1=s2;
printf("%s",s1);
}
28) What is the o/p of the program given below?
#include<stdio.h>
main(){
int I = 23 ;
printf(“%d,%d”,i++,i++) ;
}
29) What is the o/p of the program given below?
#include<stdio.h>
main(){
printf(“%d”,sizeof(‘A’));
}
30​) ​ What will be the value of i & j after the loop is executed? for(i=0,j=0;i<5,j<25;i++,j++) ;
(a) i=4,j= 24 (b) i=24,j= 24 ​(c)​ i=25,j= 25 (d) i=5,j=25

EXAM – 2

1. What is the output of the following program :


Main()
{
printf(“%d %d %d %d”, 72, 072, 0x72, 0X72);
}
2. What is the output of the following program :
Main()
{
printf(“%d”, 4/3);
printf(“%d”, 4/-3);
prinft(“%d”, -4/3);
printf(“%d”, -4/-3);
}
3. What is the output of the following program :
Main()
{
printf(“%d”, 4%3);
printf(“%d”, 4%-3);
prinft(“%d”, -4%3);
printf(“%d”, -4%-3);
}
4. What is the output of the following program :
Main()
{
float a=12.25, b=13.65;
if (a=b)
printf(“a and b are equal”);
else
printf(“a and b are not equal”);
}
5. What is the output of the following program :
Main()
{
float a=0.7;
if(a<0.7)
printf(“stoned”);
else
printf(“Avenged”);
}
6. What is the output of the following program :
Main()
{
int a=3, b=4;
b%=3+4;
a*=a+5;
printf(b = %d a = %d”, b, a);
}
7. What is the output of the following program :
Main()
{
int x=3, z;
z=x++ + ++x;
printf(“x = %d z = %d”, x, z);
}
8. What is the output of the following program :
Main()
{
int x=3, z;
z=x----1;
printf(“x = %d z = %d”, x, z);
}
9. What is the output of the following program :
Main()
{
int x, y, z;
x = y = z = -1;
z=++x||++y&&++z;
printf(“x = %d y = %d z = %d\n”, x, y, z);
}
10. What is the output of the following program :
Main()
{
int i;
for(i=-1; i<=10; i++)
{
if (i<5)
continue;
else
break;
printf(“Gets printed only once!!”);
}
}
11. What is the output of the following program :
Main()
{
int x=10, y;
y = --x--;
printf(“x = %d y = %d”, x, y);
}
12. What is the output of the following program :
Main()
{
int i;
printf(“Enter any number”);
scanf(“%d”, &i);
switch(i)
{
case 1:
printf(“Do”);
case 2:
printf(“Re”);
case 3:
printf(“Me”);
case default:
printf(“Fa So La Ti Do”);
}
}
13. What is the output of the following program :
Main()
{
int i=3;
switch(i)
{
case 1:
printf(“au revoir!”);
case 2:
printf(“adieu!”);
case 3:
continue;
default:
printf(“plain simple goodbye!”);
}
}
14. What is the output of the following program :
Main()
{
char ch=’E’;
switch(ch)
{
case (ch>=65 && ch<=90):
printf(“Capital Letter”);
break;
case (ch>=97 && ch<=122):
printf(“Small case Letter”);
break;
case (ch>=48 && ch<=57):
printf(“Digit”);
break;
default:
printf(“Any other character”);
}
}
15. What is the output of the following program :
Main()
{
int I, k=1;
here :
if(k>2)
goto out;
there :
for(i=1; i<=5; i++)
printf(“%d”, i);
k++;
goto there;
out : ;
}
16. What is the output of the following program :
Main()
{
int i=45;
float c;
c=check(i);
printf(“c = %f”, c);
}
check(ch)
int ch;
{
ch>=45 ? return(3.14) : return(6.28);
}
17. What is the output of the following program :
Main()
{
C()
{
c()
{
printf(“C is a C…\n”);
}
printf(“..is a c…\n);
}
printf(“..is a sea afterall!”);
}

18. What is the output of the following program :


Main()
{
int k=35, z;
k=func1(k=func1(k=func1(k)));
printf(“k=%d”, k);
}
func1(k)
int k;
{
k++;
return(k);
}
19. What is the output of the following program :
Main()
{
int k=35, z;
z=func(k);
printf(“z = %d”, z);
}
func(m)
int m;
{
++m;
return(m=func1(++m));
}
func1(m)
int m;
{
m++;
return(m);
}
20. What is the output of the following program :
Main()
{
int p=23, f=24;
packman(p, f);
printf(“p = %d f = %d”, p, f);
}
packman(q, h)
int q, h;
{
q=q+q;
h=h+h;
return(q);
return(h);
}
21. What is the output of the following program :
Main()
{
char a=65, ch=’C’;
printit(a, ch);
}
printit(a, ch)
{
printf(“a = %d ch = %c”, a, ch);
}
22. What is the output of the following program :
Main()
{
int i=1;
if(!i)
printf(“Recursive calls are real pain!”);
else
{
i=0;
printf(“Recursive calls are challenging\n”);
main();
}
}
23. What is the output of the following program :
Main()
{
int i;
for(i=1; i<=10; i++)
main();
printf(“In the year of lord\n”);
}
24. What is the output of the following program :
Main()
{
float a=0.7;
double b=0.7;
long double c=0.7;
if (a==b||b==c)
printf(“Condition satisfied”);
else
printf(“Condition not satisfied”);
printf(“a = %f b = %!f c = %Lf”, a, b, c);
}
25. What is the output of the following program :
Main()
{
auto int i = 100;
printf(“i = %d\n”, i);
{
int i=1;
printf(“i = %d\n”, i);
{
i += 1;
printf(“i = %d\n”, i);
}
printf(“i = %d\n”, i);
}
printf(“i = %d\n”, i);
}
26. What is the output of the following program :
Main()
{
int i, j = 10, arrsize;
int arr[arrsize];
if (j==10)
arrsize=20;
else
arrsize=40;
for (i=0; i<arrsize; i++)
arr[i]=100;
}
27. What is the output of the following program :
Main()
{
static int b[]={10, 20, 30, 40, 50};
int i, *k;
k=&b[4] – 4;
for (i=0; i<=4; i++)
{
printf(“%d”, *k);
k++;
}
}
28. What is the output of the following program :
Main()
{
static int a[]={2, 4, 6, 8, 10};
int i;
for (i=0; i<=4; i++)
{
*(a+i)=a[i]+i[a];
printf(“%d”, *(i+a));
}
}
29. What is the output of the following program :
Main()
{
static int a[5]={2, 3, 4, 5, 6};
int i;
for (i=4; i=0; i--)
printf(“%d”, a[i]);
}
change (b)
int *b;
{
int i;
for (i=0; i<=4; i++)
{
*b = *b + 1;
b++;
}
}
30. What is the output of the following program :
Main()
{
Main()
{
static int a[]={0, 1, 2, 3, 4};
static int *p[]={a, a+1, a+2, a+3, a+4};
int **ptr = p;
ptr++;
printf(“%d %d %d\n”, ptr – p, *ptr – a, **ptr);
*ptr++;
printf(“%d %d %d\n”, ptr – p, *ptr – a, **ptr);
*++ptr;
printf(“%d %d %d\n”, ptr – p, *ptr – a, **ptr);
++*ptr;
printf(“%d %d %d\n”, ptr – p, *ptr – a, **ptr);
}
31. What is the output of the following program :
Main()
{
char str[20];
int i;
for (i=0; i<=18; i++)
i[str] = ‘C’;
i[str]=’\0’
printf(“%s”, str);
}
32. What is the output of the following program :
Main()
{
static char str[]={48, 48, 48, 48, 48, 48, 48, 48, 48, 48};
char *s;
int i;
s = str;
for (i=0; i<=9; i++)
{
if (*s)
printf(“%c”, *s);
s++;
}
}
33. What is the output of the following program :
Main()
{
static char s[]=”Lumps, bumps, swollen veins, new pains”;
char t[40];
char *ss, *tt;
tt = t;
ss = s;
while(*tt++ = *ss++0;
printf(“%s”, t);
}
34. What is the output of the following program :
Main()
{
struct s1
{
char *str;
struct s1 *ptr;
};
static struct s1 arr[]={
{“Nikhil”, arr+1},
{“Aditya”, arr+2},
{“Sudheer”, arr}
};
struct s1 *p[3];
int i;
for (i=0; i<=2; i++)
p[i]=arr[i].ptr;
printf(“%s\n”, p[0]->str);
printf(“%s\n”, (*p)->str);
printf(“%s\n”, (**p));
}
35. What is the output of the following program :
Main()
{
union a
{
int i;
chat ch[2];
};
union a u;
u.i = 256;
printf(“%d %d %d”, u.i, u.ch[0], u.ch[1]);
}

1. main()
{
struct employee
{
char name[25];
int age;
float bs;
}
struct employee e;
e.name = ”Hacker” ;
e.age = 25;
printf(“%s %d”, e.name, e.age);
}

2. main()
{
struct
{
char name[25];
char language;
}a;
static struct a = {“Hacker”, “C” };
printf(“%s %d”, e.name, e.language);
}

3. struct s
{
char ch;
int i;
float a;
};
main()
{
static struct s var = { ‘C’, 100, 12.55 }
f ( var );
g( var );
}
f (v )
struct s v;
{
printf( “ %c %d %f ”, v->ch, v->i, v->a);
}
g (v)
struct s *v;
{
printf( “ %c %d %f ”, v.ch, v.i v.a);
}

4. main( )
{
static struct emp
{
char name[20];
int age;
struct address
{
char city[20];
long int pin;
}a;
}e={“abhijeet”,30,”nagpur”,440010};
printf(“%s %s”,e.name,e.city);
}

5. main( )
{
struct a
{
char arr[10];
int i;
float b;
}v[2];
/* assume that first structure begins at address 1004 */
printf(“%d %d %d \n”, v[0].arr, &v[0].i, &v[0].b) ;
printf(“%d %d %d \n” ,v[1].arr, &v[1].i, &v[1].b) ;
}

6. main( )
{
struct a
{
char ch[7];
char *str;
};
struct b
{
char *c;
struct a ss1;
};
static struct s2 = {“Raipur”,”Kanpur”,”Jaipur”};
printf(“%s %s\n” , s2.c,s2.ss1.str);
}
7. main( )
{
struct s1
{
char *z;
int i;
struct s1 *p;
};
static struct s1 a[ ] = {
{“Nagpur”, 1,a+1},
{“Raipur”,2,a+2},
{“Kanpur”,3,a}
};
struct s1 *ptr = a;
printf (“%s %s %s\n” a[0].z ,ptr->z , a[2].p->z);
}

8. main( )
{
struct s1
{
char *z;
int I;
struct s1 *p;
};
static struct s1 a[] = {
{“Nagpur”, 1,a+1},
{“Raipur”,2,a+2},
{“Kanpur”,3,a}
};
struct s1 *ptr = a;
printf(“%s\n”,++(ptr->z);
printf(“%s\n”,a[(++ptr->i].z);
}

9. main()
{
struct s1
{
char *str;
struct s1 *ptr;
};
static struct s1 arr[] = {
{“Nikhil”,arr+1};
{“Adithya”,arr+2};
{“Sudheer”,arr};
};
struct s1 *p[3];
int i;
for(i=0;i<=2;i++)
p[i]=arr[i].ptr;
printf(“%s\n”,p[0]->str);
printf(“%s\n”,(*p)->str);
printf(“%s\n”,(**p));

10. struct s1
{
char *str;
struct s1 *next;
};
main()
{
static struct s1 arr[] = {
{“Nikhil”,arr+1};
{“Adithya”,arr+2};
{“Sudheer”,arr};
};
struct s1 *p[3];
int i;

for (i=0;i<=2;i++)
p[i]=arr[i].next;

printf(“%s %s %s”,p[0]->str,*p->str,**p.str);
swap(*p,arr);

printf(“\n%s”,p[0]->str);
printf(“\n%s”,(*p)->str);
printf(“\n%s”, (*p)->next->str);

swap ( p[0], p[0]->next);


printf(“\n%s”,p[0]->str);
printf(“\n%s”,(*++p[0].str);
printf(“\n%s”,++(*++(*p)->next).str);
}

swap (p1,p2)
struct s1 *p1, *p2;
{
char *temp;
temp=p1->str;
p1->str=p2->str;
p2->str=temp
}
11. #define NULL 0
main()
{
struct node
{
struct node *previous;
int data;
struct node *next;
}
struct node *p, *q:
p=malloc(sizeof(struct node));
q=malloc(sizeof(struct node));

p->data=75;
q->data=90;

p->previous=NULL;
p->next=q;
q->previous=p;
q->next=NULL;

while(p!=NULL)
{
printf(“%d\n”,p->data);
p=p->next;
}
}

12. #define NULL 0


main()
{
struct node
{
int dta;
struct node *next;
};
struct node *p, *q;
p=malloc(sizeof(struct node));
q=malloc(sizeof(struct node));
p->data=10;
q->data=20;
p->next=q;
q->next=p;
while(p!=NULL)
{
printf(“%d\n”,p->data);
p=p->next;
}
}
13. main()
{
union a
{
int I;
char ch[2];
};
union a u:
u.ch[0]=3;
u.ch[1]=2;
printf(“%d 5d %d”, u.ch[0], u.ch[1], u.i);
}
14. main()
{
struct a
{
int i;
int j;
};
struct b
{
char x;
char y;
};
union c
{
struct a aa;
struct b bb;
};
union c u;

u.aa.i=256;
u.aa.j=512;
printf(“%d %d”, u.aa.i, u.aa.j);
printf(“%d %d”,u.bb.x, u.bb.y);
}

15. main()
{
union
{
unsighned long l;
unsighned int d[2];
char ch[4];
}a;
stropy(a.ch,"ABC");
printf("%s\n",a.ch);
printf("%u %u\n". a.d[0], a.d[1]);
printf("%u",a.l);
}

16.main()
{
int i=32, j=65, k;
k=i/35;
printf(“k=%d\n”,k);
k= ~ k;
printf(“k=%d\n”,k);
k=i & j;
printf(“k=%d\n”,k);
}

Shellsort
void shellsort( input_type a[ ], unsigned int n )
{
unsigned int i, j, increment;
input_type tmp;

for( increment = n/2; increment > 0; increment /= 2 )


for( i = increment+1; i<=n; i++ )
{
tmp = a[i];
for( j = i; j > increment; j –= increment )
if( tmp < a[j-increment] )
a[j] = a[j-increment];
else
break;
a[j] = tmp;
}
}

Hashing
Hashing is a technique used for performing insertions, deletions and finds in constant average time.

Open Hashing (Separate Chaining)


The first strategy, commonly known as either open hashing, or separate chaining, is to keep a
list of all elements that hash to the same value. For convenience, lists have headers.
To perform a find, we use the hash function to determine which list to traverse. We then
traverse this list in the normal manner, returning the position where the item is found. To perform an
insert, we traverse down the appropriate list to check whether the element is already in place (if
duplicates are expected, an extra field is usually kept, and this field would be incremented in the event
of a match). If the element turns out to be new, it is inserted either at the front of the list or at the end
of the list, whichever is easiest.
. The hash table structure contains the actual size and an array of linked lists, which are
dynamically allocated when the table is initialized. The HASH_TABLE type is just a pointer to this
structure.

Type declaration for open hash table

typedef struct list_node *node_ptr;


struct list_node{
element_type element;
node_ptr next;
};
typedef node_ptr LIST;
typedef node_ptr position;

/* LIST *the_list will be an array of lists, allocated later */


/* The lists will use headers, allocated later */
struct hash_tbl{
unsigned int table_size;
LIST *the_lists;
};
typedef struct hash_tbl *HASH_TABLE;

Initialization routine for open hash table

HASH_TABLE initialize_table( unsigned int table_size ){


HASH_TABLE H;
int i;

if( table size < MIN_TABLE_SIZE ){


error("Table size too small");
return NULL;
}
/* Allocate table */
H = (HASH_TABLE) malloc ( sizeof (struct hash_tbl) );
if( H == NULL )
fatal_error("Out of space!!!");

H->table_size = next_prime( table_size );


/* Allocate list pointers */
H->the_lists = (position *)malloc( sizeof (LIST) * H->table_size );
if( H->the_lists == NULL )
fatal_error("Out of space!!!");
/* Allocate list headers */
for(i=0; i<H->table_size; i++ ){
H->the_lists[i] = (LIST) malloc( sizeof (struct list_node) );
if( H->the_lists[i] == NULL )
fatal_error("Out of space!!!");
else
H->the_lists[i]->next = NULL;
}
return H;
}

Find routine for open hash table

The call find(key, H) will return a pointer to the cell containing key.
Position find( element_type key, HASH_TABLE H ){
position p;
LIST L;

L = H->the_lists[ hash( key, H->table_size) ];


p = L->next;
while( (p != NULL) && (p->element != key) )
p = p->next;
return p;
}

Insert routine for open hash table

Void insert( element_type key, HASH_TABLE H ){


position pos, new_cell;
LIST L;

pos = find( key, H );


if( pos == NULL ){
new_cell = (position) malloc(sizeof(struct list_node));
if( new_cell == NULL )
fatal_error("Out of space!!!");
else{
L = H->the_lists[ hash( key, H->table size ) ];
new_cell->next = L->next;
new_cell->element = key;
L->next = new_cell;
}
}
}

Closed Hashing (Open Addressing)


Open hashing has the disadvantage of requiring pointers. This tends to slow the algorithm
down a bit because of the time required to allocate new cells, and also essentially requires the
implementation of a second data structure. Closed hashing, also known as open addressing, is an
alternative to resolving collisions with linked lists. In a closed hashing system, if a collision occurs,
alternate cells are tried until an empty cell is found. In a closed hashing system, if a collision occurs,
alternate cells are tried until an empty cell is found. More formally, cells h0(x), h1(x), h2(x) . . . are
tried in succession where hi(x) = (hash(x) + f(i))mod H_SIZE, with ¦(0) = 0. The function, f, is the
collision resolution strategy. Generally, the load factor should be below l = 0.5 for closed hashing.
In linear probing, f is a linear function of i, typically f(i) = i. This amounts to trying cells
sequentially (with wraparound) in search of an empty cell.

Quadratic Probing
Quadratic probing is a collision resolution method that eliminates the primary clustering
problem of linear probing. Quadratic probing is what you would expect–the collision function is
quadratic. The popular choice is f(i) = i2.
Although quadratic probing eliminates primary clustering, elements that hash to the same
position will probe the same alternate cells. This is known as secondary clustering

Type declaration for closed hash tables

enum kind_of_entry { legitimate, empty, deleted };


struct hash_entry{
element_type element;
enum kind_of_entry info;
};

typedef INDEX position;


typedef struct hash_entry cell;

/* the_cells is an array of hash_entry cells, allocated later */


struct hash_tbl{
unsigned int table_size;
cell *the_cells;
};
typedef struct hash_tbl *HASH_TABLE;

Routine to initialize closed hash table

HASH_TABLE initialize_table( unsigned int table_size ){


HASH_TABLE H;
int i;

if( table_size < MIN_TABLE_SIZE ){


error("Table size too small");
return NULL;
}
/* Allocate table */
H = (HASH_TABLE) malloc( sizeof ( struct hash_tbl ) );
if( H == NULL )
fatal_error("Out of space!!!");
H->table_size = next_prime( table_size );
/* Allocate cells */
H->the cells = (cell *) malloc( sizeof ( cell ) * H->table_size );
if( H->the_cells == NULL )
fatal_error("Out of space!!!");
for(i=0; i<H->table_size; i++ )
H->the_cells[i].info = empty;
return H;
}

Find routine for closed hashing with quadratic probing

As with open hashing, find(key, H) will return the position of key in the hash table. If key is
not present, then find will return the last cell. This cell is where key would be inserted if needed.
Further, because it is marked empty, it is easy to tell that the find failed. We assume for convenience
that the hash table is at least twice as large as the number of elements in the table, so quadratic
resolution will always work.

Position find( element_type key, HASH_TABLE H ){


position i, current_pos;

i = 0;
current_pos = hash( key, H->table_size );

while( (H->the_cells[current_pos].element != key ) &&


(H->the_cells[current_pos].info != empty ) ){
current_pos += 2*(++i) - 1;
if( current_pos >= H->table_size )
current_pos -= H->table_size;
}
return current_pos;
}

Insert routine for closed hash tables

Void insert( element_type key, HASH_TABLE H ){


position pos;

pos = find( key, H );


if( H->the_cells[pos].info != legitimate ){ /* ok to insert here */
H->the_cells[pos].info = legitimate;
H->the_cells[pos].element = key;
}
}
Double Hashing

The last collision resolution method we will examine is double hashing. For double hashing,
one popular choice is f(i) = i × h2(x). This formula says that we apply a second hash function to x and
probe at a distance h2(x), 2h2(x) . . . and so on.

Rehashing

If the table gets too full, the running time for the operations will start taking too long and
inserts might fail for closed hashing with quadratic resolution. A solution, then, is to build another
table that is about twice as big (with associated new hash function) and scan down the entire original
hash table, computing the new hash value for each (non-deleted) element and inserting it in the new
table. This entire operation is called rehashing.
programs. The exercises ask you to investigate the use of rehashing in conjunction with lazy deletion.
Rehashing can be used in other data structures as well. For instance, if the queue data structure of
Chapter 3 became full, we could declare a double-sized array and copy everything over, freeing the
original.
HASH_TABLE rehash( HASH_TABLE H ){
unsigned int i, old_size;
cell *old_cells;

old_cells = H->the_cells;
old_size = H->table_size;

/* Get a new, empty table */


H = initialize_table( 2*old_size );

/* Scan through old table, reinserting into new */


for( i=0; i<old_size; i++ )
if( old_cells[i].info == legitimate )
insert( old_cells[i].element, H );
free( old_cells );
return H;
}

Open Hashing
typedef struct list_node *node_ptr;
struct list_node {
element_type element;
node_ptr next;
};

typedef node_ptr LIST;


typedef node_ptr position;
struct hash_tbl {
unsigned int table_size;
LIST the_lists;
};

typedef struct hash_tbl *HASH_TABLE;


HASH_TABLE initialize_table(unsigned int table_size){
HASH_TABLE H;
int i;
H = (HASH_TABLE) malloc(sizeof(struct hash_tbl));
H table_size = next_prime(table_size);
H the_lists = (LIST) malloc(sizeof(struct list_node) * H table_size);

For (i = 0; I < H table_size; i++) {


H the_lists[i].next = NULL;
Return (H);
}

position find (element_type key, HASH_TABLE H) {


position P;
struct list_node L;
L=H the_lists [hash(key, H table_size)];
P = L.next;
while((P!=NULL) && (P element != KEY))
P=P next;
Return P;
}

void insert (element_type key, HASH_TABLE H) {


position pos, new_cell;
LIST L;
Pos = find(key, H);
If (pos == NULL) {
new_cell = (position) malloc (sizeof(struct list_node));
L = &H the_lists[hash(key, H table_size)];
new_cell next = L next;
new_cell element = key;
L next = new_cell;
}

Fundamentals of computer Algorithms

1. Prim’s minimum spanning tree algorithm :

Line procedure ​PRIM(​E, COST,​ ​n, T, mincost)


//​ E i​ s the set of edges in G //
// COST(n, ​n) ​is the cost adjacency matrix of an ​n ​vertex graph //
// such that COST(i, j) is either a positive real number or + if //
// no edge ​(i,j) ​exists. A minimum spanning tree is computed and //
// stored as a set of edges in the array ​T(1:n –​ 1, 2). ​(T(i, ​1), //
//​ T(i, ​2​))​ is an edge in the min-cost spanning tree. The final cost //
// is assigned to mincost //
1 real​ ​COST(n. n), mincost;
2 integer ​NEAR(n), n, i, j, k, l, T(1:n –​ 1, 2);
3 (k, l) edge with minimum cost
4 mincost ​COST(k, l​)
5 (T(​ 1​, 1​ ), ​T(​ 1​, 2​ )) ​(k, l​)
6 for​ ​i 1​ ​to​ ​n ​do​ // initialize NEAR //
7 if​ ​COST(i, l)​ < ​COST(i, k) t​ hen​ ​NEAR ​(​i)​ ​l
8 else​ ​NEAR(i) ​k ​endif
9 repeat
10 NEAR (k) ​NEAR (​ ​l​) 0
11 for​ ​i 2 to ​n ​– 1 ​do​ // find ​n ​– 2 additional edges for ​T /​ /
12 let j be an index such that ​NEAR ​(j) 0 and ​COST(j, NEAR (​ ​j)​ ​is m
​ inimum
13 (​T(​ ​i, ​1), ​T(​ ​i, ​2)) (​j,​ ​NEAR(​ ​j​))
14 millcost​ ​millcost ​+ ​COST(​ ​j, NEAR(​ ​j​))
15 NEAR (​ ​j​) 0
16 for​ ​k 1 ​to​ ​n d ​ o​ // update NEAR //
17 if​ ​NEAR (k) ​0 ​and​ ​COST​(​k, NEAR(​ ​k)​ )​ ​> ​COST(​ ​k, j)​
18 then​ ​NEAR (k) ​j
19 end if
20 repeat
21 repeat
22 if​ ​mincost ​then print​ (‘​no spanning tree​’)​ e​ ndif
23 end PRIM

2. Kruskal’s algorithm :

line procedure ​KRUSKAL (E, COST, n, ​1: ​mincost)


// E i​ s the set of edges in ​G. G​ has ​n v​ ertices. COST ​(u, v) ​is the //
// cost of edge ​(u, v). T i​ s the set of edges in the minimum span-//
// ning tree and mincost is its cost //
1 real ​mincost, COST (​ 1​:n, l:n)
2 integer ​PARENT(1:n), ​T(1:n –​ 1,2), ​n
3 construct a heap out of the edge costs using ​HEAPIFY
4 PARENT ​– 1​ // each vertex is in a different set //
5 i ​mincost 0
6 while ​i ​< ​n –​ 1 ​and ​heap not empty ​do
7 delete a minimum cost edge ​(u, v) ​from the heap and reheapify using ​ADJUST
8 j​ ​FIND (u); k ​FIND (v)
9 if ​j​ ​k ​then ​i ​ 1
​i +
10 T(i, 1​ ) ​u; T(i, ​2) ​v
11 mincost ​mincost + ​ ​COST(u. v)
12 call ​UNION (j, k)
13 endif
14 repeat
15 if i n ​–​ 1 then print (‘no spanning tree’) endif
16 return
17 end KR USKAL

3. A more sophisticated union algorithm :

procedure ​UNION(i,j)
// union sets with roots ​i a​ nd ​j​, ​i​ ​ j,​ using the weighting rule. //
// PARENT(​i​) = ​– C ​ OUNT(​i​) and PARENT(​i)​ = ​– C ​ OUNT(​j)​ . //
integer ​i, j, x
x PARENT(​i)​ + PARENT(​j​)
if​ ​PARENT(i) > ​ ​PARENT(j)
then ​PARENT​(​i)​ ​ ​j​ // ​i ​has fewer nodes //
PARENT(​ ​j​)​ ​x
else ​PARENT(​ ​j​)​ ​i ​//​ j​ has fewer nodes //
PARENT(​ ​i​)​ ​x
endif
end ​UNION

4. FIND using the collapsing rule :

procedure ​FIND(i)
// Find the root of the tree containing element ​i. U ​ se the collapsing rule to collapse all nodes //
// from ​i ​to the root ​j​ //
j​ ​i
while ​PARENT(​ ​j​)​ ​> 0 ​do ​// find root //
j​ ​PARENT(​ ​j​)
repeat
k ​i
while ​k​ ​ o ​// collapse nodes from ​i ​to root ​j​ //
​j d
t ​PARENT(​ ​k)​
PARENT(​ ​k)​ ​ ​j
k ​t
repeat
return​(​j​)
end ​FIND

5. Depth first search of a graph :

line procedure ​DFS(v)


// Given an undirected (directed) graph G = (​V, E)​ ​ ​with ​n ​vertices and an array VISITED(n) //
// initially set to zero, this algorithm visits all vertices reachable from ​v. ​G and VISITED are
global //
1 VISITED(v) 1
2 for ​each vertex ​w​ adjacent from ​v ​do
3 if ​VISITED(​ ​w)​ ​ ​= 0 ​then call ​DFS​(​w​)​ e​ ndif
4 repeat
5 end ​DFS
6. Algorithm for breadth first search :

line procedure​ ​BFS(v)


// A breadth first search of G is carried out beginning at vertex ​v. ​All vertices visited are marked
//
// as VISITED(​i​) = 1. The graph ​G a​ nd array VISITED are global and VISITED is initialized to
//
// zero. //
1 VISITED(​ ​v​)​ 1; ​u ​v
2 initialize ​Q​ to be an empty queue // ​Q​ ​is a queue of unexplored vertices //
3 loop
4 for​ all vertices ​w​ adjacent from ​u d ​ o
5 if​ ​VISITED(​ ​w)​ ​ ​= 0 ​then call​ ADDQ(​ ​w, Q
​ ) // ​w ​is unexplored //
6 VISITED(​ ​w)​ ​ 1
7 endif
8 repeat
9 if​ ​Q​ is empty ​then return endif​ // no unexplored vertex //
10 call​ ​DELETEQ(​ ​u, Q)​ // get first unexplored vertex //
11 repeat
12 end​ ​BFS

You might also like