Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Sunday, August 28, 2022

Array in C programming

 

Array in C programming





Introduction:

Arrays are an essential concept in programming, and they play a vital role in C programming. An array is a collection of elements of the same type that are stored in contiguous memory locations. Arrays in C are widely used to store and manipulate data, and they are an essential tool for any C programmer. In this article, we'll explore the basics of arrays in C programming, including how to declare and initialize arrays, how to access array elements, and how to perform common array operations.


What is an Array?

An array is a collection of similar data types stored in a contiguous memory location. In simple terms, an array is a group of variables of the same type that share the same name and occupy a single block of memory. Each element in an array is identified by an index number, which represents its position in the array.

      

Why do we need an array?

If we have a collection of similar data elements, we have inconvenient to give each one a unique variable name.  If we have to store the age of 10student then we have to use 10 different variable names as in the program below:

/*program to read and display average age of 7 students*/

#include<stdio.h>

#include<conio.h>

void main()

{

int age1,age2,age3,age4,age5,age6,age7;

float avg;

clrscr();

printf("enter the age1\n");

scanf("%d",&age1);

printf("enter the age2\n");

scanf("%d",&age2);

printf("enter the age3\n");

scanf("%d",&age3);

printf("enter the age4\n");

scanf("%d",&age4);

printf("enter the age5\n");

scanf("%d",&age5);

printf("enter the age6\n");

scanf("%d",&age6);

printf("enter the age7\n");

scanf("%d",&age7);

 avg=(age1+age2+age3+age4+age5+age6+age7)/7;

 printf("the average age of seven student is %", avg);

 getch();

 }

 

Output:

 Enter the age1

  23

Enter the age2

 20

  Enter the age3

  21

 Enter the age4

 22

 Enter the age5

16

Enter the age6

17

Enter the age7

18

The average age of seven students is 19.00

 

The above program reads the age of seven students only so we use seven different variables if we have to enter the age of 100 students. Would we declare the 100 different variables then? Of course not.  So clearly, we have a convenient way to refer to such collections of similar data elements with the help of an array. And this problem is solved by the use of an array by providing a way to refer to individual items in a collection by using the same variable name.


Types of Array

Declaring the name and types of an array and defining the number of elements in an array is known as dimensioning of the array. The dimension of the array can be from 1-dimension to n-dimension (multidimensional). In general, there are single-dimensional or multidimensional arrays used in programming. The number of subscripts determines the dimensions of the array.

One Dimensional Array

A one-dimensional array has only a single subscript. The one-dimensional arrays are known as vectors. The subscript numbers identify the number of individual elements in an array. It stores data only row-wise or column-wise.

Declaration of one Dimensional Array

We have to declare the array before using it. While declaring an array we have to define the number of elements we are going to use and the data types that the array can store in it. One dimensional array is declared as:
data_types array_name [size];

Here data_type denotes any valid C data types and array_name  denotes the name of the array and it can be any valid C identifier. The size of the array specifies the number of elements that can be stored in the array. It may be a positive integer constant or constants integer expression.

Here are some examples of a valid array declaration:

int age[100];

float salary[500];

char grade[20];

Following are some of the invalid array declarations:

int value[0];           //zero subscripted array is not supported

int marks[0.15]      // subscript value must be an integer

int n[-40];             // subscript value must be non-negative


Accessing array elements

The element of the array can be accessed by specifying the array name followed by a subscript in brackets. In c, the array subscripts start from 0. Hence if there is an array of size 5 then the valid subscripts will be from 0 to 4. The last valid subscript is one less than the size of the array. The last subscript is sometimes known as the upper bound of the array and 0 is known as the lower bound of the array.

Let us take an array:-

int arr[5];     // size of array is 5, can hold five integer elements.
The elements of this array are:

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

The subscript can be any expression that yields an integer value. It can be any constant, integer variable, integer expression or return value(int) from a function call. For example, if i and j are integer variables then these are some valid subscripted array elements-

arr[3];

arr[i];

arr[i+j];


Processing array elements

For processing arrays, we generally use a for loop and the loop variable is used at the place of subscript.  The initial value of the loop variable is taken as 0 since the array subscript starts from zero. The loop variable is increased by 1 each time so that we can access and process the next elements in the array. The total number of passes in the loop will be equal to the number of elements in the array and in each pass will process one element.
Suppose arr[10]  is an array of int type-

1. Reading values in arr[10]

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

scanf(“%d”,&arr[i]);

 

2. Displaying values of arr[10]

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

   printf(“%d”,arr[i]);

 

3. Adding all elements of arr[10]

sum =0;

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

  sum+=arr[i];   or sum=sum+arr[i];

 

/*program to input values into an array and display them*/

#include<stdio.h>

#include<conio.h>

void main()

{

int arr[10],i;

clrscr();

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

{

printf("Enter the value for arr[%d] :",i);

scanf("%d",&arr[i]);

}

printf("The array elements are :\n");

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

   printf("%d\t",arr[i]);

 printf("\n");

 getch();

}

        

Output: 

Enter the value for arr[0]:10

Enter the value for arr[1]:78

Enter the value for arr[2]:90

Enter the value for arr[3]:67

Enter the value for arr[4]:60

Enter the value for arr[5]: 50

Enter the value for arr[6]:34

Enter the value for arr[7]:23

Enter the value for arr[8]:12

Enter the value for arr[9]:100

The array of elements are:

10    78    90    67    60    50    34    23    12    100


Initialization of array

Each array element can be initialized when an array is declared. The initial values must appear in the order in which they will be assigned to the individual array elements, enclosed in curly braces {} and separated by commas. The syntax for initialization of array is:-

data_type array_name[size]={ value1, value2, value3…………..valueN};

Example is:

int digit[5]={1, 2, 3, 4, 5};

float salary[4]={ 200.50, 69.90, 8.6, 7.87};

char color[3]={‘R’, ‘G’, ‘B’};

 Here the first one has five element values five of which are of integer types. The second one has four element values all are of float types and the last one has three elements all are of char types.


Multidimensional array (two-dimensional arrays)

When we declare an array more than one dimensional it is known as a multidimensional array. It consists of two subscripts in which the first gives the number of rows and the second gives a number of column sizes. The maximum capacity of an array is defined as the product of row size and column size.

Declaration of 2D array

 data_type array_name[row_size][column_size];

Example:

int mat[3][2];

So the above example declaration is a 2D array of integer type. This integer array has been named mat and it can hold up to 6 elements (3rows x 2columns).

          

0

1

0

[0][0]

[0][1]

1

[1][0]

[1][1]

2

[2][0]

[2][1]

In the above example, the position of each element in the 2D array is given.


Conclusion:

In conclusion, arrays are a fundamental concept in C programming, and mastering their use is essential for any programmer. In this article, we've covered the basics of arrays in C programming, including how to declare and initialize arrays, how to access array elements, and how to perform common array operations. With this knowledge, you'll be well on your way to writing efficient and effective C programs that make use of this powerful data structure.

You can also Read:




No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages