In: Computer Science
Write a program that creates three vector objects IN C++. Fill the first two objects with 25 floating-point numbers using a for loop as follows: 1. fill the first vector object with the loop counter value; 2. fill the second vector object with the loop counter value squared; 3. finally, write a for loop that adds the corresponding elements in the first two vectors, and puts the result in the corresponding element of the third vector. Display all three vectors using the format “for counter; element + element = element”.
#include <iostream>
#include<vector>
using namespace std;
int main()
{
vector<float> vectorOne(25);
vector<float> vectorTwo(25);
vector<float> vectorThree(25);
//1. fill the first vector object with the loop counter
value
for(int i=0; i<vectorOne.size(); i++)
{
vectorOne.at(i)=i;
}
//2. fill the second vector object with the loop counter value
squared
for(int i=0; i<vectorOne.size(); i++)
{
vectorTwo.at(i)=i*i;
}
//3. finally, write a for loop that adds the corresponding elements
in the first two vectors, and puts the result in the
//corresponding element of the third vector
for(int i=0; i<vectorOne.size(); i++)
{
vectorThree.at(i)=vectorOne.at(i)+vectorTwo.at(i);
}
//Display all three vectors using the format “for counter; element
+ element = element”.
for(int i=0; i<vectorOne.size(); i++)
{
cout<<"FOR COUNTER "<<i<<":
"<<vectorOne.at(i)<<"+"<<vectorTwo.at(i)<<"
= "<<vectorThree.at(i)<<endl;
}
return 0;
}
OUTPUT: