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 1 year ago.
Improve this question
Given two positive integers A and B and a string S of size N, denoting a number in base A, the task is to convert the given string S from base A to base B.
Example:
"25"
Base A : 6
Base B : 8
Expected Result : 21
Explanation:
The Number 25 is represented in base 6 format. The goal is to convert it to base 8 which in this case is 21.
divide the number by base.
write down the remainder.
repeat the process with the integer part of the division.
stop when you reach zero.
the remainders in reverse order give you the digits in base
With an array with the chars that corresponds to the base in order, base 32 should be like:
char digits[32] = "0123456789abcdefghijklmnopqrstuv";
I would use recursivity, but its up to you. When you divide, use the remainder in the array, so if the remainder is 12 for example, you will have the 'c' char.
If you have a string instead of a number for the original number, you can use string[i]-'a'+10 in the letters to obtain the corresponding number.
use itoa
char * itoa ( int value, char * str, int base );
https://www.cplusplus.com/reference/cstdlib/itoa/
Related
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 1 year ago.
Improve this question
int a[2];
memset(a, 3, sizeof(a));
when I run this I am getting output as 0 1. Why not 3 3
Does C++ memset only works for 0 and -1?
It does work for all byte values.
int a[2];
memset(a, 3, sizeof(a));
when I run this I am getting output as 0 1.
I doubt that. Either your system is broken, or you made a mistake.
Why not 3 3
Because that's not what std::memset does. It sets every byte to the value that you provide. A multi-byte integer whose each byte have the value 3 doesn't have the value 3. In the value 3, only the least significant byte would have the value 3 and the more significant bytes would be 0.
Unless you want a repeating byte pattern, std::memset won't be useful to you. If you simply want to assign a value to each element, then you should be using std::fill or its friends:
std::fill(std::begin(a), std::end(a), 3);
Or just a bare loop:
for(int& i : a)
i = 3;
0 happens to be the only byte pattern that preserves the value across all byte widths on all systems; -1 is another but only on 2's complement systems. On 1's complement systems the other pattern has the value -0. This is why std::memset incidentally behaves the same as std::fill when using those values and only when using those values.
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 4 years ago.
Improve this question
I was solving a question on geeksforgeeks.com on c++ where I determining the resulting type of the variable after operations.
Input:
1
1 2 3 5
gfgc
Output:
4 8 4 8
32 1
Example:
Testcase 1:
b/c = 2/3 =>sizeof(2/3)=>float size is 4 bytes
"b/a = 2/1 =>sizeof(2/1)=>double size is 8 bytes"
c/a = 3/1 =>sizeof(3/1)=>integer size is 4 bytes
(c/a)+l =3+5= 8 =>sizeof(8)=>long long size is 8 bytes
sizeof(gfgc) = 32 => It is not 4 because of the reason listed here
sizeof(c) = 1 as it is just a character.
Can someone explain the reason behind the output shown in the line in quotes?
Given float a and int b (or vice versa), and the expression auto c = a/b, both a and b are coerced into floats for the division, and c is a float.
More generally, if either operand is of a floating-point type, the resulting type will the first in this list that's found in the expression: long double, double, or float; the other operand is converted to that type. If both operands are integers then a similar sort of rule applies.
These are the "usual arithmetic conversions". Refer to [expr.arith.conv].
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 5 years ago.
Improve this question
First code
double pi=3.14159,a=100.64;
cin>>a;
double sum=(a*a)*pi;
cout <<fixed<<setprecision(4)<<"Value is="<<sum<<endl;
return 0;
the value is =31819.3103
second code
float pi=3.14159,a=100.64;
float sum=(a*a)*pi;
cout <<fixed<<setprecision(4)<<"Value="<<sum<<endl;
return 0;
the value is =31819.3105
why the difference between two value ?
In both float and double (and all other floating-point types available in c++) the values are represented in floating-point form: to store x = m * 2^p, the values m and p are written to memory.
Obviously, not all real numbers can be represented in such form (especially given that the maximum length of m and p is limited). All the numbers that cannot be represented in such form are rounded to one of the nearest neighbours. Since both 3.14159 and 100.64 are infinite fractions in the binary system, both of them are rounded, and when you write a = 3.14159, a is really a bit different.
Subsequently, the result of some expression calculation on the rounded values is not precise and may vary if we use a different rounding mode, that's why you see the result you see.
Probably, the value obtained by using double is more precise as double on most architectures and compilers uses more digits of mantissa. To achieve even more precision, consider using long double.
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 6 years ago.
Improve this question
I want to write a program in C++ to cin a Decimal number and cout the digits after Decimal
for example 0.26547 -> 5.
I wrote this but not work correctly:
int main()
{
int i=0,b;
float a ;
cin>>a ;
while(a!=0)
{
a*=10 ;
b=a ;
a-=b ;
i+=1 ;
}
cout<<i ;
}
For example for 0.258 instead of 3, returns 20.
can one explain me what is the problem of this code ?
thank you
C++ permits decimal representation of floating point numbers, but as far as I know all extant implementations use binary representation. And then the idea of storing the user's decimal number specification as a floating point value, loses critical information about the decimal digits: they're simply not there any more. So to count the decimal digits in the specification, you have to store it as a string.
Pseudo-code:
input number specification as a string, e.g. using getline.
verify that it's a valid number specification, e.g. using stod.
scan for the first period from the right, call this position P.
scan for the maximum number of decimal digits from position P.
I am unsure why your code does not work (What compiler are you using?), but I think it might related to
b=a;
Try explicitly casting your float to an int
b = int(a);
Alternatively, you could choose not to use an int, and round a float down using the function floor by including math.h #include <math.h>
float a = 5.9f;
float b = floor(a);
a -= b;
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.