|
|
|||||||
| Computers & Information Technologies « Everything related to computers and internet. » |
![]() |
|
|
Share | Thread Tools | Search this Thread |
|
|
#21 |
|
Registered Member
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
|
#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. |
|
|
|
|
|
#22 |
|
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
|
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.
__________________
|
|
|
|
|
|
#23 |
|
Registered Member
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
|
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? |
|
|
|
|
|
#24 |
|
Registered Member
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
|
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 ... |
|
|
|
|
|
#25 |
|
Super Moderator
Last Online: 02-16-2022
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
|
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. |
|
|
|
|
|
#26 |
|
Registered Member
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
|
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.
|
|
|
|
|
|
#27 | |
|
Last Online: 12-20-2021
Join Date: Mar 2006
Posts: 6,245
Thanks: 2,121
Thanked 3,365 Times in 1,740 Posts
Groans: 29
Groaned at 44 Times in 35 Posts
|
Quote:
Bubble Sort.
__________________
What we do in life, echoes in eternity.
|
|
|
|
|
| The Following User Says Thank You to Tawa For This Useful Post: | Adam (02-24-2011) |
|
|
#28 |
|
Super Moderator
Last Online: 02-16-2022
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
|
Yeah it is the bubble sort, read about it on Wikipedia.
__________________
click on 'Groan' to switch to my left testicle. |
|
|
|
| The Following User Says Thank You to Kingroudy For This Useful Post: | Adam (02-24-2011) |
|
|
#29 |
|
Last Online: 12-20-2021
Join Date: Mar 2006
Posts: 6,245
Thanks: 2,121
Thanked 3,365 Times in 1,740 Posts
Groans: 29
Groaned at 44 Times in 35 Posts
|
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.
|
|
|
|
| The Following User Says Thank You to Tawa For This Useful Post: | Adam (02-24-2011) |
![]() |
|
| Tags |
| helpc |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|