How to take first 5 numbers of float [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
float number=15.555555;
printf("FIRST FIVE NUMBERS%f",number);
Desired ouput:
FIRST FIVE NUMBERS15555
Actual output:
FIRST FIVE NUMBERS15.555555
How do I get the 5 digit number and discard the decimal point?
For example:
123.43213 -> 12343
1.5634567 -> 15634

Without elegance, some brute force and rounding, you might do:
#include <cmath>
#include <iostream>
int main()
{
double number = 15.555555;
while(100000 <= number) number /= 10;
while(number < 10000) number *= 10;
// 10000.x <= number < 100000
number = round(number);
if(100000 <= number) number = round(number/10);
std::cout << number << '\n';
}
Note: Only for numbers greater than zero.

You can always do something like this:
cout << "FIRST FIVE NUMBERS" << to_string(15.555555).substr(0, 5);
That won't round or anything but maybe it's what you want?
EDIT:
Per Lightness Races in Orbit's comment I think the desired result can be obtained from:
string foo = to_string(15.555555);
for_each(foo.begin(), advance(foo.begin(), min(5, foo.size())), [](const char& i){if(i != '.')cout << i});

Related

CodeBlocks C++ Display how many numbers i have typed in [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to make it, so lets say i type in 654321, it would say that i typed in 6 numbers.
I need to make it so it counts how many numbers i have typed in, and would display so.
Looking for anyone who could do that for me, thanks in advance.
Considering your entered number is an integer, you can setup a counter variable to count the number of digits and then divide the number by 10 and subsequently increment count in a loop:
#include <iostream>
int main()
{
long long num;
int count = 0;
std::cin>> num;
do
{ count++;
num /= 10;
} while(num != 0);
std::cout<< count;
}
Use long long for large input.
If your entered number is a string, then you can use stoi() to convert it into an integer.

How secure/effective is this very short random number generator? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I was experimenting with making my own random number generator and was surprised how easy it was to generate random numbers by doing something like this.
#include <iostream>
using namespace std;
int main(){
unsigned int number = 1;
for ( unsigned int i = 0; i < 0xFFFF ; i++ ){
unsigned int * data[0xFFFF];
number = number << 1;
number = number ^ (unsigned int)&data[i];
}
cout << number << endl;
while (1);
}
My question is, how effective is this, I mean, it seems to generate pretty random numbers, but how easy would it be to figure out what the next number is going to be?
The addresses of the data items are (in practice, because they'll be the same in each iteration) monotonically increasing. They're used as a one-time entropy source. Since they're monotonically increasing they're not a very good source of entropy.
In effect, for 32-bit code your code is equivalent to this:
auto main() -> int
{
unsigned number = 1;
unsigned const entropy = 123456; // Whatever.
for ( unsigned i = 0; i < 0xFFFF ; ++i )
{
number = number << 1;
number = number ^ (entropy + 4*i);
}
}
Regarding
” How easy would it be to figure out what the next number is going to be
as I see it that's not quite the right question for a pseudo-random number generator, but still, it's very easy.
Given two successive pseudo-random numbers A and B, computing (A << 1) ^ B yields X = entropy + 4*i. Now you can compute (B << 1) ^ (X + 4) and that's your next pseudo-random number C.
As I recall pseduo-random number generators are discussed in volume 1 of Donald Knuth's The Art of Computer Programming.
That discussion includes consideration of statistical measures of goodness.
It's not random at all. not even pseudo,
It has no state and all the input bits are discarded
basically you're pulling some junk off the stack and manipulating it a bit
in many contexts it will always give the same result.

C++ - How to get a binary representation of a decimal number [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'm trying to get the binary representation of a decimal number. I have looked all over the internet but could not find anything useful.
Can anyone provide me with sample code?
Note that I want it to run on both 32-bit and 64-bit architecture.
Thanks!
Just isolate the bits one by one, starting from the highest, and print the appropriate character:
#include <limits.h>
#include <stdio.h>
void print_binary(unsigned x)
{
for (int i = sizeof(x) * CHAR_BIT; i--; )
{
putchar('0' + ((x >> i) & 1));
}
}
int main()
{
print_binary(123);
}
If you want the result in a string instead of printed to the console, I'm sure you can adjust the code.
Get the bits from the bottom up.
Then reverse the string when done.
string bits(long n)
{
string tmp ;
while ( n ) { tmp << ( n & 1 ) ? "1" : "0" ; n >>= 1 ; }
tmp= reverse( tmp) ;
return tmp ;
}
Try William Clinger's paper "How to read floating point numbers accurately".

Truncate an int value [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 8 years ago.
Improve this question
I want to make a function which takes an integer with the number like 113, and separates the one's digit "3" and and the hundreds and tens places "11" and returns the both of them in two separate integers.
x%10 for the first digit (from right) and x/10 for the rest.
#include <iostream>
#include <utility>
std::pair<int,int> split(int x)
{
return std::make_pair(x/10, x%10);
}
int main()
{
std::pair<int,int> z = split(113);
std::cout << z.first << " " << z.second;
}
I also used std::pair to return the result.
You want N % 10 to get the one's digit. For the other digits N / 10.

Convert String Contents to Integers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
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
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I'm trying to convert the individual contents of a string to integers. I need to take each character from the string and convert it to an integer to add to another. This is not using C++11. Is there a simple way to do it?
if the characters are numbers then the numeral value of each is
num_value(c) = c - '0'
This is only possible because the characters representing numbers are in order in the ASCII table.. All you have to do is loop across the string.
"I need to take each character from the string and convert it to an integer to add to another"
In case you want to calculate the sum of digits stored in std::string object, you could do:
std::string myNum("567632");
int sum = 0;
for (size_t i = 0; i < myNum.size(); ++i)
sum += (myNum[i] - '0');
std::cout << sum;
which outputs 29 (i.e. 5 + 6 + 7 + 6 + 3 + 2)
How about std::accumulate ?
#include<string>
#include<algorithm>
//...
std::string myNum("123456789");
std::cout<<accumulate( myNum.begin(), myNum.end(), 0,
[](int sum,const char& x){return sum+=x-'0'; });