Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Dropbox Link if image does\'t show: https://www.dropbox.com/s/5m7ud3q6euelt59/Un

ID: 3547927 • Letter: D

Question

Dropbox Link if image does't show: https://www.dropbox.com/s/5m7ud3q6euelt59/Untitled.jpg

Many engineering applications require "normalizing" an n - element vector V. Each element wi of the normalized vector W is defined as follow. Write a C++ program that defines a class Vector that allocates stack space for vectors of up to ten elements, and stores the actual vector size as a component of the class. Overload the >> operator to allow sentinel-terminated input of a vector. Also overload the

Explanation / Answer


#include <iostream>

#include<math.h>

using namespace std;



class Vector{

private:

double elems[10];

int size;

public:

friend ostream& operator<<(ostream&, Vector&);

friend istream& operator>>(istream&, Vector&);

static Vector normalize(Vector v_inp, Vector v_out)

{

double sum = 0;

int i;

for(i = 0; i< v_inp.size;i++)

{

sum+=(v_inp.elems[i])*(v_inp.elems[i]);

}

sum = sqrt(sum);

for(i = 0; i< v_inp.size;i++)

{

v_out.elems[i] = v_inp.elems[i]/sum;

}

v_out.size = v_inp.size;

return v_out;

}

  

};


ostream& operator<<(ostream& out, Vector& v){

out << "[";

for (int i = 0; i<v.size;i++)

{

out<<v.elems[i]<<" ";

}

out<<"] ";

return out;

}


istream& operator>>(istream& in, Vector& v){

int i;

for (i = 0;i<10;i++)

{

if (!(in>>v.elems[i]))

break;

}

v.size = i;

return in;

}


int main() {

Vector* v1 = new Vector();

cin>>*v1;

cout<<*v1;

Vector* v2 = new Vector();

*v2 = Vector::normalize(*v1,*v2);

cout<<*v2;

return 0;

}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote