1 / 24

CSE1320-003-Fall 2014 Arrays

CSE1320-003-Fall 2014 Arrays. Dr. Sajib Datta CSE@UTA Sep 9, 2014. Quiz 1 What does the following program print? (10 pts). #include&lt; stdio.h &gt; void main() { int a = 95; int b = 0; int c = -75; if( a || b ) printf (&quot;Test1<br>&quot;); else printf (&quot;Test2<br>&quot;); if( a &amp;&amp; c )

dillian
Download Presentation

CSE1320-003-Fall 2014 Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE1320-003-Fall 2014Arrays Dr. SajibDatta CSE@UTA Sep 9, 2014

  2. Quiz 1What does the following program print? (10 pts) • #include<stdio.h> • void main() • { • int a = 95; • int b = 0; • int c = -75; • 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. Quiz 1What does the following program print? (10 pts) • #include<stdio.h> • void main() • { • int a = 95; • int b = 0; • int c = 75; • 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. 1-D Array • 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. 1-D Array • 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}; • Neveraccess a variable in an array beyond the index bound!!!!

  6. Initialization • 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. Example of 1-D Array • #include <stdio.h> • intmain(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. Array Size with variables • int n=19; • intiarr[n]; • or • #define ARRAY_SIZE 19 // have it after #include <stdio.h> • intiarr[ARRAY_SIZE];

  9. Find Min and Max from an array • Given • int array1D[5] = { 32,44,33,12,65}; • Output: • The min value is 12. • The max value is 65.

  10. #include <stdio.h> • int main(void) • { • inti, 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. Print an array in reverse • Original sentence: I am at UTA • The reverse sentence: ATU ta ma I

  12. #include <stdio.h> • int main(void) • { • inti; • 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. Multidimensional Array • 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. 2-D Array • A 2D array can be viewed like a table. • To create a 2D array for this table of values, we could use • intsome_data[2][3] = { {10, 5, 13}, {2, 7, 19} }; • Or • intsome_data[][3] = { {10, 5, 13}, {2, 7, 19} };

  15. 2-D Array • 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: • intSales[2][3] = { {1, 2, 3}, {4, 5, 6} }; • The individual elements are referenced with the following combinations of array name and indices:

  16. 2-D Array • Print out a specific row or column in an array • #include <stdio.h> • intmain(void) • { • introw, 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. Copying an array • In case of normal variables: to assign value of ‘a’ to ‘b’; • inta,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. Copying an array • Have to iterate over ‘a’ and assign individual array elements into ‘b’ • for(i=0;i<10;i++) • b[i] = a[i];

  19. Linear search • 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. Linear search • int array[100]; • inti,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. Linear search • 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 220)

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

  23. Binary Search • 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. Online materials for next class • Array • http://www.java2s.com/Tutorial/C/0140__Array/Catalog0140__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/handouts/brief-complexity.pdf • http://www.csd.uwo.ca/courses/CS1037a/notes/topic13_AnalysisOfAlgs.pdf (advanced!)

More Related