Instructions
Write a C++ program that performs the following:
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 >
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:
Image Of Sample Run Input/Output:
Instructions Write a C++ program that performs the following: Accept a list of floating point values...
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 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...
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...
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; /* * 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 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 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...
Programming in C: Write a program that accepts an integer and two floating-point values and prints the sum of these values. Example: Input:12,13.14,6.7; Output:31.84. Use formatted I/O
in c++ Write a program which can add two float vectors (arrays) of any length ?. The length ? of the two vectors being added should be the same. The user will enter the length ?. Use the length to control how many times you loop. The result is also a vector.
Write a C++ program that creates a ragged table (i.e. a table with rows of different length) using a vector of vector of integers. Your program should prompt the user for a file name, open the file, read the content of the file and store it in a vector or vectors, and finally print the content of the vector of vectors. Sample Input: Enter the file name: data.dat A sample file might contain: 6 5 2 4 5 3 2...