CPSC HW

profileMalikbinn

#1 [4 points]

Part A: Working with float vector using Array

A class FloatVectArray has been declared and needs to be implemented using an array of type float to set its elements.  First, complete the class by finishing the code for the default and copy constructors,  the assignment operator and the destructor. Then use the main function to test it.

#include <iostream>

using namespace std;

class FloatVectArray {

public:

 FloatVectArray(int vsize = 10); // default constructor

 FloatVectArray(float vlist[], int arraysize); // constructor with init parameters

 FloatVectArray(const FloatVectArray& fva);  // copy constructor

 FloatVectArray& operator = (const FloatVectArray& fva); // assignment operator

 ~FloatVectArray();   // destructor

 void printElements();  // prints all elements in the vector

private:

float *vector;  // the vector

int vectorsize; // size of the vector 

};

// default constructor

FloatVectArray::FloatVectArray(int vsize)  {

// To be completed 

}

// constructor with initialization parameters

FloatVectArray::FloatVectArray(float vlist[], int arraysize) {

// To be completed 

}

// copy constructor

FloatVectArray::FloatVectArray(const FloatVectArray& fva) {

To be completed 

}

// assignment operator

FloatVectArray& FloatVectArray::operator = (const FloatVectArray &fva) {

// To be completed 

}

// destructor

FloatVectArray::~FloatVectArray() {

// To be completed 

}

void FloatVectArray::printElements() {

// To be completed 

}

int main() {

// testing the implementation of a float vector using array

// declare & initialize an array of floats and create a float vector aVect 

float FloatArray1[10] = {2.1, 7.2, 3.3, 6.4, 1.5, 43.6,72.7,19.8,39.9, 71.1};

FloatVectArray aVect(FloatArray1, 10);

cout << "Printing all elements in aVect:" << endl;

aVect.printElements();

// declare & initialize another array of floats and create float vector bVect

float FloatArray2[10] = {1.1, 2.2, 5.3, 6.4, 1.5, 7.6, 9.7,17.8, 92.9, 42.1};

FloatVectArray bVect(FloatArray2, 10);

cout << "Printing all elements in bVect:" << endl;

bVect.printElements();

// try the assignment constructor

bVect = aVect;

cout << "Assigned bVect = aVect. Printing now all elements in bVect:" <<endl;

bVect.printElements();

// try the copy constructor

FloatVectArray cVect = aVect;

cout << "cVect is a copy of aVect. Printing all elements in cVect:" << endl;

cVect.printElements();

return 0;

}

  1. Turn in the complete code, including all of the above “to be completed” sections.
  2. Test by running the main() function above and capture the console screen output, by attaching it as an captured image (use CTRL+PrtSc) or printed out as a file.

Part B: Working with integer vector using Singly-Linked List    [6 points]

Study the sample codes for Singly-Linked List from the post at URLs:

https://github.com/apanangadan/CSUF-CPSC_131/blob/master/SLinkedList.h

https://github.com/apanangadan/CSUF-CPSC_131/blob/master/SLinkedList_main.cpp

  1. Create a constructor that allows a new linked list to be populated with ten consecutive values, starting at 0 (the values are 0, 1, 2, … 9)
  2. Create a printElements() function to print out all elements in the singly-linked list without deleting any of its elements.

Test run the codes. Capture and submit the screenshots by attaching it as an captured image (use CTRL+PrtSc) or printed out as a file.

    • 9 years ago
    • 10
    Answer(1)

    Purchase the answer to view it

    blurred-text
    NOT RATED
    • attachment
      PartA-WorkingwithfloatvectorusingArray.docx