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
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
ADVERTISEMENT