Home > Back-end >  How to shift array to the right only using addresses and pointers (without indexes)
How to shift array to the right only using addresses and pointers (without indexes)

Time:11-29

I have a task to shift an array of real numbers to the right by n elements, what I have succesfully done, bu then I noticed that I was supposed to use only addresses and pointers. I tried to rewrite code with addresses but it seems to not work, can someone please help me with it.

So here is the code which is working, but it uses indexes method, hope you can help me because i'm dumb and can't rewrite it properly(

#include <stdio.h>
#include <math.h>
#define N 5

void main() {

  
  double ar[N] = {1.2, 2.2, 3.3, 4.4, 5.5};
  int n; 
  int save;
  printf_s("Enter an n:");
  scanf_s("%d", &n);
  int length = sizeof(ar) / sizeof(ar[0]);

  

  while (n) {

    save = ar[N - 1];

    for (int i = N - 1; i > 0; i--)
    ar[i] = ar[i - 1];

    ar[0] = save;
    n--;
  }
  for (int i = 0; i < length; i  ) {
    printf("%f; ", ar[i]);

  }
}

CodePudding user response:

You should find lots of references to pointer incrementing for arrays. Here is one for your reference Pointer Arithmetic.

Using that for a reference, here is a tweaked version of your program replacing array indices with a pointer that gets incremented and decremented.

#include <stdio.h>
#include <math.h>
#define N 5

int main()
{

    double ar[N] = {1.2, 2.2, 3.3, 4.4, 5.5};
    double * loc = ar;                          /* Pointer to reference elements in the array */
    int n;
    float save;
    printf("Enter an n: ");
    scanf("%d", &n);
    int length = sizeof(ar) / sizeof(ar[0]);

    while (n)
    {
        save =  *(loc   N - 1);                 /* Store the value currently stored in the last array element */

        for (int i = N - 1; i > 0; i--)
        {
            *(loc   i) = *(loc   i - 1);
        }

        *loc = save;
        n--;
    }
    for (int i = 0; i < length; i  )
    {
        printf("%f; ", ar[i]);
    }

    printf("\n");

    return 0;
}

Testing this out resulted in the following terminal output.

@Dev:~/C_Programs/Console/Shift/bin/Release$ ./Shift 
Enter an n: 2
4.400000; 5.500000; 1.200000; 2.200000; 3.300000; 
@Dev:~/C_Programs/Console/Shift/bin/Release$ ./Shift 
Enter an n: 3
3.300000; 4.400000; 5.500000; 1.200000; 2.200000; 

Give that a try and see if that meets the spirit of your project.

CodePudding user response:

Swapped ar[x] with *(ar (x)) and it worked.

#include <stdio.h>
#include <math.h>
#define N 5
void main() {

    double ar[N] = { 1.2, 2.2, 3.3, 4.4, 5.5 };
    int n; //кількість позицій
    double save;
    printf_s("Enter n");
    scanf_s("%d", &n);
    int length = sizeof(ar) / sizeof(ar[0]);



    while (n) {

        save = *(ar   N - 1); 

        for (int i = N - 1; i > 0; i--) 
            *(ar   i) = *(ar   i - 1);

        *(ar   0) = save; 
        n--; 
    }
    for (int i = 0; i < length; i  ) {
        printf("%f; ", ar[i]);

    }
        }

CodePudding user response:

Here is the implementation based on your code,

#include<stdio.h>

void pointer_shift(int *a, int n) 
{
   int temp;
   temp = a[n -1];                     /* store the last element in the temp variable  */
   for(int i =n -1; i>0; i--)
    {
        *(a i) = *(a i-1);
    }
    *a = temp;
}

void displayArr(int *a, int size)
{
    printf("\nDisplay array element: ");
    for(int i = 0; i<size; i  )
    {
        printf("%d ", a[i]);
    }
}

int main()
{
    int a1[] = {100, 101, 102};
    size_t size = sizeof(a1)/sizeof(a1[0]);
    displayArr (a1, size);
    pointer_shift(a1, 3);
    displayArr (a1, size);

    return 0;

}

Output:

Display array element: 100 101 102 
Display array element: 102 100 101 
  • Related