|
|
|
Array programs
This section contains example programs demonstrating the use of arrays.
A simple program to demonstrate the definition and use of an array. #include
main() { int i[10],j;
for ( j=0 ; j<10 ; j++) i[j] = j ;
for ( j=0 ; j<10 ; j++) printf("%d\n" , i[j]) ; }
A program to demonstrate the use of an array. #include #include
main() { int i , min_value , max_value ; int list[10] ;
for ( i=0 ; i<10 ; i++) list[i] = rand() ;
/* Find the minimum value */
min_value = 32767 ;
for ( i=0 ; i<10 ; i++) if (list[i]min_value=list[i] ; printf("Minimum value generated is %d\n" , min_value) ;
/* Find the maximum value */
max_value = 0 ;
for ( i=0 ; i<10 ; i++) if (list[i]>max_value) max_value=list[i]; printf("Maximum value generated is %d\n" , max_value) ;
}
This program uses a bubble sort. #include include
main() { int item[100]; int a,b,t; int count; /* read in numbers */ printf("How many numbers ?"); scanf("%d", &count); for (a=0;a /* now sort using bubble sort */ for (a=1; afor (b=count-1; b>=a; --b) { /* compare adjacent elements */ if (item[b-1] > item[b]) { /* exchange elements */ t= item[b-1]; item[b-1] - item[b]; item[b] = t; } } /* display sorted list */ for (t=0; t}
An example program using a two-dimensional array now follows. #include
main() { int twod[4][5]; int i,j;
for(i=0; i<4; i++) for(j=0; j<5; j++) twod[i][j] = i*j;
for (i=0; i<4; i++) { for (j=0; j<5; j++) printf("%d !, twod[i][j]); printf("\n"); } }
The program output looks like this: 00000 01234 02468 036912
|
| Author: Tejwant Singh Randhawa 15 Jun 2008 | Member Level: Silver Points : 2 |
nice program to demonstrate function array in c. array is an continuous memory allocation of same data type. if declare a variable like int a[10] then array variable a is created with 10 elements with subscript from 0 to 9 .
|