Search Your Query

Breaking

Friday, August 7, 2020

August 07, 2020

Write A C Program That Merge Two Array In Sorted Order

Merge Two Sorted Array

Code:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. void merge(int *arr,int *brr,int m,int n)
  4. {
  5. int i=0,j=0,k=0;
  6. int *crr;
  7. int l=(n+m);
  8. crr=(int*)malloc(l*sizeof(int));
  9. while(i<n && j<m)
  10. {
  11. crr[k++]=arr[i]<brr[j]?arr[i++]:brr[j++];
  12. }
  13. while(i<n)
  14. {
  15. crr[k++]=arr[i++];
  16. }
  17. while(j<m)
  18. {
  19. crr[k++]=brr[j++];
  20. }
  21. printf("After merging two array\n");
  22. for(k=0;k<l;k++)
  23. {
  24. printf("%d\n",crr[k]);
  25. }
  26. }
  27. int main()
  28. {
  29. int *arr,*brr,m,n,i;
  30. printf("Enter the size of first array\n");
  31. scanf("%d",&n);
  32. arr=(int*)malloc(n*sizeof(int));
  33. printf("Enter the elements of first array\n");
  34. for(i=0;i<n;i++)
  35. {
  36. scanf("%d",&arr[i]);
  37. }
  38. printf("Enter the size of second array\n");
  39. scanf("%d",&m);
  40. brr=(int*)malloc(m*sizeof(int));
  41. printf("enter the elements of second array\n");
  42. for(i=0;i<m;i++)
  43. {
  44. scanf("%d",&brr[i]);
  45. }
  46. printf("First array is-->\n");
  47. for(i=0;i<n;i++)
  48. {
  49. printf("%d\n",arr[i]);
  50. }
  51. printf("Second array-->\n");
  52. for(i=0;i<m;i++)
  53. {
  54. printf("%d\n",brr[i]);
  55. }
  56. merge(arr,brr,m,n);
  57. return 0;
  58. }
Hint: In line 12 we use ternary operator. crr[k++]=arr[i]<brr[j]?arr[i++]:brr[j++]; . This line means if arr[i]<brr[j] is true then this arr[i++] value assign to array crr[k++] and both arr[] and crr[] array increased by 1.If crr[k++]=arr[i]<brr[j] false then this brr[j++] value assign to crr[k++] and both brr[] and crr[] array increased by 1.
Line 16 and 20 means if any value of arr[] or brr[] still can't assigned to crr[] then it will be assigned.

Input:

Enter the size of first array
5
Enter the elements of first array
1
2
3
4
5
Enter the size of second array
5
enter the elements of second array
6
7
8
9
10
First array is-->
1
2
3
4
5
Second array-->
6
7
8
9
10

Output:

After merging two array
1
2
3
4
5
6
7
8
9
10

Thursday, July 30, 2020

July 30, 2020

Pip is not recognized as an internal and external command



Do you face problem with pip as "pip is not recognized as internal and external command"?


Here is your solution:

  • Step1: First open file . Then right clik to 'This PC' and go to 'Properties'


  • Step2: Go to 'Advanced system Settings'.



  • Step3: Go to 'Environment Variables'.

  • Step4: Double click on 'path'

  • Step5: Go to 'New'

  • Step6: Now minimize this tab (don't closed it) and go to 'C drive' and enter 'Users' folder

Then go to this folder(The floder name should be your pc name):





  • Step7: Now find the 'AppData' folder. If not found (The folder should be hidden) then follow the process.
Press left click on File and then go to this option



After go to this option then select 'view' option and then select 'Show hidden files ,folders , and drivers' option then click 'ok'.



After click ok the hidden file or folder would be shown as follow.



  • Step8: Then go to AppData>Local>Programs>Python>Python37>Scripts and copy that address 'C:\Users\dipro\AppData\Local\Programs\Python\Python37\Scripts'.


  • Step9: Then go to previous tab and paste this address and then click 'ok'.


  • Step10: Now press 'Windows key+R' and type 'cmd' and then open windows command promt and then check by 'pip'. If pip is not installed then just type 'pip install pip' and hit enter
If it work , thanks me later 😊
Now installed different python module as your wish.

Friday, October 25, 2019

October 25, 2019

Write A Program To Create A Matrix



Write A Program To Create A Matrix

Code::

#include<stdio.h>
#include<stdlib.h>
int main()
{
int **a,i,j,r,c;
printf("Enter the size of row and coloumn\n");
scanf("%d%d",&r,&c);
a=(int**)malloc(r*sizeof(int));
for(i=0;i<c;i++)
{
a[i]=(int*)malloc(c*sizeof(int));
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter elemens the position of %d%d\n",i,j);
scanf("%d",&a[i][j]);
}
}
printf("the result is-->\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}
return 0;

}

Input::

Enter the size of row and coloumn
3
3
Enter elemens the position of 00
1
Enter elemens the position of 01
2
Enter elemens the position of 02
3
Enter elemens the position of 10
4
Enter elemens the position of 11
5
Enter elemens the position of 12
6
Enter elemens the position of 20
7
Enter elemens the position of 21
8
Enter elemens the position of 22

9

Output::

the result is-->
   1   2   3
   4   5   6
   7   8   9

October 25, 2019

Insertion Sort In C



Insertion Sort In C


Code::

#include<stdio.h>
#include<stdlib.h>
void insertion(int *a,int n);
int main()
{
int *a,i=0,n,temp;
printf("Enter the size of array\n");
scanf("%d",&n);
a=(int *)malloc(n*sizeof(int));
printf("Enter the elements of array\n");
for(i=0;i<n;i++)
{
/* fflush(stdin);
printf("\n Enter %d th element-->",i);   */
scanf("%d\n",&a[i]);
}
insertion(a,n);
printf("The sorted array is-->\n");
for(i=0;i<n;i++)
{
printf("-->%d\n",a[i]);
}
return 0;
}
void insertion(int *a,int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(j>=0&&temp<a[j])
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
}

Hence: //coming soon

Input::

Enter the size of array
5
Enter the elements of array
5
4
3
2

1

Output::

The sorted array is-->
-->1
-->2
-->3
-->4
-->5


Monday, September 9, 2019

September 09, 2019

Binary Search Using Recursion


Binary Search Using Recursion

Program Code:

#include<stdio.h>
#include<stdlib.h>
void sorting(int *a,int size)
{
int i,j,temp;
for(i=0;i<=size-1;i++)
{
for(j=0;j<=size-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
int b_search(int *a,int beg,int end,int k)
{
int mid;
if(beg>end)
{
return (-1);
}
mid=(beg+end)/2;
if(k==a[mid])
{
return mid;
}
else if(k<a[mid])
{
return b_search(a,beg,mid-1,k);
}
else
{
return b_search(a,end,mid+1,k);
}
}
int main()
{
int *a,size,beg,end,l,sid,k;
printf("Enter the size of the array\n");
scanf("%d",&size);
a=(int*)malloc(size*sizeof(int));
printf("Enter the elements\n");
for(l=0;l<size;l++)
{
scanf("%d",&a[l]);
}
sorting(a,size);
printf("The sorted elements are\n");
for(l=0;l<size;l++)
{
printf("%d\n",a[l]);
}
printf("Entr the search number\n");
scanf("%d",&k);
beg=0;
end=size-1;
sid=b_search(a,beg,end,k);
if(sid!=-1)
{
printf("The search number is found\n");
}
else
{
printf("The search number is not found\n");
}
return 0;

}

Hence , //comming soon

Input:

Enter the size of the array
5
Enter the elements
5
4
3
2
1
The sorted elements are
1
2
3
4
5
Entr the search number

6

Output:

The search number is not found