Dynamic Array

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

    Dynamic Array

    Can anyone help me in finding elements stored in a dynamic array in.

  • suriya

    #2
    Re: Dynamic Array


    chai wrote:[color=blue]
    > Can anyone help me in finding elements stored in a dynamic array in.[/color]

    Hi Dear,

    U check this code. It is allocating required memory dynamically,
    checking for allocation failure, storing the values and retrieving back
    and printing them. Finally deallocating the memory. Let me know is this
    the answer to u question.

    #include<stdio. h>
    #include<malloc .h>
    int main()
    {
    int *p;
    int n,i,num;

    printf("enter num of elements");
    scanf("%d",&n);

    p= (int*)malloc(n* sizeof(int));

    if(p ==NULL)
    {
    printf("Dynamic allocation failed");
    return 1;
    }

    for(i=0;i<n;i++ )
    {
    printf("enter number");
    scanf("%d",&num );
    *(p+i)= num;
    }

    printf(" \n The elements are ");
    for(i=0;i<n;i++ )
    {
    printf(" %d", *(p+i));
    }

    free(p);
    return 0;
    }
    /* end */

    Comment

    • suriya

      #3
      Re: Dynamic Array


      chai wrote:[color=blue]
      > Can anyone help me in finding elements stored in a dynamic array in.[/color]

      Hi Dear,

      U check this code. It is allocating required memory dynamically,
      checking for allocation failure, storing the values and retrieving back
      and printing them. Finally deallocating the memory. Let me know is this
      the answer to u question.

      #include<stdio. h>
      #include<malloc .h>
      int main()
      {
      int *p;
      int n,i,num;

      printf("enter num of elements");
      scanf("%d",&n);

      p= (int*)malloc(n* sizeof(int));

      if(p ==NULL)
      {
      printf("Dynamic allocation failed");
      return 1;
      }

      for(i=0;i<n;i++ )
      {
      printf("enter number");
      scanf("%d",&num );
      *(p+i)= num;
      }

      printf(" \n The elements are ");
      for(i=0;i<n;i++ )
      {
      printf(" %d", *(p+i));
      }

      free(p);
      return 0;
      }
      /* end */

      Comment

      • Alexei A. Frounze

        #4
        Re: Dynamic Array

        "chai" <chaithramun@ya hoo.com> wrote in message
        news:1128680910 .613070.59130@g 49g2000cwa.goog legroups.com...[color=blue]
        > Can anyone help me in finding elements stored in a dynamic array in.[/color]


        This group deals with standard C, not algorithms. If you have an algorithm
        but have problems expressing it's in terms of C (your code isn't compiling
        or working as per the algorithm), come here for the help. But if you need
        the algorithm, this isn't the right group to ask for it.

        Alex


        Comment

        • Mabden

          #5
          Re: Dynamic Array

          "suriya" <aktersuriya@gm ail.com> wrote in message
          news:1128683024 .223977.299700@ g49g2000cwa.goo glegroups.com.. .[color=blue]
          >
          > chai wrote:[color=green]
          > > Can anyone help me in finding elements stored in a dynamic array in.[/color]
          >
          > Hi Dear,
          >
          > U check this code. It is allocating required memory dynamically,
          > checking for allocation failure, storing the values and retrieving[/color]
          back[color=blue]
          > and printing them. Finally deallocating the memory. Let me know is[/color]
          this[color=blue]
          > the answer to u question.
          >
          > #include<stdio. h>
          > #include<malloc .h>
          > int main()
          > {
          > int *p;
          > int n,i,num;
          >
          > printf("enter num of elements");
          > scanf("%d",&n);
          >
          > p= (int*)malloc(n* sizeof(int));
          >
          > if(p ==NULL)
          > {
          > printf("Dynamic allocation failed");
          > return 1;
          > }
          >
          > for(i=0;i<n;i++ )
          > {
          > printf("enter number");
          > scanf("%d",&num );
          > *(p+i)= num;
          > }
          >
          > printf(" \n The elements are ");
          > for(i=0;i<n;i++ )
          > {
          > printf(" %d", *(p+i));
          > }
          >
          > free(p);
          > return 0;
          > }
          > /* end */
          >[/color]

          Three times is the charm.
          Hi Dear,

          U check this code. It is allocating required memory dynamically,
          checking for allocation failure, storing the values and retrieving back
          and printing them. Finally deallocating the memory. Let me know is this
          the answer to u question.

          #include<stdio. h>
          #include<malloc .h>
          int main()
          {
          int *p;
          int n,i,num;

          printf("enter num of elements");
          scanf("%d",&n);

          p= (int*)malloc(n* sizeof(int));

          if(p ==NULL)
          {
          printf("Dynamic allocation failed");
          return 1;
          }

          for(i=0;i<n;i++ )
          {
          printf("enter number");
          scanf("%d",&num );
          *(p+i)= num;
          }

          printf(" \n The elements are ");
          for(i=0;i<n;i++ )
          {
          printf(" %d", *(p+i));
          }

          free(p);
          return 0;
          }
          /* end */

          --
          Mabden


          Comment

          • Flash Gordon

            #6
            Re: Dynamic Array

            suriya wrote:[color=blue]
            > chai wrote:
            >[color=green]
            >>Can anyone help me in finding elements stored in a dynamic array in.[/color][/color]

            You should read the comp.lang.c FAQ which has lots of information on
            pointers and dynamic memory. Also, a better statement of your problem
            including a small program showing it would allow us to help you rather more.
            [color=blue]
            > Hi Dear,
            >
            > U check this code. It is allocating required memory dynamically,[/color]

            Please don't use abbreviations like "u". All they do is make your post
            harder to read. Remember that many more people read your post than write
            it, so it is worth a few extra keystrokes.
            [color=blue]
            > checking for allocation failure, storing the values and retrieving back
            > and printing them. Finally deallocating the memory. Let me know is this
            > the answer to u question.
            >
            > #include<stdio. h>[/color]

            The space shortage was resolved years ago, so why not use spaces to make
            your code easier to read?
            #include <stdio.h>
            [color=blue]
            > #include<malloc .h>[/color]

            There is no such header as malloc.h in standard C, although your
            implementation might provide one. Use the standard stdlib.h header instead.
            #include <stdlib.h>
            [color=blue]
            > int main()[/color]

            It is better to specify that you are not using parameters.
            int main(void)
            [color=blue]
            > {
            > int *p;
            > int n,i,num;
            >
            > printf("enter num of elements");
            > scanf("%d",&n);[/color]

            You should check to see if scanf succeeded. What if the user typed in "ten"?
            [color=blue]
            > p= (int*)malloc(n* sizeof(int));[/color]

            There is no need to cast the result of malloc and doing so can hide not
            having a correct prototype in scope which is a bad thing. Also, you can
            use the sizeof the object being pointed at rather than the type to
            simplify maintenance.

            p = malloc(n * sizeof *p);
            [color=blue]
            > if(p ==NULL)
            > {
            > printf("Dynamic allocation failed");
            > return 1;[/color]

            This is a non-standard return value from main. Unless you have reason
            not to you should use 0, EXIT_SUCCESS or EXIT_FAILURE with the latter
            two defined in stdlib.h. On some systems returning 1 will signal success!
            return EXIT_FAILURE;
            [color=blue]
            > }
            >
            > for(i=0;i<n;i++ )
            > {
            > printf("enter number");[/color]

            This may not be displayed before waiting for input since stdout is
            likely to be line buffered. To maximise your chances you should:
            fflush(stdout);
            [color=blue]
            > scanf("%d",&num );[/color]

            Again you need to check whether scanf succeeded and deal with failure.
            [color=blue]
            > *(p+i)= num;[/color]

            A simpler expression is
            p[i] = num;
            [color=blue]
            > }
            >
            > printf(" \n The elements are ");
            > for(i=0;i<n;i++ )
            > {
            > printf(" %d", *(p+i));[/color]

            This can again be simplified to
            printf(" %d", *p[i]);
            [color=blue]
            > }[/color]

            To ensure the last line is visible you need to finish with a newline.
            putchar('\n');
            [color=blue]
            > free(p);
            > return 0;
            > }
            > /* end */[/color]
            --
            Flash Gordon
            Living in interesting times.
            Although my email address says spam, it is real and I read it.

            Comment

            • Martin Ambuhl

              #7
              Re: Dynamic Array

              Mabden wrote:
              [color=blue]
              > Three times is the charm.
              > Hi Dear,
              >
              > U check this code.[/color]

              By the third time, I would think that you could have learned to spell
              such simple words as "you" and "your". Script-kiddies rule, indeed.

              Comment

              • Mabden

                #8
                Re: Dynamic Array

                "Martin Ambuhl" <mambuhl@earthl ink.net> wrote in message
                news:5vx1f.1083 7$QE1.9963@news read2.news.atl. earthlink.net.. .[color=blue]
                > Mabden wrote:
                >[color=green]
                > > Three times is the charm.
                > > Hi Dear,
                > >
                > > U check this code.[/color]
                >
                > By the third time, I would think that you could have learned to spell
                > such simple words as "you" and "your". Script-kiddies rule, indeed.[/color]

                I assume you are speaking to suriya the OP...

                You see when they post the same text over and over, they don't actually
                change the content, otherwise it wouldn't be "the same text". I was just
                hurrying the process along.

                But thank you for playing.

                --
                Mabden


                Comment

                • Martin Ambuhl

                  #9
                  Re: Dynamic Array

                  Mabden wrote:[color=blue]
                  > "Martin Ambuhl" <mambuhl@earthl ink.net> wrote in message
                  > news:5vx1f.1083 7$QE1.9963@news read2.news.atl. earthlink.net.. .
                  >[color=green]
                  >>Mabden wrote:
                  >>
                  >>[color=darkred]
                  >>>Three times is the charm.
                  >>>Hi Dear,
                  >>>
                  >>>U check this code.[/color]
                  >>
                  >>By the third time, I would think that you could have learned to spell
                  >>such simple words as "you" and "your". Script-kiddies rule, indeed.[/color]
                  >
                  >
                  > I assume you are speaking to suriya the OP...[/color]

                  And to you, who reposted the illiteracies without quotation. You have
                  committed a large number of sins:
                  1) Posting those illiteracies
                  2) Plagiarism
                  and ...
                  [color=blue]
                  >
                  > You see when they post the same text over and over, they don't actually
                  > change the content, otherwise it wouldn't be "the same text". I was just
                  > hurrying the process along.
                  >
                  > But thank you for playing.[/color]

                  3) Pretending that your posting others words as your own should be read
                  as a failure on the reader's part.

                  No, I won't thank you for playing. Your double dishonesty should not be
                  thanked, nor should your idiocy.

                  Comment

                  • Mabden

                    #10
                    Re: Dynamic Array

                    "Martin Ambuhl" <mambuhl@earthl ink.net> wrote in message
                    news:eeA1f.1097 2$QE1.2348@news read2.news.atl. earthlink.net.. .[color=blue]
                    > Mabden wrote:[color=green]
                    > > "Martin Ambuhl" <mambuhl@earthl ink.net> wrote in message
                    > > news:5vx1f.1083 7$QE1.9963@news read2.news.atl. earthlink.net.. .
                    > >[color=darkred]
                    > >>Mabden wrote:
                    > >>
                    > >>
                    > >>>Three times is the charm.
                    > >>>Hi Dear,
                    > >>>
                    > >>>U check this code.
                    > >>
                    > >>By the third time, I would think that you could have learned to[/color][/color][/color]
                    spell[color=blue][color=green][color=darkred]
                    > >>such simple words as "you" and "your". Script-kiddies rule, indeed.[/color]
                    > >
                    > >
                    > > I assume you are speaking to suriya the OP...[/color]
                    >
                    > And to you, who reposted the illiteracies without quotation. You have
                    > committed a large number of sins:
                    > 1) Posting those illiteracies
                    > 2) Plagiarism
                    > and ...[/color]

                    Without quotation? Isn't what the indenting is? I don't see quotes
                    around what you posted... you plagiarismist (I assume you'll correct
                    that for me if you quote it). Oh, and you forgot to fix the text, just
                    as I did - shame on us both for our sins. Why would you post something
                    that some else wrote without fixing all of their mistakes? Oh, yeah,
                    because that's not what one does when quoting! Idiot.
                    [color=blue][color=green]
                    > > You see when they post the same text over and over, they don't[/color][/color]
                    actually[color=blue][color=green]
                    > > change the content, otherwise it wouldn't be "the same text". I was[/color][/color]
                    just[color=blue][color=green]
                    > > hurrying the process along.
                    > >
                    > > But thank you for playing.[/color]
                    >
                    > 3) Pretending that your posting others words as your own should be[/color]
                    read[color=blue]
                    > as a failure on the reader's part.[/color]

                    Well, if you didn't take the time to read it, why are you posting angry
                    messages into the Eternal Record of the Internet. It could make you look
                    the fool.
                    [color=blue]
                    > No, I won't thank you for playing. Your double dishonesty should not[/color]
                    be[color=blue]
                    > thanked, nor should your idiocy.[/color]

                    I wrote all of the text in this message that is not prefaced with
                    indentation characters. Does that make it clear to you? I wouldn't want
                    another misunderstandin g on your part to make me seem dishonest in your
                    eyes. In fact, I would like to go on record as saying I have and will
                    always preface quotations with some indentation characters. In the
                    spirit of honesty and the fact that that's the way it works.

                    My idiocy is also on record, nor are you the first to notice. Nor are
                    you the last.

                    And thanks for playing, again. Three times is, of course, the charm, as
                    stated above, so I expect an even longer rant about some meaningless
                    lesson on honesty and textual invention in the future. Please try to add
                    a Haiku to Array your anger and indignity with more, oh let's say,
                    Dynamics. Regards.

                    --
                    Mabden


                    Comment

                    • suriya

                      #11
                      Re: Dynamic Array


                      Hi Flash,
                      [color=blue]
                      > Please don't use abbreviations like "u". All they do is make your post
                      > harder to read. Remember that many more people read your post than write
                      > it, so it is worth a few extra keystrokes.[/color]

                      [color=blue][color=green]
                      > > #include<stdio. h>[/color]
                      >
                      > The space shortage was resolved years ago, so why not use spaces to make
                      > your code easier to read?
                      > #include <stdio.h>
                      > It is better to specify that you are not using parameters.
                      > int main(void)[/color]

                      Thanks for these tips



                      p= (int*)malloc(n* sizeof(int));
                      you should note that malloc returns void* and it is better to typecast
                      the return value.


                      A simpler expression is[color=blue]
                      > p[i] = num;[/color]
                      Good.
                      [color=blue]
                      > This can again be simplified to[/color]
                      printf(" %d", *p[i]);

                      This won't work as p is pointer. It should be *(p+i) or only p[i].

                      All said and done. I just want to give quick idea about dynamic array.
                      I appreciate your keen interest in analysing code line by line.
                      Thanks for giving your valuable suggestions

                      Also I apologizes to this group for sending same reply three times. It
                      just happened that I was refreshing my web page and that mail was sent
                      thrice. I am sorry for that.
                      Suriya

                      Comment

                      • Keith Thompson

                        #12
                        Re: Dynamic Array

                        "suriya" <aktersuriya@gm ail.com> writes:
                        [...][color=blue]
                        > p= (int*)malloc(n* sizeof(int));
                        > you should note that malloc returns void* and it is better to typecast
                        > the return value.[/color]

                        No, it really isn't. Yes, malloc returns void*, but an expression of
                        type void* can be implicitly converted to any pointer-to-object type.
                        The cast is unnecessary, and it can hide certain errors, such as a
                        failure to "#include <stdlib.h>". Flash Gordon explained this in text
                        that you didn't quote.

                        The best way to do the allocation is

                        p = malloc(n * sizeof *p);

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                        We must do something. This is something. Therefore, we must do this.

                        Comment

                        • Flash Gordon

                          #13
                          Re: Dynamic Array

                          suriya wrote:

                          <snip>
                          [color=blue][color=green][color=darkred]
                          >>>#include<std io.h>[/color][/color][/color]
                          [color=blue]
                          > p= (int*)malloc(n* sizeof(int));
                          > you should note that malloc returns void* and it is better to typecast
                          > the return value.[/color]

                          Yes, it returns a void*, but no, it is definitely not better to cast it
                          (unless your initials are PJP). The return value will be converted
                          without the cast, and casting would remove the warning the compiler is
                          otherwise *required* to generate if you fail to include stdlib.h

                          Also, by doing it the way you show you have to get the type correct in
                          three places, and change it in three places if the type changes. The
                          generally recommended method here is
                          p = malloc(n * sizeof *p);
                          and this is for very good reasons which have been debated many times in
                          the past.
                          [color=blue]
                          > A simpler expression is
                          >[color=green]
                          >> p[i] = num;[/color]
                          >
                          > Good.
                          >
                          >[color=green]
                          >>This can again be simplified to[/color]
                          >
                          > printf(" %d", *p[i]);
                          >
                          > This won't work as p is pointer. It should be *(p+i) or only p[i].[/color]

                          <snip>

                          That was a typo (or rather an editing error), I had intended p[i].
                          --
                          Flash Gordon
                          Living in interesting times.
                          Although my email address says spam, it is real and I read it.

                          Comment

                          • Martin Ambuhl

                            #14
                            Re: Dynamic Array

                            suriya wrote:
                            [color=blue]
                            >
                            > p= (int*)malloc(n* sizeof(int));
                            > you should note that malloc returns void* and it is better to typecast
                            > the return value.[/color]

                            BZZT! Thanks for playing. Please direct such incorrect comments to
                            comp.lang.c++, where their language has saddled them with such silly rules.
                            And 'sizeof(int)' is at best a stylistic error. Try:
                            p = malloc(n * sizeof *p);

                            Comment

                            • Gregory Pietsch

                              #15
                              Re: Dynamic Array


                              chai wrote:[color=blue]
                              > Can anyone help me in finding elements stored in a dynamic array in.[/color]

                              If you are asking about dynamic array code, just grab FreeDOS Edlin
                              from either ibiblio or alt.sources and marvel at the dynarray.h file,
                              which contains an implementation of dynamic arrays that can be used for
                              just about any type.

                              Gregory Pietsch

                              Comment

                              Working...