This is a Clilstore unit. You can .

# Array. One-dimensional array

Array is a structure of the homogeneous elements, holding a continuous memory area. The format of the array description in the language C ++:

type_of elements name_of the array [number of elements];

For example: int a;

float X, Y;

long int Z;

Contrary to Pascal, in C ++ it is impossible to determine the arbitrary ranges for indices. The lowest index value is 0, therefore the array size specified in the description is always greater by one than the maximum index value.

For example: int a describes the array from elements:

a, a, a,… a;

The array size can clearly not be specified, if upon its declaration the initialization of the element values is performed.

For example: int Х[]={3, 8, 9, 1,-5};

e.g. X=3, X=8,...X=-5

One-dimensional and two-dimensional arrays are often used in programs.

Consider work with one-dimensional arrays.

To enter elements of the one-dimensional array int a into the computer's memory, the following entry shall be used:

for (i=0; i<10; i++)

{cout<<”a[“<<i<<”]=”; cin>>a[i];} // или cin>> a[i];

To display the one-dimensional array on the screen, the following entry shall be used:

for (i=0; i<10; i++)

cout<<”\n a[“<<i<<”]=”<<a[i]; // или cout<< a[i];

Example 31. Find the arithmetic average of the positive elements of the array from 10 integers.

main()

{int a;

int S=0, i, k=0; float P;

for (i=0; i<10; i++) //enter of array elements

cin>>a[i];

for (i=0; i<10; i++)

if (a[i]>0) {S+=a[i]; k++; }

P=S/k;

cout<<"P="<<P;

}

Example 32.In the array of real numbers find the maximum and minimum elements,as well as their indexes.

#include <iostream.h>

#include <conio.h>

#include <math.h>

main()

{float a[]={4.6, 7.8, 3.5,12.6, 0.1} ;

int i, k, t; float min, max;

max=a; k=0; min= a; t= 0;

for (i=1; i<5; i++)

{if (max<a[i]) {max= a[i]; k=i;}

if (min>a[i]) {min= a[i]; t=i;}

}

cout<<"max="<<max<<" k="<<k;

cout<<"\n min="<<min<<" t="<<t;

}

Short url:   http://multidict.net/cs/6250