# I want to print ara's opposite value uses temp. Please solve this

0 votes
asked Mar 18
#include <stdio.h>                 // I want to print ara's opposite value uses temp
int main()

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

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;
}

## 6 Answers

+4 votes
answered Mar 18 by (22,320 points)
selected Mar 20 by ismail andrey

Best answer

# Problems With Your Code

I had a look at your code and discovered a few problems.

## Missing Opening Curly Brace

In your (1st) for loop your missing the the opening curly brace ({), hence the code does not compile.

## Nothing Changes

When the above problem is fixed and the code runs the expected output is not the list of numbers backwards (that's what you're after, right?), but the original list, the numbers in the same order.

Why is that?

The problem is that when you loop through your numbers to swap them from the beginning (i) with the end (j) you keep doing it until i reached the end and j the beginning. Which means, you swapped

• 0 with 9,
• 1 with 8,
• 2 with 7,
• 3 with 6,
• 4 with 5

and instead of stopping here, you kept going and you swapped again

• 5 with 4,
• 6 with 3,
• 7 with 2,
• 8 with 1,
• 9 with 0.

And swapping twice leaves everything in place as if nothing happened.

So the right thing to do is stop at halfway down the array. ;)

# A Solution

Here's a fixed solution to the problem:

```#include <stdio.h>

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

for (i = 0, j = 9; i < 5; 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;
}```
commented Mar 20 by (230 points)
Thank you so much for explain me :)
0 votes
answered Mar 18 by (220 points)

# int main(){    int arr={1,2,3,4,5,6,7,8,9,7};    int i,j;    for(int j=9;j>=0;j--)    {        printf("%d",arr[j]);    }

return 0;
}

0 votes
answered Mar 18 by (220 points)
int main()
{
int arr={10,20,30,40,50,60,70,80,90,100};
int i,j;
for(int j=9;j>=0;j--)
{
printf("%d",arr[j]);
}

return 0;
}
0 votes
answered Mar 18 by (220 points)
you have do not decided limit of J.

int main()
{
int arr={10,20,30,40,50,60,70,80,90,100};
int i,j;
for(int j=9;j>=0;j--)
{
printf("%d",arr[j]);
}

return 0;
}
0 votes
answered Mar 31 by (150 points)
missing the curly bracket
0 votes
answered 6 days ago by (140 points)
#include <stdio.h>                 // I want to print ara's opposite value uses temp
int main()

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

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;
}