Programming C: Posts 9 – Arrays in C

Before learning about the array in C, we try to do for example the opening.

1. For example, the opening

Consider the example entry 5 integer, output to the screen 5 Among them on a line and total 5 which.

// e.g about array - code by nguyenvanquan7826
#include <stdio.h>

int main() 
{
    int a, b, c, d, e; // 5 bien

    printf("Nhap a = ");
    scanf("%d", &a);

    printf("Nhap b = ");
    scanf("%d", &b);

    printf("Nhap c = ");
    scanf("%d", &c);

    printf("Nhap d = ");
    scanf("%d", &d);

    printf("Nhap e = ");
    scanf("%d", &e);

    int tong  = a + b + c + d + e;
    printf("Tong cua %d + %d + %d + %d + %d = %d \n", a, b, c, d, e, tong);

    return 0;
}

Result:

Nhap a = 3
Nhap b = 5
Nhap c = 3
Nhap d = 6
Nhap e = 1
Tong crab 3 + 5 + 3 + 6 + 1 = 18

As examples we see entering all 5 and we made some screen seems very hard and we had to use too many variables, if the number of variables that we are not 5 that is 50, 500,... Then this result is very difficult. To remedy this we use the array.

A set of array elements with the same type of data. Array of arrays 1 evening, 2 evening,... And each type of data, there 1 corresponding array (integer array, real array, character array (Chain)),... I mainly consider the array 1 dimensional array 2 evening.

Now we will solve the examples on using arrays 1 evening.

2. Arrays 1 evening

2.1 Example overview

Have you tried to read and run this program, then please see details below.

// e.g about array - code by nguyenvanquan7826
#include <stdio.h>

int main() 
{
    // khai bao mang a co n phan tu
    int n = 5, i, s = 0;
    int a[n];

    // thuc hien nhap tung phan tu mang
    for (i = 0; i < n; i++) 
    {
        printf("Nhap a[%d] = ", i);
        scanf("%d", &a[i]);
    }

    // thuc hien in cac phan tu cua mang ra man hinh
    printf("\nMang da nhap \n");
    for (i = 0; i < n; i++) 
    {
        printf ("%d \t", a[i]);
    }

    // tinh tong cac so trong mang
    for (i = 0; i < n; i++) 
    {
        s += a[i]; // s = s + a[i]
    }
    printf("\nTong cac so trong mang: %d\n", s);

    return 0;
}

Import a[0] = 3
Import a[1] = 4
Import a[2] = 6
Import a[3] = 2
Import a[4] = 7
Bring entered
3 4 6 2 7
Sum of the carrying: 22

2.2 How to declare array 1 evening

KieuDuLieu TenMang [SoPhanTu];

CEO: int a[10];

Arrays 1 afternoon included a 10 elements of type integer. After declaring have 1 array looks like this:

Arrays 1 evening

Declare array with a 10 element

2.3 How to access elements in the array

After the array is declared, each element in the array are for reference index. The index starts from 0 to n-1 (where n is the size of the array). In the above example, I declare array 10 element, the index starting 0 to 9.
And we accessed via syntax: TenMang[ChiSo]

Arrays 1 evening

Retrieve array in C

2.4 How to import and export the elements of the array

To enter data for the elements in the array we need to browse to each element in the array and proceed to enter a loop

for (i = 0; i < n; i++) 
{
    printf("Nhap a[%d] = ", i);
    scanf("%d", &a[i]);
}

The publication of the elements in the array are also conducted similar:

for (i = 0; i < n; i++) 
{
    printf ("%d \t", a[i]);
}

2.5 Enter output array using function

In many problem, we have to enter multiple arrays, and output multiple times, Meanwhile let's put on a function for easy entry and use.

// e.g about array - code by nguyenvanquan7826
#include <stdio.h>

void nhapMang(int a[], int n) 
{
    int i;
    // thuc hien nhap tung phan tu mang
    for (i = 0; i < n; i++) {
        printf("Nhap a[%d] = ", i);
        scanf("%d", &a[i]);
    }
}

void xuatMang(int a[], int n) 
{
    int i;
    for (i = 0; i < n; i++) {
        printf ("%d \t", a[i]);
    }
}

int main() 
{
    // khai bao mang a co n phan tu
    int n = 5;
    int a[n];
    nhapMang(a, n);
    xuatMang(a, n);

    return 0;
}

Noted: An array of relationships with 1 pointer, array so as 1 pointer to the array passed to the function, the elements of the array will be affected if there are any changes in the array.

2.6 A few other notes

Initialize the array: We can initialize the array in the declaration immediately. CEO: float a [5] = {3.4, 5, 6, 7, 4,2}
With this initialization if we initialize exceed 5 element will report an error, without any element is the element that is the value 0.
CEO: float a [5] = {3.4, 5, 7} => a[4] = 0.
CEO: float a [5] = {3.4, 5, 7, 1, 2, 3} => Machine error.
Also we have to declare and initialize a previously unknown number of array.
CEO: int a[] = {3,6,2,5} => Array with 4 receiving element corresponding values.
Or: int a[]; => Often used when using arrays as parameters in function form.

3. Arrays 2 evening

We can consider it as a matrix. The nature, declaring, Import-Export,... Similar array 1 evening.

KieuDuLieu TenMang [SoHang][SoCot];

VD report: int a[5][10]; => Array a composed 5 goods and 10 post (each row 10 element).

Arrays 1 evening

Arrays 2 way in C

// e.g about array - code by nguyenvanquan7826
#include <stdio.h>
#define MAX 10

// nhap mang n hang, m cot
void nhapMang(int a[MAX][MAX], int n, int m) 
{
    int i, j;
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
            printf("Nhap a[%d][%d] = ", i, j);
            scanf("%d", &a[i][j]);
        }
    }
}

// xuat mang n hang, m cot
void xuatMang(int a[MAX][MAX], int n, int m) 
{
    int i, j;
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
            printf ("%-3d", a[i][j]);
        }
        printf("\n"); // xuong dong khi het 1 dong
    }
}

int main() 
{
    // khai bao mang a co n phan tu
    int n = 2, m = 3;
    int a[MAX][MAX];
    nhapMang(a, n, m);
    xuatMang(a, n, m);

    return 0;
}

Attention: The essence of memory, the elements of the array 2 evening (and multidimensional arrays) be held as many arrays 1 dimensional consecutive, so we may provide arrays 2 dimensional plaque 1 evening.

chap9-luu-tru-bear-2-pm-in-c.png

Arrays 1 evening

Organization, storage array 2 way in C

Exercise

  1. Enter into an array, find the biggest and the smallest number in the array entered.
  2. Enter a series of numbers, print out the location of the largest (can have multiple locations).
  3. Enter a sequence of precipitation is consecutive month in year. The notice of the May rainfall than the average rainfall of the month.
  4. Enter a series of numbers, sorted and printed in ascending sequence.
  5. Entered into a matrix (Plate 2 evening). Print out the sum of each row of the matrix.