cs 241 section week #1
DESCRIPTION
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. #defineDIT( - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/1.jpg)
CS 241Section Week #1
![Page 2: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/2.jpg)
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.
![Page 3: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/3.jpg)
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 DITDAH{_DAHDITDITDAH _DIT,DITDAHDAH_,DITDAH DIT_,DITDAH _DIT_,DITDAHDIT_DAH DITDAH,DITDAH DAH_DIT DITDAH;DAHDITDIT _DIT=DIT_DAH DIT 81DAH,DIT_=_DIT__DAH;_DIT==DAH_DIT DIT _DITDAH;__DITDIT'\n'DAH DAH DAHDIT DITDAH_=_DIT;DITDAHDAH_;__DIT DIT DITDAH_DIT_?_DAH DIT DITDAHDIT_ DAH:'?'DAH,__DITDIT' 'DAH,DAH_ __DAH DAH DAHDITDITDITDAH DIT_=2,_DIT_=_DAH_;DITDAH _DIT_&&DITDITDAH _DIT_!=DIT DITDAH DAH_>='a'?DITDAHDAH_&223:DITDAH DAH_ DAH DAH;DITDITDAH DIT_ DAH __DAH,_DIT___DAH DAHDITDAH DIT_+= DIT DITDAH _DIT_>='a'? DITDAH _DIT_-'a':0DAH;}_DAH DIT DIT_ DAH{ __DIT DITDIT_>3?_DAH DIT DIT_>>1 DAH:'\0'DAH;returnDIT_&1?'-':'.';}__DIT DIT DIT_ DAH _DAHDITDIT_;{DIT void DAH write DIT1,&DIT_,1 DAH;}
![Page 4: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/4.jpg)
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 DITDAH{_DAHDITDITDAH _DIT,DITDAHDAH_,DITDAH DIT_,DITDAH _DIT_,DITDAHDIT_DAH DITDAH,DITDAH DAH_DIT DITDAH;DAHDITDIT _DIT=DIT_DAH DIT 81DAH,DIT_=_DIT__DAH;_DIT==DAH_DIT DIT _DITDAH;__DITDIT'\n'DAH DAH DAHDIT DITDAH_=_DIT;DITDAHDAH_;__DIT DIT DITDAH_DIT_?_DAH DIT DITDAHDIT_ DAH:'?'DAH,__DITDIT' 'DAH,DAH_ __DAH DAH DAHDITDITDITDAH DIT_=2,_DIT_=_DAH_;DITDAH _DIT_&&DITDITDAH _DIT_!=DIT DITDAH DAH_>='a'?DITDAHDAH_&223:DITDAH DAH_ DAH DAH;DITDITDAH DIT_ DAH __DAH,_DIT___DAH DAHDITDAH DIT_+= DIT DITDAH _DIT_>='a'? DITDAH _DIT_-'a':0DAH;}_DAH DIT DIT_ DAH{ __DIT DITDIT_>3?_DAH DIT DIT_>>1 DAH:'\0'DAH;returnDIT_&1?'-':'.';}__DIT DIT DIT_ DAH _DAHDITDIT_;{DIT void DAH write DIT1,&DIT_,1 DAH;}
Especially when you try! (More examples at www.ioccc.org)
![Page 5: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/5.jpg)
Topics This Section
• SVN• C Code Examples in Real Life• Programming Tools
![Page 6: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/6.jpg)
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.)
![Page 7: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/7.jpg)
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)
![Page 8: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/8.jpg)
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
![Page 9: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/9.jpg)
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”
![Page 10: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/10.jpg)
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!
![Page 11: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/11.jpg)
Going back
• svn log• svn up (short for?)• svn log• svn update -rXX
![Page 12: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/12.jpg)
SVN
• Conclusion– Learn it– Love it– Hate it
![Page 13: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/13.jpg)
C examples
• Go to ~/svn/ds/ds1• Time for some real fun!• Open ds1.c using your favorite editor
![Page 14: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/14.jpg)
Fun Part #1
void problem1(){ char str[7]="abc"; strcat(str,"def"); printf("%s",str);}
//Issues ?
![Page 15: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/15.jpg)
Fun Part #1
• Are you ready for the answers on the next slide?
• Did you use the manpages for strcat?
![Page 16: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/16.jpg)
Fun Part #1
#include <string.h> //strcatvoid problem1(){ char str[7]; //avoid ptr to static mem strcat(str,"abc"); strcat(str,"def"); printf("%s",str);}
![Page 17: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/17.jpg)
Test
• gcc ds.c• What is binary called?• Does it work?
• Uncomment Problem2
![Page 18: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/18.jpg)
Fun Part #2void 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);}
![Page 19: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/19.jpg)
Fun Part #2
• How many bugs can you find?
• Once you are confident test your program
![Page 20: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/20.jpg)
Fun Part #2
Are you ready for the answers?
• Try running `valgrind a.out`
![Page 21: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/21.jpg)
Fun Part #2constant int maxFieldSize=20void 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);}
![Page 22: CS 241 Section Week #1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/5681324d550346895d98c514/html5/thumbnails/22.jpg)
Questions?
• As a challenge see if you can optimize factorial for subsequent accesses - make sure that it still works for the general case