13:05 ET Dow -154.48 at 10309.92, Nasdaq -37.61 at 2138.44, S&P -19.130 1 100001 0 1 0 1 1 0 1 0 00 0 1 1 1 0 1 100001 0 1 1 100001 0 1 100001 0 1 0 1 1 0 1 0 00 0 1 1 1 0 1 100001 0 1 1 100001 0 1 100001 0 1 0 1 1 0 1 0 00 0 1 1 1 0 1 100001 0 1 1 100001 0 1 100001 0 1 0 1 1 0 1 0 00 0 1 1 1 0 1 100001 0 1 1 100001 0 1 100001 0 1 0 1 1 0 1 0 00 0 1 1 1 0 1 100001 0 1 1 100001 0 1 100001 0 1 0 1 1 0 1 0 00 0 1 1 1 0 1 100001 0 1 1 100001 0 1 100001 0 1 0 1 1 0 1 0 00 0 1 1 1 0 1 100001 0 1 1 100001 0 1 100001 0 1 0 1 1 0 1 0 00 0 1 1 1 0 1 100001 0 1 1 100001 0 1 100001 0 1 0 1 1 0 1 0 00 0 1 1 1 0 1 100001 0 1 1 100001 0 1 100001 0 1 0 1 1 0 1 0 00 0 1 1 1 0 1 100001 0 1 1 100001 0 1 100001 0 1 0 1 1 0 1 0 00 0 1 1 1 0 1 100001 0 1 1 100001 0 1 100001 0 1 0 1 1 0 1 0 00 0 1 1 1 0 1 100001 0 1 1 100001 0 1 100001 0 1 0 1 1 0 1 0 00 0 1 1 1 0 1 100001 0 1 1 100001 0 1 100001 0 1 0 1 1 0 1 0 00 0 1 1 1 0 1 100001 0 1 1 100001 13:05 ET Dow -154.48 at 10309.92, Nasdaq -37.61 at 2138.44, S&P -19.1313:05 ET Dow -154.48 at 10309.92, Nasdaq -37.61 at 2138.44, S&P -19.13

.

.

Saturday, March 12, 2011

Use of a Vector class in C++ : Also, demonstration of Switch, Goto, Cin, Cout, Print, etc - In Progress

The code below is what's been constructed for the first part of an assignment: implementation of a bubble sort.

Vectors of objects (integers, double and floats) will ultimately be entered by a user in an unordered sequence.  The bubble sort will then sort the vector by ordering the vector's objects by size.

For example, if a user enters: 12, 55, 1, 5, 90; you would have (1, 5, 12, 55, 90) after the sort.

Another objective or aspect here is to calculate the amount of time (using Big-O) it takes to sort an unordered vector using different algorithms.

Incidentally, there is an infinite loop if you select 2 in the case structure.


// Ethan Castanon

#include <iostream>
using namespace std;
#include "Vector.txt"

int main()
{

// which type of object for vector: int, double, float
int typeV;
std::cout << "What type of Vector do you want to act on?" << endl;
cin >> typeV;
switch (typeV)
{
case 1 :
{
Vector<int> Vint(0);
bool added = false;
do
{

int another;
std::cout << "Do you want to enter a number? Type '0' for 'N' and '1' for 'Y'" << endl;
cin >> another;
int toAdd;
if (another ==1)
{std::cout << "Enter an integer and press enter." << endl;
cin >> toAdd;
Vint.push_back(toAdd);
added = true;
}
else
added = false;
}
while(added==true);
printList(Vint);
goto pass;
}
case 2 :
{
Vector<double> Vdouble(0);
bool added = false;
do
{

int another;
std::cout << "Do you want to enter a number? Type '0' for 'N' and '1' for 'Y'" << endl;
cin >> another;
double toAdd;
if (another ==1)
{std::cout << "Enter an integer and press enter." << endl;
std::cin >> toAdd;
Vdouble.push_back(toAdd);
added = true;
}
else
added = false;
}
while(added==true);
printList(Vdouble);
goto pass;
}
case 3 :
{
Vector<float> Vfloat(0);
bool added = false;
do
{

int another;
std::cout << "Do you want to enter a number? Type '0' for 'N' and '1' for 'Y'" << endl;
std::cin >> another;
float toAdd;
if (another ==1)
{std::cout << "Enter an integer and press enter." << endl;
cin >> toAdd;
Vfloat.push_back(toAdd);
added = true;
}
else
added = false;
}
while(added==true);
printList(Vfloat);
goto pass;
}

pass:

// Below here is code that is not written by me (Ethan Castanon)

Vector<int> V1(0);
for (int i = 0; i < 10; i++)
V1.push_back(i);
printList(V1);

V1.reserve(20);
for (int i = 10; i <20; i++)
V1.push_back(i);
printList(V1);
cout << endl;

Vector<float> V2(0);
for (int i = 0; i < 10; i++)
V2.push_back((float)i + .1f);
printList(V2);
system("PAUSE");
return 0;
}
}

=================================================
template <typename Object>
class Vector
{
public:
// constructors
explicit Vector (int intSize = 0): theSize(intSize), 
theCapacity(intSize + SPARE_CAPACITY)
{
objects = new Object[theCapacity];
}
Vector (const Vector &rhs):objects(NULL)
{
operator=(rhs);
}
~Vector()
{
delete [] objects; 
}
const Vector & operator= (const Vector & rhs)
{
if (this != &rhs)
{
delete [] objects;
theSize = rhs.size();
theCapacity = rhs.theCapacity;
objects = new Object[capacity()];
for (int k=0; k < size(); k++)
objects[k] =rhs.objects[k];
}
return *this;
}
void resize (int newSize)
{
if (newSize > theCapacity)
reserve(newSize * 2 + 1);
theSize = newSize;
}
void reserve (int newCapacity)
{
if (newCapacity < theSize)
return;
Object *oldArray = objects;
objects = new Object[newCapacity];
for (int k=0; k < theSize; k++)
objects[k] = oldArray[k];
theCapacity = newCapacity;
delete[] oldArray;
}
Object & operator[] (int index)
{
return objects [index];
}
const Object & operator[] (int idnex) const
{
return objects [index];
}
bool empty() const
{
return size() == 0; 
}
int size() const
{
return theSize;
}
int capacity() const
{
return theCapacity;
}
void push_back(const Object &x)
{
if (theSize == theCapacity)
reserve( 2* theCapacity + 1);
objects[theSize++] = x;
}
void pop_back()
{
theSize--;
}
const Object & back() const
{
return objects [theSize - 1];
}
typedef Object* iterator;
typedef const Object* const_iterator;

iterator begin()
{
return &objects[0]; 
}
const_iterator begin() const
{
return &objects[0];
}
iterator end()
{
return &objects[size()];
}
const_iterator end() const
{
return &objects[size()];
}
enum {SPARE_CAPACITY = 16};
private:
int theSize;
int theCapacity;
Object* objects;
};
template <typename Container>
void printList (Container &vec)
{
for (Container::iterator iter = vec.begin(); 
iter != vec.end(); iter++)
cout << *iter << " ";
cout << endl;
}