S
Steven T. Hatton
Examine the following code before you compile and run it. What do you
expect to happen? Is what you expected consistent with the result of
running it?
#include <valarray>
#include <iostream>
using std::valarray;
using std::slice;
using std::cout;
using std:stream;
valarray<size_t> makeValarray(const size_t& size) {
valarray<size_t> va(size);
for(size_t i = 0; i < va.size(); i++) { va = i + 1; }
return va;
}
template<typename T>
ostream& printValarray(const valarray<T>& va, ostream& out=cout) {
for(size_t i = 0; i < va.size(); i++) {
if(i){ out << ","; }
out << va;
}
return out << "\n";
}
valarray<size_t> nibble(const valarray<size_t>& va) {
return va[slice(1, va.size() - 1, 1)];
}
void spit() {
valarray<size_t> va = makeValarray(16);
while(va.size() > 1) {
printValarray(va);
va = nibble(va);
}
}
int main()
{
spit();
return 0;
}
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell
expect to happen? Is what you expected consistent with the result of
running it?
#include <valarray>
#include <iostream>
using std::valarray;
using std::slice;
using std::cout;
using std:stream;
valarray<size_t> makeValarray(const size_t& size) {
valarray<size_t> va(size);
for(size_t i = 0; i < va.size(); i++) { va = i + 1; }
return va;
}
template<typename T>
ostream& printValarray(const valarray<T>& va, ostream& out=cout) {
for(size_t i = 0; i < va.size(); i++) {
if(i){ out << ","; }
out << va;
}
return out << "\n";
}
valarray<size_t> nibble(const valarray<size_t>& va) {
return va[slice(1, va.size() - 1, 1)];
}
void spit() {
valarray<size_t> va = makeValarray(16);
while(va.size() > 1) {
printValarray(va);
va = nibble(va);
}
}
int main()
{
spit();
return 0;
}
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell