Answer the question
In order to leave comments, you need to log in
An exercise from Stroustrup's book. A program about rice grains and a chessboard. How do you complete the task correctly?
Hello.
I did exercises for Bjarne Stroustrup's book "Programming. Principles and practice
of using C++".
The text of the exercises is:
My solution for these exercises is:
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;
#define LOG_STEPS
void how_cells(double seeds)
{
double sum = 0;
int cell_numb=0;
double seed_count=0;
for ( cell_numb=1, seed_count=1.0;
cell_numb <= 64;
cell_numb++,seed_count*=2.0
)
{
sum += seed_count;
#ifdef LOG_STEPS
cout << cell_numb << " " << fixed << seed_count << " " << fixed << sum << endl;
#endif //LOG_STEPS
if (sum>=seeds)
{
cout << "Answer is " << cell_numb << endl;
break;
}
}
}
int main()
{
//До какой клетки дойдем, чтобы получить хотя бы 1000 зерен риса в сумме
cout << "Task#1. Cell numb if 1000 seeds:" << endl;
how_cells(1000);
//До какой клетки дойдем, чтобы получить хотя бы 1000000 зерен риса в сумме
cout << "Task#2. Cell numb if 1.000.000 seeds:" << endl;
how_cells(1000000);
//До какой клетки дойдем, чтобы получить хотя бы 1000000000 зерен риса в сумме
cout << "Task#3. Cell numb if 1.000.000.000 seeds:" << endl;
how_cells(1000000000);
//До какой клетки дойдем, чтобы получить максимальное значение для int зерен риса в сумме
cout << "Task#4. Cell numb in int max:" << endl;
cout << "Max of int: " << numeric_limits<int>::max() << endl;
how_cells(numeric_limits<int>::max());
//До какой клетки дойдем, чтобы получить максимальное значение для double(только мантисса) зерен риса в сумме
cout << "Task#5. Cell numb in double max:" << endl;
//Должно быть такое значение 9223372036854775808.0 по замыслу Страуструпа, но младшие декады теряются
cout << "Max of double (mantissa): " << 9223372036854775808.0 << endl; //берется только мантисса pow(2.0,63.0) != numeric_limits<double>::max()
how_cells(9223372036854775808.0); //берется только мантисса pow(2.0,63.0) != numeric_limits<double>::max()
system("pause");
return 0;
}
#include <iostream>
#include <cfloat> //DBL_DIG
#include <limits> //std::numeric_limits<double>
using namespace std;
int main()
{
cout << "Number of digits (in decimal base) that can be represented without change." << endl;
cout << "pure C. <cfloat>. Answer: " << DBL_DIG << endl;
cout << "C++. <limits>.std::numeric_limits<double>. Answer: "<< numeric_limits<double>::digits10 << endl;
//Нет искажения (15 цифр)
cout << fixed << 999999999999999.0 << endl;
//Есть искажение по последней цифре (16 цифр)
cout << fixed << 9999999999999999.0 << endl;
//Нет искажения (16 цифр)
cout << fixed << 9999999999999912.0 << endl;
//Нет искажения (17 цифр). Наш случай макисмального неискаженного числа.
//Т.е. вышли за 15 цифр.
cout << fixed << 72057594037927936.0 << endl;
system("pause");
return 0;
}
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;
unsigned long long calc_sum()
{
unsigned long long sum = 0;
int cell_numb=0;
unsigned long long seed_count=0;
for ( cell_numb=1, seed_count=1;
cell_numb <= 64;
cell_numb++,seed_count*=2
)
{
sum += seed_count;
cout << cell_numb << " " << seed_count << " " << sum << endl;
}
return sum;
}
int main()
{
cout << "Max of unsigned long long: " << numeric_limits<unsigned long long>::max() << endl << endl;
unsigned long long total_seed_count = calc_sum();
cout << endl << "Total: " << total_seed_count << endl;
system("pause");
return 0;
}
Answer the question
In order to leave comments, you need to log in
Why do you need a double? In the task it is said - a variable of type int. To be exact unsigned int with reference to this task.
In addition, calculating the degree in a cycle is not interesting.
The problem can be solved using bitwise arithmetic. Considering that each set bit in an unsigned int is 2 of somewhat equal bit position in the number. You need to find the position of the most significant bit in the number +1 - this will be the answer to the question of the problem.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question