Page 3 of 4

Posted: Tue Aug 09, 2005 3:21 pm
by ansiemens
kai wrote:I've got the following error with g++-2.95:

tmp.cc: In function `int main()':
tmp.cc:27: `uppercase' undeclared (first use this function)
tmp.cc:27: (Each undeclared identifier is reported only once
tmp.cc:27: for each function it appears in.)
but g++ 3.4.2 can work||
i change

Code: Select all

#include<string>
#include<iostream>
#include<sstream>
using namespace std;

int main() {
        string input;
        int loc_x,loc_neg;unsigned long d;
        cout.setf(ios::uppercase);
        while(cin>>input) {
                loc_neg=input.find("-",0);
                if(loc_neg!=string::npos) break;
                else {
                        loc_x=input.find("0x",0);
                        if(loc_x!=string::npos) {
                                istringstream(input) >> hex >> d;
                                cout << dec << d << endl;
                        }
                        else {
                            istringstream(input) >> dec >> d;
                                cout << "0x" << hex << d << endl;
                        }
                }
        }
        return 0;
}

but i got wa now
can anyone give me some test data?[/code]

10473_RTE plz help me

Posted: Thu Sep 22, 2005 7:57 pm
by mohsincsedu
I got run time error:)
but why..?
Here is my coding:

Code: Select all

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>

char table[] = "0123456789ABCDEF";
long hex_to_dec(char num[])
{
	long number = 0;
	long i,j;
	long len;
	len = strlen(num);
	for(i = len-1; i>=0; i--)
	{
		for(j = 0;j<16;j++)
		{
			if(table[j]==num[i])
				break;
		}
		number +=j*pow(16,len-i-1);
	}
	return number;
}

char *dec_to_hex(long num)
{
	char number[15];
	long temp;
	long i,j,k;
	i = 0;
	while(num > 0)
	{
		temp = num%16;
		num /= 16;
		number[i++] = table[temp];
	}
	number[i] = '\0';
	char m[15];
	k = strlen(number);
	for(i = k-1,j=0;i>=0;i--,j++)
		m[j] = number[i];
	m[j] = '\0';
	return m;
}

int main()
{
	char *num;
	char temp[16];
	int i,j,k;
	long number;

	while(gets(num)!=NULL)
	{
		if(num[0]=='-')
			break;
		if(num[0]=='0'&&num[1]=='x')
		{
			k = strlen(num);
			for(i=2,j=0;i<k;i++,j++)
				temp[j] = num[i];
			temp[j] = '\0';
			number = hex_to_dec(temp);
			printf("%ld\n",number);
		}
		else
		{
			number = atol(num);
			num = dec_to_hex(number);
			printf("0x%s\n",num);
		}
	}
	return 0;
}

Posted: Tue Nov 22, 2005 6:43 am
by Roby
Maybe your table declaration that makes your program got RTE.

Code: Select all

char table[] = "0123456789ABCDEF";
Since I've coded with C/C++ for a long time, I never declare an array without its maximum capacity (I mean the index) and I know it would make some errors if I compile it with some compiler or editor. So, never declare an array without its capacity. Better you declare like this:

Code: Select all

#define MAX 20

char table[MAX] = { "0123456789ABCDEF" };
And here are some input for you test:

Code: Select all

4
7
44
0x80685
0xABCD
0x9F9F
-1
Hope it helps you :D

Posted: Tue Nov 22, 2005 12:58 pm
by mamun
char table[] = "0123456789ABCDEF";
Actually this is OK. Compiler allocates memory for unallocated array when initialized. Your problem is in main()
char *num;
where you need to allocate memory like
char *num=new char [20];
Also in dec_to_hex(long num) fuction
char number[15];
Here increase the size, at least to 16.

Re: 10473 - Simple Base Conversion

Posted: Tue Oct 06, 2009 6:50 pm
by samin_yasar
hello, below is a very simple code of this problem.it provides the correct output for smaller numbers but not for big numbers.can
any one tell me why it is happening?

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main()
{
	
	int n;

	char num[100],*p;

	while(1)
	{
		gets(num);

		n = atoi(num);

		if(n<0)break;

		if(num[1]=='x')

		{
			sscanf(num,"%X",&n);
			
			printf("%d\n",n);
		}
		else
		{
			printf("0x%X\n",n);
		}
	}

	
	return 0;

}

Re: 10473 - Simple Base Conversion

Posted: Fri May 04, 2012 9:43 am
by mahade hasan
cut got AC!!

Re: 10473 - Simple Base Conversion

Posted: Fri May 04, 2012 9:16 pm
by brianfry713
You're not printing a newline at the end.

Re: 10473 - Simple Base Conversion

Posted: Sat Oct 20, 2012 12:59 pm
by gr81
Is there any special test case, which is failing my submitted programs...

here is output with my program..

Input
4
7
44
0x80685
0x9f9f
1234567
0
0x0
0XFFFFFFFF
23342342234
-1


Output
0x4
0x7
0x2C
525957
40863
0x12D687
0x0
0
-1
0x6F4FE05A


Help...

Re: 10473 - Simple Base Conversion

Posted: Sat Oct 20, 2012 1:05 pm
by gr81
code AC, terminated digit should be -ve number, i was considering -1.

10473 - Simple Base Conversion Question

Posted: Fri Dec 14, 2012 5:21 pm
by laituanksa245
Why does Uvatoolkit answer 1867505754 for the input "0x56F4FE05A" ?
Shouldn't the answer be 23342342234 ?

By the way, what is wrong with my code ? I keep getting WA

Code: Select all

Code removed.
Got AC :D

Re: 10473 - Simple Base Conversion Question

Posted: Fri Dec 14, 2012 8:04 pm
by brianfry713
Yes 0x56F4FE05A equals 23342342234, but the problem statement requires the decimal value of the input to be less than 2147483648.

Don't print a blank line at the end of the output.

Re: 10473 - Simple Base Conversion Question

Posted: Sat Dec 15, 2012 2:09 am
by laituanksa245
@brianfry713: Thanks very much

Re: 10473 - Simple Base Conversion Question

Posted: Thu Feb 21, 2013 7:04 am
by kier.guevara

Code: Select all

AC
I'm getting WA with this easy problem..Are there special cases for this prob?

Re: 10473 - Simple Base Conversion Question

Posted: Thu Feb 21, 2013 12:21 pm
by shuvokr
Try this I/O :::
Input ::

Code: Select all

0x56F4FE05A
-1
Output ::

Code: Select all

23342342234
But your program's output is 23342284890 which is wrong.

Sorry for my poor English.

Re: 10473 - Simple Base Conversion Question

Posted: Thu Feb 21, 2013 12:51 pm
by lbv
kier.guevara wrote: I'm getting WA with this easy problem..Are there special cases for this prob?
From the problem statement: "Hexadecimal numbers always starts with a `0x' and all other numbers are to be considered as decimal numbers."

Try for example:

Input

Code: Select all

000
0033
-42
Output

Code: Select all

0x0
0x21
shuvokr wrote: Try this I/O :::
0x56F4FE05A would be invalid, and I don't think there any test cases like that. From the input specification: "The decimal value of this number will be less than 2^31."