This question already has answers here:
How do you store an arbitrarily large integer value in memory?
(8 answers)
Closed 2 years ago.
This is a fairly simple question:
How to get 2 unsigned integer values and add them in C for the following input constraint.
0 <= A, B <= 10^98
I know I can take the input as maybe a string but still I had this question in mind.
I have written the below code but it will not be able to take such a big value of 10^98
#include <stdio.h>
int main() {
unsigned long long int a, b;
int x = scanf("%llu%llu", &a, &b);
while (x > 0) {
printf("%llu\n",(a + b));
x = scanf("%llu%llu" ,&a, &b);
}
return 0;
}
You cannot use regular arithmetic types for this, even unsigned long long only has 64 bits which is much less than required for your purpose.
Also note that your loop test is incorrect: it should be while (x == 2)
Here is a small program that can handle numbers with 99 decimal digits:
#include <stdio.h>
#include <string.h>
char *bigadd(char *c, const char *a, const char *b) {
size_t alen = strlen(a);
size_t blen = strlen(b);
size_t clen = (alen > blen) ? alen : blen;
size_t i = clen;
int digit, carry = 0;
c[i] = '\0';
while (i > 0) {
digit = ((alen ? a[--alen] - '0' : 0) +
(blen ? b[--blen] - '0' : 0) +
carry);
carry = digit >= 10;
c[--i] = (char)('0' + (digit - carry * 10));
}
if (carry) {
memmove(c + 1, c, clen + 1);
c[0] = '1';
}
return c;
}
int main(int argc, char *argv[]) {
char a[100], b[100], c[101];
while (scanf("%99s%99s", a, b) == 2) {
printf("%s\n", bigadd(c, a, b));
}
return 0;
}
Try a bignum library such as GMP. Here's an example.
#include <gmp.h>
#include <stdio.h>
#include <assert.h>
int main(){
char inputStr[1024];
/*
mpz_t is the type defined for GMP integers.
It is a pointer to the internals of the GMP integer data structure
*/
mpz_t n;
int flag;
printf ("Enter your number: ");
scanf("%1023s" , inputStr); /* NOTE: never every write a call scanf ("%s", inputStr);
You are leaving a security hole in your code. */
/* 1. Initialize the number */
mpz_init(n);
mpz_set_ui(n,0);
/* 2. Parse the input string as a base 10 number */
flag = mpz_set_str(n,inputStr, 10);
assert (flag == 0); /* If flag is not 0 then the operation failed */
/* Print n */
printf ("n = ");
mpz_out_str(stdout,10,n);
printf ("\n");
/* 3. Add one to the number */
mpz_add_ui(n,n,1); /* n = n + 1 */
/* 4. Print the result */
printf (" n +1 = ");
mpz_out_str(stdout,10,n);
printf ("\n");
/* 5. Square n+1 */
mpz_mul(n,n,n); /* n = n * n */
printf (" (n +1)^2 = ");
mpz_out_str(stdout,10,n);
printf ("\n");
/* 6. Clean up the mpz_t handles or else we will leak memory */
mpz_clear(n);
}
Related
I have to find the hamming distance between two codes.
For example if I input:
a= 10
b= 1010
Automatically a should be made equal to the length of the string b by appending 0's.
So the input should become:
a=0010
b=1010
But I'm getting instead:
a = 001010
b = 1010
Here is my code:
#include<stdio.h>
#include<string.h>
void main()
{
char a[20],b[20],len1,len2,i,diff,count=0,j;
printf("Enter the first binary string\n");
scanf("%s",a);
printf("Enter the second binary string\n");
scanf("%s",b);
len1 = strlen(a);
len2 = strlen(b);
if(len1>len2)
{
diff = len1-len2;
for(i=0;i<len1;i++)
{
b[i+diff]=b[i];
}
j=i+diff;
b[j]='\0';
for(i=0;i<diff;i++)
{
b[i]='0';
}
}
else
{
diff = len2-len1;
for(i=0;i<len2;i++)
{
a[i+diff]=a[i];
}
j=i+diff;
a[j]='\0';
for(i=0;i<diff;i++)
{
a[i]='0';
}
}
printf("\nCodes are\n");
printf("a=%s\n",a);
printf("\nb=%s\n",b);
for(i=0;a[i]!='\0';i++)
{
if(a[i]!=b[i])
{
count++;
}
}
printf("hammung distance between two code word is %d\n",count);
}
Can anyone help me to fix this issue?
In your two for loop where you are moving the content of your old tab to the right to insert the zeros, you inverted the lengths.
First loop should be:
for(i=0;i<len2;i++)
{
b[i+diff]=b[i];
}
And second:
for(i=0;i<len1;i++)
{
a[i+diff]=a[i];
}
After trying it:
Codes are
a=0010
b=1010
hammung distance between two code word is 1
Also, the main function should return an int, not void. As stated in the comments, you should also change the type of your len1, len2, i, diff, count and j because you use them as number values, not as characters. You can for instance either use the int or size_t types for that.
int main()
{
char a[20],b[20];
int len1, len2, i, diff, count=0, j;
// Rest of your code
}
Here is a method that does not prepend zeros to the shortest binary string, and avoids the limitations of strtol() by comparing the elements of the string directly, starting with the last characters. The intricacies of using strtol() are traded for more complexity in handling the array indices. Note that care must be taken to avoid counting down to a negative value since size_t types are used. This method is not limited by the capacity of long types, but rather by size_t.
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[20], b[20];
printf("Enter first binary string: ");
scanf("%19s", a);
printf("Enter second binary string: ");
scanf("%19s", b);
size_t a_len = strlen(a);
size_t b_len = strlen(b);
size_t max_len = a_len > b_len ? a_len : b_len;
size_t hamming_dist = 0;
for (size_t i = 0; i < max_len; i++) {
if (a_len - i > 0 && b_len - i > 0) {
if (a[a_len - i - 1] == b[b_len - i - 1]) {
continue;
}
}
if ((a_len - i > 0 && a[a_len - i - 1] == '1') ||
(b_len - i > 0 && b[b_len - i - 1] == '1')) {
++hamming_dist;
}
}
printf("bstring_1: %s\n", a);
printf("bstring_2: %s\n", b);
printf("Hamming distance: %zu\n", hamming_dist);
return 0;
}
A way that doesn't need to pad one of the parameters with zeroes:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char *a = "1010";
char *b = "10";
long unsigned int xorab;
unsigned int hammingDistance = 0;
xorab = strtoul(a, NULL, 2) ^ strtoul(b, NULL, 2);
while (xorab) {
hammingDistance += xorab & 1;
xorab >>= 1;
}
printf("%u\n", hammingDistance);
}
It uses strtoul to convert the binary strings to unsigned long int using a base 2, then you only have to use bitwise operators (xor, and, shift) to calculate the Hamming distance without to take care of the size difference.
Obviously, this way stops to work if you want to test binary strings with values greater than an unsigned long int.
A simple problem but I can't get documentation about this kind of format: I want to print a float in a Fortran scientific notation, with its integer part always being zero.
printf("%0.5E",data); // Gives 2.74600E+02
I want to print it like this:
.27460E+03
How can I get this result as clean as possible?
If you only care about the integer part being 0 and not really leaving out the 0, i.e. if you're fine with 0.27460E+03 instead of .27460E+03 you could do something similar to this:
#include <stdio.h>
#include <stdlib.h>
void fortran_printf();
int main(void)
{
double num = 274.600;
fortran_printf(num);
exit(EXIT_SUCCESS);
}
void fortran_printf(double num)
{
int num_e = 0;
while (num > 1.0) {
num /= 10;
num_e++;
}
printf("%.5fE+%02d", num, num_e);
}
Otherwise you have to take a detour over strings. Note that the code above is only meant to get you started. It certainly doesn't handle any involved cases.
I tried doing this with log10() and pow(), but ended up having problems with rounding errors. So as commented by #Karoly Horvath, string manipulation is probably the best approach.
#include <stdlib.h>
char *fortran_sprintf_double(double x, int ndigits) {
char format[30], *p;
static char output[30];
/* Create format string (constrain number of digits to range 1–15) */
if (ndigits > 15) ndigits = 15;
if (ndigits < 1) ndigits = 1;
sprintf(format, "%%#.%dE", ndigits-1);
/* Convert number to exponential format (multiply by 10) */
sprintf(output, format, x * 10.0);
/* Move the decimal point one place to the left (divide by 10) */
for (p=output+1; *p; p++) {
if (*p=='.') {
*p = p[-1];
p[-1] = '.';
break;
}
}
return output;
}
A string manipulation approach:
int printf_NoIntegerPart(double x, int prec) {
char buf[20 + prec];
sprintf(buf, "%+.*E", prec - 1, x * 10.0); // use + for consistent width output
if (buf[2] == '.') {
buf[2] = buf[1];
buf[1] = '.';
}
puts(buf);
}
int main(void) {
printf_NoIntegerPart(2.74600E+02, 5); // --> +.27460E+03
}
This will print "INF" for |x| > DBL_MAX/10
printf() will not meet OP’s goal in one step using some special format. Using sprintf() to form the initial textual result is a good first step, care must be exercised when trying to do “math” with string manipulation.
Akin to #user3121023 deleted answer.
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
int printf_NoIntegerPart(double x, int prec) {
assert(prec >= 2 && prec <= 100);
char buffer[prec + 16]; // Form a large enough buffer.
sprintf(buffer, "%.*E", prec - 1, x);
int dp = '.'; // Could expand code here to get current local's decimal point.
char *dp_ptr = strchr(buffer, dp);
char *E_ptr = strchr(buffer, 'E');
// Insure we are not dealing with infinity, Nan, just the expected format.
if (dp_ptr && dp_ptr > buffer && E_ptr) {
// Swap dp and leading digit
dp_ptr[0] = dp_ptr[-1];
dp_ptr[-1] = dp;
// If x was not zero …
if (x != 0) {
int expo = atoi(&E_ptr[1]); // Could use `strtol()`
sprintf(&E_ptr[1], "%+.02d", expo + 1);
}
}
return puts(buffer);
}
int main(void) {
printf_NoIntegerPart(2.74600E+02, 5); // ".27460E+03"
return 0;
}
Faced same issue while fortran porting.
DId not found std C format :(
Implemented both approaches - with log10/pow and with string manipulation.
#include <ansi_c.h>
#define BUFFL 16
// using log10 , 3 digits after "."
char* fformat1(char* b, double a) {
int sign = 1;
double mant;
double order;
int ord_p1;
if (a<0) {
sign =-1;
a = -a;
}
order=log10 (a);
if (order >=0) ord_p1 = (int) order +1; // due sto property of int
else ord_p1 = (int) order;
mant=a/(pow(10,ord_p1));
sprintf(b,"%.3fE%+03d",mant,ord_p1);
if (sign==-1) b[0]='-';
return b;
}
// using string manipulation
char* fformat2(char* b, double a) {;
int sign = 1;
int i;
int N=3;
if (a<0) {
sign =-1;
a = -a;
}
sprintf(b,"%0.3E",a*10); // remember - we *10 to have right exponent
b[1]=b[0]; // 3.123 => .3123
b[0]='.';
for (i=N; i>=0; i--) // and shif all left
b[i+1]=b[i];
b[0]='0'; // pad with zero 0.312
if (sign==-1) b[0]='-'; // sign if needed
return b;
}
int main () {
char b1[BUFFL]; // allocate buffer outside.
char b2[BUFFL];
char b3[BUFFL];
char b4[BUFFL];
char b5[BUFFL];
printf("%s %s %s %s %s \n", fformat(b1,3.1), fformat(b2,-3.0), fformat(b3,3300.),
fformat(b4,0.03), fformat(b5,-0.000221));
printf("%s %s %s %s %s \n", fformat2(b1,3.1), fformat2(b2,-3.0), fformat2(b3,3300.),
fformat2(b4,0.03), fformat2(b5,-0.000221));
return 1;
}
I'm trying to write a function that parses an integer from a string representation.
My problem is that I don't know how to do this with one pass through the string. If I knew ahead of time that the input contained only characters in the range '0', '1', ..., '9' and that the string was of length n, I could of course calculate
character_1 * 10^(n-1) + character_2 * 10^(n-2) + .... + character_n * 10^0
but I want to deal with the general scenario as I've presented it.
I'm not looking for a library function, but an algorithm to achieve this in "pure C".
Here's the code I started from:
int parse_int (const char * c1, const char * c2, int * i)
{
/*
[c1, c2]: Range of characters in the string
i: Integer whose string representnation will be converted
Returns the number of characters parsed.
Exs. "2342kjsd32" returns 4, since the first 4 characters were parsed.
"hhsd3b23" returns 0
*/
int n = 0;
*i = 0;
while (c1!= c2)
{
char c = *c1;
if (c >= '0' && c <= '9')
{
}
}
return n;
}
Just as some of the comments and answers suggested, maybe a bit clearer: You have to "shift" the result "left" by multiplying it by 10 in every iteration before the addition of the new digit.
Indeed, this should remind us of Horner's method. As you have recognized, the result can be written like a polynomial:
result = c1 * 10^(n-1) + c2 * 10^(n-2) + ... + cn * 10^0
And this equation can be rewritten as this:
result = cn + 10*(... + 10*(c2 + 10*c1))
Which is the form this approach is based on. From the formula you can already see, that you don't need to know the power of 10 the first digit is to be multiplied by, directly from the start.
Here's an example:
#include <stdio.h>
int parse_int(const char * begin, const char * end, int * result) {
int d = 0;
for (*result = 0; begin != end; d++, begin++) {
int digit = *begin - '0';
if (digit >= 0 && digit < 10) {
*result *= 10;
*result += digit;
}
else break;
}
return d;
}
int main() {
char arr[] = "2342kjsd32";
int result;
int ndigits = parse_int(arr, arr+sizeof(arr), &result);
printf("%d digits parsed, got: %d\n", ndigits, result);
return 0;
}
The same can be achieved using sscanf(), for everyone that is fine with using the C standard library (can also handle negative numbers):
#include <stdio.h>
int main() {
char arr[] = "2342kjsd32";
int result, ndigits;
sscanf(arr, "%d%n", &result, &ndigits);
printf("%d digits parsed, got: %d\n", ndigits, result);
return 0;
}
The output is (both implementations):
$ gcc test.c && ./a.out
4 digits parsed, got: 2342
I think this is good solution to count parse character
int parse(char *str)
{
int k = 0;
while(*str)
{
if((*str >= '0') & (*str <= '9'))
break;
str++;
k++;
}
return k;
}
Here's a working version:
#include <stdio.h>
int parse_int (const char * c1, const char * c2, int * i)
{
/*
[c1, c2]: Range of characters in the string
i: Integer whose string representnation will be converted
Returns the number of characters parsed.
Exs. "2342kjsd32" returns 4, since the first 4 characters were parsed.
"hhsd3b23" returns 0
*/
int n = 0;
*i = 0;
for (; c1 != c2; c1++)
{
char c = *c1;
if (c >= '0' && c <= '9')
{
++n;
*i = *i * 10 + c - '0';
}
else
{
break;
}
}
return n;
}
int main()
{
int i;
char const* c1 = "2342kjsd32";
int n = parse_int(c1, c1+10, &i);
printf("n: %d, i: %d\n", n, i);
return 0;
}
Output:
n: 4, i: 2342
I want to write my own function in C for various reasons that converts an int to a string of decimals, just like sprintf.
Example:
int number = 254;
char string[10];
sprintf(string, "%d", number);
The string should look like this after the function I want to write:
[0] 2
[1] 5
[2] 4
[3] '\0'
Just to clarify even further, I want to program the actual convertion of an int to a string of chars. I don't want to use sprintf or itoa etc to do it. I want to know how it is done and be able to program it myself and do small adjustments to it.
Additional clarification:
I don't want to use itoa or sprintf, but pretty much program these myself.
#include <stdio.h>
#include <stdlib.h>
int i2a(char *s, int n){
div_t qr;
int pos;
if(n == 0) return 0;
qr = div(n, 10);
pos = i2a(s, qr.quot);
s[pos] = qr.rem + '0';
return pos + 1;
}
char* my_itoa(char *output_buff, int num){
char *p = output_buff;
if(num < 0){
*p++ = '-';
num *= -1;
} else if(num == 0)
*p++ = '0';
p[i2a(p, num)]='\0';
return output_buff;
}
int main(void){
int number = -254;
char string[12];
printf("%s", my_itoa(string, number));
return 0;
}
int number = 254;
int back=0,i=0;
char ch='\0';
char string[10];
for(i=0;(number/10)==0;i++){
back=number%10;
ch=(back*pow(10,i)+48);
string[i]=ch;
}
divide number per 10
get the rest( with % ) the rest is the digit you want starting from the right
cast the digit to char +48(ascii 0)
add to string
I want to know if there is a function in C library that convert a decimal to binary number and save number by number in a position on an array.
For example: 2 -> 10 -> array [0] = 0 array[1] = 1.
Thanks.
here:
void dec2bin(int c)
{
int i = 0;
for(i = 31; i >= 0; i--){
if((c & (1 << i)) != 0){
printf("1");
}else{
printf("0");
}
}
}
But this only prints the value of an integer in binary format. All data is represented in binary format internally anyway.
You did not define what is a decimal number for you. I am guessing it is character representation (e.g. in ASCII) of that number.
Notice that numbers are just numbers. Binary or decimal numbers do not exist, but a given number may have a binary, and a decimal, representation. Numbers are not made of digits!
Then you probably want sscanf(3) or strtol(3) pr atoi to convert a string to an integer (e.g. an int or a long), and snprintf(3) to convert an integer to a string.
If you want to convert a number to a binary string (with only 0 or 1 char-s in it) you need to code that conversion by yourself. To convert a binary string to some long use strtol.
There is no such function in C standard library. Anyway, you can write your own:
void get_bin(int *dst, intmax_t x);
Where dst is the resulting array (with 1s and 0s), and x is the decimal number.
For example:
C89 version:
#include <limits.h>
void get_bin(int *dst, int x)
{
int i;
for (i = sizeof x * CHAR_BIT - 1; i >= 0; --i)
*dst++ = x >> i & 1;
}
C99 version:
/* C99 version */
#include <limits.h>
#include <stdint.h>
void get_bin(int *dst, intmax_t x)
{
for (intmax_t i = sizeof x * CHAR_BIT - 1; i >= 0; --i)
*dst++ = x >> i & 1;
}
It works as follow: we run through the binary representation of x, from left to right. The expression (sizeof x * CHAR_BIT - 1) give the number of bits of x - 1. Then, we get the value of each bit (*dst++ = x >> i & 1), and push it into the array.
Example of utilisation:
void get_bin(int *dst, int x)
{
int i;
for (i = sizeof x * CHAR_BIT - 1; i >= 0; --i)
*dst++ = x >> i & 1;
}
int main(void)
{
int buf[128]; /* binary number */
int n = 42; /* decimal number */
unsigned int i;
get_bin(buf, n);
for (i = 0; i < sizeof n * CHAR_BIT; ++i)
printf("%d", buf[i]);
return 0;
}
Here is a version that explicitly uses a string buffer:
#include <string.h>
const char *str2bin(int num, char buffer[], const int BUFLEN)
{
(void) memset(buffer, '\0', BUFLEN );
int i = BUFLEN - 1; /* Index into buffer, running backwards. */
int r = 0; /* Remainder. */
char *p = &buffer[i - 1]; /* buffer[i] holds string terminator '\0'. */
while (( i >= 0 ) && ( num > 0 )) {
r = num % 2;
num = num / 2;
*p = r + '0';
i--;
p--;
}
return (p+1);
}
Use char * itoa ( int value, char * str, int base );
Find more here ...
the function should go like this:
int dec2bin(int n){
static int bin,osn=1,c;
if(n==0) return 0;
else {
c=n%2;
bin += c*osn;
osn*=10;
dec2bin(n/2);
}
return bin;
}
As far as i know there is no such function in any C library. But here's a recursive function that returns a binary representation of a decimal number as int:
int dec2bin(int n)
{
if(n == 0) return 0;
return n % 2 + 10 * dec2bin(n / 2);
}
The max number that it can represent is 1023 (1111111111 in binary) because of int data type limit, but you can substitute int for long long data type to increase the range. Then, you can store the return value to array like this:
int array[100], i = 0;
int n = dec2bin(some_number);
do{
array[i] = n % 10;
n /= 10;
i++;
}while(n > 10)
I know this is an old post, but i hope this will still help somebody!
If it helps you can convert any decimal to binary using bitset library, for example:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main(){
int decimal = 20;
bitset<5> binary20(decimal);
cout << binary20 << endl;
return 0;
}
So, you have an output like 10100. Bitsets also have a "toString()" method for any purpose.