1 / 18

Exercise 2

Exercise 2. CS 110 Sum 09. what (104,16). X=104 y=16. X=88 y=16. X=72 y=16. X=56 y=16. X=40 y=16. X=24 y=16. X=8 y=16. X=8 y=8. what (15,35). X=15 y=35. X=15 y=20. X=15 y=5. X=10 y=5. X=5 y=5. what (63,18). X=63 y=18. X=45 y=18. X=27 y=18. X=9

waite
Download Presentation

Exercise 2

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. Exercise 2 CS 110 Sum 09

  2. what (104,16) X=104 y=16 X=88 y=16 X=72 y=16 X=56 y=16 X=40 y=16 X=24 y=16 X=8 y=16 X=8 y=8

  3. what (15,35) X=15 y=35 X=15 y=20 X=15 y=5 X=10 y=5 X=5 y=5

  4. what (63,18) X=63 y=18 X=45 y=18 X=27 y=18 X=9 y=18 X=9 y=9

  5. Palindrome bool palindrome(string s, int first, int last) { if(first>=last) return true; else if(s[first] != s[last]) return false; else return palindrome(s, first+1, last-1); }

  6. Tracing Palindrome “dad” and “daa” “dad” “dabd” true false F=0, L=2 F=0, L=3 true false F=1, L=2 F=1, L=1

  7. n = process (a , 12 , 10 ); 05 3 0 17 10 9 20 0 1 2 3 710 1+ n=12, x=10 A[10] = 0 n = 4 1+ 1+ X=9 A[9] = 0 + X=11 A[11] = 0 0 0 0 + 1+ + X=8 A[8] = 0 X=10 X=10 X=12 0 0 X=7 X=9

  8. long int z = Func( 5 , 3 ); z=10 n=5, m=3 6 4 n=4, m=3 + n=4, m=2 3 3 3 + 1 + n=3, m=3 n=3, m=2 n=3, m=2 n=3, m=1 1 1 1 2 2 + + 2 + n=2, m=2 n=2, m=1 n=2, m=2 n=2, m=1 n=2, m=1 n=2, m=0 1 1 1 + 1 1 1 + + 1,1 1,0 1,1 1,0 1,1 1,0

  9. sum (Divide & Conquer) int sum( int a[ ], int s, int e) { if ( s > e ) return 0; else if ( s == e ) return a [ s ]; else { int m = ( s + e ) / 2; return sum ( a, s, m ) + sum ( a, m+1, e ); } }

  10. sum (Divide & Conquer) 29 s=0, e=6 M=3 14 15 s=0, e=3 M=1 + s=4, e=6 M=5 9 12 A[6] + 6 + s=0, e=1 M=0 s=2, e=3 M=2 s=4, e=5 M=4 s=6, e=6 A[0] A[4] A[1] A[2] A[3] + + + A[5] s=0,e=0 s=1,e=1 s=2,e=2 s=3,e=3 s=4,e=4 s=5,e=5

  11. oddcount int oddcount(int A[ ], int s, int e) { if (s > e) return 0; else if ( A[s]%2 ) return 1 + oddcount (A, s+1, e); else return oddcount (A, s+1, e); }

  12. asciisum (Blanks included) int asciisum (string s, int first, int last) { if( first > last) return 0; return int ( s [first] ) + asciisum (s, first + 1, last); }

  13. asciisum (Blanksexcluded) int asciisum (string s, int first, int last) { if( first > last) return 0; if( s[first] == ‘ ‘) return asciisum (s, first + 1, last); return int ( s [first] ) + asciisum (s, first + 1, last); }

  14. Assignment 3 • int map ( int i, int j ) { return i * M + j; } • void zeros ( int * count ); • void zeros ( int * & count ); // if allocate memory inside zeros • void move ( int & ibug, int & jbug, int imove[ ], int jmove[ ] ) • bool inside ( int i, int j ); • void display ( int * count, int total ) • bool done ( int * count );

  15. Assignment 3 //in main function //Initialize int imove[9]={0,-1,0,1,1,1,0,-1,-1}; int jmove[9]={0,1,1,1,0,-1,-1,-1,0}; int sum=0;

  16. Assignment 3 //should be entered by user or randomly generated with validation int ibug=10; int jbug=12; M = 15; N = 15;

  17. Assignment 3 srand ((unsigned) time (NULL)); // Memory allocation int * count = new int [ M * N ]; //initial bug location count[map(ibug, jbug)] = 1;

  18. Assignment 3 Loop incrementing sum (total) call move function to move the bug increment array at that location until done call display function Free memory //delete [ ] count;

More Related