Arrays

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Arrays as PDF for free.

More details

  • Words: 555
  • Pages: 4
ARRAYS Little Boxes on the hillside Arrays are a data structure which hold multiple variables of the same data type. Consider the case where a programmer needs to keep track of a number of people within an organisation. So far, our initial attempt will be to create a specific variable for each user. This might look like, int name1 = 101; int name2 = 232; int name3 = 231;

It becomes increasingly more difficult to keep track of this as the number of variables increase. Arrays offer a solution to this problem. An array is a multi-element box, a bit like a filing cabinet, and uses an indexing system to find each variable stored within it. In C, indexing starts at zero. Arrays, like other variables in C, must be declared before they can be used. The replacement of the above example using arrays looks like, int names[4]; names[0] = 101; names[1] = 232; names[2] = 231; names[3] = 0;

We created an array called names, which has space for four integer variables. You may also see that we stored 0 in the last space of the array. This is a common technique used by C programmers to signify the end of an array. Arrays have the following syntax, using square brackets to access each indexed value (called an element). x[i]

so that x[5] refers to the sixth element in an array called x. In C, array elements start with 0. Assigning values to array elements is done by, x[10] = g;

and assigning array elements to a variable is done by,

g = x[10];

In the following example, a character based array named word is declared, and each element is assigned a character. The last element is filled with a zero value, to signify the end of the character string (in C, there is no string type, so character based arrays are used to hold strings). A printf statement is then used to print out all elements of the array. /* Introducing array's, 2 #include <stdio.h> main() {

*/

char word[20]; word[0] = 'H'; word[1] = 'e'; word[2] = 'l'; word[3] = 'l'; word[4] = 'o'; word[5] = 0; printf("The contents of word[] is -->%s\n", word );

} Sample Program Output The contents of word[] is Hello

#include <stdio.h> main() { int numbers[100]; float averages[20]; numbers[2] = 10; --numbers[2]; printf("The 3rd element of array numbers is %d\n", numbers[2]); } Sample Program Output The 3rd element of array numbers is 9

ASSIGNING INITIAL VALUES TO ARRAYS The declaration is preceded by the word static. The initial values are enclosed in braces, eg,

#include <stdio.h> main() { int x; static int values[] = { 1,2,3,4,5,6,7,8,9 }; static char word[] = { 'H','e','l','l','o' }; for( x = 0; x < 9; ++x ) printf("Values [%d] is %d\n", x, values[x]); }

Sample Program Output Values[0] is 1 Values[1] is 2 ....

Values[8] is 9

Two Dimensional Array ============= Declaration ======== type array_name[m][n] ex int twodim[3][4]; It is a two dimenstional array with 3 rows and two columns.

Col1 Row1 Row2 Row3

[0][0] [1][0] [2][0]

col2

col3

col4

[0][1] [1][1] [2][1]

[0][2] [1][2] [2][2]

[0][3] [1][3] [2][3]

Elements in a two dimenstional array can be accessed by menas of row and colum. Array declaration ============

int values[3][4]={1,2,3,4,5,6,7,8,9,10,11,12} values[0][0]=1 values[1][0]=5 values[2][0]=9

values[0][1]=2 values[0][2]=3 values[1][1]=6 values[1][2]=7 values[2][1]=10 values[2][2]=11

values[0][3]=4 values[1][3]=8 values[2][3]=12

Related Documents

Arrays
November 2019 37
Arrays
May 2020 23
Arrays
November 2019 37
Arrays
November 2019 39
Arrays
November 2019 46