number repeated twice in the variable [closed] - c++

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

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?

Create an overflow at a given value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If a unsigned byte overflows it goes from 255 to 0 and vica versa -1 gives 255.
Would it be possible to have it overflow at for example 200?
Without using if statements.
Overflow is fairly simple:
unsigned int a = 150, b = 150;
a += b; // do an operation
a %= 200; // wrap it
However, with underflow, it's a bit harder (see orlp's answer for this).
To make it less error prone if you use this variable several times, in C++ with operator overloading, you can make a class that simulates an integer type which wraps after every operation with operator overloading.
The modulo operator does what you want, with some trickery for negative values:
int wrap(int x, int n) {
return x < 0 ? ((x % n) + n) % n : x % n;
}
// wrap(205, 200) == 5
// wrap(-1, 200) == 199
Unless your willing to learn assembly, such an action would be impossible for several reasons.
All the types like char, short, int, etc. are builtin and predefined by the parser.
200 isnt a power of two; computer represent numbers in binary.
Note: The above is only true if you want implicit overflow; modulas lets you do explicit overflow.

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

Calculate the log base n with Shift left or Shift right [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I have a little problem.
Who knows how we can calculate the log
base n with Shift_L or Shift_R?
for example: for n=2 we had this solution:
int log(int n){
int res = 0;
while((n>>=1))
res++;
return res;
}
You don't seem to want the logarithm for a base b, but the largest integer n so that n <= log_b(x). If that's the case, the following function should serve your needs:
int intlog(double base, double x) {
return (int)(log(x) / log(base));
}
well this is rather a math problem instead of an actuall programming problem, if i understand your problem correctly:
log_2 (x) = log_a (x) / log_a (2) where a can be any base.
Therefore you could use the math.h's function log(double)
double res = log(x)/log(2);

Zero-crossing Sign function [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have written a Sign function and am wondering whether it is correct or not (Stupid question to ask, I know!) I'm just interested in knowing whether this is the best method to solve this particular task:
template<typename T>
T sign(T n)
{
if(n < 0) return -1;
if(n > 0) return 1;
return 0;
}
Would this give accurate enough results for large datasets? Can anyone see a problem, that I haven't come across that may arise when putting this into a real-life context?
Thanks
I would change return 0; to return n;. If n is NaN, sign should return NaN, not 0.