1 / 22

CS 241 Section Week #1

CS 241 Section Week #1. About Sections. Each week: We’ll spend additional time on topics that the instructors feel should be reviewed. We’ll prepare you for the upcoming homework or MP submissions. We’ll provide extra review/guidance for upcoming exams. C can be Ugly. #define DIT (

ariel-doyle
Download Presentation

CS 241 Section Week #1

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. CS 241Section Week #1

  2. About Sections • Each week: • We’ll spend additional time on topics that the instructors feel should be reviewed. • We’ll prepare you for the upcoming homework or MP submissions. • We’ll provide extra review/guidance for upcoming exams.

  3. C can be Ugly #define DIT ( #define DAH ) #define __DAH ++ #define DITDAH * #define DAHDIT for #define DIT_DAH malloc #define DAH_DIT gets #define _DAHDIT char _DAHDIT _DAH_[]="ETIANMSURWDKGOHVFaLaPJBXCYZQb54a3d2f16g7c8a90l?e'b.s;i,d:" ;main DIT DAH{_DAHDIT DITDAH _DIT,DITDAH DAH_,DITDAH DIT_, DITDAH _DIT_,DITDAH DIT_DAH DIT DAH,DITDAH DAH_DIT DIT DAH;DAHDIT DIT _DIT=DIT_DAH DIT 81 DAH,DIT_=_DIT __DAH;_DIT==DAH_DIT DIT _DIT DAH;__DIT DIT'\n'DAH DAH DAHDIT DIT DAH_=_DIT;DITDAH DAH_;__DIT DIT DITDAH _DIT_?_DAH DIT DITDAH DIT_ DAH:'?'DAH,__DIT DIT' 'DAH,DAH_ __DAH DAH DAHDIT DIT DITDAH DIT_=2,_DIT_=_DAH_; DITDAH _DIT_&&DIT DITDAH _DIT_!=DIT DITDAH DAH_>='a'? DITDAH DAH_&223:DITDAH DAH_ DAH DAH; DIT DITDAH DIT_ DAH __DAH,_DIT_ __DAH DAH DITDAH DIT_+= DIT DITDAH _DIT_>='a'? DITDAH _DIT_-'a':0 DAH;}_DAH DIT DIT_ DAH{ __DIT DIT DIT_>3?_DAH DIT DIT_>>1 DAH:'\0'DAH;return DIT_&1?'-':'.';}__DIT DIT DIT_ DAH _DAHDIT DIT_;{DIT void DAH write DIT 1,&DIT_,1 DAH;}

  4. C can be Ugly #define DIT ( #define DAH ) #define __DAH ++ #define DITDAH * #define DAHDIT for #define DIT_DAH malloc #define DAH_DIT gets #define _DAHDIT char _DAHDIT _DAH_[]="ETIANMSURWDKGOHVFaLaPJBXCYZQb54a3d2f16g7c8a90l?e'b.s;i,d:" ;main DIT DAH{_DAHDIT DITDAH _DIT,DITDAH DAH_,DITDAH DIT_, DITDAH _DIT_,DITDAH DIT_DAH DIT DAH,DITDAH DAH_DIT DIT DAH;DAHDIT DIT _DIT=DIT_DAH DIT 81 DAH,DIT_=_DIT __DAH;_DIT==DAH_DIT DIT _DIT DAH;__DIT DIT'\n'DAH DAH DAHDIT DIT DAH_=_DIT;DITDAH DAH_;__DIT DIT DITDAH _DIT_?_DAH DIT DITDAH DIT_ DAH:'?'DAH,__DIT DIT' 'DAH,DAH_ __DAH DAH DAHDIT DIT DITDAH DIT_=2,_DIT_=_DAH_; DITDAH _DIT_&&DIT DITDAH _DIT_!=DIT DITDAH DAH_>='a'? DITDAH DAH_&223:DITDAH DAH_ DAH DAH; DIT DITDAH DIT_ DAH __DAH,_DIT_ __DAH DAH DITDAH DIT_+= DIT DITDAH _DIT_>='a'? DITDAH _DIT_-'a':0 DAH;}_DAH DIT DIT_ DAH{ __DIT DIT DIT_>3?_DAH DIT DIT_>>1 DAH:'\0'DAH;return DIT_&1?'-':'.';}__DIT DIT DIT_ DAH _DAHDIT DIT_;{DIT void DAH write DIT 1,&DIT_,1 DAH;} Especially when you try! (More examples at www.ioccc.org)

  5. Topics This Section • SVN • C Code Examples in Real Life • Programming Tools

  6. Subversion What it is • Collaboration tool for large projects • Good at Code Backups • Efficient - Uses diff's for backup compression • A good learning block for other version control systems (git, etc.)

  7. Subversion What it is not • File system backup (bad at binaries) • Concurrent access tool (not google docs) • Good at merging lots of changes (commit often)

  8. Try it! • svn checkout https://subversion.ews.illinois.edu/svn/sp11-cs241/NETID/ svn • If you have already checked out the repository, run `svn update` inside the directory

  9. Try it! • cd ~/svn (what does ~ mean) • echo “this file holds my idea” > idea • ls && svn add idea(what does && do) • svn status • svn commit -m “my first big idea”

  10. Try it! • Edit the file idea and save • Commit the changes (Do you remember the command) • Oh no, you ruined your first idea and want to go back!

  11. Going back • svn log • svn up (short for?) • svn log • svn update -rXX

  12. SVN • Conclusion • Learn it • Love it • Hate it

  13. C examples • Go to ~/svn/ds/ds1 • Time for some real fun! • Open ds1.c using your favorite editor

  14. Fun Part #1 • void problem1(){ • char str[7]="abc"; • strcat(str,"def"); • printf("%s",str); • } • //Issues ?

  15. Fun Part #1 • Are you ready for the answers on the next slide? • Did you use the manpages for strcat?

  16. Fun Part #1 • #include <string.h> //strcat • void problem1(){ • char str[7]; //avoid ptr to static mem • strcat(str,"abc"); • strcat(str,"def"); • printf("%s",str); • }

  17. Test • gcc ds.c • What is binary called? • Does it work? • Uncomment Problem2

  18. Fun Part #2 • void problem2(){ • char *str; • for(int i=0;i<42;i+1) • str = malloc( sizeof(char)); • if( factorial(i,str) ){ //Error • return 1; • } • printf("%d : %s\n",str,i); • } • int factorial(int num, const char* answer){ • while(num >= 0){ • num *= --num; • sprintf(answer,"%d",num); • }

  19. Fun Part #2 • How many bugs can you find? • Once you are confident test your program

  20. Fun Part #2 Are you ready for the answers? • Try running `valgrind a.out`

  21. Fun Part #2 • constant int maxFieldSize=20 • void problem2(){ • char *str; • int i; • constant int theAnswer = 42; • str = malloc( sizeof(char)*(maxFieldSize) ); • for(i=0;i<theAnswer;i++){ • if( factorial(i,str) ){ //Error • printf(“factorial failed\n”); • exit(1); • } • printf("%d : %s\n",i,str); • } • } • int factorial(int num, char* answer){ • while(num > 0){ • num *= num--; • snprintf(answer,maxFieldSize,"%d",num); • }

  22. Questions? • As a challenge see if you can optimize factorial for subsequent accesses - make sure that it still works for the general case

More Related