Introduction to Arrays in C

Introduction to Arrays in C Language

 
On this page we will discuss about introduction to arrays in C . Arrays are the kind of data structure which can be defined as the collection of similar type of elements arranged in contiguous memory allocation.
introduction to arrays in V

Introduction To Arrays In C Programming

  • Arrays are the derived data types in C programming Language.
  • Array can store the elements of all primitive data type such as int, char, float, double etc.
  • Arrays can also store the collection of derived data types such as pointers, structures etc.
  • While Storing the elements in contiguous fashion the lowest address corresponds to the first element of array

Why should we use array in Programming

Let us consider a scenario where you have to store the marks of 5 students, you can use 5 float variables as student1, student2, and so on. But, if you have to store the marks of 100 students this method would takes a lot of time.
The most efficient way to get this task done is to create an float array with name as student and size as 100.

Introduction to Arrays in C 1

Declaration of Arrays in C Programming Language

 Declaration of Array can be done by the following way in C Languge:

    Syntax : Data_type  array_name  [Array_size] ;

Example :
    integer Array : int student [50];
    char Array : char student [50]; (Character Array is also known as String in C Programming Language)
    float Array : float student [50];
    double Array : double student [50];

Initialization of Arrays in C Programming Language

 Array can be initialized in C programming language in the compile time or in the run time.

Initialization of Array in Compile Time

Initialization of Array at compile means declaring the elements of array before running the code at the time of declaration which can be done as follow:

#include<stdio.h>
int main()
{
    //integer array
    int student[5] = {60, 70, 65, 80, 85};
    //float array
    float student[5] = {60.0, 75.5, 80.0, 67.0, 83.5};
    return 0;
}

Initialization of Array in Run Time

Initialization of Array at Run Time means declaring the elements of array after running the code. Here, elements are taken from user.
we can also set the the size of array when we initialize array at run time.

#include<stdio.h>
int main()
{
    int n;                          //size of array
    scanf("%d",&n);
    int student[n];                 //array
    for(int i=0; i<n; i++)
        scanf("%d",&student[i]);
    return 0;
}

Accessing the elements of Arrays in C Programming Language

The elements of array can be accessed by programmer with the help of index number of the element.
Note: 1st element is present at the 0th index, 2nd element is present at the 1st index, and so on.

Let us consider another scenario where teacher has to input marks of 5 students, and has to calculate the average of marks, but before calculating the average teacher notice that the marks of 3rd and 5th student needs to incremented by 7.

This can be solved in C programming Language as follows

Run
#include<stdio.h>
int main()
{
int n; //size of array
scanf("%d",&n);
int student[n]; //decleration of array
int average, sum=0;
for(int i=0; i<n; i++)
{
scanf("%d",&student[i]); //initialization of array
}
student[2] = student[2] + 7; //accessing 3rd element
student[4] = student[4] + 7; //accessing 5th element

for(int i=0; i<n; i++)
{
sum = sum + student[i]; //accessing elements via loop
}
average = sum / n; //calculating average

for(int i=0; i<n; i++)
{
printf("Marks of student %d : %d\n", i+1,student[i]);
}
printf("Sum is : %d\n",sum);
printf("Average is : %d",average);
return 0;
}

Output

5
60 70 83 65 88
Marks of student 1 : 60
Marks of student 2 : 70
Marks of student 3 : 90
Marks of student 4 : 65
Marks of student 5 : 95
Sum is : 380
Average is : 76
Introduction to Arrays in C

Proving Array follows Contagious Memory Allocation

 The program given below proves that the Array follows contagious memory allocation

Run

#include<stdio.h>
int main()
{
    int student[5] = {60, 70, 83, 65, 88}; 
    printf("Size of integer in this compiler is %d\n",sizeof(int));
    for(int i=0 ;i<5 ;i++)
    {
        printf("Address student[%d] is %d\n", i, &student[i]);
    }                   
    return 0;
}

Output

Size of integer in this compiler is 4
Address student[0] is 6422280
Address student[1] is 6422284
Address student[2] is 6422288
Address student[3] is 6422292
Address student[4] is 6422296

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java