vadivel t


{ City } hyderabad
< Country > india
* Profession * software engineer
User No # 45606
Total Questions Posted # 1
Total Answers Posted # 55

Total Answers Posted for My Questions # 1
Total Views for My Questions # 2833

Users Marked my Answers as Correct # 281
Users Marked my Answers as Wrong # 166
Answers / { vadivel t }

Question { Yahoo, 16989 }

write a program to fined second smallest and largest element
in a given series of elements (without sorting)


Answer

#include

void main()
{
int a[10] = {5,4,1,7,3,0,8,23,12,24};
int Lcount = 0, Gcount = 0, i, j;

for(i = 0; i<10; i++)
{
Gcount = 0;
Lcount = 0;
for(j = 0; j<10; j++)
{
if(a[i] > a[j])
{
Gcount++;
Lcount++;
}
}
if(Gcount == 8)
{
printf("The second largest no is: %
d \n", a[i]);
}
if(Lcount == 1)
{
printf("The second smallest no is: %
d \n", a[i]);
}
}
_getch();
}

Is This Answer Correct ?    2 Yes 4 No

Question { 6114 }

A array contains dissimilar element how can we count,
and
A array contains dissimilar element how can we store in
another array with out repetition.


Answer

Hi this code ll work for the second question but first
question i dint understand...

here in souce buff we have 16 nos, in which 1 to 5 repeats
3 times.

but at the end of the program u ll get only nos 1 to 6 as
out in dest buff without duplicate.


Note: Compiler used Visual studio 2005.


#include
#include
void main()
{
int source[16] = {5,3,4,1,2,1,2,3,4,5,2,3,1,5,4,6};
int dest[6], i, j, count = 0, flag;

int test = 0;
/*Copy the first element*/
dest[0] = source[0];
count++;
for(i = 1; i<= 15; i++)
{
/*Check whether the same element is already
available in the dest buff*/
for(j = 0; j {
if(dest[j] == source[i])
{
/*Already available*/
flag = 0;
break;

}
else
{
flag = 1;
}
}
if(flag == 1)
{
/*The same element is not available
already - so copy it to the dest buffer*/
dest[count] = source[i];
count++;
}
}

for(i = 0; i<=5; i++)
printf("%d\t",dest[i]);

_getch();
}

Is This Answer Correct ?    1 Yes 0 No


Question { IBM, 6062 }

How to reverse a linked list


Answer

Hi,
I have written code for some of the possible questions
asked in Single linked list in C.

Also the code covers the answer for the question asked here.

Here in the code, almost all printf statements may be of two
lines, So if you are directly copiying the code, into ur
compiler u may get errors. So make all the printf to single
line then compile it and see the o/p.

#include
#include
#define ADDATBEG 1
#define ADDATEND 2
#define INSERT 3
#define DEL 4
#define SEARCH 5
#define SORT 6
#define REVERSE 7
#define NODEFROMEND 8

struct node
{
int data;
struct node *next;
};
struct node *BaseNode = '\0';

/*Function prototypes*/
void AddAtBeg(struct node **BaseNode, int data);
void AddAtEnd(struct node **BaseNode, int data);
void AddAtMid(struct node **BaseNode, int data, int pos);
void DelNode(struct node **BaseNode, int pos);
void DisList(struct node **BaseNode);
void SearchNode(struct node **BaseNode, int data);
void funchoice(int choice);
void SortList(struct node **BaseNode);
void NthNodeFrmEnd(struct node **BaseNode, int n);
void RevList(struct node **BaseNode);

int main()
{
int i, choice;
printf("ENTER THE CHOICE:\n");
printf("1.TO ADD THE NODE AT BEGINING - STACK \n2.TO ADD
THE NODE AT THE END - QUEUE\n");
printf("\nCHOICE:");
scanf("%d", &choice);
funchoice(choice);

printf("\n\nENTER CHOICE \n");
printf("3.TO INSERT NODE\n");
printf("4.TO DEL NODE\n");
printf("5.TO SEARCH GIVEN NODE IN THE LIST\n");
printf("6.TO SORT THE LIST\n");
printf("7.TO REVERSE THE LIST\n");
printf("8.VALUE OF 'N'th FROM LAST\n");
printf("\nCHOICE:");
scanf("%d", &choice);
funchoice(choice);

getch();
}

void funchoice(int choice)
{
int no = 0, element = 0,i;
switch(choice)
{
case ADDATBEG:
printf("\nENTER THE NO OF ELEMENTS TO BE ADDED\n");
scanf("%d", &no);
printf("\nENTER THE ELEMENTS TO BE ADDED IN THE
LIST\n");
for(i = 0; i {
scanf("%d", &element);
AddAtBeg(&BaseNode, element);

}
DisList(&BaseNode);
break;

case ADDATEND:
printf("\nENTER THE NO OF ELEMENTS TO BE ADDED\n");
scanf("%d", &no);
printf("\nENTER THE ELEMENTS TO BE ADDED IN THE
LIST\n");
for(i = 0; i {
scanf("%d", &element);
AddAtEnd(&BaseNode, element);
}
DisList(&BaseNode);
break;

case INSERT:
printf("\nENTER THE NODE POSTION WHERE THE NEW NODE
TO BE INSERTED\n");
scanf("%d", &no);
printf("\nENTER THE NO TO BE INSERTED\n");

scanf("%d", &element);
AddAtMid(&BaseNode, element, no);
DisList(&BaseNode);
break;

case DEL:
printf("\nENTER THE NODE POSTION OF THE NODE TO BE
DELETED\n");
scanf("%d", &no);
DelNode(&BaseNode, no);
DisList(&BaseNode);
break;

case SEARCH:
printf("\nENTER THE VALUE TO BE SEARCHED IN THE
LIST\n");
scanf("%d", &element);
SearchNode(&BaseNode, element);
break;

case SORT:
SortList(&BaseNode);
DisList(&BaseNode);
break;

case REVERSE:
RevList(&BaseNode);
DisList(&BaseNode);
break;

case NODEFROMEND:
printf("\nENTER THE NODE POSITION FROM END TO WHICH
THE VALUE HAS TO BE FOUND\n");
scanf("%d", &no);
NthNodeFrmEnd(&BaseNode, no);
break;

default:
exit(0);
break;

}
}
/*To print the data in the linked list*/
void DisList(struct node **BaseNode)
{
struct node *temp;
temp = *BaseNode;
printf("\nNODES IN THE LIST IS \n");
while(temp != '\0')
{
printf("%d ", temp->data);
temp = temp->next;
}
}


/*Stack Implementation*/
void AddAtBeg(struct node **BaseNode, int data)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
if(temp != '\0')
{
temp->data = data;
temp->next = *BaseNode;
*BaseNode = temp;
}
}
/*Queue Implementation*/
void AddAtEnd(struct node **BaseNode, int data)
{
struct node *temp, *temp1;
temp = *BaseNode;
if(temp == '\0')
{
/*List is empty - Create first node*/
temp1 = (struct node *)malloc(sizeof(struct node));
if(temp1 != '\0')
{
temp1->data = data;
temp1->next = '\0';
*BaseNode = temp1;
}
}
else
{
/*Node is existing already, So create another node*/
while(temp->next != '\0')
{
temp = temp->next;
}
temp1 = (struct node *)malloc(sizeof(struct node));
if(temp1 != '\0')
{
temp1->data = data;
temp1->next = '\0';
temp->next = temp1;
}
}
}

/*To insert a node between two nodes*/
void AddAtMid(struct node **BaseNode, int data, int pos)
{
/*To insert a node in between to the given node*/
struct node *temp, *temp1;
int count = 0;
temp = *BaseNode;
/*count, the no of nodes available in the list*/
while(temp->next != '\0')
{
count++;
temp = temp->next;
}
/*Check whether the enough nodes are available or not*/
if(pos <= (count+1))
{
temp = *BaseNode;
while(pos-1)
{
temp = temp->next;
pos--;
}
temp1 = (struct node *)malloc(sizeof(struct node));
if(temp1 != '\0')
{
temp1->data = data;
temp1->next = temp->next;
temp->next = temp1;
}
}
else
{
printf("NEW NODE CANT BE ADDED:THERE IS NO ENOUGH
NODES IN THE LIST\n");
}
}

/*To delete a particular node in the list*/
void DelNode(struct node **BaseNode, int pos)
{
struct node *temp, *temp1;
int count = 0, pos1;
temp = *BaseNode;
pos1 = pos;

/*count, the no of nodes available in the list*/
while(temp->next != '\0')
{
count++;
temp = temp->next;
}
/*Check whether the enough nodes are available or not*/
if(pos <= (count+1))
{
temp = *BaseNode;
temp1 = temp->next;
while(pos-2)
{
temp = temp->next;
temp1 = temp1->next;
pos--;
}
temp->next = temp1->next;
printf("\n\nTHE DATA '%d' AT THE NODE '%d' IS
DELETED \n\n", temp1->data, pos1);
free(temp1);
}
else
{
printf("\n\nGIVEN NODE CANT BE DELETED:THERE IS NO
ENOUGH NODES IN THE LIST\n");
}
}

/*To search the given data in the linked list*/
void SearchNode(struct node **BaseNode, int data)
{
struct node *temp;
int count = 0;
temp = *BaseNode;
while(temp != '\0')
{
count++;
if(temp->data == data)
{
printf("\nTHE NODE WITH DATA '%d' FOUND AT
THE POSITION '%d'\n", data, count);
break;
}
else if(temp->next == '\0')
{
printf("\nTHE NODE IS NOT FOUND IN THE
LIST\n");
}
temp = temp->next;
}
}

/*To Sort the linked list in ascending order*/
void SortList(struct node **BaseNode)
{
struct node *temp, *temp1;
int localvar;
temp = *BaseNode;
temp1 = temp->next;
while(temp1 != '\0')
{
if(temp->data > temp1->data)
{
localvar = temp->data;
temp->data = temp1->data;
temp1->data = localvar;
}
temp = temp->next;
temp1 = temp1->next;
}
}

/*To find the 'n'th node from last node of linked list by
traversing the list only once*/
void NthNodeFrmEnd(struct node **BaseNode, int n)
{
struct node *temp, *temp1;
int count = 0;
temp1 = temp = *BaseNode;
while(temp1 != '\0')
{
count++;
temp1 = temp1->next;
}
if(n <= count)
{
count = 0;
temp1 = *BaseNode;
while(temp1->next != '\0')
{
count++;
if(count > n-1)
{
temp = temp->next;
}
temp1 = temp1->next;
}
printf("\nNODE %d FROM LAST, HOLD THE VALUE %d\n",
n, temp->data);
}
else
{
printf("\nTHERE IS NO ENOUGH NODES IN THE LIST\n");

}
}

/*To Reverse a given linked list*/
void RevList(struct node **BaseNode)
{
struct node *Fst, *Sec, *Thrd;
Sec = (*BaseNode)->next;
Thrd = Sec->next;
(*BaseNode)->next = '\0';

while(Thrd != '\0')
{
Sec->next = *BaseNode;
*BaseNode = Sec;
Sec = Thrd;
Thrd = Thrd->next;
}
Sec->next = *BaseNode;
*BaseNode = Sec;
}

Is This Answer Correct ?    6 Yes 1 No

Question { Alcatel, 30813 }

implement NAND gate logic in C code without using any
bitwise operatior.


Answer

int a, b;
scanf("%d %d", &a, &b);
if(a != 0 || b != 0)
{
printf("1");
}
else
{
printf("0");
}

Is This Answer Correct ?    28 Yes 20 No

Question { 4605 }

how to write a data 10 in address location 0x2000


Answer

The below line u can use for the question asked, provided
the address is not pointing to any OS memory, system files
or any location on ROM memory.

0x2000 is invalid address(Access voilation)in my system. So
I tried with the valid address 0x12FF70 to get the o/p.

main()
{
*(int *)(0x12FF70)= 20;
printf("%d \n",*(int *)(0x12FF70));
getch();
}

O/p would be 20.

Is This Answer Correct ?    1 Yes 0 No

Question { NetApp, 5493 }

in one file global variable int i; is declared as static. In
another file it is extern int i=100;
Is this valid ?


Answer

No. it is not valid,

Bcos we can not initialise a variable with an extern key
word.ie.,

The statement extern int i = 100; is wrong.

Is This Answer Correct ?    14 Yes 5 No

Question { NetApp, 9809 }

if function is declared as static in one source file, if I
would like to use the same function in some other source
file...is it possible....how ?


Answer

It is possible. follow the guidelines below.

1.create a .c file called mai.c. and Its content is,

#include
#include "Header.h"

static func(void);
main()
{
func();
printf("\n");
func1();
getch();
}

static func(void)
{
printf("In static fucntion");
}

2.create another file called test.c. And its content is

#include "Header.h"

func1()
{
func();
}
func()
{
printf("In normal function \n");
}

3.have a .h file called Header.h and its content is,

func1();
func();


Now main.c has a function with static key word(ie., static
func()). And its prototype and definition is available in
the same file and the same function name without static is
exist in the test.c and its prototype is there in the
Header.h

When u run the program and control hits func() in main.c it
will call the static function in the same file.

When control hits next line ie., func1() it will call the
fuction func(), which is there in the test.c file(and also
there in main.c with static key word).

Now the output will be,

In static fucntion
In normal function

Is This Answer Correct ?    9 Yes 41 No

Question { Qualcomm, 4598 }

You have an int array with n elements and a structure with
three int members.
ie
struct No
{
unsigned int no1;
unsigned int no2;
unsigned int no3;
};
Point1.Lets say 1 byte in the array element is represented
like this - 1st 3 bits from LSB is one number, next 2 bits
are 2nd no and last 3 bits are 3rd no.

Now write a function, struct No* ExtractNos(unsigned int *,
int count)
which extracts each byte from array and converts LSByte in
the order mentioned in point1.and save it the structure
no1, no2, no3.

in the function struct No* ExtractNos(unsigned int *, int
count), first parameter points to the base address of array
and second parameter says the no of
elements in the array.

For example: if your array LSB is Hex F7 then result no1 =
7, no2 = 2, no3 = 7. In the same way convert all the
elements from the array and save the result in array of
structure.


Answer

Hi small mistake.

change the
printf("ELEMENT IN THE STRUCTURE \n", (i+1));
to
printf("ELEMENT IN THE STRUCTURE %d\n", (i+1));

Is This Answer Correct ?    0 Yes 2 No

Question { 2945 }

what would be the output of the following program

main()
{
int a[] = {1,2,3,4,5};
int *ptr = {a,a+1,a+2,a+3,a+4};
printf("%d %d %d %d",a,*ptr,**ptr,ptr);
}

}


Answer

Output:

1.Base address of 'a'
2.Base address of 'a' (Since ptr holds address of the array 'a')
3.Value at the base address of 'a' ie., 1
4.Base address of array of pointers ie., address of 'ptr'

The above answer is valid provided the initialisation of *ptr
should be a array of pointers.

ie., initialisation should be int *ptr[]=
{a,a+1,a+2,a+3,a+4};

Otherwise it leads to compilation error

Is This Answer Correct ?    0 Yes 0 No

Question { 2833 }

whenever a question is posted in a particular category in
allinterview.com, Is there any facility to receive an
indication mail.

For eg: I need to receive an indication email, whenever a
question is posted under the category “C Langauage”.


Answer

As far as i know, it is not there. If that is the case
then allinterview.com moderator can consider to provide
this feature to the subscribers so that it will be very
useful.

Is This Answer Correct ?    3 Yes 0 No

Prev    1   2   3    [4]