for and arrays

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

    for and arrays

    I understand this code.

    int a[5];
    int b;
    for (b=0;b<5;b=b+1)
    int a[b];

    This should take every element of the array a and set it to 1,2,3,4,5.
    Great. Now for the big question. How would you work this?

    int a [5][3];

    And make the first element of 5 all zeros and the second element of 3 equal
    to 1,2,3 ? I don't know how to work with a 2 dimensional array.

    Bill


  • Ben Bacarisse

    #2
    Re: for and arrays

    "Bill Cunningham" <nospam@nspam.c omwrites:
    I understand this code.
    >
    int a[5];
    int b;
    for (b=0;b<5;b=b+1)
    int a[b];
    Did you try?
    This should take every element of the array a and set it to 1,2,3,4,5.
    Great.
    No, a loop that "sets" things will usually have an assignment in its
    body.
    Now for the big question. How would you work this?
    >
    int a [5][3];
    >
    And make the first element of 5 all zeros and the second element of 3 equal
    to 1,2,3 ? I don't know how to work with a 2 dimensional array.
    I'd stick with 1 dimensional ones. If you are in this for the
    challenge of learning C, then it is much better to master the easy
    bits before moving on. If you have some objective in mind (i.e. you
    want to write some specific program) then you should pick a language
    with fewer pitfalls and peculiarities than C. In particular,
    interpreted languages let you learn by trying things out which, I
    suspect, is a style that would suit you.

    --
    Ben.

    Comment

    • Bill Cunningham

      #3
      Re: for and arrays


      "Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
      news:87tzeh1rbu .fsf@bsb.me.uk. ..
      "Bill Cunningham" <nospam@nspam.c omwrites:
      >
      >I understand this code.
      >>
      >int a[5];
      >int b;
      >for (b=0;b<5;b=b+1)
      >int a[b];
      >
      Did you try?
      Well I tried and it compiled with no errors. Or atleast compiled anyway.
      Yes this is an excercise to learn. All I do is an excercise to learn. What
      if one had an array of 500 ints and had the simple task of setting them
      1-500? How's this for another take.

      int a[10];
      int b;
      for (b=0;b<10;b++) {

      a[ b=b+1];
      }


      Bill


      Comment

      • Ben Bacarisse

        #4
        Re: for and arrays

        "Bill Cunningham" <nospam@nspam.c omwrites:
        "Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
        news:87tzeh1rbu .fsf@bsb.me.uk. ..
        >"Bill Cunningham" <nospam@nspam.c omwrites:
        >>
        >>I understand this code.
        >>>
        >>int a[5];
        >>int b;
        >>for (b=0;b<5;b=b+1)
        >>int a[b];
        >>
        >Did you try?
        >
        Well I tried and it compiled with no errors. Or atleast compiled
        anyway.
        You must have found out by now that a program that compiles is like an
        essay the spell-checks. It does not mean you've answered the question.
        Yes this is an excercise to learn.
        Compiling it is not enough, then. The wonderful thing about
        programming is that your program can tell you if you've got it right.
        All I do is an excercise to learn. What
        if one had an array of 500 ints and had the simple task of setting them
        1-500? How's this for another take.
        >
        int a[10];
        int b;
        for (b=0;b<10;b++) {
        >
        a[ b=b+1];
        }
        Let's assume that you intended to write a[500] rather than a[10]. You
        need to move one character characters to turn the above into
        a program that does what you want, but can you see which character
        needs to move at to where?

        [I still think that you should learn an interpreted language.]

        --
        Ben.

        Comment

        • Keith Thompson

          #5
          Re: for and arrays

          "Bill Cunningham" <nospam@nspam.c omwrites:
          I understand this code.
          No, I'm afraid you don't.
          int a[5];
          int b;
          for (b=0;b<5;b=b+1)
          int a[b];
          >
          This should take every element of the array a and set it to 1,2,3,4,5.
          You seem to be thinking that the line "int a[b];" assigns a value to
          an element of a. In fact, it doesn't do anything at all like that.
          Knowing what it actually does won't help you, so please ignore the
          following. (It declares a VLA which hides the declaration of a.)

          Just this once, I'll give you a freebie. If you want to set the
          elements of a to 1, 2, 3, 4, 5, here's one way to do it:

          int a[5];
          int i;
          for (i = 0; i < 5; i ++) {
          a[i] = i + 1;
          }
          Great. Now for the big question. How would you work this?
          >
          int a [5][3];
          >
          And make the first element of 5 all zeros and the second element of 3 equal
          to 1,2,3 ? I don't know how to work with a 2 dimensional array.
          You don't even understand what a 2 dimensional array is. Your
          question assumes that it's a 5-element array followed by a 3-element
          array. It isn't. It's a 5-element array, each of whose elements is a
          3-element array of ints, for a total of 15 (5*3) ints.

          Here's a conceptual picture of your one-dimensional array, where each
          '#' represents an element that can hold an int value:

          +-+-+-+-+-+
          |#|#|#|#|#|
          +-+-+-+-+-+

          And here's a corresponding picture for your "int a[5][3];":

          +-+-+-+
          |#|#|#|
          +-+-+-+
          |#|#|#|
          +-+-+-+
          |#|#|#|
          +-+-+-+
          |#|#|#|
          +-+-+-+
          |#|#|#|
          +-+-+-+


          But you shouldn't even be thinking about 2-dimensional arrays until
          you have a firm grasp on 1-dimensional arrays.

          --
          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

          • Bill Cunningham

            #6
            Re: for and arrays


            "Keith Thompson" <kst-u@mib.orgwrote in message
            news:lnr69lij2j .fsf@nuthaus.mi b.org...
            You don't even understand what a 2 dimensional array is. Your
            question assumes that it's a 5-element array followed by a 3-element
            array. It isn't. It's a 5-element array, each of whose elements is a
            3-element array of ints, for a total of 15 (5*3) ints.
            I understand if I am correct that each of the 5 elements have 3
            elements. Yes. What I didn't understand was

            a[i]=i+1;

            Bill


            Comment

            • Barry Schwarz

              #7
              Re: for and arrays

              On Tue, 22 Jul 2008 16:40:36 -0700, Keith Thompson <kst-u@mib.org>
              wrote:
              >"Bill Cunningham" <nospam@nspam.c omwrites:
              >I understand this code.
              >
              >No, I'm afraid you don't.
              >
              >int a[5];
              >int b;
              >for (b=0;b<5;b=b+1)
              >int a[b];
              >>
              > This should take every element of the array a and set it to 1,2,3,4,5.
              >
              >You seem to be thinking that the line "int a[b];" assigns a value to
              >an element of a. In fact, it doesn't do anything at all like that.
              >Knowing what it actually does won't help you, so please ignore the
              >following. (It declares a VLA which hides the declaration of a.)
              Since there are no braces around the statement, it is a constraint
              violation in C89 since declarations must precede statements. Even in
              C99 it should be a duplicate definition of a.


              Remove del for email

              Comment

              • Barry Schwarz

                #8
                Re: for and arrays

                On Tue, 22 Jul 2008 22:15:52 GMT, "Bill Cunningham" <nospam@nspam.c om>
                wrote:
                >I understand this code.
                You are not even close as others have pointed out.
                >
                >int a[5];
                >int b;
                >for (b=0;b<5;b=b+1)
                >int a[b];
                >
                This should take every element of the array a and set it to 1,2,3,4,5.
                >Great. Now for the big question. How would you work this?
                >
                >int a [5][3];
                >
                >And make the first element of 5 all zeros and the second element of 3 equal
                What do you think the phrase "first element of 5" means? What about
                "the second element of 3"? How many elements to you think this 2D
                array has?
                >to 1,2,3 ? I don't know how to work with a 2 dimensional array.
                Don't even try till after you understand 1D arrays.


                Remove del for email

                Comment

                • Keith Thompson

                  #9
                  Re: for and arrays

                  Barry Schwarz <schwarzb@dqel. comwrites:
                  On Tue, 22 Jul 2008 16:40:36 -0700, Keith Thompson <kst-u@mib.org>
                  wrote:
                  >
                  >>"Bill Cunningham" <nospam@nspam.c omwrites:
                  >>I understand this code.
                  >>
                  >>No, I'm afraid you don't.
                  >>
                  >>int a[5];
                  >>int b;
                  >>for (b=0;b<5;b=b+1)
                  >>int a[b];
                  >>>
                  >> This should take every element of the array a and set it to 1,2,3,4,5.
                  >>
                  >>You seem to be thinking that the line "int a[b];" assigns a value to
                  >>an element of a. In fact, it doesn't do anything at all like that.
                  >>Knowing what it actually does won't help you, so please ignore the
                  >>following. (It declares a VLA which hides the declaration of a.)
                  >
                  Since there are no braces around the statement, it is a constraint
                  violation in C89 since declarations must precede statements. Even in
                  C99 it should be a duplicate definition of a.
                  It's also a constraint violation in C89 because it's a VLA. But I
                  didn't want to go into too much detail about why it's wrong. The way
                  to transform that into what Bill intended is to delete it and write a
                  correct line.

                  --
                  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

                  • Bill Cunningham

                    #10
                    Re: for and arrays


                    "Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
                    news:87prp51oq2 .fsf@bsb.me.uk. ..
                    [I still think that you should learn an interpreted language.]
                    >
                    I've come so far with C now why stop ? Though I have even more to learn.
                    This C syntax is confusing. Bash and Perl are interesting though.

                    Bill


                    Comment

                    • Richard Heathfield

                      #11
                      Re: for and arrays

                      Bill Cunningham said:
                      >
                      "Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
                      news:87prp51oq2 .fsf@bsb.me.uk. ..
                      >
                      >[I still think that you should learn an interpreted language.]
                      >>
                      I've come so far with C
                      No, you haven't. You're still failing to grasp Chapter 1 of K&R2, after
                      what must be something like a decade of trying. That isn't far. That's
                      near.
                      now why stop ?
                      Because you're wasting your time.
                      Though I have even more to learn.
                      C is not a large language. I would estimate that you've mastered perhaps a
                      thousandth of it. In what, ten years? Life's too short. Do something else.
                      This C syntax is confusing.
                      Actually, C's syntax is astoundingly simple. Okay, maybe it takes a little
                      getting used to - but anyone who is ever going to "get it" will "get it"
                      within a few weeks or, at most, months.
                      Bash and Perl are interesting though.
                      If you think C's syntax is confusing, you have some real treats in store.

                      --
                      Richard Heathfield <http://www.cpax.org.uk >
                      Email: -http://www. +rjh@
                      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                      "Usenet is a strange place" - dmr 29 July 1999

                      Comment

                      • Kaz Kylheku

                        #12
                        Re: for and arrays

                        On 2008-07-22, Ben Bacarisse <ben.usenet@bsb .me.ukwrote:
                        "Bill Cunningham" <nospam@nspam.c omwrites:
                        >for (b=0;b<10;b++) {
                        >>
                        > a[ b=b+1];
                        > }
                        [ snip ]
                        [I still think that you should learn an interpreted language.]
                        In light of that cute a[b=b+1] above, it better be one
                        that is very liberally interpreted, to the point of being
                        downright charitable. :)

                        Comment

                        • Bill Cunningham

                          #13
                          Re: for and arrays


                          "Richard Heathfield" <rjh@see.sig.in validwrote in message
                          news:xrmdnXKS8b TFNBrVnZ2dnUVZ8 vKdnZ2d@bt.com. ..
                          Bill Cunningham said:
                          >
                          >>
                          >"Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
                          >news:87prp51oq 2.fsf@bsb.me.uk ...
                          >>
                          >>[I still think that you should learn an interpreted language.]
                          >>>
                          > I've come so far with C
                          >
                          No, you haven't. You're still failing to grasp Chapter 1 of K&R2, after
                          what must be something like a decade of trying. That isn't far. That's
                          near.
                          [snip]

                          About 5 years is more like it. And for one year I gave up on C. But
                          everything is in C or C++. Looking at Bash it doesn't look any simpler
                          really and in just looks C like. I need someone to explain things to me when
                          I get stuck. If I had a tutor you would be suprised in what I can pick up.
                          But that's one thing you don't get in clc. The one thing I need.

                          Bill


                          Comment

                          • Keith Thompson

                            #14
                            Re: for and arrays

                            "Bill Cunningham" <nospam@nspam.c omwrites:
                            "Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
                            news:87prp51oq2 .fsf@bsb.me.uk. ..
                            >
                            >[I still think that you should learn an interpreted language.]
                            >>
                            I've come so far with C now why stop ? Though I have even more to learn.
                            This C syntax is confusing. Bash and Perl are interesting though.
                            No, I'm afraid you haven't come very far at all with C.

                            You might consider studying Python. Perl is largely a mish-mash of C,
                            AWK, shell scripting, and a few other languages. Python has a more
                            coherent design. Personally, I'm a big fan of Perl, but I don't think
                            it's the language for you.

                            --
                            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

                            • Bill Cunningham

                              #15
                              Re: for and arrays


                              "Keith Thompson" <kst-u@mib.orgwrote in message
                              news:lnd4l4ustp .fsf@nuthaus.mi b.org...
                              "Bill Cunningham" <nospam@nspam.c omwrites:
                              >"Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
                              >news:87prp51oq 2.fsf@bsb.me.uk ...
                              >>
                              >>[I still think that you should learn an interpreted language.]
                              >>>
                              > I've come so far with C now why stop ? Though I have even more to
                              >learn.
                              >This C syntax is confusing. Bash and Perl are interesting though.
                              >
                              No, I'm afraid you haven't come very far at all with C.
                              >
                              You might consider studying Python. Perl is largely a mish-mash of C,
                              AWK, shell scripting, and a few other languages. Python has a more
                              coherent design. Personally, I'm a big fan of Perl, but I don't think
                              it's the language for you.
                              >
                              There's always assembly :-)

                              Bill


                              Comment

                              Working...