Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Stroustrup exercise 7 section 5.9 (using arrays)

 
 
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: 2 arrays, 1
for months & 1 for days.

this is the solution i have created:

#include <iostream>
#include <string>

const int arr_size = 12;

std::string arr_mon[arr_size] =
{"jan", "feb", "mar", "apr", "may", "june", "july", "aug", "sep",
"oct", "nov", "dec"};

int arr_days[arr_size] =
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31};

void print_2_arrays(std::string month[], int days[])
{
for(int i=0; i < arr_size; ++i)
{
std::cout << month[i]
<< "\t"
<< days[i] <<"\n";
}
std::cout << "=========================================\n";
}


int main() {
print_2_arrays(arr_mon, arr_days);
}

 
Reply With Quote
 
 
 
 
mlimber
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: 2 arrays, 1
> for months & 1 for days.
>
> this is the solution i have created:
>
> #include <iostream>
> #include <string>
>
> const int arr_size = 12;
>
> std::string arr_mon[arr_size] =
> {"jan", "feb", "mar", "apr", "may", "june", "july", "aug", "sep",
> "oct", "nov", "dec"};
>
> int arr_days[arr_size] =
> {31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
> 30, 31};
>
> void print_2_arrays(std::string month[], int days[])
> {
> for(int i=0; i < arr_size; ++i)
> {
> std::cout << month[i]
> << "\t"
> << days[i] <<"\n";
> }
> std::cout << "=========================================\n";
> }
>
>
> int main() {
> print_2_arrays(arr_mon, arr_days);
> }


It looks fine, but I would tweak it thusly:

1. Minimize the scope of both of your arrays by putting them in main()
or at the very least in an anonymous namespace. You should always try
to put variables in the minimum scope required because it means you
have less to worry about when trying to figure out what some piece of
code does. (As it stands, your main() doesn't even need to pass the
arrays to the print function since the latter has access to globals
also.)

2. Make both arrays (and your function parameters) constant. The
general rule here is to make everything constant unless it needs to be
modified. This also helps one to more easily understand code because by
inspection, you know that such-and-such variable won't change. (See
also
http://www.parashift.com/c++-faq-lit....html#faq-18.1.)

Cheers! --M

 
Reply With Quote
 
 
 
 
arnuld
Guest
Posts: n/a
 
      11-08-2006
> It looks fine, but I would tweak it thusly:

ok, go ahead

> 1. Minimize the scope of both of your arrays by putting them in main()
> or at the very least in an anonymous namespace. You should always try
> to put variables in the minimum scope required because it means you
> have less to worry about when trying to figure out what some piece of
> code does. (As it stands, your main() doesn't even need to pass the
> arrays to the print function since the latter has access to globals
> also.)
>
> 2. Make both arrays (and your function parameters) constant. The
> general rule here is to make everything constant unless it needs to be
> modified. This also helps one to more easily understand code because by
> inspection, you know that such-and-such variable won't change.


grabbed them

> (See also
> http://www.parashift.com/c++-faq-lit....html#faq-18.1.)


looking into it.


thanks a lot

 
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 struct) arnuld C++ 6 11-08-2006 04:40 PM
Stroustrup 5.9 exercise 6 arnuld C++ 7 11-08-2006 01:44 PM



Advertisments