Created
February 17, 2018 18:54
-
-
Save ayaderaghul/c7bc4cafb41696856c6145e00de7779f to your computer and use it in GitHub Desktop.
sudoku: take input and print the matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
void ft_putchar(char c) | |
{ | |
write(1, &c, 1); | |
} | |
int length(char *str) | |
{ | |
int i; | |
i = 0; | |
while (str[i]) | |
++i; | |
return (i); | |
} | |
// check if a cell contains a number? if yes, return the number, if no, return 0 | |
char check_number(char c) | |
{ | |
return((('0' <= c) && (c <= '9')) ? (c - '0') : 0); | |
} | |
// put a string ".....9..." into an array (which is supposed to be a line) | |
void put_in_array (int i, char *str, char **array) | |
{ | |
int j; | |
j = 0; | |
while (j < 9) | |
{ | |
array[i][j] = check_number(str[j]); | |
++j; | |
} | |
} | |
// print the array line by line | |
void print_array(char **arr) | |
{ | |
int x; | |
int y; | |
y = 0; | |
while (y < 9) | |
{ | |
x = 0; | |
while (x < 9) | |
{ | |
ft_putchar(arr[y][x] + '0'); | |
if (x < 8) | |
ft_putchar(' '); | |
x++; | |
} | |
ft_putchar('\n'); | |
y++; | |
} | |
} | |
// convert input into a nested array, print the matrix | |
void convert_input(int argrc, char **argrv) | |
{ | |
int i; | |
char **array; | |
array = (char**)malloc(9 * sizeof(char*)); | |
i = 1; | |
while (i < argrc) | |
{ | |
array[(i - 1)] = malloc(9 * sizeof(char)); | |
put_in_array((i - 1), argrv[i], array); | |
++i; | |
} | |
print_array(array); | |
} | |
// main | |
int main(int argrc, char **argrv) | |
{ | |
if (argrc != 10) | |
{ | |
printf("Error\n"); | |
} | |
else | |
{ | |
int i; | |
int l; | |
i = 1; | |
l = 0; | |
while (i < argrc) | |
{ | |
l = l + length(argrv[i]); | |
++i; | |
} | |
if (l != 81) | |
printf("Error\n"); | |
else | |
convert_input(argrc, argrv); | |
return (0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment