Arrays?

G

gbvk

Hi,

I'm currently writing a C++ program for an assignment.

"Create a C++ program to manage 10 bank accounts... "

To include;

"An appropriate type definition to store the name, account number and
balance of a bank account
An array to record the details of the 10 bank accounts
Functions/procedures to update/interrogate the bank accounts array."

I have all the code for a single bank account where you can depost/
withdaw and vew balance, but, I’m not quite sure about arrays and how
to access them, so my idea was this:

float account [2][10];

// account munber on [0][x]
account[0][0] = 10001;
account[0][1] = 10002;
account[0][2] = 10003;
account[0][3] = 10004;
account[0][4] = 10005;
account[0][5] = 10006;
account[0][6] = 10007;
account[0][7] = 10008;
account[0][8] = 10009;
account[0][9] = 10010;

// account balance on [1][x]

account[1][0] = 0;
account[1][1] = 0;
account[1][2] = 0;
account[1][3] = 0;
account[1][4] = 0;
account[1][5] = 0;
account[1][6] = 0;
account[1][7] = 0;
account[1][8] = 0;
account[1][9] = 0;

But I have no idea how to include a name, or how I would start with
"insert account number" and get this to pull details.
 
G

gbvk

I'm currently writing a C++ program for an assignment.
"Create a C++ program to manage 10 bank accounts... "
To include;
"An appropriate type definition to store the name, account number and
balance of a bank account
An array to record the details of the 10 bank accounts
Functions/procedures to update/interrogate the bank accounts array."
I have all the code for a single bank account where you can depost/
withdaw and vew balance, but, I’m not quite sure about arrays and how
to access them, so my idea was this:
float account [2][10];
// account munber on [0][x]
account[0][0] = 10001;
account[0][1] = 10002;
account[0][2] = 10003;
account[0][3] = 10004;
account[0][4] = 10005;
account[0][5] = 10006;
account[0][6] = 10007;
account[0][7] = 10008;
account[0][8] = 10009;
account[0][9] = 10010;
// account balance on [1][x]
account[1][0] = 0;
account[1][1] = 0;
account[1][2] = 0;
account[1][3] = 0;
account[1][4] = 0;
account[1][5] = 0;
account[1][6] = 0;
account[1][7] = 0;
account[1][8] = 0;
account[1][9] = 0;
But I have no idea how to include a name, or how I would start with
"insert account number" and get this to pull details.

Consider defining a new *type* that would store all this information in
"one place". Something like

struct Account {
float balance; // if you think that 'float' is the right type
std::string owner; //
unsigned number; //
};

Or have you not gone over user-defined types?

V

Not to my knowledge :/

Arrays have been specified in the assignment brief
 
G

gbvk

even some direction to some online reading materials which would be
relevant to this would be more than appreciated.

not looking for specifics just some subtle guidance :)
 
C

Christian Hackl

even some direction to some online reading materials which would be
relevant to this would be more than appreciated.

not looking for specifics just some subtle guidance :)

The most important thing to know about arrays in C++ is that you should
avoid them. They're considered "evil", because their behaviour is
confusing, error-prone and inconsistent with the rest of the language.

std::vector is a safe, robust alternative.

http://www.cppreference.com/cppvector/index.html
http://www.parashift.com/c++-faq-lite/containers.html

I sincerely hope your teacher does not force you to use arrays rather
than std::vector! :)
 
O

osmium

even some direction to some online reading materials which would be
relevant to this would be more than appreciated.

not looking for specifics just some subtle guidance :)

You must start by learning about "struct" which is geek speak for what is
called in English, a "record". This is the bare minimum entry point to this
question. Details beyond that depend on the order of teaching things that
the instructor is using. If he said to use an array, use an array. If he
hasn't told you about the string *class*, don't use it. I am sure you can
see that the method you have started with is doomed to failure for a bank
with several thousand accounts. Wikipedia is often a great help is CSci
problems.

It is even possible that the instructor neglected to teach struct yet. Such
is life with real people.
 
O

osmium

"Victor Bazarov" write:

Well, if you didn't go through user-defined types (which is rather strange
because that's the bread-and-butter of C++), you could simply define an
array of ten account number, ten account balances, ten account names
(account holder names), ten whatever else you want or see relevant to your
task...

Gee, it's been so long since I did that that I completely forgot about the
approach. Self taught programmers sometimes use this technique, it is
called "parallel arrays". This went out with the Volkswagen beetle. (sp?)
 
C

Chris Gordon-Smith

Unfortunatly he is

Hmmm. I suppose any C++ programmer should be able to recognise arrays and
understand roughly how they work, but using them in this day and age seems
questionable to say the least. If you don't use the Standard Template
Library (including std::vector) then I think you'll be missing out on many
of the best features available to C++ programmers. Apart from being more
reliable, its so much more enjoyable using the STL.

Chris Gordon-Smith
www.simsoup.info
 
D

Daniel T.

I'm currently writing a C++ program for an assignment.

"Create a C++ program to manage 10 bank accounts... "

To include;

"An appropriate type definition to store the name, account number and
balance of a bank account

Your instructor, or your book should explain how to create "an
appropriate type definition to store the name, account number and
balance of a bank account." Hint, it will involve the "struct"
keyword.

Based on the rest of your post, the above [not knowing how to define a
type] is your basic problem...

Programmer defined types are compositions of other, already defined
types (both programmer defined and intrinsic.)

For example you might make a "person" type:

struct Person {
string firstName;
string lastName;
};

Now when you define a Person object, it will have both a firstName and
a lastName. Like this:

int main() {
Person p;
p.firstName = "Joe";
p.lastName = "Cool";

// do something with 'p'
}

You can also pass objects of this type to functions and return them
from functions:

void print( Person p );
Person findTeacherForRoom( int i );
 
J

James Kanze

osmium said:
"Victor Bazarov" write: [..]
define an array of ten account number, ten account
balances, ten account names (account holder names), ten
whatever else you want or see relevant to your task...
Gee, it's been so long since I did that that I completely
forgot about the approach. Self taught programmers
sometimes use this technique, it is called "parallel
arrays". This went out with the Volkswagen beetle. (sp?)

Let's hope it doesn't suffer a similar comeback. (It actually
went out when Fortran introduced used defined types.)
In some applications (high throughput analysis) this approach
is still used, in the form of tables composed of column
arrays. This is beneficial if one routinely performs
operations on whole columns. The cache locality is better and
the functions operating on the array do not need to care about
what other data there is in the table.

On some processors, multiplying by a power of two in the address
calculation may be significantly faster than multiplying by some
arbitrary numer. The basic types will all have sizes which are
a power of two; a user defined type probably won't.

Still, we're talking about very exotic optimizing techniques
here, not things that a beginner should start with.
 
G

gbvk

int main() {
Person p;
p.firstName = "Joe";
p.lastName = "Cool";

// do something with 'p'

}

You can also pass objects of this type to functions and return them
from functions:

void print( Person p );
Person findTeacherForRoom( int i );

okay.

if I have this correct, then i could use the account munber as the
person, i.e.:

int main() {
Person 10001; //10001 being the account number
10001.firstName = "Joe";
10001.lastName = "Cool";

}

could I then add "10001.balance = 250;"

also, in the program I would like to start it with "please enter
account number: " and for this to access the "person 10001"'s details
so that the balance may be added to/subtracted from and also to be
able to set this to 0 (all these i should be able to cover) but also
to change the balance with "cin"

i then need to have all acounts printing to screen and an additional
option to print to file. to print to screen the average (mean) balance
over all accounts, the account numbers and balances of all account <0
and also tho print to screen the account numbers and balances of the
'n' richest accounts.
 
G

gbvk

okay.

if I have this correct, then i could use the account munber as the
person, i.e.:

int main() {
Person 10001; //10001 being the account number
10001.firstName = "Joe";
10001.lastName = "Cool";

}

could I then add "10001.balance = 250;"

i'm guesisng not, as 10001.f is being highlighted as red, assuming
that this cannot be a numerical field
 
G

gbvk

#include <iostream.h>
#include <stdlib.h>

struct Account
{
string fName;
string lName
float balance;
};

void main ()
{

Account 10001, 10002, 10003, 10004, 10005, 10006, 10007, 10008, 10009,
10010;

10001.fName = "joe";
10001.lName = "cool";
10001.balance = 250;

was my basic understanding of this other than the numerical struct
 
G

gbvk

Person a10001;


awesome :)


now; the following is my basic bank account program. all working and
fully tested. how would i get it to "load" (if that's the right word)
a10001's (or any other Account) details to manipulate through this?

cout<< "Do you wish to:"<<endl;
cout<< "1: Deposit"<<endl;
cout<< "2: Withdraw"<<endl;
cout<< "3: View Balance"<<endl;
cout<< "4: Exit" <<endl;
cin>>choice;


while ((choice != 1) && (choice != 2) && (choice != 3) && (choice !=
4))
{
cout<<"you have input and invalid choice, try again"<<endl;
cout<< "Do you wish to:"<<endl;
cout<< "1: Deposit"<<endl;
cout<< "2: Withdraw"<<endl;
cout<< "3: View Balance"<<endl;
cout<< "4: Exit" <<endl;
cin>>choice;
}

while (choice != 4)
{
if (choice == 1)
{
cout<<"Please enter deposit amount: " <<endl;
cin>>amount;
account = account + amount;
cout<<"New balance: "<<account<<endl;
}//end if 1
else if (choice == 2)
{
cout<<"Please enter withdrawl amount: " <<endl;
cin>>amount;
account = account - amount;
cout<<"New balance: "<<account<<endl;
}//end if 2
else if(choice == 3)
{
cout<<"The account balance is: ";
cout<<account<<endl;
}//end if 3

cout<< "Do you wish to:"<<endl;
cout<< "1: Deposit"<<endl;
cout<< "2: Withdraw"<<endl;
cout<< "3: View Balance"<<endl;
cout<< "4: Exit" <<endl;
cin>>choice;


while ((choice != 1) && (choice != 2) && (choice != 3) && (choice !=
4))
{
cout<<"you have input and invalid choice, try again"<<endl;
cout<< "Do you wish to:"<<endl;
cout<< "1: Deposit"<<endl;
cout<< "2: Withdraw"<<endl;
cout<< "3: View Balance"<<endl;
cout<< "4: Exit" <<endl;
cin>>choice;
}
}//end while

}//end main
 
G

gbvk

Microsoft Visual C++ Express Edition 2008. Free. Download, install and
work in one of the best development environments around.

I have the 2005 edition already installed it seems :)

stupid question #1: how do i compile in this environment?

stupid question#2: how do i run what i have wrote?

thanks in advance
 
K

kwikius

awesome :)


now; the following is my basic bank account program. all working and
fully tested. how would i get it to "load" (if that's the right word)
a10001's (or any other Account) details to manipulate through this?

Now you can make an array and maybe use the index in the array as the
account number:

#include <string>
#include <iostream>

struct Account{
std::string fName;
std::string lName;
float balance;
};

int main()
{
// one way to create and fill array of accounts
Account Accounts [] =
{
{"John","Smith",1000.0},
{"Mary","Jones",2000.0},
{"Bill","Simpkins",20000.0}
};

// one way to know how many accounts were created for this type of array
// use of static is a technicality which you could find out about later
/*static */const int NumAccounts = sizeof(Accounts)/ sizeof(Account);

// for here means go forever unless you get a valid account number
for (;;){
std::cout << "Please enter your account number : ";
//maybe start with invalid account number
int AccountNumber=-1;

// read in from user

std::cin >> AccountNumber ;

std::cout << "\n\n";

if( (AccountNumber >= 0) && (AccountNumber < NumAccounts)){
std::cout << "Thankyou "
<< Accounts[AccountNumber].fName
<< " "
<< Accounts[AccountNumber].lName
<< "\n. You have a balance of "
<< Accounts[AccountNumber].balance
<< " euros in your account\n";
break; // break out of loop
}
else{
std::cout << "Sorry, This isnt a valid account number...\n";
continue; // go round the loop again
}
}
}

regards
Andy Little
 
K

kwikius

Victor Bazarov said:
Who said anything about .h?


Get a better compiler.

He better stick with what he has been given. If that is the compiler his
tutor uses then he had better just plug away until he getes it to work on
That compiler :)

regards
Andy Little
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top