c++ ternary operator expected an expressionC/C++(29) [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
double recursively(int n) {
(n==0)?(return 0.0):((n==1)?(return 1.0):(return 2*recursively(n-1)+recursively(n-2)));
}
When I use the ternary operator in a recursive function it is not showing "expected an expressionC/C++(29)" error. I am using Visual Studio Code. Any possible reason?

The official name is "the conditional operator", and it is an expression that produces a value - a ? b : c is not shorthand for if (a) b; else c; but a choice between the values b and c.
Rewrite to return the value of the expression:
return n==0 ? 0.0 : (n==1 ? 1.0 : 2*recursively(n-1)+recursively(n-2));
The parentheses around n == 1 ? ... are technically redundant, but make it easier to read.
I sometimes break long lines like this:
return n == 0
? 0.0
: n == 1
? 1.0
: 2 * recursively(n-1) + recursively(n-2);

Related

If Statement Alternative (Conditional Operator) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I keep getting a syntax error when trying to compile the following:
template <class Type>
Type larger(Type x, Type y)
{
return (x >= y) ? x : y);
}
I am used to the normal if-else statements but I was trying to get some practice with this format. Any idea where I went wrong?
You have an extra ) at the end of the return statement :
return (x >= y) ? x : y);
should be :
return (x >= y) ? x : y;

How to get a not equal in a while loop in GMP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm Trying to do a do-while loop in VC 2008 Express using GMP integers
mpz_t d;
mpz_init(d);
do{
}while(d!=1);
The Error is: error C2040: '!=' : 'mpz_t' differs in levels of indirection from 'int'
The d!=1 part is causing this. What ways around this. The reason im using GMP is for big numbers.
As from the docs
Function: int mpz_cmp (MP_INT *operand1, MP_INT *operand2)
...
Compare operand1 and operand2. Return a positive value if operand1 > operand2, zero if
operand1 == operand2, and a negative value if operand1 < operand2.
Check the mpz_set_<xx> functions to setup a mpz_t value from a regular integer constant (as 1 represents) to compare with
mpz_t d;
mpz_init(&d);
mpz_t one;
mpz_set_si(&one,1);
// ...
do {
} while(mpz_cmp(&d,&one) != 0);

error in C for simulated templade [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have this error in Dev C. It says there is an error regarding pointers, but I'm not using pointers.
[Error] invalid conversion from 'int*' to 'int' [-fpermissive]
The error is in this line:
E=suma1 + distancias [x,y];
(Where suma1 and E are integers, and distancias is a matrix)
The expression x,y is actually a single value in C and C++. It evaluates both x and y but gives you the single value y. You can see this in action if you try:
#include <stdio.h>
int main (void) {
int a;
a = (1, 2, 3, 4, 5);
printf ("%d\n", a);
return 0;
}
which will output 5.
Hence what your current expression distancias [x,y] is being evaluated is is a simple distancias [y] (because evaluating x here has no side effects), which is why it's complaining about an int pointer being used where an int is expected.
The correct syntax for multi-dimensional arrays would be distancias [x][y].

Pointer + operator issue [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I recently started learning C++ and I'm a bit confused with pointers. Could you please explain me WHY in the following example variable "a" equals 1 and z = 0?????? I'm really confused!!!!!!
#include<iostream>
using namespace std;
void main()
{
int a;
int Z[3] ={1, 2, 3};
int *z;
z=Z;
a = (*z)--;
cout<<a<<" "<<*z<<"\n";
system ("pause");
}
logically ,I believe, first of all *z points to the 0-th element of the array - that is 1
then -- operator decreases 0-th element's value by 1 and now z[0] should be 0
but WHY it still returns 1 for "a" variable????
The order of your operations is this:
a = *z //*z = 1 here
*z = *z - 1 //*z = 0 here
Decrement operator happens after the assignment.
It is because the decrement operator is after the expression.
a = (*z)--;
Here first *z is evaluated and a is assigned the value (1). After that *z is decremented to zero.
If it had been
a = --(*z);
Then *z would have evaluated and decremented 1st. After that the value would have been assigned to a. Hence in this case both would be zero.
Post-decrement, thing--, yields the value before decrementing; so a is assigned the previous value of *z, which is 1.
Pre-decrement, --thing, yields the value after decrementing, so changing to a = --(*z); would set a to zero.

number repeated twice in the variable [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int a = 101;
return 0;
}
Question : How do I know that the number (1) is repeated twice in the variable
If you look at the code, you will see that the number 101 is assigned to the variable a and that number has the digit 1 twice in its decimal representation. So direct inspection is the way to go. I wouldn't even write the code for such a trivial requirement.
Use modulus 10 and division 10 to find it. Rough idea is,
while( a > 0 )
{
if( a % 10 == 1 )count_one++;
a=a/10;
}