Learn C programming
ARRAYS
In this lesson we are going to discuss the following topics:
- What is an ARRAY
- Defining an ARRAY
- Initializing ARRAYS
- Processing an ARRAY
Arrays in 'C' |
What is an ARRAY
- An array is a group of data having common characteristics that share a common name, and that are different from one another by their positions within the array. The individual data items can be characters, integers, floating-point numbers, etc. all having the same type and storage class.
- An array is an aggregate of similar elements, having a common group name. Each element of the array has the same data type and are stored in successive locations. The data elements are known as the members of the array.
- An array is an identifier that refers to a collection of data items which all have the same name, are of the same type. The individual data items are represented by their corresponding array elements, which are distinguished from one another by the value that is assigned to a subscript.
- Each array element is referred to by specifying the array name followed be one or more subscripts, with each subscript(nonnegative integer) enclosed in square brackets.
- The dimension of the array is determined by the number of subscripts. For example text[i] is one-dimensional array, text[i][i] is a two-dimensional array.
- The members of an array identified by means of an index (subscript). An index holds integer values starting at 0 (zero). Thus an array with N elements, the supscript will be an integer quantity whose values range from 0 to n-1. The first element has an index of 0 and a null character (\0) is automatically placed at the end of the array.
Example:
- if the string "computer" is to be stored in a one-dimensional character array called text. As "computer" contains 8 characters, text will be an 9-element array.
Element Number
Subscript Value
Array Element
Corresponding Data Item
1
0
Text[0]
c
2
1
Text[1]
o
3
2
Text[2]
m
4
3
Text[3]
p
5
4
Text[4]
u
6
5
Text[5]
t
7
6
Text[6]
e
8
7
Text[7]
r
9
8
Text[8]
\n
Defining An Array
Element Number |
Subscript Value |
Array Element |
Corresponding Data Item |
1 |
0 |
Text[0] |
c |
2 |
1 |
Text[1] |
o |
3 |
2 |
Text[2] |
m |
4 |
3 |
Text[3] |
p |
5 |
4 |
Text[4] |
u |
6 |
5 |
Text[5] |
t |
7 |
6 |
Text[6] |
e |
8 |
7 |
Text[7] |
r |
9 |
8 |
Text[8] |
\n |
Array are defined the same way as ordinary variables, except that each array name must include its size in number of elements. A one-dimension array definition may be expressed as
storage-class data-type array[expression];
- storage-class refers to the storage class of the array. (optional)
- data-type is the data type
- array is the array name
- expression is a positive-valued integer expression that indicates the number of array elements.
Example:
int number[50];
char name[25]
static float temperature[12];
static char text[10];
Program-1
Read in a line of lower-case text and write it out in upper-case
#include <stdio.h>
#define MAX_SIZE 25
void main()
/* Read in a line of lower-case text and write it out in upper-case */
{ char line[MAX_SIZE ];
int counter;
printf("\nEnterText Till Here :\n");
for(counter=0; counter< MAX_SIZE; ++counter)
line[counter]=getchar();
printf("\n The output is : ");
for(counter=0; counter< MAX_SIZE; ++counter)
putchar(toupper(line[counter])); }
Initializing ARRAYS
Automatic arrays cannot be initialized. External and arrays can be initialized by following the declaration with a list of initializers enclosed in braces and separated by commas. The format is :
storage-class data-type array[expression] = {value1, value2,....);
value1 refers to the value of the first array element
value1 refers to the value of the second array element, and so on.
value1 refers to the value of the second array element, and so on.
expression is optional, if initial values are present.
Examples:
int number[5] = {1,2,3,4,5};
static float temperature[4] = {0, 0.46, -0.22, 0, 4.5};
static float temperature[4] = {0, 0.46, -0.22, 0, 4.5};
char text[8] = {c, o, m, p, u, t, e, r};
Array elements that are not assigned initial values will automatically be set to zero.
Example:
int number[8] = {6, 4, 7, 2};
Array size need not be specified when initial values are included as a part of an array definition. In a numerical array, the array size will automatically be set equal to the number of initial values included within the definition.
Example:
int number[ ] = {1, 2, 3, 4}
When a string is assigned to an external or a static character array as a part of the array definition, the array size is omitted. The proper array size will be assigned automatically. This will include a provision for the null character \0, when is automatically added at the end of every string.
Examples:
char os[4] = "UNIX";
char os[ ] = "UNIX";
char os[ ] = "UNIX";
os[0] ='U' os[0] ='U'
os[1] ='N' os[1] ='N'
os[2] ='I' os[2] ='I'
os[3] ='X' os[3] ='X'
os[4] ='\0'
Program-2
Calculate total of all positive numbers in a given array
#include <stdio.h>
/* Calculate total of all positive numbers in a given array */
void main()
{ static int number[ ] = {10, 10, -10, 10, -10, 10, 10, -10, 10, 10};
int i, total=0, size=10;
for(i=0; i<size; i++)
{ if(number[i] < 0)
continue;
total=total+number[i]; }
printf("Total of all positive numbers = %d\n",total); }
Program-3
Find the Sizeof of an Array
#include <stdio.h>
/* Find the sizeof of an Array */
main()
{ char a 1[ ] = "Computer";
cirscr();
printf("Number of charaters = %d\n", sizeof(a1));
}
Program-4
Initializing Static and Extern Arrays
#include <stdio.h>
/* initializing Static and Extern Arrays */
void main()
{ static int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31,30,31};
int i;
extern char month[12][10];
clrscr();
printf("\t\t\tMonth Name\t\tNo. Of Days\n");
printf("\t\t\t --------- \t\t --------\n");
for(i=0; i<=11; i++)
printf("\t\t\t%-10s\t\t %\n", month[i], days[i]);
}
Processing an Array
Single operation processing entire array is not possible in array. Thus, if num1 and num2 are similar arrays - assignment operations, comparison operations, and so on must be carried out on an element-by-element basis. This is done within a loop, where each pass through loop is used to process one array element. The number of passes through the loop will therefore equal the number of array elements to be processed.
Program-5
Program to accept data in an array, and display them back
#include <stdio.h>
/* Program to accept data in an array, and display them back */
void main()
{ int array[5], i;
printf("Enter 5 Integers \n");
for(i=0; i<=5; i++)
printf("Integer [%d] : "' i+1);
scanf("%d"' &array[i]);
} printf("\n You Entered The Following Elements In This Array\n");
for(i=0; i<=5; i++)
printf("Integer [%d] = %d\n"' i+1, array[i]);
}
If you find this article informative, please share the post to your friends.
*************
Exercise regularly. Eat a nutritious diet. Don't smoke - WHO (Covid-19).
- Learn how to write the C program step-by-step | Lesson -1
- Learn how to write the C program step-by-step | Lesson -2
- Learn how to write the C program step-by-step | Lesson -3
- Learn how to write the C program step-by-step | Lesson -4
- Learn how to write the C program step-by-step | Lesson -5
- Learn how to write the C program step-by-step | Lesson -6
- Learn how to write the C program step-by-step | Lesson -7
Let's see also...
Thanks a lot for reading.
Neel Kamal
Learn how to write the C program step-by-step | Lesson - 8
Reviewed by Neel Kamal
on
June 26, 2021
Rating:
No comments:
For More Details Subscribe & Comment..!