Subject: Computer Science
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.
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 [15];
This definition is one-dimensional array where,
int is data type of the array,
age is the name of the array and
[15] is the maximum number of elements in the array.
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:
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 [10] ={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
[10] indicates the maximum number of elements
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; are the elements explicitly declared.
Then,
number [1] = 1
number [2] = 2
number [3] = 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.
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 <stdio.h> #include <conio.h> void main () { int a[10]= {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 [10] 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.
Int a[5] ={2, 4, 6, 8, 10};
Array element | a[0] | a[1] | a[2] | a[3] | a[4] |
value | 2 | 4 | 6 | 8 | 10 |
Address | 4000 | 4001 | 4002 | 40003 | 40004 |
Example of an array initialization with the constant value.
#include <stdio.h> #include <conio.h> void main () { int j, sum=0; int num[5]={ 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 <stdio.h> #include <conio.h> 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 <stdio.h> #include <conio.h> void main () { int a[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 this program, the definition int a[10] 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 <stdio.h> #include <conio.h> void main () { int age[10]; 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<n; i++) { printf(“Age:”); scanf(“%d”, &age[i]); sum+=age[i]; } printf(“\n The input ages are:\n”); for (i=0; i<n; i++) { printf(“%d\n” age[i]); } printf(“\n The average is %5.2f”, sum\n); } } |
# Write a program to sort the given list of array elements in ascending order.
#include <stdio.h> #include <conio.h> void main() { int i, j, n; float a[50], 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<n; i++) { scanf(“%f”, &a[i]); } /* sorting array elements using selection sort */ for (i=0; i<n-1; i++) { for (j=i+1; j<n; j++) /* for sorting in descending order change ‘>’ */ /*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<n; i++) printf(“8.2f”,a[i]); } |
# Write a program to initialize 10 positive numbers (elements) in the array and find the largest among them.
#inlcude <stdio.h> #include <conio.h> void main () { int j, g=0; int num[10] ={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
© 2019-20 Kullabs. All Rights Reserved.