Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Sajib Datta Feb 11, 2014.  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will.

Similar presentations


Presentation on theme: "Dr. Sajib Datta Feb 11, 2014.  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will."— Presentation transcript:

1 Dr. Sajib Datta CSE@UTA Feb 11, 2014

2  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will hold 3 doubles variables*/  /* later we can provide the specific array values. notice how the array index begins at 0 */  someData[0] = 3.5;  someData[1] = 4.0;  someData[2] = 9.34;

3  We may also declare and initialize the array at the same time, so we don’t need to include the number of array elements in the square brackets.  double myData[] = {3.5, 4.0, 9.34};  Never access a variable in an array beyond the index bound!!!!

4  If we initialize only some of the array values when the array is declared, the initialized values will be at the beginning of the array.  The remaining values will be initialized to zero for an array of int, “space” for an array of char.

5  #include  int main(void)  {  int j, intarr[3] = {2,3};  char charr[3] = {'a', 'b'};  for (j = 0; j < 3; j++)  {  printf(" %d ", intarr[j]);  }  printf("\n");  for (j = 0; j < 3; j++)  {  printf(" %c ", charr[j]);  }  return 0;  }

6  int n=19;  int iarr[n];  or  #define ARRAY_SIZE 19 // have it after #include  int iarr[ARRAY_SIZE];

7  Given  int array1D[5] = { 32,44,33,12,65};  Output:  The min value is 12.  The max value is 65.

8  #include  int main(void)  {  int i, min, max, array1D[5] = {32,44,33,12,65};  min = max = array1D[0];  for (i = 1; i < 5; i++)  {  if (array1D[i] > max)  max = array1D[i];  if (array1D[i] < min)  min = array1D[i];  }  printf("The min value is %d.\n", min);  printf("The max value is %d.\n", max);  return 0;  }

9  Original sentence: I am at UTA  The reverse sentence: ATU ta ma I

10  #include  int main(void)  {  int i;  char arraych[11] = {'I', ' ', 'a', 'm', ' ', 'a', 't', ' ', 'U', 'T', 'A'};  printf("Original sentence: ");  for (i = 0; i <= 10; i++)  printf("%c", arraych[i]);  printf("\n");  printf("The reverse sentence: ");  for (i = 10; i >=0; i--)  printf("%c", arraych[i]);  printf("\n");  return 0;  }

11  We can also have arrays of arrays, also known as multidimensional arrays.  E.g., A two-dimensional array is an array of several one-dimensional arrays.  The basic form for declaring a 2D array is ◦ type array_name[rows][columns];  The mathematical equivalent of a two- dimensional array is a matrix.

12  A 2D array can be viewed like a table.  To create a 2D array for this table of values, we could use ◦ int some_data[2][3] = { {10, 5, 13}, {2, 7, 19} };  Or ◦ int some_data[][3] = { {10, 5, 13}, {2, 7, 19} }; 10513 2719

13  As with one-dimensional arrays, 2D arrays indices begin at zero. The difference is now we have to keep track of indices in two dimensions. So, if we create the following array: ◦ int Sales[2][3] = { {1, 2, 3}, {4, 5, 6} };  The individual elements are referenced with the following combinations of array name and indices:

14  Print out a specific row or column in an array  #include  int main(void)  {  int row, column, Sales[2][3] = { {1, 2, 3}, {4, 5, 6} };  printf("The values on row 0 are : \n");  for (column = 0; column < 3; column++)  printf("%d ", Sales[0][column] );  printf("\n");  printf("The values on column 0 are: \n");  for (row = 0; row < 2; row++) ◦ printf("%d\n", Sales[row][0]);  return 0;  }

15  In case of normal variables: to assign value of ‘a’ to ‘b’; ◦ int a,b; ◦ a=10; ◦ b = a;  Assume we want to copy the values of array ‘a’ to array ‘b’ ◦ int a[10], b[10]; ◦ Initialize a; ◦ b = a;//wrong

16  Have to iterate over ‘a’ and assign individual array elements into ‘b’  for(i=0;i<10;i++) ◦ b[i] = a[i];

17  Given an array on integers and another integer ‘x’, find if ‘x’ appears in the array. Print ‘Not found’, if ‘x’ does not appear, otherwise print ‘Found’.  Strategy:  Simply iterate over the array if look for a match

18  int array[100];  int i,x, found=0;  ….  for(i=0; i<100;i++)  { ◦ if( array[i] == x) ◦ {  found =1;  printf(“Found %d”,x);  break; ◦ }  }  if(!found)  { ◦ printf(“%d not found”, x);  }

19  The worst case – the loop is executed n times  On the average – the loop is executed n/2 times  What if the array has a million integers (about 2 20 )

20  Can make it faster.  However, unlike linear search, binary search has a precondition ◦ The sequence must be sorted

21  Strategy: ◦ Compare ‘x’ with the middle element.  If equal, we found it  Else If ‘x’ < middle element, we can discard the half of the elements; all elements that are greater than the middle element and middle element itself  Now search in the remaining half of the array  Similarly, if ‘x’ > middle element, we can discard the half of the elements; all elements that are smaller than the middle element and middle element itself

22  Array ◦ http://www.java2s.com/Tutorial/C/0140__Array/Catalog 0140__Array.htm  Sorting and searching ◦ http://www.java2s.com/Tutorial/C/0280__Search- Sort/Catalog0280__Search-Sort.htm  Time complexity ◦ http://community.topcoder.com/tc?module=Static&d1= tutorials&d2=complexity1 ◦ http://www.cs.toronto.edu/~vassos/teaching/c73/hand outs/brief-complexity.pdf ◦ http://www.csd.uwo.ca/courses/CS1037a/notes/topic13 _AnalysisOfAlgs.pdf (advanced!)


Download ppt "Dr. Sajib Datta Feb 11, 2014.  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will."

Similar presentations


Ads by Google