Stroustrup exercise 7 section 5.9 (using struct)

A

arnuld

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

Victor Bazarov

arnuld said:
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
 
M

mlimber

Victor said:
arnuld said:
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
 
E

Earl Purple

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


yes i could.

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

// etc to

{ "Dec", 31 }
};
 
A

arnuld

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".
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top