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
7
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.
 
Joined
Mar 28, 2022
Messages
82
Reaction score
11
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:
Joined
May 28, 2022
Messages
7
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

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,728
Latest member
FernMcmull

Latest Threads

Top