Numbers
Hi all,
Please can you help me with following:
I want to change program so that user will choose the size of numberA and numberB, the numberA can be different to numberB
import java.util.*;
publicclass Numbers
{
static Scanner console =new Scanner(System.in);
staticfinalint ARRAY_SIZE=20;
publicstaticvoid main(String[] args)
{
int[] numberOne =newint[ARRAY_SIZE];
int[] numberTwo =newint[ARRAY_SIZE];
System.out.print("numberOne elements: ");
printArray(numberOne, numberOne.length);
System.out.println();
System.out.print("Enter " + numberOne.length +" integers: ");
fillArray(numberOne, numberOne.length);
System.out.println();
System.out.println("After filling "+"numberOne, the elements are:" +"\n");
printArray(numberOne, numberOne.length);
System.out.println("\n");
System.out.println("Sum of the "+"elements of listA is: "+sumArray(numberOne, numberOne.length)+"\n");
System.out.println("Location of "+"the largest element in "+"listA is: "+(indexLargestElement(numberOne,numberOne.length)+1)+"\n");
System.out.println("Largest element in "+"listA is: "+numberOne[indexLargestElement(numberOne, numberOne.length)]+"\n");
copyArray(numberOne, numberOne, numberOne.length);
System.out.print("Line 16: After copying the "+"elements of numberOne into listB\n"+"listB elements are: ");
printArray(numberOne, numberOne.length);
System.out.println();
}
//Method to input data and store in an array
publicstaticvoid fillArray(int[] list,int noOfElements)
{
int index;
for(index = 0; index < noOfElements; index++)
{
list[index] = console.nextInt();
}
}
//Method to print the array
publicstaticvoid printArray(int[] list,int noOfElements)
{
int index;
for(index = 0; index < noOfElements; index++)
System.out.print(list[index] +" ");
}
//Method to find and return the sum of an array
publicstaticint sumArray(int[] list,int noOfElements)
{
int index;
int sum = 0;
for(index = 0; index < noOfElements; index++)
sum = sum + list[index];
return sum;
}
//Method to find and return the index of the
//largest element of an array
publicstaticint indexLargestElement(int[] list,int noOfElements)
{
int index;
int maxIndex = 0;//Assume first element is the largest
for(index = 1; index < noOfElements; index++)
if(list[maxIndex] < list[index])
maxIndex = index;
return maxIndex;
}
//Method to copy one array into another array
publicstaticvoid copyArray(int[] list1,int[] list2,
int noOfElements)
{
int index;
for(index = 0; index < noOfElements; index++)
list2[index] = list1[index];
}
}
If I try following, program gets in endless loop:
import java.util.*;
publicclass Numers2
{
static Scanner console =new Scanner(System.in);
staticint[] numberOne;
publicstaticvoid main(String[] args)
{
int size = console.nextInt();
numberOne =newint[size];
int i;
for (i=0; i<numberOne.length; i++)
{
System.out.print("listA "+i+" = ");
numberOne[i] = console.nextInt();
}
System.out.print("Enter " + numberOne +" integers: ");
System.out.println();
}
}
>

