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

Assignment, write a simple c++ program to that use the value to add five nu...

write a simple c++ program to that use the value to add five number .your program should prompt the user to enter these five numbers one after the other

Arrays, how to write the code for operation research

how to write the code for operation research

Write a program that allows two players to play connect four, You may work ...

You may work in pairs for this assignment. Submit only one project per team; both partners will receive the same grade. Also, you can earn up to 40 points (out of 25) on this assig

Algorithm and flowcharts, algorithm to find out all the factors of given po...

algorithm to find out all the factors of given positive integers

Fill an array of randomly generated integers, The task consists of two part...

The task consists of two parts which are both preferably implemented in one source file. Towards the end of this document you will find a code skeleton that you must assume. Start

Explain the for loop - computer programming, Explain the For Loop - Compute...

Explain the For Loop - Computer Programming? Similar to the while statement, for loop is an entry controlled loop and the code of the for loop will be executed itereatively. Th

Stream input and output functions, Within C we access external devices by ...

Within C we access external devices by means of a pointer i.e. address. The address could point to any memory mapped device i.e. Ram, Rom, Duart, Disk drives etc. Therefore there i

Code for design software, In this project, you will write a software for th...

In this project, you will write a software for the Registrar's office of a university called YouOIT to maintain information about students and courses at the university. The reg

Luminous Jewels, Jewels can only be removed for polishing from either end o...

Jewels can only be removed for polishing from either end of the necklace

Write a function that computes the total interest charged, You are going to...

You are going to write a function that computes the total interest charged on a credit card before it is paid off. List all the inputs the function will need.

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