100% found this document useful (1 vote)
348 views3 pages

Chapter 6 - Arrays

Arrays allow us to store multiple variables of the same type. They are declared with syntax like int number[12] to store 12 integers. Each array element has an index from 0 to N-1, where N is the array size. Values can be assigned and initialized individually, or all at once. A sample program demonstrates taking user input to populate an array, then calculating the sum of all elements.

Uploaded by

Gaurav
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
348 views3 pages

Chapter 6 - Arrays

Arrays allow us to store multiple variables of the same type. They are declared with syntax like int number[12] to store 12 integers. Each array element has an index from 0 to N-1, where N is the array size. Values can be assigned and initialized individually, or all at once. A sample program demonstrates taking user input to populate an array, then calculating the sum of all elements.

Uploaded by

Gaurav
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Beginners Guide to C++ - Chapter 6

Chapter 6 – Arrays

Arrays are used to store a collection of variables all of the same type. For instance you
can have arrays that store numbers, words etc.

The following could be used to declare arrays:

int number[12] //This array is called numbers and will store 12 integers
char name[10] //This array is called names and will store 9 characters plus \0

Each member in the array has an index number. This index number is used to access
the value of that member.

Suppose we had an array called "int arr[10]", then;


The first member of the array has index 0. I.e. arr[0]
The last member of the array has index 9 NOT 10, since the index goes from 0 to 9.

The ten members are:

arr[0] arr[1] arr[2] arr[3] arr[4]


arr[5] arr[6] arr[7] arr[8] arr[9]

In general if an array has N members then the first member has index 0 and the last
has index N-1.

You can assign values to each member just as with other variables

arr[0] = 10;
arr[3] = 3;

These two lines will set the first member of arr equal to 10 and the fourth equal to 3.
You can initialise an array at the same time as you are defining it as follows:

int arr[5] = {11,2,6,7,3};


Beginners Guide to C++ - Chapter 6

Alternatively you could input data from the keyboard

for(i=0; i<5; i++)


{
cout << “enter member “ << i << endl;
cin >> arr[i];
}

or you could initialise each member individually

arr[0] = 11;
arr[1] = 2;
arr[2] = 6;
arr[3] = 7;
arr[4] = 3;

Here is a program that will find the sum of an array.

1 #include<iostream.h>
2 int main()
3 {
4 const int SIZE=5;
5 int arr[SIZE];
6 int i;
7 int sum
8 sum=0; //We must initialise sum to zero
9
10 for(i=0; i<SIZE; i++)
11 {
12 cout<< "Enter a number" <<endl;
13 cin>>arr[i];
14 }
15
16 for(i=0; i<SIZE; i++)
17 {
18 sum=sum+arr[i];
19 }
20
21 cout<< "The sum of the five numbers is " << sum <<endl;
22
23 return 0;
24 }
Example 6a

When you enter in 5 numbers the program will print out the sum of those 5 numbers.
The line "const int SIZE=5;" is used in case the size of the array needs to be changed -
then only one value will need to be changed. The const keyword means that SIZE is a
Beginners Guide to C++ - Chapter 6

constant and not a variable. Constants are usually written in CAPITAL letters and
here I have also coloured the constant orange for clarity.

You might also like