140 likes | 291 Views
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
E N D
ECS30 Discussion SectionWeek 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 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
content 1 Address Processor 2 Tower of Hanoi 3 GNU debugger
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.
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;
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';
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; } } }
4 Arguments for Function main 13.7 int main(int argc, //argument count char *argv[]) //argument vector { FILE *fp; fp = fopen(argv[1], "r"); }
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)
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'); }
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
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!
Demonstration and Questions • Thanks! • If you have further questions, please post them to the Facebook group