3

sir i am a new programmer.So i am facing some new problems which I really need to solve. Like I write a code

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

I thought the result would be 100,90,80,70,60,50,40,30,20,10 but the result is not like this...Would anyone please tell me why the result shows 100,90,80,70,60,60,70,80,90,100

8
  • 4
    Perhaps first explain why you thought the result would be "100,90,80,70,60,50,40,30,20,10" with code changing ara[] with ara[i]=ara[j];? Perhaps try this with int ara[] ={1,2,3,4}? Commented Aug 29, 2017 at 19:02
  • @chux I totally missread the question, I thought OP said that was the result and they were expecting it to be the actual result, I thought, "That makes no sense" Commented Aug 29, 2017 at 19:04
  • 3
    Step through the code with your debugger, watching the values at each step. All will become clear. Commented Aug 29, 2017 at 19:05
  • int ara_r[10];..ara_r[i]=ara[j];printf("%d\n",ara_r[i]); Commented Aug 29, 2017 at 19:06
  • 1
    @chux sir,I got it.Now it make sense.Thanks a lot sir. Commented Aug 29, 2017 at 19:17

3 Answers 3

3

After printing value 60, i.e. half of the array when you try to access elements further, actually you will access those elements which are of the locations after mid of the array. because you are assigning them using ara[i] = ara[j]..so you will end up with repetition after half of array. As:-

ara[0] = ara[9]
ara[1] = ara[8]
ara[2] = ara[7]
ara[3] = ara[6]
ara[4] = ara[5]
ara[5] = ara[4]

The simplest solution may be creating a temporary array:-

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

}

Sign up to request clarification or add additional context in comments.

Comments

1

What you are doing in this code?

reversing an array

Why are you printing 100,90,80,70,60,50,60,70,...

because when you reach the 6th one (ara[4]), you've already swapped it with 4th one (ara[6])

Comments

1

You need to save in the temporary variable the value which is written first

Some examples

char *reverse(char *str)
{
    char tmp;
    int len;

    if (str != NULL)
    {
        len = strlen(str);
        for (int i = 0; i < len / 2; i++)
        {
            tmp = *(str + i);
            *(str + i) = *(str + len - i - 1);
            *(str + len - i - 1) = tmp;

        }
    }
    return str;
}

int *reverseints(int *data, size_t size)
{
    int tmp;

    if (data != NULL)
    {
        for (int i = 0; i < size / 2; i++)
        {
            tmp = *(data + i);
            *(data + i) = *(data + size - i - 1);
            *(data + size - i - 1) = tmp;

        }
    }
    return data;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.