Question

CISP 400 C++ Programming    Please note, you are required to include the following when you...

CISP 400 C++ Programming   

Please note, you are required to include the following when you use g++ to compile: -pedantic

           For example:

                                    g++ -pedantic filename.cpp

This will give a warning if you attempt to use any non-standard feature in g++. You must correct your code so that you do not receive any warnings of this type.

  • Log on to the Linux server.
  • Create four files:
    • Vector.h
    • Vector.cpp
    • VectorMain.cpp
    • Makefile
  • In Vector.h:

//Student header here

#ifndef VECTOR_H_DEF   //DEF: These are my initials replace with yours

#define VECTOR_H_DEF //DEF: These are my initials replace with yours

#include <iostream>

#include <cmath> //if needed

using namespace std;

class Vector

{

double _x;

double _y;

double _z;

public:

Vector();

Vector(double X, double Y, double Z = 0.0);

void display() const; // <_x, _y, _z>

  void add(const Vector&);

void sub(const Vector&);

void mult(const double&);

void div(const double&);

void normalize();

double length()const;

};

#endif

In Vector.cpp, implement these member functions.

In VectorMain.cpp, write a test main to test your code.

Submit these four files via the drop-box

Definitions:

<x1, y1, z1> + <x2, y2, z2> = <x1+x2, y1+y2, z1+z2>

<x1, y1, z1> - <x2, y2, z2> = <x1-x2, y1-y2, z1-z2>

<x, y, z> * d = <x*d, y*d, z*d>

<x, y, z> / d = <x/d, y/d, z/d>

|<x, y, z>| = Sqrt(x2 + y2 + z2) |v|: the length of v

normalize(v) = v/|v|

Can someone help me with this? I made the files 4 files

Homework Answers

Answer #1

// Vector.h

#ifndef VECTOR_H_DEF   //DEF: These are my initials replace with yours

#define VECTOR_H_DEF //DEF: These are my initials replace with yours

#include <iostream>

#include <cmath> //if needed

using namespace std;

class Vector

{

double _x;

double _y;

double _z;

public:

Vector();

Vector(double X, double Y, double Z = 0.0);

void display() const; // <_x, _y, _z>

void add(const Vector&);

void sub(const Vector&);

void mult(const double&);

void div(const double&);

void normalize();

double length()const;

};

#endif

//end of Vector.h

// Vector.cpp

#include "Vector.h"

Vector::Vector()

{}

Vector::Vector(double X, double Y, double Z ): _x(X), _y(Y), _z(Z)

{}

void Vector:: display() const // <_x, _y, _z>

{

       cout<<"<"<<_x<<", "<<_y<<", "<<_z<<">";

}

void Vector:: add(const Vector &v)

{

       _x += v._x;

       _y += v._y;

       _z += v._z;

}

void Vector:: sub(const Vector &v)

{

       _x -= v._x;

       _y -= v._y;

       _z -= v._z;

}

void Vector::mult(const double &d)

{

       _x *= d;

       _y *= d;

       _z *= d;

}

void Vector::div(const double &d)

{

       if(d > 0) // avoid division by 0

       {

             _x /= d;

             _y /= d;

             _z /= d;

       }

}

void Vector:: normalize()

{

       div(length());

}

double Vector::length()const

{

       return(sqrt(pow(_x,2)+pow(_y,2)+pow(_z,2)));

}

//end of Vector.cpp

// VectorMain.cpp : C++ driver program to test the Vector class

#include "Vector.h"

#include <iostream>

using namespace std;

int main()

{

       Vector v1(2,3,4), v2(3,4,5), v3(5,1);

       cout<<"V1 : ";

       v1.display();

       cout<<" V2 : ";

       v2.display();

       cout<<" V3 : ";

       v3.display();

       cout<<endl<<"Length of V1 : "<<v1.length()<<endl;

       v1.normalize();

       cout<<"After normalize , V1 : ";

       v1.display();

       cout<<endl;

       v1.add(v2);

       cout<<"After V1 + V2, V1 : ";

       v1.display();

       cout<<endl;

       v2.sub(v3);

       cout<<"After V2 - V3, V2: ";

       v2.display();

       cout<<endl;

       v3.mult(5.2);

       cout<<"After V3 * 5.2, V3 : ";

       v3.display();

       return 0;

}

//end of VectorMain.cpp

Output:

Know the answer?
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
Similar Questions
Determine if completeness and transitivity are satisfied for the following preferences defined on x = (x1,...
Determine if completeness and transitivity are satisfied for the following preferences defined on x = (x1, x2) and y = (y1, y2). (Hints: 1- You have to use z = (z1, z2) to prove or disprove transitivity. 2- You can disprove by a counter example) — x ≽y iff x1 > y1 or x1 = y1 and x2 > y2.
Determine if completeness and transitivity are satisfied for the following preferences defined on x = (x1,...
Determine if completeness and transitivity are satisfied for the following preferences defined on x = (x1, x2) and y = (y1, y2). (Hints: 1- You have to use z = (z1, z2) to prove or disprove transitivity. 2- You can disprove by a counter example) — x ≽y iff x1 > y1 or x1 = y1 and x2 > y2.
Determine if completeness and transitivity are satisfied for the following preferences defined on x = (x1,...
Determine if completeness and transitivity are satisfied for the following preferences defined on x = (x1, x2) and y = (y1, y2). (Hints: 1- You have to use z = (z1, z2) to prove or disprove transitivity. 2- You can disprove by a counter example) — x ≽y iff x1 > y1 or x1 = y1 and x2 > y2.
Determine if completeness and transitivity are satisfied for the following preferences defined on x = (x1,...
Determine if completeness and transitivity are satisfied for the following preferences defined on x = (x1, x2) and y = (y1, y2). x ≽ y iff x1 > y1 or x1 = y1 and x2 > y2. (Hints: 1- You have to use z = (z1, z2) to prove or disprove transitivity. 2- You can disprove by a counter example)
Please answer the following C question: Read the files vec5C.h, vec5C.c, and main5C.h. Build an executable...
Please answer the following C question: Read the files vec5C.h, vec5C.c, and main5C.h. Build an executable using gcc -Wall main5C.c vec5C.c Do this: Add function definitions to vec5C.c so that functions are available for dot product, sum, and cross product. Do not change the function prototypes in vec5C.h. Then add code to main to call these functions and display the dot product of u and v, the sum of u and v, and the cross product of u and v....
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I feel like I know how to write that. What I'm having trouble with is implementing two files the professer gave us. I would appreicate any help in understanding their purpose as in if Im supposed to take information from those files or give it information. Thank you! I have attatched the homework instructions and the two files given. Implementation The main program, called calculator.cpp...
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream> using namespace std; void shownumbers(int, int); int main() { int x, y; cout << "Enter first number : "; cin >> x; cout << "Enter second number : "; cin >> y; shownumbers(x, y); return 0; } void shownumbers(int a, int b) { bool flag; for (int i = a + 1; i <= b; i++) { flag = false; for (int j =...
R Code Directions: All work has to be your own, you may not work in groups....
R Code Directions: All work has to be your own, you may not work in groups. Show all work. Submit your solutions in a pdf document on Moodle. Include your R code (which must be commented and properly indented) in the pdf file. Name this pdf file ‘your last name’-HW5.pdf. Also submit one text file with your R code, which must be commented and properly indented. You may only use ‘runif’ to generate random numbers; other random number generating functions...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads data from a file and performs regression analysis using polyfit and polyval. The function shall have the following features: The input arguments shall include the file name (string), a vector of integers for the degrees of polynomial fits to be determined, and an optional plot type specifier (‘m’ for multiple plots, ‘s’ for a single plot - default). The data files will be text...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT