1 / 14

ECS30 Discussion Section Week 7

ECS30 Discussion Section Week 7. TA's: Yun Li Keith Wang Shizhuo Yu University of California, Davis, CA. Office Hour This Week. Thursday 10-12am 3-5pm 53 Kemper yunli@ucdavis.edu kcwang@ucdavis.edu szyu@ucdavis.edu. hw7. Tuesday, March 13 th , 2012, 11:59 p.m

apollo
Télécharger la présentation

ECS30 Discussion Section Week 7

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. ECS30 Discussion SectionWeek 7 TA's: Yun Li Keith Wang Shizhuo Yu University of California, Davis, CA

  2. Office Hour This Week Thursday 10-12am 3-5pm 53 Kemper yunli@ucdavis.edu kcwang@ucdavis.edu szyu@ucdavis.edu

  3. hw7 Tuesday, March 13th, 2012, 11:59 p.m Disallowed to use printf/scanf Must use fprintf/fscanf instead At least one debugging fprintf statement per function Use “gdb” to debug your programs Use “script”to record the debugging process

  4. content 1 Address Processor 2 Tower of Hanoi 3 GNU debugger

  5. Project 11.5 You are designing a program to 1 process a list of Internet addresses 2 identify all pairs of computers from the came locality You should do 1 create a structure type with five components 2 read a list of addresses and nicknames 3 display same locality pair messages 4 include your program 3 functions.

  6. 1 create a structure type—page 568 #define size 10 typedef struct{ int adr1; /*xx*/ int adr2; /*yy*/ int adr3; /*zz*/ int adr4; /*mm*/ char name[size]; /*nick name*/ }address_t;

  7. 2 create array of structure read a list of structure #define MAX 100 address_t address[MAX]; /*array of structures*/ for(int i=0;i<MAX;i++){ If (sentinel) {save i as number of adr; go out of the loop;} else {Read from file;} } Sentinel 1 (address_t.adr1==0 && address_t.adr2==0 && ... ); 2 address_t.name=='none';

  8. 3 display same locality pair messages for(i=0;i<LEN-1;i++){ for(j=i+1,j<LEN;j++){ if(address[i].adr1==address[j].adr1&&...) {compare=1; /*true*/ output address[i].name and address[j].name; } } }

  9. 4 Arguments for Function main 13.7 int main(int argc, //argument count char *argv[]) //argument vector { FILE *fp; fp = fopen(argv[1], "r"); }

  10. Tower of Hanoi(recursions) 3 pegs problem H(n) : times/solution to Hanoi of n H(1)=1 H(n+1)=H(n)+1+H(n) |H(n)|=2^n-1 O(2^n)

  11. Pseudo code void move(char x,char y) { printf("%c-->%c\n",x,y);} //move from 'one' to 'three' using 'two' void hanoi (int n,char one,char two,char three) { If (n==1) { move(one,three);} else { hanoi(n-1,one,three,two); move(one,three); hanoi(n-1,two,one,three); } } int main(){ Input the number of disks n; hanoi(n,'A','B','C'); }

  12. 4 pegs problem H(1)=1 H(2)=3 H(n)=H(n-2)+1+1+1+H(n-2) O(2^(n/2)) Much less than 3 pegs problem

  13. Time consuming Example: 3 peg problem, 64disks-->2^64 My computer-->2GHz One minute-->60*2*10^9 times=2^37 If you input number of 64, it'll take you 2^27 min to compute. Experiment: 4 pet problem, 30disks → woooa, so quick!

  14. Demonstration and Questions • Thanks! • If you have further questions, please post them to the Facebook group

More Related