Week 2 - Arrays, Strings, Random Numbers and Functions Flashcards

1
Q

In C, how do you declare a 1d array of 5 characters, initialised with 5 values?

A

char array[5] = {‘A’, ‘B’, ‘C’, ‘D’, ‘E’};

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

In C, how do you declare an initialised 2d 2x3 array of ints?

A

int array[2][3] = { {1, 2, 3}, {4, 5, 6} };

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

Where is it guaranteed that arrays are stored in C?

A

In contiguous memory.

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

How do arrays reserve an appropriate amount of memory in C? Use example int array[10].

A

int array[10] reserved 10 x sizeof(int) = 10 * 4 bytes = 40 bytes saved for array.

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

What is another name for an array of chars in C?

A

String

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

In C, what variable type uses single quotes (‘’) and what uses double quotes (“”)?

A

A char uses single, a string uses double

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

In C, what does the null terminator do?

A

The null terminator is ‘\0’. It tells printf when to stop printing out characters

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

In C, how does memory get allocated for a string?

A

Memory allocated is number of characters plus one for the null terminator

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

What happens if your string doesn’t have a null terminator?

A

printf keeps going through memory and printing out characters until it encounters a null terminator (which is one reason arrays are stored contiguously)

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

How would you print out 3 characters of the string variable char animal[21]=”elephant” in C?

A

printf(“0.3s\n”, name);

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

How would you write this C code with width and precision? printf(“%20.10s\n”, “My elephants”);

A

int wid = 20;
int prec = 10;
char str[] = “My elephants”;
printf(“%.s\n”, wid,prec, str);

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

What do these special characters mean?
\n
\t
\v
\b
\r
\f

'
"
%%

A

\n - line-feed (new line)
\t - horizontal tab
\v - vertical tab
\b - backspace
\r - carriage return
\f - form-feed (new page)
\ - backslash
' - single quote
" - double quote
%% - percent sign

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

What do these C string functions do?
strlen(a)
strcpy(a, b)
strcat(a, b)
strcmp(a, “hello”)
sprintf(buffer, “%s %d”, a, b)

A

strlen(a) - returns the number of characters in variable name, not including the null terminator
strcpy() - copies the string a into b, including the null terminator
strcat(a, b) - concatenates b onto the end of a, overwriting a from first null terminator
strcmp(a, “hello”) - returns 0 if the two strings are the same, otherwise some other int value
sprintf(buffer, “%s %d”, a, b) - like printf, but places the result in buffer instead of printing it out

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

Give 3 features of pseudo-random numbers.

A

Generated by an algorithm
Repeats over a (hopefully) very long period
Look random but aren’t

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

Describe these random number distributions:
Uniform
Normal (Gaussian)
Poisson
Bernoulli
Gamma

A

Uniform - a distribution where all values in a defined range are equally likely
Normal (Gaussian) - the bell curve, a result of the Central Limit Theorem
Poisson - the distribution of a count
Bernoulli - the distribution of two mutually exclusive events (call them 0 and 1)
Gamma - a distribution of positive values only

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

What header file in C is needed to include random numbers?

A

stdlib.h

17
Q

What is the command to generate a random number in C?

A

rand()

18
Q

What is a “good enough” sequence length for PRNGs (Pseudo Random Number Generators)?

A

2^26 = (1.8 x 10^19)

19
Q

What is the general formula for how pseudo random numbers are generated?

A

j := (j x a + c)%
Where a, c and m determine the period and complexity of the sequence

20
Q

Does integer division in C round up or down?

A

Down

21
Q

How would you cast an integer to a float in C?

A

Preceding the integer value with (float)

22
Q

How do you set the random number seed in C?

A

srand(num)

23
Q

What is GSL?

A

The GNU Scientific Library, used for producing high quality randomness in random numbers

24
Q

What link flags need to be included for the GSL library?

A

-lgsl -lgslcblas

25
Q

In C, are functions and methods the same?

A

No

26
Q

What is a function in C?

A

A uniquely named group of program statements (so no overloading), which accepts 0 or more arguments or parameters, and returns 0 or 1 value to the calling code.

27
Q

How is a function declared in C?

A

<return> myFunction (<parameter>)
{
/* zero or more program statements*/
return <value>;
}
</value></parameter></return>

28
Q

What is the form of the main function in C?

A

int main()
{
/zero or more statements/
return 0;
}

29
Q

In C, what is a procedure?

A

A function with no return value (void)

30
Q

Functions need to be declared in C (like variables) because…

A
  1. The (one-pass) compiler needs to know a function’s definition before it is called
  2. Functions may appear in any order in the program file (main first, by strong convention)
  3. The functions may call each other
  4. The code for the function might not be in this program file
31
Q

Where are prototypes for standard functions in C often supplied?

A

In .h header files

32
Q

What is function prototyping?

A

Declaring functions at the start of the program, e.g. int calculateAge();

33
Q

Should global variables be used?

A

No - it is very bad practice!

34
Q

In C, what are static variables and how are they declared?

A

Declared using the “static” key word, e.g. static int num = 2;

They are variables initialised once when the function is first called, and retain their value when they go out of scope.