mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add rotate-2-dimensional-array-90-degrees/c/function
This commit is contained in:
parent
b70ff28313
commit
ee6733c760
@ -0,0 +1,3 @@
|
||||
# rotate-2-dimensional-array-90-degrees/c/function
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 5 December 2021.
|
@ -0,0 +1,97 @@
|
||||
#include "array_2D_int.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "character.h"
|
||||
|
||||
void array_2D_int_print(int **array, size_t number_of_rows, size_t number_of_columns) {
|
||||
for (size_t i = 0; i < number_of_rows; i++) {
|
||||
for (size_t j = 0; j < number_of_columns; j++) {
|
||||
printf("%d", array[i][j]);
|
||||
if (j != number_of_columns - 1) {
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int **array_2D_int_input(size_t *number_of_rows, size_t *number_of_columns) {
|
||||
int **array = malloc(sizeof(int *));
|
||||
*number_of_rows = 1;
|
||||
*number_of_columns = 1;
|
||||
array[0] = malloc(*number_of_columns * sizeof(int));
|
||||
array[0][0] = 0;
|
||||
char character;
|
||||
size_t length = 1;
|
||||
char *string = malloc(length * sizeof(char));
|
||||
*string = '\0';
|
||||
while ((character = getchar()) != EOF) {
|
||||
if (character == '\n') {
|
||||
int number = atoi(string);
|
||||
array[*number_of_rows - 1][*number_of_columns - 1] = number;
|
||||
length = 1;
|
||||
memset(string, 0, length * sizeof(char));
|
||||
*string = '\0';
|
||||
*number_of_rows = *number_of_rows + 1;
|
||||
*number_of_columns = 1;
|
||||
array = realloc(array, *number_of_rows * sizeof(int *));
|
||||
array[*number_of_rows - 1] = malloc(*number_of_columns * sizeof(int));
|
||||
} else {
|
||||
if (character == ' ') {
|
||||
int number = atoi(string);
|
||||
array[*number_of_rows - 1][*number_of_columns - 1] = number;
|
||||
length = 1;
|
||||
memset(string, 0, length * sizeof(char));
|
||||
*string = '\0';
|
||||
*number_of_columns = *number_of_columns + 1;
|
||||
} else {
|
||||
length++;
|
||||
string = realloc(string, length * sizeof(char));
|
||||
character_append(string, character);
|
||||
}
|
||||
}
|
||||
}
|
||||
int number = atoi(string);
|
||||
array[*number_of_rows - 1][*number_of_columns - 1] = number;
|
||||
return array;
|
||||
}
|
||||
|
||||
int **array_2D_int_reverse_rows(int **array, size_t *number_of_rows, size_t *number_of_columns) {
|
||||
int **rotated_array = malloc(*number_of_columns * sizeof(int *));
|
||||
for (size_t i = 0; i < *number_of_columns; i++) {
|
||||
rotated_array[i] = malloc(*number_of_rows * sizeof(int));
|
||||
}
|
||||
for (size_t i = 0; i < *number_of_columns; i++) {
|
||||
for (size_t j = 0; j < *number_of_rows; j++) {
|
||||
rotated_array[i][j] = array[*number_of_rows - i - 1][j];
|
||||
}
|
||||
}
|
||||
return rotated_array;
|
||||
}
|
||||
|
||||
int **array_2D_int_rotate_90_degrees_clockwise(int **array, size_t *number_of_rows, size_t *number_of_columns) {
|
||||
int **rotated_array = malloc(*number_of_columns * sizeof(int *));
|
||||
for (size_t i = 0; i < *number_of_columns; i++) {
|
||||
rotated_array[i] = malloc(*number_of_rows * sizeof(int));
|
||||
}
|
||||
for (size_t i = 0; i < *number_of_columns; i++) {
|
||||
for (size_t j = 0; j < *number_of_rows; j++) {
|
||||
rotated_array[i][j] = array[*number_of_rows - j - 1][i];
|
||||
}
|
||||
}
|
||||
size_t number_of_rows_temp = *number_of_rows;
|
||||
*number_of_rows = *number_of_columns;
|
||||
*number_of_columns = number_of_rows_temp;
|
||||
return rotated_array;
|
||||
}
|
||||
|
||||
int **array_2D_int_rotate_90_degrees_anticlockwise(int **array, size_t *number_of_rows, size_t *number_of_columns) {
|
||||
int **result = array_2D_int_rotate_90_degrees_clockwise(array, number_of_rows, number_of_columns);
|
||||
result = array_2D_int_rotate_90_degrees_clockwise(result, number_of_rows, number_of_columns);
|
||||
result = array_2D_int_rotate_90_degrees_clockwise(result, number_of_rows, number_of_columns);
|
||||
result = array_2D_int_reverse_rows(result, number_of_rows, number_of_columns);
|
||||
return result;
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
#ifndef __ARRAY_2D_INT__
|
||||
#define __ARRAY_2D_INT__
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* @brief Prints a 2D array of integers.
|
||||
*
|
||||
* @param array
|
||||
* @param number_of_rows
|
||||
* @param number_of_columns
|
||||
*/
|
||||
void array_2D_int_print(int **array, size_t number_of_rows, size_t number_of_columns);
|
||||
|
||||
/**
|
||||
* @brief Read from stdin a 2D array (rectangle or square) of integers.
|
||||
*
|
||||
* @param number_of_rows
|
||||
* @param number_of_columns
|
||||
* @return int**
|
||||
*/
|
||||
int **array_2D_int_input(size_t *number_of_rows, size_t *number_of_columns);
|
||||
|
||||
/**
|
||||
* @brief Reverse the order of the rows of a 2D array of integers.
|
||||
*
|
||||
* @param array
|
||||
* @param number_of_rows
|
||||
* @param number_of_columns
|
||||
* @return int**
|
||||
*/
|
||||
int **array_2D_int_reverse_rows(int **array, size_t *number_of_rows, size_t *number_of_columns);
|
||||
|
||||
/**
|
||||
* @brief Rotate a 2D array of integers by 90 degrees clockwise.
|
||||
*
|
||||
* @param array
|
||||
* @param number_of_rows
|
||||
* @param number_of_columns
|
||||
* @return int**
|
||||
*/
|
||||
int **array_2D_int_rotate_90_degrees_clockwise(int **array, size_t *number_of_rows, size_t *number_of_columns);
|
||||
|
||||
/**
|
||||
* @brief Rotate a 2D array of integers by 90 degrees anticlockwise.
|
||||
*
|
||||
* @param array
|
||||
* @param number_of_rows
|
||||
* @param number_of_columns
|
||||
* @return int**
|
||||
*/
|
||||
int **array_2D_int_rotate_90_degrees_anticlockwise(int **array, size_t *number_of_rows, size_t *number_of_columns);
|
||||
|
||||
#endif
|
@ -0,0 +1,10 @@
|
||||
#include "character.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void character_append(char* string, char character) {
|
||||
size_t length = strlen(string);
|
||||
string[length] = character;
|
||||
string[length + 1] = '\0';
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
#ifndef __CHARACTER__
|
||||
#define __CHARACTER__
|
||||
|
||||
/**
|
||||
* @brief Append a character to a string, assuming string points to an array
|
||||
* with enough space.
|
||||
*
|
||||
* @param string
|
||||
* @param character
|
||||
*/
|
||||
void character_append(char* string, char character);
|
||||
|
||||
#endif
|
@ -0,0 +1,19 @@
|
||||
#include "input.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "character.h"
|
||||
|
||||
char* input() {
|
||||
char character;
|
||||
size_t length = 1;
|
||||
char* string = malloc(length * sizeof(char));
|
||||
*string = '\0';
|
||||
while ((character = getchar()) != '\n' && character != EOF) {
|
||||
length++;
|
||||
string = realloc(string, length * sizeof(char));
|
||||
character_append(string, character);
|
||||
}
|
||||
return string;
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
#ifndef __INPUT__
|
||||
#define __INPUT__
|
||||
|
||||
/**
|
||||
* @brief Read a line from stdin.
|
||||
*
|
||||
* @return char*
|
||||
*/
|
||||
char* input();
|
||||
|
||||
#endif
|
@ -0,0 +1,26 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "array_2D_int.h"
|
||||
#include "input.h"
|
||||
|
||||
int main() {
|
||||
size_t number_of_rows = 0;
|
||||
size_t number_of_columns = 0;
|
||||
char *direction = input();
|
||||
int **array = array_2D_int_input(&number_of_rows, &number_of_columns);
|
||||
|
||||
if (strcmp(direction, "clockwise") == 0) {
|
||||
array = array_2D_int_rotate_90_degrees_clockwise(array, &number_of_rows, &number_of_columns);
|
||||
} else {
|
||||
array = array_2D_int_rotate_90_degrees_anticlockwise(array, &number_of_rows, &number_of_columns);
|
||||
}
|
||||
array_2D_int_print(array, number_of_rows, number_of_columns);
|
||||
|
||||
for (size_t i = 0; i < number_of_rows; i++) {
|
||||
free(array[i]);
|
||||
}
|
||||
free(array);
|
||||
free(direction);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
FROM node:16.13.0
|
||||
FROM node:16.13.1
|
||||
COPY ./ ./
|
||||
CMD ["node", "solution.js"]
|
||||
|
@ -1,4 +1,4 @@
|
||||
FROM rust:1.56.1
|
||||
FROM rust:1.57.0
|
||||
COPY ./ ./
|
||||
RUN rustc solution.rs
|
||||
CMD ["./solution"]
|
||||
|
@ -1,4 +1,4 @@
|
||||
FROM node:16.13.0
|
||||
FROM node:16.13.1
|
||||
RUN npm install --global ts-node typescript @types/node
|
||||
COPY ./ ./
|
||||
CMD ["ts-node", "solution.ts"]
|
||||
|
Loading…
Reference in New Issue
Block a user