1 / 14

ARRAYS in C

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];

marrs
Download Presentation

ARRAYS in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ARRAYS in C

  2. Index • Introducing Arrays. • Declaration of a Array Variables, Creating Arrays. • The Length of Arrays. • Initializing Arrays. • Multidimensional Arrays.

  3. 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.

  4. Declaring Array Variables data type array-name[index]; Example: int list[10]; char num[15]; float hat[20];

  5. 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.

  6. 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.

  7. 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;

  8. Multidimensional Arrays intmatrix[10] [10]; for (i=0; i<10; i++) { for (j=0; j<10; j++) { matrix[i] [j] = i * j; } }

  9. Two-dimensional array with three rows and four columns. Multidimensional Arrays

  10. 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

  11. 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;

  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]); }

  13. /* 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 */

  14. Question ???

More Related