Initializing Int Array With a Non-Zero Value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • simondex@yahoo.com

    Initializing Int Array With a Non-Zero Value

    Hi, Everyone!

    Does anyone know how to initialize an int array with a non-zero number?


    Thank You Very Much.

    Truly Yours, Simon Dexter

  • John Carson

    #2
    Re: Initializing Int Array With a Non-Zero Value

    <simondex@yahoo .com> wrote in message
    news:1121229979 .062891.274430@ g47g2000cwa.goo glegroups.com[color=blue]
    > Hi, Everyone!
    >
    > Does anyone know how to initialize an int array with a non-zero
    > number?
    >
    >
    > Thank You Very Much.
    >
    > Truly Yours, Simon Dexter[/color]

    Int is not int. C++ is case sensitive.

    The only way to do this at the point of declaration is to repeat the number
    as many times as you need it, e.g.,

    int array[5] = {9,9,9,9,9};

    Alternatively, you can do it in a loop after the declaration.

    Vectors allow you to avoid repeating the number:

    std::vector<int > array(5, 9);


    --
    John Carson

    Comment

    • Rick N. Backer

      #3
      Re: Initializing Int Array With a Non-Zero Value

      On 12 Jul 2005 21:46:19 -0700, simondex@yahoo. com did courageously
      avow:
      [color=blue]
      >Hi, Everyone!
      >
      >Does anyone know how to initialize an int array with a non-zero number?[/color]

      int intArr[n1] = {x1}; // declaration and initialization

      n1 can be any positive integer value and x1 can be any legitimate
      value; number, char, pointer to another array, etc.. I believe you
      don't need to specify all the elements in the list. That said, I
      trust I will be corrected if I'm wrong

      -- OR --

      int intArr[n1]; // declaration
      intArr[0] = x1; // initialization

      It all depends on how far you want to go. You can give it a completer
      list in the first example if you want. Or, you can use a for loop
      that runs through the array index until it has filled the last
      element, using the second statement inside the loop, substituting your
      index variable for the zero and applying whatever number you
      application feels is appropriate for the array.

      [color=blue]
      >
      >Thank You Very Much.[/color]

      You're welcome.

      Ken Wilson

      Amer. Dlx. Tele, Gary Moore LP, LP DC Classic w/P90s,
      Jeff Beck Strat, Morgan OM Acoustic,
      Rick 360/12, Std. Strat (MIM), Mesa 100 Nomad,
      Mesa F-30

      "Goodnight Austin, Texas, wherever you are."

      Comment

      • John Carson

        #4
        Re: Initializing Int Array With a Non-Zero Value

        "Rick N. Backer" <ken.wilson@NsO hSaPw.cAaM> wrote in message
        news:if99d1lp7n bc780vp1duba89b feppbgacr@4ax.c om[color=blue]
        > On 12 Jul 2005 21:46:19 -0700, simondex@yahoo. com did courageously
        > avow:
        >[color=green]
        >> Hi, Everyone!
        >>
        >> Does anyone know how to initialize an int array with a non-zero
        >> number?[/color]
        >
        > int intArr[n1] = {x1}; // declaration and initialization
        >
        > n1 can be any positive integer value and x1 can be any legitimate
        > value; number, char, pointer to another array, etc.. I believe you
        > don't need to specify all the elements in the list. That said, I
        > trust I will be corrected if I'm wrong[/color]

        Your code has the effect of initializing the first element of the array to
        x1 and the rest to zero. You also can't use pointer values without a cast.


        --
        John Carson

        Comment

        • upashu2

          #5
          Re: Initializing Int Array With a Non-Zero Value

          >> how to initialize an int array with a non-zero number?[color=blue][color=green]
          >> John Carson .....
          >> first element of the array to x1 and the rest to zero[/color][/color]
          if we write, int a[20]; it leaves the whole array uninintialized. It
          doesn't initalize array with 0's at all. after all it is c++, not java
          or vb.
          if we write int a[20]={ 1,2};
          then it will initialize only first two elements, and leave all others
          uninitialized.
          Still if you want to initialize an array with 0's, you have to write it
          expilicitly.
          int a[3]={0,0,0}; or use std::vector, std::vector<int > a(size,0);
          The answer is whether u want to initialize array with zero or non-zero
          value , you have to explicitly initialize them.

          Comment

          • makc.the.great@gmail.com

            #6
            Re: Initializing Int Array With a Non-Zero Value

            > Does anyone know how to initialize an int array with a non-zero number?

            did anyone mention memset?

            like, int * pi = new int (123);
            memset(pi, <some byte here>, 123*sizeof(int) );

            pity you'd have pretty limited initial values set to choose from.

            Comment

            • Maxim Yegorushkin

              #7
              Re: Initializing Int Array With a Non-Zero Value

              On Wed, 13 Jul 2005 11:44:15 +0400, upashu2 <upashu1@rediff mail.com> wrote:
              [color=blue][color=green][color=darkred]
              >>> how to initialize an int array with a non-zero number?
              >>> John Carson .....
              >>> first element of the array to x1 and the rest to zero[/color][/color]
              > if we write, int a[20]; it leaves the whole array uninintialized. It
              > doesn't initalize array with 0's at all. after all it is c++, not java
              > or vb.
              > if we write int a[20]={ 1,2};
              > then it will initialize only first two elements, and leave all others
              > uninitialized.[/color]

              This is wrong.

              [dcl.init.aggr] 8.5.1 Aggregates
              ....
              7 If there are fewer initializers in the list than there are members in
              the aggregate, then each member not explicitly initialized shall be
              value-initialized (8.5).
              [Example:
              struct S { int a; char* b; int c; };
              S ss = { 1, "asdf" };
              initializes ss.a with 1, ss.b with "asdf", and ss.c with the value of an
              expression of the form int(), that is, 0. ]

              --
              Maxim Yegorushkin
              <firstname.last name@gmail.com>

              Comment

              • Maxim Yegorushkin

                #8
                Re: Initializing Int Array With a Non-Zero Value

                On Wed, 13 Jul 2005 08:46:19 +0400, <simondex@yahoo .com> wrote:
                [color=blue]
                > Does anyone know how to initialize an int array with a non-zero number?[/color]

                Just iterate over the array and initialize its members with any values you
                like.

                The standard library provides some basic function templates for filling
                arrays as well as ranges:

                fill/fill_n
                generate

                Example:

                int a[10];
                fill_n(a, sizeof(a) / sizeof(*a), 1); // fill with ones

                --
                Maxim Yegorushkin
                <firstname.last name@gmail.com>

                Comment

                • John Carson

                  #9
                  Re: Initializing Int Array With a Non-Zero Value

                  <makc.the.great @gmail.com> wrote in message
                  news:1121245672 .341856.247560@ g44g2000cwa.goo glegroups.com[color=blue][color=green]
                  >> Does anyone know how to initialize an int array with a non-zero
                  >> number?[/color]
                  >
                  > did anyone mention memset?
                  >
                  > like, int * pi = new int (123);[/color]

                  That should be

                  int *pi = new int[123];
                  [color=blue]
                  > memset(pi, <some byte here>, 123*sizeof(int) );[/color]

                  This does byte by byte initialisation, which is viable for the very small
                  fraction of integers which have the same number in each byte.
                  [color=blue]
                  > pity you'd have pretty limited initial values set to choose from.[/color]

                  Small and obsure. You cannot do this:

                  memset(pi, 5, 123*sizeof(int) );

                  and initialise all integers to 5. You initialise all integers to whatever
                  number consists of a 5 in each byte.

                  --
                  John Carson

                  Comment

                  • Rick N. Backer

                    #10
                    Re: Initializing Int Array With a Non-Zero Value

                    On Wed, 13 Jul 2005 15:46:54 +1000, "John Carson"
                    <jcarson_n_o_sp _am_@netspace.n et.au> did courageously avow:
                    [color=blue]
                    >"Rick N. Backer" <ken.wilson@NsO hSaPw.cAaM> wrote in message
                    >news:if99d1lp7 nbc780vp1duba89 bfeppbgacr@4ax. com[color=green]
                    >> On 12 Jul 2005 21:46:19 -0700, simondex@yahoo. com did courageously
                    >> avow:
                    >>[color=darkred]
                    >>> Hi, Everyone!
                    >>>
                    >>> Does anyone know how to initialize an int array with a non-zero
                    >>> number?[/color]
                    >>
                    >> int intArr[n1] = {x1}; // declaration and initialization
                    >>
                    >> n1 can be any positive integer value and x1 can be any legitimate
                    >> value; number, char, pointer to another array, etc.. I believe you
                    >> don't need to specify all the elements in the list. That said, I
                    >> trust I will be corrected if I'm wrong[/color]
                    >
                    >Your code has the effect of initializing the first element of the array to
                    >x1 and the rest to zero. You also can't use pointer values without a cast.[/color]

                    Where is this pointer you speak of?
                    I see an int array being initialized to hold n1 elements of which the
                    first will be x1 and the rest zero as you say. If you had all my post
                    here, you would see I also explained how to declare all the members at
                    once if the OP so wished, how to declare an array without
                    initialization and then initialize a single element later, and also
                    suggest how it could be done in a for loop. Why are you centering on
                    one item and not the whole post?


                    Ken Wilson

                    Amer. Dlx. Tele, Gary Moore LP, LP DC Classic w/P90s,
                    Jeff Beck Strat, Morgan OM Acoustic,
                    Rick 360/12, Std. Strat (MIM), Mesa 100 Nomad,
                    Mesa F-30

                    "Goodnight Austin, Texas, wherever you are."

                    Comment

                    • John Carson

                      #11
                      Re: Initializing Int Array With a Non-Zero Value

                      "Rick N. Backer" <ken.wilson@NsO hSaPw.cAaM> wrote in message
                      news:72fad1du1h 3rpms6mhklbt3u2 7oe02gusg@4ax.c om[color=blue]
                      > On Wed, 13 Jul 2005 15:46:54 +1000, "John Carson"
                      > <jcarson_n_o_sp _am_@netspace.n et.au> did courageously avow:
                      >[color=green]
                      >> "Rick N. Backer" <ken.wilson@NsO hSaPw.cAaM> wrote in message
                      >> news:if99d1lp7n bc780vp1duba89b feppbgacr@4ax.c om[color=darkred]
                      >>> On 12 Jul 2005 21:46:19 -0700, simondex@yahoo. com did courageously
                      >>> avow:
                      >>>
                      >>>> Hi, Everyone!
                      >>>>
                      >>>> Does anyone know how to initialize an int array with a non-zero
                      >>>> number?
                      >>>
                      >>> int intArr[n1] = {x1}; // declaration and initialization
                      >>>
                      >>> n1 can be any positive integer value and x1 can be any legitimate
                      >>> value; number, char, pointer to another array, etc.. I believe you
                      >>> don't need to specify all the elements in the list. That said, I
                      >>> trust I will be corrected if I'm wrong[/color]
                      >>
                      >> Your code has the effect of initializing the first element of the
                      >> array to x1 and the rest to zero. You also can't use pointer values
                      >> without a cast.[/color]
                      >
                      > Where is this pointer you speak of?[/color]

                      You say: "x1 can be any legitimate value; number, char, pointer to another
                      array, etc". Thus the "pointer to another array" is the pointer that I speak
                      of. x1 cannot be a pointer without using a cast.
                      [color=blue]
                      > I see an int array being initialized to hold n1 elements of which the
                      > first will be x1 and the rest zero as you say. If you had all my post
                      > here, you would see I also explained how to declare all the members at
                      > once if the OP so wished, how to declare an array without
                      > initialization and then initialize a single element later, and also
                      > suggest how it could be done in a for loop. Why are you centering on
                      > one item and not the whole post?[/color]

                      The OP asked how to initialize an array with a non-zero number. I took this
                      to mean initialize the *whole* array with a non-zero number and so I took
                      your answer to be a claim that

                      int intArr[n1] = {x1};

                      initialized each element in the array to x1. The fact that you also gave
                      alternative methods of initialization was not inconsistent with this
                      interpretation.

                      If my interpretation was incorrect, I apologize.

                      --
                      John Carson

                      Comment

                      • Default User

                        #12
                        Re: Initializing Int Array With a Non-Zero Value



                        upashu2 wrote:[color=blue][color=green][color=darkred]
                        > >> how to initialize an int array with a non-zero number?
                        > >> John Carson .....
                        > >> first element of the array to x1 and the rest to zero[/color][/color][/color]
                        [color=blue]
                        > if we write, int a[20]; it leaves the whole array uninintialized. It
                        > doesn't initalize array with 0's at all. after all it is c++, not java
                        > or vb.[/color]

                        That's true, but not what John presented. He initialized the first
                        value. The rest of the array will be default initialized.
                        [color=blue]
                        > if we write int a[20]={ 1,2};
                        > then it will initialize only first two elements, and leave all others
                        > uninitialized.[/color]

                        You are incorrect. The rest of the array will be filled with 0.
                        [color=blue]
                        > Still if you want to initialize an array with 0's, you have to write it
                        > expilicitly.[/color]

                        Wrong.




                        Brian

                        Comment

                        • John Carson

                          #13
                          Re: Initializing Int Array With a Non-Zero Value

                          [some further thoughts]

                          "Rick N. Backer" <ken.wilson@NsO hSaPw.cAaM> wrote in message
                          news:72fad1du1h 3rpms6mhklbt3u2 7oe02gusg@4ax.c om[color=blue]
                          > On Wed, 13 Jul 2005 15:46:54 +1000, "John Carson"
                          > <jcarson_n_o_sp _am_@netspace.n et.au> did courageously avow:
                          >[color=green]
                          >> "Rick N. Backer" <ken.wilson@NsO hSaPw.cAaM> wrote in message
                          >> news:if99d1lp7n bc780vp1duba89b feppbgacr@4ax.c om[color=darkred]
                          >>> On 12 Jul 2005 21:46:19 -0700, simondex@yahoo. com did courageously
                          >>> avow:
                          >>>
                          >>>> Hi, Everyone!
                          >>>>
                          >>>> Does anyone know how to initialize an int array with a non-zero
                          >>>> number?
                          >>>
                          >>> int intArr[n1] = {x1}; // declaration and initialization
                          >>>
                          >>> n1 can be any positive integer value and x1 can be any legitimate
                          >>> value; number, char, pointer to another array, etc.. I believe you
                          >>> don't need to specify all the elements in the list. That said, I
                          >>> trust I will be corrected if I'm wrong[/color]
                          >>
                          >> Your code has the effect of initializing the first element of the
                          >> array to x1 and the rest to zero. You also can't use pointer values
                          >> without a cast.[/color]
                          >
                          > Where is this pointer you speak of?
                          > I see an int array being initialized to hold n1 elements of which the
                          > first will be x1 and the rest zero as you say. If you had all my post
                          > here, you would see I also explained how to declare all the members at
                          > once if the OP so wished, how to declare an array without
                          > initialization and then initialize a single element later, and also
                          > suggest how it could be done in a for loop.[/color]

                          You didn't say at *any* point in your post that

                          int intArr[n1] = {x1};

                          would set all elements after the first to zero, so it was surely worth
                          pointing it out so the OP would know what to expect.
                          [color=blue]
                          > Why are you centering on one item and not the whole post?[/color]

                          I was not writing a review. It is perfectly legitimate to focus on the one
                          part of a post that is in need of clarification.

                          --
                          John Carson

                          Comment

                          • Rick N. Backer

                            #14
                            Re: Initializing Int Array With a Non-Zero Value

                            On Thu, 14 Jul 2005 03:25:54 +1000, "John Carson"
                            <jcarson_n_o_sp _am_@netspace.n et.au> did courageously avow:
                            [color=blue]
                            >[some further thoughts]
                            >
                            >"Rick N. Backer" <ken.wilson@NsO hSaPw.cAaM> wrote in message
                            >news:72fad1du1 h3rpms6mhklbt3u 27oe02gusg@4ax. com[color=green]
                            >> On Wed, 13 Jul 2005 15:46:54 +1000, "John Carson"
                            >> <jcarson_n_o_sp _am_@netspace.n et.au> did courageously avow:
                            >>[color=darkred]
                            >>> "Rick N. Backer" <ken.wilson@NsO hSaPw.cAaM> wrote in message
                            >>> news:if99d1lp7n bc780vp1duba89b feppbgacr@4ax.c om
                            >>>> On 12 Jul 2005 21:46:19 -0700, simondex@yahoo. com did courageously
                            >>>> avow:
                            >>>>
                            >>>>> Hi, Everyone!
                            >>>>>
                            >>>>> Does anyone know how to initialize an int array with a non-zero
                            >>>>> number?
                            >>>>
                            >>>> int intArr[n1] = {x1}; // declaration and initialization
                            >>>>
                            >>>> n1 can be any positive integer value and x1 can be any legitimate
                            >>>> value; number, char, pointer to another array, etc.. I believe you
                            >>>> don't need to specify all the elements in the list. That said, I
                            >>>> trust I will be corrected if I'm wrong
                            >>>
                            >>> Your code has the effect of initializing the first element of the
                            >>> array to x1 and the rest to zero. You also can't use pointer values
                            >>> without a cast.[/color]
                            >>
                            >> Where is this pointer you speak of?
                            >> I see an int array being initialized to hold n1 elements of which the
                            >> first will be x1 and the rest zero as you say. If you had all my post
                            >> here, you would see I also explained how to declare all the members at
                            >> once if the OP so wished, how to declare an array without
                            >> initialization and then initialize a single element later, and also
                            >> suggest how it could be done in a for loop.[/color]
                            >
                            >You didn't say at *any* point in your post that
                            >
                            >int intArr[n1] = {x1};
                            >
                            >would set all elements after the first to zero, so it was surely worth
                            >pointing it out so the OP would know what to expect.[/color]

                            I have no problem and readily concede that point and, truthfully,
                            hadn't considered that aspect. I may have boiled the question down to
                            much in trying to simplify it and my response.[color=blue]
                            >[color=green]
                            >> Why are you centering on one item and not the whole post?[/color]
                            >
                            >I was not writing a review. It is perfectly legitimate to focus on the one
                            >part of a post that is in need of clarification.[/color]

                            Oh how I hate first encounters :-). I am used to people only
                            applying to one point and will concede this also and stand now
                            unoffended. I am just used to people interjecting in line and
                            returning the original document intact so context of the discussion
                            can be maintained. In this instance I ass-u-me'd, incorrectly, that
                            you hadn't paid attention to the rest of the post. My apologies.


                            Ken Wilson

                            Amer. Dlx. Tele, Gary Moore LP, LP DC Classic w/P90s,
                            Jeff Beck Strat, Morgan OM Acoustic,
                            Rick 360/12, Std. Strat (MIM), Mesa 100 Nomad,
                            Mesa F-30

                            "Goodnight Austin, Texas, wherever you are."

                            Comment

                            Working...