Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Sajib Datta Sep 10, 2014.  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else.

Similar presentations


Presentation on theme: "Dr. Sajib Datta Sep 10, 2014.  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else."— Presentation transcript:

1 Dr. Sajib Datta CSE@UTA Sep 10, 2014

2  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else ◦ printf("Test2\n");  if( a && c ) ◦ printf("Test3\n");  else ◦ printf("Test4\n"); if( !a ) printf("Test5\n"); else printf("Test6\n"); if( (a && b) || c ) printf("Test7\n"); else printf("Test8\n"); if( !c || !b ) printf("Test9\n"); else printf("Test10\n"); }

3  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b )  printf("Test1\n");  else  printf("Test2\n");  if( a && c )  printf("Test3\n");  else  printf("Test4\n");  if( !a )  printf("Test5\n");  else  printf("Test6\n");  if( (a && b) || c )  printf("Test7\n");  else  printf("Test8\n");  if( !c || !b )  printf("Test9\n");  else  printf("Test10\n");  } Answer: Test1 Test3 Test6 Test7 Test9

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

5  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!!!!

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

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

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

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

10  #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;  }

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

12  #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;  }

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

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

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

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

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

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

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

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

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

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

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

24  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 Sep 10, 2014.  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else."

Similar presentations


Ads by Google