What happens when you try to access 6th element of an array when the size of the array is 5?

Arrays in C

An array is a variable that can store multiple values. For example, if you want to store 100 integers, you can create an array for it.

int data[100];

How to declare an array?

dataType arrayName[arraySize];

For example,

float mark[5];

Here, we declared an array, mark, of floating-point type. And its size is 5. Meaning, it can hold 5 floating-point values.

It's important to note that the size and type of an array cannot be changed once it is declared.

Access Array Elements

You can access elements of an array by indices.

Suppose you declared an array mark as above. The first element is mark[0], the second element is mark[1] and so on.

Declare an Array

Few keynotes:

  • Arrays have 0 as the first index, not 1. In this example, mark[0] is the first element.
  • If the size of an array is n, to access the last element, the n-1 index is used. In this example, mark[4]
  • Suppose the starting address of mark[0] is 2120d. Then, the address of the mark[1] will be 2124d. Similarly, the address of mark[2] will be 2128d and so on.
    This is because the size of a float is 4 bytes.

How to initialize an array?

It is possible to initialize an array during declaration. For example,

int mark[5] = {19, 10, 8, 17, 9};

You can also initialize an array like this.

int mark[] = {19, 10, 8, 17, 9};

Here, we haven't specified the size. However, the compiler knows its size is 5 as we are initializing it with 5 elements.

Initialize an Array

Here,

mark[0] is equal to 19 mark[1] is equal to 10 mark[2] is equal to 8 mark[3] is equal to 17 mark[4] is equal to 9

Change Value of Array elements

int mark[5] = {19, 10, 8, 17, 9} // make the value of the third element to -1 mark[2] = -1; // make the value of the fifth element to 0 mark[4] = 0;

Input and Output Array Elements

Here's how you can take input from the user and store it in an array element.

// take input and store it in the 3rd element ​scanf["%d", &mark[2]]; // take input and store it in the ith element scanf["%d", &mark[i-1]];

Here's how you can print an individual element of an array.

// print the first element of the array printf["%d", mark[0]]; // print the third element of the array printf["%d", mark[2]]; // print ith element of the array printf["%d", mark[i-1]];

Example 1: Array Input/Output

// Program to take 5 values from the user and store them in an array // Print the elements stored in the array #include int main[] { int values[5]; printf["Enter 5 integers: "]; // taking input and storing it in an array for[int i = 0; i < 5; ++i] { scanf["%d", &values[i]]; } printf["Displaying integers: "]; // printing elements of an array for[int i = 0; i < 5; ++i] { printf["%d\n", values[i]]; } return 0; }

Output

Enter 5 integers: 1 -3 34 0 3 Displaying integers: 1 -3 34 0 3

Here, we have used a for loop to take 5 inputs from the user and store them in an array. Then, using another for loop, these elements are displayed on the screen.

Example 2: Calculate Average

// Program to find the average of n numbers using arrays #include int main[] { int marks[10], i, n, sum = 0; double average; printf["Enter number of elements: "]; scanf["%d", &n]; for[i=0; i < n; ++i] { printf["Enter number%d: ",i+1]; scanf["%d", &marks[i]]; // adding integers entered by the user to the sum variable sum += marks[i]; } // explicitly convert sum to double // then calculate average average = [double] sum / n; printf["Average = %.2lf", average]; return 0; }

Output

Enter number of elements: 5 Enter number1: 45 Enter number2: 35 Enter number3: 38 Enter number4: 31 Enter number5: 49 Average = 39.60

Here, we have computed the average of n numbers entered by the user.

Access elements out of its bound!

Suppose you declared an array of 10 elements. Let's say,

int testArray[10];

You can access the array elements from testArray[0] to testArray[9].

Now let's say if you try to access testArray[12]. The element is not available. This may cause unexpected output [undefined behavior]. Sometimes you might get an error and some other time your program may run correctly.

Hence, you should never access elements of an array outside of its bound.

Multidimensional arrays

In this tutorial, you learned about arrays. These arrays are called one-dimensional arrays.

In the next tutorial, you will learn about multidimensional arrays [array of an array].

C++ Video Course [Hindi & English]

In simple English, an array is a collection.

In C also, it is a collection of similar type of data which can be either of int, float, double, char [String], etc. All the data types must be same. For example, we can't have an array in which some of the data are integer and some are float.

Why Array?

Suppose we need to store marks of 50 students in a class and calculate the average marks. So, declaring 50 separate variables will do the job but no programmer would like to do so. And there comes array in action.

How to declare an array

datatype   array_name [ array_size ] ;

For example, take an array of integers 'n'.

int n[6];

n[ ] is used to denote an array 'n'. It means that 'n' is an array.

So, int n[6] means that 'n' is an array of 6 integers. Here, 6 is the size of the array i.e. there are 6 elements in the array 'n'.

We need to give the size of the array because the complier needs to allocate space in the memory which is not possible without knowing the size. Compiler determines the size required for an array with the help of the number of elements of an array and the size of the data type present in the array.

Here 'int n[6]' will allocate space to 6 integers.

We can also declare an array by another method.

int n[ ] = {2, 3, 15, 8, 48, 13};

In this case, we are declaring and assigning values to the array at the same time. Here, there is no need to specify the array size because compiler gets it from { 2,3,15,8,48,13 }.

Index of an Array

Every element of an array has its index. We access any element of an array using its index.

Pictorial view of the above mentioned array is:

0, 1, 2, 3, 4 and 5 are indices. It is like they are identity of 6 different elements of an array. Index always starts from 0. So, the first element of an array has a index of 0.

Index of an array starts with 0.

We access any element of an array using its index and the syntax to do so is:

array_name[index]

For example, if the name of an array is 'n', then to access the first element [which is at 0 index], we write n[0].

Here, n[0] is 2 n[1] is 3 n[2] is 15 n[3] is 8 n[4] is 48 n[5] is 13

n[0], n[1], etc. are like any other variables we were using till now i.e., we can set there value as n[0] = 5; like we do with any other variables [x = 5;, y = 6;, etc.].

Assigning Values to Array

By writing int n[ ]={ 2,4,8 }; , we are declaring and assigning values to the array at the same time, thus initializing it.

But when we declare an array like int n[3];, we need to assign values to it separately. Because 'int n[3];' will definitely allocate space of 3 integers in memory but there are no integers in that space.

To initialize it, assign a value to each of the elements of the array.

n[0] = 2;
n[1] = 4;
n[2] = 8;

It is just like we are declaring some variables and then assigning values to them.

int x,y,z;
x=2;
y=4;
z=8;

Thus, the first way of assigning values to the elements of an array is by doing so at the time of its declaration.

And the second method is declaring the array first and then assigning values to its elements.

int n[3]; n[0] = 2; n[1] = 4;

n[2] = 8;

You can understand this by treating n[0], n[1] and n[2] as similar to different variables you used before.

Just like variable, array can be of any other data type also.

float f[ ]= { 1.1, 1.4, 1.5};

Here, 'f' is an array of floats.

First, let's see the example to calculate the average of the marks of 3 students. Here, marks[0] represents the marks of the first student, marks[1] represents marks of the second and marks[2] represents marks of the third student.

#include int main[] { int marks[3]; float average; printf["Enter marks of first student\n"]; scanf[" %d" , &marks[0]]; printf["Enter marks of second student\n"]; scanf[" %d" , &marks[1]]; printf["Enter marks of third student\n"]; scanf[" %d" , &marks[2]]; average = [marks[0] + marks[1] + marks[2]] / 3.0; printf ["Average marks : %f\n" , average]; return 0; }

Output

Enter marks of first student
23 Enter marks of second student

25

Enter marks of third student

30

Average marks : 26.000000

Here you just saw a working example of array, we treated elements of the array in an exactly similar way as we had treated normal variables. &marks[0], &marks[1] and &marks[2] represent the addresses of marks[0], marks[1] and marks[2] respectively.

In the above example, two points should be kept in mind. The average value should be of type 'float' because the average of integers can be float also.

Secondly, while taking out the average, the sum of the numbers should be divided by 3.0 and not 3, otherwise you will get the average value as an integer and not float.

We can also use for loop as done in the next example.

#include int main[] { int n[10]; /* declaring n as an array of 10 integers */ int i,j; /* initializing elements of array n */ for [i = 0; i

Chủ Đề