Skip to main content

Array in C Language Btech Bca Notes

 Array in C Language Btech Bca Notes

Array in C Language Btech Bca Notes

An array is a data form that can hold several values all of the same type. An array is a collection of variable of the same type that are referral to by a common name.
Also Read: Beginner our view on C programming Language .
Array are vital for most programming language. They are collection of variables, which we call elements.
array in C language
Each of the element in an array is stored sequentially in the computeric memory. This making it easy to manipulate them and navigate among them.

Since all the elements in an array are of same type, array allows us to represent a group of similar elements as an ordered  sequence  and work on them as a whole (grouping together related data). Array organize data in such a way that it can be easily sorted.

To create an array, we use a declaration statement. An array declaration should indicate three things:

1. The type of value to be stored in each element.

2. The name of the array.

3. The number of elements in the array.

The basic format of an array declaration is as follows:

Syntax:- 
datatype  name of the array [Number of elements/Size of array]; 
For Example:-  
Int temperature[7];
Where-
Int- type/datatype of each element.
Temperature- Name of the array.
[7]- Size of array or Number of element in array.

array in C language
Each element, In essence, is a variable that can be treated as a simple variable.

Each element in an array is assigned a unique index number. The index number of the first element is 0, Index number of second element 1, and so on. The index number of the last element is size -1.
To access an element in an array, We use square brackets( [ ] ) after the array name specify the index of the element to be accessed.
array name [index Number]
Sample Code:
#Sample code of an array

#include<conio.h>
#include<stdio.h>
void main()
{
int a[10]; //array created
int i; clrscr();
printf("enter 10 rollno's:\n");
for(i=0; i<=9; i=i+1)
    {

        scanf("%d",7a[i]);

    }
printf("the data is\n");
for(i=0; i<=9; i=i+1)
    {

        printf("%d ",a[i]);

    }
getch();
}

* Initializing element of an array at the time of creation.

We can initialize the values of the individual array element at the time of declaring/creating an array. The is done by declaring the values after the array declaration. The values are enclosed in a block and are separated by a comma.
Syntax:-  datatype name of the array [size]={...values...};
The number of values should not exceed the size of the array.
Also Read: Variables & Datatypes In C Language .
For example:-.
int x[5]={1,9,8,113,0};
Will create an array and also initialize/fill its elements at the time of creation.

Initializing element of an array at the time of creation.
If we initialize elements of an array at the time of declaration/creation, then we can even leave the [ ] (square brackets) after the array name empty.
For Example:-   
int x[ ]={0,4,7,-6,13};
In such a case, the size of the array will depend on the number of values in the blocks. The size will be automatically determine by compiler.
Sample Code:
//Program to Initialize elements of an array at the time of creation. 

#include<conio.h>
#include<stdio.h>
void main()
{
int i;
int a[7]={ 5,6,3,6,8,9,11,13 };
clrscr();
printf("The data is\n");
for( i=0; i<=6; i=i+1)
    {

        printf("%d ", a[i]);

    }
getch();
}

* Types of array

Types of array

A. One Dimensional B. Multi Dimensional

One Dimensional array

One dimensions array is a list of related variable. 1d array are also called Vectors in mathematics. While creating a one dimensional array, a single square bracket ([ ]) is used/specified after the variable/array name.
Syntax:- datatype array name [size];

Multi Dimensional array

A Multidimensional array is an they has two or more dimensions, and an individual element is Access through the combination of two or more indices/square brackets.
Syntax:- array name[r][c];
Where, r= total number of rows
        c=total number of columns

Array can be used to represent multidimensional data, Just like, a chess board or a checker board. An array can have any number of dimensions. Multidimensional array are used for tables and other complex arrangements.
A multidimensional array is an array of array. There is no limit, in principle, to the number of indices which an array can have.
The simplest form of multidimensional array is the two dimensional. In a two dimensional array, The location of any specific element is specified by two indices. If you think of a two dimensional array as a table of information, one index indicates the row, the other indicates the column.

Multi Dimensional array in C

Creating A Two Dimension Array

Syntax:- datatype name of the array[r][c];
Where, datatype= Any valid datatype
        r= total number of rows
        c= total number of columns
For Example:-
int a[3][3];
The array a has 3 rows and 3 columns. Both rows and columns are indexed from 0.

Creating A Two Dimension Array in c
To access any element in a two dimensional array, we need two indicates to pin-point it- one for the row, and one for the column.
array name  [rows no.] [column no.]

Creating A Two Dimension Array in c
For example:-
To access the 2nd element in the first row of array a, we will use:
a [0][1]
Although we can visualize two dimensional arrays as tables, that not the way they are actually stored in computer memory. C stores array in row-major order, with row 0 first, then row1, and so forth.
For example:- Int  a[3][3];

An array cannot be stored in the memory as grid.

Initializing two dimensional array

Multidimensional arrays can be initialized by specifying bracketed values for each row.
For Example:-
Int  x[2][4]={ {5,6,4,10}  //data of 1st row 
            {3,5,-6,-9} }; //data of 2nd row
We can also initialize a two dimensional array by providing the initial values in one sequence inside a curly braces, Omitting the inner braces(used to represent individual rows).
For Example:-
int  rollno.s [3][5]={21,,4,5,32,               
                          43,11,9,41,8,9,           
                          59,5,32,81,100 };
 /*once the compiler has seen enough values  to fill 1 row it begins filling the next*/

Initializing two dimensional array in c

Accessing elements of a two dimensional array

Nested for loop are ideal for processing multidimensional arrays. A pair of nested for loops - one that steps through every row index and one that steps through each column index.
Sample Code:
//Program of Accesing A 2d Array.
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j;         // i- For rows and j- For Columns
int a[3][3];
clrscr();
printf("Enter 9 values\n");
for( i=0; i<=2; i=i+1 )
    {
        for( j=0; j<=2; j=j+1 )
        {
            scanf("%d",&a[i][j]);
        }
    }
printf("The data is\n");
for( i=0; i<=2; j=j+1)
    {
        for( j=0; j<=2; j=j+1)
        {
            printf("%d ",a[i][j]);
        }
printf("\n");
        }
getch();
}


We will going on series of C Programming language of all topics. So, you can follow us for more updates.

Comments

Popular posts from this blog

Control Statements in c language

So, Today we are going on new topic name control statements in C language. In which we read Looping(For, While and Do While), Jumping(Break, goto and Continue) and Decision Control statements(Switch, If, If Else and  If else if ladder)  in C. Before you read this article May you also like our previous posts :-    Structure Of C Language Program   Variables & Datatypes In C Language    Format Specifier In C Programming  .   It may help you to understanding it. A Statement is the smallest unit that is a complete instruction in itself statements contains expression and usually end with a semicolon. Statements are of two Types:- Types of Statements in C  1. Sequential Statement   2. Control Statement Sequential Statements are statements which are executed by one in sequential manner. Control Statements are the statements which alter the flow of execution and provide better control to the programme. On the flow of execution. They are useful to write better and complex program. Control S

Beginner our view on C programming Language

  Beginner our view on C programming Language   Beginner ourview on C programming Language History about C programming Language. The founder or we can say developer of C programming language was Dennis Ritchie. He was found in  Bell labs USA. The Birth of C language is originally on 1972. After the time C is widely popular all around the world. C is also used in to develop many OS system some are Linux and Windows. Now days C is still widely used or popular language in Programming world. Here, we have a question is What is a Program ?  A Program is a set of instruction which on   execution by a machine produces a specific result. A machine needs set of instructions to perform an operation. Those set of instruction are contained in a program. What is program? What is Programming languages ? What Is a Programming Languages? Meaning- A programming language allows a programmer to develop instructions which can be executed by a machine. A programming language acts as an intermediate bet

What is Blogger? How we create a account on it.

What is Blogger ? How we create a account on it. Welcome to blog. Here I am talking about blog on blogger.com. Hope you guys like it, Thanks. Here, I will talk about Ø What is blog. Ø About blogger. Ø How to create an account on blogger. Ø Can we earn money to blog. What is Blog Blog is a kind of platform, where you can write your thinking about that particular topic. Also you can say blog is article which we write through blogger.com and WordPress, etc. After writing the blog we go to publish in internet. If we write on a unique content hope traffic will came on our blog. You should write a blog on different topics like - Games, Technical, programming, Knowledge, education, and many more you like. What is blogger.com     Blogger is website that manages by Google. Google bought blogger from Pyra labs in 2003.Blogger is free for all, it give space to our passion in his platform. Around the world millions of peoples are used blogger daily and post something every