Pages

This blog is under construction

Thursday, January 10, 2019

Introduction to Arrays in c

In C programming, one of the frequent problems is to handle similar types of data. For example: if the user wants to store marks of 100 students, this can be done by creating 100 variables individually that increase the code complexity. These types of problem can be handled in C programming using arrays.

An array in C Programming can be defined as the number of memory locations, each of which can store the same data type and which can be referenced through the same variable name. It is a collective name given to a group of similar quantities.

Thus we can say an array is a sequence of data item of homogeneous values (same type). These values could be all integers, floats or characters etc. 
We have two types of arrays:
1.       One-dimensional arrays.  2. Multidimensional arrays.

A one-dimensional array is a list of related variables. The general form of a one-dimensional array declaration is:

data_type  array_name[array_size];

a) data_type: base type of the array, determines the data type of each element in the array
b) array_size: how many elements the array will hold
c) array_name: the name of the array

For example: int age[5];

An individual element within an array is accessed by use of an index. An index describes the position of an element within an array. Note: In C the first element has the index zero!

The following will be the result of the above declarations:
    age[0]        age[1]     age[2]      age[3]      age[4]






 
Initializing Arrays 
Initializing of array is very simple in c programming. The initializing values are enclosed within the curly braces in the declaration and placed following an equal sign after the array name. Here is an example which declares and initializes an array of five elements of type int. An array can also be initialized after declaration. Declaration and initialization of an array.

int age[5]={2,3,4,5,6};

It is not necessary to define the size of arrays during initialization e.g.
int age[]={2,3,4,5,6};

In this case, the compiler determines the size of array by calculating the number of elements of an array.
  
      age[0]      age[1]      age[2]      age[3]      age[4]
  2
  3
  4
   5
   6

Accessing array elements: 
In C programming, arrays can be accessed and treated like variables in C.
 For example:
scanf("%d",&age[3]); //statement to insert value in the forth element of array age[] 
printf("%d",age[3]); //statement to print forth element of an array.
Arrays can be accessed and updated using its index. An array of n elements, has indices ranging from 0 to n-1. C arrays may not display any warnings if out of bounds indices are accessed. Instead, compiler may access the elements out of bounds, thus leading to critical run time errors.
Examples:
Write a c program to find the sum of five input numbers.

#include <stdio.h>
int main()
{
int num[5];
int i,sum=0;
printf("Enter 5 numbers:\n ");
for(i=0;i<5;i++){
printf("Enter number %d: ",i+1);
scanf("%d",&num[i]); 
}
for(i=0;i<5;i++)
{
sum=sum+num[i];
}
printf("Sum of numbers = %d",sum);
return 0;
}

Enter 5 numbers:                                                                                                                 
Enter number 1: 44                                                                                                              
Enter number 2: 33                                                                                                               
Enter number 3: 22                                                                                                               
Enter number 4: 55                                                                                                               
Enter number 5: 11                                                                                                               
Sum of numbers = 165    


No comments:

Post a Comment