1 / 45

CS 211

CS 211. Recitation 1 TA: Ishani Chakraborty. Admin stuff. Email: ishanic@cs.rutgers.edu Office hours: 5:00-6:00 pm, Tuesdays at Core Bldg, room 246. Switching recitations is allowed, missing them is not ! Please follow the instructions for turning in projects.

Download Presentation

CS 211

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 211 Recitation 1 TA: Ishani Chakraborty

  2. Admin stuff • Email: ishanic@cs.rutgers.edu • Office hours: 5:00-6:00 pm, Tuesdays at Core Bldg, room 246. • Switching recitations is allowed, missing them is not ! • Please follow the instructions for turning in projects. • Check the course webpage every day. - http://www.panic-lab.rutgers.edu/users/peery/cs211/

  3. Programming review • Programming paradigms • Procedural(C)– programming task broken into • Variables • Data structures • PROCEDURES • Object oriented (Java) – task broken into • OBJECTS

  4. Sample C program /*The first C program*/ #include <stdio.h> int main(void) { printf(“hello, world!\n"); return 0; }

  5. Editing • How do you write the code? Popular editors: vi, emacs… • Compiling • How to convert source code to executable code? We use gcc (GNU Compiler Collection) as the compiler.

  6. Compiling code $ gcc hello.c • creates file a.out • Execute by typing $ ./a.out $ gcc –o hello hello.c • Creates file hello • Execute by typing $ ./hello make hello

  7. The three steps • $ vi hello.c • Write code, save, exit. • $ gcc –o hello hello.c • $ ./hello

  8. vi editor: Quickstart • $ vi hello.c • Press i (for inserting text) • Start writing code • When finished, press escape key (to exit editing mode) • Type $ :wq to save and exit • Type $ :q! to quit without saving changes

  9. vi editor: Quickstart • To make any changes eg delete, cut, copy etc. exit the editing mode by pressing the escape key.

  10. Emacs editor • $ emacs hello.c • A good tutorial link –http://www.cs.colostate.edu/helpdocs/emacs.html

  11. Multiple files • A complex program can and should be broken into multiple source files. • To illustrate we look at a simple program with 4 files • main.c • hello.c • factorial.c • functions.h

  12. main.c functions.h void print_hello(void); int factorial(int n); #include <stdio.h> #include "functions.h“ int main(void) { print_hello(); printf("\n"); printf("The factorial of 5 is %d\n",factorial(5)); return 0; } hello.c factorial.c #include "functions.h" int factorial(int n) { if(n!=1){ return(n * factorial(n-1)); } else return 1; } #include <stdio.h> #include "functions.h“ void print_hello() { printf("Hello World!\n"); }

  13. $ gcc main.c • $ gcc –c main.c

  14. gcc-ing with –c flag creates an object file that is independent of other files. • gcc-ing without –c flag = compiling and linking files, so interdependent files have to be compiled together. $ gcc main.c hello.c factorial.c –o hello

  15. Makefile • Compilation of several programs whose files are dependent on one other ? • When making changes to one(few) file(s), several files remain unchanged so don’t need to compile them. • make recompiles only those files that need to be updated (Looks at the timestamps of source files, if source files newer than object files, recompile.)

  16. Makefile • Set of rules target: dependencies [tab] system command myprog.o: myprog.c myprog.h gcc –o myprog myprog.c Target Dependency list [tab] Action(s) to create the target

  17. Makefile-1 all: gcc -o hello main.c hello.c factorial.c

  18. Makefile-2 all: hello hello: main.o factorial.o hello.o gcc main.o factorial.o hello.o –o hello main.o: main.c gcc -c main.c factorial.o: factorial.c gcc -c factorial.c hello.o: hello.c gcc -c hello.c

  19. Makefile-3 # Variable CC will be the compiler to use. CC=gcc # Set the CFLAGS options to be passed to the compiler. CFLAGS=-c –Wall all: hello hello: main.o factorial.o hello.o $(CC) main.o factorial.o hello.o -o hello main.o: main.c $(CC) $(CFLAGS) main.c factorial.o: factorial.c $(CC) $(CFLAGS) factorial.c hello.o: hello.c $(CC) $(CFLAGS) hello.c

  20. Admin stuff • Recitation ppts uploaded at: http://www.research.rutgers.edu/~ishanic/cs211/cs21109.htm • Project 1 due: 16th Feb ’09. • https://handin.rutgers.edu • Single tar file • If your files are in folder called project1 use the command tar -cvf project1.tar project1

  21. Debugging • Bug: Error/fault in a computer program producing incorrect/unexpected results. • A simple debugger: print statements in code. • Code needs recompilation every time. • Guess the source of the problem. • Impractical in complex programs. • Debuggers are powerful and flexible.

  22. Factorial function (fact.c) #include <stdio.h> int factorial(int); void main(void) { int number; printf("Please enter a positive integer: “); scanf("%d",&number); if (number < 0) printf("That is not a positive integer.\n”); else printf(“The factorial of %d is %d“,number,factorial(number)); } int factorial(int number) { int temp; if(number <= 1) return 1; temp = number * factorial(number - 1); return temp; }

  23. gdb basics • GNU debugger • gcc –g source • gdb executable • run • list • break • step • backtrace • print

  24. list

  25. So we have written, compiled, ran and debugged a code. • Now we will focus on what goes into a code, namely data and their structures. • Data structures eg arrays, structures, unions, stacks, queues, trees etc…

  26. Arrays • Group of elements accessed by indexing. • Elements have same data type. • Placed in contiguous memory locations.

  27. Arrays • char foo[80]; • – An array of 80 characters • – sizeof(foo) • = 80 × sizeof(char) • = 80 × 1 = 80 bytes • int bar[40]; • – An array of 40 integers • – sizeof(bar) • = 40 × sizeof(int) • = 40 × 4 = 160 bytes

  28. Nested Arrays • 2D array A • int A[4][3] • Space required to store A ?

  29. Element Address int A[4][3] = { {1, 2, 3}, {4, 5, 6}, {7,8, 9}, {10, 11, 12} };

  30. Structures • Heterogeneous data structure: • Elements can have different data types. • Contiguous memory locations.

  31. structures #include <stdio.h> struct data { char name; struct { int age; int height; } s; }; int main(void) { struct data cs211; cs211.name = ‘H’; cs211.s.age = 21; printf("%c is %d years old\n", cs211.name,cs211.s.age); return 0; }

  32. What does this program do ? #include <stdio.h> #define ITERATIONS 20 int main() { int num[3]={0,1}; int count1, count2; printf("%d %d ", num[0], num[1]); for (count1=0; count1 < ITERATIONS-2; count1++) { num[2] = num[0] + num[1]; printf("%d ",num[2]); for (count2 =0; count2 < 2; count2++) num[count2] = num[count2+1]; } printf(“\n”); return 0; }

  33. What does this program do ? #include <stdio.h> int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n", i, argv[i]); return 0; }

More Related