CS 211 - Rutgers University

Download Report

Transcript CS 211 - Rutgers University

CS 211
Recitation 1
TA: Ishani Chakraborty
Admin stuff
• Email: [email protected]
• 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/
Programming review
• Programming paradigms
– Procedural(C)– programming task broken into
• Variables
• Data structures
• PROCEDURES
– Object oriented (Java) – task broken into
• OBJECTS
Sample C program
/*The first C program*/
#include <stdio.h>
int main(void)
{
printf(“hello, world!\n");
return 0;
}
• 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.
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
The three steps
• $ vi hello.c
– Write code, save, exit.
• $ gcc –o hello hello.c
• $ ./hello
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
vi editor: Quickstart
• To make any changes eg delete, cut, copy
etc. exit the editing mode by pressing the
escape key.
x
Delete a character
dd
Delete a line
yy
Copy a line
p
Paste a copied line
Emacs editor
• $ emacs hello.c
– A good tutorial link –
http://www.cs.colostate.edu/helpdocs/emacs.html
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
main.c
#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
#include <stdio.h>
#include "functions.h“
void print_hello()
{
printf("Hello World!\n");
}
functions.h
void print_hello(void);
int factorial(int n);
factorial.c
#include "functions.h"
int factorial(int n)
{
if(n!=1){
return(n * factorial(n-1));
}
else return 1;
}
• $ gcc main.c
• $ gcc –c main.c
• 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
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.)
Makefile
• Set of rules
target: dependencies
[tab] system command
Target
Dependency list
myprog.o: myprog.c myprog.h
gcc –o myprog myprog.c
[tab]
Action(s) to create the target
Makefile-1
all:
gcc -o hello main.c hello.c factorial.c
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
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
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
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.
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;
}
gdb basics
• GNU debugger
– gcc –g source
– gdb executable
– run
– list
– break
– step
– backtrace
– print
list
• 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…
Arrays
• Group of elements accessed by indexing.
• Elements have same data type.
• Placed in contiguous memory locations.
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
Nested Arrays
• 2D array A
• int A[4][3]
• Space required to store A ?
Element
Address
A[0][0]
xA
A[0][1]
xA + 4
A[0][2]
xA + 8
A[1][0]
xA + 12
A[1][1]
xA + 16
A[1][2]
xA + 20
A[2][0]
xA + 24
A[2][1]
xA + 28
A[2][2]
xA + 32
A[3][0]
xA + 36
A[3][1]
xA + 40
A[3][2]
xA + 44
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} };
Structures
• Heterogeneous data structure:
– Elements can have different data types.
• Contiguous memory locations.
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;
}
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;
}
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;
}