## Gupta

BAN USERm = ++i&& ++j || ++k;

in this case first value of 'i' is incremented , so it become 0 . in AND binary operation if one value is zero then there is no need to check other value , so we dont increment value of 'j' or say we do not bother about 'j' .so value of 'j' is still 2.

in this expression OR is also there so we have to value of k, so we increment value of 'k', that will now 1.

after all expression become true so value of 'm' is 1.

so final out put is 0 2 1 1

i am just trying it other way. let me know if i am approaching wrong way.

#include<stdio.h>

main()

{

char A[] ={'a','a','b',' ',' ','c',' ','d','e'};

int i=0;

int p=0;

int count = 0;

int length=sizeof(A)/sizeof(A[0]);

printf("Original array is:: %s\n and length of array is %d\n",A,strlen(A));

while(i<length)

{

if(A[i] == ' ')

{

if(count<1)

p = i;

count++;

}

else

{

if(count>=1)

{

A[p]=A[i];

p++;

count--;

}

}

i++;

}

A[p] = '\0';

printf("Final array is :: %s \n and legth of final string is %d\n",A,strlen(A));

getch();

}

// A singly link list having next and random pointer . we have to replicate of this link list.

#include<stdio.h>

// structure of link list node

struct node

{

int data;

struct node *next;

struct node *random;

};

void push(struct node **head_ref, int new_data)

{

struct node* current = *head_ref;

struct node* new_node = (struct node*) malloc(sizeof(struct node));

new_node->data = new_data;

new_node->next = NULL;

new_node->random = NULL;

if(*head_ref == NULL )

{

*head_ref = new_node;

}

else

{

while(current->next!=NULL)

{

current=current->next;

}

current->next = new_node;

}

}

/* this function will assign the random pointer to the node. random_pointer_no is the node number

which is assign as random pointer for node_number node.

*/

void random(struct node *node, int node_number, int random_pointer_no)

{

int count = 1;

struct node *temp = node;

struct node *current = node;

while(current != NULL && count != node_number ) // find out the node for which we want to assign the random pointer

{

current = current->next;

count++;

}

count = 1; // again initialize the variable for below loop

if(random_pointer_no !=1)

{

while(temp != NULL && count != (random_pointer_no-1)) // assign the next node as random pointer to the current node.

{

temp = temp->next;

count++;

}

current->random = temp->next;

}

else

current->random = node;

}

// for clone of link list

void cloneLinkList(struct node *node, struct node **node_clone)

{

// copy the original link list to new link list

struct node *current = node;

while(current != NULL)

{

struct node *current_clone = *node_clone;

struct node* new_node_clone = (struct node*) malloc(sizeof(struct node));

new_node_clone->data = current->data;

new_node_clone->next = NULL;

new_node_clone->random = NULL;

if(*node_clone == NULL )

{

*node_clone = new_node_clone;

}

else

{

while(current_clone->next!=NULL)

{

current_clone=current_clone->next;

}

current_clone->next = new_node_clone;

}

current = current->next;

}

// now i am working for random pointer of new link list

current = node;

struct node *current_clone = *node_clone;

while(current != NULL)

{

int toggle = 1;

struct node *temp = current;

struct node *temp_clone = current_clone;

while(temp->next != current->random)

{

if(temp->next == NULL) // if we reach end of first link list

{

temp = node;

temp_clone = *node_clone;

if(current->random == temp) // if first node is random pointer

{

current_clone->random = temp_clone;

toggle = 0;

break;

}

}

else

{

temp = temp->next;

temp_clone = temp_clone->next;

}

}

if(toggle) // if we dont set the random pointer already

{

current_clone->random= temp_clone->next;

}

current = current->next;

current_clone = current_clone->next;

}

}

void printlist(struct node *node)

{

while(node->next != NULL)

{

printf(" %d ->",node->data);

node = node->next;

}

printf(" %d ->NULL",node->data);

}

void printAddress(struct node *node)

{

printf("\n node value node address node next add node random address \n");

while(node != NULL)

{

printf(" %3d \t %12d \t %12d \t %12d \n",node->data,node,node->next,node->random);

node = node->next;

}

}

int main()

{

int no_node;

int index;

int val;

int random_pos;

struct node *head = NULL;

struct node *head_clone = NULL;

printf("how many node you want in link list::");

scanf("%d",&no_node);

while(no_node < 1)

{

printf("Cheater you enter less than 1 node ,\nPlease again enter number of node in link list:: ");

scanf("%d",&no_node);

}

printf("Enter the integer node data ");

for(index = 0; index < no_node; index++)

{

printf("Enter the %d data ::",(index+1));

scanf("%d",&val);

push(&head,val);

}

printf("Entered link list is :: \n");

printlist(head);

printf("\nFor Random pointer ::\n Please enter the number between 1 to %d \n", no_node);

for(index = 0;index < no_node; index++)

{

printf("for node %d , Enter Random pointer node :: ",(index+1));

scanf("%d",&random_pos);

if(random_pos < 1 || random_pos > no_node)

{

printf("You entered wrong number so i am quitting from this program");

exit(0);

}

random(head,index+1,random_pos);

}

printf("For original link listnode values with next pointer and random pointer::\n");

printAddress(head);

// for clone of link list

printf("\n process for clone link list\n");

cloneLinkList(head,&head_clone);

printf("\n clone link list is :: \n");

printlist(head_clone);

printf("\n For clone link list ,node values with next pointer and random pointer::\n");

printAddress(head_clone);

getch();

}

i am writing my code in C language. i don't know whether this is a write approach or not but this code is working fine .you can give a try for it and let me know if you fine some mischief behavior with code.

/* first i convert the number in to binary representation and then count the sum of bit and check the sum is fibnoci or not*/

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#define LENGTH 40

// check 5n2+4 or 5n2-4 is a perfact squre

int isFibonacci(int number)

{

double x1 = 5 * number*number + 4;

double x2 = 5 * number*number - 4;

long x1_sqrt = sqrt(x1);

long x2_sqrt = sqrt(x2);

return (x1_sqrt*x1_sqrt == x1) || (x2_sqrt*x2_sqrt == x2) ? 1 : 0;

}

int main()

{

unsigned long long int input1,input2;

unsigned long long int i,number;

int index,index1;

int *array;

int sum_of_bits;

int count=0;

printf("Enter the initial and end number to check \n");

scanf("%llu%llu",&input1,&input2);

printf("Entered input are %llu %llu\n",input1,input2);

for(i = input1; i <= input2; i++)

{

index = index1 = sum_of_bits= 0;

array = (int*)malloc(LENGTH*sizeof(int));

number = i;

while(number>0)

{

array[index] = number%2;

number = number/2;

index++;

}

index1 = index-1;

// to print the binary representation of number and sum the bits of number

printf("binary representation of %d is::",i);

for(index = index1; index >= 0; index--)

{

printf("%d",array[index]);

sum_of_bits += array[index];

}

printf("\n");

int check = isFibonacci(sum_of_bits);

if(check == 1)

count++;

free(array);

}

printf("Between %llu and %llu there are %d numbers that have sum of bits equal to a fibonacci number",input1,input2,count);

getch();

}

working code for both part (a) and (b) is:

i think this is self understandable code. you just go through this . let me know if some one has problem with this code.

#include<stdio.h>

#include<malloc.h>

#include<stdlib.h>

/* Link list node */

struct node

{

int data;

struct node* next;

};

/* Function to reverse the linked list */

static void reverse(struct node** head_ref)

{

struct node* prev = NULL;

struct node* current = *head_ref;

struct node* next;

while (current != NULL)

{

next = current->next;

current->next = prev;

prev = current;

current = next;

}

*head_ref = prev;

}

/* Function to push a node */

void push(struct node** head_ref, int new_data)

{

/* allocate node */

struct node* new_node =(struct node*) malloc(sizeof(struct node));

/* put in the data */

new_node->data = new_data;

/* link the old list off the new node */

new_node->next = (*head_ref);

/* move the head to point to the new node */

(*head_ref) = new_node;

}

struct node* addition (struct node* temp1, struct node* temp2)

{

struct node* prev = NULL;

int carry = 0,a,b,result;

while (temp1 || temp2) //while both lists exist

{

result = carry;

if(temp1)

result+=temp1->data;

if(temp2)

result+=temp2->data;

carry=result/10;

struct node * newnode = (struct node*) malloc (sizeof(struct node));

newnode->data=(result)%10;

newnode->next = prev;

prev=newnode;

if(temp1)

temp1=temp1->next;

if(temp2)

temp2=temp2->next;

}

return prev;

}

void printList(struct node *node)

{

while(node != NULL)

{

printf("%d ", node->data);

node = node->next;

}

}

/* Drier program to test above function*/

int main(void)

{

/* Start with the empty list */

struct node* head = NULL;

struct node* head1 = NULL;

struct node* head2 = NULL;

int num1;

int num2;

printf("Enter number 1 ::");

scanf("%d",&num1);

printf("Enter number 2::");

scanf("%d",&num2);

int rem = 0;

if(num1 > 0 && num2 > 0)

{

while(num1 > 0)

{

rem = num1%10;

push(&head1,rem);

num1 = num1/10;

}

while(num2 >0)

{

rem = num2%10;

push(&head2,rem);

num2 = num2/10;

}

printf("list 1 is \t");

printList(head1);

printf("\n");

reverse(&head1);

printf("list 1 reverse is \t");

printList(head1);

printf("\n");

printf("list 2 is \t");

printList(head2);

printf("\n");

reverse(&head2);

head=addition(head1,head2);

printf("resultant list is \t");

printList(head);

printf("\n");

}

else

printf("you have entered wrong number , please enter again");

getch();

}

@Manan , one team can play single match per day. but by ur program single team can play with 2 different teams.

so please modify accordingly.

suppose we have 4 team then your program will give following output.

Day 1 Matches :Match 1: Team 1 Vs Team 2 Match 2: Team 2 Vs Team 3

Day 2 Matches :Match 3: Team 1 Vs Team 3 Match 4: Team 2 Vs Team 4

Day 3 Matches :Match 5: Team 1 Vs Team 4 Match 6: Team 3 Vs Team 4

Total Number of Days is 3

i got answer as

12

35

52

but

When i ran this program on machine i shocked to get the answer 12,32,50.

i am writing here what concept i used.

If we do some operation in printf statement on same element then we have to use stack.

so in first printf statement:-

structure of stack is

++i (top of stack) => 6

i++ => 6

and after the execution of this value of i is 7 and i++ + ++i is 6+6 = 12

in second printf statement

structure of stack is

i++ <- (top of stack) = > 7

i++ =>8

++i => 10

i++ (lower index of stack) => 10

and after the execution of this value of i is 11 and i++ + ++i + i++ + i++is 10 + 10 + 8 + 7 = 35

in third printf statement

structure of stack is

i++ <- (top of stack) = > 11

++i =>13

i++ => 13

++i (lower index of stack) => 15

and after the execution of this value of i is 15 and ++i + i++ + ++i + i++ is 15 + 13 + 13 + 11 = 52

but When i ran this program on machine i shocked to get the answer 12,32,50.

i dont know what concept they used ..most probably side effect..

let me know if some have answer of this question with proper explanation.

thanks in advance ..!!!

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

main()

{

int card[52];

int n;

int i,c;

srand(time(NULL)); // initialize the seed randomly

printf("How many card you want to open from the deck after shuffle");

scanf("%d",&n);

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

{

card[i] = i; // fill the array in order

}

//--- Shuffle elements by randomly exchanging each with one other.

for (i=0; i<(52-1); i++)

{

int r = i + (rand() % (52-i)); // Random remaining position.

printf("rand()%(52-i):%d\n",r);

int temp = card[i];

card[i] = card[r];

card[r] = temp;

}

//--- Print first n cards as ints.

for (c=0; c<n; c++)

{

printf("%d ",card[c]);

}

getch();

}

Suppose there are four person named as a,b,c,d , which take 1,2,5,10 minutes to cross the bridge.

1)first 'A' and 'B' cross the bridge and 'A' will come back again at original side , it will take total of 3 minutes (2+1).

2) now 'C' and 'D' cross the bridge and 'B' will come back again at original side with flash light.it will take total of 12 minutes (10 + 2)

3) Finally 'A' and 'B' cross the bridge with flash light and no need to come back again at original side. because all four are at other side of bridge. this step will take 2 minute.

So total tike taken is 3 + 12+ 2 => 17 minutes.

Let me know if more clarity requires.

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Open Chat in New Window

- Gupta July 23, 2012