what is the command for finding integer in a integer? - c++

for example I have a c++ which takes 2 integers from a file. For example int a = 2052342 and int b = 2. I need to do a search that will search if b is inside a. So in this example, b is inside a, but if b would be for example 7 it wouldn't be. What kind of function, should I use, and could you give me a quick demo of it?
Edit: No it's not a homework, I will compete in Programmers Olympiad this year, so I'm programming all of the C++ exercises that were in last Programmers Olympiads.

You use a pretty flakey example without much explanation of what "if b is inside a' means. Are you just checking to see if the substring of 'int b' is inside 'int a'? if B were 205, or 234, would that count?
Perhaps not the best way, but simple enough, is to convert each integer to a string, and then do a substring check to see if stringB is a substring of stringA.
Update: Infact, when you're parsing your file, i'd just read them in as string, don't even look at this as a problem of integers. You're just doing substring checks. Also, if you parse them as integers, you'd run in to 0-padding being lost.
If you're talking about division or something else, that could be done by doing division.

The simplest answer is just to keep the numbers in their string
representation, and use std::find. If you already have the numbers in
internal format, and converting them to a string would be too expensive:
a % 10 == b will be true if the low digit of a is equal to b, and
a /= 10 will remove the low digit. So:
while ( a != 0 && a % 10 != b ) {
a /= 10;
}
return a != 0;
should do the trick.

Related

C++ Turning Character types into int type

So I read and was taught that subtracting '0' from my given character turns it into an int, however my Visual Studio isn't recognizing that here, saying a value of type "const char*" cannot be used to initialize an entity of type int in C++ programming here.
bigint::bigint(const char* number) : bigint() {
int number1 = number - '0'; // error code
for (int i = 0; number1 != 0 ; ++i)
{
digits[i] = number1 % 10;
number1 /= 10;
digits[i] = number1;
}
}
The goal of the first half is to simply turn the given number into a type int. The second half is outputting that number backwards with no leading zeroes. Please note this function is apart of the class declared given in a header file here:
class bigint {
public:
static const int MAX_DIGITS = 50;
private:
int digits[MAX_DIGITS];
public:
// constructors
bigint();
bigint(int number);
bigint(const char * number);
}
Is there any way to convert the char parameter to an int so I can then output an int? Without using the std library or strlen, since I know there is a way to use the '0' char but I can't seem to be doing it right.
You can turn a single character in the range '0'..'9' into a single digit 0..9 by subtracting '0', but you cannot turn a string of characters into a number by subtracting '0'. You need a parsing function like std::stoi() to do the conversion work character-by-character.
But that's not what you need here. If you convert the string to a number, you then have to take the number apart. The string is already in pieces, so:
bigint::bigint(const char* number) : bigint() {
while (number) // keep looping until we hit the string's null terminator
{
digits[i] = number - '0'; // store the digit for the current character
number++; // advance the string to the next character
}
}
There could be some extra work involved in a more advanced version, such as sizing digits appropriately to fit the number of digits in number. Currently we have no way to know how many slots are actually in use in digits, and this will lead to problems later when the program has to figure out where to stop reading digits.
I don't know what your understanding is, so I will go over everything I see in the code snippet.
First, what you're passing to the function is a pointer to a char, with const keyword making the char immutable or "read only" if you prefer.
A char is actually a 8-bit sized 1 integer. It can store a numerical value in binary form, which can be also interpreted as a character.
Fundamental types - cppreference.com
Standard also expects char to be a "type for character representation". It could be represented in ASCII code, but it could be something else like EBCDIC maybe, I'm not sure. For future reference just remember that ASCII is not guaranteed, although you're likely to never use a system where it's no ASCII (if I'm correct). But it's not so much that char is somehow enforcing encoding - it's the functions that you pass those chars and char pointers to, that interpret their content as characters in ASCII encoding, while on some obscure or legacy platforms they could actually interpret them as characters in some less common encoding. Standard however demands that encoding used has this property: codes for characters '0' to '9' are subsequent, and thus '9' - '0' means: subtract code of '0' from code of '9'. The result is 9, because code for '9' is 9 positions from code for '0' in ASCII. Ranges 'a'-'z' and 'A'-'Z' have this quality as well, in case you need that, but it's a little bit trickier if your input is in base higher than 10, like a popular base of 16 called hexadecimal.
A pointer stores an address, so the most basic functionality for it is to "point" to a variable. But it can be used in various ways, one of which, very frequent in C, is to store address of the beginning of an array of variables of the same type. Those could be chars. We could interpret such an array as a line of text, or a string (a concept, not to be confused with C++ specific string class).
Since a pointer does not contain information on length or end of such an array, we need to get that information across to the function we pass the pointer to. Sometimes we can just provide the length, sometimes we provide the end pointer. When dealing with "lines of text" or c-style strings, we use (and c standard library functions expect) what is callled a null-terminated string. In such a string, the first char after the last one used for a line is a null, which is, to simplify, basically a 0. A 0, but not a '0'.
So what you're passing to the function, and what you interpret as, say 416, is actually a pointer to a place in memory where '4' is econded and stored as a number, followed by '1' and then '6', taking up three bytes. And depending on how you obtained this line of text, '6' is probably followed by a NULL, that is - a zero.
NULL - cppreference.com
Conversion of such a string to a number first requires a data type able to hold it. In case of 416 it could be anything from short upwards. If you wanted to do that on your own, you would need to iterate over entire line of text and add the numbers multiplied by proper powers of 10, take care of signedness too and maybe check if there are any edge cases. You could however use a standard function like int atoi (const char * str);
atoi - cplusplus.com
Now, that would be nice of course, but you're trying to work with "bigints". However you define them, it means your class' purpose is to deal with numbers to big to be stored in built-in types. So there is no way you can convert them just like that.
What you're trying to do right now seems to be a constructor that creates a bigint out of number represented as a c style string. How shall I put it... you want to store your bigint internally as an array of it's digits in base 10 (a good choice for code simplicity, readability and maintainability, as well as interoperation with base 10 textual representation, but it doesn't make efficient use of memory and processing power.) and your input is also an array of digits in base 10, except internally you're storing numbers as numbers, while your input is encoded characters. You need to:
sanitize the input (you need criteria for what kind of input is acceptable, fe. if there can be any leading or trailing whitespace, can the number be followed by any non-numerical characters to be discarded, how to represent signedness, is + for positive numbers optional or forbidden etc., throw exception if the input is invalid.
convert whatever standard you enforce for your input into whatever uniform standard you employ internally, fe. strip leading whitespace, remove + sign if it's optional and you don't use it internally etc.
when you know which positions in your internal array correspond with which positions in the input string, you can iterate over it and copy every number, decoding it first from ASCII.
A side note - I can't be sure as to what exactly it is that you expect your input to be, because it's only likely that it is a textual representation - as it could just as easily be an array of unencoded chars. Of course it's obviously the former, which I know because of your post, but the function prototype (the line with return type and argument types) does not assure anyone about that. Just another thing to be aware of.
Hope this answer helped you understand what is happening there.
PS. I cannot emphasize strongly enough that the biggest problem with your code is that even if this line worked:
int number1 = number - '0'; // error code
You'd be trying to store a number on the order of 10^50 into a variable capable of holding on the order of 10^9
The crucial part in this problem, which I have a vague feeling you may have found on spoj.com is that you're handling BIGints. Integers too big to be stored in a trivial manner.
1 ) The standard does not actually require for char to be this size directly, but indirectly it requires for it to be at least 8 bits, possibly more on obscure platforms. And yes, I think there were some platforms where it was indeed over 8 bits. Same thing with pointers that may behave strange on obscure architectures.

Mastermind Solver Implementation Issues

I'm writing in C++ an mastermind solver, with a-z possible letters, and the length of the string is to be inserted by the user - and should not be bound, given that we can hold a string of that size in memory. All of this, without using STD containers such as vector, etc. Also, without guessing guesses that are incorrect given the previously given feedbacks.
I wrote a dumbed-down version of Knuth's algorithm:
For each feedback we get back after a guess, we eliminate all other possibilities that if they were the actual code, they would not result this feedback. Unlike Knuth's, I'm not using minmax after that, I'm just eliminating from the group of possible guesses those that can't possibly be correct, based on feedbacks.
I implemented this in two ways:
Holding a boolean table of size 26^Length, which will denote if a guess X is still possible. Then at each getPossibleGuess() call we would return the string corresponding to the first valid cell (cell number 0 is aaa...a, cell 2 is aaaa...ab etc).
And when receiving a feedback we iterate over the table and setting cells to 'false' if the string they represent would not give the same feedback if they were the actual code.
Problem with this: Array size. 26^Length can get pretty big, and even a short length like 20 gives us a number not even unsigned long long can handle. So we can't satisfy the requirement for unbound length.
Second implementation:
Holding a class member which is the current string. When given a feedback, store that feedback. When asked for a possible guess, increment the current string (aaa...a into aaa..ab, aaa...az into aaaa...aba etc) and check if it complies with all the previously stored feedbacks. If so, return it. If not, increment and continue to search.
Problem with this: This can deal with as long a string as we want, but it is very very very slow. very.
I would be very grateful if someone could point me in the right direction on how to solve these issues.
Thank you!
As I mentioned in the comments I propose that you have an array of unsigned longs to track the possiblities. An unsigned int will also suffice on many platforms as long as sizeof(int) is more than 26 bits.
Each unsigned long in the array represents the set of possible letters that can be played at the corresponding position.
Examining the bits of the long:
00000000000000000000000000010101
ZYXWVUTSRQPONMLKJIHGFEDCBA
In this example the value is set to 21 which would correspond to 'A', 'C', and 'E' being already ruled as unplayable and all the other letters still being in contention.
You could use constants to track the "bit value" of each letter. (An enum won't work if sizeof(enum value) is less than 26 bits).
const unsigned long A = 1 << 0; // ==1 or 00000000000000001
const unsigned long B = 1 << 1; // ==2 or 00000000000000010
const unsigned long C = 1 << 2; // ==4 or 00000000000000100
const unsigned long D = 1 << 3; // ==8 or 00000000000001000
const unsigned long E = 1 << 4; // ==16 or 00000000000010000
// etc ...
You then create an array with one unsigned long for each position where a letter can be placed:
unsigned long possibilities [20] = {};
Then to rule out a letter you can bitwise OR | the constant letter value with the unsigned long in the correct position in the array. So for example, to rule out the letter 'D' at location 4, you could call:
possibilities[3] |= D; // rules out d by flipping the 'D' bit to 1.
To reset at the end of the game simply reset all values in your possibilities array back to 0.
To determine if a letter is still playable the following code using bitwise AND & would work:
if( (possibilities[3] & D) == D )
{
// D's bit is set to 1 therefore D has been ruled out
}
else
{
// D's bit is set to 0 therefore D is still a possibility
}
Note that this can also be accomplished (more elegantly) using std containers such as std::bitset or std::vector
You can encapsulate all this in a nice class so that the mucking around with bits is shielded wholly from any client code.
1) Knuth's algorithm only works for small puzzle sets, e.g. 6 colors, 4 locations for a total of 64 = 1296 possible answers. Large puzzle sets won't fit into memory, and take too long to analyze with minmax.
2) The counting algorithm will take forever since, for example, 2620 = 294. Forget about verifying whether a given solution is consistent with the feedback, just a simple loop that counts to 294 would take too long.
3) So you need a whole different approach. The best approach is one that's simple for a computer to implement (although boring and tedious for a human player).
Start with a guess that's all as, the feedback tells you how many as are in the answer. Repeat for b through z. Now you know how many of each letter you need.
Next you need to determine the correct locations for the letter a. So make a guess that has one a and all the rest b. The feedback for that guess will be 1 of 3 different possibilities. Assuming that the number of bs is known to be B, then the feedback will be
1 white and B-1 black, if the 'a' is where a 'b' should be.
1 white and B black, if the 'a' is where a 'c' thru 'z' should be.
0 white and B+1 black, if the 'a' is in one of the correct locations.
After guessing with a single a at each position, you will know exactly where all of the as and bs go. From then on, you can always guess with the as and bs in the correct locations. In the unknown positions, put one c and the rest ds ...

Operation similar to Python's "if x in y" for C/C++?

I have known Python for a while, trying to get my head around C.
I was wondering if there was anything similar to Python's
if x in y:
for example if x is 2 and y is 2540 the statement would be true, as y contains x as a digit.
Is there anything similar to this for C/C++? Because I haven't found it yet if there is.
In C++, if y is a std::string or a standard collection, you could use e.g. std::find (or the collections own find method) to see if it's in the collection.
In C, if y is a string, you could use strchr to see if a character is in the string.
For integers, there is no such method. You could convert the number to a string (using std::to_string in C++, or strtol in C) and then use one of the methods mentioned above.
I wouldn't convert the numbers to strings for this particular problem
simply because it is not necessary. Instead, look at this function (C code):
#include <assert.h>
int is_digit_in_number(unsigned char digit, unsigned int number)
{
assert(digit < 10);
while(number)
{
if(number % 10 == digit)
return 1;
number /= 10;
}
return 0;
}
this saves you the overhead of converting the number to string, and gives the compiler some room for smart optimizations. Also, imo it's clearer for the reader what the code should do, which is always a good thing :)
Cheers,
Andy

Another double type trick in C++?

First, I understand that the double type in C++ has been discussed lots of time, but I wasn't able to answer my question after searching. Any help or idea is highly appreciated.
The simplified version of my question is: I got three different results (a=-0.926909, a=-0.926947 and a=-0.926862) when I computed a=b-c+d with three different approaches and the same values of b, c and d, and I don't know which one to trust.
The detailed version of my question is:
I was recently writing a program (in C++ on Ubuntu 10.10) to handle some data. One function looks like this:
void calc() {
double a, b;
...
a = b - c + d; // c, d are global variables of double
...
}
When I was using GDB to debug the above code, during a call to calc(), I recorded the values of b, c and d before the statement a = b - c + d as follows:
b = 54.7231
c = 55.4051
d = -0.244947
After the statement a = b - c + d excuted, I found that a=-0.926909 instead of -0.926947 which is calculated by a calculator. Well, so far it is not quite confusing yet, as I guess this might just be a precision problem. Later on I re-implemented another version of calc() for some reason. Let's call this new version calc_new(). calc_new() is almost the same as calc(), except for how and where b, c and d are calculated:
void calc_new() {
double a, b;
...
a = b - c + d; // c, d are global variables of double
...
}
This time when I was debugging, the values of b, c and d before the statement a = b - c + d are the same as when calc() was debugged: b = 54.7231, c = 55.4051, d = -0.244947. However, this time after the statement a = b - c + d executed, I got a=-0.926862. That being said, I got three different a when I computed a = b - c + d with the same values of b, c and d. I think differences between a=-0.926862, a=-0.926909 and a=-0.926947 are not small, but I cannot figure out the cause. And which one is correct?
With Many Thanks,
Tom
If you expect the answer to be accurate in the 5th and 6th decimal place, you need to know exactly what the inputs to the calculation are in those places. You are seeing inputs with only 4 decimal places, you need to display their 5th and 6th place as well. Then I think you would see a comprehensible situation that matches your calculator to 6 decimal places. Double has more than sufficient precision for this job, there would only be precision problems here if you were taking the difference of two very similar numbers (you're not).
Edit: Unsurprisingly, increasing the display precision would have also shown you that calc() and calc_new() were supplying different inputs to the calculation. Credit to Mike Seymour and Dietmar Kuhl in the comments who were the first to see your actual problem.
Let me try to answer the question I suspect that you meant to ask. If I have mistaken your intent, then you can disregard the answer.
Suppose that I have the numbers u = 500.1 and v = 5.001, each to four decimal places of accuracy. What then is w = u + v? Answer, w = 505.101, but to four decimal places, it's w = 505.1.
Now consider x = w - u = 5.000, which should equal v, but doesn't quite.
If I only change the order of operations however, I can get x to equal v exactly, not by x = w - u or by x = (u + v) - u, but by x = v + (u - u).
Is that trivial? Yes, in my example, it is; but the same principle applies in your example, except that they aren't really decimal places but bits of precision.
In general, to maintain precision, if you have some floating-point numbers to sum, you should try to add the small ones together first, and only bring the larger ones into the sum later.
We're discussing here about smoke. If nothing changed in the environment an expression like:
a = b + c + d
MUST ALWAYS RETURN THE SAME VALUE IF INPUTS AREN'T CHANGED.
No rounding errors. No esoteric pragmas, nothing at all.
If you check your bank account today and tomorrow (and nothing changed in that time) I suspect you'll go crazy if you see something different. We're speaking about programs, not random number generators!!!
The correct one is -0.926947.
The differences you see are far too large for rounding errors (even in single precision) as one can check in this encoder.
When using the encoder, you need to enter them like this: -55.926909 (to account for the potential effect of the operator commutativity effects nicely described in previously submitted answers.) Additionally, a difference in just the last significant bit may well be due to rounding effects, but you will not see any with your values.
When using the tool, 64bit format (Binary64) corresponds to your implementation's double type.
Rational numbers do not always have a terminating expansion in a given base. 1/3rd cannot be expressed in a finite number of digits in base ten. In base 2, rational numbers with a denominator that is a power of two will have a terminating expansion. The rest won't. So 1/2, 1/4, 3/8, 7/16.... any number that looks like x/(2^n) can be represented accurately. That turns out to be a fairly sparse subset of the infinite series of rational numbers. Everything else will be subject to the errors introduced by trying to represent an infinite number of binary digits within a finite container.
But addition is commutative, right? Yes. But when you start introducing rounding errors things change a little. With a = b + c + d as an example, let's say that d cannot be expressed in a finite number of binary digits. Neither can c. So adding them together will give us some inaccurate value, which itself may also be incapable of being represented in a finite number of binary digits. So error on top of error. Then we add that value to b, which may also not be a terminating expansion in binary. So taking one inaccurate result and adding it to another inaccurate number results in another inaccurate number. And because we're throwing away precision at every step, we potentially break the symmetry of commutativity at each step.
There's a post I made: (Perl-related, but it's a universal topic) Re: Shocking Imprecision (PerlMonks), and of course the canonical What Every Computer Scientist Should Know About Floating Point Math, both which discuss the topic. The latter is far more detailed.

Non-Integer numbers in an String and using atoi

If there are non-number characters in a string and you call atoi [I'm assuming wtoi will do the same]. How will atoi treat the string?
Lets say for an example I have the following strings:
"20234543"
"232B"
"B"
I'm sure that 1 will return the integer 20234543. What I'm curious is if 2 will return "232." [Thats what I need to solve my problem]. Also 3 should not return a value. Are these beliefs false? Also... if 2 does act as I believe, how does it handle the e character at the end of the string? [Thats typically used in exponential notation]
You can test this sort of thing yourself. I copied the code from the Cplusplus reference site. It looks like your intuition about the first two examples are correct, but the third example returns '0'. 'E' and 'e' are treated just like 'B' is in the second example also.
So the rules are
On success, the function returns the converted integral number as an int value.
If no valid conversion could be performed, a zero value is returned.
If the correct value is out of the range of representable values, INT_MAX or INT_MIN is returned.
According to the standard, "The functions atof, atoi, atol, and atoll need not affect the value of the integer expression errno on an error. If the value of the result cannot be represented, the behavior is undefined." (7.20.1, Numeric conversion functions in C99).
So, technically, anything could happen. Even for the first case, since INT_MAX is guaranteed to be at least 32767, and since 20234543 is greater than that, it could fail as well.
For better error checking, use strtol:
const char *s = "232B";
char *eptr;
long value = strtol(s, &eptr, 10); /* 10 is the base */
/* now, value is 232, eptr points to "B" */
s = "20234543";
value = strtol(s, &eptr, 10);
s = "123456789012345";
value = strtol(s, &eptr, 10);
/* If there was no overflow, value will contain 123456789012345,
otherwise, value will contain LONG_MAX and errno will be ERANGE */
If you need to parse numbers with "e" in them (exponential notation), then you should use strtod. Of course, such numbers are floating-point, and strtod returns double. If you want to make an integer out of it, you can do a conversion after checking for the correct range.
atoi reads digits from the buffer until it can't any more. It stops when it encounters any character that isn't a digit, except whitespace (which it skips) or a '+' or a '-' before it has seen any digits (which it uses to select the appropriate sign for the result). It returns 0 if it saw no digits.
So to answer your specific questions: 1 returns 20234543. 2 returns 232. 3 returns 0. The character 'e' is not whitespace, a digit, '+' or '-' so atoi stops and returns if it encounters that character.
See also here.
If atoi encounters a non-number character, it returns the number formed up until that point.
I tried using atoi() in a project, but it wouldn't work if there were any non-digit characters in the mix and they came before the digit characters - it'll return zero. It seems to not mind if they come after the digits, for whatever reason.
Here's a pretty bare bones string to int converter I wrote up that doesn't seem to have that problem (bare bones in that it doesn't work with negative numbers and it doesn't incorporate any error handling, but it might be helpful in specific instances). Hopefully it might be helpful.
int stringToInt(std::string newIntString)
{
unsigned int dataElement = 0;
unsigned int i = 0;
while ( i < newIntString.length())
{
if (newIntString[i]>=48 && newIntString[i]<=57)
{
dataElement += static_cast<unsigned int>(newIntString[i]-'0')*(pow(10,newIntString.length()-(i+1)));
}
i++;
}
return dataElement;
}
I blamed myself up to this atoi-function behaviour when I was learning-approached coding program with function calculating integer factorial result given input parameter by launching command line parameter.
atoi-function returns 0 if value is something else than numeral value and "3asdf" returns 3. C -language handles command line input parameters in char -array pointer variable as we all already know.
I was told that down at the book "Linux Hater's Handbook" there's some discussion appealing for computer geeks doesn't really like atoi-function, it's kind of foolish in reason that there's no way to check validity of given input type.
Some guy asked me why I don't brother to use strtol -function located on stdlib.h -library and he gave me an example attached to my factorial-calculating recursive method but I don't care about factorial result is bigger than integer primary type value -range, out of ranged (too large base number). It will result in negative values in my program.
I solved my problem with atoi-function first checking if given user's input parameter is truly numerical value and if that matches, after then I calculate the factorial value.
Using isdigit() -function located on chtype.h -library is following:
int checkInput(char *str[]) {
for (int x = 0; x < strlen(*str); ++x)
{
if (!isdigit(*str[x])) return 1;
}
return 0;
}
My forum-pal down in other Linux programming forum told me that if I would use strtol I could handle the situations with out of ranged values or even parse signed int to unsigned long -type meaning -0 and other negative values are not accepted.
It's important upper on my code check if charachter is not numerical value. Negotation way to check this one the function returns failed results when first numerical value comes next to check in string. (or char array in C)
Writing simple code and looking to see what it does is magical and illuminating.
On point #3, it won't return "nothing." It can't. It'll return something, but that something won't be useful to you.
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/
On success, the function returns the converted integral number as an int value.
If no valid conversion could be performed, a zero value is returned.
If the correct value is out of the range of representable values, INT_MAX or INT_MIN is returned.