0% found this document useful (0 votes)
11 views2 pages

Merge Two 1D Arrays in Java

This document provides a Java programming assignment for Class 10 students, focusing on merging two one-dimensional arrays into a third array. It includes example input values and detailed code to accomplish the merging process, as well as alternative methods for implementation. The assignment is created by Prashant Tandon, a computer teacher at SMC Kanpur.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Merge Two 1D Arrays in Java

This document provides a Java programming assignment for Class 10 students, focusing on merging two one-dimensional arrays into a third array. It includes example input values and detailed code to accomplish the merging process, as well as alternative methods for implementation. The assignment is created by Prashant Tandon, a computer teacher at SMC Kanpur.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

For Class 10 # Created by Prashant Tandon, Computer Teacher, SMC Kanpur # Mobile 9839575830

1D Array – Assignment 1 - Solution


Que 40 ( Text Book Page No 367 ) or (Shared PDF Page No 47)
WRAP to Merge two given OneD Array into Third Array then display all three array
values. For example, Given array values are:
FST 52 9 77 27

SEC 5 19 88 12 279
Output Required Merged Array Values :
TRD 52 9 77 27 5 19 88 12 279

import java . util . * ;


class Merge_Array
{
public static void main( int M, int N )
{
Scanner SC = new Scanner( [Link] );
int FST [ ] = new int [ M ] ;
int SEC [ ] = new int [ N ] ;
int TRD [ ] = new int [ M + N ] ; // Take enough space for Merging
int k ;
[Link]( " Enter the Integer values for First Array : " ) ;
for( k = 0 ; k < [Link] ; k++ )
{
[Link]( "\t Enter the value for FST[ " + k + " ] : " );
FST[k] = [Link]( );
}

[Link]( " Enter the Integer values for Second Array : " ) ;
for( k = 0 ; k < [Link] ; k++ )
{
[Link]( "\t Enter the value for SEC[ " + k + " ] : " );
SEC[k] = [Link]( );
}

[Link]( "\n Given First Array values are : " );


for( k =0 ; k < [Link] ; k++ )
{
[Link]( "\t" + FST[k] );
}

For Class 10 # Created by Prashant Tandon, Computer Teacher, SMC Kanpur # Mobile 9839575830
Page No 1 of 2
For Class 10 # Created by Prashant Tandon, Computer Teacher, SMC Kanpur # Mobile 9839575830
[Link]( "\n Given Second Array values are : " );
for( k =0 ; k < [Link] ; k++ )
{
[Link]( "\t" + SEC[k] );
}

////// Merging process of both array FST & SEC into TRD array //////////
int index = 0 ;
for( k = 0 ; k < [Link] ; k++ )
{
TRD[ index ] = FST[ k ] ; // First Copying FST array into TRD
index ++ ; // using for loop
}

k=0;
while( k < [Link] )
{ // Then Copying SEC array into TRD
TRD[ index ++ ] = SEC[ k++ ] ; // using while loop
}

/////////////////////////////////////////

[Link]( "\n After Merged both array, Third Array values are : " );
for( k = 0 ; k < [Link] ; k++ )
{
[Link]( "\t" + TRD[k] );
}

} // end of main
} // end of class

Instead of a while loop, we can also use for loop like we transferred first array (FST) into TRD :

for( k = 0 ; k < [Link] ; k++ )


{
TRD[ index ] = SEC[ k ] ; // Then Copying SEC array into TRD
index ++ ; // using for loop
}

For Class 10 # Created by Prashant Tandon, Computer Teacher, SMC Kanpur # Mobile 9839575830
Page No 2 of 2

You might also like