Help! help!

Q

questions

Tell me what fault it is ?

when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.why?????


#include<stdio.h>
int main()
{ int x,y,z,m,n,w;

printf("Give me an integer\n");
scanf("%d",&x);

y=x/10000;
z=x%10000/1000;
m=x%1000/100;
n=x%100/10;
w=x%10;

printf("%d %d %d %d %d",y,z,m,n,w);

return 0;}
 
R

Richard Seriani

questions said:
Tell me what fault it is ?

when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.why?????


#include<stdio.h>
int main()
{ int x,y,z,m,n,w;

printf("Give me an integer\n");
scanf("%d",&x);

y=x/10000;
z=x%10000/1000;
m=x%1000/100;
n=x%100/10;
w=x%10;

printf("%d %d %d %d %d",y,z,m,n,w);

return 0;}

Convert 12345 to hex.
How many bytes do you need to store that number?
Convert 78569 to hex.
How many bytes do you need to store that number?

How many bytes in an int?

Richard
 
B

Ben Bacarisse

questions said:
Tell me what fault it is ?

when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.why?????

Not for me. I get 7 8 5 6 9. Presumably your system has INT_MAX less
than mine.
#include<stdio.h>
int main()
{ int x,y,z,m,n,w;

Try "long int" instead.
printf("Give me an integer\n");
scanf("%d",&x);

and "%ld" to match here
y=x/10000;
z=x%10000/1000;
m=x%1000/100;
n=x%100/10;
w=x%10;

printf("%d %d %d %d %d",y,z,m,n,w);

and here.
 
B

Bartc

questions said:
Tell me what fault it is ?

when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.why?????
y=x/10000;
z=x%10000/1000;
m=x%1000/100;
n=x%100/10;
w=x%10;

printf("%d %d %d %d %d",y,z,m,n,w);

Your question has been answered. However, you want to find out how to use
loops more. If you wanted more digits, you'd need a lot more typing with
your method. Just one example of many possibilties:

#include<stdio.h>

/* extract n'th digit of x (right-most is #1) */
int gd(int x, int n)
{ int d=1;

if (x<0) x=-x;
while (--n) d*=10;
return x%(d*10)/d; /* Using your method */
}

int main(void)
{ int i,x;
# define ndigits 5

printf("Give me an integer:\n");
scanf("%d",&x);

for (i=ndigits; i>=1; --i) printf("%d ",gd(x,i));
puts("");

return 0;
}
 
Q

questions

Convert 12345 to hex.
How many bytes do you need to store that number?
Convert 78569 to hex.
How many bytes do you need to store that number?

How many bytes in an int?

Richard- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -

5 bytes
 
Q

questions

Your question has been answered. However, you want to find out how to use
loops more. If you wanted more digits, you'd need a lot more typing with
your method. Just one example of many possibilties:

#include<stdio.h>

/* extract n'th digit of x (right-most is #1) */
int gd(int x, int n)
{ int d=1;

if (x<0) x=-x;
while (--n) d*=10;
return x%(d*10)/d; /* Using your method */

}

int main(void)
{ int i,x;
# define ndigits 5

printf("Give me an integer:\n");
scanf("%d",&x);

for (i=ndigits; i>=1; --i) printf("%d ",gd(x,i));
puts("");

return 0;

}

thank you
 
J

James Kuyper

questions said:

How did you arrive at that conclusion?

It's perfectly possible for the size of an int to be 5 bytes, but
extraordinarily unlikely. Given that your program didn't work as you
intended, it's certainly not the case on your machine.
 
B

Ben Bacarisse

James Kuyper said:
How did you arrive at that conclusion?

It's perfectly possible for the size of an int to be 5 bytes, but
extraordinarily unlikely. Given that your program didn't work as you
intended, it's certainly not the case on your machine.

Nit: that can't be deduced as certain. sizeof(int) can be 5 and still
have too few value bits to represent the larger of two numbers
originally presented (as I am sure you know).
 
M

mason

Tell me what fault it is ?

when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.why?????

#include<stdio.h>
int main()
{ int x,y,z,m,n,w;

printf("Give me an integer\n");
scanf("%d",&x);

y=x/10000;
z=x%10000/1000;
m=x%1000/100;
n=x%100/10;
w=x%10;

printf("%d %d %d %d %d",y,z,m,n,w);

return 0;}

int ai=12345;
int b1,b2,b3,b4,b5;

b5=ai/10000;
b4=(ai%10000)/1000;
b3=((ai%10000)%1000)/100;

b2=((ai%10000)%1000)%100/10;

b1=((ai%10000)%1000)%100%10/1;

printf("\n%d \t%d \t%d \t%d \t%d\t",b1,b2,b3,b4,b5);
 
T

Tony Quinn

In message
Tell me what fault it is ?

when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.why?????


#include<stdio.h>
int main()
{ int x,y,z,m,n,w;

printf("Give me an integer\n");
scanf("%d",&x);

y=x/10000;
z=x%10000/1000;
m=x%1000/100;
n=x%100/10;
w=x%10;

printf("%d %d %d %d %d",y,z,m,n,w);

return 0;}


Even I, as a rank amateur beginner, know this ...... the number that
you're inputting (78569) is larger than "int" on your system (where as
12345 isn't to big). I suspect that "int" on your system contains 8 bits
and can have a maximum of 32767, even *if* you were using "unsigned int"
you could input a maximum value of 65535 before overflow occurred

I think that you might need the data type "long". But of course I may
be wrong. It has been known
 
L

Lew Pitcher

In message
Tell me what fault it is ?

when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.why?????
[snip]
Even I, as a rank amateur beginner, know this ...... the number that
you're inputting (78569) is larger than "int" on your system (where as
12345 isn't to big). I suspect that "int" on your system contains 8 bits

ITYM 16 bits. 8 bits would only give a range of -128 to 127 (for signed
integer) or 0 - 255 (for unsigned integer)
and can have a maximum of 32767, e
Even *if* you were using "unsigned int"
you could input a maximum value of 65535 before overflow occurred

I think that you might need the data type "long". But of course I may
be wrong. It has been known

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
T

Tony Quinn

Lew said:
In message
Tell me what fault it is ?

when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.why?????
[snip]
Even I, as a rank amateur beginner, know this ...... the number that
you're inputting (78569) is larger than "int" on your system (where as
12345 isn't to big). I suspect that "int" on your system contains 8 bits

ITYM 16 bits. 8 bits would only give a range of -128 to 127 (for signed
integer) or 0 - 255 (for unsigned integer)
and can have a maximum of 32767, e
Even *if* you were using "unsigned int"
you could input a maximum value of 65535 before overflow occurred

I think that you might need the data type "long". But of course I may
be wrong. It has been known

See I told you that I'm a beginner and can be wrong, but my line of
thinking is correct (range overflow), is it not?
 
J

James Kuyper

Ben said:
Nit: that can't be deduced as certain. sizeof(int) can be 5 and still
have too few value bits to represent the larger of two numbers
originally presented (as I am sure you know).

Yes, I wasn't thinking about that. Demote that possibility from
"certain" to "nearly certain".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top