Home > Blockchain >  How can I make the code print 1234 2341 3412 4123 while using the map container
How can I make the code print 1234 2341 3412 4123 while using the map container

Time:09-26

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h> //malloc library

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

int main(void)
{
    int r;
    int c;
    int num;
    int *map;
    int i;

    r = 4;
    c = 4;
    i = 0;
    map = malloc((r * c) * (sizeof(int)));
    num = 1;
    while (map[i] < r * c)
    {
        ft_putchar(num   '0');
        ft_putchar(' ');
        num  ;
        if (num == 5)
        {
            ft_putchar('\n');
            num = 1;
        }
        i  ;
    }
    free(map);
    return (0);
}

I got the code to output

1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4 

in a 4x4 format but I need help to make it

1 2 3 4 
2 3 4 1 
3 4 1 2 
4 1 2 3 

in a 4x4 format.

I'm sorry if this looks silly but I'm really new to coding, any help is appreciated!

CodePudding user response:

Basically, you need to rotate elements in an array left one position at a time, and then do that n-1 times for n being the length of the array.

 --------------v
 --- --- --- --- 
| 1 | 2 | 3 | 4 |
 --- --- --- --- 
     <-- <-- <--

This individual rotation broken out into a very straightforward function.

void rotate_left1(int *arr, size_t n) {
    int first = arr[0];
    for (size_t i = 1; i < n;   i) {
        arr[i-1] = arr[i];
    } 
    arr[n-1] = first;
}

In your code, please note that C does allow for variable length arrays, and your map lives in main so there is no need to dynamically allocate it unless you scale it to a point where it will not be able to live on the stack.

CodePudding user response:

I have modified your code to avoid the undefined behaviour in the loop condition you are not asking about in order that it will in fact run in my test:

This will rotate the sequence 1 2 3 4 on each output line.

int r = 4;
int c = 4;
int* map = malloc((r * c) * (sizeof(*map)));
int start = 0 ;
int num = start ;

for( int i = 0; i < r * c; i   )
{
    ft_putchar( num   '1');
    ft_putchar(' ');
    num = (num   1) % 4 ;
    if( num == start)
    {
        ft_putchar('\n');
        start = (start   1) % 4 ;
        num = start ;
    }
}
free(map);

I have removed the separate instantiation/initialisation nonsense you had - don't do that.

The solution increments the start value on each iteration and uses modulo-4 arithmetic to wrap from 3 to zero. The output is '1' rather then '0' because arithmetically it is easier to use 0 to 3 rather then 1 to 4 (it allows the use of the % modulo operator).

CodePudding user response:

The original code wasn't making use of the map array so I modified it to first populate map then just print what's in the array.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h> //malloc library

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

int main(void)
  {
  int rows = 4;
  int cols = 4;
  char *map;

  map = malloc((rows * cols) * (sizeof(char)));

  for(int r = 0 ; r < rows ;   r)
    for(int c = 0 ; c < cols ;   c)
      map[(r * cols)   c] = '0'   ((r   c) % cols)   1;

  for(int i = 0 ; i < (rows * cols) ;   i)
    {
    ft_putchar(map[i]);
    if(i % cols == (cols - 1))
      ft_putchar('\n');
    }

  free(map);
  return 0;
  }

Of course, there really isn't a need for map, and getting rid of it makes the code that much simpler:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h> //malloc library

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

int main(void)
  {
  int rows = 4;
  int cols = 4;

  for(int r = 0 ; r < rows ;   r)
    for(int c = 0 ; c < cols ;   c)
      {
      ft_putchar('0'   ((r   c) % cols)   1);

      if(((r * cols)   c) % cols == (cols - 1))
        ft_putchar('\n');
      }

  return 0;
  }
  •  Tags:  
  • c
  • Related