Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
February 7th, 2013, 07:01 PM
 nirvana_1
Registered User

Join Date: Feb 2013
Posts: 2
Time spent in forums: 26 m 22 sec
Reputation 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;
}

#2
February 8th, 2013, 01:37 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,256
Time spent in forums: 2 Months 4 Weeks 1 Day 13 h 40 m 12 sec
Reputation Power: 1827
Just look at a few other recent posts to see how it's done, and what code looks like when it is done.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#3
February 8th, 2013, 12:01 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,140
Time spent in forums: 1 Month 3 Weeks 2 Days 7 h 17 m 40 sec
Reputation Power: 455
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;
}```
__________________
[code]Code tags[/code] are essential for python code!

#4
February 13th, 2013, 07:18 PM
 harun95
Registered User

Join Date: Feb 2013
Posts: 1
Time spent in forums: 22 m 57 sec
Reputation Power: 0

#5
February 13th, 2013, 08:05 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,140
Time spent in forums: 1 Month 3 Weeks 2 Days 7 h 17 m 40 sec
Reputation Power: 455
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 */
int i;
puts(msg);
for (i = 0; i < n; ++i)
pc[i] = getchar();
}```
On review I also see that the messages could have included the number of characters expected,
"Enter the 4 characters for array A" .
Likewise, compilation and piping the input via echo all on one line is subtle. And you might be unfamiliar with unix. load should have a FILE* argument.

Last edited by b49P23TIvg : February 13th, 2013 at 08:11 PM.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > C programming question again.