i need help.....

i need to write a SHORT java program that takes two arrays a and b of length n storing int values, and returns the dot product of a and b.

i have this code but not really made by me...can you help/guide me with making another one...??just clues and tips though..

public class product {

   public static void main(String args[])
   {
      int[] arrayA;
      int[] arrayB;
      int[] arrayC;
      int length;

      System.out.print("\n  Enter length of array: ");
      length = SavitchIn.readLineInt();

      arrayA = new int[length];
      arrayB = new int[length];
      arrayC = new int[length];

      System.out.print("\n  Enter " + length +"numbers for array A: (one per line)\n");

      for(int i=0; i<length; i++)
      {
         arrayA[i] = SavitchIn.readLineInt();
      }

      System.out.print("\n  Enter " + length +"numbers for array B: (one per line)\n");

      for(int i=0; i<length; i++)
      {
         arrayB[i] = SavitchIn.readLineInt();
      }


         for(int i=0; i<length; i++)
      {
         arrayC[i] = arrayA[i] * arrayB[i];
      }


      System.out.print("\t[A]\t[B]\t[C]\n");
      for(int i=0; i<length; i++)
      {
         System.out.println("\t" + arrayA[i] + "\t" + arrayB[i] + "\t" + arrayC[i]);
      }

   }
}

well storing arrays is easy.

int[] a = {4,5,6};
int[]b = {1,2,3};

but I supposed you have to do it some otherway.

Let's see.

I am still new, so I am not positive if this will work.

public class DotProduct {
	public static void main(String[] args) {
		int[] V1 = {1,2,3};
		int[] V2 = {4,5,6};
		System.out.println(V1[1]*V2[1] + V1[2]*V2[2] + V1[0]*V2[0]);
		

	}

}

So dot product is between two vectors, just plug in whatever values you want for the Vectors 1 and 2

This is a very easy problem I don't think you need help with this; but I'll help you any way

public class DotProduct {


	public static void main(String[] args) throws Exception {
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter the lenght of your arrays >> ");
		final int SIZE = input.nextInt();
		int[] a = new int[SIZE];
		int[] b = new int[SIZE];
		
		for(int i = 0; i < SIZE; i++){
			System.out.print("input a[" + i + "] >> ");
			a[i] = input.nextInt();
			System.out.print("\nEnter b[" + i + "] >> ");
			b[i] = input.nextInt();
		}
		
		System.out.print("Dot Product of " + printArray(a) + " and " + printArray(b)  + "is : ");
		System.out.println(getDotProduct(a, b));

	}
	
	public static int getDotProduct(int[] a, int[] b) throws Exception{
		int dotProduct = 0;
		
		if(a.length != b.length){ // make sure a and b have equal lengths
			throw new Exception("The two arrays must if equal lenght");
		}
		for(int i = 0; i < a.length; i++){
			dotProduct += a[i] * b[i];
		}
		
		return dotProduct;
	}
	
	public static String printArray(int[] a){
		String output = "[";
		for(int i = 0; i < a.length; i++){
			if(i == a.length-1){
				output += a[i] + "]";
				break;
			}
			output += a[i] + ",";
			
		}
		return output;
	}

}

After putting numbers in both tables and calculating the third table

Instead of this:

System.out.print("\t[A]\t[b]\t[C]\n");
for(int i=0; i<length; i++)
{
System.out.println("\t" + arrayA[i] + "\t" + arrayB[i] + "\t" + arrayC[i]);
}

You will need this:

int sum=0;
for(int i=0; i<length; i++)
{
sum = sum + arrayC[i];
}

Or without the third array:

int sum=0;
for(int i=0; i<length; i++)
{
sum = sum + arrayA[i]*arrayB[i];
}

I've updated the code it contains three methods
inputArray(int size);
printArray(int[] a);
getDotProduct(int[] a, int[] b)
read the documentation in the code to understand what each method does

public class DotProduct {

	public static void main(String[] args) throws Exception {
		
		System.out.print("Enter the lenght of your arrays >> ");
		final int SIZE = input.nextInt();
		
		System.out.println("Array a:");
		int[] a = inputArray(SIZE);
		System.out.println("Array b:");
		int[] b = inputArray(SIZE);
		
		System.out.print("Dot Product of " + printArray(a) + " and " + printArray(b)  + "is : ");
		System.out.println(getDotProduct(a, b));

	}
	
	/** calulate the Dot Product of two arrays */
	public static int getDotProduct(int[] a, int[] b) throws Exception{
		int dotProduct = 0;
		
		if(a.length != b.length){ // make sure a and b have equal lengths
			throw new Exception("The two arrays must have equal lenght");
		}
		for(int i = 0; i < a.length; i++){
			dotProduct += a[i] * b[i];
		}
		
		return dotProduct;
	}
	
	/** returns a string representation of an array	 */
	public static String printArray(int[] a){
		String output = "[";
		for(int i = 0; i < a.length; i++){
			if(i == a.length-1){
				output += a[i] + "]";
				break;
			}
			output += a[i] + ",";
			
		}
		return output;
	}
	
	/** returns an array generated from user input */
	public static int[] inputArray(int size){
		int[] a = new int[size];
		Scanner input = new Scanner(System.in);
		for(int i = 0; i < size; i++){
			System.out.print("input Array[" + i + "] >> ");
			a[i] = input.nextInt();
		}
		System.out.println();
		return a;
	}

}

to java addict:

have you tried maing a running program for this???

to ejosiah:

the code you posted,has 6 errors...ill just send you the errors later...but thanks anyway...

to java addict:

have you tried maing a running program for this???

What do you mean by that?

Also, I am not going to send the entire program like ejosiah did. You already have complete program as you posted on 2 Days Ago 12:52 3rd post. All you have to do is change where you calculate the result with my suggestion. Actually ejosiah told you the same algorithm (sum += a * b) but he wrote a new program version. Since you have the solution then use it to alter the code you have: 2 Days Ago 12:52 3rd post.

you should be able to debug the code yourself. If you want to get better at this you need to explore. Anywayz here is the correct code

import java.util.Scanner;
public class DotProduct {

	public static void main(String[] args) throws Exception {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the lenght of your arrays >> ");
		final int SIZE = input.nextInt();

		System.out.println("Array a:");
		int[] a = inputArray(SIZE);
		System.out.println("Array b:");
		int[] b = inputArray(SIZE);

		System.out.print("Dot Product of " + printArray(a) + " and " + printArray(b)  + "is : ");
		System.out.println(getDotProduct(a, b));

	}

	/** calulate the Dot Product of two arrays */
	public static int getDotProduct(int[] a, int[] b) throws Exception{
		int dotProduct = 0;

		if(a.length != b.length){ // make sure a and b have equal lengths
			throw new Exception("The two arrays must have equal lenght");
		}
		for(int i = 0; i < a.length; i++){
			dotProduct += a[i] * b[i];
		}

		return dotProduct;
	}

	/** returns a string representation of an array	 */
	public static String printArray(int[] a){
		String output = "[";
		for(int i = 0; i < a.length; i++){
			if(i == a.length-1){
				output += a[i] + "]";
				break;
			}
			output += a[i] + ",";

		}
		return output;
	}

	/** returns an array generated from user input */
	public static int[] inputArray(int size){
		int[] a = new int[size];
		Scanner input = new Scanner(System.in);
		for(int i = 0; i < size; i++){
			System.out.print("input Array[" + i + "] >> ");
			a[i] = input.nextInt();
		}
		System.out.println();
		return a;
	}

}

to java addict:

i have tried your suggestion..unfortunately,it doesnt display the third array or array C that will display the product of the contents of arrays A and B...

to java addict:

i have tried your suggestion..unfortunately,it doesnt display the third array or array C that will display the product of the contents of arrays A and B...

The result in not an array, but the sum of the A*B
So I told you that you can omit the third array and instead of doing this:


C[i] = A[i]*B[i];
//..........
sum+=C[i];

You can do this:

sum+=A[i]*B[i];

That is the result you will get from ejosiah 's code. He doesn't use a third array.

This question has already been answered. Start a new discussion instead.