A
arnuld
i have solved the problem but it is quite length. as usual, i wanted some
coding-standards or good-design opinions:
/* C++ Primer 4/e
* chapter 3 - Library Types
* exercise 3.13
* STAMEMENT
* read a set of integers into the vector. calculate and print the
sum of each * pair of adjacent elements of the vector. If there is an odd
number then tell the * user about it and print the value of the last
element without summing it. *
*/
#include <iostream>
#include <vector>
int main()
{
std::vector<int> ivec; /* empty vector */
int ivec_limit = 11;
for(int i = 5; i != ivec_limit; ++i) /* inserts itegers */
ivec.push_back(i);
/* now we will print the elements of vector */ std::cout << "These are
the elements of vector:\t"; for(std::vector<int>::const_iterator
iter=ivec.begin(); iter != ivec.end(); ++iter)
{
std::cout << *iter << " ";
}
std::cout << std::endl;
/* actual programme */
for(std::vector<int>::iterator iter = ivec.begin(); iter != ivec.end();
++iter)
{
if((*iter % 2) != 0)
{
std::cout << "\n"
<< *iter
<< " is an odd number"
<< " not adding it to the total."
<< std::endl;
if(iter == ivec.begin())
{
std::cout << *iter
<< " is the 1st element of vector"
<< std::endl;
}
else
{
std::cout << "--> last element was: "
<< *(iter - 1)
<< std::endl;
}
}
else
{
if((iter + 1) == ivec.end())
{
std::cout << "\nLast element of vector is: "
<< *iter
<< std::endl;
}
else
{
std::cout << "\nAdjacent elements are: "
<< *iter
<< " & "
<< *(iter + 1)
<< "\n--> and their sum is: "
<< *iter + *(iter + 1)
<< std::endl;
}
}
}
return 0;
}
===== OUTPUT =============
[arnuld@arch cpp ]% g++ -ansi -pedantic -Wall -Wextra ex_03-13.cpp
[arnuld@arch cpp ]% ./a.out
These are the elements of vector: 5 6 7 8 9 10
5 is an odd number not adding it to the total. 5 is the 1st element of
vector
Adjacent elements are: 6 & 7
--> and their sum is: 13
7 is an odd number not adding it to the total. --> last element was: 6
Adjacent elements are: 8 & 9
--> and their sum is: 17
9 is an odd number not adding it to the total. --> last element was: 8
Last element of vector is: 10
[arnuld@arch cpp ]%
coding-standards or good-design opinions:
/* C++ Primer 4/e
* chapter 3 - Library Types
* exercise 3.13
* STAMEMENT
* read a set of integers into the vector. calculate and print the
sum of each * pair of adjacent elements of the vector. If there is an odd
number then tell the * user about it and print the value of the last
element without summing it. *
*/
#include <iostream>
#include <vector>
int main()
{
std::vector<int> ivec; /* empty vector */
int ivec_limit = 11;
for(int i = 5; i != ivec_limit; ++i) /* inserts itegers */
ivec.push_back(i);
/* now we will print the elements of vector */ std::cout << "These are
the elements of vector:\t"; for(std::vector<int>::const_iterator
iter=ivec.begin(); iter != ivec.end(); ++iter)
{
std::cout << *iter << " ";
}
std::cout << std::endl;
/* actual programme */
for(std::vector<int>::iterator iter = ivec.begin(); iter != ivec.end();
++iter)
{
if((*iter % 2) != 0)
{
std::cout << "\n"
<< *iter
<< " is an odd number"
<< " not adding it to the total."
<< std::endl;
if(iter == ivec.begin())
{
std::cout << *iter
<< " is the 1st element of vector"
<< std::endl;
}
else
{
std::cout << "--> last element was: "
<< *(iter - 1)
<< std::endl;
}
}
else
{
if((iter + 1) == ivec.end())
{
std::cout << "\nLast element of vector is: "
<< *iter
<< std::endl;
}
else
{
std::cout << "\nAdjacent elements are: "
<< *iter
<< " & "
<< *(iter + 1)
<< "\n--> and their sum is: "
<< *iter + *(iter + 1)
<< std::endl;
}
}
}
return 0;
}
===== OUTPUT =============
[arnuld@arch cpp ]% g++ -ansi -pedantic -Wall -Wextra ex_03-13.cpp
[arnuld@arch cpp ]% ./a.out
These are the elements of vector: 5 6 7 8 9 10
5 is an odd number not adding it to the total. 5 is the 1st element of
vector
Adjacent elements are: 6 & 7
--> and their sum is: 13
7 is an odd number not adding it to the total. --> last element was: 6
Adjacent elements are: 8 & 9
--> and their sum is: 17
9 is an odd number not adding it to the total. --> last element was: 8
Last element of vector is: 10
[arnuld@arch cpp ]%