Week 11: Recursion / Command Line Parameters / UNIX Flashcards

1
Q

What is recursion?

A

Defining something in terms of itself

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Recursive definitions MUST include

A

A base case

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In recursion, the base case is also known as the

A

Simplest case

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In recursion, the recursive part is also known as the

A

General case

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is a base case needed in recursion?

A

Recursive definitions without a base case will run infinitely

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Recursion can be an alternative to

A

Iteration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

A function that makes use of recursion is called

A

A recursive method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does a recursive method for defining a factorial look like?

A

The base case is that the number being tested is 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

With recursion, what happens with the memory?

A

Every time the recursive function calls itself, it creates a new place in memory in the stack frame for the new function call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

For a RECURSIVE function that calculates the factorial of a number, what is the BASE case?

A

if (numb == 1) {

result == 1;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

For a RECURSIVE function that calculates the factorial of a number, what is the NON BASE case? When does it execute?

*given the function is called SumRec()*

A

else {

result = (numb + SumRec( numb - 1));

}

This executes when numb is not equal to 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

For a recursive function , how many copies of the code are there?

A

Just one, like any other function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When does the recursive function stop calling itself?

A

When the base case is reached

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What happens when the base case is finally reached?

A

One after one, the function returns by popping off the runtime stack to the calling function until we get to the first call of the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

The most important function of C is ____ function. It is mostly defined with a return type of _____ and without _____

A

The most important function of C is main() function. It is mostly defined with a return type of int and without parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

For C, command-line arguments are given after the …

A

Name of the program in the command-line shell of the operating system

17
Q

To allow a C program to take in command line arguments, we must make the _____ of the _____ have the following two ______

________

and

________

A

To allow a C program to take in command line arguments, we must make the main function of the file have the following two parameters

first argument is the number of command-line arguments

and

the second is the list of the arguments

int main( int argc, char *argv[])

18
Q

What does a main function is C look like that is designed to take in command-line arguments?

A

int main( int argc, char *argv[])

19
Q

In a C main function parameter that is intended to take command-line arguments, the first parameter ____ should….

A

int argc

Should never be negative

20
Q

In C, for a command-line argument main function, the command line argument argv[0] is what?

A

The name of the program

21
Q

In C, for a command-line argument main function, argc stands for what?

A

ARGument Count

22
Q

In C, for a command-line argument main function, argv means what?

A

ARGument Vector

23
Q

What are the properties of command-line arguments?

A
  1. They are passed to main() function.
  2. They are parameters/arguments supplied to the program when it is invoked.
  3. They are used to control program from outside instead of hard coding those values inside the code.
  4. argv[argc] is a NULL pointer.
  5. argv[0] holds the name of the program.
  6. argv[1] points to the first command line argument and argv[n] points last argument.
24
Q

To pass multiple words as a single command-line argument, you can…

A

Surround them with double or single quotes

25
Q

What must be included in order to use functions such as getting a process ID?

A

include

26
Q

What data type is a process ID?

A

pid_t

27
Q

To get a process ID what must be done first?

A

include

pid_t n = getpid()

28
Q

What lines are required to get the current working directory?

A

include unistd.h

char *getcwd(char *buf, size_t size);

int chdir(const char *path);

29
Q

The time.h header includes _ different representations of time

___

___

A

Two different representations of time:

time_t

struct tm

30
Q

struct tm

Has what information?

A

struct tm{

int tm_sec; // seconds [0,61]

int tm_min; // minutes [0,59]

int tm_hour; // hour [0,23]

int tm_mday; // day of month [1,31]

int tm_mon; // month of year [0,11]

int tm_year; // years since 1900

int tm_wday; // day of week [0,6] (Sunday = 0)

int tm_yday; // day of year [0,365]

int tm_isdst; // daylight savings flag

}

31
Q

struct tm tm_wday has information regarding the ____

if tm_wday = 0 what does this mean?

6?

3?

A

struct tm tm_wday has information regarding the day of the week

if tm_wday = 0 the day is SUNDAY

6 = SATURDAY

3 = WEDNESDAY

32
Q

What is the difference between tm_mday and tm_wday

A

tm_mday is the day of the month while tm_wday is the day of the week from 0-6

33
Q

In a C program we can run a UNIX command using …

A

The system() function

form: int k = system(“string”);

34
Q

In a C program we can pipe using the…

A

popen() function

popen(“command”, “mode”);

if mode is r, for read

if mode is w, for write

35
Q

In a C program, to run a command or program IN PLACE of the current program, use….

A

execl()

36
Q

execl() does what and has what parameters?

A

Transfers the current process to the one called, it has the following parameters:

int execl(const char *path, const char *arg0, …, const char *argn, char * /*NULL*/);

path is the pathname of the executable file. arg0 should be the same as path or the filename. arg1 to argn are the actual arguments The last parameter must be NULL (or 0).

37
Q

To run several processes im parallel, use the ___ function

A

fork() function

38
Q

fork() returns … if a child process and … if the parent process, otherwise …

A

fork() returns 0 if a child process and the child process ID if the parent process, otherwise -1 if there was an error