Help! help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • questions

    Help! help!

    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;}
  • Richard Seriani

    #2
    Re: Help! help!


    "questions" <mengqidhuang@y ahoo.cnwrote in message
    news:4ae55173-d77a-4f5b-930e-8e49aa139647@z6 g2000pre.google groups.com...
    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


    Comment

    • Ben Bacarisse

      #3
      Re: Help! help!

      questions <mengqidhuang@y ahoo.cnwrites:
      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.
      return 0;}
      --
      Ben.

      Comment

      • Mark McIntyre

        #4
        Re: Help! help!

        questions wrote:
        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);
        x is too large to fit into an int. if you want numbers greater than the
        size of an int on your PC, use a larger type such as long.

        Also: don't use scanf - its an unsafe function for beginners due to the
        ease with which you can create buffer overruns.

        --
        Mark McIntyre

        CLC FAQ <http://c-faq.com/>
        CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

        Comment

        • Bartc

          #5
          Re: Help! help!


          "questions" <mengqidhuang@y ahoo.cnwrote in message
          news:4ae55173-d77a-4f5b-930e-8e49aa139647@z6 g2000pre.google groups.com...
          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;
          }

          --
          bartc

          Comment

          • questions

            #6
            Re: Help! help!

            On 10ÔÂ25ÈÕ, ÏÂÎç6ʱ59·Ö, "Richard Seriani" <richard_s...@c ox.netwrote:
            "questions" <mengqidhu...@y ahoo.cnwrote in message
            >
            news:4ae55173-d77a-4f5b-930e-8e49aa139647@z6 g2000pre.google groups.com...
            >
            >
            >
            >
            >
            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- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
            >
            - ÏÔʾÒýÓõÄÎÄ×Ö -
            5 bytes

            Comment

            • questions

              #7
              Re: Help! help!

              On 10ÔÂ25ÈÕ, ÏÂÎç7ʱ20·Ö, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
              questions <mengqidhu...@y ahoo.cnwrites:
              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.
              >
              return 0;}
              >
              --
              Ben.
              thanks,thanks

              Comment

              • questions

                #8
                Re: Help! help!

                On 10ÔÂ25ÈÕ, ÏÂÎç7ʱ44·Ö, "Bartc" <b...@freeuk.co m>wrote:
                "questions" <mengqidhu...@y ahoo.cnwrote in message
                >
                news:4ae55173-d77a-4f5b-930e-8e49aa139647@z6 g2000pre.google groups.com...
                >
                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;
                >
                }
                >
                --
                bartc
                thank you

                Comment

                • James Kuyper

                  #9
                  Re: Help! help!

                  questions wrote:
                  On 10ÔÂ25ÈÕ, ÏÂÎç6ʱ59·Ö, "Richard Seriani" <richard_s...@c ox.netwrote:
                  >"questions" <mengqidhu...@y ahoo.cnwrote in message
                  >>
                  >news:4ae5517 3-d77a-4f5b-930e-8e49aa139647@z6 g2000pre.google groups.com...
                  >>
                  >>
                  >>
                  >>
                  >>
                  >>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<stdi o.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- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
                  >>
                  >- ÏÔʾÒýÓõÄÎÄ×Ö -
                  >
                  5 bytes
                  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.

                  Comment

                  • Ben Bacarisse

                    #10
                    Re: Help! help!

                    James Kuyper <jameskuyper@ve rizon.netwrites :
                    questions wrote:
                    >On 10月25日, 下午6时59分 , "Richard Seriani" <richard_s...@c ox.netwrote:
                    <snip>
                    >>How many bytes in an int?
                    <snip>
                    >5 bytes
                    >
                    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).

                    --
                    Ben.

                    Comment

                    • mason

                      #11
                      Re: Help! help!

                      On 10ÔÂ25ÈÕ, ÏÂÎç6ʱ45·Ö, questions <mengqidhu...@y ahoo.cnwrote:
                      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,b 3,b4,b5);

                      Comment

                      • Tony Quinn

                        #12
                        Re: Help! help!

                        In message
                        <4ae55173-d77a-4f5b-930e-8e49aa139647@z6 g2000pre.google groups.com>,
                        questions <mengqidhuang@y ahoo.cnwrites
                        >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
                        --
                        If one person has delusions, we call them psychotic. If, however, 1.5 billion
                        people have delusions we must apparently call them a religious group, and
                        respect their delusionary state.

                        Comment

                        • Lew Pitcher

                          #13
                          Re: Help! help!

                          On October 26, 2008 10:52, in comp.lang.c, Tony Quinn (tony@tqvideo.c o.uk)
                          wrote:
                          In message
                          <4ae55173-d77a-4f5b-930e-8e49aa139647@z6 g2000pre.google groups.com>,
                          questions <mengqidhuang@y ahoo.cnwrites
                          >>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. ------


                          Comment

                          • Tony Quinn

                            #14
                            Re: Help! help!

                            In message <a4233$49048663 $4c0a8c26$30318 @TEKSAVVY.COM-Free>, Lew
                            Pitcher <lpitcher@teksa vvy.comwrites
                            >On October 26, 2008 10:52, in comp.lang.c, Tony Quinn (tony@tqvideo.c o.uk)
                            >wrote:
                            >
                            >In message
                            ><4ae55173-d77a-4f5b-930e-8e49aa139647@z6 g2000pre.google groups.com>,
                            >questions <mengqidhuang@y ahoo.cnwrites
                            >>>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?

                            --
                            A 'Project Manager' is someone who, when told that the aircraft in which
                            he is currently a passenger is about to crash, demands that the pilot
                            stops all that tiresome struggling-with-the-controls-in-the-cockpit
                            stuff and instead attends a half-hour-long impact-assessment meeting.

                            Comment

                            • James Kuyper

                              #15
                              Re: Help! help!

                              Ben Bacarisse wrote:
                              James Kuyper <jameskuyper@ve rizon.netwrites :
                              >
                              >questions wrote:
                              >>On 10月25日, 下午6时59分 , "Richard Seriani" <richard_s...@c ox.netwrote:
                              <snip>
                              >>>How many bytes in an int?
                              <snip>
                              >>5 bytes
                              >How did you arrive at that conclusion?
                              >>
                              >It's perfectly possible for the size of an int to be 5 bytes, but
                              >extraordinaril y 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).
                              Yes, I wasn't thinking about that. Demote that possibility from
                              "certain" to "nearly certain".

                              Comment

                              Working...