By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,399 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

beginner c++ homework questions --=[let me try one more time]=--

P: n/a
Let me try again. I could use some help with this assignment, even
though my teacher does not grade assignments.but because I need to
know this stuff for a test very soon, but haven't been in class for
awhile and don't know what I'm doing.

I have included my (probably wrong) answers for the first few
questions. It would be great if someone could tell me what is missing
or what I need to work on at least just for the first few.

I understand that no one wants to do someone elses homework, so if you
don't want to give any pointers, I would be more than grateful if
anyone could direct me to a URL where I could learn about these topics
since I don't own a textbook.
thanks again for any help.

__________________________________________________ _________________
class Money

{

public:

friend istream& operator >> (istream& IS, Money& anAmount) ;

private:

int mDollars;

int mCents;

};

class ISPAccount

{

public:

friend istream& operator >> (istream& IS, Money& anISPAccount) ;

private:

string mUser ;

Money mBalance ;

int mOnline ;

};

template <class Thing>

class AccountList

{

public:

private:

vector <Thing> mSomeAccts ;

};

1) Write a line of code that declares an AccountList object of
ISPAccounts. Call the object theAccounts.
AccountList <ISPAccounts> theAccounts;

2) Write a public function in the AccountList class that returns the
index of an ISPAccount parameter. If the ISPAccount
is not in the mSomeAccts vector, return -1. Name the function
getIndex. Note that you will need to add some functionality to
the ISPAccount class.

class AccountList
{
public:
int getIndex(ISPAccount
target_parameter);
};

class ISPAccount
{
public:
friend bool operator == (const
ISPAccount & target_parameter, const ISPAccount & parameter);
};

int AccountList::getIndex(ISPAccount target_parameter)
{
while (

3) Write a function in the AccountList class that will return the
total of all the balances in an AccountList. Call the

function getTotalBal. Note that you will need to add some
functionality to the ISPAccount class and/or the Money class.

class AccountList
{
public:
double getTotalBal();
}

double AccountList::getTotalBal()
{

double total_balance = 0, temp_balance;
while (vector mSomeAccounts.mBalance >>
temp_balance)
{
total_balance += temp_balance;
}

return total_balance
{

4) Write a default constructor for the Money Class that initializes
a Money object to $0.

class Money
{
public:
money();
};

money::money()
{
mDollars = 0;
mCents = 0;
}
5) Overload the default constructor so that it takes two arguments,
the first for the number of dollars and the second for

the number of cents.

class BunchOfCards

{

public:

friend vector<Card> operator + (vector<Card> B1,
vector<Card> B2) ;

private:

vector <Card> mTheCards ;

// the “top” of the bunch is the last item on the Card on the
vector.

};

6) Write a member function that ‘splits’ a bunch of card in half. The
top half of the bunch is deleted from the bunch and

then returned. Consider the middle index of the bunch equal to
mTheCards.size() / 2. Here’s a sample function call:

{

BunchOfCards aBunch(“the_deck.txt”);

// creates and initializes a BunchOfCards from a file named
the_deck.txt

// that has 36 cards.

BunchOfCards anotherBunch ; // creates an empty BunchOfCards

anotherBunch = aBunch . topHalf() ;

// post condition: anotherBunch contains 18 cards taken off the

// top of aBunch. aBunch contains the 18 cards left on the bottom of
aBunch.

}

7a) Overload the + operator so that it adds two bunches of cards
together.

friend vector<Card> operator + (vector<Card> B2, vector<Card> B2) ;

7b) Rewrite the header making good style and efficient use of const
and &.

8) Write a member function of the BunchOfCards called cut that takes
the top half of a BunchOfCards object and puts it on

the bottom.

(Question 9 is not related to any of the classes above)

9) Write a nonmember, templated function that will reverse the order
of a vector of anything

vector new_vector;
for (int index_from_back = a_vector.size() - 1,
the_index_from_back >= 0, the_index_from_back--) ;
{
a_vector[the_index]
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
If you need help with a university course, college course or high school
course your first recourse should be your instructor, teaching assistants
and fellow students/peers - not a forum of experts who want to help other
serious programmers in their efforts to become better. The people here have
gone through all the stuff you're going through now. Part of your learning
experience should not be simply learning to code, but learning where to
access and how to utilise the resources available to you, while not wasting
the time of those who haven't got the time or inclination to allow you to
waste it. While I was doing my undergraduate degree I found most of my work
was done either in the lab at university EVERY day or home EVERY night. It's
not a simple thing to obtain a degree - you do have to work. Every day and
every night. If, for some reason you're unable to do this right now then you
should withdraw until you're better able to do so - if you are able to do
it, then you should be doing so. Otherwise you're simply wasting your time,
the time of your instructors and the seat space you use. What you are doing
by asking, 'please do my homework?' here is tantamount to asking a mountain
climber to come back down from miles above you to help you cross a creek -
it ain't reasonable and ain't gunna happen. Find someone nearby in the same
mess as you and muddle through it together.

good luck and regards,
L.

"N3TB1N" <45*********@60x.5K> wrote in message
news:fo********************************@4ax.com...
Let me try again. I could use some help with this assignment, even
though my teacher does not grade assignments.but because I need to
know this stuff for a test very soon, but haven't been in class for
awhile and don't know what I'm doing.

I have included my (probably wrong) answers for the first few
questions. It would be great if someone could tell me what is missing
or what I need to work on at least just for the first few.

I understand that no one wants to do someone elses homework, so if you
don't want to give any pointers, I would be more than grateful if
anyone could direct me to a URL where I could learn about these topics
since I don't own a textbook.
thanks again for any help.

__________________________________________________ _________________
class Money

{

public:

friend istream& operator >> (istream& IS, Money& anAmount) ;

private:

int mDollars;

int mCents;

};

class ISPAccount

{

public:

friend istream& operator >> (istream& IS, Money& anISPAccount) ;

private:

string mUser ;

Money mBalance ;

int mOnline ;

};

template <class Thing>

class AccountList

{

public:

private:

vector <Thing> mSomeAccts ;

};

1) Write a line of code that declares an AccountList object of
ISPAccounts. Call the object theAccounts.
AccountList <ISPAccounts> theAccounts;

2) Write a public function in the AccountList class that returns the
index of an ISPAccount parameter. If the ISPAccount
is not in the mSomeAccts vector, return -1. Name the function
getIndex. Note that you will need to add some functionality to
the ISPAccount class.

class AccountList
{
public:
int getIndex(ISPAccount
target_parameter);
};

class ISPAccount
{
public:
friend bool operator == (const
ISPAccount & target_parameter, const ISPAccount & parameter);
};

int AccountList::getIndex(ISPAccount target_parameter)
{
while (

3) Write a function in the AccountList class that will return the
total of all the balances in an AccountList. Call the

function getTotalBal. Note that you will need to add some
functionality to the ISPAccount class and/or the Money class.

class AccountList
{
public:
double getTotalBal();
}

double AccountList::getTotalBal()
{

double total_balance = 0, temp_balance;
while (vector mSomeAccounts.mBalance >>
temp_balance)
{
total_balance += temp_balance;
}

return total_balance
{

4) Write a default constructor for the Money Class that initializes
a Money object to $0.

class Money
{
public:
money();
};

money::money()
{
mDollars = 0;
mCents = 0;
}
5) Overload the default constructor so that it takes two arguments,
the first for the number of dollars and the second for

the number of cents.

class BunchOfCards

{

public:

friend vector<Card> operator + (vector<Card> B1,
vector<Card> B2) ;

private:

vector <Card> mTheCards ;

// the "top" of the bunch is the last item on the Card on the
vector.

};

6) Write a member function that 'splits' a bunch of card in half. The
top half of the bunch is deleted from the bunch and

then returned. Consider the middle index of the bunch equal to
mTheCards.size() / 2. Here's a sample function call:

{

BunchOfCards aBunch("the_deck.txt");

// creates and initializes a BunchOfCards from a file named
the_deck.txt

// that has 36 cards.

BunchOfCards anotherBunch ; // creates an empty BunchOfCards

anotherBunch = aBunch . topHalf() ;

// post condition: anotherBunch contains 18 cards taken off the

// top of aBunch. aBunch contains the 18 cards left on the bottom of
aBunch.

}

7a) Overload the + operator so that it adds two bunches of cards
together.

friend vector<Card> operator + (vector<Card> B2, vector<Card> B2) ;

7b) Rewrite the header making good style and efficient use of const
and &.

8) Write a member function of the BunchOfCards called cut that takes
the top half of a BunchOfCards object and puts it on

the bottom.

(Question 9 is not related to any of the classes above)

9) Write a nonmember, templated function that will reverse the order
of a vector of anything

vector new_vector;
for (int index_from_back = a_vector.size() - 1,
the_index_from_back >= 0, the_index_from_back--) ;
{
a_vector[the_index]

Jul 22 '05 #2

P: n/a
On Sun, 09 May 2004 06:55:56 -0400, N3TB1N <45*********@60x.5K> wrote:

[snip]

It is much better to post your own efforts, it shows willing.

I will comment on some of your answers. Do not assume that the
answers I don't comment on are correct. Do not assume that I have
commented on all the errors in the answers I do comment on. You still
have to do some thinking.

Some general guidance:
1 Write compilable code and actually compile it.
2 Write incrementally don't try to do it all in one big chunk.
Experienced people work this way, it is just that they can handle
bigger increments.
2.1 Get something simple working first. Test it so you are happy that
it works well.
2.2 When it is working add a bit more to it. Test it again until you
are happy.
2.3 Repeat until you have solved the whole problem.

rossum

3) Write a function in the AccountList class that will return the
total of all the balances in an AccountList. Call the
function getTotalBal. Note that you will need to add some
functionality to the ISPAccount class and/or the Money class.
template <class Thing>
AccountList is a templated class, you might need to refer to "Thing"
in your answer, don't leave it out here.class AccountList
{
public:
double getTotalBal();
private:
vector <Thing> mSomeAccts ;
Why are you leaving out part of the class? You need to know this for
your answer.} ^-- Missing semicolon
double AccountList::getTotalBal() Why are you using a double here? The question says "Note that you
will need to add some functionality to the ISPAccount class and/or the
Money class." To me that is a *big hint* that your instructor wants
something more like: Money AccountList::getTotalBal()
{
double total_balance = 0, temp_balance; ^-- use 0.0 to initialise a double while (vector mSomeAccounts.mBalance >> temp_balance) Oh dear. I think I can see what you are trying to do, but what you
have written does not do it. You start with "while" so you are trying
to loop - a good start. You seem to be trying to loop through each
element of the vector, but are probably not going about it correctly.

If I declare vector<Thing> my_thing_vector then if I want to refer to
the first Thing in the vector I could use my_thing_vector[0]. Start
by writing code to get at one single element of your vector and
storing the relevant value in a variable called temp_balance (which
may not be a double).

When you can do this for one element of the vector then think about
how to automate stepping through the vector picking up each element
once, and once only. You have the right idea with using a loop, but
you need a lot more work on the detail of the loop. Remember that
"while" is not the only type of loop.
{
total_balance += temp_balance;
} return total_balance
{ ^-- Typo!

4) Write a default constructor for the Money Class that initializes
a Money object to $0.

class Money
{
public:
money(); ^-- The class is called Money (leading capital). C++ is case
sensitive so this is an error.

Don't leave out the private: part of the Money class.
};

money::money() ^------^-- Same again, twice.{
mDollars = 0;
mCents = 0;
}
7a) Overload the + operator so that it adds two bunches of cards together. friend vector<Card> operator + (vector<Card> B2, vector<Card> B2) ; I suspect that the question had B1 here --^

9) Write a nonmember, templated function that will reverse the order
of a vector of anything
Have a good look at the AccountList class. Notice the first line that
goes: template <class Thing>. AccountList is a templated class. You
are asked to write a templated function. That means that somewhere in
your answer there will be a line like:
template <class Something>
vector new_vector; This is a syntax error - you don't say what type of thing the vector
is holding. Always write enough code so you can compile everything
you write, even if you don't submit all of it.
for (int index_from_back = a_vector.size() - 1, semicolon, not comma --^ the_index_from_back >= 0, the_index_from_back--) ; semicolon, not comma --^ aaarrrggghhh!--^
I assume you realise that the semicolon after the closing bracket of
the for-expression means that your loop does precisely nothing.
{
a_vector[the_index]


Start by writing a small complete compilable program that reverses a
simple vector, say vector<int>. When you have got that working
correctly then think about how to turn it into a general templated
vector reverser. Here is something to get you started:

#include <iostream>
#include <vector>

typedef std::vector<int> Simple_vector;

//---------------------------------------

// This is the function that will eventually be
// the answer to Q9
Simple_vector vector_reverse(const Simple_vector vec)
{
// Your code to reverse a vector of int goes here
return reversed_vec;
}

//---------------------------------------

void show_vector(std::ostream& os, const Simple_vector vec)
{
// Your code to output the contents of a vector goes here
return;
}

//---------------------------------------

int main()
{
Simple_vector my_vector;
my_vector.push_back(1);
my_vector.push_back(2);
my_vector.push_back(3);
my_vector.push_back(4);
my_vector.push_back(5);

// my_vector now looks like {1, 2, 3, 4, 5}
std::cout << "Before reversal the vector looks like:\n";
show_vector(std::cout, my_vector);
std::cout << std::endl;

Simple_vector rev_vector = vector_reverse(my_vector);

// rev_vector should look like {5, 4, 3, 2, 1}
std::cout << "After reversal the vector looks like:\n";
show_vector(std::cout, rev_vector);
std::cout << std::endl;

return 0;
}

--

The Ultimate Truth is that there is no Ultimate Truth
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.