B
bpascal123
Hi,
This code below works until value 22. I really wonder why from this
point, the merging is not working and my computer says value 22 is
423, value 23 : 431, value 24 : 0, value 25 : 1 and as you can see if
you run this code, it's not matching table B... Why so?
Thanks for helping,
Pascal
#include <stdio.h>
main()
{
int a[50], b[50], merg[100] ;
int ia, ib ;
int i, j, k, k2, imerg ;
int op, cnt ;
ia = 10 ; ib = 15 ;
printf("\n\n==============================\n\n") ;
printf("This code reads, displays 2 arrays and merges them into a
third array \"merg\".") ;
printf("\n\n===============\n\n") ;
printf("\nTable A : \n\n") ;
for ( i = 0 ; i < ia ; i++ )
{
a = op * 10 / 8 + 6 ;
op = a ;
printf("Value %2d : %4d \n", cnt++, a) ;
}
printf("\n\nTable B : \n\n") ;
for ( j= 0, cnt = 1, op = 0 ; j < ib ; j++ )
{
b[j] = op * 10 / 7 + 4 ;
op = b[j] ;
printf("Value %2d : %4d \n", cnt++, b[j]) ;
}
/* MERGING OF TABLE A AND B */
i = 0 ; j = 0 ; imerg = 0 ;
k2 = ia + ib ;
/*
(ia < ib ) ? k2 = ib : k2 = ia ;
*/
for ( k = 0 ; k < k2 ; k++ )
{
if ( a < b[j] )
{
merg[imerg] = a ;
imerg++ ;
i++ ;
}
else if ( a > b[j] )
{
merg[imerg] = b[j] ;
imerg++ ;
j++ ;
}
else if ( i <= ia )
{
merg[imerg] = a ;
imerg++ ;
i++ ;
}
else if ( j <= ib )
{
merg[imerg] = b[j] ;
imerg++ ;
j++ ;
}
}
/*
while ( ( i < ia ) && ( j < ib ) )
if ( a < b[j] )
{
merg[imerg] = a ;
imerg++ ;
i++;
}
else
{
merg[imerg] = b[j] ;
imerg++ ;
j++ ;
}
while ( i < ia )
{
merg[imerg] = a ;
imerg++ ;
ia++ ;
}
while (j < ib )
{
merg[imerg] = b[j] ;
imerg++ ;
j++ ;
}
*/
printf("\n\n===============\n\n") ;
printf("Affichage rsltat ");
printf("\n===============\n\n") ;
for ( imerg = 0, cnt = 1 ; imerg < k2 ; imerg++ )
printf("\nValeur %2d : %4d ", cnt++, merg[imerg]) ;
printf("\n\n==============================\n\n") ;
return 0 ;
}
This code below works until value 22. I really wonder why from this
point, the merging is not working and my computer says value 22 is
423, value 23 : 431, value 24 : 0, value 25 : 1 and as you can see if
you run this code, it's not matching table B... Why so?
Thanks for helping,
Pascal
#include <stdio.h>
main()
{
int a[50], b[50], merg[100] ;
int ia, ib ;
int i, j, k, k2, imerg ;
int op, cnt ;
ia = 10 ; ib = 15 ;
printf("\n\n==============================\n\n") ;
printf("This code reads, displays 2 arrays and merges them into a
third array \"merg\".") ;
printf("\n\n===============\n\n") ;
printf("\nTable A : \n\n") ;
for ( i = 0 ; i < ia ; i++ )
{
a = op * 10 / 8 + 6 ;
op = a ;
printf("Value %2d : %4d \n", cnt++, a) ;
}
printf("\n\nTable B : \n\n") ;
for ( j= 0, cnt = 1, op = 0 ; j < ib ; j++ )
{
b[j] = op * 10 / 7 + 4 ;
op = b[j] ;
printf("Value %2d : %4d \n", cnt++, b[j]) ;
}
/* MERGING OF TABLE A AND B */
i = 0 ; j = 0 ; imerg = 0 ;
k2 = ia + ib ;
/*
(ia < ib ) ? k2 = ib : k2 = ia ;
*/
for ( k = 0 ; k < k2 ; k++ )
{
if ( a < b[j] )
{
merg[imerg] = a ;
imerg++ ;
i++ ;
}
else if ( a > b[j] )
{
merg[imerg] = b[j] ;
imerg++ ;
j++ ;
}
else if ( i <= ia )
{
merg[imerg] = a ;
imerg++ ;
i++ ;
}
else if ( j <= ib )
{
merg[imerg] = b[j] ;
imerg++ ;
j++ ;
}
}
/*
while ( ( i < ia ) && ( j < ib ) )
if ( a < b[j] )
{
merg[imerg] = a ;
imerg++ ;
i++;
}
else
{
merg[imerg] = b[j] ;
imerg++ ;
j++ ;
}
while ( i < ia )
{
merg[imerg] = a ;
imerg++ ;
ia++ ;
}
while (j < ib )
{
merg[imerg] = b[j] ;
imerg++ ;
j++ ;
}
*/
printf("\n\n===============\n\n") ;
printf("Affichage rsltat ");
printf("\n===============\n\n") ;
for ( imerg = 0, cnt = 1 ; imerg < k2 ; imerg++ )
printf("\nValeur %2d : %4d ", cnt++, merg[imerg]) ;
printf("\n\n==============================\n\n") ;
return 0 ;
}