Question about Operator Precedence in C++ - c++

The output is 34 but How this expression evaluated?
Could you please show with parenthesis?
Correct operator precedence is:
a += ((((((2 * i++) % 5) * 4) + (--j)) - (3 / k)) + 2);
But I think correct should be:
a += (((((2 * i++) % (5 * 4)) + (--j)) - (3 / k)) + 2);
#include <iostream>
using namespace std;
int main()
{
int a = 3, i = 12, j = 14, k = 16;
a += 2 * i++ % 5 * 4 + --j - 3 / k + 2;
cout << a;
}

The operators *, / and % have same precedence and are grouped left to right. Therefore it is ((2 * i++) % 5) * 4 and not (2 * i++) % (5 * 4)

Related

Why are the results different under debugging and running?

I'm solving a algorithm problem https://codeforces.com/contest/1671/problem/E. Although my submit can pass the tests provided by the contest, I find it fails on specific test(Hack). When I'm trying to find where's the error, I find that if I choose start debugging, the program would run perfectly. However, when I click "run", it would give a wrong answer. So, I'm curious about what happens.
#include<iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int const NN = 1e6;
int const MOD = 998244353;
char str[NN];
long long dfs_data[NN];
int powans[20];
string myhash[NN];
int n;
long long dfs(int num) {
if (dfs_data[num] != 0) return dfs_data[num];
if (num >= powans[n - 1] - 1) {
dfs_data[num] = 1;
return 1;
}
if (myhash[num * 2 + 1] == myhash[num * 2 + 2]) {
dfs_data[num] = (dfs(num * 2 + 1) % MOD) * (dfs(num * 2 + 2) % MOD) % MOD;
} else dfs_data[num] = 2 * (dfs(num * 2 + 1) % MOD) * (dfs(num * 2 + 2) % MOD) % MOD;
return dfs_data[num];
}
void gethashcode(int t) {
if (t >= powans[n - 1] - 1) {
myhash[t] += str[t];
return;
}
if (myhash[2 * t + 1] == "") gethashcode(2 * t + 1);
if (myhash[2 * t + 2] == "") gethashcode(2 * t + 2);
if (myhash[2 * t + 1] < myhash[2 * t + 2]) myhash[t] = str[t] + myhash[2 * t + 1] + myhash[2 * t + 2];
else myhash[t] = str[t] + myhash[2 * t + 2] + myhash[2 * t + 1];
}
void solve() {
memset(dfs_data, 0, sizeof dfs_data);
cin >> n;
cin >> str;
powans[0] = 1;
for (int i = 1; i < 19; i++) {
powans[i] = 2 * powans[i - 1];
}
for (int i = 0; i < NN; i++) {
myhash[i] = "";
}
gethashcode(0);
cout << dfs(0);
}
int main() {
solve();
}

Computing Pi with C++

I am trying to calculate Pi using this formula:
http://functions.wolfram.com/Constants/Pi/06/01/01/0005/
And this is the code:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
long double n;
cin >> n;
long double first_part = 0.0, second_part = 0.0, pi = 0.0;
for(int i = 0; i <= n; i++)
{
first_part += (pow(-1, n)) / ((2 * n + 1) * pow(5, 2 * n + 1));
second_part += (pow(-1, n)) / ((2 * n + 1) * pow(239, 2 * n + 1));
}
pi = (first_part * 16) - (second_part * 4);
cout << pi << endl;
return 0;
}
But something goes wrong. For example, for n = 300 it outputs 6.65027e-420.
I really cannot find my mistake.
Please help me.
Thank you very much.
You're using the wrong variable:
for(int i = 0; i <= n; i++)
^^^^^
iterating over 'i'
But:
first_part += (pow(-1, n)) / ((2 * n + 1) * pow(5, 2 * n + 1));
second_part += (pow(-1, n)) / ((2 * n + 1) * pow(239, 2 * n + 1));
^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^
all operations use 'n'
You reached the limit of floating point accuracy:
#include <cmath>
#include <iostream>
int main()
{
// This will print inf (infinite)
std::cout << std::pow(5.0, 600.0) << "\n"; // pow(5, 2 * n + 1))
return 0;
}
Change your code replacing all n to i in the for loop:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
long double n;
cin >> n;
long double first_part = 0.0, second_part = 0.0, pi = 0.0;
for(int i = 0; i <= n; i++)
{
first_part += (pow(-1, i)) / ((2 * i + 1) * pow(5, 2 * i + 1));
second_part += (pow(-1, i)) / ((2 * i + 1) * pow(239, 2 * i + 1));
}
pi = (first_part * 16) - (second_part * 4);
cout << pi << endl;
return 0;
}
I ran the above code and found outout:
3.14159
Careful: pow(239, 2 * n + 1)) can overflow second_part! Because double has the range:
1.7E +/- 308 (15 digits)
Reference here.

C++/3D Terrain: std::vector pushback() crashes with c0000374

When attempted to push back a vector of UINT, the progrma crashes with Critical error detected c0000374. Below is the initial code:
void Terrain::CreateIndexList(UINT Width, UINT Height){
UINT sz_iList = (Width - 1)*(Height - 1) * 6;
UINT *iList = new UINT[sz_iList];
for (int i = 0; i < Width; i++){
for (int j = 0; j < Height; j++){
iList[(i + j * (Width - 1)) * 6] = ((UINT)(2 * i));
iList[(i + j * (Width - 1)) * 6 + 1] = (UINT)(2 * i + 1);
iList[(i + j * (Width - 1)) * 6 + 2] = (UINT)(2 * i + 2);
iList[(i + j * (Width - 1)) * 6 + 3] = (UINT)(2 * i + 2);
iList[(i + j * (Width - 1)) * 6 + 4] = (UINT)(2 * i + 1);
iList[(i + j * (Width - 1)) * 6 + 5] = (UINT)(2 * i + 3);
}
}
for (int i = 0; i < sz_iList; i++){
Geometry.IndexVertexData.push_back(iList[i]);
}
delete[] iList;
}
The goal is to take the generated indices from the iList array and fill the Geometry.IndexVertexData vector array. While debugging this, I've created several other implementations of this:
//After creating the iList array:
Geometry.IndexVertexData.resize(sz_iList); //Fails with "Vector subscript out of range?"
UINT in = 0;
for (int i = 0; i < Width; i++){
for (int j = 0; j < Height; j++){
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 1] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 2] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 3] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 4] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 5] = iList[in];
in++;
}
}
And a final, direct to vector implementation:
Geometry.IndexVertexData.reserve(sz_iList);
for (int index = 0; index < sz_iList; index+=6) {
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6] = ((UINT)(2 * i));
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 1] = (UINT)(2 * i + 1);
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 2] = (UINT)(2 * i + 2);
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 3] = (UINT)(2 * i + 2);
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 4] = (UINT)(2 * i + 1);
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 5] = (UINT)(2 * i + 3);
}
sz_iList has a final value of 2166, resultant from a grid of 20x20 (400 total points) and is used to initialize sizes. In all cases, the vector array would not fully fill, crashing with Critical error detected c0000374. Am I doing something wrong?
Your sz_iList doesn't appear to be big enough. Let's use a simple example of Width = Height = 2;, then sz_iList = (2 - 1) * (2 - 1) * 6 = 6, right? But in your nested loops, the last iteration occurs when i = j = 1 (i is one less than Width and j is one less than Height), where (in the last line of your loop), you try to access element (i + j * (Width - 1)) * 6 + 5 = (1 + 1 * (2 - 1)) * 6 + 5 = (1 + 1 * 1) * 6 + 5 = 2 * 6 + 5 = 17, which is bigger than the size of your array. This results in undefined behavior.

Partition function based on Euler's formula

I am trying to find the partitions of a number using the Euler's formula for that:
It produces results like:
P(3) = P(2) + P(1) = 3
P(4) = P(3) + P(2) = 3+ 2 = 5
P(5) = P(4) + P(3) - P(0) = 5 + 3 - 1 = 7
P(6) = P(5) + P(4) - P(1) = 7 + 5 - 1 = 11 and so on..
* P(0) = 1
It produces two positive and then two negative values and so on.
I am using recursion for that but the code goes into an infinite loop without producing any result.
long result = 0;
long counter = 0;
class Euler
{
public:
long Partition(long n)
{
int exponent = 0;
if (n < 0)
{
return 0;
}
else
{
counter = counter + 1;
exponent = pow(-1, counter - 1) ;
if (n == 0)
{
n = 1;
}
return Partition((exponent * (n - ( (counter * ( (3 * counter) - 1)) / 2)))) +
Partition(((exponent * (n - ( (counter * ( (3 * counter) + 1)) / 2)) )));
}
}
};
int main(int argc, char** argv)
{
long result= 0;
long a = 3;
Euler * obj = new Euler();
long s = obj->Partition(a);
std::cout << s;
return 0;
}
Your global counter is modified by the first call to Partition, so the second one operates on a different one; in fact, the counter changes more or less unpredictably.
Do not use globals.

How Can I Remove Pixel Noise from ofxKinect Video?

I'm looking for some help figuring out how to remove some low quality pixel noise from a video, that I'm obtaining from an xbox kinect via open frameworks. I'm running logic against "moving" parts of an image, to determine what color is moving the most, and use those regions to also detect the depth of which those pixels are moving. I'm attaching a photo to try to better explain my issue.
http://imago.bryanmoyles.com/xxw80
Of course I know code will be asked for, so I'll post what I have so far, but what I'm looking for more than anything else, is a good algorithm for smoothing out pixelated regions in a photo using C++
for(int y = 0; y < kinect.height; y += grid_size) {
for(int x = 0; x < kinect.width * 3; x += 3 * grid_size) {
unsigned int total_r = 0, total_b = 0, total_g = 0;
for(int r = 0; r < grid_size; r++) {
for(int c = 0; c < grid_size; c++) {
total_r += color_pixels[(y * kinect.width * 3 + r * kinect.width * 3) + (c * 3 + x + 0)];
total_b += color_pixels[(y * kinect.width * 3 + r * kinect.width * 3) + (c * 3 + x + 1)];
total_g += color_pixels[(y * kinect.width * 3 + r * kinect.width * 3) + (c * 3 + x + 2)];
}
}
unsigned char average_r = total_r / (grid_size * grid_size),
average_b = total_b / (grid_size * grid_size),
average_g = total_g / (grid_size * grid_size);
for(int r = 0; r < grid_size; r++) {
for(int c = 0; c < grid_size; c++) {
color_pixels[(y * kinect.width * 3 + r * kinect.width * 3) + (c * 3 + x + 0)] = average_r;
color_pixels[(y * kinect.width * 3 + r * kinect.width * 3) + (c * 3 + x + 1)] = average_b;
color_pixels[(y * kinect.width * 3 + r * kinect.width * 3) + (c * 3 + x + 2)] = average_g;
}
}
}
}
for(int y = 0; y < kinect.height; y++) {
for (int x = 0; x < kinect.width * 3; x += 3) {
int total_difference = abs(color_pixels[y * kinect.width * 3 + x + 0] - rgb[0])
+ abs(color_pixels[y * kinect.width * 3 + x + 1] - rgb[1])
+ abs(color_pixels[y * kinect.width * 3 + x + 2] - rgb[2]);
unsigned char defined_color;
if(total_difference < 40) {
defined_color = (unsigned char) 255;
} else {
defined_color = (unsigned char) 0;
}
color_pixels[y * kinect.width * 3 + x + 0] = defined_color;
color_pixels[y * kinect.width * 3 + x + 1] = defined_color;
color_pixels[y * kinect.width * 3 + x + 2] = defined_color;
}
}
Again, I'd like to reiterate that my code is not the problem, I'm simply posting it here so that you understand I'm not just asking blindly. What I really need, is some direction on how to smooth out pixelated images, so that my averages don't get messed up frame by frame by poor quality.
You can process your image from the camera with some methods from the ofxOpenCV addon. There you will have methods like blur, undistort, erode, etc. Its easy to setup, because its already an addon. Have a look at the openCvExample which should be packed with your openFrameworks. For more information on the mentioned methods, take a look here. If I understand your problem correctly, then a little blur on the image could fix your problem already.