Write a program in Java that multiplies the column vector A [n] bysome number. The matrix and number are initiated by a random value generator.


Joined
May 28, 2022
Messages
2
Reaction score
1
Write a program in Java that multiplies the column vector A [n] by
some number. The matrix and number are initiated by a random value generator.
 
Ad

Advertisements

Joined
Mar 28, 2022
Messages
50
Reaction score
6
Multiplying a vector by a scalar
Java:
import java.util.Random;
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
    // Random number generator
    Random rng = new Random();

    // Get vector dimensions
    Scanner input = new Scanner(System.in);
    System.out.print("\nHow many dimensions in vector A[n]? ");
    int dimensions = input.nextInt();

    // fill vector with random values
    double[] vector = new double[dimensions];
    for(int index = 0; index < dimensions; index++){
      vector[index] = rng.nextDouble();
    }

    displayVector(vector);

    // get multiplier
    System.out.print("What shall we multiply the vector by? ");
    double scalar = input.nextFloat();

    // multiply each component by scaling factor
    for(int index = 0; index < dimensions; index++){
      vector[index] *= scalar;
    }

    displayVector(vector);
      
    input.close(); // prevent resource leak
  } // main

  // display vector
  static void displayVector(double[] vector) {
    String output = "v[";
    for(double dimension : vector){
      output += String.format(" %4.4f", dimension);
    }
    System.out.println(output + " ]");
  }
} // Main
 
Last edited:
Ad

Advertisements

Joined
May 28, 2022
Messages
2
Reaction score
1
Multiplying a vector by a scalar
Java:
import java.util.Random;
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
    // Random number generator
    Random rng = new Random();

    // Get vector dimensions
    Scanner input = new Scanner(System.in);
    System.out.print("\nHow many dimensions in vector A[n]? ");
    int dimensions = input.nextInt();

    // fill vector with random values
    double[] vector = new double[dimensions];
    for(int index = 0; index < dimensions; index++){
      vector[index] = rng.nextDouble();
    }

    displayVector(vector);

    // get multiplier
    System.out.print("What shall we multiply the vector by? ");
    double scalar = input.nextFloat();

    // multiply each component by scaling factor
    for(int index = 0; index < dimensions; index++){
      vector[index] *= scalar;
    }

    displayVector(vector);
     
    input.close(); // prevent resource leak
  } // main

  // display vector
  static void displayVector(double[] vector) {
    String output = "v[";
    for(double dimension : vector){
      output += String.format(" %4.4f", dimension);
    }
    System.out.println(output + " ]");
  }
} // Main
Thanks a lot. I send you the love of the whole world
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top