C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++ tutorials and pdf

the program contains 2 functions. The first 1 divides the list,in this case, the array into small chunks of items. each chunk having 1 or at most 2 items. here,we use recursion to divide the list into these chunks.

the second function takes these chunks and set it in a temporary array temp[] according to its value. no recursion takes place here. left is 0, right=n-1 and mid=mid index. (u need to preserve the value of left and right.. so put them in variables say l,r) now a variable say t is initialized as 0,or left as left is 0.(it will provide index for temp[]) compare the values in the merge[] for indexes l and mid. in case l is small, inc l by 1. if mid is small inc. mid by 1 and in both the steps put the small value in temp[].

even i learnt it today only. and have some doubts in the final steps. i have no idea about why the penultimate if else statement is used. but i am sure about this much.

#6/3/13.. i guess u have ur exam tomorrow.. even i have.. all the best..

penultimate if else is used for the case when either of the both half gets sorted while the remaining has not been sorted.This if else will sort the remaining part also.

when there is more than one recursive calls then the thing that should be noted is value will be received only in the calling function so u need to be aware that which function is calling and where's my value receiving.

The condition on "if(low<high)" in partition() function should be "if(high - low < 2)". I tried your code, when I used 1 million numbers, everything was okay. But, when I used more and more numbers (about 10 millions numbers), there was a segmentation fault. I have no idea why it happened. If I'm using "if(high - low < 2)", it just works fine.

ups my bad, using "(high - low < 2)" makes getting worse, it screws up the result. I think the segmentation fault problem is occured because of huge number of data is used when temp[max] is initialized.

#include void main() { int A[10],B[10],C[20]; int i, j,k; int n1,n2,n3;

printf(" Enter the number of elements in A[] \n"); scanf("%d", &n1); printf(" Enter the Array elements of A[] in sorted form \n"); for (i = 0; i < n1; i++) scanf("%d", &A[i]); printf(" A[] Entered is \n"); for (i = 0; i < n1; i++) printf(" %d", A[i]); printf("\n Enter the number of elements of B[] \n"); scanf("%d", &n2); printf(" Enter the Array elements of B[] in sorted form\n"); for (i = 0; i < n2; i++) scanf("%d", &B[i]); printf(" B[] Entered is \n"); for (i = 0; i < n2; i++) printf(" %d", B[i]); i=0;j=0; k=0; while (i<n1 && j<n2) { if (A[i]<=B[j]) { C[k]=A[i]; i++; } else { C[k]=B[j]; j++; } k++; } while (i<n1) {

C[k]=A[i]; i++; k++; } while (j<n2) {

C[k]=B[j]; j++; k++; }

n3=n1+n2; printf("\n New Array is...\n"); for (i = 0; i < n3; i++) printf(" %d",C[i]); }

#include void main() { int A[10],B[10],C[20]; int i, j,k; int n1,n2,n3;

printf(" Enter the number of elements in A[] \n"); scanf("%d", &n1); printf(" Enter the Array elements of A[] in sorted form \n"); for (i = 0; i < n1; i++) scanf("%d", &A[i]); printf(" A[] Entered is \n"); for (i = 0; i < n1; i++) printf(" %d", A[i]); printf("\n Enter the number of elements of B[] \n"); scanf("%d", &n2); printf(" Enter the Array elements of B[] in sorted form\n"); for (i = 0; i < n2; i++) scanf("%d", &B[i]); printf(" B[] Entered is \n"); for (i = 0; i < n2; i++) printf(" %d", B[i]); i=0;j=0; k=0; while (i<n1 && j<n2) { if (A[i]<=B[j]) { C[k]=A[i]; i++; } else { C[k]=B[j]; j++; } k++; } while (i<n1) {

C[k]=A[i]; i++; k++; } while (j<n2) {

C[k]=B[j]; j++; k++; }

n3=n1+n2; printf("\n New Array is...\n"); for (i = 0; i < n3; i++) printf(" %d",C[i]); }

Please Teach me some Programing....We are learning algorithm and i am not able to understand anything in class...Please if you could help me i will be gratefull to sir

## 24 comments:

plz give me its algorithm..

i'm quiet confuse every time when more than two recursion function calling...plzzz give me some idea... & help me to solve such a situation ...

Thank you, sir. It works like a charm.

same here confused with more than one recursions :( can you give me some ideas

pls give its algo

the program contains 2 functions. The first 1 divides the list,in this case, the array into small chunks of items. each chunk having 1 or at most 2 items. here,we use recursion to divide the list into these chunks.

the second function takes these chunks and set it in a temporary array temp[] according to its value. no recursion takes place here.

left is 0, right=n-1 and mid=mid index.

(u need to preserve the value of left and right.. so put them in variables say l,r)

now a variable say t is initialized as 0,or left as left is 0.(it will provide index for temp[])

compare the values in the merge[] for indexes l and mid. in case l is small, inc l by 1. if mid is small inc. mid by 1 and in both the steps put the small value in temp[].

even i learnt it today only. and have some doubts in the final steps.

i have no idea about why the penultimate if else statement is used. but i am sure about this much.

#6/3/13.. i guess u have ur exam tomorrow.. even i have.. all the best..

Can u tell the formula for counting the number of times the basic operation is repeated??

penultimate if else is used for the case when either of the both half gets sorted while the remaining has not been sorted.This if else will sort the remaining part also.

when there is more than one recursive calls then the thing that should be noted is value will be received only in the calling function so u need to be aware that which function is calling and where's my value receiving.

why max 50 was used

using max 50 is not a must. . . it s just 2 specify the array size. . .

thank u.

THANK YOU SO MUCH

Just start drawing recursion tree, Also keep in mind every block or function maintains separate stack of local variables :D

The condition on "if(low<high)" in partition() function should be "if(high - low < 2)". I tried your code, when I used 1 million numbers, everything was okay. But, when I used more and more numbers (about 10 millions numbers), there was a segmentation fault. I have no idea why it happened. If I'm using "if(high - low < 2)", it just works fine.

Thanks for your code, though.

ups my bad, using "(high - low < 2)" makes getting worse, it screws up the result.

I think the segmentation fault problem is occured because of huge number of data is used when temp[max] is initialized.

problem solved. I should initialize temp to -> temp[max+1]

*) int temp[high+1], or

int *temp = malloc((high+1) * sizeof(int));

#include

void main()

{

int A[10],B[10],C[20];

int i, j,k;

int n1,n2,n3;

printf(" Enter the number of elements in A[] \n");

scanf("%d", &n1);

printf(" Enter the Array elements of A[] in sorted form \n");

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

scanf("%d", &A[i]);

printf(" A[] Entered is \n");

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

printf(" %d", A[i]);

printf("\n Enter the number of elements of B[] \n");

scanf("%d", &n2);

printf(" Enter the Array elements of B[] in sorted form\n");

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

scanf("%d", &B[i]);

printf(" B[] Entered is \n");

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

printf(" %d", B[i]);

i=0;j=0;

k=0;

while (i<n1 && j<n2)

{

if (A[i]<=B[j])

{

C[k]=A[i];

i++;

}

else

{

C[k]=B[j];

j++;

}

k++;

}

while (i<n1)

{

C[k]=A[i];

i++;

k++;

}

while (j<n2)

{

C[k]=B[j];

j++;

k++;

}

n3=n1+n2;

printf("\n New Array is...\n");

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

printf(" %d",C[i]);

}

#include

void main()

{

int A[10],B[10],C[20];

int i, j,k;

int n1,n2,n3;

printf(" Enter the number of elements in A[] \n");

scanf("%d", &n1);

printf(" Enter the Array elements of A[] in sorted form \n");

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

scanf("%d", &A[i]);

printf(" A[] Entered is \n");

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

printf(" %d", A[i]);

printf("\n Enter the number of elements of B[] \n");

scanf("%d", &n2);

printf(" Enter the Array elements of B[] in sorted form\n");

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

scanf("%d", &B[i]);

printf(" B[] Entered is \n");

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

printf(" %d", B[i]);

i=0;j=0;

k=0;

while (i<n1 && j<n2)

{

if (A[i]<=B[j])

{

C[k]=A[i];

i++;

}

else

{

C[k]=B[j];

j++;

}

k++;

}

while (i<n1)

{

C[k]=A[i];

i++;

k++;

}

while (j<n2)

{

C[k]=B[j];

j++;

k++;

}

n3=n1+n2;

printf("\n New Array is...\n");

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

printf(" %d",C[i]);

}

Please Teach me some Programing....We are learning algorithm and i am not able to understand anything in class...Please if you could help me i will be gratefull to sir

Teach me some logics in programing....i couldn't understand anything in class...please help me

#include

void mergeSort(int*, int, int);

using namespace std;

void main() {

int x[] = { 2,12,3,4,7,5,8,12,13,1,9,11,10,1,6,23,45,443,345,1,2,7,6,23,45 };

mergeSort(x, 0, 24);

for (int i = 0; i < 25; i++)

cout << x[i] << ",";

cout << endl;

system("pause");

}

void mergeSort(int *x, int start1, int end2) {

if (start1 >= end2)

return;

int i, j, k, end1 = (start1 + end2) / 2, start2 = end1 + 1, A[20], B[20], n1 = start2 - start1, n2 = end2 - end1;

mergeSort(x, start1, end1);

mergeSort(x, start2, end2);

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

A[i] = x[start1 + i];

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

B[i] = x[start2 + i];

i = j = k = 0;

while (i < n1 && j < n2)

if (A[i] < B[j])

x[start1 + k++] = A[i++];

else

x[start1 + k++] = B[j++];

if (i == n1)

while (j < n2)

x[start1 + k++] = B[j++];

else if (j == n2)

while (i < n1)

x[start1 + k++] = A[i++];

}

give me the pseudo code for merge sort

Post a Comment