Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.

Similar presentations


Presentation on theme: "Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values."— Presentation transcript:

1 Arrays in C

2 What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values. Array is a set of ordered data items.

3 What is the need for an array? If there are situations in which we would want to store more than one value at a time in a single variable. For example, suppose we wish to arrange the grades obtained by 100 students in ascending order. In such a case we have two options to store these grades in memory: 1. Construct 100 variables to store grades obtained by 100 different students, i.e. each variable containing one student’s grade. 2. Construct one variable (called array) capable of storing or holding all the hundred values.

4 Declaring Array Declare an array called grades, contain 5 elements of type integer. int grades[ 5 ] ; The elements of an array can be of any type such as: int, float, double, char …etc. The number of elements must be of integer value. X [100] Y [ 50.7] Z [ -9 ] M [ 0 ]

5 Declaring Array In an array of n elements, the first one is stored in position [0], the second one in position [1], and the last one in[n−1]. For example, the statement Valid indexes: values[0]=5; values[9]=7; Invalid indexes: values[10]=3; values[-1]=6; In memory: elements of an array are stored at consecutive locations int values[ 10 ] ;

6 Example C does not check if the index goes out of the array bounds. It is the programmer’s responsibility to assure that this won’t happen. If it does, the program may behave unpredictably. E.g. arr[5] = 601 ; int main() { int i, arr[3]; arr[0] = 100; arr[1] = 101; arr[2] = 102; for (i = 0; i < 3; i++) printf("%i\n",arr[i]); return 0; } C:\Windows\System32\cmd.exe 100 101 102 Press any key to continue...

7 Constants After declaring an array, you can’t change the number of its elements; it remains fixed. The array’s length is specified by an integer constant expression. #include #define SIZE 100 int main() { float arr[SIZE];. }

8 Array Initialization int arr[4] = {1,2,3,4}; int arr[4] = {1,2}; char letters[5] = { 'a', 'b', 'c', 'd', 'e' }; float sample_data[] = { 10.5, 30.6, 50.54}; int arr[] = {10, 20}; If the initialization list is shorter than the number of the elements, the remaining elements are set to 0 If the array’s length is omitted, the compiler will create an array with length equal to the number of the values in the list

9 Example Write a program that declares an array of 5 float-type values and read the element values from the user. Next, the program should display the sum of elements. #include #define SIZE 5 int main() { float arr[SIZE]; float sum = 0; int i; for (i = 0; i < SIZE; i++) { scanf("%f", &arr[i]); sum = sum + arr[i]; } printf("The sum = %f\n",sum); return 0; } C:\Windows\System32\cmd.exe 2 1.5 1 3 2.3 The sum = 9.800000 Press any key to continue...

10 The const Qualifier The compiler allows you to associate the const qualifier with variables whose values will not be changed by the program. if you try to assign a value to a const variable after initializing it, or try to increment or decrement it, the compiler might issue an error message. Example: const double pi = 3.141592654; If you wrote a line like this in your program: pi = pi / 2; the compiler would give you a warning message

11 Character Array int main() { char word[] = { 'H', 'e', 'l', 'l', 'o', '!' }; int i; for (i = 0; i < 6; ++i) printf("%c", word[i]); printf("\n"); return 0; } C:\Windows\System32\cmd.exe Hello! Press any key to continue...

12 Multidimensional Array

13 Declare Two-Dimensional Array int arr[3][4] data_type array_name[number_of_rows][number_of_columns]; C language allows arrays of any number of dimensions Two-dimensional array: matrix

14 Initializing Two-Dimensional Array int M[4][5]; // matrix, 4 rows, 5 columns M[4][5] = { { 10, 5, -3, 17, 82 }, { 9, 0, 0, 8, -7 }, { 32, 20, 1, 0, 14 }, { 0, 0, 8, 7, 6 } }; Alternatively, we can omit the inner braces and write: int M[4][5] = { 10, 5, -3, 17, 82, 9, 0, 0, 8, -7, 32,20, 1, 0, 14, 0, 0, 8, 7, 6 };

15 Example #define ROW 3 #define COL 4 int main(void) { int a[ROW][COL]; int i, j; /* read matrix elements */ for (i = 0; i < ROW; i++) for (j = 0; j< COL; j++) { printf("a[%d][%d] = ", i, j); scanf("%d", &a[i][j]); } /* print matrix elements */ for (i = 0; i < ROW; i++) { for (j = 0; j< COL; j++){ printf("%5d", a[i][j]); } printf("\n"); } return 0; } C:\Windows\System32\cmd.exe a[0][0] = 1 a[0][1] = 2 a[0][2] = 3 a[0][3] = 4 a[1][0] = 5 a[1][1] = 6 a[1][2] = 7 a[1][3] = 8 a[2][0] = 9 a[2][1] = 10 a[2][2] = 11 a[2][3] = 12 1 2 3 4 5 6 7 8 9 10 11 12 Press any key to continue...

16 Variable-Length Arrays The C language allows you to declare arrays of a variable size. For example int size = 100; float arr[size]; Variable-length array does NOT mean that you can modify the length of the array after you create it full support for variable-length arrays was not offered by all compilers


Download ppt "Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values."

Similar presentations


Ads by Google