Matthew Thibodeau
Matthew Thibodeau

Reputation: 161

Array in C containing Incorrect Values

The goal of this program is to add the first and last elements of an array together and set that value as the first element of an output array, and then continue moving inwards as such. All of the sums will be stored in an output array. For this program, the rules stipulate that I may only use pointers and pointer arithmetic (i.e. no subscripting, no '[]', etc.) I have gotten the program to work for arrays of length 2 as well and length 4 (as I have only implemented functionality for even-lengthed arrays) however when I try any array of length 6 or above, the program adds together incorrect values that are not in the first array.

I have already tried using two different debuggers to isolate where the problem is coming from and for the life of me I can not figure it out. I have spent a few hours looking over my notes on C and going through the code, reworking it however I can. I feel as if there is something wrong with how I am interacting between the array and the pointer variables, but I am unsure. I couldn't seem to find any questions on Stack Overflow too similar to this one (yes, I looked).

void add(int *a1, int array_size, int *a2) {

    int * p;
    int * temp = (a1+(array_size-1));

    if (array_size % 2 == 0) {
        array_size = array_size/2;
        for (p = a2; p < (a2+array_size); p++) {
            *p = *a1 + *temp;
            printf("%d", *a1);
            printf(" + %d", *temp);
            a1++;
            temp--;
            printf(" = %d\n", *p);
        }
    }

}

For arrays of length 2 and 4 (again, I am only testing even numbers for now), the code works fine.

Example Output:

Enter the length of the array: 2                                                                                               

Enter the elements of the array:  1 2                                                                                          
1 + 2 = 3   
The output array is: 3

Enter the length of the array: 4                                                                                               

Enter the elements of the array:  1 2 3 4                                                                                      
1 + 4 = 5                                                                                                                      
2 + 3 = 5  
The output array is: 5 5

Now this is where it is going wrong.

When I do this:

Enter the length of the array: 6                                                                                                 

Enter the elements of the array:  1 2 3 4 5 6 

I expect:

1 + 6 = 7                                                                                                                        
2 + 5 = 7                                                                                                                        
3 + 4 = 7 

The output array is: 7 7 7

But instead, the output is:

1 + 0 = 1                                                                                                                        
2 + 3 = 5                                                                                                                        
3 + 4 = 7          

The output array is: 1 5 7  

My best guess is that something went wrong with my use of pointers or perhaps pointer syntax. Any help I can get, positive or negative, would be greatly appreciated.

This is the main() function:

int main() {

  int size = 0;
  int out_size = 0;
  int arr[size];

  printf("Enter the length of the array: ");
  scanf("%d", & size);
  printf("\nEnter the elements of the array:  ");
  for (int i = 0; i < size; i++) {
    scanf("%d", & arr[i]);
  }

  if (size % 2 == 0) {
      out_size = size/2;
  }
  else{
      out_size = ((size-1)/2) + 1;
  }

  int out[out_size];

  //calling the add function and using the addresses of arrays + array size
  add(arr, size, out);

  //iterating through and printing output array which has now been
  //altered by the move function
  printf("\nThe output array is: ");
  for (int i = 0; i < out_size; i++) {
    printf("%d ", out[i]);
  }
  return 0;
}

Upvotes: 1

Views: 1265

Answers (1)

Josu Go&#241;i
Josu Go&#241;i

Reputation: 1274

You are using an array of size 0:

int main() {
    int size = 0;
    int out_size = 0;
    int arr[size]; // <- Here is your problem

You could move the array declarations after the size reading:

int main() {
    int size = 0;
    int out_size = 0;

    printf("Enter the length of the array: ");
    scanf("%d", & size);
    int arr[size];

    printf("\nEnter the elements of the array:  ");
    for (int i = 0; i < size; i++) {
        scanf("%d", & arr[i]);
    }

Upvotes: 3

Related Questions