Class 1
Class 1
Class 1
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
printf(“%d”,z-x) ;
}
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
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.
Solution
main(){
int 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
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.
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
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 (.) { .. }.
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])())();
3. Use the cdecl program, which turns English into C and viceversa:
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.)
main(){
static int i=3;
printf("%d",i--);
return (i>0 ?) main():0;
}
Ans: 321
Ans: Infinite, b'cos 'i' is an unsigned integer and it will not decrement below '0' and hence end up
in an infinite loop.
}
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);
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
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]);
}
}
**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);
}
printf (“\n”);
for ( i = 0; i <= 2; i++)
printf (“%d”, *a[i]);
printf (“\n”);
for ( i = 0; i <= 2; i++)
{
printf (“%d”, **ptr1);
ptr1++;
}
}
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.
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.
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.
int *a[10] ;
char ( * abc[10] ) ( ) ;
int * ( * abc[ ] ) ( ) ;
42) What does the declaration void (*signal(int x,void (*y)(int)))(int) mean?
int x=myfun(5,23,31,56,11,120) ;
clrscr() ;
printf("%d",x) ;
getch();
}
Bonus Question
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;
EXAM – 2
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 (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;
}
}
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;
Hashing
Hashing is a technique used for performing insertions, deletions and finds in constant average time.
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;
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
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.
i = 0;
current_pos = hash( key, H->table_size );
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;
Open Hashing
typedef struct list_node *node_ptr;
struct list_node {
element_type element;
node_ptr next;
};
2. Kruskal’s 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
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