Question

Instructions Write a C++ program that performs the following: Accept a list of floating point values...

Instructions

Write a C++ program that performs the following:

  1. Accept a list of floating point values on a single line (separated by spaces). This is vector A. The user may enter as many values as they desire.
  2. Accept a second list of floating point values on a single line (separated by spaces). This is vector B. The user may enter as many values as they desire. There is no requirement that the two vectors have the same number of elements.
  3. Display the norm of each vector.
  4. If the vectors have the same number of elements, then
    1. Display the inner product of A and B
    2. Display the outer product of A and B

Design Notes

There is no need to use big integers for this assignment. The elements of the vectors and matrices can be standard C++ floating point data (float, double, or long double).

You should use an "extensible" data structure since you do not know how many elements the user might enter on a line, and therefore can not pre-allocate storage. Vectors or trees are a good choice. Arrays can be used, but are somewhat more complicated because they can not increase or decrease in size once they are allocated.

Example

The following example program was completed on the syccuxas01 server. Your output does not need to look exactly like this - this is just an example. The input used when grading may be different than what is shown here.

-- syccuxas01 > main.exe
Enter vector elements (float, space separated): 1 2 3
Enter vector elements (float, space separated): 4 5 6
Vector: <1,2,3>
Norm: 3.74166

Vector: <4,5,6>
Norm: 8.77496

Inner Product: 32
Outer Product:
Matrix:
      4.00       5.00       6.00
      8.00      10.00      12.00
     12.00      15.00      18.00

-- syccuxas01 >

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code For Above Problem:


#include <iostream>
#include <sstream>
#include <vector>
#include <math.h> 
#include<bits/stdc++.h> 
using namespace std;

//function to calculate norm of vector
double calculateNorm(vector<float>vec)
{
    float sum=0;
    
    for(int i=0;i<vec.size();i++)
    {
        sum+=pow(vec[i],2);
    }
    return sqrt(sum);
}

//function to calculate inner product of vectorA and vectorB
float innerProduct(vector<float>vectorA,vector<float>vectorB)
{
        float x=0;
        for(int i=0;i<vectorA.size();i++)
        {
                x+=vectorA[i]*vectorB[i];
        }
}

int main()
{
    std::string line1;//to read  elements of vectorA
    std::string line2;//to read  elements of vectorB
    int number;
    std::vector<float> vectorA;//declaring vectorA
    std::vector<float> vectorB;//declaring vectorB

        //Asking user to enter values for vectorA space seperated
    std::cout << "Enter vector elements(float,space seperated): ";
    std::getline(std::cin, line1);
    
        //Asking user to enter values for vectorB space seperated
    std::cout << "Enter vector elements(float,space seperated): ";
    std::getline(std::cin, line2);
    
        //storing elements to vectorA from entered line1
    std::istringstream stream(line1);
    while (stream >> number)
        vectorA.push_back(number);
        
        //storing elements to vectorB from entered line2
    std::istringstream stream1(line2);
    while (stream1 >> number)
        vectorB.push_back(number);
        
        //printing vectorA
    cout<<"Vector: ";
    for(int i=0;i<vectorA.size();i++)
    {
        cout<<vectorA[i]<<" ";
    }
    
        //calculating norm of vectorA and print result
    double norm1=calculateNorm(vectorA);
    cout<<"\nNorm: "<<norm1<<endl;
    
    //printing vectorB
    cout<<"\nVector: ";
    for(int i=0;i<vectorB.size();i++)
    {
        cout<<vectorB[i]<<" ";
    }
        //calculating norm of vectorB and print result
    double norm2=calculateNorm(vectorB);
    cout<<"\nNorm: "<<norm2<<endl;

        //if both vector sizes are same
        if(vectorA.size()==vectorB.size())
        {       
                //calculate inner product of vectorA and vectorB
                float x=innerProduct(vectorA,vectorB);
                cout<<"\nInner product:"<<x<<endl;
                
                //calculating and printing outer product
                cout<<"\nOuter product:\nMatrix\n";
                for(int i=0;i<vectorA.size();i++)
                {
                        for(int j=0;j<vectorB.size();j++)
                        {
                                cout<< fixed << setprecision(2)<<vectorA[i]*vectorB[j]<<"\t";
                        }
                        cout<<endl;
                }
        }
    return 0;
}

Sample Run Input/Output Of Code:

Enter vector elements(float,space seperated): 1 2 3
Enter vector elements(float,space seperated): 4 5 6
Vector: 1 2 3 
Norm: 3.74166

Vector: 4 5 6 
Norm: 8.77496

Inner product:32

Outer product:
Matrix
4.00    5.00    6.00    
8.00    10.00   12.00   
12.00   15.00   18.00   

Images Of Code:

} 1 2 #include <iostream> 3 #include <sstream 4 #include <vector> 5 #include <math.h> 6 #include<bits/stdcht.h> 7 using names

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 //storing elements to vectors from entered line2 std:

Image Of Sample Run Input/Output:

Enter vector elements(float,space seperated): 1 2 3 Enter vector elements(float, space seperated): 4 5 6 Vector: 1 2 3 Norm:

Add a comment
Know the answer?
Add Answer to:
Instructions Write a C++ program that performs the following: Accept a list of floating point values...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming: Write a program that inputs two strings that represent floating-point values, convert the strings...

    C Programming: Write a program that inputs two strings that represent floating-point values, convert the strings to double values. Calculate the sum,difference,and product of these values and print them. int main(){    // character string value array for user    char stringValue[10];       double sum=0.0;// initialize sum to store the results from 2 double values       double difference=0.0; // initialize difference to store the results from 2 double values       double product = 0.0; // intitialzie product...

  • USING C# 1. Write a program that takes outputs a string, an integer and a floating-point number s...

    USING C# 1. Write a program that takes outputs a string, an integer and a floating-point number separated by commas. Sample output: Bob Marley, 20, 5.2 2. 2. Write a program that asks the user for a string of letters of any size (no spaces), and finally a special character (values 33 to 47 in the Ascii table, or ‘!’ to ‘/’). Generate a random number of any size, integer or floating point, and combine those three pieces of information...

  • Programming in C: Write a program that accepts an integer and two floating-point values (All three...

    Programming in C: Write a program that accepts an integer and two floating-point values (All three in the same line and separated by a comma) and prints the sum of these values. Example: Input: 12,13.14,6.7; Output: 31.84. Use formatted I/O

  • In Python 3, Write a program that reads a set of floating-point values. Ask the user...

    In Python 3, Write a program that reads a set of floating-point values. Ask the user to enter the values, then print: The number of values entered. The smallest of the values The largest of the values. The average of the values. The range, that is the difference between the smallest and largest values. If no values were entered, the program should display “No values were entered” The program execution should look like (note: the bold values are sample user...

  • Create a program (in C, not C++) called lab3.c that declares the following variables and displays...

    Create a program (in C, not C++) called lab3.c that declares the following variables and displays their values: Declare a character variable with value 'a', and display the character using printf with %c format. Then add a second printf that displays the character with a %d format. Declare a short int variable with a value set to the maximum value of a short int (the maximum value for whatever machine I happen to use to grade your assignment - so...

  • Write a program that lets the user enter 10 values into an array. The program should...

    Write a program that lets the user enter 10 values into an array. The program should then display the largest and the smallest values stored in the array. The program should display the following message to the user at the beginning "This program will ask you to enter ten values. Then it will determine the largest and smallest of the values you entered.Please enter 10 integers separated by spaces: " And then after user entered the numbers, it should display...

  • C++ Suppose a user is prompted to enter four floating point number values. Write a C++...

    C++ Suppose a user is prompted to enter four floating point number values. Write a C++ program that outputs the number of input values, the number of negative values, and the number of even values.

  • Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std;...

    Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std; /* * Calculate the square of a number */ float square (float x) { return x*x; } /* * Calculate the average from a list of floating point numbers */ float average(vector<float>& values) { float sum = 0.0f; float average; for (float x : values) sum += x; average = sum / values.size(); return average; } /** Calculate the standard deviation from a vector...

  • c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers...

    c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers into an array of capacity 100. The program will then ask for one of three letters(A, M or S). if the user inputs something other than one of these letters, then the program should ask for that input until an A, M, or S is entered. A do-while loop should be used for this. If the user inputs an A, then the program should...

  • Assignment Input from the user 9, 64-bit, floating-point values as a 3x3, row-major, multi-dimensional array. This...

    Assignment Input from the user 9, 64-bit, floating-point values as a 3x3, row-major, multi-dimensional array. This will be the 3x3 matrix. Then, input 3, 64-bit, floating-point values. This will be a single-dimensional array and is the vector. Your program will simply ask the user to enter matrix values. Remember, there are 9 of these, but they need to be stored into a 3x3 multi-dimensional array (row-major). Then, your program will ask for the vector values, which will be stored into...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT