One Dimensional Array in C Programming Examples
Following C program explains One dimensional Array with examples. and ready to execute code with clean output, in easy way with simple steps.
An array which has only one subscript is known as one dimensional Array i.e) int arr[10]. To Lean more on this C Arrays with Examples.
C Arrays are most useful when they have a large number of elements: that is, in cases where it would be completely impractical to have a different name for every storage space in the memory.
The following example uses array x to read the values and compute the sum of their squares. This program using a single-sub-scripted variable to evaluate the following expressions:
Example : One dimensional Array
#include <stdio.h> main() { int i ; float x[10], value, total ; /* . . . . . .READING VALUES INTO ARRAY . . . . . . */ printf("ENTER 10 REAL NUMBERS\n") ; for( i = 0 ; i < 10 ; i++ ) { scanf("%f", &value) ; x[i] = value ; } /* . . . . . . .COMPUTATION OF TOTAL . . . . . . .*/ total = 0.0 ; for( i = 0 ; i < 10 ; i++ ) total = total + x[i] * x[i] ; /*. . . . PRINTING OF x[i] VALUES AND TOTAL . . . */ printf("\n"); for( i = 0 ; i < 10 ; i++ ) printf("x[%2d] = %5.2f\n", i+1, x[i]) ; printf("\ntotal = %.2f\n", total) ; }
Output Example,
ENTER 10 REAL NUMBERS 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.10 x[ 1] = 1.10 x[ 2] = 2.20 x[ 3] = 3.30 x[ 4] = 4.40 x[ 5] = 5.50 x[ 6] = 6.60 x[ 7] = 7.70 x[ 8] = 8.80 x[ 9] = 9.90 x[10] = 10.10 Total = 446.86
Let me know if you find any difficulty in understanding this C Program example and I would be glad to explain it further.