Course Content
Detailed Content of Programming in C
0/1
Introduction
0/1
Structure of C program
0/1
Answers of ‘C’ Pointers Programs
0/1
About Lesson

One Dimensional Array:

A one-dimensional array is a linear collection of elements, all of the same data type, arranged in a sequence. It is a fundamental data structure in computer programming and is often used to store and manipulate a fixed-size sequential collection of elements. Each element in the array is identified by its index or position within the array, starting from 0.

Declaration: 

Before using the array in the Program, it must be Declared:

Syntax: 

data_type array_name[size];

  • ‘data-type’ represents the type of elements present in the array.
  • array_name’ represents the name of the array.
  • size‘ represents the number of elements that can be stored in Array.

Example: 

int age[1oo];

float sal[15];

char grade[20];

Here, ‘age’ is an integer type array, which can store 100 Elements of Integer Type. The Array ‘sal’ is floating type array of size 15, can hold float values ‘Grade’ is a character type array which holds 20 Characters.

Initialization: 

We can explicitly initialize arrays at the time of declaration.

Syntax: 

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

  • ‘value1, value2, valueN’ are the constants values known as initializers, which are assigned to the array elemens one after another.

Example: 

int marks[5]={10,2,0,23,4};

The values of the array elements after this initialization are:

marks[0]=10,

marks[1]=2,

marks[2]=0,

marks[3]=23,

marks[4]=4

Note:

  1. In 1-D arrays it is optional to specify the size of the array. If size is omitted during initialization, then the compiler assumes the size of array equal to the number of initializers.
    • Example: int marks[]={10,2,0,23,4}; 
    • Here the size of array marks is initialized to 5.
  2. We can’t copy the elements of our array to another array by simply assigning it.
    • Example: int a[5]={9,8,7,6,5}; 
    • int b[5];
    • b=1;//not valid
    • to copy all the elements by using for loop:
    • for(a=i; i<5; i++)
    • b[i]=a[i];

Processing: 

For Processing Arrays we mostly use for loop. The total no. of passes is equal to the no. of elements present in the array and in each pass one element is processed.

Example: 

#include <stdio.h>
int main()
{
    int a[3], i;
    for (i = 0; i <= 2; i++) // Reading the array values
    {
        printf(“Enter the Elements”);
        scanf(“%d”, &a[i]);
    }
    for (i = 0; i <= 2; i++)
    {
        printf(“%d”, a[i]);
        printf(“n”);
    }
}

Output: The Program reads and displays three elements of integer type

Enter the Elements3
Enter the Elements4
Enter the Elements5
3
4
5