C++ arrays and loops Help - 26660

Solution Posted by

Julia

Rating : (2)A+
Solution Detail
Price: \$30.00
• From: Computer Science,
• Posted on: Mon 30 Sep, 2013
• Request id: None
• Purchased: 0 time(s)
• Average Rating: No rating
Request Description

An array lets you declare and work with a collection of values of the same type. Let’s say you want to declare four integers. With the knowledge from the last few tutorials you would do something like this:

`` ``
``        int a , b , c , d;``
`` ``

What if you wanted to declare a thousand variables?
That will take you a long time to type. This is where arrays come in handy. An easier way is to declare an array of four integers, like this:

`` ``
``        int a[4];``
`` ``

The four separate integers inside this array are accessed by an index. Each element can be accessed, by using square brackets, with the element number inside. All arrays start at element zero and will go to n-1. (In this case from 0 to 3.)

Note: The index number, which represents the number of elements the array is going to hold, must be a constant value. Because arrays are build out of non-dynamic memory blocks. In a later tutorial we will explain arrays with a variable length, which uses dynamic memory.

So if we want to fill each element you get something like this:

`` ``
``        int a[4];``
``        a[0] = 1;``
``        a[1] = 2;``
``        a[2] = 3;``
``        a[3] = 4;``
`` ``

If you want to use an element, for example for printing, you can do this:

`` ``
``        cout << a[1];``
`` ``

Arrays and loops

One of the nice things about arrays is that you can use a loop to manipulate each element. When an array is declared, the values of each element are not set to zero automatically.

In some cases you want to “re-initialize” the array (which means, setting every element to zero). This can be done like in the example above, but it is easier to use a loop. Here is an example:

`` ``
``        #include<iostream>``
``        using namespace std;``
`` ``
``        int main()``
``        {``
``               int a[4];``
``               int i;``
`` ``
``               for ( i = 0; i < 4; i++ )``
``                       a[i] = 0;``
``               for ( i = 0; i < 4; i++ )``
``                       cout << a[i] << '\n';``
``               return 0;``
``        }``
`` ``
Solution Description

An array lets you declare and work with a collection of values of the same type. Let’s say you want to declare four integers. With the knowledge from the last few tutorials you would do something like this:

`` ``
``        int a , b , c , d;``
`` ``

What if you wanted to declare a thousand variables?
That will take you a long time to type. This is where arrays come in handy. An easier way is to declare an array of four integers, like this:

`` ``
``        int a[4];``
`` ``

The four separate integers inside this array are accessed by an index. Each element can be accessed, by using square brackets, with the ele

Attachments