473,386 Members | 1,864 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,386 software developers and data experts.

Stroustrup 5.9, exercise 10

this works fine, any advice for improvement:

------------- PROGRAMME ------------------
/* Stroustrup, 5.9, exercise 10

STATAMENT:
define an array of strings,where strings contains the names months
.. Print those strings. Pass the array to a function that prints those
strings.
SOLUTION:

1.) 1st, i will print array int he "main" using "for" loop
and array indexing.

2.) then i wil print he array using a function and passing the array
to the function as argument(pass by reference).

NOTICE: this post contains only implementation of (1)
*/

#include<iostream>

void print_arr(const char**, size_t);

int main()
{
const char* arr[] = {"january", "february", "march", "april", "may",
"june",
"july", "august", "september", "october", "november",
"december"};
const size_t arr_size = sizeof(arr) / sizeof(*arr);

for(unsigned int i=0; i < arr_size; ++i)
std::cout << '\t' << arr[i] << std::endl;

return 0;
}

------------ OUTPUT --------------------------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra test.cpp
[arch@voodo tc++pl]$ ./a.out
january
february
march
april
may
june
july
august
september
october
november
december
[arch@voodo tc++pl]$

Apr 2 '07 #1
5 1743
* arnuld:
this works fine, any advice for improvement:

------------- PROGRAMME ------------------
/* Stroustrup, 5.9, exercise 10

STATAMENT:
define an array of strings,where strings contains the names months
. Print those strings. Pass the array to a function that prints those
strings.
SOLUTION:

1.) 1st, i will print array int he "main" using "for" loop
and array indexing.

2.) then i wil print he array using a function and passing the array
to the function as argument(pass by reference).

NOTICE: this post contains only implementation of (1)
*/

#include<iostream>

void print_arr(const char**, size_t);

int main()
{
const char* arr[] = {"january", "february", "march", "april", "may",
"june",
"july", "august", "september", "october", "november",
"december"};
const size_t arr_size = sizeof(arr) / sizeof(*arr);

for(unsigned int i=0; i < arr_size; ++i)
std::cout << '\t' << arr[i] << std::endl;

return 0;
}
This looks good. I'd add an extra 'const', "const char* const arr[]",
but that's it. Some people may comment on the use of 'unsigned' for the
loop variable, because 'unsigned' can become problematic for a loop that
counts down; however, with e.g. std::vector there are good reasons to
use std::size_t for the loop variable, and that's an unsigned type too.

------------ OUTPUT --------------------------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra test.cpp
This also looks mainly good. Improvement: in order to get warnings
about some (real nasty) problems you need to add the "-O" (optimize)
option, or some other optimization. The only further problem is that I
seem to remember that the g++ folks have deprecated -Wall, and
introduced some other option or set of options. However, I use -Wall
myself. But then, I also use "-o" (out file) with Visual C++, which is
an undocumented option. So perhaps I'm not the best example to follow.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 2 '07 #2
On Apr 2, 12:43 pm, "Alf P. Steinbach" <a...@start.nowrote:
This looks good. I'd add an extra 'const', "const char* const arr[]",
but that's it.
const char* const arr[];

says:

"elements of the array can not be modified and array itself can not"

IIRC, you can not modify an array at all. so i think we do not need
the extra "const".

right ?
Some people may comment on the use of 'unsigned' for the
loop variable, because 'unsigned' can become problematic for a loop that
counts down; however, with e.g. std::vector there are good reasons to
use std::size_t for the loop variable, and that's an unsigned type too.
yes, otherwise, i get a warning saying something like: matching of an
unsigned type with a signed type.

This also looks mainly good. Improvement: in order to get warnings
about some (real nasty) problems you need to add the "-O" (optimize)
option, or some other optimization.
this i what gcc says about "-0":

------------
With -O, the compiler tries to reduce code size and execution time,
without performing any optimizations that
take a great deal of compilation time.

-O turns on the following optimization flags: -fdefer-pop -
fdelayed-branch -fguess-branch-probability
-fcprop-registers -floop-optimize -fif-conversion -fif-
conversion2 -ftree-ccp -ftree-dce -ftree-domina-
tor-opts -ftree-dse -ftree-ter -ftree-lrs -ftree-sra -ftree-
copyrename -ftree-fre -ftree-ch -funit-at-a-time
-fmerge-constants

-O also turns on -fomit-frame-pointer on machines where
doing so does not interfere with debugging.
-----------------
The only further problem is that I
seem to remember that the g++ folks have deprecated -Wall, and
introduced some other option or set of options.
NO, i just checked the manual, it isn't deprecated, 4.1.2 is the
latest version and i use it on my "Arch Voodo".

However, I use -Wall
myself. But then, I also use "-o" (out file) with Visual C++, which is
an undocumented option. So perhaps I'm not the best example to follow.
my friends has VC++ which does not even recognise
"#include<iostream>". it always demands a ".h" in the end. don't know
which version he is using
Apr 2 '07 #3
* arnuld:
>On Apr 2, 12:43 pm, "Alf P. Steinbach" <a...@start.nowrote:
>This looks good. I'd add an extra 'const', "const char* const arr[]",
but that's it.

const char* const arr[];

says:

"elements of the array can not be modified and array itself can not"

IIRC, you can not modify an array at all. so i think we do not need
the extra "const".

right ?
'const' is never technically /needed/. It's just good programming
practice to apply 'const' wherever possible. Because that way the
chance of inadvertently changing something that shouldn't be changed, is
minimized.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 2 '07 #4
ajk
On Mon, 02 Apr 2007 12:54:01 +0200, "Alf P. Steinbach"
<al***@start.nowrote:
>'const' is never technically /needed/. It's just good programming
practice to apply 'const' wherever possible. Because that way the
chance of inadvertently changing something that shouldn't be changed, is
minimized.
not to forget: it also provides information for the next programmer
Apr 2 '07 #5
On Apr 2, 12:46 pm, "arnuld" <geek.arn...@gmail.comwrote:
On Apr 2, 12:43 pm, "Alf P. Steinbach" <a...@start.nowrote:
This looks good. I'd add an extra 'const', "const char* const arr[]",
but that's it.
const char* const arr[];
says:
"elements of the array can not be modified and array itself can not"
Not quite. The array contains pointers. The first const says
that what these pointers point to cannot be modified. (Sort of.
More exactly, it says that it cannot be modified through this
pointer without an explicit cast.) The second const says that
the pointer itself cannot be modified, to point to something
else.

Formally, an array is never const. But it's elements can be
const, which comes out to pretty much the same thing.
IIRC, you can not modify an array at all.
What makes you think that? You cannot modify the topology of
the array (dimensions, etc.), but you can certainly modify the
contents. Like Alf, I use both const in this sort of
expression, to ensure that nothing gets modified.
so i think we do not need the extra "const".
You never need it (except on reference arguments, if you want to
pass temporaries to them). On the other hand, it offers a fair
degree of protection, and very good documentation. I'd use it.
Some people may comment on the use of 'unsigned' for the
loop variable, because 'unsigned' can become problematic for a loop that
counts down; however, with e.g. std::vector there are good reasons to
use std::size_t for the loop variable, and that's an unsigned type too.
yes, otherwise, i get a warning saying something like: matching of an
unsigned type with a signed type.
Well, it's just a warning:-). But it is there for a reason.
Mixed signedness can sometimes have some very strange results.

As a general rule, of course, unless there is some reason for
doing otherwise (and interfacing with an externally imposed
interface which uses unsigned types is a good reason), stick
with int.
This also looks mainly good. Improvement: in order to get warnings
about some (real nasty) problems you need to add the "-O" (optimize)
option, or some other optimization.
this i what gcc says about "-0":
I think that Alf's point was that certain types of errors are
only detectable if the compiler does some flow analysis, and
that g++ (and most other compilers) only do flow analysis if
optimization is turned on.

It's an interesting point of view. Because optimizers are less
robust than normal code generators, I avoid optimization in
production code unless I need it. On the other hand, Alf is
right that there are a number of potential errors (e.g. using an
uninitialized variable) which a compiler can only find if it
does some optimization. So I guess the rule should be to use
optimization in debug builds, but to turn it off in production
builds.

[...]
my friends has VC++ which does not even recognise
"#include<iostream>". it always demands a ".h" in the end. don't know
which version he is using
It must be a very old one; I think <iostreamwas already
present in VC++ 5.0, and it was certainly present in 6.0 (which
is what, 10 years old now?).

Note that VC++ was one of the few compilers which handled the
transition gracefully, and offered two different
implementations. Maybe the reason your friends don't use
<iostreamis that some of there code counts on the behavior of
the classical iostream, and either doesn't compile or has the
wrong behavior if they include <iostream>.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 3 '07 #6

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...
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?...
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...
0
by: arnuld | last post by:
this programme runs without any trouble. it took 45 minutes of typing. i posted it here so that people can save their precious time: // Stroustrup special edition // chapter 4 exercise 2 //...
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 -------------- /*...
16
by: arnuld | last post by:
i did what i could do at Best to solve this exercise and this i what i have come up with: ----------- PROGRAMME -------------- /* Stroustrup 5.9, exercise 3 STATEMENT: Use typedef to define...
11
by: arnuld | last post by:
/* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a table of the name sof months o fyear and the number of days in each month. write out that table. Do this twice: 1.) using ar array of char...
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...
14
by: arnuld | last post by:
there is no "compile-time error". after i enter input and hit ENTER i get a run-time error. here is the code: ---------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 11 STATEMENT:...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.