mafendee
asked on
to convert 100 decimal digit integer to binary, ternary and others
dear all,
i am trying to find a program in c/c++ which is capable of taking input of 100 decimal digit and convert to other radix base number.
does anyone out there who can help me out to solve this problem?
i would hugely appreciate that! Thanx.
i am trying to find a program in c/c++ which is capable of taking input of 100 decimal digit and convert to other radix base number.
does anyone out there who can help me out to solve this problem?
i would hugely appreciate that! Thanx.
You also posted in the C# section, is C# ok? Shouldn't be too hard to convert to C++. Just created, tested, might be not totally free of errors and if performance is important, you might want to use a stringbuilder instead. Works for bases of 1-16, but easily extensible:
private string ConvertToRadix(int number, int radix)
{
string[] digits = new string[] {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
if (radix < 1 || radix > 16)
throw new ArgumentOutOfRangeException("Radix must be between 1 and 16 inclusive");
if (radix == 1)
return new string('1', number);
string retVal = "";
for (int i = number; i > 0; i = i / radix)
{
int rest = i % radix;
retVal = digits[rest] + retVal;
}
return retVal;
}
For your convenience, output for number = 12345:
{02}-base: 11000000111001
{03}-base: 121221020
{04}-base: 3000321
{05}-base: 343340
{06}-base: 133053
{07}-base: 50664
{08}-base: 30071
{09}-base: 17836
{10}-base: 12345
{11}-base: 9303
{12}-base: 7189
{13}-base: 5808
{14}-base: 46db
{15}-base: 39d0
{16}-base: 3039
{02}-base: 11000000111001
{03}-base: 121221020
{04}-base: 3000321
{05}-base: 343340
{06}-base: 133053
{07}-base: 50664
{08}-base: 30071
{09}-base: 17836
{10}-base: 12345
{11}-base: 9303
{12}-base: 7189
{13}-base: 5808
{14}-base: 46db
{15}-base: 39d0
{16}-base: 3039
ASKER
abel, i am not much into C# and do you think your program works with big integers.
sunnycoder, i have installed the gmp but am having a problem when trying to compile the example program. example program is below and the error as well. any idea why?
=======================exa mple program=================== ========== ========== =
#include <stdlib.h> /* Standard Lib ISOC */
#include <stdio.h> /* I/O lib ISOC */
#include <stdarg.h> /* Variable args ISOC */
#include <gmp.h> /* GNU GMP Library */
using namespace std;
// 6005662386412099
// 600566238641209
// 31074182404900437213507500 3588856793 0037346022 8427275457 2016194882 3206440518 0815045563 4682967172 3286782437 9162728380 3341547107 3108501919 5485290073 3772482278 3525742386 4540146917 3660247765 2346609
int main(int argc, char *argv[]);
int main(int argc, char *argv[]) {
mpz_t m, n, p, k;
int i,j;
// Get the number to play with
if(argc < 2) {
printf("No argument provided, using built in number\n");
mpz_init_set_str(m, "6005662386412099", 10);
} else {
mpz_init_set_str(m, argv[1], 10);
}
// Output the number we are working on
printf(" m: ");
mpz_out_str(stdout, 10, m);
printf("\n");
// Check for 2|m
if(mpz_divisible_ui_p(m, 2)) {
printf("FOUND p|m: 2\n");
exit(1);
}
// Compute k=SQRT(M)
mpz_init(k);
mpz_sqrt(k, m);
gmp_printf(" SQRT(m): %Zd\n", k);
// 2|SQRT(M) => p=SQRT(M)+5, else p=SQRT(M)+4
mpz_init(p);
if(mpz_divisible_ui_p(k, 2)) {
mpz_add_ui(p, k, 5);
} else {
mpz_add_ui(p, k, 4);
}
// Move DOWN from SQRT(m) by two looking for something that | m
mpz_init(n);
for(i=0,j=0;1 || i<10000000;i++) {
// Subtract 2, and test for |.
mpz_sub_ui(p, p, 2);
if(mpz_divisible_p(m, p)) {
gmp_printf("FOUND p|m: %Zd\n", p);
if(mpz_cmp_ui(p, 1) == 0)
printf("m was prime!\n");
exit(1);
}
// Print status every 100K tests
j++;
if(j>100000) {
j=0;
mpz_sub(n, k, p);
gmp_printf(" CHECKED: %Zd\n", n);
}
}
// If we got here, then we didn't find the value..
printf("No Factors Found\n");
exit(0);
return 0;
}
========================== compilatio n error===================== ========== ==========
Compiler: Default compiler
Executing g++.exe...
g++.exe "D:\my program\tt.cpp" -o "D:\my program\tt.exe" -I"C:\Dev-Cpp\lib\gcc\ming w32\3.4.2\ include" -I"C:\Dev-Cpp\include\c++\ 3.4.2\back ward" -I"C:\Dev-Cpp\include\c++\ 3.4.2\ming w32" -I"C:\Dev-Cpp\include\c++\ 3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
D:\my program\tt.cpp:4:60: gmp.h: No such file or directory
D:\my program\tt.cpp: In function `int main(int, char**)':
D:\my program\tt.cpp:14: error: `mpz_t' undeclared (first use this function)
D:\my program\tt.cpp:14: error: (Each undeclared identifier is reported only once for each function it appears in.)
D:\my program\tt.cpp:14: error: expected `;' before "m"
D:\my program\tt.cpp:20: error: `m' undeclared (first use this function)
D:\my program\tt.cpp:20: error: `mpz_init_set_str' undeclared (first use this function)
D:\my program\tt.cpp:27: error: `mpz_out_str' undeclared (first use this function)
D:\my program\tt.cpp:31: error: `mpz_divisible_ui_p' undeclared (first use this function)
D:\my program\tt.cpp:37: error: `k' undeclared (first use this function)
D:\my program\tt.cpp:37: error: `mpz_init' undeclared (first use this function)
D:\my program\tt.cpp:38: error: `mpz_sqrt' undeclared (first use this function)
D:\my program\tt.cpp:39: error: `gmp_printf' undeclared (first use this function)
D:\my program\tt.cpp:42: error: `p' undeclared (first use this function)
D:\my program\tt.cpp:44: error: `mpz_add_ui' undeclared (first use this function)
D:\my program\tt.cpp:50: error: `n' undeclared (first use this function)
D:\my program\tt.cpp:53: error: `mpz_sub_ui' undeclared (first use this function)
D:\my program\tt.cpp:54: error: `mpz_divisible_p' undeclared (first use this function)
D:\my program\tt.cpp:56: error: `mpz_cmp_ui' undeclared (first use this function)
D:\my program\tt.cpp:64: error: `mpz_sub' undeclared (first use this function)
Execution terminated
sunnycoder, i have installed the gmp but am having a problem when trying to compile the example program. example program is below and the error as well. any idea why?
=======================exa
#include <stdlib.h> /* Standard Lib ISOC */
#include <stdio.h> /* I/O lib ISOC */
#include <stdarg.h> /* Variable args ISOC */
#include <gmp.h> /* GNU GMP Library */
using namespace std;
// 6005662386412099
// 600566238641209
// 31074182404900437213507500
int main(int argc, char *argv[]);
int main(int argc, char *argv[]) {
mpz_t m, n, p, k;
int i,j;
// Get the number to play with
if(argc < 2) {
printf("No argument provided, using built in number\n");
mpz_init_set_str(m, "6005662386412099", 10);
} else {
mpz_init_set_str(m, argv[1], 10);
}
// Output the number we are working on
printf(" m: ");
mpz_out_str(stdout, 10, m);
printf("\n");
// Check for 2|m
if(mpz_divisible_ui_p(m, 2)) {
printf("FOUND p|m: 2\n");
exit(1);
}
// Compute k=SQRT(M)
mpz_init(k);
mpz_sqrt(k, m);
gmp_printf(" SQRT(m): %Zd\n", k);
// 2|SQRT(M) => p=SQRT(M)+5, else p=SQRT(M)+4
mpz_init(p);
if(mpz_divisible_ui_p(k, 2)) {
mpz_add_ui(p, k, 5);
} else {
mpz_add_ui(p, k, 4);
}
// Move DOWN from SQRT(m) by two looking for something that | m
mpz_init(n);
for(i=0,j=0;1 || i<10000000;i++) {
// Subtract 2, and test for |.
mpz_sub_ui(p, p, 2);
if(mpz_divisible_p(m, p)) {
gmp_printf("FOUND p|m: %Zd\n", p);
if(mpz_cmp_ui(p, 1) == 0)
printf("m was prime!\n");
exit(1);
}
// Print status every 100K tests
j++;
if(j>100000) {
j=0;
mpz_sub(n, k, p);
gmp_printf(" CHECKED: %Zd\n", n);
}
}
// If we got here, then we didn't find the value..
printf("No Factors Found\n");
exit(0);
return 0;
}
==========================
Compiler: Default compiler
Executing g++.exe...
g++.exe "D:\my program\tt.cpp" -o "D:\my program\tt.exe" -I"C:\Dev-Cpp\lib\gcc\ming
D:\my program\tt.cpp:4:60: gmp.h: No such file or directory
D:\my program\tt.cpp: In function `int main(int, char**)':
D:\my program\tt.cpp:14: error: `mpz_t' undeclared (first use this function)
D:\my program\tt.cpp:14: error: (Each undeclared identifier is reported only once for each function it appears in.)
D:\my program\tt.cpp:14: error: expected `;' before "m"
D:\my program\tt.cpp:20: error: `m' undeclared (first use this function)
D:\my program\tt.cpp:20: error: `mpz_init_set_str' undeclared (first use this function)
D:\my program\tt.cpp:27: error: `mpz_out_str' undeclared (first use this function)
D:\my program\tt.cpp:31: error: `mpz_divisible_ui_p' undeclared (first use this function)
D:\my program\tt.cpp:37: error: `k' undeclared (first use this function)
D:\my program\tt.cpp:37: error: `mpz_init' undeclared (first use this function)
D:\my program\tt.cpp:38: error: `mpz_sqrt' undeclared (first use this function)
D:\my program\tt.cpp:39: error: `gmp_printf' undeclared (first use this function)
D:\my program\tt.cpp:42: error: `p' undeclared (first use this function)
D:\my program\tt.cpp:44: error: `mpz_add_ui' undeclared (first use this function)
D:\my program\tt.cpp:50: error: `n' undeclared (first use this function)
D:\my program\tt.cpp:53: error: `mpz_sub_ui' undeclared (first use this function)
D:\my program\tt.cpp:54: error: `mpz_divisible_p' undeclared (first use this function)
D:\my program\tt.cpp:56: error: `mpz_cmp_ui' undeclared (first use this function)
D:\my program\tt.cpp:64: error: `mpz_sub' undeclared (first use this function)
Execution terminated
> abel, i am not much into C# and do you think your program works with big integers.
The function would work with any integer, but C#'s largest integer, native, is the 64 unsigned bit integer, which goes up to 18446744073709551616. Not sure what your task is, but if you need larger numbers, you can always use the BigInteger classes, free for download here: http://www.codeproject.com/KB/cs/biginteger.aspx (commercial packages also exist).
To work with 64 bit numbers, you'll have to change the function signature to:
private string ConvertToRadix(Int64 number, Int64 radix)
If you want the same code for C++ I can try to transform it. The task at hand shouldn't be too hard. Would be handy though to know what you need it for and what size the input can become.
The function would work with any integer, but C#'s largest integer, native, is the 64 unsigned bit integer, which goes up to 18446744073709551616. Not sure what your task is, but if you need larger numbers, you can always use the BigInteger classes, free for download here: http://www.codeproject.com/KB/cs/biginteger.aspx (commercial packages also exist).
To work with 64 bit numbers, you'll have to change the function signature to:
private string ConvertToRadix(Int64 number, Int64 radix)
If you want the same code for C++ I can try to transform it. The task at hand shouldn't be too hard. Would be handy though to know what you need it for and what size the input can become.
>D:\my program\tt.cpp:4:60: gmp.h: No such file or directory
It was unable to find the required header file ... Search and locate the directory where it has been installed.
It was unable to find the required header file ... Search and locate the directory where it has been installed.
Ah, I think I got it "to convert 100 decimal digit integer to binary, ternary and others" means: "integers of 100 digits" instead of "100 integers with decimal digits".
Here's the same sample code with the BigInteger class from the J# redistributable package (J# redistributable package 2nd ed download @ microsoft). You can also use the BigInteger class from CodeProject BigInteger.
While I was working on the transformation to big integers of the ConverToRadix method, I noticed that the J# redistributable already had this method in the BigInteger class, through toString(radix) (note that it is "toString", not "ToString"). Though the self-made code looks as below, one line would've been sufficient:
System.Console.WriteLine (new BigInteger(inputStringOfBi gInteger). toString(r adix));
The internal radix works from base 2 to base 36.
Here's the same sample code with the BigInteger class from the J# redistributable package (J# redistributable package 2nd ed download @ microsoft). You can also use the BigInteger class from CodeProject BigInteger.
While I was working on the transformation to big integers of the ConverToRadix method, I noticed that the J# redistributable already had this method in the BigInteger class, through toString(radix) (note that it is "toString", not "ToString"). Though the self-made code looks as below, one line would've been sufficient:
System.Console.WriteLine (new BigInteger(inputStringOfBi
The internal radix works from base 2 to base 36.
// put the following line on top of the file
// after creating a reference to vjslib.dll:
using java.math;
/// <summary>
/// Converts big integers to any radix between 1-16
/// </summary>
/// <param name="number">Any positive number in base 10 as string</param>
/// <param name="radix">A radix between 1 and 16 incl.</param>
/// <returns></returns>
private string ConvertToRadix(string number, int radix)
{
string[] digits = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
BigInteger biInput = new BigInteger(number);
BigInteger biRadix = new BigInteger(radix.ToString());
string retVal = "";
BigInteger[] divAndRem;
do
{
divAndRem = biInput.divideAndRemainder(biRadix);
retVal = digits[divAndRem[1].longValue()] + retVal;
biInput = divAndRem[0];
} while (divAndRem[0].bitCount() > 0);
return retVal;
}
ASKER
sunnycoder,
i changed the directory to "#include <C:/Dev-Cpp/gmp-4.2.4/gmp. h>" and now i got this error msg:
Compiler: Default compiler
Executing g++.exe...
g++.exe "D:\my program\tt1.cpp" -o "D:\my program\tt1.exe" -I"C:\Dev-Cpp\lib\gcc\ming w32\3.4.2\ include" -I"C:\Dev-Cpp\include\c++\ 3.4.2\back ward" -I"C:\Dev-Cpp\include\c++\ 3.4.2\ming w32" -I"C:\Dev-Cpp\include\c++\ 3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
D:\my program\tt1.cpp: In function `int main()':
D:\my program\tt1.cpp:5: error: `mpz_class' undeclared (first use this function)
D:\my program\tt1.cpp:5: error: (Each undeclared identifier is reported only once for each function it appears in.)
D:\my program\tt1.cpp:5: error: expected `;' before "a"
D:\my program\tt1.cpp:6: error: `a' undeclared (first use this function)
D:\my program\tt1.cpp:7: error: `b' undeclared (first use this function)
D:\my program\tt1.cpp:8: error: `c' undeclared (first use this function)
Execution terminated
-------------------------- ------
any idea???
i changed the directory to "#include <C:/Dev-Cpp/gmp-4.2.4/gmp.
Compiler: Default compiler
Executing g++.exe...
g++.exe "D:\my program\tt1.cpp" -o "D:\my program\tt1.exe" -I"C:\Dev-Cpp\lib\gcc\ming
D:\my program\tt1.cpp: In function `int main()':
D:\my program\tt1.cpp:5: error: `mpz_class' undeclared (first use this function)
D:\my program\tt1.cpp:5: error: (Each undeclared identifier is reported only once for each function it appears in.)
D:\my program\tt1.cpp:5: error: expected `;' before "a"
D:\my program\tt1.cpp:6: error: `a' undeclared (first use this function)
D:\my program\tt1.cpp:7: error: `b' undeclared (first use this function)
D:\my program\tt1.cpp:8: error: `c' undeclared (first use this function)
Execution terminated
--------------------------
any idea???
#include <gmpxx.h>
You'd also need these flags
-lgmpxx -lgmp
-lgmpxx -lgmp
ASKER
sunnycoder,
this is my program now (am trying with a short one).
#include <iostream>
#include <stdio.h>
#include <C:/Dev-Cpp/gmp-4.2.4/gmp. h>
#include <C:/Dev-Cpp/gmp-4.2.4/gmpx x.h>
using namespace std;
int main () {
mpz_class a, b, c;
a = 1234;
b = "-5678";
c = a+b;
cout << "sum is " << c << "\n";
cout << "absolute value is " << abs(c) << "\n";
cin >> a;
return 0;
}
An error msg i am getting is
Compiler: Default compiler
Executing g++.exe...
g++.exe "D:\myprogram\tt1.cpp" -o "D:\myprogram\tt1.exe" -I"C:\Dev-Cpp\lib\gcc\ming w32\3.4.2\ include" -I"C:\Dev-Cpp\include\c++\ 3.4.2\back ward" -I"C:\Dev-Cpp\include\c++\ 3.4.2\ming w32" -I"C:\Dev-Cpp\include\c++\ 3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
In file included from D:\myprogram\tt1.cpp:4:
C:/Dev-Cpp/gmp-4.2.4/gmpxx .h:41:17: gmp.h: No such file or directory
Execution terminated
I am sure gmp.h and gmpxx.h are inside the directory i have included.
The command i used to compile
gcc d:/myprogram/tt1.cpp -lgmpxx -lgmp
...need help still.
this is my program now (am trying with a short one).
#include <iostream>
#include <stdio.h>
#include <C:/Dev-Cpp/gmp-4.2.4/gmp.
#include <C:/Dev-Cpp/gmp-4.2.4/gmpx
using namespace std;
int main () {
mpz_class a, b, c;
a = 1234;
b = "-5678";
c = a+b;
cout << "sum is " << c << "\n";
cout << "absolute value is " << abs(c) << "\n";
cin >> a;
return 0;
}
An error msg i am getting is
Compiler: Default compiler
Executing g++.exe...
g++.exe "D:\myprogram\tt1.cpp" -o "D:\myprogram\tt1.exe" -I"C:\Dev-Cpp\lib\gcc\ming
In file included from D:\myprogram\tt1.cpp:4:
C:/Dev-Cpp/gmp-4.2.4/gmpxx
Execution terminated
I am sure gmp.h and gmpxx.h are inside the directory i have included.
The command i used to compile
gcc d:/myprogram/tt1.cpp -lgmpxx -lgmp
...need help still.
Strange, it doesn't show that whole code that I cut 'n' pasted there (the vertical bar doesn't show int he code snippet section).
I understand from your other comments that you'll to stick to C++, so I'll leave you to it. But if you want to switch at some point to use C#, which has it already built in, check above and below:
System.Console.WriteLine (new BigInteger(inputStringOfBi gInteger). toString(r adix));
OR:
private string ConvertToRadix(string number, int radix)
{
string[] digits = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
BigInteger biInput = new BigInteger(number);
BigInteger biRadix = new BigInteger(radix.ToString( ));
string retVal = "";
BigInteger[] divAndRem;
do
{
divAndRem = biInput.divideAndRemainder (biRadix);
retVal = digits[divAndRem[1].longVa lue()] + retVal;
biInput = divAndRem[0];
} while (divAndRem[0].bitCount() > 0);
return retVal;
}
I understand from your other comments that you'll to stick to C++, so I'll leave you to it. But if you want to switch at some point to use C#, which has it already built in, check above and below:
System.Console.WriteLine (new BigInteger(inputStringOfBi
OR:
private string ConvertToRadix(string number, int radix)
{
string[] digits = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
BigInteger biInput = new BigInteger(number);
BigInteger biRadix = new BigInteger(radix.ToString(
string retVal = "";
BigInteger[] divAndRem;
do
{
divAndRem = biInput.divideAndRemainder
retVal = digits[divAndRem[1].longVa
biInput = divAndRem[0];
} while (divAndRem[0].bitCount() > 0);
return retVal;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>C:/Dev-Cpp/gmp-4.2.4/gmpx x.h:41:17: gmp.h: No such file or directory
>I am sure gmp.h and gmpxx.h are inside the directory i have included.
Something is amiss. Are both files in the same directory? It obviously found gmpxx.h but could not find gmp.h
>I am sure gmp.h and gmpxx.h are inside the directory i have included.
Something is amiss. Are both files in the same directory? It obviously found gmpxx.h but could not find gmp.h
Since you're using Dev-C++, why don't you make use of their package system ? It's a lot easier, and installs in the standard directories, avoiding the need to specify full paths.
Here's the DevPak for GMP v4.2.4 :
http://devpaks.org/details.php?devpak=247
Here's the DevPak for GMP v4.2.4 :
http://devpaks.org/details.php?devpak=247
ASKER
Infinity08,
now i have installed GMP package (ONLY) from package manager window. is there any installation guidelines or so? is there any other i should have installed?
anyway, when i compile this program
#include <stdio.h>
#include <iostream>
#include <gmp.h>
#include <gmpxx.h>
using namespace std;
int main () {
mpz_class a, b, c;
a = 1234;
b = "-5678";
c = a+b;
cout << "sum is " << c << "\n";
cout << "absolute value is " << abs(c) << "\n";
cin >> a;
return 0;
}
I got the following error message
Compiler: Default compiler
Executing g++.exe...
g++.exe "D:\myprogram\tt1.cpp" -o "D:\myprogram\tt1.exe" -I"C:\Dev-Cpp\lib\gcc\ming w32\3.4.2\ include" -I"C:\Dev-Cpp\include\c++\ 3.4.2\back ward" -I"C:\Dev-Cpp\include\c++\ 3.4.2\ming w32" -I"C:\Dev-Cpp\include\c++\ 3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cEVaaaa.o( .text$_ZN1 0__gmp_exp rIA1_12__m pz_structS 1_ED1Ev[__ gmp_expr<_ _mpz_struc t [1], __mpz_struct [1]>::~__gmp_expr()]+0xd): tt1.cpp: undefined reference to `__gmpz_clear'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cEVaaaa.o( .text$_ZN1 0__gmp_exp rIA1_12__m pz_structS 1_EaSEPKc[ __gmp_expr <__mpz_str uct [1], __mpz_struct [1]>::operator=(char const*)]+0x55):tt1.cpp: undefined reference to `__gmpz_set_str'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cEVaaaa.o( .text$_ZN1 0__gmp_exp rIA1_12__m pz_structS 1_EaSEi[__ gmp_expr<_ _mpz_struc t [1], __mpz_struct [1]>::operator=(int)]+0x14 ):tt1.cpp: undefined reference to `__gmpz_set_si'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cEVaaaa.o( .text$_ZN1 0__gmp_exp rIA1_12__m pz_structS 1_EC1Ev[__ gmp_expr<_ _mpz_struc t [1], __mpz_struct [1]>::__gmp_expr()]+0xd):t t1.cpp: undefined reference to `__gmpz_init'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cEVaaaa.o( .text$_Zls IA1_12__mp z_structER SoS2_RK10_ _gmp_exprI T_S4_E[std ::basic_os tream<char , std::char_traits<char> >& operator<< <__mpz_struct [1]>(std::basic_ostream<ch ar, std::char_traits<char> >&, __gmp_expr<__mpz_struct [1], __mpz_struct [1]> const&)]+0x1c):tt1.cpp: undefined reference to `operator<<(std::ostream&, __mpz_struct const*)'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cEVaaaa.o( .text$_Zls IA1_12__mp z_struct16 __gmp_unar y_exprI10_ _gmp_exprI S1_S1_E18_ _gmp_abs_f unctionEER SoS7_RKS3_ IT_T0_E[st d::basic_o stream<cha r, std::char_traits<char> >& operator<< <__mpz_struct [1], __gmp_unary_expr<__gmp_exp r<__mpz_st ruct [1], __mpz_struct [1]>, __gmp_abs_function> >(std::basic_ostream<char, std::char_traits<char> >&, __gmp_expr<__mpz_struct [1], __gmp_unary_expr<__gmp_exp r<__mpz_st ruct [1], __mpz_struct [1]>, __gmp_abs_function> > const&)]+0x6b):tt1.cpp: undefined reference to `operator<<(std::ostream&, __mpz_struct const*)'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cEVaaaa.o( .text$_Zrs IA1_12__mp z_structER SiS2_R10__ gmp_exprIT _S4_E[std: :basic_ist ream<char, std::char_traits<char> >& operator>><__mpz_struct [1]>(std::basic_istream<ch ar, std::char_traits<char> >&, __gmp_expr<__mpz_struct [1], __mpz_struct [1]>&)]+0x1c):tt1.cpp: undefined reference to `operator>>(std::istream&, __mpz_struct*)'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cEVaaaa.o( .text$_ZN1 0__gmp_exp rIA1_12__m pz_structS 1_EC1IS1_1 6__gmp_una ry_exprIS2 _18__gmp_a bs_functio nEEERKS_IT _T0_E[__gm p_expr<__m pz_struct [1], __mpz_struct [1]>::__gmp_expr<__mpz_str uct [1], __gmp_unary_expr<__gmp_exp r<__mpz_st ruct [1], __mpz_struct [1]>, __gmp_abs_function> >(__gmp_expr<__mpz_struct [1], __gmp_unary_expr<__gmp_exp r<__mpz_st ruct [1], __mpz_struct [1]>, __gmp_abs_function> > const&)]+0xd):tt1.cpp: undefined reference to `__gmpz_init'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cEVaaaa.o( .text$_ZN1 7__gmp_bin ary_plus4e valEP12__m pz_structP KS0_S3_[__ gmp_binary _plus::eva l(__mpz_st ruct*, __mpz_struct const*, __mpz_struct const*)]+0x1b):tt1.cpp: undefined reference to `__gmpz_add'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cEVaaaa.o( .text$__gm pz_abs[___ gmpz_abs]+ 0x1d):tt1. cpp: undefined reference to `__gmpz_set'
collect2: ld returned 1 exit status
Execution terminated
-------------------------- --------
gmp.h and gmpxx.h were installed inside C:/Dev-Cpp/include/
any ideas?
now i have installed GMP package (ONLY) from package manager window. is there any installation guidelines or so? is there any other i should have installed?
anyway, when i compile this program
#include <stdio.h>
#include <iostream>
#include <gmp.h>
#include <gmpxx.h>
using namespace std;
int main () {
mpz_class a, b, c;
a = 1234;
b = "-5678";
c = a+b;
cout << "sum is " << c << "\n";
cout << "absolute value is " << abs(c) << "\n";
cin >> a;
return 0;
}
I got the following error message
Compiler: Default compiler
Executing g++.exe...
g++.exe "D:\myprogram\tt1.cpp" -o "D:\myprogram\tt1.exe" -I"C:\Dev-Cpp\lib\gcc\ming
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
collect2: ld returned 1 exit status
Execution terminated
--------------------------
gmp.h and gmpxx.h were installed inside C:/Dev-Cpp/include/
any ideas?
-lgmpxx -lgmp
ASKER
sunnycoder,
when i compile the program from command prompt using "g++ d:/myprogram/tt1.cpp -lgmpxx -lgmp", there is no error msg. but if i compile from Dev-C++ 4.9.9.2 environment i got this error msg
Compiler: Default compiler
Executing g++.exe...
g++.exe "D:\myprogram\tt1.cpp" -o "D:\myprogram\tt1.exe" -I"C:\Dev-Cpp\lib\gcc\ming w32\3.4.2\ include" -I"C:\Dev-Cpp\include\c++\ 3.4.2\back ward" -I"C:\Dev-Cpp\include\c++\ 3.4.2\ming w32" -I"C:\Dev-Cpp\include\c++\ 3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cU9baaa.o( .text$_ZN1 0__gmp_exp rIA1_12__m pz_structS 1_ED1Ev[__ gmp_expr<_ _mpz_struc t [1], __mpz_struct [1]>::~__gmp_expr()]+0xd): tt1.cpp: undefined reference to `__gmpz_clear'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cU9baaa.o( .text$_ZN1 0__gmp_exp rIA1_12__m pz_structS 1_EaSEPKc[ __gmp_expr <__mpz_str uct [1], __mpz_struct [1]>::operator=(char const*)]+0x55):tt1.cpp: undefined reference to `__gmpz_set_str'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cU9baaa.o( .text$_ZN1 0__gmp_exp rIA1_12__m pz_structS 1_EaSEi[__ gmp_expr<_ _mpz_struc t [1], __mpz_struct [1]>::operator=(int)]+0x14 ):tt1.cpp: undefined reference to `__gmpz_set_si'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cU9baaa.o( .text$_ZN1 0__gmp_exp rIA1_12__m pz_structS 1_EC1Ev[__ gmp_expr<_ _mpz_struc t [1], __mpz_struct [1]>::__gmp_expr()]+0xd):t t1.cpp: undefined reference to `__gmpz_init'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cU9baaa.o( .text$_Zls IA1_12__mp z_structER SoS2_RK10_ _gmp_exprI T_S4_E[std ::basic_os tream<char , std::char_traits<char> >& operator<< <__mpz_struct [1]>(std::basic_ostream<ch ar, std::char_traits<char> >&, __gmp_expr<__mpz_struct [1], __mpz_struct [1]> const&)]+0x1c):tt1.cpp: undefined reference to `operator<<(std::ostream&, __mpz_struct const*)'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cU9baaa.o( .text$_Zls IA1_12__mp z_struct16 __gmp_unar y_exprI10_ _gmp_exprI S1_S1_E18_ _gmp_abs_f unctionEER SoS7_RKS3_ IT_T0_E[st d::basic_o stream<cha r, std::char_traits<char> >& operator<< <__mpz_struct [1], __gmp_unary_expr<__gmp_exp r<__mpz_st ruct [1], __mpz_struct [1]>, __gmp_abs_function> >(std::basic_ostream<char, std::char_traits<char> >&, __gmp_expr<__mpz_struct [1], __gmp_unary_expr<__gmp_exp r<__mpz_st ruct [1], __mpz_struct [1]>, __gmp_abs_function> > const&)]+0x6b):tt1.cpp: undefined reference to `operator<<(std::ostream&, __mpz_struct const*)'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cU9baaa.o( .text$_Zrs IA1_12__mp z_structER SiS2_R10__ gmp_exprIT _S4_E[std: :basic_ist ream<char, std::char_traits<char> >& operator>><__mpz_struct [1]>(std::basic_istream<ch ar, std::char_traits<char> >&, __gmp_expr<__mpz_struct [1], __mpz_struct [1]>&)]+0x1c):tt1.cpp: undefined reference to `operator>>(std::istream&, __mpz_struct*)'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cU9baaa.o( .text$_ZN1 0__gmp_exp rIA1_12__m pz_structS 1_EC1IS1_1 6__gmp_una ry_exprIS2 _18__gmp_a bs_functio nEEERKS_IT _T0_E[__gm p_expr<__m pz_struct [1], __mpz_struct [1]>::__gmp_expr<__mpz_str uct [1], __gmp_unary_expr<__gmp_exp r<__mpz_st ruct [1], __mpz_struct [1]>, __gmp_abs_function> >(__gmp_expr<__mpz_struct [1], __gmp_unary_expr<__gmp_exp r<__mpz_st ruct [1], __mpz_struct [1]>, __gmp_abs_function> > const&)]+0xd):tt1.cpp: undefined reference to `__gmpz_init'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cU9baaa.o( .text$_ZN1 7__gmp_bin ary_plus4e valEP12__m pz_structP KS0_S3_[__ gmp_binary _plus::eva l(__mpz_st ruct*, __mpz_struct const*, __mpz_struct const*)]+0x1b):tt1.cpp: undefined reference to `__gmpz_add'
C:\DOCUME~1\mafendee\LOCAL S~1\Temp/c cU9baaa.o( .text$__gm pz_abs[___ gmpz_abs]+ 0x1d):tt1. cpp: undefined reference to `__gmpz_set'
collect2: ld returned 1 exit status
Execution terminated
-------------------------- ---------- ---------
i thought the compilation from the prompt was OK but when i changed the value of integer a = 123412412342141243134141 (very bit number) then i got this msg (from both methods of compilations)
Compiler: Default compiler
Executing g++.exe...
g++.exe "D:\myprogram\tt1.cpp" -o "D:\myprogram\tt1.exe" -I"C:\Dev-Cpp\lib\gcc\ming w32\3.4.2\ include" -I"C:\Dev-Cpp\include\c++\ 3.4.2\back ward" -I"C:\Dev-Cpp\include\c++\ 3.4.2\ming w32" -I"C:\Dev-Cpp\include\c++\ 3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
D:\myprogram\tt1.cpp:11:6: warning: integer constant is too large for its type
D:\myprogram\tt1.cpp: In function `int main()':
D:\myprogram\tt1.cpp:11: warning: this decimal constant is unsigned only in ISO C90
D:\myprogram\tt1.cpp:11: error: integer constant is too large for "long" type
D:\myprogram\tt1.cpp:11: error: ambiguous overload for 'operator=' in 'a = 0xa2a2282850abebbaull'
C:/Dev-Cpp/include/gmpxx.h :1605: note: candidates are: __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>& __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>::operator =(const __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>&)
C:/Dev-Cpp/include/gmpxx.h :1610: note: __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>& __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>::operator =(signed char)
C:/Dev-Cpp/include/gmpxx.h :1611: note: __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>& __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>::operator =(unsigned char)
C:/Dev-Cpp/include/gmpxx.h :1613: note: __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>& __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>::operator =(int)
C:/Dev-Cpp/include/gmpxx.h :1614: note: __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>& __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>::operator =(unsigned int)
C:/Dev-Cpp/include/gmpxx.h :1617: note: __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>& __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>::operator =(short int)
C:/Dev-Cpp/include/gmpxx.h :1619: note: __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>& __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>::operator =(short unsigned int)
C:/Dev-Cpp/include/gmpxx.h :1622: note: __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>& __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>::operator =(long int)
C:/Dev-Cpp/include/gmpxx.h :1624: note: __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>& __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>::operator =(long unsigned int)
C:/Dev-Cpp/include/gmpxx.h :1626: note: __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>& __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>::operator =(float)
C:/Dev-Cpp/include/gmpxx.h :1627: note: __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>& __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>::operator =(double)
C:/Dev-Cpp/include/gmpxx.h :1632: note: __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>& __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>::operator =(const char*) <near match>
C:/Dev-Cpp/include/gmpxx.h :1638: note: __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>& __gmp_expr<__mpz_struct[1] , __mpz_struct[1]>::operator =(const std::string&) <near match>
Execution terminated
-------------------------- ---------- ------
i am lost...
when i compile the program from command prompt using "g++ d:/myprogram/tt1.cpp -lgmpxx -lgmp", there is no error msg. but if i compile from Dev-C++ 4.9.9.2 environment i got this error msg
Compiler: Default compiler
Executing g++.exe...
g++.exe "D:\myprogram\tt1.cpp" -o "D:\myprogram\tt1.exe" -I"C:\Dev-Cpp\lib\gcc\ming
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
C:\DOCUME~1\mafendee\LOCAL
collect2: ld returned 1 exit status
Execution terminated
--------------------------
i thought the compilation from the prompt was OK but when i changed the value of integer a = 123412412342141243134141 (very bit number) then i got this msg (from both methods of compilations)
Compiler: Default compiler
Executing g++.exe...
g++.exe "D:\myprogram\tt1.cpp" -o "D:\myprogram\tt1.exe" -I"C:\Dev-Cpp\lib\gcc\ming
D:\myprogram\tt1.cpp:11:6:
D:\myprogram\tt1.cpp: In function `int main()':
D:\myprogram\tt1.cpp:11: warning: this decimal constant is unsigned only in ISO C90
D:\myprogram\tt1.cpp:11: error: integer constant is too large for "long" type
D:\myprogram\tt1.cpp:11: error: ambiguous overload for 'operator=' in 'a = 0xa2a2282850abebbaull'
C:/Dev-Cpp/include/gmpxx.h
C:/Dev-Cpp/include/gmpxx.h
C:/Dev-Cpp/include/gmpxx.h
C:/Dev-Cpp/include/gmpxx.h
C:/Dev-Cpp/include/gmpxx.h
C:/Dev-Cpp/include/gmpxx.h
C:/Dev-Cpp/include/gmpxx.h
C:/Dev-Cpp/include/gmpxx.h
C:/Dev-Cpp/include/gmpxx.h
C:/Dev-Cpp/include/gmpxx.h
C:/Dev-Cpp/include/gmpxx.h
C:/Dev-Cpp/include/gmpxx.h
C:/Dev-Cpp/include/gmpxx.h
Execution terminated
--------------------------
i am lost...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
now, i have done the program. i am thanking you all for the helps, though i didnt get full answer, your ideas and direction has guided me to the one. i have to split the points between your effort and my gain accordingly, i do hope it will satisfy everyone. cheers.
check Function: char * mpz_get_str (char *str, int base, mpz_t op)
http://gmplib.org/manual/Converting-Integers.html#Converting-Integers