C++ Primer ex 7.12

A

arnuld

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

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 $

*/
 
I

Ian Collins

arnuld said:
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.
 
F

Frank Birbacher

Hi!
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C++ Primer ex 5.18 5
C++ Primer ex 4.16 2
C++ Primer ex 8.3 21
C++ Primer ex 7.5 18
C++ Primer ex 3.14 8
C++ Primer ex 3.24 (bitset class) 5
Reference to an Array of some dimension 7
C++ Primer ex 7.16 - arguments to main 15

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top