decrementing arrays

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

    decrementing arrays

    I don't know if I'll need pointers for this or not. I wants numbers
    10^16. Like a credit card 16 digits of possible 10 numbers, so I guess that
    would be 10^16. So I have

    int num [16][10];
    These are of course declared and not initialized. Now I want to initialize
    them all with '\0'. That I'm guessing would involve while ( --). Or maybe
    for. Would pointers be involved in this?

    With this excercise I would learn working with multi-dimensional
    arrary, maybe pointers and initializing to zero. I tried to do this using
    putchar so I obviously don't know what to do. Can someone help me learn
    this?

    Bill

    -----
    "You Big Dummy" -- Fred Sanford


  • santosh

    #2
    Re: decrementing arrays

    Bill Cunningham wrote:
    I don't know if I'll need pointers for this or not. I wants numbers
    10^16. Like a credit card 16 digits of possible 10 numbers,
    Your question is not clear. Do you want an integer type that can store
    values upto 1e16? Do you want an array of 16 integers each of which can
    hold a ten digit decimal value?
    so I guess that would be 10^16. So I have
    >
    int num [16][10];
    These are of course declared and not initialized. Now I want to
    initialize them all with '\0'.
    Why? Why not just zero?
    That I'm guessing would involve while (
    --). Or maybe for. Would pointers be involved in this?
    Why should they be explicitly involved.
    With this excercise I would learn working with multi-dimensional
    arrary, maybe pointers and initializing to zero. I tried to do this
    using putchar so I obviously don't know what to do. Can someone help
    me learn this?
    So you want to set that array to zero? Do:

    for (int i = 0; i < 16; i++)
    for (int j = 0; j < 10; j++)
    num[i][j] = 0;

    Comment

    • Bill Cunningham

      #3
      Re: decrementing arrays


      "santosh" <santosh.k83@gm ail.comwrote in message
      news:g3h26k$444 $1@registered.m otzarella.org.. .
      So you want to set that array to zero? Do:
      >
      for (int i = 0; i < 16; i++)
      for (int j = 0; j < 10; j++)
      num[i][j] = 0;
      Wow thanks Santosh. I've seen things like this in code but the i's j's k's
      and so on start to blur after awhile and I just give up. So now I know what
      that means. What about randomizing ? So all is set to zero. Would srand be
      involved ?

      Bill

      -----
      "You big Dummy" --Fred Sanford
      >

      Comment

      • Bill Cunningham

        #4
        Re: decrementing arrays


        "santosh" <santosh.k83@gm ail.comwrote in message
        news:g3h26k$444 $1@registered.m otzarella.org.. .
        So you want to set that array to zero? Do:
        >
        for (int i = 0; i < 16; i++)
        for (int j = 0; j < 10; j++)
        num[i][j] = 0;
        Why is there a =0 after num [i][j] ?

        Bill

        -----
        "You big dummy" --Fred Sanford


        Comment

        • Ben Bacarisse

          #5
          Re: decrementing arrays

          "Bill Cunningham" <nospam@nspam.c omwrites:
          I don't know if I'll need pointers for this or not. I wants numbers
          10^16. Like a credit card 16 digits of possible 10 numbers, so I guess that
          would be 10^16. So I have
          >
          int num [16][10];
          That is an array of 16 arrays of 10 ints. To store ten things, each
          of which is 16 elements long you would write:

          T num[10][16];

          where T is the element type. int is not the obvious choice here.
          char is quite big enough to store a single digit. You will have
          trouble if you think these arrays will behave like strings. To so
          that you'd need 17 elements in each to allow for a null at the end of
          even the longest ones.
          These are of course declared and not initialized. Now I want to initialize
          them all with '\0'.
          That is an odd thing to initial an integer array with, but it works.
          It is the same as 0.
          That I'm guessing would involve while ( --). Or maybe
          for. Would pointers be involved in this?
          Not for initialising:

          int num[16][10] = {'\0'};

          and it is done.
          With this excercise I would learn working with multi-dimensional
          arrary, maybe pointers and initializing to zero. I tried to do this using
          putchar so I obviously don't know what to do. Can someone help me learn
          this?
          Are you sure you know enough about 1-dimensional arrays? I will be
          hard to learn about arrays of arrays if the base concept is at all
          hazy.

          --
          Ben.

          Comment

          • Bill Cunningham

            #6
            Re: decrementing arrays


            "Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
            news:87od5vvoqx .fsf@bsb.me.uk. ..
            "Bill Cunningham" <nospam@nspam.c omwrites:
            Would pointers be involved in this?
            >
            Not for initialising:
            >
            int num[16][10] = {'\0'};
            >
            and it is done.
            Are you sure Ben? I am not the one to be questioning you but I was told
            or atleast my understanding from the past has been to to this.

            Ok char
            char num[10]={0,0,0,0,0,0,0 ,0,0,0};

            but char num[10]={'\0'}; Is certainly easier.


            Bill
            -----
            "You big dummy" --Fred Sanford


            Comment

            • Bill Cunningham

              #7
              Re: decrementing arrays

              What if I wanted to look at the value of one element in the array. One
              dimensional and multi dimensional?

              printf("%c\n",n um[5]); ?

              Bill
              -----
              "You big dummy" --Fred Sanford


              Comment

              • Richard

                #8
                Re: decrementing arrays

                "Bill Cunningham" <nospam@nspam.c omwrites:
                I don't know if I'll need pointers for this or not. I wants numbers
                10^16. Like a credit card 16 digits of possible 10 numbers, so I guess that
                would be 10^16. So I have
                >
                int num [16][10];
                These are of course declared and not initialized. Now I want to initialize
                them all with '\0'. That I'm guessing would involve while ( --). Or maybe
                for. Would pointers be involved in this?
                >
                With this excercise I would learn working with multi-dimensional
                arrary, maybe pointers and initializing to zero. I tried to do this using
                putchar so I obviously don't know what to do. Can someone help me learn
                this?
                >
                Bill
                >
                -----
                "You Big Dummy" -- Fred Sanford
                You surprise me more and more.

                Firstly what you want is a single credit card number. Which is 16 digits
                of 0-9.

                I doubt very very much you are doing any calculations with this number.

                So simply store it as a string : zero terminated or not.

                char CCNum[] ="0123456789012 345"; /* zero terminated credit card number as a
                string */

                If you want to learn pointers simply go out and buy some and wave them
                around.


                Comment

                • santosh

                  #9
                  Re: decrementing arrays

                  Bill Cunningham wrote:
                  >
                  "santosh" <santosh.k83@gm ail.comwrote in message
                  news:g3h26k$444 $1@registered.m otzarella.org.. .
                  >
                  >So you want to set that array to zero? Do:
                  >>
                  > for (int i = 0; i < 16; i++)
                  > for (int j = 0; j < 10; j++)
                  > num[i][j] = 0;
                  >
                  Why is there a =0 after num [i][j] ?
                  Because it's the same as using '\0' and more logical. Use '\0' for char
                  arrays and 0 for arrays of short, int, long, long long etc., and use
                  NULL to initialise arrays of pointers.

                  Comment

                  • Bill Cunningham

                    #10
                    Re: decrementing arrays


                    "Richard" <rgrdev@gmail.c omwrote in message
                    news:g3h4l2$fem $1@registered.m otzarella.org.. .

                    You surprise me more and more.
                    Is that good or bad :-)
                    Firstly what you want is a single credit card number. Which is 16 digits
                    of 0-9.
                    >
                    I doubt very very much you are doing any calculations with this number.
                    >
                    So simply store it as a string : zero terminated or not.
                    What do you mean zero terminated or not? Doesn't C stick \0
                    automatically ? The only exception I know is strlen.

                    Bill

                    -----
                    "You big dummy" --Fred Sanford


                    Comment

                    • Ben Bacarisse

                      #11
                      Re: decrementing arrays

                      "Bill Cunningham" <nospam@nspam.c omwrites:
                      "Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
                      news:87od5vvoqx .fsf@bsb.me.uk. ..
                      >"Bill Cunningham" <nospam@nspam.c omwrites:
                      Would pointers be involved in this?
                      >
                      >>
                      >Not for initialising:
                      >>
                      >int num[16][10] = {'\0'};
                      >>
                      >and it is done.
                      >
                      Are you sure Ben?
                      Yes.

                      And, by the way, to set to 0 later you don't need a loop:

                      memset(num, 0, sizeof num);

                      will do it if num is an array of integer types (chars, ints, etc) but
                      check back here if num is a parameter -- that complicates things.

                      --
                      Ben.

                      Comment

                      • Ben Bacarisse

                        #12
                        Re: decrementing arrays

                        "Bill Cunningham" <nospam@nspam.c omwrites:
                        What if I wanted to look at the value of one element in the array. One
                        dimensional and multi dimensional?
                        >
                        printf("%c\n",n um[5]); ?
                        No. num[5] is an array-valued expression -- it will be converted to a
                        pointer to the first element. %c can't be used with an int * argument
                        (num was declared int num[16][10]).

                        I can't suggest what you should use, because I don't know what you
                        want to do.

                        --
                        Ben.

                        Comment

                        • Richard

                          #13
                          Re: decrementing arrays

                          "Bill Cunningham" <nospam@nspam.c omwrites:
                          "Richard" <rgrdev@gmail.c omwrote in message
                          news:g3h4l2$fem $1@registered.m otzarella.org.. .
                          >
                          >
                          >You surprise me more and more.
                          >
                          Is that good or bad :-)
                          >
                          >Firstly what you want is a single credit card number. Which is 16 digits
                          >of 0-9.
                          >>
                          >I doubt very very much you are doing any calculations with this number.
                          >>
                          >So simply store it as a string : zero terminated or not.
                          >
                          What do you mean zero terminated or not? Doesn't C stick \0
                          automatically ? The only exception I know is strlen.
                          >
                          Bill
                          >
                          -----
                          "You big dummy" --Fred Sanford
                          I mean you might not need it to be zero terminated since you know its
                          length is always 16. But better to be I dare say in your code and makes
                          it easier for display.

                          But I guess you understood what I sad?

                          Solving a problem is all about analysing the problem. And you are making
                          problems there for yourself that are not there.

                          All that 10^16 stuff was nonsense.


                          Comment

                          • Bill Cunningham

                            #14
                            Re: decrementing arrays




                            "Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
                            news:877icjvmd3 .fsf@bsb.me.uk. ..
                            I can't suggest what you should use, because I don't know what you
                            want to do.
                            I used %c because it was suggested I use an array of type char. If char
                            num [10][17] contained different numbers how could I view a specific
                            element's value of the array using printf? I think that might answer all my
                            questions if I could know this.

                            Thanks

                            Bill
                            --
                            "You big dummy" --Fred Sanford






                            Comment

                            • Keith Thompson

                              #15
                              Re: decrementing arrays

                              "Bill Cunningham" <nospam@nspam.c omwrites:
                              "Richard" <rgrdev@gmail.c omwrote in message
                              news:g3h4l2$fem $1@registered.m otzarella.org.. .
                              [...]
                              >So simply store it as a string : zero terminated or not.
                              >
                              What do you mean zero terminated or not? Doesn't C stick \0
                              automatically ? The only exception I know is strlen.
                              You're right, and Richard is wrong. A string is zero-terminated *by
                              definition*.

                              I think what Richard meant is to use an array of char. The array can
                              be zero-terminated (making it a string), or not zero-terminated (in
                              which case it's not a string).

                              I'm not convinced a character array is the best approach; I'll post a
                              separate followup.

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              Nokia
                              "We must do something. This is something. Therefore, we must do this."
                              -- Antony Jay and Jonathan Lynn, "Yes Minister"

                              Comment

                              Working...