how do vectors work

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 852 Views - Last Post: 31 October 2016 - 08:55 PM Rate Topic: -----

#1 Firecharmlily  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 03-October 16

how do vectors work

Posted 28 October 2016 - 05:52 PM

#pragma once
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

class Item
{
private:
	vector<int> id;
	vector<string> description;
	vector<int> quantity_ordered;
	vector<int> quantity_sold;
	vector<double> item_cost;
	vector<double> sales_prices;

public:
	Item();
	~Item();

	void getinfo();
	friend bool operator== (const Item &c1, const Item &c2);
	friend bool operator!= (const Item &c1, const Item &c2);

	friend ostream& operator<< (ostream&, Item &);
	friend istream& operator >> (istream&, Item &);

	friend bool operator> (const Item &c1, const Item &c2);
	friend bool operator<= (const Item &c1, const Item &c2);

	friend bool operator< (const Item &c1, const Item &c2);
	friend bool operator>= (const Item &c1, const Item &c2);

	void bubbleSort(vector<int> id, vector<string> description, 
		vector<int> quantity_ordered, vector<int> quantity_sold,
		vector<double> item_cost, vector<double> sales_prices);

	int totalProfit(int quantity_sold[], double item_cost[], double sales_prices[], int items);
};



#include "Item.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;


Item::Item()
{
}


Item::~Item()
{
}

void Item::getInfo()
{
	cin >> id >> description >> quantity_ordered >> quantity_sold >> item_cost >> sales_prices;
}

bool operator== (const Item &c1, const Item &c2)
{
	return (c1.id == c2.id &&
		c1.id == c2.id);
}

bool operator!= (Item &c1, Item &c2)
{
	return !(c1 == c2);
}

ostream& operator<<(ostream& osObject, Item& mObject) //output
{
	cout << left;
	osObject << setw(6) << mObject.id << mObject.description << mObject.quantity_ordered << mObject.quantity_sold 
		<< mObject.item_cost << mObject.sales_prices;

	return osObject;
}

istream& operator >> (istream& isObject, Item& mObject)     //input
{
	isObject >> mObject.id >> mObject.description >> mObject.quantity_ordered >> mObject.quantity_sold
		>> mObject.item_cost >> mObject.sales_prices;

	return isObject;
}

bool operator>(const Item & c1, const Item & c2)
{
	return c1.id > c2.id;
}

bool operator>= (const Item &c1, const Item &c2)
{
	return c1.id >= c2.id;
}

bool operator< (const Item &c1, const Item &c2)
{
	return c1.id < c2.id;
}

bool operator<= (const Item &c1, const Item &c2)
{
	return c1.id <= c2.id;
}

void Item::bubbleSort(vector<int> id, vector<string> description, 
	vector<int> quantity_ordered, vector<int> quantity_sold,
	vector<double> item_cost, vector<double> sales_prices)
{
	vector<int> temp1;
	vector<string> temp;
	vector<int> temp2;
	vector<int> temp3;
	vector<double> temp4;
	vector<double> temp5;

	for (vector<int>::iterator it = id.begin(); it != id.end(); ++it)
	{
		for (vector<int>::iterator it = id.begin(); it != id.end(); ++it)
		{
			if (id > id)
			{
				temp1 = id;
				id = id;
				id = temp1;

				temp = description;
				description = description;
				description = temp;

				temp2 = quantity_ordered;
				quantity_ordered = quantity_ordered;
				quantity_ordered = temp2;

				temp3 = quantity_sold;
				quantity_sold = quantity_sold;
				quantity_sold = temp3;

				temp4 = item_cost;
				item_cost = item_cost;
				item_cost = temp4;

				temp5 = sales_prices;
				sales_prices = sales_prices;
				sales_prices = temp5;

			}
		}
	}
}

int Item::totalProfit(int quantity_sold[], double item_cost[], double sales_prices[], int items)
{
	double profit;

	for (int i = 0; i < items; i++)
		profit = quantity_sold[i] * (sales_prices[i] - item_cost[i]);

	return profit;
}



#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include "Item.h"

using namespace std;

const int MAX = 5;



int main()
{
	Item id;

	id.getinfo();
	do
	{
		cout << "Item info? ";
		cin >> id;
		cout << endl;

	} while (id != -1);

	id.bubbleSort(id);

	for (vector<int>::iterator it = items.begin(); it != items.end(); ++it)
	{
		cout << id;
	}

	cout << "Total Profit: " << endl;

	system("PAUSE");
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: how do vectors work

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3699
  • View blog
  • Posts: 13,369
  • Joined: 08-August 08

Re: how do vectors work

Posted 28 October 2016 - 06:56 PM

I have no idea what you're asking about the code you've posted so I'll just answer your title with this:
http://www.dreaminco...-using-vectors/
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1850
  • View blog
  • Posts: 6,646
  • Joined: 19-February 09

Re: how do vectors work

Posted 28 October 2016 - 09:37 PM

Parallel containers are not usually an advisable way to store data. A container of Items is advised.

So turn it upside down:
class Item 
{
  private:
    int    id;
    string description;

  //...

};

//...

vector<Item> item_list;



Was This Post Helpful? 2
  • +
  • -

#4 Firecharmlily  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 03-October 16

Re: how do vectors work

Posted 30 October 2016 - 07:00 PM

I changed up the code and it takes input and couts the array, but it doesn't sort well. How do I fix this?
#pragma once
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

class Item
{
private:
	int id;
	string description;
	int ordered;
	int sold;
	double cost;
	double price;

public:
	Item();
	~Item();

	void getinfo();
	friend bool operator== (const Item &c1, const int &c2)
	{
		return (c1.id == c2 && c1.id == c2);
	};

	friend bool operator!= (const Item &c1, const int &c2)
	{
		return !(c1.id == c2);
	};

	friend ostream& operator<< (ostream&, Item &);
	friend istream& operator >> (istream&, Item &);

	friend bool operator> (const Item &c1, const Item &c2)
	{
		return c1.id > c2.id;
	};

	friend bool operator<= (const Item &c1, const Item &c2)
	{
		return c1.id <= c2.id;
	};

	friend bool operator< (const Item &c1, const Item &c2)
	{
		return c1.id < c2.id;
	};

	friend bool operator>= (const Item &c1, const Item &c2)
	{
		return c1.id >= c2.id;
	};

	
};



#include "Item.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;


Item::Item()
{
}


Item::~Item()
{
}

void Item::getinfo()
{
	
}

ostream& operator<<(ostream& osObject, Item& mObject) //output
{
	cout << left;
	osObject << fixed << setprecision(2) << setw(6) << mObject.id << setw(6) << mObject.description << setw(6) << mObject.ordered << setw(6)
		<< mObject.sold << setw(6) << mObject.cost << setw(6) << mObject.price;

	return osObject;
}

istream& operator >> (istream& isObject, Item& mObject)     //input
{
	isObject >> mObject.id >> mObject.description >> mObject.ordered >> mObject.sold
		>> mObject.cost >> mObject.price;

	return isObject;
}



#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>

#include "Item.h"

using namespace std;

void bubbleSort(vector<Item> id);

//int totalProfit(vector<Item> id, double prof);

int main()
{
	vector<Item> ItemArray;
	Item things;
	double profit = 0;


	do
	{
		cout << "Item info? ";
		cin >> things;
		cout << endl;

		ItemArray.push_back(things);

	} while (things != -1);

	ItemArray.pop_back();

	bubbleSort(ItemArray);

	//totalProfit(ItemArray, profit);
	for (unsigned int i = 0; i < ItemArray.size(); i++)
	{
			cout << left;
			cout << endl << ItemArray[i] << endl;
	}

	cout << endl;

	system("PAUSE");
	return 0;
}

void bubbleSort(vector<Item> id)
{
	unsigned int index;
	unsigned int iteration;
	for (index = 0; index < id.size(); index++)
	{
		for (iteration = 0; iteration < (id.size() - index); iteration++)
		{
			if (id[index] > id[index + 1])
			{
				Item temp;

				temp = id[index];
				id[index] = id[index + 1];
				id[index + 1] = temp;

				cout << id[index] << endl;
			}
			else if (id[index] == -1)
			{

			}
		}
	}
}


/*int totalProfit(vector<Item> id, double prof)
{
	for (unsigned int i = 0; i < (id.size() - 1); i++)
		prof = id[i] * (id[i] - id[i]);

	return prof;
}*/


This post has been edited by Firecharmlily: 30 October 2016 - 07:50 PM

Was This Post Helpful? 0
  • +
  • -

#5 Firecharmlily  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 03-October 16

Re: how do vectors work

Posted 30 October 2016 - 08:19 PM

anybody have advise for me?
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1850
  • View blog
  • Posts: 6,646
  • Joined: 19-February 09

Re: how do vectors work

Posted 30 October 2016 - 08:46 PM

Hi, what problems are you having?

In operator== it would seem that you would want to compare the id of the first Item with the id of the second Item.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: how do vectors work

Posted 31 October 2016 - 05:02 AM

It's a good idea to put some prints in your code to see what's going on. I'm going to put some more.
// what name kind of name is id for a vector?
// this is wrong, should be vector<Item> &id
void bubbleSort(vector<Item> id) {
    // these should be in the loops
    unsigned int index;
    unsigned int iteration;
    cout << "bubbleSort: size=" << id.size() << endl;
    for (index = 0; index < id.size(); index++) {
        cout << "  index=" << index << endl;
        for (iteration = 0; iteration < (id.size() - index); iteration++) {
            cout << "    iteration=" << iteration << endl;
            if (id[index] > id[index + 1]) {
                Item temp = id[index];
                id[index] = id[index + 1];
                id[index + 1] = temp;

                cout << "      swapped: " << id[index] << endl;
            // wtf is this?!?
            } else if (id[index] == -1) {

            }
        }
    }
    cout << "done, yay" << endl;
}

So, some sample output: [code]
sorting
bubbleSort: size=3
  index=0
    iteration=0
    iteration=1
    iteration=2
  index=1
    iteration=0
    iteration=1
  index=2
    iteration=0
!!! infinite loop, crash, panic, whatever



What's going on? You're trying to look at indexes 2 and 3 when your max index is 2. You can fix this, if you think about it. However, you still won't find any sorting.

First, this isn't really a bubble sort. Actually, at the moment, it isn't really any kind of sort. I'm rather fond of bubble sorts and did a whole writeup on them if you're curious: http://www.dreaminco...al-bubble-sort/

For the style of faux bubble sort you're aiming for, the output one would hope to see would look like this:
bubbleSort: size=5
  index=0
    iteration=0
    iteration=1
    iteration=2
    iteration=3
  index=0
    iteration=0
    iteration=1
    iteration=2
  index=0
    iteration=0
    iteration=1
  index=0
    iteration=0



Note, I changed size to 5 so you can more easily see the pattern. Why this pattern? Consider the sort:
a = [ 3 2 9 1 5 ], size = 5
i=0, cmp 3 2 [ 2 3 9 1 5 ] swap
i=1, cmp 3 9 [ 2 3 9 1 5 ]
i=2, cmp 9 1 [ 2 3 1 9 5 ] swap
i=3, cmp 9 5 [ 2 3 1 5 9 ] swap
size = 4
i=0, cmp 2 3 [ 2 3 1 5 9 ]
i=1, cmp 3 1 [ 2 1 3 5 9 ] swap
i=2, cmp 3 5 [ 2 1 3 5 9 ]
size = 3
i=0, cmp 2 1 [ 1 2 3 5 9 ] swap
i=1, cmp 2 3 [ 1 2 3 5 9 ]
size = 2
i=0, cmp 1 2 [ 1 2 3 5 9 ] 
size = 1
sorted



Hope this helps.
Was This Post Helpful? 2
  • +
  • -

#8 Firecharmlily  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 03-October 16

Re: how do vectors work

Posted 31 October 2016 - 02:16 PM

that helped a lot.
Was This Post Helpful? 0
  • +
  • -

#9 Firecharmlily  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 03-October 16

Re: how do vectors work

Posted 31 October 2016 - 03:56 PM

I got it to sort, but now I have a question. As you can see with my code, when i call things, it takes in multiple elements in one go, and puts it into a vector. How do I specifically call on one of those elements in main?

#pragma once
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

class Item
{
private:
	int id;
	string description;
	int ordered;
	int sold;
	double cost;
	double price;
	
public:
	Item();
	~Item();

	double profit(vector<Item>&);

	friend bool operator== (const Item &c1, const int &c2)
	{
		return (c1.id == c2 && c1.id == c2);
	};

	friend bool operator!= (const Item &c1, const int &c2)
	{
		return !(c1.id == c2);
	};

	friend bool operator== (const Item &c1, const Item &c2)
	{
		return (c1.id == c2.id && c1.id == c2.id);
	};

	friend bool operator!= (const Item &c1, const Item &c2)
	{
		return !(c1.id == c2.id);
	};

	friend ostream& operator<< (ostream&, const Item &);
	friend istream& operator >> (istream&, Item &);

	friend bool operator> (const Item &c1, const Item &c2)
	{
		return c1.id > c2.id;
	};

	friend bool operator>= (const Item &c1, const Item &c2)
	{
		return c1.id >= c2.id;
	};

	friend bool operator<= (const Item &c1, const Item &c2)
	{
		return c1.id <= c2.id;
	};

	friend bool operator< (const Item &c1, const Item &c2)
	{
		return c1.id < c2.id;
	};
};



#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

#include "Item.h"

using namespace std;

Item::Item()
{
}


Item::~Item()
{
}

double Item::profit(vector<Item> &length)
{
	double *profit;
	int max = length;

	for (int i = 1; i < length; i++)
		profit[i] = (sold * (price - cost)) + profit[i-1];

	return profit[max];
}

ostream& operator<<(ostream& osObject, const Item& mObject) //output
{
	cout << left;
	osObject << fixed << setprecision(2) << setw(6) << mObject.id << setw(6) << mObject.description << setw(6) << mObject.ordered << setw(6)
		<< mObject.sold << setw(6) << mObject.cost << setw(6) << mObject.price;

	return osObject;
}

istream& operator >> (istream& isObject, Item& mObject)     //input
{
	isObject >> mObject.id >> mObject.description >> mObject.ordered >> mObject.sold
		>> mObject.cost >> mObject.price;

	return isObject;
}



#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

#include "Item.h"

using namespace std;

void bubbleSort(vector<Item>&, int);

//int totalProfit(vector<Item> id, double prof);

int main()
{
	vector<Item> ItemArray;
	Item things;
	double profit = 0;

	try
	{
		do
		{
			cout << "Item info? ";
			cin >> things;
			cout << endl;

			ItemArray.push_back(things);

		} while (things != -1);

		ItemArray.pop_back();

		int length = 0;

		length = ItemArray.size();

		bubbleSort(ItemArray, length);
	}
	catch (...)
	{
		cout << "Error" << endl;
	}

	system("PAUSE");
	return 0;
}

void bubbleSort(vector<Item> &ItemArray, int length)
{
	Item temp;
	unsigned int index;
	unsigned int iteration;

	for (iteration = 1; iteration < length; iteration++)
	{
		for (index = 0; index < length - iteration; index++)
		{
			if (ItemArray[index] > ItemArray[index+1])
			{
				temp = ItemArray[index];
				ItemArray[index] = ItemArray[index+1];
				ItemArray[index+1] = temp;
			}
		}
	}

	for (unsigned int i = 0; i < ItemArray.size(); i++)
	{
		cout << left;
		cout << ItemArray[i] << endl;
	}

	things.profit = ()

	cout << "Total Profit: " << fixed << setprecision(2) << profit << endl;
}


Was This Post Helpful? 0
  • +
  • -

#10 Firecharmlily  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 03-October 16

Re: how do vectors work

Posted 31 October 2016 - 05:52 PM

help! this is due at midnight and it is this one tiny thing!

This post has been edited by Firecharmlily: 31 October 2016 - 06:45 PM

Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5825
  • View blog
  • Posts: 19,844
  • Joined: 05-May 12

Re: how do vectors work

Posted 31 October 2016 - 08:05 PM

You would access one element the same way you were accessing individual elements in your bubble sort.
Was This Post Helpful? 0
  • +
  • -

#12 Firecharmlily  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 03-October 16

Re: how do vectors work

Posted 31 October 2016 - 08:09 PM

so use an overload? would I then use an operator+ overload like this?

void totalProfit(vector<Item> &ItemArray, int length)
{
	double profit = 0;
	unsigned int index;
	unsigned int iteration;

	for (iteration = 1; iteration < length; iteration++)
	{
		for (index = 0; index < length - iteration; index++)
		{
			profit = ItemArray[index] + ItemArray[index+1];

		}
	}

	cout << "Total Profit: " << fixed << setprecision(2) << profit << endl;
}


Was This Post Helpful? 0
  • +
  • -

#13 Firecharmlily  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 03-October 16

Re: how do vectors work

Posted 31 October 2016 - 08:14 PM

friend bool operator+ (const Item &c1, const Item &c2)
	{
		return ((c1.sold * (c1.price - c1.cost)) + (c2.sold * (c2.price - c2.cost)));
	};


Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5825
  • View blog
  • Posts: 19,844
  • Joined: 05-May 12

Re: how do vectors work

Posted 31 October 2016 - 08:21 PM

Step back and let me ask you: Why does your Item::profit() take a vector of items as a parameter? Why not simply let each item compute its own profit and then then your main() or totalProfit() iterates over all the items in the vector and sums up all the profit?
Was This Post Helpful? 0
  • +
  • -

#15 Firecharmlily  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 03-October 16

Re: how do vectors work

Posted 31 October 2016 - 08:24 PM

how would i do that? my teacher never taught me how and the book doesn't help.

This post has been edited by Firecharmlily: 31 October 2016 - 08:24 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2