***All programs must be written in C and must be compilable in DEV C++ (if possi
ID: 3558860 • Letter: #
Question
***All programs must be written in C and must be compilable in DEV C++ (if possible)***
-Complete programs only
-Please run them before you submit them to make sure they work
-Incomplete programs that do not run will not be awarded points.
Thanks a bunch!!
QUESTION 7. (15 points) Write a code that merges two sorted arrays so that the result is a sorted array. In your code, declare an array called "A1' of size 5 and an array "A2' of size 5. Fill them with numbers that are in ascending order. Write a code that merges the two arrays in an array called "A' of size 10 so that A' contains all the numbers in ascending order. This is a sample output. A1 1 3 5 7 8 A1 2 4 69 10 Array: 1 2 3 4 5 6 7 8 9 10Explanation / Answer
#include<stdio.h>
int main()
{
int A1[5]={1 ,2 ,3 ,4, 5};
int A2[5]={6 ,7 ,8 ,9 ,10};
int A[10],i,p=0,c,d,n=10,swap;
for (i=0;i<5;i++)
{
A[p]=A1[i];
A[p+1]=A2[i];
p=p+2;
}
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (A[d] > A[d+1]) /* For decreasing order use < */
{
swap = A[d];
A[d] = A[d+1];
A[d+1] = swap;
}
}
}
printf("Sorted list in ascending order: ");
for (i=0;i<=9;i++)
{
printf("%d ",A[i]);
}
}
Incase you want to input numbers:
#include<stdio.h>
int main()
{
int A1[5];
int A2[5];
int A[10],i,p=0,c,d,n=10,swap;
printf("Please Enter Array A1 ");
for(i=0;i<5;i++)
{
scanf("%d",&A1[i]);
}
printf("Please Enter Array A2 ");
for(i=0;i<5;i++)
{
scanf("%d",&A2[i]);
}
for (i=0;i<5;i++)
{
A[p]=A1[i];
A[p+1]=A2[i];
p=p+2;
}
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (A[d] > A[d+1]) /* For decreasing order use < */
{
swap = A[d];
A[d] = A[d+1];
A[d+1] = swap;
}
}
}
printf("A1 ");
for (i=0;i<=4;i++)
{
printf("%d ",A1[i]);
}
printf("A2 ");
for (i=0;i<=4;i++)
{
printf("%d ",A2[i]);
}
printf("Sorted list in ascending order: ");
for (i=0;i<=9;i++)
{
printf("%d ",A[i]);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.