Computer Applications
Suppose A, B, C are arrays of integers of sizes m, n, m + n respectively. Give a program to produce a third array C, containing all the data of array A and B.
Java
Java Arrays
6 Likes
Answer
import java.util.Scanner;
public class KboatArrayMerge
{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Enter size m : ");
int m = in.nextInt();
int A[] = new int[m];
System.out.print("Enter size n : ");
int n = in.nextInt();
int B[] = new int[n];
int C[] = new int[m + n];
System.out.println("Enter elements of array A:");
for(int i = 0; i < m; i++) {
A[i] = in.nextInt();
}
System.out.println("Enter elements of array B:");
for(int i = 0; i < n; i++) {
B[i] = in.nextInt();
}
int idx = 0;
while(idx < A.length) {
C[idx] = A[idx];
idx++;
}
int j = 0;
while(j < B.length) {
C[idx++] = B[j++];
}
System.out.println("Elements of Array C :");
for(int i = 0; i < C.length; i++) {
System.out.print(C[i] + " ");
}
}
}Variable Description Table
Program Explanation
Output

Answered By
2 Likes
Related Questions
What do you understand by two-dimensional arrays ? State some situations that can be easily represented by two-dimensional arrays.
How are two-dimensional arrays represented in memory ?
Write a short program that doubles every element of an array A[4][4].
Let A(n x n) that are not diagonal array. Write a program to find the sum of all the elements which lie on either diagonal. For example, for the matrix shown below, your program should output 68 = (1 + 6 + 11 + 16 + 4 + 7 + 10 + 13):