943,723 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1675
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 15th, 2007
0

Interger Arrays---Need Help!

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alexasmith is offline Offline
13 posts
since Oct 2007
Nov 15th, 2007
0

Re: Interger Arrays---Need Help!

What error are you getting? Or is it just a logic error somewhere? Nothing really stands out, but if you can describe how it isn't working I might be able to find something...
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Nov 15th, 2007
0

Re: Interger Arrays---Need Help!

Also, can I suggest putting a few extra println statements in while you are trying to debug your program. Try something like
java Syntax (Toggle Plain Text)
  1. num = scan.nextInt();
  2. 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
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Nov 15th, 2007
0

Re: Interger Arrays---Need Help!

well after the user enters the elements of that they want in the array(intergers 1-100) The prompt comes several times and then an error message comes up saying that it is outbounds---
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alexasmith is offline Offline
13 posts
since Oct 2007
Nov 15th, 2007
0

Re: Interger Arrays---Need Help!

Are you sure you have compiled it after recent changes? You program worked just fine for me. I was able to enter the number of elements that I had set ARRAY1 to hold with no errors.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 15th, 2007
0

Re: Interger Arrays---Need Help!

yeah but it doesn't print out the array when i tried to trouble shoot it...and when i tried to add the two arrays it also did not work
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alexasmith is offline Offline
13 posts
since Oct 2007
Nov 15th, 2007
0

Re: Interger Arrays---Need Help!

Well, the code you posted does not have anything written to do either of those things. If you are having trouble with an updated version of it, you'll need to post that with your question. We can't discuss what we can't see very well.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 15th, 2007
0

Re: Interger Arrays---Need Help!

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alexasmith is offline Offline
13 posts
since Oct 2007
Nov 15th, 2007
0

Re: Interger Arrays---Need Help!

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 element" + (index+1) + ": ");
array[index] = scan.nextInt();
}
}//end class

Try something like this.....cheers
Reputation Points: 10
Solved Threads: 1
Newbie Poster
VirusTalker is offline Offline
6 posts
since Nov 2007
Nov 16th, 2007
0

Re: Interger Arrays---Need Help!

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
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Suggest me good books for Swing and JDBC ?
Next Thread in Java Forum Timeline: Bubble Sort in Linked List- Help Greatly Appreciated





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC