ARRAY IN C

📚 Topics Covered

WHAT IS AN ARRAY?

DEFINITION:

An array is a varible which store multiple values of homogenous (same) datatype. It is a type of variable
which stores more than 1 value. Each value is stored inside an index number. If you want to access a single
value from the array we use index number to access each of the single value stored inside an array.

real life example of a variable

In the above example, We have created an ARRAY which can store 5 values inside it.

TYPES OF ARRAYS

There are 3 types of Array statements in C:

real life example of a variable

In this chapter we are going to learn about only 2 types: 1-D & 2-D Array.

1. 1-D ARRAY

SYNTAX: TO CREATE AN ARRAY


datatype arrayName[arraysize] = {value1,value2,....valueN};
    

EXAMPLE:


int subject_marks[6] = {35,45,49,40,38,32};
    

NOTE:

real life example of a variable

Each value inside the array is stored inside an index number. Index value starts from 0 and ends with n-1.
Where n is size. Meaning, 1st value is stored inside index number 0 and last nth value is stored inside n-1 index number.

PROGRAM 1: DISPLAY 5 VALUES ENTERED BY THE USER

EXAMPLE:


//Write a program to take 5 subject marks from the user & display the subject marks on the screen using 1-D Array.

#include

int main()
{
	int subject_marks[5],i;

	printf("Enter 5 subject marks according to this sequence:\n1. English\n2. Hindi\n3. Marathi\n4. Maths\n5. Science\n\n-> ");

	for(int i=0;i<5;i++)
	{
		scanf("%d",&subject_marks[i]);
	}
	
	//Displaying Marks obtained in Each Subject
	printf("\n\nDisplaying marks....\nAccording to the sequence given above.\n\n");

	for(i=0;i<5;i++)
	{
		printf("%d\n",subject_marks[i]);
	}
	return 0;
}
    

In the above program, We used 1-D Array to store 5 subject values and displayed on the screen.

OUTPUT:


Enter 5 subject marks according to this sequence:
1. English
2. Hindi
3. Marathi
4. Maths
5. Science

-> 38
34
48
44
39
49


Displaying marks....
According to the sequence given above.

38
34
48
44
39
49
    

2. 2-D ARRAY

DEFINTIION:

2-D Array stands for 2 Dimensional Array. It consists of 2 sizes:

  1. size1 -> row size
  2. size2 -> column size
With the help of 2-D Array we can create tables.

SYNTAX: TO CREATE 2-D ARRAY

There are many ways to create an 2-D Array but we will use only 1 way to create the variable with value.


datatype arrayName[size1][size2] = {value1,value2,value3,.......}; //Where size1 = rowsize AND size2 = columnsize
    

For example, we have given rowsize & columnsize as 3 & 4 so we can add upto 12 values in the 2-D Array.

EXAMPLE:


//Write a program to take 4 values from the user and then display the values in the form of 2 x 2 Matrix

#include

int main()
{
	int i,j,numbers[2][2];

	printf("Enter the numbers:\n");
	for(i=0;i<2;i++)
	{
		for(j=0;j<2;j++)
		{	
			scanf("%d",&numbers[i][j]);
		}
	}

	
	printf("\n\nDisplaying the values in the form of 2 x 2 Matrix\n\n");
	for(i=0;i<2;i++)
	{
		for(j=0;j<2;j++)
		{
			printf("%d\t",numbers[i][j]);
		}
		printf("\n");
	}

	return 0;
}
    

In the above program, We have used 2-D Array to store the values and display the value in the form of 2 x 2 Matrix.

OUTPUT:


Enter the numbers: 
2
3
4
5


Displaying the values in the form of 2 x 2 Matrix


2	3
4	5
    

NOTE:

To understand about the 2-D array you must be familiar with the Nested for loop concept as well as the Conditional statements in C.

PASSING ARRAY TO FUNCTION

DEFINITION:

In this topic, we learn about how to pass an array as an argument to a function having array as an parameter.

EXAMPLE:


//Write a program to pass subject marks to displayMarks() 

#include stdio.h

void displayMarks(int marks[]);

int main()
{
	int subject_marks[5],i;
	
	printf("Enter 5 subject marks: \n");
	
	for(i=0;i<5;i++)
	{
		scanf("%d",&subject_marks[i]);
	}

	displayMarks(subject_marks);	

	return 0;
}

void displayMarks(int marks[])
{
	int i;
	printf("\nDisplaying marks....\n\n");
	for(i=0;i<5;i++)
	{
		printf("Subject %d: %d\n",i+1,marks[i]);
	}
}
    

In the above program, We have passed subject_marks array as an argument to the parameter of the displayMarks().

OUTPUT:


Enter 5 subject marks:
35
40
45
48
32

Displaying marks....

Subject 1: 35
Subject 2: 40
Subject 3: 45
Subject 4: 48
Subject 5: 32
    

Video Explanation

If video not loading,watch on YouTube

Practice Task

Write a program to calculate sum of n natural numbers

Challenge

Try to create an interesting mini project with Arrays as we can store multiple value inside a variable.