445,778 Members | 1,919 Online
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

# Stroustrup exercise 7 section 5.9 (using struct)

 P: n/a 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 #include 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); } Nov 8 '06 #1
6 Replies

 P: n/a 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 #include 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 Nov 8 '06 #2

 P: n/a 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 #include 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 Nov 8 '06 #3

 P: n/a 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. Nov 8 '06 #4

 P: n/a 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! ;-) Nov 8 '06 #5

 P: n/a 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 } }; Nov 8 '06 #6

 P: n/a 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". Nov 8 '06 #7

### This discussion thread is closed

Replies have been disabled for this discussion.