473,383 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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
2 2253
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Atip Asvanund | last post by:
Dear sirs, I am trying to learn how to use Boehm's garbage collector: http://www.hpl.hp.com/personal/Hans_Boehm/gc/ on a Linux machine. I am a beginner, and I find its documentation inadequate....
4
by: N3TB1N | last post by:
Here is my assignment. I am hoping that someone here quickly knows all of the correct answers... especially for question #5 and everything after. Thanks in advance. ...
6
by: Last Timer | last post by:
I need help with the following questions: a) if a class has virtual functions, is it better to make the destructor virtual b) can a friend function be inherited c) if a function argument is...
4
by: Yoram Biberman | last post by:
I have a few questions concerning concurrency control. I shall thank whoever can help me. Question #1 ========= Assume the following (concurrent) schedule, in which both transactions run in a...
6
by: xfile | last post by:
Hello, I am very new to donet and wondering how to solve the following scenario: (1) Our current hosted site has .Net 1.1 and can be upgraded to 2.0 if needed. Some downtime are expected and...
10
by: See_Red_Run | last post by:
Hi, I am trying to figure out how to get started with PHP/MySQL. Everything I've read so far says to start with PHP first. I was expecting something like Visual Basic Express or some other type...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.