6.13k likes | 13k Views
ARRAYS in C. Index. Introducing Arrays. Declaration of a Array Variables, Creating Arrays. The Length of Arrays. Initializing Arrays. Multidimensional Arrays. Introducing Arrays. Array is a data structure that represents a collection of the same types of data. int num[10];
E N D
Index • Introducing Arrays. • Declaration of a Array Variables, Creating Arrays. • The Length of Arrays. • Initializing Arrays. • Multidimensional Arrays.
Introducing Arrays • Array is a data structure that represents a collection of the same types of data. int num[10]; num reference An Array of 10 Elements of type int.
Declaring Array Variables data type array-name[index]; Example: int list[10]; char num[15]; float hat[20];
The Length of Arrays • Once an array is created, its size is fixed. It cannot be changed. For Example, int arr[10]; You can not insert any number to arr[11] location because it is not initialized.
Declaration of One-dimensional arrays data type array-name[size]; or type variable-name[size]; Example: int num[10]; num[0] references the first element in the array. num[9] references the last element in the array.
Initialization of One-dimensional Arrays Declaring, creating, initializing in one step: int num[6] = {2, 4, 6, 7, 8, 12}; Individual elements can also be initialize as: num[0] = 2; num[1] = 4; num[2] = 6; num[3] = 7; num[4] = 8; num[5] = 12;
Multidimensional Arrays intmatrix[10] [10]; for (i=0; i<10; i++) { for (j=0; j<10; j++) { matrix[i] [j] = i * j; } }
Two-dimensional array with three rows and four columns. Multidimensional Arrays
Multidimensional Array Illustration int[][] array = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; int matrix[5] [5]; matrix[0] [2] = 3 matrix[2] [1] = 10
Initializing of Multidimensional Arrays To declare, create and initialize a multidimensional array. For example, int[][] array = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, }; This is equivalent to the following statements: array[0][0] = 1; array[0][1] = 2; array[0][2] = 3; array[0][3] = 4; array[1][0] = 5; array[1][1] = 6; array[1][2] = 7; array[1][3] = 8; array[2][0] = 9; array[2][1] = 10; array[2][2] = 11; array[2][3] = 12;
#include <stdio.h> #include <conio.h> #include <math.h> #define MAXSIZE 10 main() { /* Start of main() function */ float x[MAXSIZE]; int i, n; /* declares the variables mean , var, SD and sum as float */ float mean, var, SD, sum=0, sum1=0; clrscr(); printf("Enter the value of N\n"); /* accepts values from user */ scanf("%d", &n); printf("Enter %d real numbers\n",n); for(i=0; i<n; i++) { scanf("%f", &x[i]); }
/* Compute the sum of all elements */ for(i = 0; i < n; i ++) { sum = sum + x[i]; /* calculating the mean using the equation */ } mean = sum /(float) n; /* Compute variance and standard deviation */ for(i=0; i<n; i++) { /* calculate the variance using general equation */ sum1 = sum1 + pow((x[i] - mean), 2); } var = sum1 / (float) n; SD = sqrt(var); /* prints of the output are start here*/ printf("Average of all elements = %.2f\n", mean); printf("Varience of all elements = %.2f\n", var); printf("Standard deviation = %.2f\n", SD); } /* End of main() function */