| | |
Interger Arrays---Need Help!
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 13
Reputation:
Solved Threads: 0
Hey! I am new to java programing and need some help with some homework, here are the requirements:
Right now I am working on just setting up my arrays but it is not working. I read the size of the array from the user and then tell the user to enter the elements of the array. I am very confused and frustrated!! Please help if you can, thanks for all the advice in advance.
The assignment
Write a program that will provide methods for manipulating arrays.
Write static methods within your main program class to do the following.
Assume A, B are arrays of int.
ReadArray (A) - reads int values from std input into the array A.
PrintArray (A) - printlns the elements of A to std output 8 per line.
int Sum (A) - returns the sum of the elements in array A.
int[] AddArrays (A, B) - returns a new array where each element is the sum
of the corresponding elements in A and B.
int DotProduct (A, B) - returns the dot product a1*b1 + a2*b2 + ...
The methods should handle arrays of any length. AddArray and DotProduct
should handle two arrays of different length by treating the shorter array as
if it were padded with zeros out to the length of the longer array.
The main method should prompt the user to enter two arrays and their values
(prompt for number of elements, create array, call Readarray). The program
should then print each array and its sum, add the two arrays, print the sum
of this new array, and print the dot product of the two original arrays.
import java.util.Scanner;
import java.io.*;
public class AKL
{
//---------------------------------------------------------------------------
// Method main
//
//---------------------------------------------------------------------------
public static void main(String[] args) throws IOException
{
int num =0;
int[] myInts1;
int[] myInts2;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the size of ARRAY 1 : ");
num = scan.nextInt();
myInts1 = new int[num];
System.out.println("Please enter the size of ARRAY 2 : ");
int num1 = scan.nextInt();
myInts2 = new int[num1];
readarray(myInts1);
} // ends main
public static void readarray(int []array)
{
Scanner scan = new Scanner(System.in);
for (int index = 0; index < array.length; index ++)
{
System.out.println("Please enter the elements for ARRAY1 : ");
array[index] = scan.nextInt();
}
}
}//end class
Right now I am working on just setting up my arrays but it is not working. I read the size of the array from the user and then tell the user to enter the elements of the array. I am very confused and frustrated!! Please help if you can, thanks for all the advice in advance.
The assignment
Write a program that will provide methods for manipulating arrays.
Write static methods within your main program class to do the following.
Assume A, B are arrays of int.
ReadArray (A) - reads int values from std input into the array A.
PrintArray (A) - printlns the elements of A to std output 8 per line.
int Sum (A) - returns the sum of the elements in array A.
int[] AddArrays (A, B) - returns a new array where each element is the sum
of the corresponding elements in A and B.
int DotProduct (A, B) - returns the dot product a1*b1 + a2*b2 + ...
The methods should handle arrays of any length. AddArray and DotProduct
should handle two arrays of different length by treating the shorter array as
if it were padded with zeros out to the length of the longer array.
The main method should prompt the user to enter two arrays and their values
(prompt for number of elements, create array, call Readarray). The program
should then print each array and its sum, add the two arrays, print the sum
of this new array, and print the dot product of the two original arrays.
import java.util.Scanner;
import java.io.*;
public class AKL
{
//---------------------------------------------------------------------------
// Method main
//
//---------------------------------------------------------------------------
public static void main(String[] args) throws IOException
{
int num =0;
int[] myInts1;
int[] myInts2;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the size of ARRAY 1 : ");
num = scan.nextInt();
myInts1 = new int[num];
System.out.println("Please enter the size of ARRAY 2 : ");
int num1 = scan.nextInt();
myInts2 = new int[num1];
readarray(myInts1);
} // ends main
public static void readarray(int []array)
{
Scanner scan = new Scanner(System.in);
for (int index = 0; index < array.length; index ++)
{
System.out.println("Please enter the elements for ARRAY1 : ");
array[index] = scan.nextInt();
}
}
}//end class
Also, can I suggest putting a few extra println statements in while you are trying to debug your program. Try something like
This will confirm whether you are reading the correct value. Put this sort of statement after every input and don't forget to display your output so you can see the result. This will help to decifer what is happening in your program. Just don't forget to comment them all out before handing in your final assignment!
Cheers,
darkagn
java Syntax (Toggle Plain Text)
num = scan.nextInt(); System.out.println("### num = "+num+(" ###");
This will confirm whether you are reading the correct value. Put this sort of statement after every input and don't forget to display your output so you can see the result. This will help to decifer what is happening in your program. Just don't forget to comment them all out before handing in your final assignment!
Cheers,
darkagn
•
•
Join Date: Oct 2007
Posts: 13
Reputation:
Solved Threads: 0
sorry i thought I had given you the correct one but this is the one I am working on---
import java.util.Scanner;
import java.io.*;
public class AKL
{
//---------------------------------------------------------------------------
// Method main Class NKOp8
//
//---------------------------------------------------------------------------
public static void main(String[] args) throws IOException
{
int num =0;
int[] myInts1;
int[] myInts2;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the size of ARRAY 1 : ");
num = scan.nextInt();
myInts1 = new int[num];
System.out.println("Please enter the size of ARRAY 2 : ");
int num1 = scan.nextInt();
myInts2 = new int[num1];
ReadArray(myInts1);
} // ends main
public static void ReadArray(int []array)
//-------------------------------------------------------------------------
// Read Array Method
//
//-------------------------------------------------------------------------
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the elements for ARRAY1 : ");
for (int index = 0; index < myInts1.length; index ++)
{
myInts1[index] = scan.nextInt();
}
for (int index = 0; index < myInts1.length; index ++)
{
myInts2[index] = scan.nextInt();
}
}//end of ReadArray
}//end class
import java.util.Scanner;
import java.io.*;
public class AKL
{
//---------------------------------------------------------------------------
// Method main Class NKOp8
//
//---------------------------------------------------------------------------
public static void main(String[] args) throws IOException
{
int num =0;
int[] myInts1;
int[] myInts2;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the size of ARRAY 1 : ");
num = scan.nextInt();
myInts1 = new int[num];
System.out.println("Please enter the size of ARRAY 2 : ");
int num1 = scan.nextInt();
myInts2 = new int[num1];
ReadArray(myInts1);
} // ends main
public static void ReadArray(int []array)
//-------------------------------------------------------------------------
// Read Array Method
//
//-------------------------------------------------------------------------
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the elements for ARRAY1 : ");
for (int index = 0; index < myInts1.length; index ++)
{
myInts1[index] = scan.nextInt();
}
for (int index = 0; index < myInts1.length; index ++)
{
myInts2[index] = scan.nextInt();
}
}//end of ReadArray
}//end class
Hi alexasmith,
The problem you were having has to do with scope. Basically you declare your variable myInts1 in your main method and pass it as a parameter to your readArray method (this is all correct). However, inside your readArray method, the name myInts1 is not defined - it is outside the scope of this method. Inside your readArray method it is called array (which is the name it is given as the parameter). In order to perform calculations on myInts2, you need a second call to your readArray method passing myInts2 to it. This is also called array inside the readArray method - basically the readArray method can perform calculations on one array at a time (the array that gets passed in when the method is called).
I hope this has explained your problem further.
Enjoy!
darkagn
The problem you were having has to do with scope. Basically you declare your variable myInts1 in your main method and pass it as a parameter to your readArray method (this is all correct). However, inside your readArray method, the name myInts1 is not defined - it is outside the scope of this method. Inside your readArray method it is called array (which is the name it is given as the parameter). In order to perform calculations on myInts2, you need a second call to your readArray method passing myInts2 to it. This is also called array inside the readArray method - basically the readArray method can perform calculations on one array at a time (the array that gets passed in when the method is called).
I hope this has explained your problem further.
Enjoy!
darkagn
![]() |
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Arrays (C++)
- How to Return Multidimensional Arrays (C++)
- C file input/output 2D arrays. (C)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: Suggest me good books for Swing and JDBC ?
- Next Thread: Bubble Sort in Linked List- Help Greatly Appreciated
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui health html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list mac main map method methods mobile myregfun netbeans notdisplaying number online printf problem program project qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor






