473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Stroustrup exercise 7 section 5.9 (using struct)

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

Nov 8 '06 #1
6 1739
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
Nov 8 '06 #2
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

Nov 8 '06 #3
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Oplec | last post by:
Hi, I am learning standard C++ as a hobby. The C++ Programming Language : Special Edition has been the principal source for my information. I read the entirety of the book and concluded that I...
3
by: arnuld | last post by:
here is the code from section 2.5.1 from Stroustrup (Special Edition): namespace Stack { struct Rep; // definition of stack layout is elsewhere typedef Rep& stack; stack create(); //...
7
by: arnuld | last post by:
problem: define functions F(char), g(char&) & h(const char&). call them with arguments 'a', 49, 3300, c, uc & sc where c is a char, uc is unsigned char & sc is signed char. whihc calls are legal?...
9
by: arnuld | last post by:
problem: define a /struct Date/ to keep track of dates. provide functions that read Dates from input, write Dates to output & initialize a date with date. solution: i thought of a /vector/ of...
0
by: arnuld | last post by:
Stroustrup has a table in section 4.9 of declarations and definitions. he asks to write a similar table but in opposite sense: char ch; // declaration with definition he asks to do the...
2
by: arnuld | last post by:
MAX and MIN values of CHAR could not be displayed. Why ? BTW, any advice on improvement ? (please remember i have covered chapter 4 only) ------------- PROGRAMME -------------- /*...
6
by: arnuld | last post by:
this one was much easier and works fine. as usual, i put code here for any further comments/views/advice: --------- PROGRAMME ------------ /* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a...
27
by: arnuld | last post by:
it works fine without any trouble. i want to have advice on improving the code from any angle like readability, maintenance etc: ---------- PROGRAMME ------------ /* Stroustrup, 5.9, exercise 11...
6
by: arnuld | last post by:
This one works to seem fine. Can I make this program better ? 1) the use of get(ch) function was inspired from Stroustrup 21.5.1, page number 638. 2) I see, when you create an object of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.