C Programming

8-Arrays in C Programming

C Programming Lessons, Learn C Programming, Introduction to C Language, How to Program with C, Arrays

Arrays are data structures that are related to each other and contain data of the same type.

Simply, an array is actually a variable with many elements.

In the following example, let’s examine an integer array named C.

It has 12 members. Any of the elements can be called by the array name and element position.

The first element of an array is always the zeroth element. That is, the i’th element can be represented as c[i-1].

c[0]        -45

c[1]        6

c[2]        0

c[3]        72

c[4]        1543

c[5]        -89

c[6]        0

c[7]        62

c[8]        -3

c[9]        1

c[10]      6453

c[11]      78

  • the name of the array is the same for all of them, the numbers indicating the position of the element in the array are increasing from 0 to 11.
  • arrays start with the 0th element

For example, to collect the first 3 elements;

                printf(“%d”, c[0]+c[1]+c[2])

to divide the 7th element by 2 and assign the result to x;

                x=c[6]/2;

An example , assigning 0 to all elements of an array

/*assigning 0 to an array with 10 elements*/

#include<stdio.h>
int main()
{
    int n[10], i;   //defining an array with 10 elements
    for(i=0;i<=9;i++)
    {
        n[i]=0; //each element of the array is assigned 0, respectively
    }
    printf(" %s %13s\n", "Element", "Value"); //13 has been used to leave a gap
    for(i=0;i<=9;i++)
        printf("%5d %13d\n", i, n[i]);
    return 0;
}
assigning zero to all elements of an array 
arrays in c programming
assigning zero to all elements of an array
/* assigning even numbers from 2 to 20 to the aray s */

#include<stdio.h>
#define SIZE 10

int main()
{
    //assigning even numbers from 0 to 20 to each element of the array
    int s[SIZE], j;
    for(j=0;j<=SIZE-1;j++)
        s[j]=2+2*j;
    //print the results on the screen
    printf("%s %13s\n", "ELEMENT", "VALUE");
    for(j=0;j<=SIZE-1;j++)
        printf("%4d %14d\n",j,s[j]);
    return 0;
}
assigning even numbers from 2 to 20 to the aray s
assigning even numbers from 2 to 20 to the aray s

Multidimensional Arrays

Arrays of arrays also known multidimensional arrays.

Example;

int b[2][2] = {{6,3}, {7,5}}

6 and 3                 b[0][0] and b[0][1]

7 and 5                 b[1][0] and b[1][1]

/* find the sum of two matrices of order 2*2 */


#include <stdio.h>
int main()
{
  int a[2][2], b[2][2], sum[2][2];

  // Taking input using nested for loop
  printf("Enter elements of 1st matrix\n");
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
    {
      printf("Enter a%d%d: ", i+1, j+1);
      scanf("%d", &a[i][j]);
    }

  // Taking input using nested for loop
  printf("\nEnter elements of 2nd matrix\n");
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
    {
      printf("Enter b%d%d: ", i+1, j+1);
      scanf("%d", &b[i][j]);
    }

  // adding corresponding elements of two arrays
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
    {
      sum[i][j] = a[i][j] + b[i][j];
    }

  // Displaying the sum
  printf("\nSum Of 2 Matrices: \n");

  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
    {
      printf("%d\t", sum[i][j]);

      if (j == 1)
        printf("\n");
    }
  return 0;
}
find the sum of two matrices of order 2*2
find the sum of two matrices of order 2*2

Sorting Arrays

In the example below we will print random numbers in ascending order.

/* printing random numbers in ascending order */

#include<stdio.h>
#define SIZE 10

int main()
{
    int a[SIZE]={2,6,4,8,10,12,89,68,45,37};
    int i, round, hold;

    printf("Variables are in original order\n");
    for(i=0;i<=SIZE-1;i++)
        printf("%4d", a[i]);
    for(round=1; round<=SIZE-1; round++) /*each round*/
        for(i=0;i<=SIZE-2;i++)  /*1 round*/
        if(a[i]>a[i+1]) //compare
    {
        hold=a[i]; //change
        a[i]=a[i+1];
        a[i+1]=hold;
    }

    printf("\n\n Variables are in ascending order\n");
    for(i=0;i<=SIZE-1;i++)
        printf("%4d",a[i]);
    printf("\n");
    return 0;
}
printing random numbers in ascending order 
arrays in c programming
printing random numbers in ascending order

Leave a Reply

Your email address will not be published. Required fields are marked *