Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 211 Recitation 1 TA: Ishani Chakraborty. Admin stuff Office hours: 5:00-6:00 pm, Tuesdays at Core.

Similar presentations


Presentation on theme: "CS 211 Recitation 1 TA: Ishani Chakraborty. Admin stuff Office hours: 5:00-6:00 pm, Tuesdays at Core."— Presentation transcript:

1 CS 211 Recitation 1 TA: Ishani Chakraborty

2 Admin stuff Email: ishanic@cs.rutgers.eduishanic@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 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. xDelete a character ddDelete a line yyCopy a line pPaste a copied line

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

11 Example of Makefile Project1: Project1.o Inventory.o Cd.o Date.o gcc -o proj1 Project1.o Inventory.o Cd.o Date.o Project1.o: Project1.c Inventory.h gcc -c Project1.c Inventory.o: Inventory.c Inventory.h Cd.h gcc -c Inventory.c Cd.o: Cd.c Cd.h Date.h gcc -c Cd.c Date.o: Date.c Date.h gcc -c Date.c Example taken from course webpage of 341 from www.cs.umbc.edu

12 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

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

14 $ gcc main.c $ gcc –c main.c

15 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

16 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.)

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

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

19 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

20 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

21

22

23

24

25 Admin stuff Recitation ppts uploaded at: http://www.research.rutgers.edu/~ishanic/cs211/cs21109.htm Project 1 due: 16 th 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

26 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.

27 Factorial function (fact.c) #include 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; }

28

29

30 gdb basics GNU debugger –gcc –g source –gdb executable –run –list –break –step –backtrace –print

31 list

32

33

34

35 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…

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

37 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

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

39 A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] A[2][0] A[2][1] A[2][2] A[3][0] A[3][1] A[3][2] xAxA x A + 4 x A + 8 x A + 12 x A + 16 x A + 20 x A + 24 x A + 28 x A + 32 x A + 36 x A + 40 x A + 44 ElementAddress A[0][0]A[0][1]A[0][2] A[1][0]A[1][1]A[1][2] A[2][0]A[2][1]A[2][2] A[3][0]A[3][1]A[3][2] int A[4][3] = { {1, 2, 3}, {4, 5, 6}, {7,8, 9}, {10, 11, 12} };

40 Structures Heterogeneous data structure: – Elements can have different data types. Contiguous memory locations.

41 structures #include 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; }

42

43 What does this program do ? #include #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; }

44

45 What does this program do ? #include 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; }

46


Download ppt "CS 211 Recitation 1 TA: Ishani Chakraborty. Admin stuff Office hours: 5:00-6:00 pm, Tuesdays at Core."

Similar presentations


Ads by Google