An array is a collection of similar types of data items, stored in a contiguous memory location and accessed using a common name.
An array is a convenient way of grouping a lot of homogeneous variables under a single variable name.
If a lot of homogeneous data needs to be stored, instead of using a lot of variable names they can be stored in an array. More specifically, if we don't know how much data we want to store, we can't declare that number of variables. Hence in this context, there is no alternative to an array.
If an array is declared, contiguous memory space is allocated for it with a specified size. The following program shows that the memory allocation is contiguous.
Source code to print addresses of an array elements
#include <stdio.h>
int main()
{
int numbers[10], i;
for(i = 0; i < 10; i++)
{
printf(" %u ", &numbers[i]);
}
return 0;
}
In the above example, we have taken an array of integer elements. An integer takes 4 bytes of memory space. If you look into the allocated address of the integer array, you will find the addresses are continuous as the memory locations of the elements are increasing by the size of the data type.
To store 100 integer values, an array is declared as below:
int arr[100];
or
#define MAX 100 ... int arr[MAX];
If an integer variable takes 4 Bytes of memory space, 400 Bytes of memory block will be allocated for this array arr. This memory block is contiguous, i.e. it is not being allocated from multiple locations.
In the following example, we will store some numbers (taken from the user as input) in an array, arr and we will calculate the sum as well as the average of those numbers. We will write the program in a file calculate_sum.c.
Program To Calculate Sum of Integer Values
#include <stdio.h>
int main()
{
int numbers[100], i, n, res = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements: ");
for(i = 0; i < n; i++)
{
scanf("%d", &numbers[i]);
}
for(i = 0; i < n; i++)
{
res = res + numbers[i];
}
printf("The sum of nuebers is : %d\n", res);
printf("The average of nuebers is : %f\n", ( (float) res / n));
return 0;
}
Now to compile the program, use cc or gcc command as shown below.
#cc calculate_sum.c
Now one file will be generated by name a.out. To run this program do the following and check the result.
#./a.out
A program to merge two sorted arrays.
#include <stdio.h>
int main()
{
int numbers[100], i, n, res = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements: ");
for(i = 0; i < n; i++)
{
scanf("%d", &numbers[i]);
}
for(i = 0; i < n; i++)
{
res = res + numbers[i];
}
printf("The sum of nuebers is : %d\n", res);
printf("The average of nuebers is : %f\n", ( (float) res / n));
return 0;
}
Different Operations On One Dimensional Array
#include <stdio.h>
#include <stdlib.h>
int search(int arr[], int size, int key);
int insert(int arr[], int *size, int key);
int delete(int arr[], int *size, int key);
void replace(int arr[], int size, int elm1, int elm2);
void reverse(int arr[], int size);
void show(int arr[], int size);
int main() {
int arr[20];
int i, size, key, elm1, elm2;
int c;
printf("Enter the number of elements of array :");
scanf("%d", &size);
printf("Enter the elements of array :");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
do {
printf("\n\t******* MENU *******");
printf("\n\t1 to search an element ");
printf("\n\t2 to insert an element ");
printf("\n\t3 to delete an element ");
printf("\n\t4 to replace an element ");
printf("\n\t5 to reverse the array ");
printf("\n\t6 to show the elements ");
printf("\n\tOther to exit ");
printf("\n\tEnter your choice :");
scanf("%d", &c);
switch (c) {
case 1:
printf("\nEnter the element to search :");
scanf("%d", &key);
search(arr, size, key);
break;
case 2:
printf("\nEnter the element to insert at end :");
scanf("%d", &key);
insert(arr, &size, key);
break;
case 3:
printf("\nEnter the element to delete :");
scanf("%d", &key);
delete(arr, &size, key);
break;
case 4:
printf("\nEnter the array element & the element to be replaced with :");
scanf("%d %d", &elm1, &elm2);
replace(arr, size, elm1, elm2);
break;
case 5:
printf("\nThe elements are : ");
reverse(arr, size);
show(arr, size);
break;
case 6:
printf("\nThe elements are : ");
show(arr, size);
break;
default:
exit(-1);
}
printf("\nDo you want to run the programme ?(y/n)");
scanf("%c", &c);
} while (c != 'n');
}
int search(int arr[], int size, int key) {
int i, flag;
flag = 0;
for(i = 0; i < size; i++)
{
if(key == arr[i])
{
flag = 1;
break;
}
}
if (flag == 1)
printf("The element is present");
else
printf("The element is not present");
return (0);
}
int insert(int arr[], int *size, int key) {
arr[*size] = key;
(*size)++;
return (0);
}
int delete(int arr[], int *size, int key) {
int j, i;
int loc, found = 0;
for (i = 0; i < *size; i++) {
if (arr[i] == key) {
found = 1;
loc = i;
}
}
if (found) {
for (i = loc; i < *size; i++)
arr[i] = arr[i + 1];
(*size)--;
arr[*size] = 0;
}
for (j = 0; j < *size; j++)
printf("\t%d", arr[j]);
return (0);
}
void replace(int arr[], int size, int elm1, int elm2) {
int i;
for (i = 0; i < size; i++)
if (arr[i] == elm1)
arr[i] = elm2;
for (i = 0; i < size; i++)
printf("\t%d", arr[i]);
}
void reverse(int arr[], int size) {
int j, temp;
for (j = 0; j < size /2; j++)
{
temp = arr[j];
arr[j] = arr[size - 1 - j];
arr[size - 1 - j] = temp;
}
}
void show(int arr[], int size) {
int i;
for (i = 0; i < size; i++)
printf("\t%d", arr[i]);
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.