Search Your Query

Breaking

Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

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

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

Sunday, July 7, 2019

July 07, 2019

Find The Maximum Number Using 2D Array


Find The Maximum Number Using 2D Array


Program Code:

#include<stdio.h>
#include<stdlib.h>
int main()
{
int **a,i,j,r,c,max,min;
printf("Enter the size of row and column\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));
}
printf("Enter the elements\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
max=-32766;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(max<a[i][j])
{
max=a[i][j];
}
}
}
printf("Maximum number is-->%d\n",max);
return 0;

}


Hence, logic are same of 1d array

Input:

Enter the size of array
5
Enter the elements
12
47
10
65
89
30

Output:

Maximum number is-->89
July 07, 2019

Find Maximum And Minimum Number In An Array


Find Maximum And Minimum Number In An Array

Program Code:

#include<stdio.h>
#include<stdlib.h>
int main()
{
int *a,i,max,min,n;
printf("Enter the size of array\n");
scanf("%d",&n);
a=(int*) malloc (n*sizeof(int));
printf("Enter the elements\n");
for(i=0;i<=n;i++)
{
scanf("%d",&a[i]);
}
max=a[0];
min=a[0];
for(i=0;i<=n;i++)
{
if(max<a[i])
{
max=a[i];
}
else if(min>a[i])
{
min=a[i];
}
}
printf("maximum number is=%d \n minimum number is=%d\n",max,min);
return 0;
}

Hence, when this two statement  max=a[0]; min=a[0]; will execute first max amd min are both variable will assigns 12 . when if(max<a[i]) condition will execute inside  the loop it check , 12 is less than 47 ? if yes than max will assign 47 either the second condition will execute and min will assign 47 and this procces will continue untill i<=n this condition will false in the loop

Input:

Enter the size of array
5
Enter the elements
12
47
10
65
89
30

Output:


maximum number is=89
minimum number is=10

Saturday, April 27, 2019

April 27, 2019

Sum Of Even Number Using Array



Sum Of Even Number Using Array

Program Code :

#include<stdio.h>
#include<stdlib.h>
int main()
{
int *a,i,size,sum=0;
printf("Enter the size of array\n");
scanf("%d",&size);
printf("Enter the elements\n");
a=(int*)malloc(size*sizeof(int));
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<size;i++)
{
if(a[i]%2==0)
{
sum=sum+a[i];
}
}
printf("The result is-->%d\n",sum);
return 0;

}

Hence , logic is here
if(a[i]%2==0)
{
sum=sum+a[i];
}

Input Of Program :

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

Output :


The result is-->6