Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Stroustrup exercise 7 section 5.9 (using struct)

Reply
Thread Tools

Stroustrup exercise 7 section 5.9 (using struct)

 
 
arnuld
Guest
Posts: n/a
 
      11-08-2006
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[i].month << "\t" << as[i].days << "\n";
}


int main() {
print_struct_arr(arr_struct);
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-08-2006
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[i].month << "\t" << as[i].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


 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      11-08-2006
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[i].month << "\t" << as[i].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

 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      11-08-2006
> 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.

 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      11-08-2006
> 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!

 
Reply With Quote
 
Earl Purple
Guest
Posts: n/a
 
      11-08-2006

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 }
};

 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      11-08-2006
> 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".

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stroustrup Section 7.10, Exercise 4 arnuld C++ 10 11-08-2008 01:01 PM
Stroustrup 5.9 exercise 9 arnuld C++ 5 11-09-2006 10:45 AM
Stroustrup 5.9 exercise 6 (char& as argument) arnuld C++ 5 11-08-2006 06:03 PM
Stroustrup exercise 7 section 5.9 (using arrays) arnuld C++ 2 11-08-2006 03:18 PM
Stroustrup 5.9 exercise 6 arnuld C++ 7 11-08-2006 01:44 PM



Advertisments