0% found this document useful (0 votes)
101 views7 pages

CPPS Unit 3

An array is a data structure that stores a collection of similar type of data elements using contiguous memory locations. In C programming language, arrays can store primitive data types like integers, characters, floats, etc. as well as derived data types like pointers and structures. Elements in an array can be accessed using their index number. Arrays provide benefits like storing similar elements together efficiently and allowing random access to any element. However, arrays have a fixed size that cannot be dynamically increased.

Uploaded by

jsanandkumar22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views7 pages

CPPS Unit 3

An array is a data structure that stores a collection of similar type of data elements using contiguous memory locations. In C programming language, arrays can store primitive data types like integers, characters, floats, etc. as well as derived data types like pointers and structures. Elements in an array can be accessed using their index number. Arrays provide benefits like storing similar elements together efficiently and allowing random access to any element. However, arrays have a fixed size that cannot be dynamically increased.

Uploaded by

jsanandkumar22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Array

 An array 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.
 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.
 All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.

Characteristics Properties of Array


The array contains the following properties.
 An array holds elements that have the same data type.
 Array elements are stored in subsequent memory locations.
 Two-dimensional array elements are stored row by row in subsequent memory locations.
 Array name represents the address of the starting element.
 Array size should be mentioned in the declaration. Array size must be a constant expression and
not a variable.
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can't exceed the
limit. So, it doesn't grow the size dynamically like LinkedList which we will learn later.
DECLARATION OF C ARRAY
We can declare an array in the c language in the following way.
data_type array_name[array_size];
Now, let us see the example to declare the array.
int marks[5];
Here, int is the data_type, marks are the array_name, and 5 is the array_size.
INITIALIZATION OF C ARRAY
The simplest way to initialize an array is by using the index of each element. We can initialize each
element of the array by using the index. Consider the following example.
1. marks[0]=80;//initialization of array
2. marks[1]=60;
3. marks[2]=70;
4. marks[3]=85;
5. marks[4]=75;

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.

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.
1. // take input and store it in the 3rd element
2. scanf("%d", &mark[2]);
3.
4. // take input and store it in the ith element
5. scanf("%d", &mark[i-1]);
Here's how you can print an individual element of an array.
1. // print the first element of the array
2. printf("%d", mark[0]);
3.
4. // print the third element of the array
5. printf("%d", mark[2]);
6.
7. // print ith element of the array
8. printf("%d", mark[i-1]);
Example 1: Array Input/Output
1. // Program to take 5 values from the user and store them in an array
2. // Print the elements stored in the array
3. #include <stdio.h>
4.
5. int main() {
6. int values[5];
7.
8. printf("Enter 5 integers: ");
9.
10. // taking input and storing it in an array
11. for(int i = 0; i < 5; ++i) {
12. scanf("%d", &values[i]);
13. }
14.
15. printf("Displaying integers: ");
16.
17. // printing elements of an array
18. for(int i = 0; i < 5; ++i) {
19. printf("%d\n", values[i]);
20. }
21. return 0;
22. }
Output
Enter 5 integers: 1
-3
34
0
3
Displaying integers: 1
-3
34
0
3
Example 2: Calculate Average
1. // Program to find the average of n numbers using arrays
2.
3. #include <stdio.h>
4. int main()
5. {
6. int marks[10], i, n, sum = 0, average;
7.
8. printf("Enter number of elements: ");
9. scanf("%d", &n);
10.
11. for(i=0; i<n; ++i)
12. {
13. printf("Enter number%d: ",i+1);
14. scanf("%d", &marks[i]);
15.
16. // adding integers entered by the user to the sum variable
17. sum += marks[i];
18. }
19.
20. average = sum/n;
21. printf("Average = %d", average);
22.
23. return 0;
24. }
Output
Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39
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.
TYPES OF C ARRAYS:
There are 2 types of C arrays. They are,
1. One dimensional array
2. Multi dimensional array
 Two dimensional array
 Three dimensional array
 four dimensional array etc…
1. ONE DIMENSIONAL ARRAY IN C:
Syntax : data-type arr_name[array_size];
Array declaration, initialization and accessing Example

Integer array example:


int age [5];
int age[5]={0, 1, 2, 3, 4};
age[0]; /*0 is accessed*/
age[1]; /*1 is accessed*/
age[2]; /*2 is accessed*/
Array declaration syntax:
Character array example:
data_type arr_name [arr_size];Array initialization
syntax: char str[10];
data_type arr_name [arr_size]=(value1, value2, char str[10]={‘H’,‘a’,‘i’};
value3,….);Array accessing syntax: (or)
arr_name[index]; char str[0] = ‘H’;
char str[1] = ‘a’;
char str[2] = ‘i;

str[0]; /*H is accessed*/


str[1]; /*a is accessed*/
str[2]; /*i is accessed*/
EXAMPLE PROGRAM FOR ONE DIMENSIONAL ARRAY IN C:
#include<stdio.h>

int main()
{
int i;
int arr[5] = {10,20,30,40,50};

// declaring and Initializing array in C


//To initialize all array elements to 0, use int arr[5]={0};
/* Above array can be initialized as below also
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50; */

for (i=0;i<5;i++)
{
// Accessing each variable
printf("value of arr[%d] is %d \n", i, arr[i]);
}

}
OUTPUT:
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50
2. TWO DIMENSIONAL ARRAY IN C:
 Two dimensional array is nothing but array of array.
 syntax : data_type array_name[num_of_rows][num_of_column];

Array declaration, initialization and


accessing Example

Array declaration syntax: Integer array example:


data_type arr_name int arr[2][2];
[num_of_rows][num_of_column];Array int arr[2][2] = {1,2, 3, 4};
initialization syntax: arr [0] [0] = 1;
data_type arr_name[2][2] = arr [0] ]1] = 2;
{{0,0},{0,1},{1,0},{1,1}};Array accessing arr [1][0] = 3;
syntax: arr [1] [1] = 4;
arr_name[index];

EXAMPLE PROGRAM FOR TWO DIMENSIONAL ARRAY IN C:


#include<stdio.h>

int main()
{
int i,j;
// declaring and Initializing array
int arr[2][2] = {10,20,30,40};
/* Above array can be initialized as below also
arr[0][0] = 10; // Initializing array
arr[0][1] = 20;
arr[1][0] = 30;
arr[1][1] = 40; */
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
{
// Accessing variables
printf("value of arr[%d] [%d] : %d\n",i,j,arr[i][j]);
}
}
}
OUTPUT:
value of arr[0] [0] is 10
value of arr[0] [1] is 20
value of arr[1] [0] is 30
value of arr[1] [1] is 40

C – String

You might also like