Re: C++ Primer 4/e exercise 1.19
On Jul 12, 5:02 pm, Robert Bauck Hamar <roberth+n...@ifi.uio.no>
wrote:
James Kanze wrote:
width = ciel( log10( upper ) ) ;
printf( "%*d", ciel( log10( upper ), i ) ;
std::cout << std::setw( ciel( log10( upper ) ) ) << i ;
I reckon ciel is a new way to spell ceil?
YES, it is "ceil". i just checked Stroustrup and found that it belongs
to <cmath> library. so here is the complete programme:
#include <iostream>
#include <cmath>
#include <iomanip>
int main()
{
std::cout << "Enter 2 numbers: ";
int v1, v2;
std::cin >> v1 >> v2;
int lower,upper;
if(v1 <= v2)
{
lower = v1;
upper = v2;
}
else
{
lower = v2;
upper = v1;
}
int counter = 0;
for(int i=lower; i <= upper; ++i)
{
if(counter > 9)
{
std::cout << std::endl;
counter =0;
}
double width = ceil(log10(upper));
std::cout << std::setw(width) << i << ' '; // inserts space
between 2 numbers
++counter;
}
std:: cout << "\nThank you Bantu ;-)" << std::endl;
return 0;
}
and output is really nicely formatted:
[arnuld@arch cpp]$ ./a.out
Enter 2 numbers: 9 200
9 10 11 12 13 14 15 16 17 18
19 20 21 22 23 24 25 26 27 28
29 30 31 32 33 34 35 36 37 38
39 40 41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56 57 58
59 60 61 62 63 64 65 66 67 68
69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88
89 90 91 92 93 94 95 96 97 98
99 100 101 102 103 104 105 106 107 108
109 110 111 112 113 114 115 116 117 118
119 120 121 122 123 124 125 126 127 128
129 130 131 132 133 134 135 136 137 138
139 140 141 142 143 144 145 146 147 148
149 150 151 152 153 154 155 156 157 158
159 160 161 162 163 164 165 166 167 168
169 170 171 172 173 174 175 176 177 178
179 180 181 182 183 184 185 186 187 188
189 190 191 192 193 194 195 196 197 198
199 200
Thank you Bantu ;-)
[arnuld@arch cpp]$
BUT what exactly this fragment does:
double width = ceil(log10(upper));
std::cout << std::setw(width) << i
???