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!
Related
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 π.
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?
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 3 years ago.
Improve this question
I have tried to access values stored in a vector in reverse order. The following code shows no error:
for (long long int i = 0; i < end.size(); i++)
cout << end[end.size() - 1 - i] << "\n";
But the following code shows runtime error:
for(long long int i = end.size()-1;i>=0;i--) cout<<end[i]<<"\n";
Is there any difference between the two methods?
Is there any difference between the two methods?
end.size() returns std::size_t which is an unsigned type. Given an empty vector, you subtract 1 from unsigned zero. The result is a very large unsigned number due to modular arithmetic that unsigned numbers use.
Here, the behaviour depends on the version of the language, as well as the implementation. If long long can represent the large unsigned value, then you overflow the array with this large index (any index being outside the bounds of an empty vector) and behaviour will be undefined. This would happen on 32 bit systems where std::size_t is presumably 32 bits and long long 64 bits.
If the value is unrepresentable by long long, then prior to C++20, the resulting value will be implementation defined. If that value is negative, then you have desired behaviour, other wise undefined behaviour. After C++20, the result would be congruent with a representable value modulo the number of representable values. If bit width of long long matches with std::size_t, then the result would be -1 and behaviour would be as desired.
In conclusion: Latter approach is broken on some implementations. The first one doesn't have this problem.
The proper way to do it is:
for(auto i=end.size(); i-- ;) cout << end[i] << "\n" ;
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.
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;
}