Skip to content

Instantly share code, notes, and snippets.

@hyunmoon
hyunmoon / gist:5ab9251f36c2b501031c
Created June 4, 2015 03:38
Traversing matrix diagonally
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n = 4; // size of rectangular 2d array
int array[4][4] = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
@hyunmoon
hyunmoon / gist:4c3ee53b9d040abfd746
Last active October 12, 2021 20:18
Rotate array
#include <stdio.h>
#include <stdlib.h>
/* rotate array to the left by n elements */
void rotate_array_left(int array[], int size, int n) {
int *tmp_array = (int*)malloc(n * sizeof(int));
memcpy(tmp_array, array, n * sizeof(int)); // temporarily save leading n elements
memmove(array, array + n, (size - n) * sizeof(int)); // shift array to the left
memmove(array + size - n, tmp_array, n * sizeof(int)); // append saved
free(tmp_array);