the following proposed code:
- properly sorts both initial arrays using the 'bubble sort' algorithm
- properly merges both sorted arrays
- outputs the contents of sorted DTA[]
- outputs the contents of sorted DTB[]
- outputs the contents of merged big_array[]
Note: the original sorting algorithm did not work correctly
Note: the original merge algorithm did not work correctly
and now, the proposed code:
#include <stdio.h>
#include <string.h>
//initialising variables and the arrays containing each of the four groups.
//
char *DTA [12] =
{
"James Smith DT01 DT265A",
"John Murphy DT02 DT265A",
"Robert Lam DT03 DT265A",
"Michael Martin DT04 DT265A",
"William Brown DT05 DT265A",
"David Roy DT06 DT265A",
"Richard Tremblay DT07 DT265A",
"Joseph Lee DT08 DT265A",
"Thomas Gagnon DT09 DT265A",
"Charles Wilson DT10 DT265A",
"Chris Philips DT11 DT265A",
"Henry Hill DT12 DT265A"
} ;
char *DTB [14] =
{
"Brian Smith DT23 DT265B",
"Edward Brown DT24 DT265B",
"Ronald Wilson DT25 DT265B",
"Tim Robertson DT26 DT265B",
"Jason Thomson DT27 DT265B",
"Jeff Campbell DT28 DT265B",
"Geoff Stewart DT29 DT265B",
"Ryan Anderson DT30 DT265B",
"Gary Scott DT31 DT265B",
"Jacob Murray DT32 DT265B",
"Nicholas MacDonald DT33 DT265B",
"Eric Reid DT34 DT265B",
"Nicholas Taylor DT35 DT265B",
"Larry Clark DT36 DT265B"
} ;
int main( void )
{
size_t lengthDTA = sizeof(DTA)/sizeof(DTA[0]);
size_t lengthDTB = sizeof(DTB)/sizeof(DTB[0]);
char *big_array [ lengthDTA + lengthDTB ];
//This is a bubble sorting Algorithm.
//It sorts DTA & DTB in order and then
//prints the sorted arrays out.
//
for ( size_t j = 0; j < lengthDTA - 1 ; j++ )
{
for ( size_t i = j + 1 ; i < lengthDTA ; i++ )
{
if( strcmp(DTA[j], DTA[i]) > 0 )
{
char *temp = DTA[j] ;
DTA[j] = DTA[i] ;
DTA[i] = temp ;
}//end if
}//end for
}//end for
for ( size_t j = 0; j < lengthDTB - 1 ; j++ )
{
for ( size_t i = j + 1 ; i < lengthDTB ; i++ )
{
if( strcmp(DTB[j], DTB[i]) > 0 )
{
char *temp = DTB[j] ;
DTB[j] = DTB[i] ;
DTB[i] = temp ;
}//end if
}//end for
}//end for
puts( "\nsorted DTA" );
for( size_t k=0; k < lengthDTA; k++ )
{
printf( "DTA #%3.3lu = %s\n", k, DTA[k] );
}
puts( "\nsorted DTB" );
for( size_t k=0; k < lengthDTB; k++ )
{
printf( "DTB #%3.3lu = %s\n", k, DTB[k] );
}
//This is a merging algorithm.
//It merges DTA & DTB into the array big_array.
//
size_t i = 0;
size_t j = 0;
size_t index = 0;
while( i < lengthDTA
&& j < lengthDTB )
{
if( strcmp( DTA[i], DTB[j] ) > 0 )
{
big_array[ index ] = DTA[i] ;
i++;
}
else
{
big_array[ index ] = DTB[j] ;
j++;
}
index++;
}//end while
while( i < lengthDTA )
{
big_array[ index ] = DTA[i];
i++;
index++;
}
while( j < lengthDTB )
{
big_array[ index ] = DTB[ j ];
j++;
index++;
}
puts("");
for ( size_t index=0; index < (lengthDTA+lengthDTB); index++)
{
printf("Merged %3.3lu is %s\n", index+1, big_array[index]);
}
getchar();
getchar();
return 0;
}//end main()
The resulting output:
sorted DTA
DTA #000 = Charles Wilson DT10 DT265A
DTA #001 = Chris Philips DT11 DT265A
DTA #002 = David Roy DT06 DT265A
DTA #003 = Henry Hill DT12 DT265A
DTA #004 = James Smith DT01 DT265A
DTA #005 = John Murphy DT02 DT265A
DTA #006 = Joseph Lee DT08 DT265A
DTA #007 = Michael Martin DT04 DT265A
DTA #008 = Richard Tremblay DT07 DT265A
DTA #009 = Robert Lam DT03 DT265A
DTA #010 = Thomas Gagnon DT09 DT265A
DTA #011 = William Brown DT05 DT265A
sorted DTB
DTB #000 = Brian Smith DT23 DT265B
DTB #001 = Edward Brown DT24 DT265B
DTB #002 = Eric Reid DT34 DT265B
DTB #003 = Gary Scott DT31 DT265B
DTB #004 = Geoff Stewart DT29 DT265B
DTB #005 = Jacob Murray DT32 DT265B
DTB #006 = Jason Thomson DT27 DT265B
DTB #007 = Jeff Campbell DT28 DT265B
DTB #008 = Larry Clark DT36 DT265B
DTB #009 = Nicholas MacDonald DT33 DT265B
DTB #010 = Nicholas Taylor DT35 DT265B
DTB #011 = Ronald Wilson DT25 DT265B
DTB #012 = Ryan Anderson DT30 DT265B
DTB #013 = Tim Robertson DT26 DT265B
Merged 001 is Charles Wilson DT10 DT265A
Merged 002 is Chris Philips DT11 DT265A
Merged 003 is David Roy DT06 DT265A
Merged 004 is Henry Hill DT12 DT265A
Merged 005 is James Smith DT01 DT265A
Merged 006 is John Murphy DT02 DT265A
Merged 007 is Joseph Lee DT08 DT265A
Merged 008 is Michael Martin DT04 DT265A
Merged 009 is Richard Tremblay DT07 DT265A
Merged 010 is Robert Lam DT03 DT265A
Merged 011 is Thomas Gagnon DT09 DT265A
Merged 012 is William Brown DT05 DT265A
Merged 013 is Brian Smith DT23 DT265B
Merged 014 is Edward Brown DT24 DT265B
Merged 015 is Eric Reid DT34 DT265B
Merged 016 is Gary Scott DT31 DT265B
Merged 017 is Geoff Stewart DT29 DT265B
Merged 018 is Jacob Murray DT32 DT265B
Merged 019 is Jason Thomson DT27 DT265B
Merged 020 is Jeff Campbell DT28 DT265B
Merged 021 is Larry Clark DT36 DT265B
Merged 022 is Nicholas MacDonald DT33 DT265B
Merged 023 is Nicholas Taylor DT35 DT265B
Merged 024 is Ronald Wilson DT25 DT265B
Merged 025 is Ryan Anderson DT30 DT265B
Merged 026 is Tim Robertson DT26 DT265B