Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7795841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:11:12+00:00 2026-06-01T23:11:12+00:00

Write a method static int [][] pr (int [] v) that returns a 2D

  • 0

Write a method static int [][] pr (int [] v) that returns a 2D array consisting of all the pairwise products
of the elements of v. E.g., for a = {{2, 3, 1} the method returns 2D array b = {{4, 6, 2}, {6, 9, 3}, {2, 3, 1}}. Please note: This is an 2010 exam question, taken from Software and Programming 1 module.

public class ETenBTwoD
{
    public static int [] [] pr (int [] v)
    {
        int [] a = {2, 3, 1};
        int [][] b = {{0, 0, 0},{0, 0, 0}, {0, 0, 0}};
        for (int i = 0; i<a.length; i++)
        {
            a[i] = a[i] * a[i];
        }
        return a;
    }
}

I am not sure how to do this. Help will be appreciated. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:11:13+00:00Added an answer on June 1, 2026 at 11:11 pm

    The key is to use a nested loop. As you can see v[i] takes the first number (choosen by the first loop) and multiplies it by all the numbers in v (parameter array) since j has to increment the full length of v. The first loop then picks out the second number and the second loop does it again. In b[i][j] the i represents the current number we are calculating pairwise products for.

    public static void main(String[] args){
        int[] v = {5,4,3,2,1};
        pr(v);}
        public static int [] [] pr (int [] v)
        {
            int [][] b =new int[v.length][v.length] ;
            for (int i = 0; i<v.length; i++)
            {
                for(int j=0; j<v.length;j++){
                    b[i][j] = v[i] * v[j];
                }
            }
    
            System.out.println(b[0][0]+" "+b[0][1]+" "+b[0][2]);
            System.out.println(b[1][0]+" "+b[1][1]+" "+b[1][2]);
            System.out.println(b[2][0]+" "+b[2][1]+" "+b[2][2]);
            return b;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the assignment: Write a method that sorts the elements of a matrix
Good evening all, I'm trying to write a method that creates and returns a
I'm trying to write a method like this: public static T Test<T>() { if
I wrote a method that extracts fields from an object like this: private static
i write a method that loops through an array of buttons and checks if
I want to write a method that when supplied an array of ints will
I tried to write generic function that remove the duplicate elements from array. public
Write a method called wordCount that accepts a String as its parameter and returns
I want to write a method that when supplied an array of ints will
Let's say I have the following method: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.