473,785 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stroustrup 5.9, exercise 10 (using a function)

this does not work, i know there is some problem in the "for loop" of
"print_arr" function. i am not able to correct the weired results i am
getting. i have no compile time error, it is only semantic-bug that is
causing the trouble:

EXPECTED: january, february, march....decemb er
GOT: january, january, january........ .january

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

STATEMENT:
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: posted code is implementation of (2)
*/

#include<iostre am>

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

print_arr(arr, arr_size);

return 0;

}

void print_arr(const char** arr, size_t arr_size)
{
const char** p = arr;

std::cout << "\n\tUSING FUNCTION\n";
for(unsigned int i=0; i < arr_size; ++i)
std::cout << *p << std::endl;

}

-------------- OUTPUT ----------------------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra 5.9_ex-10-
function.cpp
[arch@voodo tc++pl]$ ./a.out

USING FUNCTION
january
january
january
january
january
january
january
january
january
january
january
january
[arch@voodo tc++pl]$

Apr 2 '07 #1
13 2236
On Apr 2, 12:39 pm, Michael DOUBEZ <michael.dou... @free.frwrote:
Using pointer arithmetic, you forgot to increment p; use:
for(unsigned int i=0; i < arr_size; ++i,++p)

You can also use arr instead or just deference it:
for(unsigned int i=0; i < arr_size; ++i)
std::cout << arr[i] << std::endl;
Michael

OOPS!, sorry

Apr 2 '07 #2
OK, i have a little of improvement. what do you say?

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

STSTAMENT:
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: posted code is implementation of (2)
*/

#include<iostre am>

void print_arr(const char**);

int main()
{
const size_t MonthsInYear = 12;

const char* arr[MonthsInYear + 1] =
{"january", "february", "march", "april", "may", "june",
"july", "august", "september" , "october", "november",
"december", '\0'};
print_arr(arr);

return 0;
}
void print_arr(const char** arr)
{
std::cout << "\n\tUSING FUNCTION\n";
for(const char** p = arr; *p != '\0' ; ++p)
std::cout << *p << std::endl;
}

--------- OUTPUT -------------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra -O 5.9_ex-10-
function.cpp
[arch@voodo tc++pl]$ ./a.out

USING FUNCTION
january
february
march
april
may
june
july
august
september
october
november
december
[arch@voodo tc++pl]$

Apr 2 '07 #3
On 2 Apr, 10:07, "arnuld" <geek.arn...@gm ail.comwrote:
OK, i have a little of improvement. what do you say?

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

STSTAMENT:
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: posted code is implementation of (2)
*/

#include<iostre am>

void print_arr(const char**);

int main()
{
const size_t MonthsInYear = 12;

const char* arr[MonthsInYear + 1] =
{"january", "february", "march", "april", "may", "june",
"july", "august", "september" , "october", "november",
"december", '\0'};

print_arr(arr);

return 0;

}

void print_arr(const char** arr)
{
std::cout << "\n\tUSING FUNCTION\n";
for(const char** p = arr; *p != '\0' ; ++p)
std::cout << *p << std::endl;

}
Personally I'd prefer a version where the size is passed as an
argument just to be safe, something like your first try, but also
incrementing arr:

void print_arr(const char** arr, size_t arr_size)
{
std::cout << "\n\tUSING FUNCTION\n";
for(unsigned int i=0; i < arr_size; ++i, arr++)
std::cout << *arr << std::endl;

}

--
Erik Wikström

Apr 2 '07 #4
On Apr 2, 1:47 pm, "Erik Wikström" <eri...@student .chalmers.sewro te:

Personally I'd prefer a version where the size is passed as an
argument just to be safe, something like your first try, but also
incrementing arr:

void print_arr(const char** arr, size_t arr_size)
{
std::cout << "\n\tUSING FUNCTION\n";
for(unsigned int i=0; i < arr_size; ++i, arr++)
std::cout << *arr << std::endl;

}
this is the 1st time i have seen someone incrementing an "array",
never saw so even in any book. does it work like this:

*arr == &arr[0]
++arr == &arr[0 + 1]

?

IOW, like a pointer

Apr 2 '07 #5
On 2 Apr, 11:00, "arnuld" <geek.arn...@gm ail.comwrote:
On Apr 2, 1:47 pm, "Erik Wikström" <eri...@student .chalmers.sewro te:
Personally I'd prefer a version where the size is passed as an
argument just to be safe, something like your first try, but also
incrementing arr:
void print_arr(const char** arr, size_t arr_size)
{
std::cout << "\n\tUSING FUNCTION\n";
for(unsigned int i=0; i < arr_size; ++i, arr++)
std::cout << *arr << std::endl;
}

this is the 1st time i have seen someone incrementing an "array",
never saw so even in any book. does it work like this:

*arr == &arr[0]
++arr == &arr[0 + 1]

?

IOW, like a pointer
Yes, arr[N] is the same as *(arr[0] + N) == *(arr + N).

The following might help you understand but should *never* be used in
real life, read the rest at your own risk:
Using the transitivity of addition (meaning that A+B == B+A) we can
get some pretty ugly, but valid, syntax when indexing into arrays:

So arr[N] == *(arr + N) then we apply the transitivity rule on the
left hand giving *(N + arr) and then go back to the array-form gives
N[arr].

So given array arr then 2[arr] will give the third element in arr. And
then we replace the N with a variable and we get something like this:

int main()
{
int arr[3] = {1, 2, 3};
for (int i = 0; i < 3; ++i)
std::cout << i[arr];
return 0;
}

--
Erik Wikström

Apr 2 '07 #6
* Erik Wikström:
>
Using the transitivity of addition (meaning that A+B == B+A)
ITYM commutativity.

--
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 #7
On 2 Apr, 11:40, "Alf P. Steinbach" <a...@start.now rote:
* Erik Wikström:
Using the transitivity of addition (meaning that A+B == B+A)

ITYM commutativity.
Ah, yes.

--
Erik Wikström

Apr 2 '07 #8
arnuld wrote:
OK, i have a little of improvement. what do you say?
const char* arr[MonthsInYear + 1] =
{"january", "february", "march", "april", "may", "june",
"july", "august", "september" , "october", "november",
"december", '\0'};
void print_arr(const char** arr)
{
std::cout << "\n\tUSING FUNCTION\n";
for(const char** p = arr; *p != '\0' ; ++p)
std::cout << *p << std::endl;
}
I don't like the use of '\0' there. While it's technically not wrong,
as that's a legitimate null pointer constant, I think it betrays a lack
of understanding on your part.

That last element of the array is NOT a character, particularly it is
not a character like the null terminator in a character array (C-style
string).

What you want in this case for an array terminator is a null pointer.
I'd prefer the use of a plain 0, or even NULL (although many in the C++
community don't care for that macro).


Brian
Apr 2 '07 #9
On Apr 2, 9:55 pm, "Default User" <defaultuse...@ yahoo.comwrote:

I don't like the use of '\0' there. While it's technically not wrong,
as that's a legitimate null pointer constant, I think it betrays a lack
of understanding on your part.
:-(
That last element of the array is NOT a character, particularly it is
not a character like the null terminator in a character array (C-style
string).
well, its a new thing you told :-)

What you want in this case for an array terminator is a null pointer.
I'd prefer the use of a plain 0, or even NULL (although many in the C++
community don't care for that macro).
OK, 0 (zero) then
Brian

Apr 2 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
3154
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 had done myself a disservice by having not attempted and completed the exercises. I intend to rectify that. My current routine is to read a chapter and then attempt every exercise problem, and I find that this process is leading to a greater...
9
2266
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 /Date structures/. "Date" struct carries 3 things: month, date & year. i wrote the following code & of course it gives an error: #include <iostream>
16
2375
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 the types: unsigned char
11
1844
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 for names of months and an array of numbers for number of days. 2.) using an array of structures. each structure holds the name of the
6
3044
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 table of the name sof months o fyear and the number of days in each month. write out that table. Do this twice:
1
322
by: arnuld | last post by:
this does not work, i know there is some problem in the "for loop" of "print_arr" function. i am not able to correct the weired results i am getting. i have no compile time error, it is only semantic-bug that is causing the trouble: EXPECTED: january, february, march....december GOT: january, january, january.........january ------------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 10
14
2481
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: Read a sequence of words from the input. use "quit" as the word
5
468
by: arnuld | last post by:
i am not able to come up with any solution for this. i am not even able to think of one line of code for this solution. /* Strouostrup, 5.9, exercise 12 * * * STATEMENT: * write a function that counts the number of occurrences of a pair of * letters ina "string" and another that does the same in a zero- terminated
6
2480
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 std::ifstream while passing a pointer to it, it automatically opens the file. 3) If I open a file using std::ostream, then I am confused whether it will open the file for writing or appending ?.
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.