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

C++ Primer ex 7.12

it compiles and runs and at one place it produces strange output becaus
eof a semantic-bug. i can't find it:
/* C++ Primer - 4/e
*
* exercise 7.12
* STATEMENT:
* write a programme to calculate the sum of the elements in an
array. write th e function 3 times.each time using a different approach
to manage the array bounds.
*
*/
#include <iostream>

/* i wanted both parameters, the pointer itself the int to which the
pointer points, to be const but for me this "const" thing in C++ is too
complex to be understood :( , any help on that */

/* Method-1: using Pointers (like iterators) */
int print_sum_iter( const int *pbegin, const int *pend ) {
int sum = 0;
while( pbegin != pend )
{
sum += *pbegin++;
}

return sum;
}

/* Method-2: using indexing */
int print_sum_index( const int ia[], size_t arr_size ) {
int sum = 0;
for( size_t i=0; i != arr_size; ++i)
{
sum += ia[i];
}

return sum;
}
/* Method-3: using NULL terminator */

int print_sum_NULL( const int *parr ) {
int sum = 0;
while( *parr != '\0' )
{
sum += *parr++;
}

return sum;
}
int main()
{
const int arr_size = 3;
int ia[arr_size] = { 0, 2, 4 };

std::cout << "print_sum_iter: "
<< print_sum_iter( ia, ia + arr_size ) << "\n";

std::cout << "print_sum_index: "
<< print_sum_index( ia, arr_size )
<< "\n";

std::cout << "print_sum_NULL: "
<< print_sum_NULL( ia )
<< std::endl;

return 0;
}

/* OUTPUT
~/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra ex_07-12.cpp
~/programming/cpp $ ./a.out
print_sum_iter: 6
print_sum_index: 6
print_sum_NULL: 0
~/programming/cpp $

*/

--
http://arnuld.blogspot.com

Aug 16 '07 #1
2 1782
arnuld wrote:
it compiles and runs and at one place it produces strange output becaus
eof a semantic-bug. i can't find it:

int print_sum_NULL( const int *parr ) {
int sum = 0;
while( *parr != '\0' )
{
sum += *parr++;
}

return sum;
}
int main()
{
const int arr_size = 3;
int ia[arr_size] = { 0, 2, 4 };
std::cout << "print_sum_NULL: "
<< print_sum_NULL( ia )
<< std::endl;
The first element in ia is 0, so the loop end condition in
print_sum_NULL will be true.

--
Ian Collins.
Aug 16 '07 #2
Hi!

arnuld schrieb:
it compiles and runs and at one place it produces strange output becaus
eof a semantic-bug. i can't find it:
/* C++ Primer - 4/e
*
* exercise 7.12
* STATEMENT:
* write a programme to calculate the sum of the elements in an
array. write th e function 3 times.each time using a different approach
to manage the array bounds.
*
*/
#include <iostream>

/* i wanted both parameters, the pointer itself the int to which the
pointer points, to be const but for me this "const" thing in C++ is too
complex to be understood :( , any help on that */
You have to read types from right to left:
1. "int const *p" - p is a pointer to a const int
2. "const int *p" - p is a pointer to int which is const (same as 1)
3. "int *const p" - p is a const pointer to int
4. "int const *const p" - p is a const pointer to const int
/* Method-3: using NULL terminator */

int print_sum_NULL( const int *parr ) {
int sum = 0;
while( *parr != '\0' )
{
sum += *parr++;
}

return sum;
}
'\0' is a character constant, value 0
0 is an integer constant, value 0
NULL is a macro for an integer constant, value 0

This way your function find the first "0"-element in the array and
terminates. Also note:
const int arr_size = 3;
int ia[arr_size] = { 0, 2, 4 };
Your array has exactly three elements. There will be no extra "NULL" at
the end. This is different to string litterals. They get an extra 0 char
at the end:

char text[] = "Hallo";

The "text" array has 6 (!) elements because of the extra 0.

So finding the end of an array by a sentinel (this is what your approach
is called) is usually avoided because it can break so easily. Suppose
you hadn't had a zero in your array at all. Your loop would happily
search through your whole memory to find a zero.

Frank
Aug 16 '07 #3

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

Similar topics

7
by: Sandman | last post by:
Could anyone give me a tip about a good primer on object oriented php programming - why I should use it, the benefits, the drawbacks, the bugs, the glory? And, should I upgrade to php5 before...
1
by: Charles L | last post by:
Does anyone know where I can find errata for Stan Lippman's 'C++ Primer 2nd Edition'? Charles Leng
1
by: hugo | last post by:
what is L&L ,people or book?
5
by: hajime | last post by:
I purchased this book: C++ Primer, 4th Edition ISBN: 0201721481 in Australia. The right side of the last three lines on page 231 are not legible. A stain shows a piece of paper was on that part...
7
by: Lycan. Mao.. | last post by:
Hello, I am a newbie in C++ and I'm in trouble in choosing books, I hope some one who can give me some tips. I'm already know C and a little about Scheme, C#, Python, Lua and so on, and now I want...
2
by: W. Watson | last post by:
Is there a primer out there on these two items? I have the Python tutorial, but would like either a Tkinter tutorial/primer to supplement it, or a primer/tutorial that addresses both. Maybe there's...
20
by: arnuld | last post by:
I get an error, can't find what is the problem: /* C++ Primer - 4/e * * Chapter 8, exercise 8.3 * STATEMENT * write a function that takes and returns an istream&. the function should read...
2
by: xianwei | last post by:
First, typedef struct pair { Node *parent; Node *child; } Pair; static Pair SeekItem(cosnt Item *pI, const Tree *pTree) { Pair look;
1
by: Kveldulv | last post by:
Hi all, here is the code: http://pastebin.com/m6e74d36b I'm stuck at highfink constructor, last line before #endif. As parameters, I have reference to one parent class and int member of another...
0
by: cincerite | last post by:
Hello , guys , I'm reading C++ Primer 3rd edition recently.I tried to download the errata of it from Stan Lippman's Home Page:http:// staff.develop.com/slip/ ,but it says:We're Sorry, we could not...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.