Arrays?

Discussion in 'C++' started by gbvk@hotmail.co.uk, May 27, 2008.

  1. Guest

    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.
    , May 27, 2008
    #1
    1. Advertising

  2. Guest

    On May 27, 2:46 pm, Victor Bazarov <> wrote:
    > wrote:
    > > 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.

    >
    > 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
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Not to my knowledge :/

    Arrays have been specified in the assignment brief
    , May 27, 2008
    #2
    1. Advertising

  3. Guest

    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 :)
    , May 27, 2008
    #3
  4. wrote:

    > 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! :)

    --
    Christian Hackl
    Christian Hackl, May 27, 2008
    #4
  5. Guest

    On May 27, 3:18 pm, Christian Hackl <> wrote:
    > wrote:
    > > 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/cppvect...ww.parashift.com/c -faq-lite/containers.html
    >
    > I sincerely hope your teacher does not force you to use arrays rather
    > than std::vector! :)
    >
    > --
    > Christian Hackl


    Unfortunatly he is
    , May 27, 2008
    #5
  6. osmium Guest

    <> wrote:

    > 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.
    osmium, May 27, 2008
    #6
  7. osmium Guest

    "Victor Bazarov" write:


    >> 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 :)

    >
    > 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?)

    > I don't have any "online reading materials". Didn't your course
    > instructor give you anything?
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
    osmium, May 27, 2008
    #7
  8. wrote:

    > On May 27, 3:18 pm, Christian Hackl <> wrote:
    >> wrote:
    >> > 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/cppvect...ww.parashift.com/c -faq-lite/containers.html
    >>
    >> I sincerely hope your teacher does not force you to use arrays rather
    >> than std::vector! :)
    >>
    >> --
    >> Christian Hackl

    >
    > 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
    Chris Gordon-Smith, May 27, 2008
    #8
  9. Daniel T. Guest

    wrote:
    > 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 );
    Daniel T., May 27, 2008
    #9
  10. James Kanze Guest

    On May 27, 10:27 pm, Paavo Helde <> wrote:
    > "osmium" <> kirjutas:
    > > "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.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, May 28, 2008
    #10
  11. Guest


    > 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.
    , May 28, 2008
    #11
  12. Guest


    > 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
    , May 28, 2008
    #12
  13. Guest

    #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
    , May 28, 2008
    #13
  14. Guest

    > 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
    , May 28, 2008
    #14
  15. Stefan Ram Guest

    Victor Bazarov <> writes:
    >10001 is not a valid identifier, you can't use '10001' as a variable
    >name. You can, of course, do
    > Person a10001;


    or

    Person l000l;
    Stefan Ram, May 28, 2008
    #15
  16. Guest

    > > #include <stdlib.h>
    >
    > #include <cstdlib>


    Error: softdev1ass2.cpp(2,2):Unable to open include file 'CSTDLIB.h'

    >
    > using namespace std;


    Error: softdev1ass2.cpp(3,16):Namespace name expected


    Using Borland 5.02 :/
    , May 28, 2008
    #16
  17. Guest

    > Who said anything about .h?

    the complier

    > Get a better compiler.


    open to suggestions :)
    , May 28, 2008
    #17
  18. Guest

    > 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
    , May 28, 2008
    #18
  19. kwikius Guest

    <> wrote in message
    news:...
    >> 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?


    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
    kwikius, May 28, 2008
    #19
  20. kwikius Guest

    "Victor Bazarov" <> wrote in message
    news:g1k8aq$q64$...
    > wrote:
    >>>> #include <stdlib.h>
    >>> #include <cstdlib>

    >>
    >> Error: softdev1ass2.cpp(2,2):Unable to open include file 'CSTDLIB.h'

    >
    > Who said anything about .h?
    >
    >>
    >>> using namespace std;

    >>
    >> Error: softdev1ass2.cpp(3,16):Namespace name expected
    >>
    >>
    >> Using Borland 5.02 :/

    >
    > 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
    kwikius, May 28, 2008
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandra Stehman
    Replies:
    5
    Views:
    30,601
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,252
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    565
  4. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    384
  5. Philipp
    Replies:
    21
    Views:
    1,126
    Philipp
    Jan 20, 2009
Loading...

Share This Page