Ask the user for an array size.Then receive this number of character values. Pla
ID: 3631599 • Letter: A
Question
Ask the user for an array size.Then receive this number of character values. Place the values within a char array, i.e. the first array.• Now ask the user for another array size. Copy the first array to this new array. If the new size is smaller than the original, then only copy this number (i.e. the second array’s size) from the first array.
• If the new size is larger than the original,copythe whole array to the second array, and then ask the user for additional characters.
• Print both arrays.
Explanation / Answer
This is an easy task to do. You can understand this algorithm as such
1. Take as input the length of first array.
2. Take as input the elements of first array (here i have used the variable theArray for first array)
3.Take as input the length of first array.
4. Compare the two lengths.
5.Now if length of second array is less than length of first array then copy all elements of first array into second array till the required length and leave out the rest of the elements.
6. Conversely if length of first array is less than length of second array then copy all elements of first array into second array and ask user to enter the rest of the elements.
7. Print both arrays.
// Program starts here
import java.util.Scanner;
public class ArrayTest
{
public static void main( String argv[] )
{
Scanner keyboard = new Scanner(System.in);
System.out.print( "How large do you want the array? " );
int[] theArray = new int[keyboard.nextInt()]; // Take as input the length of first array.
// Take as input the elements of first array.
for( int i=0 ; i<theArray.length ; i++ )
{
System.out.print( "Enter value " + (i+1) + ": " );
theArray[i] = keyboard.nextInt();
}
// We Tackle the second array henceforth
Scanner keyboard2 = new Scanner(System.in);
System.out.print( "How large do you want the 2nd array? " );
int[] theArray2 = new int[keyboard2.nextInt()]; // Take as input the length of second array.
// Compare length of two arrays
if(theArray2.length<theArray.length) // If the second array is smaller
for( int i=0 ; i<theArray2.length ; i++ )
{
theArray2[i] = theArray[i]; // We copy the elements of array 1 to array 2 and leave out the rest
}
else // // In case the second array is larger
{
for( int i=theArray.length ; i<theArray2.length ; i++ )
{
System.out.print( "Enter value " + (i+1) + ": " ); // Ask user to enter the rest of the elements
theArray[i] = keyboard.nextInt();
}
// Print the elements of first array
System.out.println ( "All the values in the array 1 are: " );
for ( int i = 0; i < theArray.length; i++ )
System.out.println (theArray[i] );
// Print the elements of second array
System.out.println ( "All the values in the array 2 are: " );
for ( int i = 0; i < theArray2.length; i++ )
System.out.println ( theArray2[i] );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.