Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Sajib Datta  We can also have arrays of arrays, also known as multidimensional arrays.  E.g., A two-dimensional array is an array of several.

Similar presentations


Presentation on theme: "Dr. Sajib Datta  We can also have arrays of arrays, also known as multidimensional arrays.  E.g., A two-dimensional array is an array of several."— Presentation transcript:

1 Dr. Sajib Datta CSE@UTA

2  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.

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

4  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:

5  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;  }

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

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

8  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

9  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);  }

10  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 )

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

12  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

13

14  #include  #define ARRSIZE 7  int main(void)  {  int intarr[ARRSIZE], target, i, left, right, mid;  printf("Please input %d integers which are sorted in the ascending order and a target value.", ARRSIZE);  scanf("%d", &target);   for(i = 0; i<ARRSIZE; i++)  {  scanf("%d", &intarr[i]);  }  left = 0;  right = ARRSIZE-1;  while(left <= right)  {  mid = (left+right)/2;  if(intarr[mid] == target)  {  printf("The index of the target in the array is %d.\n", mid);  break;  }  else if (target > intarr[mid])  {  left = mid + 1;  }  else  {  right = mid - 1;  }  if(left > right)  printf("The target is not in the array.\n");  return 0;  }

15  How many comparisons on average?  Each time ‘x’ is not found, we are taking the half of the remaining array  If the array has size ‘n’, how many times we can slice it into half?

16  After 1 st half: array size n/2  After 2 nd half: array size n/4  After 2rd half: array size n/8  ….  After m-th half: array size n/(2 m )  Since, the minimum array size is 1 [there has to be at least 1 element]  n/(2 m ) = 1 => m = log 2 (n)

17  So, in binary search, there are log 2 (n) comparisons.  If you are given a sorted array of a million integers (2 20 ), ◦ Linear search can make a million comparison operations ◦ Binary search will make around 20 comparisons ◦ You will learn more on performance in algorithm classes. This is very important aspect of programming.

18  You are given an array containing 9 integers, ranging from 1 to 10. Each number between 1 to 10 appears exactly once in the array, but unfortunately one of them is missing. Can you find the missing number?

19  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  We can also have arrays of arrays, also known as multidimensional arrays.  E.g., A two-dimensional array is an array of several."

Similar presentations


Ads by Google