1

Given a array x with n integer components, write functions that allow performing the following operation: Carry out the circular permutation of the given array

I tried to do without poiters for the code, as it ends up with only arrays

I think the problem is that the array in the pcircular is lost and can't pass the value to the write function

Note: for array > 6 it will not work

#include <stdio.h>

int ArraySize(){   // get the size of the array
    int size;
    printf("What's the array size? ");
    scanf("%d", &size);
    return size;
}

void readArray(int size, int array[]){ /*put the values ​​in the first array*/
    for (int i = 0; i < size; i++){
        printf("What is the \033[1;36m%d\033[m value? ", i+1);
        scanf("%d", &array[i]);
    }
}
void pcircular(int size, int array[size][size]){ //Circular permutation function
    for (int j = 0; j <= size; j++){
        /* printf("("); */
        for (int i = 0; i < size; i++){
            if (i == size - 1){
                array[j+1][0] = array[j][i];
                /* printf("%d", array[j+1][0]); */
            }
            else{
                array[j+1][i+1] = array[j][i];
                /* printf("%d", array[j+1][i+1]);
                printf(", "); */
            }
        }
        /* printf(")"); */
        
    }
    
} 
void writeArray(int size, int array[size][size]){ //Write the Array
    for (int i = 0; i <= size; i++){
        printf("(");
        for (int j = 0; j < size; j++){
            /* printf("\ni = %d j = %d\n", i, j);  */
            printf("%d", array[i][j]);
            if (j != size-1){
                printf(", ");
            }
        }
        printf(")");
    }
    
}

void main(){
    int size = ArraySize();
    int list[size][size];     // create the array
    readArray(size, list[0]);
    pcircular(size, list);
    writeArray(size, list);
}

Input:

What's the array size? 6

What is the 1 value? 1

What is the 2 value? 2

What is the 3 value? 3

What is the 4 value? 4

What is the 5 value? 5

What is the 6 value? 6

Expected Output: (1, 2, 3, 4, 5, 6)(6, 1, 2, 3, 4, 5)(5, 6, 1, 2, 3, 4)(4, 5, 6, 1, 2, 3)(3, 4, 5, 6, 1, 2)(2, 3, 4, 5, 6, 1)(1, 2, 3, 4, 5, 6)

Real Output: (

  • What is your question? If you need help fixing the code, could you copy and paste the text of a shell session into your question, showing us what input you gave and what the output was? Also we would need to know what the expected output is. P.S. `read` and `write` are standard system calls in Linux, so it's best not to define your own things with the same names. – David Grayson Nov 07 '22 at 19:04
  • Not sure, but having `int size() { int size; .... }` seems very confusing. – unwind Nov 07 '22 at 19:23
  • I made some changes and added the output and input – benno vasconcellos Nov 07 '22 at 19:36
  • The output you showed seems like it might be fine. What's wrong with it? Will you show us the expected output and the actual output and clearly label which one is which? – David Grayson Nov 07 '22 at 19:37
  • I think the problem is that the array in the pcircular is lost and can't pass the value to the write. – benno vasconcellos Nov 07 '22 at 19:50
  • in pcricular you are going over size + 1, means initialize your array int list[size + 1][size +1]; and will be fine, even though your code has other bugs , but will work – amirhm Nov 07 '22 at 20:44
  • you are very welcome, notice that I wrote the pcircular to see you could write that function in more safe way. – amirhm Nov 07 '22 at 20:57

1 Answers1

1

write your pcircular function as follow:

void pcircular(int size, int array[size][size]){ //Circular permutation
    for (int j = 1; j <= size; j++){
        for (int i = 0; i < size; i++){
            array[j][i ] = array[j - 1][(i + 1) % size];
        }        
    }
}

but the main problem is here:

int list[size + 1][size];     // create the array

since you are creating one more row than the number, indeed the last row is copy of the first row.

if you dont change the pcircular function, and use your version, you should as well add to second dimention too.

amirhm
  • 1,239
  • 9
  • 12