Skip to content

Instantly share code, notes, and snippets.

@yclim95
Last active April 5, 2025 11:07
Show Gist options
  • Save yclim95/a00e7b256ac3f16837779cb9649542d6 to your computer and use it in GitHub Desktop.
Save yclim95/a00e7b256ac3f16837779cb9649542d6 to your computer and use it in GitHub Desktop.
C Piscine Rush 01

So far what have done & this will most likely be the final result of our project.

  1. check for how many arguments are they (argc).
  2. Print "Error" message if user enter only 1 argument && more than 2 arguments
  3. Check if the length of the string is for (4x4)
  4. Check if the string of characters is in number format.
  5. Reserve space (4x4) for puzzle && assign 0 to all space using malloc()
  6. If puzzle is NOT NULL, print the (4x4) puzzle.
  7. At the end Free memory space using free().
  8. Norminette -R CheckSourceForbiddenHeader : main.c && extra.c && header.h

Piscine Rush01 Output

Not Done

  1. Algorithm -> If meet the condition print what
  2. Print the output

Code Explanation (header.h)

  1. #ifndef : Check if this definition does NOT EXIST
  2. #define : Define the definition
  3. #endif : End of IF

Code Explanation (extra.c)

  1. #include "header.h" : include the header file
  2. void ft_putchar(char c) {} : write a character
  3. void ft_convert_char_to_num(int nb) {} : convert character to number
  4. int ft_strlen(char ** argv) : return the number of characters from the arguments (user enter)
  5. int ft_checknum(char ** argv) : return 1 if the string characters is a number, return 0 if the string character is NOT a number
  6. void ft_free_memory(int ** puzzle) : Free the memory space created in the puzzle arrays [][]

Code Explanation (main.c)

  1. #include "header.h" : include the header file
  2. int main(int argc, char * argv[]) :
    1. the start of the program.
    2. If the argument IF is == 1 OR argument > 2, Output Error message.
    3. If the argument == 2, Call & run execute() function.
    4. Once execute() function is done , the program will end.
  3. int ** ft_create_puzzle(char ** argv)
    1. puzzle = (int ** )malloc(ROW_NUM * sizeof(int * ));
      • malloc() function: allows to reserve a memory space
      • size of (int *) : 4 byte
      • (int**) : Type cast int variable to the pointer of pointer **
      • Reserve 4 Row of memory space & assigned it to puzzle.
    2. puzzle[i] = (int * )malloc(COL_NUM * sizeof(int));
      • malloc() function: allows to reserve a memory space
      • size of (int) : 4 byte
      • (int*) : Type cast int variable to the pointer *
      • Reserve 4 Col of memory space & assigned it to puzzle.
    3. puzzle = ft_assign_puzzle(i, j, puzzle);
      • assign value of 0 to all (4x4) memory space
  4. void ft_print_puzzle(int ** puzzle)
    1. print out the (4x4) puzzle
  5. void execute(char ** argv, int ** puzzle)
    1. if ((ft_strlen(argv) % 4 == 0))
      • This check if this can be divdable by 4 with 0 remainder : to ensure (4x4)
    2. if (ft_checknum(argv))
      • check if the string of characters is in a number format
    3. if (puzzle != NULL)
      • To ensure Puzzle (4x4) there is memory space exist
    4. to control the the main execution flow of this program

Steps

  1. int main(int argc, char*argv[])
    1. if (argc == 1 || argc > 2)
      • write (1, "Error\n", 7);
    2. if (argc == 2)
      • execute(argv, puzzle);
  2. execute(char ** argv, int ** puzzle)
    1. if ((ft_strlen(argv) % 4 == 0))
      1. if (ft_checknum(argv))
        1. puzzle = ft_create_puzzle(argv);
        2. if (puzzle != NULL)
          1. ft_print_puzzle(puzzle);
          2. ft_free_memory(puzzle);
      2. else write(1, "Please enter in number format\n", 29);
    2. write(1, "Invalid dimension\n", 18);

main.c

/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   main.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: lyao-che <[email protected]>    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/04/16 07:43:37 by lyao-che          #+#    #+#             */
/*   Updated: 2022/04/17 11:12:39 by lyao-che         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "header.h"

int	main(int argc, char*argv[])
{
	int	**puzzle;

	puzzle = NULL;
	if (argc == 1 || argc > 2)
		write (1, "Error\n", 7);
	if (argc == 2)
	{
		execute(argv, puzzle);
	}
	return (0);
}

int	**ft_create_puzzle(char **argv)
{
	int	**puzzle;
	int	i;
	int	j;

	i = 0;
	j = 0;
	puzzle = (int **)malloc(ROW_NUM * sizeof(int *));
	if (puzzle == NULL)
		exit(0);
	while (i < ROW_NUM)
	{
		puzzle[i] = (int *)malloc(COL_NUM * sizeof(int));
		if (argv[i] == NULL)
		{
			write(1, "Reserving space\n", 16);
		}
		i++;
	}
	i = 0;
	puzzle = ft_assign_puzzle(i, j, puzzle);
	return (puzzle);
}

void	ft_print_puzzle(int **puzzle)
{
	int	i;
	int	j;

	i = 0;
	j = 0;
	while (i < ROW_NUM)
	{
		j = 0;
		while (j < COL_NUM)
		{
			ft_convert_char_to_num(puzzle[i][j]);
			ft_putchar(' ');
			j++;
		}
		ft_putchar('\n');
		i++;
	}
}

void	execute(char **argv, int **puzzle)
{
	if ((ft_strlen(argv) % 4 == 0))
	{
		if (ft_checknum(argv))
		{
			puzzle = ft_create_puzzle(argv);
			if (puzzle != NULL)
			{
				ft_print_puzzle(puzzle);
				ft_free_memory(puzzle);
			}
		}
		else
			write(1, "Please enter in number format\n", 29);
	}
	else
		write(1, "Invalid dimension\n", 18);
}

int	**ft_assign_puzzle(int row_counter, int col_counter, int **puzzle)
{
	while (row_counter < ROW_NUM)
	{
		col_counter = 0;
		while (col_counter < COL_NUM)
		{
			puzzle[row_counter][col_counter] = 0;
			col_counter++;
		}
		row_counter++;
	}
	return (puzzle);
}

extra.c

/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   extra.c                                            :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: lyao-che <[email protected]>    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/04/17 09:33:51 by lyao-che          #+#    #+#             */
/*   Updated: 2022/04/17 11:05:47 by lyao-che         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "header.h"

void	ft_putchar(char c)
{
	write(1, &c, 1);
}

void	ft_convert_char_to_num(int nb)
{
	if (nb < 0)
	{
		ft_putchar('-');
		nb *= -1;
	}
	if (nb < 10)
	{
		ft_putchar(nb + 48);
		return ;
	}
	else
		ft_convert_char_to_num(nb / 10);
	ft_convert_char_to_num(nb % 10);
}

int	ft_strlen(char **argv)
{
	int	i;

	i = 0;
	while (argv[1][i] != '\0')
		i++;
	return (i);
}

int	ft_checknum(char **argv)
{
	int	i;
	int	valid;

	i = 0;
	valid = 0;
	while (argv[1][i] != '\0')
	{
		if (argv[1][i] >= '0' && argv[1][i] <= '9')
			valid = 1;
		else
			valid = 0;
		i++;
	}
	return (valid);
}

void	ft_free_memory(int **puzzle)
{
	int	i;

	i = 0;
	while (i < ROW_NUM)
	{
		free(puzzle[i]);
		i++;
	}
	free(puzzle);
}

header.h

/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   header.h                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: lyao-che <[email protected]>    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/04/17 10:07:26 by lyao-che          #+#    #+#             */
/*   Updated: 2022/04/17 11:32:47 by lyao-che         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef HEADER_H
# define HEADER_H
# include <unistd.h>
# include <stdlib.h>

# define ROW_NUM 4
# define COL_NUM 4

void	ft_putchar(char c);
void	ft_convert_char_to_num(int nb);
int		**ft_create_puzzle(char **argv);
void	ft_print_puzzle(int **puzzle);
void	ft_free_memory(int **puzzle);
int		ft_strlen(char **argv);
int		ft_checknum(char **argv);
void	execute(char **argv, int **puzzle);
int		**ft_assign_puzzle(int row_counter, int col_counter, int **puzzle);

#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment