error in C for simulated templade [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 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].

Related

c++ ternary operator expected an expressionC/C++(29) [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 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);

get the memory address instead of double value [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 2 years ago.
Improve this question
I just practice on c++ and want to get the area of circle from a given radius but the result giving me a memory address instead of value
#include <iostream>
#include<math.h>
using namespace std;
int main() {
double a, n, r;
n = 3.14159;
cin >> r;
a = pow(r,r) * n;
cout << "A=" << a<<endl;
}
while the input is 100.64 i got the output
A=1.13759e+202
but the result giving me a memory address instead of value
while the input is 100.64 i got the output
A=1.13759e+202
Your assumption is wrong. That is not a "memory address". That is the correct result of π × rʳ.
However, your calculation is not the correct one for area of a circle. Correct formula is π × r².
Bonus hint: r * r is typically better than calling std::pow.
Bonus hint 2: C++20 has constant std::numbers::pi in the <numbers> header. It provides you with the closes representable approximation of π.

string::length() returning garbage value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
I am trying to use string::length() inside a function I wrote, named match(), but I'm getting garbage values.
Why does the marked line is outputting garbage values?
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
void match(string st1,string st2)
{
for(int i=0;i<st1.length();i++)
{
cout<<"Why this value is garbage "<<(i-st1.length()-1)<<"\t";
// this expression gives wrong values ^^^^^^^^^^^^^^^
}
}
int main()
{
string st1,st2;
cout<<"Enter the required string\n";
cin>>st1>>st2;
match(st1,st2);
return 0;
}
imagine a string "foo":
i-st1.length()-1 means:
when i is 0:
0 - 3 = -3
- 1 = -4
but st1.length() is a size_t, which is unsigned, so all terms in the expression are promoted to unsigned values.
(unsigned)0 - (unsigned)3 = 0xfffffffffffffffd
- 1 = 0xfffffffffffffffc
= 18446744073709551612
The problem is that i is an int value, while string::length will return you a size_t value. The first one is a signed value, while the second is unsigned. One way to prevent this is to cast your st1.length() as an int, so all the elements in your operation are signed values. You will then get the value you are looking for.
i-(int)st1.length()-1
This is not garbage, you are implicitly converting/promoting a signed type (i is signed int) to an 'unsigned' one (the return type of length() is size_t which is unsigned).
It happens implicitly because an unsigned type is more powerful than a signed one. This is a common source of bugs in the C/C++ world.
This is what you need to modify:
cout<<" **NOT** garbage "<<(i-(int)st1.length()-1)<<"\t"<<endl;
Happy programming!

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);

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.