Merge the Elements of 2
Sorted Array
#include
#include
void main()
{
int array1[50], array2[50], array3[100], m, n, i, j, k = 0;
clrscr();
printf("\n Enter size of array Array 1: ");
scanf("%d", &m);
printf("\n Enter sorted elements of array 1: \n");
for (i = 0; i < m; i++)
{
scanf("%d", &array1[i]);
}
printf("\n Enter size of array 2: ");
scanf("%d", &n);
printf("\n Enter sorted elements of array 2: \n");
for (i = 0; i < n; i++)
{
scanf("%d", &array2[i]);
}
i = 0;
j = 0;
while (i < m && j < n)
{
if (array1[i] < array2[j])
{
array3[k] = array1[i];
i++;
}
else
{
array3[k] = array2[j];
j++;
}
k++;
}
if (i >= m)
{
while (j < n)
{
array3[k] = array2[j];
j++;
k++;
}
}
if (j >= n)
{
while (i < m)
{
array3[k] = array1[i];
i++;
k++;
}
}
printf("\n After merging: \n");
for (i = 0; i < m + n; i++)
{
printf("\n%d", array3[i]);
}
getch();
}
Output
Enter size of array Array 1: 4
Enter sorted elements of array 1:
12
18
40
60
Enter size of array 2: 4
Enter sorted elements of array 2:
47
56
89
90
After merging:
12
18
40
47
56
60
89
90
Input a String & Store
their Ascii Values in an Integer Array & Print the Array
#include
#include
void main()
{
char string[20];
int n, count = 0;
printf("Enter the no of characters present in an array \n ");
scanf("%d", &n);
printf(" Enter the string of %d characters \n" , n);
scanf("%s", string);
while (count < n)
{
printf(" %c = %d\n", string[count], string[count] );
++ count ;
}
getch();
}
Output
Enter the no of characters present in an array
10
Enter the string of 10 characters
sanfoundry
s = 115
a = 97
n = 110
f = 102
o= 111
u = 117
n = 110
d = 100
r = 114
y = 121
Input an Array, Store the Squares of these
Elements in an Array & Print it
#include
#include
#define max_rows 3
#define max_cols 4
void print_square(int [ ] );
void main (void)
{
int i;
int num [max_rows][max_cols] = { {10, 20, 30, 40}, {50, 60, 70, 80}, {90, 100, 110, 120} };
for (i = 0; i < max_rows; i++)
print_square(num[i]);
}
void print_square(int x[ ])
{
int j;
for (j = 0; j < max_cols; j++)
printf ("%d\t", x[j] * x[j]);
printf("\n");
getch();
}
Output
100 400 900 1600
2500 3600 4900 6400
8100 10000 12100 14400
Print the Alternate Elements in an Array
#include
#include
void main()
{
int array[10];
int i, j, temp;
printf("Enter the element of an array \n");
for (i = 0; i < 10; i++)
scanf("%d", &array[i]);
printf("Alternate elements of a given array \n");
for (i = 0; i < 10; i += 2)
printf( "%d\n", array[i]) ;
getch();
}
Output
Enter the element of an array
12
23
45
57
68
73
84
97
120
125
Alternate elements of a given array
12
45
68
84
120
No comments:
Post a Comment