Vcoderz Community
We create websites that have it all, beauty & brains
Lebanon Web Design & Development - Coddict
 

Go Back   Vcoderz Community > Computer Zone > Computers & Information Technologies

Notices

Computers & Information Technologies Everything related to computers and internet.

Reply
 
Share Thread Tools Search this Thread
Old 01-12-2011   #21
IM@D
Registered Member
 
IM@D's Avatar
 
Last Online: 05-28-2013
Join Date: Jun 2006
Posts: 781
Thanks: 168
Thanked 303 Times in 187 Posts
Groans: 23
Groaned at 22 Times in 18 Posts
Default

#include <iostream>
#include <string>
using namespace std;

class Stock
{
int numofshares;
string ticker;
double buyprice,currentprice;

public:
Stock(int n,string s,double a,double b)
{ numofshares=n;
ticker=s;
buyprice=a;
currentprice=b;
}
void setnumofshares(int n)
{ numofshares=n;}
int getnumofshares()
{ return numofshares; }
void setticker(string s)
{ ticker=s; }
string getticker()
{ return ticker; }
void setbuyprice(double a)
{ buyprice=a; }
double getbuyprice()
{ return buyprice;}
void setcurrentprice(double b)
{ currentprice=b;}
double getcurrentprice()
{ return currentprice; }
};

class stockAccount
{
string name;
int ownedstocks=0;
Stock owned[100];

public:
stockAccount(string s)
{ name=s;}

void buy(int numShares, string ticker, double currentPrice)
{
int j=0;
for (int i=0;i<=ownedstocks;i++)
{
if (ticker==owned[i].getticker())
{ numShares=numShares + owned[i].getnumofshares();
owned[i].setnumofshares(numShares);
owned[i].setbuyprice(currentPrice);
owned[i].setcurrentprice(currentPrice);
owned[i].setticker(ticker);
j++;}
}

if (j==0)
{owned[ownedstocks].setnumofshares(numShares);
owned[ownedstocks].setcurrentprice(currentPrice);
owned[ownedstocks].setbuyprice(currentPrice);
owned[ownedstocks].setticker(ticker);
ownedstocks++;}
}

void updatePrice(string ticker, double newPrice)
{
for (int i=0;i<=ownedstocks;i++)
{
if (ticker==owned[i].getticker())
{owned[i].setbuyprice(owned[i].getcurrentprice());
owned[i].setcurrentprice(newPrice);}
}
}

double currentValue()
{
double S=0;
for (int i=0;i<=ownedstocks;i++)
S=S+owned[i].getcurrentprice();
return S;}

double currentProfit()
{
double S=0;
for (int i=0;i<=ownedstocks;i++)
{ double J=owned[i].getcurrentprice()-owned[i].getbuyprice();
S=S+J;}
return S;}

void printReport()
{
for (int i=0;i<=ownedstocks;i++)
{
cout<<name<<"has bought "<<owned[i].getnumofshares()<<" shares of the stock "<<owned[i].getticker()<<endl;
cout<<"The current price is "<<owned[i].getcurrentprice()<<" and the buy price was "<<owned[i].getbuyprice()<<endl;
}
cout<<" The current value of all this account is "<<currentValue()<<endl;
cout<<" The current profit made from this account is "<<currentProfit()<<endl;
}
};


void main()
{
stockAccount s("John Lucky");
s.buy(100,"GE",13.5);
s.buy(25,"GOOG",110.1);
s.buy(97,"IBM",23.3);
s.buy(250,"MSFT",2.5);
s.buy(200,"YHOO",11.8);
s.updatePrice("GE",45.5);
s.updatePrice("GOOG",700.1);
s.updatePrice("IBM",86.5);
s.updatePrice("MSFT",24.8);
cout<<"The current value of the account is: "<<s.currentValue()<<endl;
cout<<"The profit made from this account is: "<<s.currentProfit()<<endl;
s.printReport();
}

(39) : error C2864: 'stockAccount:wnedstocks' : only static const integral data members can be initialized within a class
(43) : error C2512: 'Stock' : no appropriate default constructor available

May someone helps me to understand these errors, please.


IM@D is offline   Reply With Quote
Old 01-13-2011   #22
Google

 
Google's Avatar
 
Last Online: 05-30-2013
Join Date: Jan 2008
Posts: 1,788
Thanks: 10,018
Thanked 1,100 Times in 651 Posts
Groans: 1
Groaned at 6 Times in 6 Posts
Default

You cannot initialize data member like that unless it is declared as static const. Or if you wanna do this, do it in the constructor. As for the second error, just add Stock() { }; and you will be fine.
__________________

Google is offline   Reply With Quote
Old 01-13-2011   #23
IM@D
Registered Member
 
IM@D's Avatar
 
Last Online: 05-28-2013
Join Date: Jun 2006
Posts: 781
Thanks: 168
Thanked 303 Times in 187 Posts
Groans: 23
Groaned at 22 Times in 18 Posts
Default

Thanks google it worked .
But i didn't understand the second error?! eno leh jabaroune a3moul default constructor in Stock w ma baddoun wa7ad bi StockAccount?
IM@D is offline   Reply With Quote
Old 02-23-2011   #24
Adam
Registered Member
 
Adam's Avatar
 
Last Online: 06-03-2013
Join Date: Feb 2008
Posts: 1,410
Thanks: 1,444
Thanked 1,235 Times in 689 Posts
Groans: 25
Groaned at 21 Times in 19 Posts
Default

I need help figuring out what sorting algorithm is being used on this array:

Code:
3, 45, 12, 7, 9, 16, 22, 55, 8
3, 7, 45, 12, 8, 9, 16, 22, 55
3, 7, 8, 45, 12, 9, 16, 22, 55
3, 7, 8, 9, 45, 12, 16, 22, 55
3, 7, 8, 9, 12, 45, 16, 22, 55
...
I've been at it for a while and it's driving me nuts.
Adam is offline   Reply With Quote
Old 02-23-2011   #25
Kingroudy
Super Moderator
 
Kingroudy's Avatar
 
Last Online: 08-01-2019
Join Date: May 2006
Posts: 5,580
Thanks: 1,888
Thanked 2,653 Times in 1,593 Posts
Groans: 55
Groaned at 35 Times in 32 Posts
Default

this algorithm is so raggedy ass.
you find out which is the smallest, you put it first.
you find out which is second smallest, you put it second, then, you need to shift (to the left) the elements that are between index 2 to index X-1 inclusive (where X is the original index of the second smallest value). After that, you check for the smallest value between index X+1 and the end of the array, and then put it at index X+1, shifting the elements that have index between X+1 and Y-1 to the left (where Y is the index of the smallest value between X+1 and the end).
and so on.
__________________
click on 'Groan' to switch to my left testicle.
Kingroudy is offline   Reply With Quote
Old 02-23-2011   #26
Adam
Registered Member
 
Adam's Avatar
 
Last Online: 06-03-2013
Join Date: Feb 2008
Posts: 1,410
Thanks: 1,444
Thanked 1,235 Times in 689 Posts
Groans: 25
Groaned at 21 Times in 19 Posts
Default

Yep, that's selection-sort you're describing. But if you notice in the first two lines, both 7 & 8 were moved in the same pass. Selection-sort wouldn't do that.
Adam is offline   Reply With Quote
Old 02-23-2011   #27
Tawa
 
Tawa's Avatar
 
Last Online: 06-12-2019
Join Date: Mar 2006
Posts: 6,245
Thanks: 2,120
Thanked 3,365 Times in 1,740 Posts
Groans: 29
Groaned at 44 Times in 35 Posts
Default

Quote:
Originally Posted by Adam View Post
I need help figuring out what sorting algorithm is being used on this array:

Code:
3, 45, 12, 7, 9, 16, 22, 55, 8
3, 7, 45, 12, 8, 9, 16, 22, 55
3, 7, 8, 45, 12, 9, 16, 22, 55
3, 7, 8, 9, 45, 12, 16, 22, 55
3, 7, 8, 9, 12, 45, 16, 22, 55
...
I've been at it for a while and it's driving me nuts.

Bubble Sort.
__________________
What we do in life, echoes in eternity.
Tawa is offline   Reply With Quote
The Following User Says Thank You to Tawa For This Useful Post:
Adam (02-24-2011)
Old 02-23-2011   #28
Kingroudy
Super Moderator
 
Kingroudy's Avatar
 
Last Online: 08-01-2019
Join Date: May 2006
Posts: 5,580
Thanks: 1,888
Thanked 2,653 Times in 1,593 Posts
Groans: 55
Groaned at 35 Times in 32 Posts
Default

Yeah it is the bubble sort, read about it on Wikipedia.
__________________
click on 'Groan' to switch to my left testicle.
Kingroudy is offline   Reply With Quote
The Following User Says Thank You to Kingroudy For This Useful Post:
Adam (02-24-2011)
Old 02-23-2011   #29
Tawa
 
Tawa's Avatar
 
Last Online: 06-12-2019
Join Date: Mar 2006
Posts: 6,245
Thanks: 2,120
Thanked 3,365 Times in 1,740 Posts
Groans: 29
Groaned at 44 Times in 35 Posts
Default

And this is how it works step by step:
Bold Black is a swap, Bold Red is not. The | character separates the sorted side from the unsorted side of the array.
-----------------------------------------------------------------
3, 45, 12, 7, 9, 16, 22, 55, 8
3, 45, 12, 7, 9, 16, 22, 8, 55
3, 45, 12, 7, 9, 16, 8, 22, 55
3, 45, 12, 7, 9, 8, 16, 22, 55
3, 45, 12, 7, 8, 9, 16, 22, 55
3, 45, 12, 7, 8, 9, 16, 22, 55
3, 45, 7, 12, 8, 9, 16, 22, 55
3, 7, 45, 12, 8, 9, 16, 22, 55 // This is the 2nd line you provided
3| 7, 45, 12, 8, 9, 16, 22, 55
3| 7, 45, 12, 8, 9, 16, 22, 55
3| 7, 45, 12, 8, 9, 16, 22, 55
3| 7, 45, 12, 8, 9, 16, 22, 55
3| 7, 45, 12, 8, 9, 16, 22, 55
3| 7, 45, 8, 12, 9, 16, 22, 55
3| 7, 8, 45, 12, 9, 16, 22, 55 // The 3rd line.
3, 7| 8, 45, 12, 9, 16, 22, 55
3, 7| 8, 45, 12, 9, 16, 22, 55
3, 7| 8, 45, 12, 9, 16, 22, 55
3, 7| 8, 45, 12, 9, 16, 22, 55
3, 7| 8, 45, 9, 12, 16, 22, 55
3, 7| 8, 9, 45, 12, 16, 22, 55 // The 4th line
3, 7, 8| 9, 45, 12, 16, 22, 55
3, 7, 8| 9, 45, 12, 16, 22, 55
3, 7, 8| 9, 45, 12, 16, 22, 55
3, 7, 8| 9, 45, 12, 16, 22, 55
3, 7, 8| 9, 12, 45, 16, 22, 55 // The 5th line
3, 7, 8, 9| 12, 45, 16, 22, 55
3, 7, 8, 9| 12, 45, 16, 22, 55
3, 7, 8, 9| 12, 45, 16, 22, 55
3, 7, 8, 9| 12, 16, 45, 22, 55 // Should've been the 6th
3, 7, 8, 9, 12| 16, 45, 22, 55
3, 7, 8, 9, 12| 16, 45, 22, 55
3, 7, 8, 9, 12| 16, 22, 45, 55 // 7th
3, 7, 8, 9, 12, 16| 22, 45, 55
3, 7, 8, 9, 12, 16| 22, 45, 55 // 8th
3, 7, 8, 9, 12, 16, 22| 45, 55 // 9th

3, 7, 8, 9, 12, 16, 22, 45, 55| // Last line
-----------------------------------------------------------------
__________________
What we do in life, echoes in eternity.
Tawa is offline   Reply With Quote
The Following User Says Thank You to Tawa For This Useful Post:
Adam (02-24-2011)
Reply

  Vcoderz Community > Computer Zone > Computers & Information Technologies

Tags
helpc



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:02 AM.


Lebanon web design and development
Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Share