Loop without for, while or goto [closed] - c++

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
In Windows, one can use structured exception handling in C to write a pseudo-loop that prints all numbers from 1 to 1000 like this:
int n = 0;
__try {
*(int *)0 = 0;
}
__except(printf("%i\n", ++n), n < 1000 ? -1 : 1) {
}
I wonder if there are also other ways in C/C++ to create a loop that isn't trivial to detect if you search the code for the usual suspect keywords for, while and goto.

In C++, a simple lambda can do that:
std::function<void(int,int)> print = [&](int from, int to)
{
std::cout << from << " ";
if ( from < to ) print(++from, to);
};
print(1, 1000);
It will print all integers from 1 to 1000. And it doesn't use for, while or goto.

Related

C++ code: count not working [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I wanted to ask, i formulated this code to solve a question, but count does not seem to provide the right value.
Any advice.Any help appreciated. Thanks.
#include<iostream.h>
#include<conio.h>
void main()
{
int count;
for(int a=1;a<125;a++)
for(int m=1;m<125;m++)
for(int n=1;n<125;n++)
{
if(a*(m+n+2)==249-m)
{
cout<<"a = "<<a<<" m = "<<m<<" n = "<<n<<"\n";
count=count+1;
}
}
cout<<"count = "<<count<<"\n";
getch();
}
You do not init the count. Remember to set int count = 0;.
Your compiler will warn you about this and save you the trouble of debugging or asking if you only let it. (from #chris)

replace function in std::string giving problems in C++ [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am using C++ string functions in cocos2dx. I have the following string CorrectAns = "below".
for(int i = 0; i<CorrectAns.size();i++)
{
CorrectAns.replace(i,i,"?");
}
This function should return my string as "?????", but its returning only 4 charcters ie "????".
When I write like this,
for(int i = 0; i<CorrectAns.size();i++)
{
if(i == 0)
{
CorrectAns.replace(i,i,"?");
}
}
It just crashes.
and works fine only when I write it as " CorrectAns.replace(i,i+1,"?");"
Why is the function working this way?? Can anyone help me please??
string& replace ( size_t pos1, size_t n1, const string& str );
For the versions with parameters pos1 and n1, the section replaced
begins at character position pos1 and spans for n1 characters within
the string.
So you should use
for(int i = 0; i<CorrectAns.size();i++)
{
CorrectAns.replace(i,1,"?");
}
Mb it will be more usefull use something like
CorrectAns.assign(CorrectAns.size(), '?');

What is the check_union256d? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
What is the check_union256d function?
It's placed in following code:
/* { dg-do run } */
/* { dg-require-effective-target avx } */
/* { dg-options "-O2 -mavx" } */
#include "avx-check.h"
void static
avx_test (void)
{
int i;
union256d u, s1, s2;
double e [4];
s1.x = _mm256_set_pd (2134.3343,1234.635654,453.345635,54646.464356);
s2.x = _mm256_set_pd (41124.234,2344.2354,8653.65635,856.43576);
u.x = _mm256_div_pd (s1.x, s2.x);
for (i = 0; i < 4; i++)
e[i] = s1.a[i] / s2.a[i];
if (check_union256d (u, e))
abort ();
}
It's from Intel AVX which is:
a new 256 bit instruction set extension to SSE and is designed for applications that are Floating Point (FP) intensive.

function keeps returning NaN [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
Hi guys i know what NaN(let me say i know the acronym stands for Not a Number) is but i don't understand why C++ returns it - The following is the approximation of the mathematical constant e - When using the debugger the functions evaluate fine, it's when writing to the console that it returns NaN
Thanks for any feedback
double Factorial(int k)
{
if(k == 0)
return 1;
int value = 1;
for(int i = k; i > 0; i--)
value *= k;
return value;
}
double e(int p)
{
double value = 0.0;
for(int i = 0; i < p; i++)
{
value += 1/Factorial(i);
}
}
You don't return a value in your e function.
You forgot to return value at the end of e. I don't know when c++ stopped warning about missing returns.

Interesting C++ operator overloading problem [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I made a Matrix class on my own. but there is interesting problem when overloading + operator.
Matrix Matrix::operator+ (Matrix& operand)
{
if(row_size == operand.row_size && col_size == operand.col_size)
{
Matrix temp(row_size, col_size);
for(int i = 0; i < col_size; i ++)
{
for(int j = 0; j < row_size; j ++)
{
temp[i][j] = data_list[i][j] + operand.data_list[i][j];
}
}
return temp;
}
else
throw Error::Matrix_error(0);
}
When I do
matrix + matrix2;
on main function, it seems like the program is in a infinite loop. So, I just debugged it and found out that the code stops at return temp; part.
I have no idea why this function cannot return temp since return type of operator+ is Matrix.
Plus, this code totally works well when I compile in a RELEASE mode (visual studio 2010). However it does not work when I compile in a DEBUG mode.
The problem was I miss created copy constructor. It looks like it fell on infinite loop at copy constructor but the debugger didn't catch it.