Home > Mobile >  My C function to reverse an array fills the first and last index with random numbers after consecuti
My C function to reverse an array fills the first and last index with random numbers after consecuti

Time:11-23

I've created a function to reverse an array in C. The first run of the function will reverse the array perfectly. Runs following that will fill the first and/or last index of the array with seemingly random info. I'm assuming this may have to do with the array accidentally receiving information from surrounding memory addresses after several runs.

My function is written as follows:

 void reverse_array(int array[], int* result, int size) {

    int index = 0;
    int reverse_index;

    for (reverse_index = size - 1; reverse_index > 0; reverse_index--) {
        result[index] = array[reverse_index];

        index  ;
      }
  } 

Running the following code with "nums" being an array filled with integers 0 to 99, the expected output is returned:

int main(void) {

    int size = 100;
    int nums[size];

    int i;
    for (i = 0; i < size; i  ) {
        nums[i] = i;
    }

    int reversed[size];
    reverse_array(nums, reversed, size);

    print_array(reversed);

    return 0;
}

The problem arises when I try and run the function several times consecutively. In the following code, I attempt to flip an array several times:

int main(void) {

    int size = 100;
    int nums[size];

    int i;
    for (i = 0; i < size; i  ) {
        nums[i] = i;
    }

    int reversed[size];
    reverse_array(nums, reversed, size);

    int reversed2[size];
    reverse_array(reversed, reversed2, size);

    int reversed3[size];
    reverse_array(reversed2, reversed3, size);

    int reversed4[size];
    reverse_array(reversed3, reversed4, size);

    int reversed5[size];
    reverse_array(reversed4, reversed5, size);

    print_array(reversed5, size);

    return 0;
}

Printing out the second flip, or "reversed2," returns the following:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 32760,

The output is almost exactly as to be expected, except for the final index. This happens with all following flips, though with different values. Printing the 5th flip, or "reversed5," yields the following:

0, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 
79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 
59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 
39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20,
19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 32760,

I'm very new to C, and can only guess what is causing this. As stated above, I think it may have to do with the array accidentally picking up information from surrounding memory addresses, but that's only a guess. Thanks for reading, any help is greatly appreciated.

Edit: Thank you all very much. I could not ask for more straight forward and helpful responses. I feel kind of dumb now, but that's part of the learning process.

CodePudding user response:

for (reverse_index = size - 1; reverse_index > 0; reverse_index--)

Your problem is with this line of code,this > should be changed to >=.

  • Related