-1

I am trying to rewrite my code that takes the user input array, goes to the function and adds zeros between each number and saves it to array 2. My source code works just fine but I am having trouble trying to make it so that it uses pointer arithmetic just for the function to visit each array element, it cannot be sub scripting. What can you tell me about my code or suggestions on how to do this?

Source code:

#include <stdio.h>

void insert0(int n, int a1[], int a2[]);

int main(void) {

  int i;     
  int n;

  printf("Please enter the length of the input array: ");   
    scanf("%d", &n);

  int a[n];   
  int b[2*n];  

  printf("Enter %d numbers for the array: ", n);   
    for (i = 0; i < n; i++){     
      scanf("%d", &a[i]);
    }

  insert0(n, a,  b);

  printf("Output array:");   
    for (i = 0; i < 2*n; i++){
      printf(" %d", b[i]);   
        printf("\n");
    }
    return 0; 
}

void insert0(int n, int a[], int b[]) {

  int i, j = 0; 

  for(i = 0; i < n; i++, j+=2){    
    b[j]= a[i];    
      b[j+1] = 0; 
  }
}

My arithmetic:

   #include <stdio.h>

    void insert0(int n, int *a1, int *a2);

    int main(void) {

      int i;     
      int n;

      printf("Please enter the length of the input array: ");   
        scanf("%d", &n);

      int a1[n];   
      int a2[2*n];  

      printf("Enter %d numbers for the array: ", n);   
        for (i = 0; i < n; i++){     
          scanf("%d", &a2[i]);
        }

//not sure if this is how you call it, I've seen it called with arr
      insert0(n, a1, a2); 

      printf("Output array:");   
        for (i = 0; i < 2*n; i++){
          printf(" %d", a2[i]);   
            printf("\n");
        }
        return 0; 
    }

    void insert0(int n, int *a1, int *a2) {

      int *p;
      int j = 0;

        // I think I translated this properly     
        for(p = a1; p < a1+n; p++, j+=2){
          a2+j = a1+p;  
          //unsure how to get a2[j+1] to still work here with pointer  
            a2(j+1) = 0; 
      }
    }
user6124417
  • 69
  • 2
  • 9

3 Answers3

2

You need to dereference your pointer.

a2+j = a1+p;

Doesn't have any effect. This is saying "set the address a2 + j to the address a1 + p," which you can't do. To set the value stored at a2+j to the value stored at a1+p, you need to do this:

*(a2+j) = *(a1+p)

You need to enclose your pointer arithmetic inside parentheses, and use the dereference operator on it.

a1 + p, though, isn't the address you want. p is storing the actual address, not just the offset, so you probably just want p.

NmdMystery
  • 2,778
  • 3
  • 32
  • 60
1

Problem 1:

Here in your code :

int a1[n];   
int a2[2*n];  

printf("Enter %d numbers for the array: ", n);   

for (i = 0; i < n; i++)
{     
    scanf("%d", &a2[i]); //maybe you are using wrong array to input elements 
}
  • I think here you need to scan elements into a1 array as it's size is n and you are scanning n elements...
  • but you are scanning elements into a2 and further in the insert0() function you are rewriting the elements of a2 with help of a1 which is uninitialized...

solution:

scan elements into a1

for (i = 0; i < n; i++)
{     
    scanf("%d", &a1[i]); //scanning into `a1`
}

Problem 2:

  • Note : another point to note is that for a2[], (2*n)-1 elements are enough as you are assigning only zeroes in between the numbers i.e,

example :

1,1,1 // 3 elements

1,0,1,0,1 // (2*3)-1=5 elements after inserting

Solution :

    printf("Please enter the length of the input array: ");
    scanf("%d", &n);

    int a1[n];
    int a2[(2*n)-1];  

and finally....

but I am having trouble trying to make it so that it uses pointer arithmetic just for the function to visit each array element, it cannot be sub scripting.

Yes, sub-scripting can be used!

this way :

void insert0(int n, int *a1, int *a2)
{

    int j = 0;

    for(j=0;j<(2*n)-1; j++)
    {
        if(j%2!=0)
            a2[j]=0;
        else
            a2[j]=a1[j/2];
    }
}

So your code would be :

  #include <stdio.h>

  void insert0(int n, int *a1, int *a2);

  int main(void) 
  {

    int i;
    int n;

    printf("Please enter the length of the input array: ");
    scanf("%d", &n);

    int a1[n];
    int a2[(2*n)-1];

    printf("Enter %d numbers for the array: ", n);
    for (i = 0; i < n; i++)
    {
      scanf("%d", &a1[i]);
    }

    insert0(n, a1, a2);

    printf("Output array:");
    for (i = 0; i < 2*n-1; i++)
    {
        printf(" %d", a2[i]);
        printf("\n");
    }
    return 0;
}

void insert0(int n, int *a1, int *a2)
{

    int j = 0;

    for(j=0;j<(2*n)-1; j++)
    {
        if(j%2!=0)
            a2[j]=0;
        else
            a2[j]=a1[j/2];
    }
}

input :

3
1 2 3

output :

1
0
2
0
3
Cherubim
  • 5,287
  • 3
  • 20
  • 37
  • 1
    Very interesting how you used if else statements here. I never thought of that. Nonetheless good and very helpful reply. – user6124417 Jun 25 '16 at 18:00
0

This is a reply which is not intended to be a demo in efficiency of using pointer arithmetic, but rather to show how array indexing (subscripting) translates into pointer arithmetic, applied to your code. Please, take a look at this SO post containing valuable information from the C standard.

Your code also has few logical bugs, reading into and writing from wrong arrays, etc… You can easily find them yourself. Here's a working fix:

#include <stdio.h>

void insert0(int n, int *a1, int *a2) {
  int p;    //just a normal int for counter
  int j = 0;

    for(p = 0; p < n; p++, j+=2){
      *(a2+j) = *(a1+p);   //values pointed at are getting assigned
                           //equivalent to a2[j] = a1[p]
      if(p < n - 1 )       //we insert only between numbers
        *(a2 + j + 1) = 0; //pointer pointing at memory into which 0 is copied incremented by 1
    }
 }


int main(void) {
  int i;     
  int n;

  printf("Please enter the length of the input array: ");   
    scanf("%d", &n);

  int a1[n];   
  int a2[2*n];  //one element too long, should be a2[n+n-1];

  printf("Enter %d numbers for the array: ", n);   //reading into smaller array
    for (i = 0; i < n; i++){     
      scanf("%d", &a1[i]);
    }

  insert0 (n, a1, a2); //this is OK

  printf("Output array: ");   
  for (i = 0; i < 2*n - 1; i++){
     printf(" %d", a2[i]);              //printing from the bigger, zero-padded array
  }
  printf("\n");
  return 0; 
}
Community
  • 1
  • 1
user3078414
  • 1,942
  • 2
  • 16
  • 24
  • Would the DVer please comment to help improve the reply? Thanks – user3078414 Jun 25 '16 at 17:48
  • Your reply is solid and easily demonstrates how to use pointer arithmetic, in my opinion (a newbie to pointer arithmetic) it is straight forward and doesn't need to be improved. I'd use it and probably look back at it again in the future when I have to use pointer arithmetic. – user6124417 Jun 25 '16 at 17:58