CS21003 - Multi-Paradigm Programming

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/68

flashcard set

Earn XP

Description and Tags

NEED TO ADD: stack, deq, all C++

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

69 Terms

1
New cards

C - display output function

printf()

2
New cards

C - get input function

scanf()

3
New cards

Format specifier for int

%d

4
New cards

Format specifier for float

%f

5
New cards

Format specifier for double

%lf

6
New cards

Format specifier for char

%c

7
New cards

Format specifier for string

%s

8
New cards

#include <> vs ““

9
New cards

Is C an OOP?

No

10
New cards
int numbers[?][?] = {
	{1,2.3},
	{4,5,6}
};

Fill in the question marks

2 3

11
New cards

What is the keyword for setting a constant

#define

12
New cards

Flag to rename .exe file when using gcc compilation

-o (gcc hello.c -o my-prog)

13
New cards

What do structs contain?

attributes

14
New cards

What is the main include we use?

<stdio.h>

15
New cards

How to guard header files?

#ifndef __FILE_H__

#define __FILE_H__

#endif

16
New cards

Write a hello world program

#include <stdio.h>
int main()
{
	printf("Hello World!\n");
	return 0;
}

17
New cards

Fill in the blanks (put commas between answers)

#....... (1) <stdio.h> 
.......... (2)
{
	printf("Welcome!\n").. (3)
}

include, int main(), ;

18
New cards

n++ vs ++n

The expression ++n increments n before its value is used, while n++ increments n after its value has been used.

(ex: int n =5;
x = n++; sets x to 5
x = ++n; sets x to 6)

19
New cards

What is a pointer?

A variable that contains the address of a variable

20
New cards

Declare a pointer with variable name: age and type: int

int *age;

21
New cards

Fill in the blank line

int myAge = 20;
//pointer age stored address of myAge
.................. 
printf("%d", *age);

int *age = &myAge;

22
New cards

Declare a pointer with variable name: name and type: char

char *name;

23
New cards

Find the error

char myName = "Maddie";
char *name = &actualName;

printf("%s", name);

There should not be a & before actualName

24
New cards

int a[10];


1. Defines an array 'a' or size 10
2. A block of 10 consecutive objects named a[0], a[1], ... , a[9]

25
New cards

Name an alternative to scanf()

gets(), fgets(), getline(), getchar(), getc(), fgetc()

26
New cards

What is the difference between strlen() and sizeof()?

Size of includes the \0 char when counting

27
New cards

strncpy(char `destination, char `source, size_t n);

Copy up to ‘n’ chars from one string to another

28
New cards

strncmp(str1, str2, n)

Compare up to ‘n’ chars between 2 strings

29
New cards

What does strcmp return if both strings are the same?

0

30
New cards

strlen()

Get length of string

31
New cards

strdup()

Duplicate a string by making another copy of it in memory

32
New cards

What library do we need to include to use atoi/atof?

<stdlib.h>

33
New cards

What does atoi() do?

Converts string to int

34
New cards

What does atof() do?

Converts string to float

35
New cards

Is there an error if functions are declared before main() but defined after main()?

No

36
New cards

Create a struct ‘person’ with attributes ‘name’ (50 chars) and ‘age’

struct person{
	char name[50];
	int age;
);

37
New cards

Set pointer ‘pInt’ to null then set it to the memory address of ‘num’

int *pInt = NULL;
pInt = &num

38
New cards

What will this output?

int number = 10;

int *numPtr = &number;
*numPtr = 22;
printf("%d\n", number);

22

39
New cards

Find the error (what does it output?)

int a[5] = {0,1,2,3,4};
    
    for (int *p = a; p < a + 5; p++){
        printf("%d\n", p);
    }

p in the print statement should be *p (currently outputs memory addresses)

40
New cards

Fill in the blanks

.......... student {
    char name[20];
    int marks;
} Student;

typedef struct

41
New cards

How do you limit the number of characters a user can input?

Put a number between % and s (e.g. %99s)

42
New cards

Write ONE line of code to get an ‘ID’ of 10 characters or less

scanf("%10s", ID);

43
New cards

Fill in the blanks (comma between)

printf("The length of (1) is (2)", col, strlen(col));

%s, %zu

44
New cards
int cmp = strcmp(myStr1, myStr2)
if (.......){
	printf("SNAP!");
}

Fill in the blank

cmp == 0

45
New cards

What is the difference between static and dynamic memory allocation?

Static memory is allocated at compile time, has a fixed size, and is automatically freed whereas dynamic memory is allocated at runtime, can change size, and must be manually freed

46
New cards

Which header file needs to be included for dynamic memory allocation?

<stdlib.h>

47
New cards

Name memory allocation functions in C

malloc(), calloc(), free() realloc()

48
New cards

malloc()

Allocates a specified number of bytes of memory and returns a pointer to the first allocated byte

49
New cards

calloc()

Similar to malloc but initialises all of the bytes allocated to zero – useful when allocating arrays of numbers or other values

50
New cards

free()

Frees the memory allocated by either of the above. You must free any memory that you allocate with malloc() or calloc()

51
New cards

realloc()

Can be used to resize a previously allocated space

52
New cards

Fill in the blank

int (1)ptr1 = malloc(size);

*

53
New cards

Fill in the blanks

newNode->(1) = pStack->top;
pStack->top = (2);

next, newNode

54
New cards

What is a graph?

A data structure that consists of a finite collection of vertices/nodes and edges

55
New cards

How are graphs represented in a computer?

As an adjacency matrix/adjacency list

56
New cards

What is an unweighted edge recorded as?

1

57
New cards

What is Breadth-First Traversal?

A graph traversal algorithm that explores all nodes at the current "level" before going through all deeper levels

58
New cards

Name 3 applications of BFS

Indexing web pages, GPS (near current location), social media friend recommendations

59
New cards

What is Depth-First Traversal?

A graph traversal algorithm that explores one node at the current "level" before going through all deeper levels of that node

60
New cards

Name 3 applications for DFS

Path finding, exploring dependencies, solving puzzles

61
New cards

What are the two main steps of implementing push operation in a stack?

top++ //increment top

stack[top] = push_value //put value in next available location

62
New cards

Steps for DFT

Choose start node (current_node)

Mark current_node as visited (node location e.g. 0 visited? Is true therefore 0 contains 1)

Find first non-zero value in row current_node. If not visited, visit.

Stack will keep record of nodes traversed.

Make next node current_node (e.g. if node 0 then node 1 becomes current_node)

Repeat from 2

63
New cards

How to backtrack during DFT?

Pop current_node to backtrack to revious node

Go through new current_node to find path

If no new path backtrack further (new current_node)

64
New cards

What does this code do?

int visited[VERTEX_NUM];
for (inti i=0; i<VERTEX_NUM; i++)
     visited[i] = 0;

//An array to store the nodes we have visited already

65
New cards

What does this code do?

66
New cards

What does this code do?

top++;
stack[top] = node;

Adds a node to the stack

67
New cards

What does this code do?

node = stack[top];
top--;

Removes a node from the stack

68
New cards
69
New cards