Truncate an int value [closed] - c++

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.

Related

How to take first 5 numbers of float [closed]

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

Using a counter-controlled while loop [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
The program should be print the numbers 0 through 10, along with their values doubled and tripled. The data file contains the necessary variable declarations and output statements.
Example the output:
single 1 double 2 triple 3
single 2 double 4 triple 6
single 3 double 6 triple 9
single 4 double 8 triple 12
here my code tell me if correct
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int x, double, triple;
int x = 0
while (x<=10)
{
cout << "x" << double = (x*2) << triple = (x*3) << endl;
x++;
}
return EXIT_SUCCESS
I'll attempt to put a guiding answer so I'm not going to give you a straight code that you can just copy and paste into your homework, but if you read and follow it, it should be the answer. (And next time, do go find your lecturer, or your tutor).
Some issues:
You are not printing the "Single" and "double" and "Triple" text which you should (based on your expected answer) So add that in.
You did your calculation to get the number for double, and triple - good. But again, you did not print them out.
Also C++ allows you to stack multiple cout all on one line, so for example:
cout << "My name is " << myname << endl;
Will print out:
My name is (content of variable myname)
And then print an end of line (endl). You can use that same technique to do part of your assignment to print out the results to meet the expected output.
Credit to Synetech
you miss a lot of code in there. You didn't print nothing what you want
try again with this inside while loop:
cout << “single “ << x << double << x*x
<<“ triple “<< x*x*x << endl;
x++;

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".

Calculating Fibonacci Numbers [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
that displays exactly 8 (eight) Fibonacci numbers starting and ending from user-specified numbers (program’s input). For example, if a user inputs index 3 and 10, then numbers (values) F3 - F10 are shown on the screen. Erroneous user’s input (e.g. negative number) or a smaller ending number than the first, should lead to warning and automatic repetition of the input.
To give you a small hint without doing all the work for you (as this seems to be some task for school, college, or university), here's how a Fibonacci number is defined:
f(0) = 0;
f(1) = 1;
f(n) = f(n - 1) + f(n - 2);
So in C++ this could be written like this:
int fibonacci(int n) {
if (n == 0)
return 0;
if (n == 1)
return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
This of course can be further optimized and it's not necessarily the best approach. And it also includes possible errors, that might lead to stack overflows (hey, isn't that what this site is about? :)). So try to understand the code, then try to learn and improve it. Don't just copy & paste.

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