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
For example:
1: 123456 should be converted to 214365
2: 12345 should be converted to 103254
int32_t conv(int32_t n){
char buff[16], *p = &buff[1];
int i, v = n < 0 ? -n : n;
sprintf(p, "%010d", v);
for(i=0;i<5;++i,++p){
char c;
c = *p;
*p = p[1];
*(++p) = c;
}
buff[0] = (n < 0) ? '-' : ' ';
return atoi(buff);
}
size_t conv(size_t n){
size_t q, r, wk, mul=1, ret = 0;
for(;n;n/=100, mul*=100){
wk = n % 100;
r = wk % 10;
q = wk / 10;
ret += (r * 10 + q)*mul;
}
return ret;
}
,
First to clarify: there's a difference between an integer and the decimal representation of that integer in a string. You want the second.
As H2CO3 points out you can do this on your own using modulo's: decompose your number into the decimal base, do the swaps then recompose the number (only mul/add).
Here's another way:
Create a string from your integer using malloc and sprintf.
Loop through the string and do the swapping of characters
print that string or if you want convert it back to an integer with atoi
Related
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 6 days ago.
Improve this question
Why when I run this code b in the end transforms to 1410065408?
But 1 step before b was 100000000000 and it should be the same
(It's calculator from dec form to binnar)
P.S. all program steps I watched in C visualize
#include<stdio.h>
int binnar(int a){
long long int b = 0, x = 1;
while(a != 0){
b += (a % 2) * x;
x *= 10;
a = a/2;
}
return b;
}
void main(){
int a = 1024;
printf("%lld", binnar(a));
}
I expect that b will be 100000000000, not 1410065408
% is the modulo operator (returns the remainder)
What happens here is
a=1024, a%2=0, x=1, b=0
a=512, a%2=0, x=10, b=0 (b is not incremented because a%2 is 0)
and it continues until
a=1, a%2=1, x=100000000000 BUT because the return type of binnar is an int, it does not work the way you want it to, and it overflows. Try changing the return type of binnar to a long long and see if it works as you intend.
edit: added code
#include<stdio.h>
long long int binnar(int a){
long long int b = 0, x = 1;
while(a != 0){
b += (a % 2) * x;
x *= 10;
a = a/2;
}
return b;
}
void main(){
int a = 1024;
printf("%lld", binnar(a));
}
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
How can I remove the zeros that will not affect the number value
float X1 = (-b + sqrt(pow(b,2) - (4*a*c)))/(2*a);
sometimes if the X1 was a number without a decimal it will be X1=4.0000
how can I avoid that
{edited}
option #1
Use %g of printf
Unless alternative representation is requested the trailing zeros are
removed, also the decimal point character is removed if no fractional
part is left.
code sample:
double x = 4.0;
printf("DEBUG:%f\n", x);
printf("X1=%g\n", x);
OUTPUT:
DEBUG:4.000000
X1=4
option #2
Output it to a string and process it.
code sample:
double x = 4.0;
char buff[48];
int len;
len = snprintf(buff, sizeof buff, "%.4f", x);
printf("DEBUG:%s\n", buff);
char *p = buff + len -1;
while(*p == '0'){
*p-- = 0;
}
if(*p == '.')
*p = 0;
printf("X1=%s\n", buff);
OUTPUT:
DEBUG:4.0000
X1=4
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 need a function (in the C language) which can convert the binary contents of an array, in this case myArray[8] {*MSB* 1, 1, 1, 1, 1, 1, 1, 1 *LSB*} into the decimal equivalent = 255.
Any ideas on how to solve this, as efficiently as possible, as it is being done on a microcontroller?
Using bitwise shifting
#include <stdio.h>
int main(void) {
char myArray[8] = {1,1,1,1,1,1,1,1};
int i;
int result = 0;
for (i=0; i<sizeof(myArray); i++){
result += myArray[sizeof(myArray)-i-1] << i;
}
printf("%d\n", result);
return 0;
}
One solution is to add and shift in a Horner scheme.
result = (...((myArray[0])<<1 + myArray[1])<< 1+ ... ) << 1 ... ) + myArray[8];
or in multiple expressions:
result = myArray[0];
result <<= 1;
result += myArray[1];
result <<= 1;
...
result += myArray[8];
This solution is for MSB.
set up the for loop
unsigned int result = myarray[0];
for (int i = 1; i < 8, i++);
{
result <<= 1;
result += myarray[i];
}
#include <stdio.h>
int main()
{
int a[8] = {1,1,1,1,1,1,1,1};
int n=8,dec=0;
int j=0,f;
for(int i=(n-1);i>=0;i--)
{
dec=(a[i]*(int)pow(2,j))+dec;
j++;
}
printf("The converted Decimal number is: %d",dec);
return 0;
}
Output: The converted Decimal number is: 255
The result is also available at: http://ideone.com/NQK0Il
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 8 years ago.
Improve this question
Imagine we've got an int number = 1040 or int number = 105 in a C program, and we want to know if this number contains a 0 and in which position/s are they. How could we do it?
Examples
1040 -> position 0 and 2.
1000 -> position 0, 1 and 2.
104 -> position 1.
56 -> NO ZEROS.
Thanks!
I would divide by 10 and check the remainder. If remainder is 0, then last position of the number is 0. Then repeat the same step until number is less than 10
#include<iostream>
int main(void)
{
long int k = 6050404;
int iter = 0;
while (k > 10) {
long int r = k % 10;
if( r == 0) {
std::cout << iter << " ";
}
k = k / 10;
iter++;
}
}
I would convert it to a string; finding a character in a string is trivial.
As a rule of thumb, if you are doing maths on something, it's a number; otherwise, it's probably (or should be treated as) a string.
Alternatively, something like:
#include <stdio.h>
int main(void) {
int input=1040;
int digitindex;
for (digitindex=0; input>0; digitindex++) {
if (input%10==0) {
printf("0 in position %i\n",digitindex);
}
input/=10;
}
return 0;
}
This basically reports if the LAST digit is 0, then removes the last digit; repeat until there is nothing left. Minor modifications would be required for negative numbers.
You can play with this at http://ideone.com/oEyD7N
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 want to put a number like 123456 in to a array of digits. Could you please give me a hint to the process? Can i define an array with unknown number of elements?
First calculate no of digits
int count = 0;
int n = number;
while (n != 0)
{
n /= 10;
cout++;
}
Now intialize the array and assign the size:
if(count!=0){
int numberArray[count];
count = 0;
n = number;
while (n != 0){
numberArray[count] = n % 10;
n /= 10;
count++;
}
}
If you don't mind using char as the array element type, you can use snprintf():
char digits[32];
snprintf(digits, sizeof(digits), "%d", number);
Each digit will be represented as the character values '0' though '9'. To get the integer value, subtract the character value by '0'.
int digit_value = digits[x] - '0';
"Can i define an array with unknown number of elements ?"
If the number is too large you can input it as string and then accordingly extract digits from it
Something like following :
char buf[128];
int *array;
//fscanf(stdin,"%s",buf);
array = malloc(strlen(buf) * sizeof(int)); //Allocate Memory
int i=0;
do{
array[i] = buf[i]-'0'; //get the number from ASCII subtract 48
}while(buf[++i]); // Loop till last but one
int x[6];
int n=123456;
int i=0;
while(n>0){
x[i]=n%10;
n=n/10;
i++;
}
Here are teh steps. First, get the size needed to store all the digits in the number -- do a malloc of an array. Next, take the mod of the number and then divide the number by 10. Keep doing this till you exhaust all digits in the number.