1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
2
Rep Power
0

C programming question again.

i was doing this programming to merge an sorted array of size n into another sorted array of size 2n and i'm kinda stuck again. Can somebody please help me out again.

#include <stdio.h>
#define arrsize 4
#define arrsize_b 2
#define arrsize_a (arrsize+arrsize_b)
int main(void)
{
int i,j,k=0,a[4],b[2],temp;
int array_a[arrsize_a];
int array_b[arrsize_b];

printf("Enter the numbersfor array A:\n");
for(i=0; i<arrsize; i++)
{
scanf("%d",&a[i]);
}
printf("Enter the numbers for array B:\n");
for(i=0;i<arrsize_b;i++)
{
scanf("%d",&b[i]);
}
i=0;
j=i-1;
while(i==j)
{
for(i=0;i<arrsize_a;i++)
{

if(a[i]>b[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
i++;
j++;

}

}

}
printf("\n\n\n After combined and sorted both array : ");
for(i=0;i<arrsize_a;i++)
{
printf("\n Element [%d] = %d",i+1,a[i]);
}

return 0;
}
Just look at a few other recent posts to see how it's done, and what code looks like when it is done.
3. My only c code of the day, I promise.

Notes about intent of merge function. If the implementation varies it is a programming error.
• I've ensured there is enough space for the result.
(The requirement that the space exactly matches is stronger than necessary a necessary condition.)
• Project: generalize merge using *void data type and int(*compare)(void*,void*) as arguments.
• The result pointer (index variable ic) increments for each pass of the loop.
• The other index variables ia and ib increment only when data is taken from corresponding array.
• When one of the arrays is consumed it must be true that the rest of the data is copied into the resultant c array.
• I've made sure not to overrun the array A and B sizes.

Code:
```/*
* merge two sorted arrays into a sorted output
* Example in bash, linux.
* \$ a=./c && make -k \$a && echo 246813 | ./c
* cc -Wall c.c -o c
* Enter the characters for array A:
* Enter the characters for array B:
* A holds
* 2468
* B holds
* 13
* merge A B into C
* verify that A is sorted ascending
* verify that B is sorted ascending
* C holds
* 123468
* verify that C is sorted ascending
*/

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

#define DIM(A) ((sizeof(A))/(sizeof(*(A))))

puts(msg);
while (0 < n--)
*pc++ = getchar();
}

void display(char*msg,char*pc,size_t n) {
puts(msg);
while (0 < n--)
putchar(*pc++);
putchar('\n');
}

void verify(char*msg,char*pc,size_t n) {
int i;
printf("verify that %s is sorted ascending\n",msg);
for (i = 1; i < n; ++i)
assert(pc[i-1] <= pc[i]);
}

void merge(char*A,size_t na,char*B,size_t nb,char*C,size_t nc) {
int ia,ib,ic;
assert((na+nb) == nc);
verify("A",A,na);
verify("B",B,nb);
for (ia = ib = ic = 0; ic < nc; ++ic)
if ((ia < na) && (ib < nb))	/* yes, we could get by with fewer comparisons */
C[ic] = (A[ia] < B[ib]) ? A[ia++] : B[ib++];
else if (ia < na)
C[ic] = A[ia++];
else
C[ic] = B[ib++];
}

int main(void) {
char A[4], B[2], C[DIM(A)+DIM(B)];
load("Enter the characters for array A:",A,DIM(A));
load("Enter the characters for array B:",B,DIM(B));
display("A holds",A,DIM(A));
display("B holds",B,DIM(B));
puts("merge A B into C");
merge(A,sizeof(A),B,sizeof(B),C,sizeof(C));
display("C holds",C,DIM(C));
verify("C",C,sizeof(C));
return 0;
}```
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
1
Rep Power
0
5. What is your specific question? The main program of the
February 8th, 2013 12:01 PM
post reads about as clearly in English as I could write without strange contrivances. The other functions are short but I admit terse since the usual way to write some of these would be with a for loop and an extra variable as
Code:
```/* untested */