Answer to Question #26803 in C++ for veeray

Question #26803
Write a class to represent a vector (a series of float values). Include member functions to perform the following tasks: a. To create the vector b. To modify the value of a given element c. To multiply a scalar value d. To display the vector in the form (10,20,30, …) Write a program to test your class.
1
Expert's answer
2013-03-29T11:32:17-0400
#include <iostream>

using namespace std;

class vector
{
public:
& vector();
& vector(int size);
& vector(int size, float *a);
& ~vector();
& void set_value(int index, float value);
& void multiplay(float scalar);
& void display();
private:
& int itssize;
& float *itsvalue;
};

vector::vector()
{
itssize = 1;
itsvalue = new float [1];
itsvalue[0] = 0;
}

vector::vector(int size)
{
itssize = size;
itsvalue = new float [size];
for (int i = 0; i < size; i++)
& itsvalue[i] = 0;
}

vector::vector(int size, float *a)
{
itssize = size;
itsvalue = new float [size];
for (int i = 0; i < size; i++)
& itsvalue[i] = a[i];
}

vector::~vector()
{
delete itsvalue;
}

void vector::set_value(int index, float value)
{
itsvalue[index - 1] = value;
}

void vector::multiplay(float scalar)
{
for (int i = 0; i < itssize; i++)
& itsvalue[i] *= scalar;
}

void vector::display()
{
cout << "(";
for (int i = 0; i < itssize - 1; i++)
& cout << itsvalue[i] << ", ";
cout << itsvalue[itssize - 1] << ")" << endl;
}

int main()
{
cout << "Creating a NULL vector..." << endl;
vector a;
a.display();
cout << "Creating three-dimensional NULL vector..." << endl;
vector b(3);
b.display();
cout << "Seting second coordinate equal to 5..." << endl;
b.set_value(2, 5);
b.display();
cout << "Creating a four-dimensinal random vector..." << endl;
float *d = new float[4];
for (int i = 0; i < 4; i++)
& d[i] = float(rand()%(100)) / 10;
vector c(4, d);
c.display();
cout << "Multiplayin vector on scolar 2..." << endl;
c.multiplay(2);
c.display();
system("PAUSE");
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

echema samuel
25.05.17, 14:23

you are indeed an expert,u've saved me coz i was stack like as if i was in the mud my dear, thnx so very much and i'll always need u

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS