Stroustrup exercise 7 section 5.9 (using struct)

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: "a struct &
    an array of that struct"

    this is the solution i have created:

    #include <iostream>
    #include <string>

    struct month_day {
    std::string month;
    int days;
    };

    // creating 12 structures
    month_day md0 = {"Jan", 31};
    month_day md1 = {"Feb", 28};
    .................................................
    month_day md11 = {"Dec", 31};

    month_day arr_struct[arr_size] =
    {md0, md1, md2, md3, md4, md5, md6, md7, md8, md9, md10, md11};

    void print_struct_arr(month_day as[])
    {
    for(int i=0; i < arr_size; ++i)
    std::cout << as.month << "\t" << as.days << "\n";
    }


    int main() {
    print_struct_arr(arr_struct);
    }
    arnuld, Nov 8, 2006
    #1
    1. Advertising

  2. 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: "a struct
    > & an array of that struct"
    >
    > this is the solution i have created:
    >
    > #include <iostream>
    > #include <string>
    >
    > struct month_day {
    > std::string month;
    > int days;
    > };
    >
    > // creating 12 structures
    > month_day md0 = {"Jan", 31};
    > month_day md1 = {"Feb", 28};
    > ................................................
    > month_day md11 = {"Dec", 31};
    >
    > month_day arr_struct[arr_size] =
    > {md0, md1, md2, md3, md4, md5, md6, md7, md8, md9, md10, md11};


    There is duplication of data here. What for? Couldn't you simply
    initialise the array elements using the brace notation?

    >
    > void print_struct_arr(month_day as[])
    > {
    > for(int i=0; i < arr_size; ++i)
    > std::cout << as.month << "\t" << as.days << "\n";
    > }
    >
    >
    > int main() {
    > print_struct_arr(arr_struct);
    > }


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 8, 2006
    #2
    1. Advertising

  3. arnuld

    mlimber Guest

    Victor Bazarov wrote:
    > 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: "a struct
    > > & an array of that struct"
    > >
    > > this is the solution i have created:
    > >
    > > #include <iostream>
    > > #include <string>
    > >
    > > struct month_day {
    > > std::string month;
    > > int days;
    > > };
    > >
    > > // creating 12 structures
    > > month_day md0 = {"Jan", 31};
    > > month_day md1 = {"Feb", 28};
    > > ................................................
    > > month_day md11 = {"Dec", 31};
    > >
    > > month_day arr_struct[arr_size] =
    > > {md0, md1, md2, md3, md4, md5, md6, md7, md8, md9, md10, md11};

    >
    > There is duplication of data here. What for? Couldn't you simply
    > initialise the array elements using the brace notation?
    >
    > >
    > > void print_struct_arr(month_day as[])
    > > {
    > > for(int i=0; i < arr_size; ++i)
    > > std::cout << as.month << "\t" << as.days << "\n";
    > > }
    > >
    > >
    > > int main() {
    > > print_struct_arr(arr_struct);
    > > }


    Besides that, the comments from my response to your post on the array
    version apply here, too.

    Cheers! --M
    mlimber, Nov 8, 2006
    #3
  4. arnuld

    arnuld Guest

    > Victor Bazarov wrote:

    > There is duplication of data here. What for?


    i know but i was not able to find a way out.

    > Couldn't you simply
    > initialise the array elements using the brace notation?


    yes i could.
    arnuld, Nov 8, 2006
    #4
  5. arnuld

    arnuld Guest

    > Besides that, the comments from my response to your post on the array
    > version apply here, too.


    thanks buddy :)

    > Cheers! --M


    ok i say, cheers! ;-)
    arnuld, Nov 8, 2006
    #5
  6. arnuld

    Earl Purple Guest

    arnuld wrote:
    > > Victor Bazarov wrote:

    >
    > > There is duplication of data here. What for?

    >
    > i know but i was not able to find a way out.
    >
    > > Couldn't you simply
    > > initialise the array elements using the brace notation?

    >
    > yes i could.


    const month_day arr_struct[] =
    {
    { "Jan", 31 },
    { "Feb", 28 },
    { "Mar", 31 },

    // etc to

    { "Dec", 31 }
    };
    Earl Purple, Nov 8, 2006
    #6
  7. arnuld

    arnuld Guest

    > const month_day arr_struct[] =
    > {
    > { "Jan", 31 },
    > { "Feb", 28 },
    > { "Mar", 31 },
    >
    > // etc to
    >
    > { "Dec", 31 }
    > };


    thanks & Stroustrup said in problem statement that it *must* be an
    "array of struct".
    arnuld, Nov 8, 2006
    #7
    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
    Replies:
    13
    Views:
    508
    Default User
    Nov 8, 2006
  2. arnuld
    Replies:
    2
    Views:
    291
    arnuld
    Nov 8, 2006
  3. arnuld
    Replies:
    6
    Views:
    355
    James Kanze
    Apr 3, 2007
  4. arnuld
    Replies:
    13
    Views:
    482
    James Kanze
    Apr 3, 2007
  5. arnuld
    Replies:
    10
    Views:
    592
    Erik Wikström
    Nov 8, 2008
Loading...

Share This Page