Interger Arrays---Need Help!

Thread Solved

Join Date: Oct 2007
Posts: 13
Reputation: alexasmith is an unknown quantity at this point 
Solved Threads: 0
alexasmith alexasmith is offline Offline
Newbie Poster

Interger Arrays---Need Help!

 
0
  #1
Nov 15th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 791
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Interger Arrays---Need Help!

 
0
  #2
Nov 15th, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 791
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Interger Arrays---Need Help!

 
0
  #3
Nov 15th, 2007
Also, can I suggest putting a few extra println statements in while you are trying to debug your program. Try something like
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 13
Reputation: alexasmith is an unknown quantity at this point 
Solved Threads: 0
alexasmith alexasmith is offline Offline
Newbie Poster

Re: Interger Arrays---Need Help!

 
0
  #4
Nov 15th, 2007
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---
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Interger Arrays---Need Help!

 
0
  #5
Nov 15th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 13
Reputation: alexasmith is an unknown quantity at this point 
Solved Threads: 0
alexasmith alexasmith is offline Offline
Newbie Poster

Re: Interger Arrays---Need Help!

 
0
  #6
Nov 15th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Interger Arrays---Need Help!

 
0
  #7
Nov 15th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 13
Reputation: alexasmith is an unknown quantity at this point 
Solved Threads: 0
alexasmith alexasmith is offline Offline
Newbie Poster

Re: Interger Arrays---Need Help!

 
0
  #8
Nov 15th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: VirusTalker is an unknown quantity at this point 
Solved Threads: 1
VirusTalker VirusTalker is offline Offline
Newbie Poster

Re: Interger Arrays---Need Help!

 
0
  #9
Nov 15th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 791
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Interger Arrays---Need Help!

 
0
  #10
Nov 16th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC