2. What is an array?
⢠An array is a variable that can store multiple values.
⢠More formally, it is defined as the collection of similar type of data items stored
at contiguous memory locations.
⢠Arrays are the derived data type in C programming language which can store the
primitive type of data such as int, char, double, float, etc. It also has the capability
to store the collection of derived data types, such as pointers, structure, etc.
⢠The array is the simplest data structure where each data element can be
randomly accessed by using its index number.
3. Why need array?
⢠C array is beneficial if you have to store similar elements. For example, if we want
to store the marks of a student in 6 subjects, then we don't need to define
different variables for the marks in the different subject. Instead of that, we can
define an array which can store the marks in each subject at the contiguous
memory locations.
⢠By using the array, we can access the elements easily. Only a few lines of code are
required to access the elements of the array.
4. How to declare an array?
For example,
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.
dataType arrayName[arraySize];
float mark[5];
5. 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.
6. 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.
7. How to initialize an array?
⢠It is possible to initialize an array during declaration. For example,
⢠You can also initialize an array like this,
⢠Here, we haven't specified the size. However, the compiler knows its size
is 5 as we are initializing it with 5 elements.
int mark[5] = {19, 10, 8, 17, 9};
int mark[] = {19, 10, 8, 17, 9};
8. 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;
9. Input and Output Array Elements
// take input and store it in the 3rd element
scanf("%d %d", &mark[2], &mark[3]);
// take input and store it in the ith element
scanf("%d", &mark[i-1]);
// 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]);
0; i<= 4; i++)
â, &mark[i]);
10.
11.
12. Access elements out of its bound!
Suppose you declared an array of 10 elements. Let's say,
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.
int testArray[10];
13. Important things to know about Arrays
⢠Array indexes always begin with 0. Hence when we say array of size 10, array has
elements from index 0 to 9. If we specify or use array as intArr[10], intArr[11],
intArr[200], the C compiler will not show any error, but we will get run time errors
while executing the program.
⢠Arrays are supported by primitive datatypes, non-primitive types like structures,
unions, pointers etc.
14. Advantages of C
⢠In an array, accessing an element is very easy by using the index number.
⢠The search process can be applied to an array easily.
⢠2D Array is used to represent matrices.
⢠For any reason a user wishes to store multiple values of similar type then the
Array can be used and utilized efficiently.
15. Disadvantages of C
⢠It allows us to enter only fixed number of elements into it. We cannot alter the size
of the array once array is declared. Hence if we need to insert more number of
records than declared then it is not possible. We should know array size at the
compile time itself.
⢠Inserting and deleting the records from the array would be costly since we add /
delete the elements from the array, we need to manage memory space too.
⢠It does not verify the indexes while compiling the array. In case there is any indexes
pointed which is more than the dimension specified, then we will get run time errors
rather than identifying them at compile time.
16. ⢠Array size is fixed: The array is static, which means its size is always fixed. The
memory which is allocated to it cannot be increased or decreased.
17. ⢠Explanation: In the above program the array of size 10 is declared and the value
is assigned at a particular index. But when the value at index 11 is printed then it
prints the garbage value because the array was accessed out of the bound index.
In some compiler, it gives error as âArray Index Out Of Bound.â
18. ⢠Array is homogeneous: The array is homogeneous, i.e., only one type of value
can be store in the array. For example, if an array type âintâ, can only store integer
elements and cannot allow the elements of other types such as double, float,
char so on.
19. ⢠Explanation: The above code gives âCompilation Errorâ as an integer type array is
assigned value to a string and float type.
20. ⢠Array is Contiguous blocks of memory: The array stores data in contiguous(one
by one) memory location.
5 7 12 9 28
0 1 2 4
3
21. ⢠Insertion and deletion are not easy in Array: The operation insertion and
deletion over an array are problematic as to insert or delete anywhere in the
array, it is necessary to traverse the array and then shift the remaining elements
as per the operation. This operation cost is more.
22. C Program Exercises
⢠Write a C program to perform input/output of all basic data types.
⢠Write a C program to enter two numbers and find their sum.
⢠Write a C program to enter two numbers and perform all arithmetic operations.
⢠Write a C program to enter length and breadth of a rectangle and find its
perimeter.
⢠Write a C program to enter length and breadth of a rectangle and find its area.
⢠Write a C program to enter radius of a circle and find its diameter, circumference
and area.
⢠Write a C program to enter length in centimeter and convert it into meter and
kilometer.
⢠Write a C program to enter temperature in Celsius and convert it into Fahrenheit.
⢠Write a C program to enter temperature in Fahrenheit and convert to Celsius
⢠Write a C program to enter marks of five subjects and calculate total, average and
percentage.
23. ⢠Write a C program to reverse a number.
⢠Write a C program to find maximum between two numbers.
⢠Write a C program to find maximum between three numbers.
⢠Write a C program to check whether a number is negative, positive or zero.
⢠Write a C program to check whether a number is divisible by 5 and 11 or not.
⢠Write a C program to check whether a number is even or odd.
⢠Write a C program to check whether a year is leap year or not.
⢠Write a C program to input electricity unit charges and calculate total
electricity bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill