Stroustrup exercise 7 section 5.9 (using arrays)

Discussion in 'C++' started by arnuld, Nov 8, 2006.

  1. arnuld

    arnuld Guest

    i do not have any problem here. i solved the problem but i wanted to
    know the views of you. please look at it from a newbie's perspective:

    problem: define a table with names of months of the year & the number
    of days in each month. write out that table. do this using: 2 arrays, 1
    for months & 1 for days.

    this is the solution i have created:

    #include <iostream>
    #include <string>

    const int arr_size = 12;

    std::string arr_mon[arr_size] =
    {"jan", "feb", "mar", "apr", "may", "june", "july", "aug", "sep",
    "oct", "nov", "dec"};

    int arr_days[arr_size] =
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
    30, 31};

    void print_2_arrays(std::string month[], int days[])
    {
    for(int i=0; i < arr_size; ++i)
    {
    std::cout << month
    << "\t"
    << days <<"\n";
    }
    std::cout << "=========================================\n";
    }


    int main() {
    print_2_arrays(arr_mon, arr_days);
    }
     
    arnuld, Nov 8, 2006
    #1
    1. Advertising

  2. arnuld

    mlimber Guest

    arnuld wrote:
    > i do not have any problem here. i solved the problem but i wanted to
    > know the views of you. please look at it from a newbie's perspective:
    >
    > problem: define a table with names of months of the year & the number
    > of days in each month. write out that table. do this using: 2 arrays, 1
    > for months & 1 for days.
    >
    > this is the solution i have created:
    >
    > #include <iostream>
    > #include <string>
    >
    > const int arr_size = 12;
    >
    > std::string arr_mon[arr_size] =
    > {"jan", "feb", "mar", "apr", "may", "june", "july", "aug", "sep",
    > "oct", "nov", "dec"};
    >
    > int arr_days[arr_size] =
    > {31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
    > 30, 31};
    >
    > void print_2_arrays(std::string month[], int days[])
    > {
    > for(int i=0; i < arr_size; ++i)
    > {
    > std::cout << month
    > << "\t"
    > << days <<"\n";
    > }
    > std::cout << "=========================================\n";
    > }
    >
    >
    > int main() {
    > print_2_arrays(arr_mon, arr_days);
    > }


    It looks fine, but I would tweak it thusly:

    1. Minimize the scope of both of your arrays by putting them in main()
    or at the very least in an anonymous namespace. You should always try
    to put variables in the minimum scope required because it means you
    have less to worry about when trying to figure out what some piece of
    code does. (As it stands, your main() doesn't even need to pass the
    arrays to the print function since the latter has access to globals
    also.)

    2. Make both arrays (and your function parameters) constant. The
    general rule here is to make everything constant unless it needs to be
    modified. This also helps one to more easily understand code because by
    inspection, you know that such-and-such variable won't change. (See
    also
    http://www.parashift.com/c -faq-lite/const-correctness.html#faq-18.1.)

    Cheers! --M
     
    mlimber, Nov 8, 2006
    #2
    1. Advertising

  3. arnuld

    arnuld Guest

    > It looks fine, but I would tweak it thusly:

    ok, go ahead ;-)

    > 1. Minimize the scope of both of your arrays by putting them in main()
    > or at the very least in an anonymous namespace. You should always try
    > to put variables in the minimum scope required because it means you
    > have less to worry about when trying to figure out what some piece of
    > code does. (As it stands, your main() doesn't even need to pass the
    > arrays to the print function since the latter has access to globals
    > also.)
    >
    > 2. Make both arrays (and your function parameters) constant. The
    > general rule here is to make everything constant unless it needs to be
    > modified. This also helps one to more easily understand code because by
    > inspection, you know that such-and-such variable won't change.


    grabbed them

    > (See also
    > http://www.parashift.com/c -faq-lite/const-correctness.html#faq-18.1.)


    looking into it.


    thanks a lot
     
    arnuld, Nov 8, 2006
    #3
    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. arnuld

    Stroustrup 5.9 exercise 6

    arnuld, Nov 8, 2006, in forum: C++
    Replies:
    7
    Views:
    380
    arnuld
    Nov 8, 2006
  2. arnuld
    Replies:
    6
    Views:
    393
    arnuld
    Nov 8, 2006
  3. arnuld
    Replies:
    6
    Views:
    359
    James Kanze
    Apr 3, 2007
  4. arnuld
    Replies:
    13
    Views:
    500
    James Kanze
    Apr 3, 2007
  5. arnuld
    Replies:
    10
    Views:
    611
    Erik Wikström
    Nov 8, 2008
Loading...

Share This Page