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
What must be included in order to use functions such as getting a process ID?
#include
26
What data type is a process ID?
pid\_t
27
To get a process ID what must be done first?
#include pid\_t n = getpid()
28
What lines are required to get the current working directory?
#include unistd.h char \*getcwd(char \*buf, size\_t size); int chdir(const char \*path);
29
The time.h header includes _ different representations of time \_\_\_ \_\_\_
Two different representations of time: time\_t struct tm
30
struct tm Has what information?
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
struct tm tm\_wday has information regarding the \_\_\_\_ if tm\_wday = 0 what does this mean? 6? 3?
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
What is the difference between tm\_mday and tm\_wday
tm\_mday is the day of the month while tm\_wday is the day of the week from 0-6
33
In a C program we can run a UNIX command using ...
The system() function form: int k = system("string");
34
In a C program we can pipe using the...
popen() function popen("command", "mode"); if mode is r, for read if mode is w, for write
35
In a C program, to run a command or program IN PLACE of the current program, use....
execl()
36
execl() does what and has what parameters?
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
To run several processes im parallel, use the ___ function
fork() function
38
fork() returns ... if a child process and ... if the parent process, otherwise ...
fork() returns 0 if a child process and the child process ID if the parent process, otherwise -1 if there was an error