top of page
Blog: Blog2

Array

  • Writer: Michael Felix
    Michael Felix
  • Dec 13, 2018
  • 1 min read

An array is a collection of data values ​​of the same type in a certain order that uses the same name.


According to its dimensions, arrays can be divided into:


1. One dimensional array

o Each array element can be accessed through an index


o The index array by default starts at 0


o Array declaration:

array_name [size]

Example

// Program to find the average of x (x < 10) numbers using arrays

#include <stdio.h>
int main()
{
     int marks[10], i, x, sum = 0, average;
     printf("Enter number: ");
     scanf("%d",&x);
     for(i=1; i<=x; ++i)
     {
          printf("Enter marks: " );
          scanf("%d",&marks[i]);
          sum += marks[i];
     }
     average = sum/x;

     printf("Average = %d",average);
     getchar();

     return 0;
}

2. An array of two dimensions

- A two-dimensional array is an array consisting of n rows and n columns. The form can be a matrix or table.


- array declaration:

array_name [row][column]

Example

#include <stdio.h>

int main() {
    int x[2][3] = {{1, 2, 3}, {4, 5, 6}}; // initialize data
    int i, j;
    
    for (i=0; i<2; i++) { // first for
        for (j=0; j<3; j++) { // second for
            printf("%d ", x[i][j]); // print array
        }
        printf("\n");
    }
    return 0;   
}

3. Multidimensional arrays

- Multidimensional arrays are arrays that have more than two sizes. The form of multidimensional array declaration is tantamount to declarations of one and two dimensional arrays.


- array declaration:

array_name [size1][size2]...[sizeN]


 
 
 

Recent Posts

See All
IF CONDITON

An if structure is formed from an if statement and is often used to select a single condition. If the selected process is fulfilled or...

 
 
 
Struct

Struct is a formed data type that contains a collection of variables that are in the same name and are related to one another. In...

 
 
 
POINTER

A pointer is a variable that points to another variable memory address. A pointer does not contain a data value as well as an ordinary...

 
 
 

Comments


  • instagram
  • generic-social-link

©2018 by systemnotfound. Proudly created with Wix.com

bottom of page