Thread: Input multiple floats directly to array

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    135

    Input multiple floats directly to array

    Hey, so this term I'm taking C/C++ course, and I'm tasked with some exercises in C programming.

    Here is one:

    Write a program that reads in two vectors and calculates the inner product between them, as follows:
    1. Read in the length of the vectors (an integer followed by a newline).
    2. Read in the two vectors (floats separated by spaces, newline separating the vectors).
    3. The inner (scaler) product is the sum of the multiplication of each two elements:
      • If the vectors are [1, 0.5, 0] and [0.2, 1, 0.3] the inner product is: 1 * 0.2 + 0.5 * 1 + 0 * 0.3 = 0.7.

    4. Print the result up to 2 digits after the decimal point (hint: "%.2f").

    You may assume the input is valid, but you may not assume a maximum length.

    For example:
    Input Result
    3
    1 0.5 0
    0.2 1 0.3
    0.70
    0 0.00


    I thought first to input the lengths of both the vectors and then define the vectors in the suitable lengths (by pointers).

    But my problem is with the input of the coordinates - as it's in one row, so I don't know how I can insert efficiently and elegantly floats directly to the arrays.

    Does someone have an idea how can I input the coordinates correctly?
    Thank in advance!
    Last edited by HelpMeC; 11-30-2019 at 11:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pass array directly to new object
    By a.mlw.walker in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2013, 03:03 PM
  2. Replies: 3
    Last Post: 04-09-2010, 05:07 AM
  3. how to fill multiple array locations with one input
    By kryonik in forum C++ Programming
    Replies: 5
    Last Post: 06-08-2006, 10:12 PM
  4. Replies: 3
    Last Post: 04-30-2006, 06:01 AM
  5. Input stream directly to structure elements?
    By thenrkst in forum C++ Programming
    Replies: 1
    Last Post: 04-24-2003, 11:43 AM

Tags for this Thread