Notes on Array and Strings | Grade 12 > Computer Science > Programming in C | KULLABS.COM

• Note
• Things to remember

### INTRODUCTION

It is true that many applications require the processing of multiple data items that have common features. Here, it should be noted that the data items are similar. The individual data items can be characters, integers, floating point numbers, etc. However, they all must be of the same type and the same storage class. Having these features an array can be formed.

Each array element or individual data item in the array is referred by specifying the array name followed by one or more subscripts. Therefore, it is also known as subscripted variables as in Algebra. Hence, an array is a sequence of data in memory, wherein all the data are of the same type and are placed in physically adjacent locations. For example, 10 integer items or elements are placed one after another in the memory.

It is true that strings characters are also placed in arrays. The strings are the array of characters. The array can be one dimensional, two dimensional or multidimensional.

#### Define an Array

An Array can be defined as the collection of data items of the same type and we can access the array using the common name.

Int age ;

This definition is one-dimensional array where,

int is data type of the array,

age is the name of the array and

 is the maximum number of elements in the array.

#### Accessing Array Elements

For accessing a particular element in an array, the array name is followed by square braces enclosing an integer which is called array index. The array index indicates the particular element of the array which is going to be accessed.

Note:

• The value of the symbolic constant will be substituted for the constant itself during the compilation process.
• Automatic variables can be initialized but automatic arrays cannot be initialized.
• The array size need not be specified explicitly when initial values are included as a part of an array definition.
• With a numerical array, the array size will automatically be set equal to the number of initial value included within the definition.

#### Initialization of One-Dimensional Array

One dimensional array is the elements that construct a row or a single column. If the elements of the same type are placed one after the other with similar characteristics, it forms a one-dimensional array.

int number  ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Here,

int is the data type of the array

the number is the name of the array

 indicates the maximum number of elements

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; are the elements explicitly declared.

Then,

number  = 1

number  = 2

number  = 3

etc.

If a program requires a one-dimensional array declaration, the declaration is written in the same manner as the array definition with the following exceptions.

1. The square brackets may be empty since the array size will be specified as a part of the array definition. Array declarations are customarily written in this form.
2. Initial values cannot be included in the declaration.

#### Accessing One Dimensional Array

While accessing one-dimensional array through the keyboard we use the convention;

&arrayname [expression];

The expression is the position at which the array can be located.

# Write a program to read an print static array.

 #include #include void main () { int a= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int i; float sum=0; for(i=0; i<10; i++) { sum +=a[i]; } printf(“\n The sum of the digits in the array is: %5.2f”,sum); getch(); }

In the above program, int a  explicitly declares 10 elements in the array. The array elements are enclosed in the curly braces. The elements are summed up and their result is obtained in floating point.

#### Array Initialization with Constant Value

Int a ={2, 4, 6, 8, 10};

 Array element a a a a a value 2 4 6 8 10 Address 4000 4001 4002 40003 40004

Example of an array initialization with the constant value.

 #include #include void main () { int j, sum=0; int num={ 2, 4, 6, 8, 10}; clrscr (); printf(“\n Data stored in the array variable are:”); for (j=0; j<5; j++) { printf(“\n num [%d] = %d, j, num[j]); sum +=num[j]; } printf(“\n Sum is M”,sum); }

# Write a program to read the static array elements and find their sum.

 #include #include void main () { int a[] ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int i; float sum=0; for (i=0; i<10; i++) {/*printf(“\n Enter a number:”); scanf(“%d”, &a[i]);*/ sum +=a[i]; } printf(“\n The sum of the digits in the array is :%5.2f”, sum); getch (); }

In the above program, the array definition a[] is the empty parameter which can hold as many elements as required. You can include more elements within the curly braces.

# Write a program to accept 10 elements in an array find their sum.

 #include #include void main () { int a; int i; float sum=0; for(i=0; i<10; i++) { printf(“\n Enter a number:”); scanf(“%d”, &a[i]); sum +=a[i]; printf(“\n The sum of the digits in the array is: %5.2f”,sum); getch(); }

In this program, the definition int a declares 10 integer type of elements that can be grouped together but they are not declared. They are accessed through the keyboard and placed in the array. Their sum is obtained and printed.

# Write a program to accept ages of 10 persons and print the average age.

 #include #include void main () { int age; int i, n; float sum=0; printf(“\n No of persons (less than 5): “); scanf(“%d”, &n); if (n<=0 && n>10) { printf(“\n Invalid entery.”); } else { for (i=0; i

# Write a program to sort the given list of array elements in ascending order.

 #include #include void main() { int i, j, n; float a, temp; printf(“\n Number of elements (less than 50).”): scanf(“%d”,&n); printf(“\n Enter the elements in next lines:”\n); for(i=0; i’ */ /*operator into ‘<’ in the if structure below */ if (a[i]>a[j] { temp =a[i] a[i]=a[j] a[j]=temp; } } printf(“\n Elements in ascending order are:\n”); for(i=0; i

# Write a program to initialize 10 positive numbers (elements) in the array and find the largest among them.

 #inlcude #include void main () { int j, g=0; int num ={12, 13, 56, 33, 2, 99, 35, 76, 87, 88}; clrscr (); for (j=0; j<10; j++) { if(num[j]>g { g=num[j]; } } printf(“\n The greatest number in the array list is: %d”, g); }

References:

Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. 216-223.

Adhikari, Deepak Kumar.,et.al., Computer Science XII,Asia Publication Pvt.Ltd

1. The individual data items can be characters, integers, floating point numbers, etc. However, they all must be of the same type and the same storage class. Having these features an array can be formed
2. The array index indicates the particular element of the array which is going to be accessed.
3. Array is also known as subscripted variables as in Algebra. Hence, an array is a sequence of data in memory, wherein all the data are of the same type and are placed in physically adjacent locations.
4. One dimensional array is the elements that construct a row or a single column. If the elements of the same type  are placed one after the other with similar characteristics, it forms the one-dimensional array.
5. For accessing a particular element in an array, the array name is followed by square braces enclosing an integer which is called array index.
.

0%