Ara-C Programing

Ara-C Programing
0

Intention is, there’s 10 elements inside ara and we’ve to inverse this, mean~last to first.
I already know from my programing book that,there’s a bug inside code but what is bug? and how to debug it?

Here is my code----


#include <stdio.h>
int main()
{
    int ara[] = {10,20,30,40,50,60,70,80,90,100};
    int i,j,temp;
    
    for(i=0,j=9;i<10;i++,j--){
        temp = ara[j];
        ara[j] = ara[i];
        ara[i] = temp;
    }
    
    for(i=0;i<10;i++){
        printf("%d\n",ara[i]);
    }
    
    return 0;
}

Anyone Help me, please :slight_smile:

the loop should be carried out only 5 times,
if you carry it out ten times (as you have), it reverses the array (to the your desired output) and then reverses it back to its initial state.
The bug that your programming book was talking about was a logical one in which your were supposed to look at the code and realise that it is reversing the array twice.
Here is a fix:

#include <stdio.h>

int main()
{
    int ara[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
    int i, j, temp;

    for (i = 0, j = 9; i < 4; i++, j--)
    {
            temp = ara[j];
            ara[j] = ara[i];
            ara[i] = temp;
    }

    for (i = 0; i < 10; i++)
    {
        printf("%d\n", ara[i]);
    }

    return 0;
}

I hope my explanation helps you see the logical error in the code you had implemented:+1:

1 Like

Can u please more simplify this or suggest me some video that help me to learn about this more deeply?