(If you're reading this, I apologize this README.md is chaotic - they're my personal notes while studying for exam rank 02)
EXAM PREP:
**DEBUG WITH \0
int main(int argc, char **argv)
{
char *s = argv[1];
if (argc == 2)
rev_print(argv[1]);
write(1, "\n", 1);
while (*s)
{
printf("%c (%d)\n", *s, *s);
s++;
}
printf("\\0 (%d)\n", *s); //This line should output 0 as the ascii value. Any different will imply an error or misinterpretation of the \0 handling in the code
return (0);
}
- Account for:
- *tmp vs tmp
int tmp;
doesn't need a pointer because it temporarily stores a regular integer.- *a and *b are pointers because the values they point to, can be modified.
- Account for:
putchar_times(char c, int n)
- formula for
n
:- LC:
(*s + 1) - 'a'
- UC:
(*s + 1) - 'A'
- LC:
\
integer division, yields the quotient without the remainder.- result will be 0, if the dividen is smaller than the divisor.
%
modulo, yields the remainder, not the quotient.- result will be 0 if division is even.
- result will be the dividend, if it is smaller than the divisor.
- Account for:
- UC and LC
*s = (((*s - 'A' + key) % 26) + 'A')
- Account for:
int digit;
stores the digit, one at a time- Use recursion,
if()
loop with(n / 10)
to "descend" through, and removing each digit, from the rightmost, to the leftmost. - At base case, when
n < 9
,digit
is updated to the ASCII value of the char represenation of the digit, e.g.1
becomes"1"
which is49
in ASCII. - Print the ASCII value (e.g. integer
49
) at the address of &digit. - Go back to each recursive call, printing the ASCII rightmost digit of
n
until all digits have printed, from leftmost, to rightmost.
- Remember, it only converts the inital portion of a string: "*&!+12345. 6789" will print "12345".
- Account for:
int sign;
for negative valuesint result;
initialized to0
for the formula to work, and stores the converted value- Skip leading whitespace between 9 and 13, and including
' '
and'+'
. - Search for chars that represent digits.
- If found, convert the chars to integers.
res
will be an accumulated value starting fromres = 0
.res = (res * 10) + (*s++ - '0');
- If no digits found, return (0).
- Account for:
int value
to store digit value of current character when iterating through each character.- Check for each character, if digit, LC or UC. Formula of
value
for each: 3. digit:*s - ‘0’
4. LC:*s - ‘a’ + 10
5. UC:*s - ‘A’ + 10
- Where10
represents the base the assignment asks us to convert to. - Break program if character is not digit, UC or LC, or
- if value is >= the base.
- Result formula:
res = (res * base) + value
, the same as ft_atoi()
- Account for:
abs()
to get the absoloute value oflen = (end - start) + 1
when end is a negative number.temp
variable to iterate the array, and preserve the pointer to the begining ofarray
.- Use
len-- > 0
to iterate in either case: 4. Check if range increases,start <= end
5. Else the range decreases, in which case,start
value decreases. - We're printing from the leftmost to the right, hence
tmp++ = start++/--
- Is the same, except in reverse
- Everything the same except:
len = start - end
end
decrements or increments depending ifend
increases or dereases range. Recall, we're printing from the rightmost to left, hencetmp = end
- Need to find the lowest common multiple.
- Check conditions for returning 0.
n
variable to store possible common multiples- assign to
n
the larger value betweena
andb
- Create infinite loop,
while (1)
(because non-zero values always true) 5. incrementn
until a common multiple is found 6. wheren
is divisble by botha
andb
with no remainders
- Account for:
len_digits()
len
variable to return
- account for a negative number
- account for
0
- iterate through each digit to retrieve the length?
n /= 10
untiln == 0
ft_itoa()
str
variable to return
- account for longer integer types which int cannot cover
- length of number is needed to allocate enough memory for the comverted string
- account for negative numbers, both in value, and in the string
- account for if number is
0
- assign the digit value, backwards for each string character. Formula:
*(str + len - 1) = n % 10 + '0'
n /= 10
- decrement
len
, assigning each digit, backwards.
- Testing:
- Declare individual nodes
- allocate memory for each node
malloc(sizeof(t_list)
- assign the data
- assign the next pointers
- printf the list function or the list data
- free all the nodes
- allocate memory for each node
- Declare individual nodes
- Account for 1.
- Account for:
- current pointer
- previous pointer = NULL, to update the next pointer of the previous node when a node is removed
- while current list
- check if current->data and data_ref is = 0
5. check if current is head node, if previous pointer = NULL
- update headnode to skip to the current's next node before removal
*begin_list = current->next
6. else not headnode - update previous's next node to skipprev->next = current->next
7. free(current) after removal (via skipping) 8. resetcurrent = prev
prev = current
too keep track of previous before future removalcurrent = current->next
move to the next node
- Think about ft_swap()
- Account for:
int temp
to store values while nodes are swapped*head = lst
keep track of top nodewhile (lst->next)
iterate 4. check against*cmp()
if the current data, and thelst->next->data
== 0 for false 5. if so, do the swap the data: - tmp = current - current = next - next = tmp -lst = head
update top node pointer 6. if not, do the skip -lst = lst->next
lst = head
ensure it points to the top of sorted list before returning
- Each bit's worth:
- In binary, each bit is worth
n
to the power of 2:2^n
n
is the position of the bit from the rightmost bit to the leftmost.1
is "on",0
is "off".
- In binary, each bit is worth
- Operators:
&
"AND" results in1
if both bits are1
, else it'll result in0
.|
"OR" results in1
if either or bits is1
.~
"NOT" flips the bits1
s to0
s, making the binary number a "two's complement", where the leftmost bit idicates a positive0
or negative1
.^
"XOR" results in1
if either bit is1
, and0
if both bits are the same, e.g. both are1
or0
.<<
shifts the bits left, filling in0
s from the right.>>
shifts the bits right, filling in0
s from the left.
- Compound assignment:
- Each operator can be used with
=
e.g.a &= b
, equivalent toa = a & b
.
- Each operator can be used with
- Bitmask:
- 15 = 0000 1111, but can also use >> 4
- 240 = 1111 0000, but can also use << 4
while (bit--)
- Prime numbers:
- A number greater than 1, but is not the product of multiplying two smaller numbers:
2, 3, 5, 7, 11, 13, ...
. - Has only two distinct positive divisors: 1 and itself.
- A number greater than 1, but is not the product of multiplying two smaller numbers:
- Composite numbers:
- A number that has divisors other than 1 and itself, and can be divided evenly:
4, 6, 8, 9, 10, 12, ...
.
- A number that has divisors other than 1 and itself, and can be divided evenly:
- Prime factors:
- Are the prime numbers that divide a given number exactly, without leaving a remainder.
- For example, the prime factors of 12 are 2, 2, and 3, because 2 * 2 * 3 equals 12.
- The prime factors of a number are the building blocks that, when multiplied together, give the original number
- Are the prime numbers that divide a given number exactly, without leaving a remainder.
- Re prime numbers.
ft_atoi()
to convertargv[1]
.ft_putnbr()
to print the sum result.char digit
if()
recursion
is_prime(unsigned int)
to check for prime numbers:- Account for:
i
initialised to2
to look for factors (n divisible by i with no remainder)- Check if n is 1 or negative, not a prime, return (0)
- Iterate from i * i to n, return (0) if
n % i == 0
- Return (1) if n is prime.
- Account for:
add_prime_sum()
to add all the prime of a number:- Account for:
unsigned int res
to store converted atoiunsigned int sum
to store the sum result, initialised to0
for accumulationunsigned int i
to iterate. Initialised to2
, the smallest prime- Iterate from 2 to res, checking for primes, and accumulate sum
- print
sum
at the end of the iterations.
- Account for:
- Re prime factors.
int i
to iterate. Initialised to2
, the smallest prime- ensure
1
is printed ifn == 1
- Loop until
n = 1
because any number multiplied by 1 is valid. - if
n % i == 0
, it is divided evenly, and we print the first prime factor. - if
n /= i > 1
, thenn
needs to be multiplied by another prime factor, another building block, to give us the original number.- print the
*
before retrieving the next prime factor.
- print the
- increment
i
if needed, to find the next prime factor where there are no remainders. - ensure
fprime()
is called when input is a positive number.
- Account for:
ft_atoi()
since argv is type char- if n > 16
print_hex()
recursively calls itself withn / 16
becuase, 'base 16' char *hex
that contains the base 16 values- write hex[n % 16]
- lowest power of 2 is 1
- Always check if n is 0
- Use recursion.
- Account for:
- variable
target
to store the begin points of the array, e.g.tab[x][y]
fill()
helper function 3. check each x and y points are within ranges>= 0 to size
&& check if current points== target
4. if conditions met, assign the current points the character to be replaced with 5. recursively call itself with: 1.x + 1, y
filling the right of x 2.x - 1, y
filling the left of x 3.x, y + 1
filling the top of y 4.x, y - 1
filling the bottom of y- Call
fill()
with the begin points
- variable
- Caution where x and y is placed, e.g., must be
tab[y][x]
nottab[x][y]
- Account for:
ft_strlen()
and use len-- to iterate backwardsint end
to track last character of a wordint start
to tract first characterint flag
which =start
to keep track of the last word.0
implies the end of the string.- Make sure the
\0
isn't printed.
- Account for:
malloc
first word to print last- Length of first word
- Temp variables for original string and for first word to preserve pointers and be able to iterate NOTE: Order of executions
- Skip leading whitespace and "discard"
- Check for first word and length
- Iterate through remaining string, and again, caution re order of conditions
- for an word ending character, print 1 space, remember the '\0' at end of string, need space also
- else write the character as is
- Print the first word
- Account for:
word_count()
ft_split()
- word count
- **result array
- array index [i]
- skip leading whitespace
- Break if end of string
- word start index
- word length
- allocate memory of word in array[i]
- copy current word
array[i][word] = start[word]
- null terminate word, move to next
reverse_bits
- COMPOUND ASSIGNMENT
- unsigned char res = 0; int i = 8;
- while (i--) 3. res <<= 1; 4. res |= octet & 1; 5. octet >>= 1;
ft_strrev
- SWAP
- char *start; *end; tmp;
- while (*end)
- end++;
- end--;
- while (start < end)
- tmp = *end;
- swap
print_bits
- int i = 8;
- while (i--) 3. if octet >> i & 1 - write "1" 4. else - write "0"
add_prime_sum
- HELPER FTS
- ft_atoi(simple);
- put_nbr(int);
- char digit
- if n > 9 recursion(n /10);
- digit = n % 10 + '0'
- write &digit
- int is_prime(int):
- int i = 2;
- return 0 if n <= 1;
- while (i * i <= n)
- if n % i == 0 return (0) i++;
- return (1);
- add_prime_sum(int n)
- int sum = 0; i = 2;
- while i <= n
- if is_prime(i)
- sum += i;
- i++;
- if is_prime(i)
ft_atoi_base
- LIKE ATOI (BASE 10) BUT HERE, BASE <= 16
- int res = 0; int sign = 1; int value;
- if '-', sign = -1, s++;
- while (s) 4. if digit - value = *s - '0'; 5. if LC - value = *s - 'a' + 10; 6. if UC - value = *s - 'A' + 10; 7. else invalid - break ; 8. if value >= base - break ; 9. res = (res * base) + value;
ft_rrange
- int len = abs(start - end) + 1; opposit of ft_range
- int *array = malloc
- int *temp = array;
- if start <= end
- while len-- > 0;
- *tmp++ = end--;
- else
- while len-- > 0;
- *tmp++ = end++;
pgcd
- SWAP
- unsigned int gcd(unsigned int a, unsigned int b) 2. unsigned int tmp; 3. while b != 0 - tmp = b; - b = a % b; - a = tmp; 4. return a;
- in int main(), first check if argc != 3, then print \n and return 1
sort_list
- SWAP
- int tmp; t_list *head = lst;
- while (list->next) 3. if cmp() first two nodes - tmp = lst->data; - swap - lst = head; 4. else - skip to next node
- lst = head;
ft_list_remove_if
- t_list *curr = *begin_list; t_list *prev = NULL;
- while curr 3. if cmp() 4. if prev == NULL, curr is head - skip to next node 5. else not head - skip curr node: prev->next = curr->next; 6. prev = curr; 7. curr = curr->next
sort_int_tab
- SWAP
- unsigned int i = 0; int tmp;
- while i < size - 1 3. if tab[i] > tab [i + 1] - temp = tab[i]; - swap - reset i = 0; 4. else i++;