Explain the ways of initializing the arrays, C/C++ Programming

Assignment Help:

Various ways of initializing the Arrays.

- The for loop initializes 10 elements with the value of their index.

 

   void main()

    {

                const int size = 10; 

                int arr[size];

 

for(int i = 0; i < size ; i++ ) // You can declare a

                variable here in C++.

                  {

                       arr[i] = i;

                   }

      }                      

 

  • An array can be explicitly initialized as follows.

e.g.

                int arr[3] = {0,1,2};

- An explicitly initialized array need not specify size but if specified the number of elements provided must not exceed the size. If the size is given and some elements are not explicitly initialized they are set to zero

                e.g.

 

                int arr[] = {0,1,2};

 

     int arr1[5] = {0,1,2}; // Initialized as {0,1,2,0,0}

      const char a_arr1[] = {'c'.'+','+'}          //size = 3;

 

                     const char a_arr2[] = {"c++"}  //size = 4 because of 

                                            null character at the 

                                                                 end of  the string;

  const char a_arr3[6] = "Daniel"; // ERROR; Daniel has 7  elements

 


Related Discussions:- Explain the ways of initializing the arrays

What are the two steps that happen while i say delete p?, A: N delete p is ...

A: N delete p is a two-step procedure: it calls the destructor, and then releases the memory. The code developed for delete p is functionally similar to this (supposing p is of typ

Padovan.., count the number of string in n-th padovan string

count the number of string in n-th padovan string

Oops, write a c++ program to find the prime numbers

write a c++ program to find the prime numbers

Flowchart, how to define a contanst in a flowchart like we do in c language...

how to define a contanst in a flowchart like we do in c language

Arrays and pointers, i have an array of structs, and am trying to make a po...

i have an array of structs, and am trying to make a pointer that can point to a certain struct within the array

Determine the size of an interger data type without using , Determine the s...

Determine the size of an interger data type without using sizeof() function? A: #include int main() { int *i ; int *j = i + 1; cout }

Flight Departure in C, I really have a hard time analyzing how to write a c...

I really have a hard time analyzing how to write a code in C. It''s all bout Time Departure and we must used a 24 hour clock. Can u help me?

Plz reply on [email protected], You are to implement a code cracking ...

You are to implement a code cracking system. The system will deploy four processes (p1,p2,p3 & p4) to try to guess the password of an encrypted code. For encryption, we consider th

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd