get the memory address instead of double value [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
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 π.

Related

How to write a factorial calculator that extends to negative numbers? [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'm trying to write a program for a factorial which tries to calculate as such.
If n is the natural number, then the answer is n*(n-1)(n-2)....1(-1)(-2))(-3)...*(-10)
Here is C++ code which just doesn't go beyond printing n. It works without the if else statement.
#include <iostream>
using namespace std;
int main() {
int val=0, prod=1;
std::cout<<"Enter the number"<<std::endl;
std::cin>>val;
std::cout<<"The number is "<<val<<std::endl;
while(val>=-10)
{
prod=prod*val;
if (val=1)
{
val=val-2;
}
else
{
val=val-1;
}
}
std::cout<<prod<<std::endl;
return 0;
}
if (val=1)
should be
if (val==1)
= for assignment and == for comparison.
I would expect your compiler to warn you about this very common error. If it didn't you should find out why, if it did you should pay attention.
Compiler warnings will save you loads of time in the long run.
Sometimes programming tests are about common sense, like in this one:
You say that you need to calculate:
n*(n-1)*...*1*(-1)*(-2)*...*(-10)
This is the same as (there's an even number of negatives, so it becomes positive):
n*(n-1)*...*1*fact(10) // fact(10)=3,628,800
So, I would just write the function for calculating the factorial of a number and multiply the result by 3,628,800.
Obviously, there might a catch: fact(10) is about three million, while on most computers, the maximum value of int (the basic type you're using) is about two billion, which is not even a thousand times larger than the value you need to multiply with.
So, instead of using a simple int, I'd suggest you to use integer types which can hold larger numbers, like long long or unsigned long long. Maybe this is the real purpose of this exercise?

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!

C++ : Working of sine function [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 am trying to plot a sine curve in C++ and came across something interesting.
I have a function that returns sine value of a value in degree
double sind(double a)
{
return sin(a*3.14159/180);
}
Now in the main function
sind(18)==sind(18)?cout<<1:cout<<0;
I write the above code. The result seems to be false and it prints 0 on the console.
But according to me sin(18) and sin(18) are equal. So what is happening in the computer's mind?
Also if I want to check equality of two sine values how would I go about it?
On the PC, at least as of old, floating points values were calculated with 80 bits, but were rounded down to 64 bits for main memory. When the compiler recognizes that it can reuse an 80-bit result for an 80-bit comparison you can get baffling results like this. And yes, it's permitted by the Holy Standard.
By the way, void main is not valid. This means the code has Undefined Behavior and in principle can do anything whatsoever, including doing nothing, or what you expect. In practice it's not that bad, it just makes the code non-portable, but still, don't do it: it's silly to add one character in order to make the code non-portable, so write int main.
Also if I want to check equality of two sine values how would I go about it?
There is a very good example of a floating point "almost_equal" function here
by request, code copied from original source:
#include <cmath>
#include <limits>
#include <iomanip>
#include <iostream>
#include <type_traits>
#include <algorithm>
template<class T>
typename std::enable_if<!std::numeric_limits<T>::is_integer, bool>::type
almost_equal(T x, T y, int ulp)
{
// the machine epsilon has to be scaled to the magnitude of the values used
// and multiplied by the desired precision in ULPs (units in the last place)
return std::abs(x-y) < std::numeric_limits<T>::epsilon() * std::abs(x+y) * ulp
// unless the result is subnormal
|| std::abs(x-y) < std::numeric_limits<T>::min();
}
int main()
{
double d1 = 0.2;
double d2 = 1 / std::sqrt(5) / std::sqrt(5);
if(d1 == d2)
std::cout << "d1 == d2\n";
else
std::cout << "d1 != d2\n";
if(almost_equal(d1, d2, 2))
std::cout << "d1 almost equals d2\n";
else
std::cout << "d1 does not almost equal d2\n";
}

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.