Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

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

0 votes
asked Mar 18 by ismail andrey (230 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;
}

6 Answers

+4 votes
answered Mar 18 by Peter Minarik (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 ismail andrey (230 points)
Thank you so much for explain me :)
0 votes
answered Mar 18 by ANMOL TIWARI (220 points)

int main()
{
    int arr[10]={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 ANMOL TIWARI (220 points)
int main()
{
    int arr[10]={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 ANMOL TIWARI (220 points)
you have do not decided limit of J.

int main()
{
    int arr[10]={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 Alok Ranjan (150 points)
missing the curly bracket
0 votes
answered 6 days ago by Cristi Rusu (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;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...